|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.rim.device.api.crypto.CryptoByteArrayArithmetic
public class CryptoByteArrayArithmetic
Modular arithmetic on large unsigned integers. Integers are repesented as byte arrays
with the most significant byte at position 0 in the array, or position offset
if offset
is specified. The arrays can be up to 256 bytes long.
When possible, methods that can work with a modulus that is a power of two are provided.
In this case, the modulus will be 2log2modulus, where log2modulus
is the integer representing log2 of the modulus that will be used in the calculations.
Each arithmetic method also has a version which allows an offset into each array to be specified,
as well as the length in bytes of the integer stored in the array. In this case, the integer operated on
will be the integer in the array that begins at byte offset
and continues for
length
bytes.
Method Summary | ||
---|---|---|
|
static void |
add(byte[] a,
byte[] b,
byte[] modulus,
byte[] result)
Computes the sum of the integers represented by arrays a and b , mod the integer
represented by the array modulus , and stores the result in the array result . |
|
static void |
add(byte[] a,
byte[] b,
int log2modulus,
byte[] result)
Computes the sum of the integers represented by arrays a and b ,
mod 2log2modulus and stores the result in the array result . |
|
static void |
add(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the sum of the integers of length length in the arrays beginning at
a[ aOffset ] and b[ bOffset ] , mod the
integer beginning at modulus[ modulusOffset ] , and stores the result
in the array result , beginning at resultOffset . |
|
static void |
add(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Computes the sum of the integers in the arrays beginning at a[ aOffset ] and b[ bOffset ] , mod 2log2modulus, and
stores the result in the array result ,
beginning at resultOffset . |
|
static int |
compare(byte[] a,
byte[] b)
Compares the integer represented in array a to the one represented
in array b . |
|
static int |
compare(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength)
Compares the integer represented in array a to the one represented
in array b . |
|
static byte[] |
createArray(int log2n)
Returns an array containing 2^ log2n |
|
static void |
decrement(byte[] a,
byte[] modulus,
byte[] result)
Subtracts one from the integer represented by the array a , mod the integer
represented by the array modulus , and stores the result in the array result . |
|
static void |
decrement(byte[] a,
int log2modulus,
byte[] result)
Subtracts one from the integer represented by array a ,
mod 2log2modulus and stores the result in the array result . |
|
static void |
decrement(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Subtracts one from the integer of length aLength in the array beginning at
a[ aOffset ] , mod the integer beginning at modulus[ modulusOffset ] ,
and stores the result in the array result beginning at resultOffset . |
|
static void |
decrement(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Subtracts one from the integer in the array beginning at a[ aOffset ] , mod 2log2modulus, and
stores the result in the array result , beginning at resultOffset . |
|
static void |
divide(byte[] dividend,
byte[] divisor,
byte[] quotient,
byte[] remainder)
Computes the quotient and remainder of dividend / divisor
The quotient is stored in quotient , and the remainder is stored in remainder . |
|
static void |
divide(byte[] dividend,
int dividendOffset,
int dividendLength,
byte[] divisor,
int divisorOffset,
int divisorLength,
byte[] quotient,
int quotientOffset,
int quotientLength,
byte[] remainder,
int remainderOffset,
int remainderLength)
Computes the quotient and remainder of the integer in the array dividend ,
beginning at dividendOffset , divided by the integer in the array
divisor , beginning at divisorOffset . |
|
static void |
divideByTwo(byte[] a,
byte[] modulus,
byte[] result)
Divide the integer represented by the array a by two modulo the integer
represented by the array modulus . |
|
static void |
divideByTwo(byte[] a,
int log2modulus,
byte[] result)
Divide the integer represented by the array a by a modulus
which is a power of 2. |
|
static void |
divideByTwo(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Divide the integer of length aLength beginning at a[ aOffset ]
by two modulo the integer of length modulusLength beginning at
modulus[ modulusOffset ] . |
|
static void |
divideByTwo(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Divide the integer of length length beginning at a[ aOffset ]
by a modulus which is a power of 2. |
|
static byte[] |
ensureLength(byte[] array,
int length)
Takes a byte array and adjusts its length by trimming or padding it. |
|
static void |
exponent(byte[] base,
byte[] exponent,
byte[] modulus,
byte[] result)
Modular exponentiation with the integers found in base , exponent and modulus
Takes baseexponent mod modulus and stores it in result
result must be at least as large as modulus . |
|
static void |
exponent(byte[] base,
int baseOffset,
int baseLength,
byte[] exponent,
int exponentOffset,
int exponentLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Modular exponentiation with the integers found in base , exponent and modulus
Takes basemodulus mod modulus and stores the result in the resultLength
bytes of result , beginning at result[ resultOffset ] . |
|
static int |
findFirstNonZeroByte(byte[] a,
int offset,
int length)
Returns the first non-zero byte in the array a , starting at
a[ offset ] continuing for length bytes. |
|
static void |
gcd(byte[] a,
byte[] b,
byte[] result)
Computes the greatest common divisor of the integer represented by the array a
and the integer represetned by the array b , and stores the result in the array
result must be at least as long as the longest of a and b . |
|
static void |
gcd(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the greatest common divisor of the integer represetned by the array a , beginning at
aOffset , and the integer represented by the array b , beginning at
bOffset and stores the result in the array result , beginning at
result[ resultOffset ] . |
|
static int |
getNumBits(byte[] array)
Returns the minimum number of bits required to encode the number contained in array . |
|
static void |
increment(byte[] a,
byte[] modulus,
byte[] result)
Adds one to the integer represented by the array a , mod the integer
represented by the array modulus , and stores the result in the array result . |
|
static void |
increment(byte[] a,
int log2modulus,
byte[] result)
Adds one to the integer represented by array a ,
mod 2log2modulus and stores the result in the array result . |
|
static void |
increment(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Adds one to the integer of length aLength in the array beginning at
a[ aOffset ] , mod the integer beginning at modulus[ modulusOffset ] ,
and stores the result in the array result beginning at resultOffset . |
|
static void |
increment(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Adds one to the integer in the array beginning at a[ aOffset ] , mod 2log2modulus, and
stores the result in the array result , beginning at resultOffset . |
|
static void |
invert(byte[] a,
byte[] modulus,
byte[] result)
Computes the inverse of the integer represented by the array a , modulo the integer
represented by the array modulus and stores the result in result . |
|
static void |
invert(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the inverse of the integer in in the array a beginning at aOffset ,
modulo the integer in modulus beginning at modulusOffset and stores the result
the the array result , beginning at result[ resultOffset ] . |
|
static boolean |
isOne(byte[] a)
Compares array a to 1. |
|
static boolean |
isOne(byte[] a,
int offset,
int length)
Compares array a to 1. |
|
static boolean |
isZero(byte[] a)
Compares array a to 0. |
|
static boolean |
isZero(byte[] a,
int offset,
int length)
Compares array a to 0. |
|
static void |
mod(byte[] a,
byte[] modulus,
byte[] result)
Computes the modulus of the integer represented by the array a moduloed by the integer
represented by the array modulus , and stores the result in result . |
|
static void |
mod(byte[] a,
int log2modulus,
byte[] result)
Computes the modulus of the integer represented by the array a , moduloed by the an
integer which is a power of 2, and stores the result in result . |
|
static void |
mod(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the modulus of the integer in the array a , beginning at
aOffset , moduloed by the integer in the array modulus ,
beginning at modulusOffset , and stores the result in result ,
beginning at result[ resultOffset ] . |
|
static void |
mod(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Computes the modulus of the integer in the array a , beginning at aOffset ,
moduloed by the an integer which is a power of 2 and stores the result in result
beginning at result[ resultOffset ] . |
|
static void |
multiply(byte[] a,
byte[] b,
byte[] modulus,
byte[] result)
Computes the product of the integers represented by the arrays a and b ,
mod the integer represented by the array modulus , and stores the result in the array
result . |
|
static void |
multiply(byte[] a,
byte[] b,
int log2modulus,
byte[] result)
Computes the product of the integers represented by the arrays a and b ,
mod 2log2modulus, and stores the result in result . |
|
static void |
multiply(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the product of the integers in the arrays beginning at a[ aOffset ] and b[ bOffset ] ,
mod the integer in the array beginning at modulus[ modulusOffset ] , and stores
the result in result , beginning at resultOffset . |
|
static void |
multiply(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Computes the product of the integers in the arrays beginning at a[ aOffset ] and b[ bOffset ] , mod 2log2modulus, and
stores the result in result , beginning at resultOffset . |
|
static void |
multiplyByTwo(byte[] a,
byte[] modulus,
byte[] result)
Multiply the integer representned by array a by two modulo the integer represented
by array modulus . |
|
static void |
multiplyByTwo(byte[] a,
int log2modulus,
byte[] result)
Multiply the integer represetned by array a by a modulus which is a power of 2. |
|
static void |
multiplyByTwo(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Multiply the integer of length aLength beginning at a[ aOffset ]
by two modulo the integer of length modulusLength beginning
at modulus[ modulusOffset ] , and stores the result in result beginning
at resultOffset . |
|
static void |
multiplyByTwo(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Multiply the integer of length length beginning at a[ aOffset ]
by a modulus which is a power of 2. |
|
static byte[] |
pad(byte[] array,
int numberOfZeros)
Pads byte array with zeros. |
|
static void |
square(byte[] a,
byte[] modulus,
byte[] result)
Computes the square of the integer represented by the array a ,
mod the integer represented by the array modulus , and stores the result in the array
result must be at least as long as modulus . |
|
static void |
square(byte[] a,
int log2modulus,
byte[] result)
Computes the square of the integer represented by the array a ,
mod 2log2modulus, and stores the result in result . |
|
static void |
square(byte[] a,
int aOffset,
int aLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the square of the integer of length aLength in the array beginning at
a[ aOffset ] , mod the integer in the array beginning at modulus[ modulusOffset ] ,
and stores the result in result , beginning at resultOffset . |
|
static void |
square(byte[] a,
int aOffset,
int aLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Computes the square of the integer of length aLength in the array beginning
at a[ aOffset ] , mod 2log2modulus, and
stores the result in result , beginning at resultOffset . |
|
static void |
subtract(byte[] a,
byte[] b,
byte[] modulus,
byte[] result)
Computes the difference of the integers represented by arrays a and b , mod the integer
represented by the array modulus , and stores the result in
the array result . |
|
static void |
subtract(byte[] a,
byte[] b,
int log2modulus,
byte[] result)
Computes the difference of the integers represented by arrays a and b ,
mod 2log2modulus and stores the result in the array result . |
|
static void |
subtract(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
byte[] modulus,
int modulusOffset,
int modulusLength,
byte[] result,
int resultOffset,
int resultLength)
Computes the difference of the integers in the arrays beginning at a[ aOffset ] and b[ bOffset ] , mod the
integer beginning at modulus[ modulusOffset ] , and stores the result in the array
result , beginning at resultOffset . |
|
static void |
subtract(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength,
int log2modulus,
byte[] result,
int resultOffset,
int resultLength)
Computes the difference of the integers in the arrays beginning at a[ aOffset ] and b[ bOffset ] , mod 2log2modulus,
and stores the result in the array result , beginning at resultOffset . |
|
static byte[] |
trim(byte[] array)
Removes the leading zeros from given byte[] and returns a new byte[] without them. |
|
static long |
valueOf(byte[] array)
Converts array into a long integer (Note: the returned value should be considered as an unsigned value). |
|
static byte[] |
valueOf(long i)
Converts i into a byte array. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static boolean isZero(byte[] a, int offset, int length)
a
to 0.
a
- The array containing the integer to be compared to zero.offset
- The Offset into the array a
, where the integer begins.length
- The number of bytes in the integer being compared.
a
equals zero.public static boolean isZero(byte[] a)
a
to 0.
a
- The array to be compared to zero.
a
equals zero.public static boolean isOne(byte[] a, int offset, int length)
a
to 1.
a
- The array containing the integer to be compared to one.offset
- The offset into the array a
, where the integer begins.length
- The number of bytes in the integer being compared.
a
equals one.public static boolean isOne(byte[] a)
a
to 1.
a
- The array to be compared to one.
a
equals one.public static int findFirstNonZeroByte(byte[] a, int offset, int length)
a
, starting at
a[ offset ]
continuing for length
bytes.
If the integer represented in the array is zero, the position of the last byte is returned. If there is no data to read the result returned will be -1.
a
- The byte array.offset
- The first byte of the integer in a
.length
- The length in bytes of the integer in a
.
a
of the first non-zero byte in the integer. If the integer
represented in the array is zero, it returns the position of the last byte in the integer.public static int compare(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength)
a
to the one represented
in array b
.
a
- The first integer.aOffset
- The offset into a
, where the integer begins.aLength
- The length in bytes of the integer in a
.b
- The second integer.bOffset
- The offset into b
, where the integer begins.bLength
- The length in bytes of the integer in b
.
a
is numerically
less than, equal to, or greater than the integer in b
.public static int compare(byte[] a, byte[] b)
a
to the one represented
in array b
.
a
- The first integer.b
- The second integer.
a
is numerically
less than, equal to, or greater than the integer in b
.public static void multiplyByTwo(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
aLength
beginning at a[ aOffset ]
by two modulo the integer of length modulusLength
beginning
at modulus[ modulusOffset ]
, and stores the result in result
beginning
at resultOffset
.
a
- The integer to multiply by two.aOffset
- The offset into a
, where the integer begins.aLength
- The length of the integer in a
.modulus
- The modulus.modulusOffset
- The offset into modulus
, where the integer begins.modulusLength
- The length of the integer in modulus
.result
- The array where the result will be stored.resultOffset
- The offset into result
, where the result begins.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void multiplyByTwo(byte[] a, byte[] modulus, byte[] result) throws ArithmeticException
a
by two modulo the integer represented
by array modulus
.
The result
must be at least as long as modulus
.
a
- The integer to multiply by two.modulus
- The modulus.result
- The array where the result is stored.
ArithmeticException
- Thrown if the modulus is zero.public static void multiplyByTwo(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
length
beginning at a[ aOffset ]
by a modulus which is a power of 2.
a
- The integer to multiply by two.aOffset
- The offset into a
, where the integer begins.aLength
- The length of the integer in a
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result will be stored.resultOffset
- The offset into result
, where the result begins.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
public static void multiplyByTwo(byte[] a, int log2modulus, byte[] result)
a
by a modulus which is a power of 2.
result
must be at least as long as modulus
a
- The integer to multiply by two.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*the length of result
.result
- The array where the result is stored.public static void divideByTwo(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
aLength
beginning at a[ aOffset ]
by two modulo the integer of length modulusLength
beginning at
modulus[ modulusOffset ]
.
NOTE: The result of the operation does not exist if a
is odd and
modulus
is even.
a
- The integer to divide by two.aOffset
- The offset into a
, where the integer begins.aLength
- The length of the integer in a
.modulus
- The modulus.modulusOffset
- The offset into modulus
, where the integer begins.modulusLength
- The length of the integer in modulus
.result
- The byte array that will store the result.resultOffset
- The offset of the result data within the array.resultLength
- The length of the result data in the array.
ArithmeticException
- Thrown when the modulus is zero or a
is odd and modulus
is even.public static void divideByTwo(byte[] a, byte[] modulus, byte[] result) throws ArithmeticException
a
by two modulo the integer
represented by the array modulus
.
NOTE: The result of the operation does not exist if a
is odd and
modulus
is even.
a
- The integer to multiply by two.modulus
- The modulus.result
- A byte array containing the result.
ArithmeticException
- Thrown if the modulus is zero or a
is odd and modulus
is even.public static void divideByTwo(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
length
beginning at a[ aOffset ]
by a modulus which is a power of 2.
NOTE: The result of the operation does not exist if a
is odd, since the
modulus is even.
a
- The integer to divide by two.aOffset
- The offset into a
, where the integer begins.aLength
- The length of the integer in a
.log2modulus
- The log2 of the modulus. 0 <= log2modulus
<= 8*aLength
.result
- The byte array that will store the result.resultOffset
- The offset of the result data within the array.resultLength
- The length of the result data in the array.
ArithmeticException
- Thrown if a
is odd.public static void divideByTwo(byte[] a, int log2modulus, byte[] result)
a
by a modulus
which is a power of 2.
NOTE: The result of the operation does not exist if a
is odd, since the
modulus is even.
a
- The integer to divide by two.log2modulus
- The log2 of the modulus 0 <= log2modulus
<= 8*the length of a
.result
- A byte array containing the result.
ArithmeticException
- Thrown if a
is odd.public static void add(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
length
in the arrays beginning at
a[ aOffset ]
and b[ bOffset ]
, mod the
integer beginning at modulus[ modulusOffset ]
, and stores the result
in the array result
, beginning at resultOffset
.
a
- The array containing the first integer in the addition.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the addition.bOffset
- The offset into the second array, where the first byte of the second integer is located.bLength
- The length of the integer in b
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where the first byte
of the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular addition will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void add(byte[] a, byte[] b, byte[] modulus, byte[] result) throws ArithmeticException
a
and b
, mod the integer
represented by the array modulus
, and stores the result in the array result
.
result
must be at least as long as modulus
.
a
- The array containing the first integer in the addition.b
- The array containing the second integer in the addition.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular addition will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void add(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a[ aOffset ]
and b[ bOffset ]
, mod 2log2modulus, and
stores the result in the array result
,
beginning at resultOffset
.
a
- The array containing the first integer in the addition.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the addition.bOffset
- The offset into the second array, where the first byte of the second integer is located.bLength
- The length of the integer in b
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular addition will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
The value resultLength*8
must be at least log2modulus
.public static void add(byte[] a, byte[] b, int log2modulus, byte[] result)
a
and b
,
mod 2log2modulus and stores the result in the array result
.
a
- The array containing the first integer in the addition.b
- The array containing the second integer in the addition.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular addition will be stored.public static void subtract(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
a[ aOffset ]
and b[ bOffset ]
, mod the
integer beginning at modulus[ modulusOffset ]
, and stores the result in the array
result
, beginning at resultOffset
.
a
- The array containing the first integer in the subtraction.aOffset
- The offset into the first array, where the first byte of the
first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the subtraction.bOffset
- The offset into the second array, where the first byte of the
second integer is located.bLength
- The length of the integer in b
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where the first byte of
the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular subtraction will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void subtract(byte[] a, byte[] b, byte[] modulus, byte[] result) throws ArithmeticException
a
and b
, mod the integer
represented by the array modulus
, and stores the result in
the array result
.
The result
must be at least as long as modulus
.
a
- The array containing the first integer in the operation.b
- The array containing the second integer in the operation.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular operation will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void subtract(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a[ aOffset ]
and b[ bOffset ]
, mod 2log2modulus,
and stores the result in the array result
, beginning at resultOffset
.
a
- The array containing the first integer in the operation.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the operation.bOffset
- The offset into the second array, where the first byte of the second integer is locatedbLength
- the length of the integer in b
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular operation will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
public static void subtract(byte[] a, byte[] b, int log2modulus, byte[] result)
a
and b
,
mod 2log2modulus and stores the result in the array result
.
a
- The array containing the first integer in the operation.b
- The array containing the second integer in the operation.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular operation will be stored.public static void multiply(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
a[ aOffset ]
and b[ bOffset ]
,
mod the integer in the array beginning at modulus[ modulusOffset ]
, and stores
the result in result
, beginning at resultOffset
.
a
- The array containing the first integer in the operation.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the operation.bOffset
- The offset into the second array, where the first byte of the second integer is located.bLength
- The length of the integer in b
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where first byte of the the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular operation will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void multiply(byte[] a, byte[] b, byte[] modulus, byte[] result) throws ArithmeticException
a
and b
,
mod the integer represented by the array modulus
, and stores the result in the array
result
.
result
must be at least as long as modulus
a
- The array containing the first integer in the operation.b
- The array containing the second integer in the operation.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular operation will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void multiply(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a[ aOffset ]
and b[ bOffset ]
, mod 2log2modulus, and
stores the result in result
, beginning at resultOffset
.
a
- The array containing the first integer in the operation.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.b
- The array containing the second integer in the operation.bOffset
- The offset into the second array, where the first byte of the second integer is located.bLength
- The length of the integer in b
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular operation will be stored.resultOffset
- The offs et into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
public static void multiply(byte[] a, byte[] b, int log2modulus, byte[] result)
a
and b
,
mod 2log2modulus, and stores the result in result
.
a
- The array containing the first integer in the operation.b
- The array containing the second integer in the operation.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular operation will be stored.public static void square(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
aLength
in the array beginning at
a[ aOffset ]
, mod the integer in the array beginning at modulus[ modulusOffset ]
,
and stores the result in result
, beginning at resultOffset
.
a
- The array containing the integer to be squared.aOffset
- The offset into the first array, where the first byte of the first integer is located.aLength
- The length of the integer in a
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where first byte of the the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular operation will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void square(byte[] a, byte[] modulus, byte[] result) throws ArithmeticException
a
,
mod the integer represented by the array modulus
, and stores the result in the array
result
must be at least as long as modulus
.
a
- The array containing the first integer in the operation.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular operation will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void square(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
aLength
in the array beginning
at a[ aOffset ]
, mod 2log2modulus, and
stores the result in result
, beginning at resultOffset
.
a
- The array containing the first integer in the operation.aOffset
- The offset into the first array, where the first byte of
the first integer is located.aLength
- The length of the array a.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular operation will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
.public static void square(byte[] a, int log2modulus, byte[] result)
a
,
mod 2log2modulus, and stores the result in result
.
a
- The array containing the first integer in the operation.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular operation will be stored.public static void mod(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
a
, beginning at
aOffset
, moduloed by the integer in the array modulus
,
beginning at modulusOffset
, and stores the result in result
,
beginning at result[ resultOffset ]
.
a
- The array containing the integer to take the modulo of.aOffset
- The offset into a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.modulus
- The array containing the modulus.modulusOffset
- The offset into modulus
, where the integer begins.modulusLength
- The length of the integer in modulus
.result
- Where the resulting value of the operation will be stored.resultOffset
- The offset into result
where the result will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if the modulus is zero.public static void mod(byte[] a, byte[] modulus, byte[] result) throws ArithmeticException
a
moduloed by the integer
represented by the array modulus
, and stores the result in result
.
result
must be at least as long as modulus
.
a
- The array containing the array to take the modulo of.modulus
- The array containing the modulus.result
- An array where the resulting value of the operation will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void mod(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a
, beginning at aOffset
,
moduloed by the an integer which is a power of 2 and stores the result in result
beginning at result[ resultOffset ]
.
a
- The byte array containing the array to take the modulo of.aOffset
- The offset into a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array that will store the resulting value of the operation.resultOffset
- The offset into result
where the result will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
.public static void mod(byte[] a, int log2modulus, byte[] result)
a
, moduloed by the an
integer which is a power of 2, and stores the result in result
.
a
- The byte array containing the array to take the modulo of.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the resulting value of the operation will be storedpublic static void invert(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
a
beginning at aOffset
,
modulo the integer in modulus
beginning at modulusOffset
and stores the result
the the array result
, beginning at result[ resultOffset ]
.
a
- The byte array containing the integer to take the inverse of.aOffset
- The offset into a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.modulus
- The array containing the modulus. The integer represented in
modulus
must be odd.modulusOffset
- The offset into modulus
, where the first byte of the integer is located.modulusLength
- The length of the integer in modulus
.result
- Where the resulting value of the operation will be stored.resultOffset
- The offset into result
where the result will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
.
ArithmeticException
- Thrown if modulus is zero or inverse doesn't exist.public static void invert(byte[] a, byte[] modulus, byte[] result) throws ArithmeticException
a
, modulo the integer
represented by the array modulus
and stores the result in result
.
result
must be at least as long as modulus
.
a
- The byte array containing the intger to take the inverse of.modulus
- The array containing the modulus. The integer represented in
modulus
must be odd.result
- Where the resulting value of the operation will be stored.
ArithmeticException
- Thrown if the modulus is zero or inverse doesn't exist.public static void gcd(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength, byte[] result, int resultOffset, int resultLength)
a
, beginning at
aOffset
, and the integer represented by the array b
, beginning at
bOffset
and stores the result in the array result
, beginning at
result[ resultOffset ]
.
a
- The byte array containing the first integer.aOffset
- The offset into a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.b
- The byte array containing the second integer.bOffset
- The offset into bLength
- The length of the integer in b
.result
- Where the resulting value of the operation will be stored.resultOffset
- The offset into result
where the result of the gcd will be stored.resultLength
- The length of the integer in result
.
Must be >= the maximum of aLength
and bLength
.public static void gcd(byte[] a, byte[] b, byte[] result)
a
and the integer represetned by the array b
, and stores the result in the array
result
must be at least as long as the longest of a
and b
.
a
- The byte array containing the first integer.b
- The byte array containing the second integer.result
- Where GCD( a, b ) will be stored.public static void exponent(byte[] base, int baseOffset, int baseLength, byte[] exponent, int exponentOffset, int exponentLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength) throws ArithmeticException
base
, exponent
and modulus
Takes basemodulus mod modulus
and stores the result in the resultLength
bytes of result
, beginning at result[ resultOffset ]
.
resultLength
must be at least as large as
modulusLength
.
Note:MODULUS
must be prime for exponentiate to work!
base
- The array containing the base in the exponentiation.baseOffset
- The offset into base
, where the first byte of the base is located.baseLength
- The length of the integer in base
.exponent
- The array containing the exponent.exponentOffset
- The offset into exponent
, where the first byte of the exponent is located.exponentLength
- The length of the integer in exponent
.modulus
- The array containing the modulus of the operation. Note:
this value must be prime.modulusOffset
- The offset into the modulus array, where the first byte
of the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular exponentiation will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
ArithmeticException
- Thrown if the modulus is zero.public static void exponent(byte[] base, byte[] exponent, byte[] modulus, byte[] result) throws ArithmeticException
base
, exponent
and modulus
Takes baseexponent mod modulus
and stores it in result
result
must be at least as large as modulus
.
base
- The array containing the base in the exponentiation.exponent
- The array containing the exponent.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular exponentiation will be stored.
result
must be at least as large as modulus
.
ArithmeticException
- Thrown if the modulus is zero.public static void increment(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength)
aLength
in the array beginning at
a[ aOffset ]
, mod the integer beginning at modulus[ modulusOffset ]
,
and stores the result in the array result
beginning at resultOffset
.
a
- The array containing the integer to be incremented.aOffset
- The offset into the first array, where the first byte of the integer is located.aLength
- The length of the integer in a
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where the first byte
of the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular addition will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
ArithmeticException
- Thrown if the modulus is zero.public static void increment(byte[] a, byte[] modulus, byte[] result)
a
, mod the integer
represented by the array modulus
, and stores the result in the array result
.
result
must be at least as long as modulus
.
a
- The array containing the first integer in the addition.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular addition will be stored.
ArithmeticException
- thrown if the modulus is zero.public static void increment(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a[ aOffset ]
, mod 2log2modulus, and
stores the result in the array result
, beginning at resultOffset
.
a
- The array containing the integer to have one added to.aOffset
- The offset into the array a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular addition will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
.public static void increment(byte[] a, int log2modulus, byte[] result)
a
,
mod 2log2modulus and stores the result in the array result
.
a
- The array containing the integer that will have one added to it.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular addition will be stored.public static void decrement(byte[] a, int aOffset, int aLength, byte[] modulus, int modulusOffset, int modulusLength, byte[] result, int resultOffset, int resultLength)
aLength
in the array beginning at
a[ aOffset ]
, mod the integer beginning at modulus[ modulusOffset ]
,
and stores the result in the array result
beginning at resultOffset
.
a
- The array containing the integer to be decremented.aOffset
- The offset into the first array, where the first byte of the integer is located.aLength
- The length of the integer in a
.modulus
- The array containing the modulus of the operation.modulusOffset
- The offset into the modluls array, where the first byte
of the modulus integer is located.modulusLength
- The length of the integer in modulus
.result
- The array where the result of the modular subtraction will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
. Must be >= modulusLength
ArithmeticException
- Thrown if the modulus is zero.public static void decrement(byte[] a, byte[] modulus, byte[] result)
a
, mod the integer
represented by the array modulus
, and stores the result in the array result
.
result
must be at least as long as modulus
.
a
- The array containing the first integer in the subtraction.modulus
- The array containing the modulus of the operation.result
- The array where the result of the modular subtraction will be stored.
ArithmeticException
- Thrown if the modulus is zero.public static void decrement(byte[] a, int aOffset, int aLength, int log2modulus, byte[] result, int resultOffset, int resultLength)
a[ aOffset ]
, mod 2log2modulus, and
stores the result in the array result
, beginning at resultOffset
.
a
- The array containing the integer to have one subtracted from.aOffset
- The offset into the array a
, where the first byte of the integer is located.aLength
- The length of the integer in a
.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8*resultLength
.result
- The array where the result of the modular addition will be stored.resultOffset
- The offset into the result array, where the resulting integer will be stored.resultLength
- The length of the integer in result
.
resultLength*8
must be at least log2modulus
.public static void decrement(byte[] a, int log2modulus, byte[] result)
a
,
mod 2log2modulus and stores the result in the array result
.
a
- The array containing the integer that will have one subtracted from it.log2modulus
- The log2 of the modulus. 0 <= log2modulus <= 8* the length of result
.result
- The array where the result of the modular subtraction will be stored.public static void divide(byte[] dividend, int dividendOffset, int dividendLength, byte[] divisor, int divisorOffset, int divisorLength, byte[] quotient, int quotientOffset, int quotientLength, byte[] remainder, int remainderOffset, int remainderLength) throws ArithmeticException
dividend
,
beginning at dividendOffset
, divided by the integer in the array
divisor
, beginning at divisorOffset
. The quotient is
stored in quotient
, beginning at quotientOffset
and the
remainder is stored in remainder
, beginning at
remainderOffset
.
After the operation, the identity dividend = divisor*quotient +
remainder
will hold.
NOTE: this operation is not modular arithmetic, like most of the rest of the methods in CryptoByteArrayArithmetic
dividend
- The array containing the integer which is the dividend.dividendOffset
- The offset into dividend
, where the first byte of the integer is located.dividendLength
- The length of the integer in dividend
.divisor
- The array containing the integer which is the divisor.divisorOffset
- The offset into divisor
, where the first byte of the integer is located.divisorLength
- The length of the integer in divisor
.quotient
- The output array which will contain the quotient of dividend / divisor
.quotientOffset
- The offset into quotient
, where the first byte of the integer will be.quotientLength
- The length of the integer in quotient
. Must be >= dividendLength
.remainder
- The output array which will contain the remainder of dividend / divisor
.remainderOffset
- The offset into remainder
, where the first byte of the integer will be.remainderLength
- The length of the integer in remainder
. Must be >= divisorLength
.
ArithmeticException
- Thrown if the divisor is zero.public static void divide(byte[] dividend, byte[] divisor, byte[] quotient, byte[] remainder) throws ArithmeticException
dividend / divisor
The quotient is stored in quotient
, and the remainder is stored in remainder
.
The array quotient
must be at least as long as dividend
and
the array remainder
must be at least as long as
divisor
.
After the operation, the identity dividend = divisor*quotient + remainder
will hold.
NOTE: this operation is not modular arithmetic, like most of the rest of the methods in CryptoByteArrayArithmetic
.
dividend
- The array containing the integer which is the dividend.divisor
- The array containing the integer which is the divisor.quotient
- The output array which will contain the quotient of dividend / divisor
.remainder
- The output array which will contain the remainder of dividend / divisor
.
ArithmeticException
- Thrown if the divisor is zero.public static byte[] trim(byte[] array)
array
- The byte array from which the leading zeros will be removed.
public static byte[] pad(byte[] array, int numberOfZeros)
array
- The byte array to be padded.numberOfZeros
- The number of zero bytes to pad array with.
public static byte[] ensureLength(byte[] array, int length) throws NumberFormatException
array
- The byte[] to be adjustedlength
- The length to be adjusted to
NumberFormatException
- Thrown if its not possible to adjust byte[] to the requested sizepublic static byte[] valueOf(long i)
i
into a byte array.
IllegalArgumentException
- if i < 0
public static long valueOf(byte[] array)
array
into a long integer (Note: the returned value should be considered as an unsigned value).
IllegalArgumentException
- if array
contains a number bigger than 64 bits.public static byte[] createArray(int log2n)
log2n
IllegalArgumentException
- if log2n < 0
CryptoByteArrayArithmetic.getNumBits(byte[])
public static int getNumBits(byte[] array)
array
.
IllegalArgumentException
- if array
is null.CryptoByteArrayArithmetic.createArray(int)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal