com.plumtree.openfoundation.util
Class XPBigInteger

java.lang.Object
  extended bycom.plumtree.openfoundation.util.XPBigInteger

public class XPBigInteger
extends java.lang.Object

XPBigInteger is an XP implementation of BigInteger based off of opensource code from codeproject.com.


Field Summary
static int maxLength
          maximum length of the BigInteger in int (4 bytes) change this to suit the required level of precision.
static XPBigInteger ONE
          BigInteger Representation of One value.
static XPBigInteger ZERO
          BigInteger Representation of Zero value.
 
Constructor Summary
XPBigInteger(byte[] inData)
          Translates a byte array containing the binary representation of an XPBigInteger into an XPBigInteger.
XPBigInteger(int signum, byte[] inData)
          Translates the sign-magnitude representation of an XPBigInteger into an XPBigInteger.
XPBigInteger(int bits, int confidence, XPSecureRandom rand)
          Generates a random, positive XPBigInteger that is probably prime, with the specified length.
XPBigInteger(int bits, XPSecureRandom rand)
          Populates the XPBigInteger with random bits, resulting in a random number uniformly distributed over the range 0 to (2^numBits - 1).
XPBigInteger(java.lang.String value)
          Translates the decimal String representation of an XPBigInteger into an XPBigInteger.
XPBigInteger(java.lang.String value, int radix)
          Translates the String representation of a BigInteger in the specified radix into an XPBigInteger.
 
Method Summary
 XPBigInteger Abs()
          Returns an XPBigInteger containing the absolute value of this.
 XPBigInteger Add(XPBigInteger bi2)
          Returns an XPBigInteger whose value is this plus the passed in argument.
 XPBigInteger And(XPBigInteger bi2)
          Returns an XPBigInteger whose value is the logical AND of this and the passed in argument.
 int BitLength()
          Returns the position of the most significant bit in the XPBigInteger.
 XPBigInteger[] DivideAndRemainder(XPBigInteger bi)
          Returns an array of two XPBigIntegers that result when dividing this value by the argument passed in.
 boolean Equals(XPBigInteger bi)
          Returns true if this XPBigInteger has the same value as the passed in argument.
 XPBigInteger Gcd(XPBigInteger bi)
          Returns the Greatest Common Denominator between this value, and the passed in argument.
 XPBigInteger GenCoPrime(int bits, XPSecureRandom rand)
          Returns a random XPBigInteger whose value is relatively prime to the value of this XPBigInteger.
 byte[] GetBytes()
          Returns an array of bytes containing all the bits of this XPBigInteger.
 int GetHashCode()
          Provides the BigInteger Object Hashcode value.
 boolean GreaterThan(XPBigInteger bi2)
          Returns true if this value is greater than the argument passed in.
 int IntValue()
          Returns an int representing the lowest order 32 bits of this XPBigInteger.
 boolean IsProbablePrime(int confidence)
          Returns true if this XPBigInteger is probably prime.
 boolean LessThan(XPBigInteger bi2)
          Returns true if this value is less than the argument passed in.
 long LongValue()
          Returns a long representing the lowest order 64 bits of this XPBigInteger.
 XPBigInteger Max(XPBigInteger bi)
          Returns the maximum of this or the passed in argument.
 XPBigInteger Min(XPBigInteger bi)
          Returns the minimum of this or the passed in argument.
 XPBigInteger Mod(XPBigInteger bi)
          Returns an XPBigInteger that contains (this % bi).
 XPBigInteger ModInverse(XPBigInteger modulus)
          Returns an XPBigInteger whose value is this ^ -1 (mod m).
 XPBigInteger ModPow(XPBigInteger exp, XPBigInteger m)
          Returns an XPBigInteger whose value is this ^ exp (mod m).
 XPBigInteger Multiply(XPBigInteger bi2)
          Multiplies this value by the value passed in.
 XPBigInteger Negate()
          Returns the neagative value of this XPBigInteger.
 XPBigInteger Not()
          Returns an XPBigInteger that is the logical negation of this value.
 XPBigInteger Or(XPBigInteger bi2)
          Returns an XPBigInteger whose value is the logical OR of this and the passed in argument.
 void SetBit(int bitNum)
          Sets the bit at the given index to 1.
 XPBigInteger ShiftLeft(int shiftVal)
          Returns an XPBigInteger that results from shifting the bits of this value left.
 XPBigInteger ShiftRight(int shiftVal)
          Returns an XPBigInteger that results from shifting the bits of this value right.
 XPBigInteger Sqrt()
          Returns an XPBigInteger whose value is equal to the square root of this.
 XPBigInteger Subtract(XPBigInteger bi2)
          Returns an XPBiginteger whose value is this minus the passed in argument.
 boolean TestBit(int bitNum)
          Returns true of the bit at the given index is 1, false otherwise.
 java.lang.String toString()
          Returns the string representation of this XPBigInteger in decimal notation.
 java.lang.String toString(int radix)
          Returns the string representation of this XPBigInteger in the given radix.
 void UnsetBit(int bitNum)
          Sets the bit at the given index to 0.
 XPBigInteger Xor(XPBigInteger bi2)
          Returns an XPBigInteger whose value is the logical XOR of this and the passed in argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

