|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectdanger.crypto.BigInteger
public final class BigInteger
BigInteger is an MPZ: an integer without an upper bound to its size. However,
currently danger.crypto.BigInteger has a very real upper bound of
2048 bits. This restriction may be removed in the future.
When converting a BigInteger to/from a byte array, the number is assumed to
be in "normalized" format. Normalized format is two's complement, big endian.
In other words, the first byte is the most significant, and the high bit of the
first byte determines its sign. For example, the number 2300 is represented
as the byte array { 8, 0xfc }, and the number -129 is represented
as the byte array { 0xff, 0x7f }.
A BigInteger is immutable, so operations always return a new BigInteger as the result. This may cause memory churn when doing many operations.
FIXME: There are also several fundamental math operations missing, because they weren't needed by the Terminal app that this class was created for. Those holes should be filled in.
NOTE: The current implementation of the
java.math.BigInteger class on the device is more complete than
this one. However, it is also much slower than this one. Eventually the
performance issue will be improved. Until then, you may choose to use one
or the other depending on your needs. Note that the
java.math.BigInteger class has only been implemented since
2.3.
| Field Summary | |
|---|---|
static BigInteger |
ONE
The BigInteger corresponding to 1, for convenience. |
static BigInteger |
ZERO
The BigInteger corresponding to 0, for convenience. |
| Constructor Summary | |
|---|---|
BigInteger(byte[] in)
Create a new BigInteger from a byte array in normalized format. |
|
BigInteger(byte[] in,
int offset,
int length)
Create a new BigInteger from a byte array in normalized format. |
|
BigInteger(int x)
Create a new BigInteger from a native "int". |
|
BigInteger(long x)
Create a new BigInteger from a native "long". |
|
BigInteger(String s,
int base)
Create a new BigInteger from a string. |
|
| Method Summary | |
|---|---|
BigInteger |
add(BigInteger x)
Add two BigIntegers. |
int |
bitLength()
Determines how many bits are used by the absolute value of this number. |
int |
compareTo(BigInteger x)
Compare this number to another BigInteger. |
int |
compareTo(Object o)
Compare this number to an arbitrary object. |
BigInteger |
divide(BigInteger x)
Divide two BigIntegers. |
boolean |
equals(Object o)
Determine if this number is equal to an arbitrary object. |
static BigInteger |
generatePrimeCandidate(int bits)
Generate a BigInteger which is a candidate for being prime, but hasn't had any rigorous tests applied yet. |
static BigInteger |
generateProbablePrime(int bits)
Generate a BigInteger which is probably prime. |
int |
hashCode()
Generates a hash code for this number. |
boolean |
isProbablePrime(int certainty)
Determine if a BigInteger is probably prime. |
BigInteger |
mod(BigInteger x)
Divide two BigIntegers and return the remainder. |
BigInteger |
modInverse(BigInteger x)
Find the modular inverse of a number. |
BigInteger |
modPow(BigInteger exponent,
BigInteger m)
Raise this number to the exponent power, modulo
m. |
BigInteger |
multiply(BigInteger x)
Multiply two BigIntegers. |
BigInteger |
subtract(BigInteger x)
Subtract two BigIntegers. |
byte[] |
toByteArray()
Convert this number into a byte array, in normalized format. |
int |
toInteger()
Convert this number to a java int. |
String |
toString()
Generate a string representation of this number in decimal format. |
String |
toString(int base)
Generate a string representation of this number in the given base. |
static BigInteger |
valueOf(int x)
Create a BigInteger from a java int. |
static BigInteger |
valueOf(long x)
Create a BigInteger from a java long. |
static BigInteger |
valueOf(String s)
Create a BigInteger from a java String. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final BigInteger ZERO
public static final BigInteger ONE
| Constructor Detail |
|---|
public BigInteger(int x)
x - the initial value of this BigInteger, as an intpublic BigInteger(long x)
x - the initial value of this BigInteger, as a long
public BigInteger(byte[] in,
int offset,
int length)
in - the byte array to parseoffset - offset in in to begin parsinglength - number of bytes of in to parsepublic BigInteger(byte[] in)
BigInteger(in, 0, in.length).
in - the byte array to parse
public BigInteger(String s,
int base)
s - the initial value as a stringbase - the base of the string encoding
NumberFormatException - if the base is outside the range [2, 16]| Method Detail |
|---|
public static BigInteger valueOf(int x)
x - the initial value as an int
xpublic static BigInteger valueOf(long x)
x - the initial value as an long
xpublic static BigInteger valueOf(String s)
BigInteger(String,int) constructor.
s - the initial value as a string in base 10
public BigInteger add(BigInteger x)
x - the BigInteger to add
public BigInteger subtract(BigInteger x)
x - the BigInteger to subtract
public BigInteger multiply(BigInteger x)
x - the BigInteger to multiply
public BigInteger divide(BigInteger x)
x - the BigInteger to divide
public BigInteger mod(BigInteger x)
x - the BigInteger to divide
ArithmeticException - if x <= 0public BigInteger modInverse(BigInteger x)
x - the modulus, which needs to be relatively prime to this
BigInteger
v, such that
(this * v) mod x = 1
ArithmeticException - if x <= 0, or this BigInteger has no
inverse relative to xpublic int compareTo(BigInteger x)
x - the number to compare to
0 if the two BigIntegers are equal; a negative
integer if this number is less than x; a positive
integer if this number is greater than x
public int compareTo(Object o)
throws ClassCastException
compareTo in interface Comparableo - the object to compare to
compareTo(BigInteger)) if
o is a BigInteger
ClassCastException - if o is not a BigIntegerpublic boolean equals(Object o)
equals in class Objecto - the object to compare to
compareTo(o)
returns 0.public int hashCode()
hashCode in class Objectpublic int bitLength()
public byte[] toByteArray()
public int toInteger()
int. If the number's
absolute value won't fit in 31 bits, then the lower 31 bits are
returned, with the sign preserved.
public String toString()
toString(10).
toString in class Objectpublic String toString(int base)
base - the requested base of the output string (from 2 to 16,
inclusive; anything outside that range will be assumed 10)
public BigInteger modPow(BigInteger exponent,
BigInteger m)
exponent power, modulo
m. This is frequently desired for private-key/public-key
crypto. On the Hiptop, this function is optimized heavily.
exponent - the power to raise this number tom - the modulus to coerce the result into
(this ** exponent mod m)public boolean isProbablePrime(int certainty)
certainty - the amount of certainty desired, as an exponent of 50%
(the Rabin-Miller test will be executed certainty/2 times)
(1 / 2**certainty)
that this number is prime; false if the number is definitely composite.public static BigInteger generateProbablePrime(int bits)
bits - number of bits to generate (between 2 and 2048)
ArithmeticException - if bits is out of rangepublic static BigInteger generatePrimeCandidate(int bits)
isProbablePrime(int) on the result to determine if the
candidate is actually prime (with some degree of certainty).
One potential use for this method is to have finer-grained control
over the (often time-consuming) process of generating a large prime
number. Repeatedly calling generatePrimeCandidate(int) and
isProbablePrime(int) gives you a chance to provide visible
feedback between each iteration.
Note: Currently the CJC algorithm is used when
bits is 512. This algorithm rapidly generates numbers
that are not divisible by the first six primes. For other values of
bits, this method only guarantees that the result is
odd.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||