iaik.me.security
Class BigInteger

java.lang.Object
  |
  +--iaik.me.security.BigInteger

public class BigInteger
extends Object

This class implements arbitrary precision long integer arithmetic. Its API is mostly identical to that of the java.math.BigInteger class, however there are limitations. This class is designed to be used for cryptographic applications, features not required for these applications have not been implemented. In particular:

  1. This class does not support negative numbers. Numbers zero and larger are sufficient for cryptographic applications, but care may have to be taken that intermediate results are not negative.
  2. A number of methods from the java.math.BigInteger class are not available.
  3. This class does not implement the interfaces the JDK class does, it is currently not compatible with Number, Comparable, and cannot be serialized.
ATTENTION! This class was implemented for use with CLDC and MIDP. It DOES NOT use native functions.


Field Summary
static BigInteger ONE
          BigInteger constant 1.
static BigInteger TWO
          BigInteger constant 2.
static BigInteger ZERO
          BigInteger constant 0.
 
Constructor Summary
BigInteger(byte[] val)
          Create BigInteger from 2's complement representation.
BigInteger(int signum, byte[] val)
          Create BigInteger from sign and magnitude representation.
BigInteger(int numBits, SecureRandom rndSrc)
          Create a random BigInteger which is exactly numBits long.
BigInteger(String val)
          Create a BigInteger from a decimal String representation.
BigInteger(String val, int radix)
          Create a BigInteger from a String representation in the specified radix (2 <= radix <= 36).
 
Method Summary
 BigInteger abs()
          Return the absolute value of this.
 BigInteger add(BigInteger val)
          Return this + val.
 int bitCount()
          Return the number of one bits in the binary complement representation of this number.
 int bitLength()
          Return the index of the highest bit set.
 int compareTo(BigInteger val)
          Compares this BigInteger to another
 BigInteger divide(BigInteger m)
          Calculate this divided by m.
 BigInteger[] divideAndRemainder(BigInteger val)
          Divide this by val and return the result and the remainder.
 boolean equals(Object obj)
          Test two objects for equality.
 BigInteger gcd(BigInteger m)
          Return the greatest common divisor of this and m.
 int getLowestSetBit()
          Get the index of the lowest bit set.
 int hashCode()
          Return a hash code for this object.
static boolean initNative(boolean verbose)
          Initialize the native code.
 int intValue()
          Return the value of this as a int.
 long longValue()
          Return the value of this as a long.
 BigInteger max(BigInteger val)
          Return the larger number of this and val.
 BigInteger min(BigInteger val)
          Return the smaller number of this and val.
 BigInteger mod(BigInteger m)
          Calculate this mod m.
 BigInteger modInverse(BigInteger m)
          Return the modular inverse of this with respect to m.
 BigInteger modPow(BigInteger exponent, BigInteger m)
          Compute thisexponent modulo m.
 BigInteger multiply(BigInteger val)
          Return this * val.
 BigInteger setBit(int n)
          Set the bit at the given position.
 BigInteger shiftLeft(int n)
          Create a new BigInteger which is this*2n.
 BigInteger shiftRight(int n)
          Create a new BigInteger which is this*2-n.
 int signum()
          Return the signum of this number.
 BigInteger subtract(BigInteger val)
          Return this - val.
 boolean testBit(int n)
          Test if the bit at the given position is set.
 byte[] toByteArray()
          Return a byte array representation of this, MSB first.
 byte[] toByteArray(boolean leadingZero)
          Return a byte array representation of this, MSB first.
 String toString()
          Return a base 10 string representation of this.
 String toString(int radix)
          Return a string representation of this in the given radix.
static BigInteger valueOf(long val)
          Create a BigInteger from the specified long value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final BigInteger ZERO
BigInteger constant 0.

ONE

public static final BigInteger ONE
BigInteger constant 1.

TWO

public static final BigInteger TWO
BigInteger constant 2.
Constructor Detail

BigInteger

public BigInteger(int signum,
                  byte[] val)
           throws NumberFormatException
Create BigInteger from sign and magnitude representation.
Parameters:
signum -  
val -  
Throws:
NumberFormatException -  

BigInteger

public BigInteger(byte[] val)
Create BigInteger from 2's complement representation.
Parameters:
val - 2's complement repr. as byte array

BigInteger

public BigInteger(int numBits,
                  SecureRandom rndSrc)
Create a random BigInteger which is exactly numBits long.
Parameters:
length - of the new BigInteger number
source - of the random number