maxLength

public static final int maxLength
maximum length of the BigInteger in int (4 bytes) change this to suit the required level of precision.

See Also:
Constant Field Values

ZERO

public static final XPBigInteger ZERO
BigInteger Representation of Zero value.


ONE

public static final XPBigInteger ONE
BigInteger Representation of One value.

Constructor Detail

XPBigInteger

public XPBigInteger(java.lang.String value,
                    int radix)
Translates the String representation of a BigInteger in the specified radix into an XPBigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more digits in the specified radix. The String may not contain any extraneous characters (whitespace, for example).

Parameters:
value - String representation of BigInteger.
radix - radix to be used in interpreting val.
Throws:
XPIllegalArgumentException - val is not a valid representation of a BigInteger in the specified radix, or radix is outside the range

XPBigInteger

public XPBigInteger(java.lang.String value)
Translates the decimal String representation of an XPBigInteger into an XPBigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more decimal digits. The String may not contain any extraneous characters (whitespace, for example).

Parameters:
value - decimal String representation of BigInteger.
Throws:
XPIllegalArgumentException - val is not a valid representation of a BigInteger.

XPBigInteger

public XPBigInteger(byte[] inData)
Translates a byte array containing the binary representation of an XPBigInteger into an XPBigInteger. The value is always assumed to be positive. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

Parameters:
inData - binary representation of BigInteger.

XPBigInteger

public XPBigInteger(int signum,
                    byte[] inData)
Translates the sign-magnitude representation of an XPBigInteger into an XPBigInteger. The sign is represented as an integer signum value: -1 for negative, 0 for zero, or 1 for positive. The magnitude is a byte array in big-endian byte-order: the most significant byte is in the zeroth element. A zero-length magnitude array is allowed, and will result in in an XPBigInteger value of 0, whether signum is -1, 0 or 1.

Parameters:
signum - signum of the number (-1 for negative, 0 for zero, 1 for positive).
inData - big-endian binary representation of the magnitude of the number.

XPBigInteger

public XPBigInteger(int bits,
                    XPSecureRandom rand)
Populates the XPBigInteger with random bits, resulting in a random number uniformly distributed over the range 0 to (2^numBits - 1).

Parameters:
bits - maximum length of the new XPBigInteger.
rand - source of randomness.
Throws:
XPIllegalArgumentException - bits is negative.

XPBigInteger

public XPBigInteger(int bits,
                    int confidence,
                    XPSecureRandom rand)
Generates a random, positive XPBigInteger that is probably prime, with the specified length.

Parameters:
bits - maximum length of the new XPBigInteger.
confidence - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new XPBigInteger represents a prime number will exceed (1 - 1/2 ^ certainty). The execution time of this constructor is proportional to the value of this parameter.
rand - source of randomness.
Method Detail

Add

public XPBigInteger Add(XPBigInteger bi2)
Returns an XPBigInteger whose value is this plus the passed in argument. (this + bi2)

Parameters:
bi2 - value to add
Returns:
this + bi2

Subtract

public XPBigInteger Subtract(XPBigInteger bi2)
Returns an XPBiginteger whose value is this minus the passed in argument. (this - bi2)

Parameters:
bi2 - value to subtract
Returns:
this - bi2

Multiply

public XPBigInteger Multiply(XPBigInteger bi2)
Multiplies this value by the value passed in. Returns an XPBigInteger whose value is (this * bi2)

Parameters:
bi2 - value to multiply by
Returns:
this * bi2

ShiftLeft

public XPBigInteger ShiftLeft(int shiftVal)
Returns an XPBigInteger that results from shifting the bits of this value left. (this << shiftVal)

Parameters:
shiftVal - number of bits to shift
Returns:
this << shiftVal

ShiftRight

public XPBigInteger ShiftRight(int shiftVal)
Returns an XPBigInteger that results from shifting the bits of this value right. (this >> shiftVal)

Parameters:
shiftVal - number of bits to shift
Returns:
this >> shiftVal

Not

public XPBigInteger Not()
Returns an XPBigInteger that is the logical negation of this value. (~this)

Returns:
~this

Negate

public XPBigInteger Negate()
Returns the neagative value of this XPBigInteger. (-this)

Returns:
-this

Equals

public boolean Equals(XPBigInteger bi)
Returns true if this XPBigInteger has the same value as the passed in argument.

Parameters:
bi - value to compare to
Returns:
true if values are equal

GetHashCode

public int GetHashCode()
Provides the BigInteger Object Hashcode value.

Returns:
int value of the Hashcode.

GreaterThan

public boolean GreaterThan(XPBigInteger bi2)
Returns true if this value is greater than the argument passed in. (this > bi2)

Parameters:
bi2 - the value to compare to
Returns:
this > bi2

LessThan

