Class NativeBigInteger
- All Implemented Interfaces:
Serializable
,Comparable<BigInteger>
BigInteger that takes advantage of the jbigi library for the modPow operation, which accounts for a massive segment of the processing cost of asymmetric crypto. The jbigi library itself is basically just a JNI wrapper around the GMP library - a collection of insanely efficient routines for dealing with big numbers.
There are three environmental properties for configuring this component:- jbigi.enable: whether to use the native library (defaults to "true")
- jbigi.impl: select which resource to use as the native implementation
- jbigi.ref: the file specified in this parameter may contain a resource name to override jbigi.impl (defaults to "jbigi.cfg")
If jbigi.enable is set to false, this class won't even attempt to use the
native library, but if it is set to true (or is not specified), it will first
check the platform specific library path for the "jbigi" library, as defined by
Runtime.loadLibrary(java.lang.String)
- e.g. C:\windows\jbigi.dll or /lib/libjbigi.so, as
well as the CLASSPATH for a resource named 'jbigi'. If that fails, it reviews
the jbigi.impl environment property - if that is set, it checks all of the
components in the CLASSPATH for the file specified and attempts to load it as
the native library. If jbigi.impl is not set, it uses the jcpuid library
described below. If there is still no matching resource, or if that resource
is not a valid OS/architecture specific library, the NativeBigInteger will
revert to using the pure java implementation.
When attempting to load the native implementation as a resource from the CLASSPATH, the NativeBigInteger will make use of the jcpuid component which runs some assembly code to determine the current CPU implementation, such as "pentium4" or "k623". We then use that, combined with the OS, to build an optimized resource name - e.g. "net/i2p/util/libjbigi-freebsd-pentium4.so" or "net/i2p/util/jbigi-windows-k623.dll". If that resource exists, we use it. If it doesn't (or the jcpuid component fails), we try a generic native implementation using "none" for the CPU (ala "net/i2p/util/jbigi-windows-none.dll").
Running this class by itself does a basic unit test and benchmarks the NativeBigInteger.modPow vs. the BigInteger.modPow by running a 2Kbit op 100 times. At the end of each test, if the native implementation is loaded this will output something like:
native run time: 6090ms (60ms each) java run time: 68067ms (673ms each) native = 8.947066860593239% of pure java time
If the native implementation is not loaded, it will start by saying:
WARN: Native BigInteger library jbigi not loaded - using pure java
Then go on to run the test, finally outputting:
java run time: 64653ms (640ms each) However, we couldn't load the native library, so this doesn't test much
- See Also:
-
Field Summary
Fields inherited from class java.math.BigInteger
ONE, TEN, TWO, ZERO
-
Constructor Summary
ConstructorDescriptionNativeBigInteger
(byte[] val) NativeBigInteger
(int signum, byte[] magnitude) NativeBigInteger
(int bitlen, int certainty, Random rnd) NativeBigInteger
(int numbits, Random rnd) NativeBigInteger
(String val) NativeBigInteger
(String val, int radix) NativeBigInteger
(BigInteger integer) Creates a new NativeBigInteger with the same value as the supplied BigInteger. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
cpuModel()
static String
cpuType()
boolean
static int
Get the jbigi versionstatic String
Get the libgmp versionstatic String
The name of the library loaded, if known.int
hashCode()
static boolean
isNative()
static String
static void
Compare the BigInteger.modPow vs the NativeBigInteger.modPow of some really big (2Kbit) numbers 100 different times and benchmark the performance.modPow
(BigInteger exponent, BigInteger m) modPowCT
(BigInteger exponent, BigInteger m) byte[]
cachesMethods inherited from class java.math.BigInteger
abs, add, and, andNot, bitCount, bitLength, byteValueExact, clearBit, compareTo, divide, divideAndRemainder, doubleValue, flipBit, floatValue, gcd, getLowestSetBit, intValue, intValueExact, isProbablePrime, longValue, longValueExact, max, min, mod, multiply, negate, nextProbablePrime, not, or, parallelMultiply, pow, probablePrime, remainder, setBit, shiftLeft, shiftRight, shortValueExact, signum, sqrt, sqrtAndRemainder, subtract, testBit, toString, toString, valueOf, xor
Methods inherited from class java.lang.Number
byteValue, shortValue
-
Constructor Details
-
NativeBigInteger
public NativeBigInteger(byte[] val) -
NativeBigInteger
public NativeBigInteger(int signum, byte[] magnitude) -
NativeBigInteger
-
NativeBigInteger
-
NativeBigInteger
-
NativeBigInteger
-
NativeBigInteger
Creates a new NativeBigInteger with the same value as the supplied BigInteger. Warning!, not very efficent
-
-
Method Details
-
getJbigiVersion
public static int getJbigiVersion()Get the jbigi version- Returns:
- 0 if no jbigi available, 2 if version info not supported
- Since:
- 0.9.26
-
getLibGMPVersion
Get the libgmp version- Returns:
- "unknown" if no jbigi available or if version not supported
- Since:
- 0.9.26
-
modPow
- Overrides:
modPow
in classBigInteger
- Parameters:
exponent
- must be postivem
- must be postive- Throws:
ArithmeticException
- if m <= 0 or exponent <=0
-
modPowCT
- Parameters:
exponent
- must be postivem
- must be postive and odd- Throws:
ArithmeticException
- if m <= 0 or m is even or exponent <=0- Since:
- 0.9.26 and libjbigi version 3 and GMP version 5
-
modInverse
- Overrides:
modInverse
in classBigInteger
- Throws:
ArithmeticException
- if not coprime with m, or m <= 0- Since:
- 0.9.26 and libjbigi version 3
-
toByteArray
public byte[] toByteArray()caches- Overrides:
toByteArray
in classBigInteger
-
isNative
public static boolean isNative()- Returns:
- True iff native methods will be used by this class
-
loadStatus
- Returns:
- A string suitable for display to the user
-
getLoadedResourceName
The name of the library loaded, if known. Null if unknown or not loaded. Currently non-null only if extracted from jbigi.jar.- Since:
- 0.9.17
-
cpuType
-
cpuModel
-
main
Compare the BigInteger.modPow vs the NativeBigInteger.modPow of some really big (2Kbit) numbers 100 different times and benchmark the performance.
- Parameters:
args
- -n Disable native test
-
equals
- Overrides:
equals
in classBigInteger
-
hashCode
public int hashCode()- Overrides:
hashCode
in classBigInteger
-