BigInteger

public BigInteger(String val)
           throws NumberFormatException
Create a BigInteger from a decimal String representation.
Parameters:
BigInteger - as String value
Throws:
NumberFormatException - if the characters are not between 0 and 9

BigInteger

public BigInteger(String val,
                  int radix)
           throws NumberFormatException
Create a BigInteger from a String representation in the specified radix (2 <= radix <= 36).
Parameters:
BigInteger - as string value
radix -  
Method Detail

initNative

public static boolean initNative(boolean verbose)
Initialize the native code. This functions remains in this class for campatibility reasons only
Parameters:
verbose - mode
Returns:
true if native methods are available, is always false in this version

valueOf

public static BigInteger valueOf(long val)
Create a BigInteger from the specified long value.
Parameters:
long - value representation of the new BigInteger number

compareTo

public int compareTo(BigInteger val)
Compares this BigInteger to another
Parameters:
BigInteger - to be compared to
Returns:
0 if both BigIntegers are identical, returns -1 if this BigInteger < the other BgInteger else it returns +1

shiftRight

public BigInteger shiftRight(int n)
Create a new BigInteger which is this*2-n.

shiftLeft

public BigInteger shiftLeft(int n)
Create a new BigInteger which is this*2n.

bitLength

public int bitLength()
Return the index of the highest bit set. For this method only the lowest bit is defined as bit 1, ZERO return 0.

testBit

public boolean testBit(int n)
Test if the bit at the given position is set. LSB is bit number 0.

setBit

public BigInteger setBit(int n)
                  throws ArithmeticException
Set the bit at the given position.

divideAndRemainder

public BigInteger[] divideAndRemainder(BigInteger val)
Divide this by val and return the result and the remainder. Integral result is at index 0 in array, remainder at 1.

mod

public BigInteger mod(BigInteger m)
Calculate this mod m. Equivalent to this.divideAndRemainder(m)[1].

divide

public BigInteger divide(BigInteger m)
Calculate this divided by m. Equivalent to this.divideAndRemainder(m)[0].

subtract

public BigInteger subtract(BigInteger val)
Return this - val.

add

public BigInteger add(BigInteger val)
Return this + val.

multiply

public BigInteger multiply(BigInteger val)
Return this * val.

toByteArray

public byte[] toByteArray(boolean leadingZero)
Return a byte array representation of this, MSB first. If leadingZero is true, a zero byte will be prepended if the highest bit was set, i.e. this representation is compatible with 2's complement.

toByteArray

public byte[] toByteArray()
Return a byte array representation of this, MSB first. Equivalent to toByteArray(true), i.e. 2's complement.

getLowestSetBit

public int getLowestSetBit()
Get the index of the lowest bit set. Bit numbering starts at zero, ZERO returns -1.

bitCount

public int bitCount()
Return the number of one bits in the binary complement representation of this number.

modPow

public BigInteger modPow(BigInteger exponent,
                         BigInteger m)
Compute thisexponent modulo m. This operation is central to many public key algorithms and fairly heavily optimized in this implementation. It can optionally be supported by native C code, set initNative(). Note that the modulus most be odd, as is the case with those public key algorithms.

gcd

public BigInteger gcd(BigInteger m)
Return the greatest common divisor of this and m.

modInverse

public BigInteger modInverse(BigInteger m)
Return the modular inverse of this with respect to m. If it does not exist a ArithmeticException is thrown.

toString

public String toString(int radix)
Return a string representation of this in the given radix.

toString

public String toString()
Return a base 10 string representation of this.
Overrides:
toString in class Object

abs

public BigInteger abs()
Return the absolute value of this. Supported for API compatibility, as we not support negative numbers this is always this.

signum

public int signum()
Return the signum of this number. For positive numbers this is 1, for ZERO 0. A fast way to compare with ZERO.

max

public BigInteger max(BigInteger val)
Return the larger number of this and val.

min

public BigInteger min(BigInteger val)
Return the smaller number of this and val.

intValue

public int intValue()
Return the value of this as a int. Returns the low 32 bit truncating higher its, which means the result may be negative.

longValue

public long longValue()
Return the value of this as a long. Returns the low 64 bit truncating higher bits, which means the result may be negative.

hashCode

public int hashCode()
Return a hash code for this object.
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Test two objects for equality.
Overrides:
equals in class Object

This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note).

IAIK-JCE ME 3.04, (c) 2002 IAIK, (c) 2003 to 2006 Stiftung SIC