public boolean LessThan(XPBigInteger bi2)
Returns true if this value is less than the argument passed in. (this < bi2)

Parameters:
bi2 - the value to compare to
Returns:
this < bi2

DivideAndRemainder

public XPBigInteger[] DivideAndRemainder(XPBigInteger bi)
Returns an array of two XPBigIntegers that result when dividing this value by the argument passed in. The first element is the quotient, and the second is the remainder. (this / bi)

Parameters:
bi - value to divide by
Returns:
an array containing the quotient and remainder

Mod

public XPBigInteger Mod(XPBigInteger bi)
Returns an XPBigInteger that contains (this % bi).

Parameters:
bi - modulus value
Returns:
this % bi

And

public XPBigInteger And(XPBigInteger bi2)
Returns an XPBigInteger whose value is the logical AND of this and the passed in argument. (this & bi2)

Parameters:
bi2 - value to AND with
Returns:
this & bi2

Or

public XPBigInteger Or(XPBigInteger bi2)
Returns an XPBigInteger whose value is the logical OR of this and the passed in argument. (this | bi2)

Parameters:
bi2 - value to OR with
Returns:
this | bi2

Xor

public XPBigInteger Xor(XPBigInteger bi2)
Returns an XPBigInteger whose value is the logical XOR of this and the passed in argument. (this ^ bi2)

Parameters:
bi2 - value to XOR with
Returns:
this ^ bi2

Max

public XPBigInteger Max(XPBigInteger bi)
Returns the maximum of this or the passed in argument.

Parameters:
bi - value to compare to
Returns:
the larger of the two values

Min

public XPBigInteger Min(XPBigInteger bi)
Returns the minimum of this or the passed in argument.

Parameters:
bi - value to compare to
Returns:
the smaller of the two values

Abs

public XPBigInteger Abs()
Returns an XPBigInteger containing the absolute value of this. abs(this)

Returns:
abs(this

toString

public java.lang.String toString()
Returns the string representation of this XPBigInteger in decimal notation.

Returns:
string representation

toString

public java.lang.String toString(int radix)
Returns the string representation of this XPBigInteger in the given radix. For radix > 10, the alphabetic characters A-Z are used to represent values.

Parameters:
radix - radix to use, from 2 to 36.
Returns:
string representation
Throws:
XPIllegalArgumentException - if radix is out of range.

ModPow

public XPBigInteger ModPow(XPBigInteger exp,
                           XPBigInteger m)
Returns an XPBigInteger whose value is this ^ exp (mod m).

Parameters:
exp - exponent value
m - modulus value
Returns:
this ^ exp (mod m)

Gcd

public XPBigInteger Gcd(XPBigInteger bi)
Returns the Greatest Common Denominator between this value, and the passed in argument. gcd(this, bi)

Parameters:
bi - value to compute gcd with
Returns:
gcd(this, bi)

BitLength

public int BitLength()
Returns the position of the most significant bit in the XPBigInteger.

Returns:
bit position

IsProbablePrime

public boolean IsProbablePrime(int confidence)
Returns true if this XPBigInteger is probably prime. The primality test includes RabinMiller and LucasLehmer tests, and returns true if the probablity that this is a prime exceeds (1 - .5 ^ confidence).

Parameters:
confidence - measure of certainty in the primality test
Returns:
true if this XPBigInteger is probably prime

IntValue

public int IntValue()
Returns an int representing the lowest order 32 bits of this XPBigInteger.

Returns:
the int value of this XPBigInteger

LongValue

public long LongValue()
Returns a long representing the lowest order 64 bits of this XPBigInteger.

Returns:
the long value of this XPBigInteger

GenCoPrime

public XPBigInteger GenCoPrime(int bits,
                               XPSecureRandom rand)
Returns a random XPBigInteger whose value is relatively prime to the value of this XPBigInteger. gcd(this, result) = 1

Parameters:
bits - number of bits in the new XPBigInteger
rand - source of randomness
Returns:
a relatively prime XPBigInteger

ModInverse

public XPBigInteger ModInverse(XPBigInteger modulus)
Returns an XPBigInteger whose value is this ^ -1 (mod m).

Parameters:
modulus - modulus to use
Returns:
this ^ -1 (mod m)

GetBytes

public byte[] GetBytes()
Returns an array of bytes containing all the bits of this XPBigInteger.

Returns:
array of bytes with the content of this

SetBit

public void SetBit(int bitNum)
Sets the bit at the given index to 1.

Parameters:
bitNum - bit index

UnsetBit

public void UnsetBit(int bitNum)
Sets the bit at the given index to 0.

Parameters:
bitNum - bit index

TestBit

public boolean TestBit(int bitNum)
Returns true of the bit at the given index is 1, false otherwise.

Parameters:
bitNum - bit index
Returns:
true if the bit is 1

Sqrt

public XPBigInteger Sqrt()
Returns an XPBigInteger whose value is equal to the square root of this.

Returns:
sqrt(this)


Copyright 2002, 2003, 2004 Plumtree Software Inc. All Rights Reserved.