Cryptography Reference
In-Depth Information
//Set up the prime moduli
private void setupModuli() {
//Don't know how long array should be-start with a Vector
Vector vector=new Vector();
BigInteger two=BigInteger.valueOf(2);
//Start with the largest possible int-this is an odd number
BigInteger test=BigInteger.valueOf(Integer.MAX_VALUE);
//bigBubba will be the product of all the primes
BigInteger bigBubba=BigInteger.valueOf(1);
//When this product is big enough (has long enough bit length) we have enough
//primes
while (bigBubba.bitLength()<maxModulusBitLength) {
//If test is prime, add it to the vector, and multiply bigBubba by it
if (test.isProbablePrime(10)) {
vector.addElement(test);
bigBubba=bigBubba.multiply(test);
}
//Subtract two from the test number-test is always odd
test=test.subtract(two);
}
//We know the size of our array of primes-create the array
moduli=new long[vector.size()];
//Copy the prime moduli into the array
for (int i=0;i<vector.size();i++)
moduli[i]=((BigInteger)vector.elementAt(i)).longValue();
}
The addition and multiplication methods follow. Note that the work is done entirely with
primitive types. The code is not written to do these operations in parallel; however, this
would be a great exercise for you.
public IntCRT add(IntCRT other) {
//IntCRT objects must be using the same moduli
if (maxModulusBitLength!=other.maxModulusBitLength) throw new
IllegalArgumentException
(“IntCRT objects must have same maximum modulus bit length to be added together”);
long[] answer=new long[residues.length];
//Add i-th residue of this to i-th residue of other, take residue mod i-th
//moduli
for (int i=0;i<residues.length;i++)
answer[i]=(residues[i]+other.residues[i])%moduli[i];
return new IntCRT(answer);
}
public IntCRT multiply(IntCRT other) {
//IntCRT objects must be using the same moduli
Search WWH ::




Custom Search