Page MenuHomec4science

LagrangeInterpolation.java
No OneTemporary

File Metadata

Created
Tue, Dec 31, 00:07

LagrangeInterpolation.java

package ch.epfl.lca1.crypto;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.asn1.x9.X9ECParameters;
public class LagrangeInterpolation {
/**
* @param args
*/
public static void main(String[] args) {
ECElGamal ecceg = new ECElGamal();
SecureRandom randGen = new SecureRandom();
BigInteger order = ecceg.getECParams().getN();
BigInteger secret = new BigInteger(order.bitLength() - 1, randGen);
BigInteger a1 = new BigInteger(order.bitLength() - 1, randGen);
BigInteger[] coefficients = new BigInteger[2];
coefficients[0] = secret;
coefficients[1] = a1;
BigInteger first = getPartialSecret(1, coefficients, order);
BigInteger second = getPartialSecret(2, coefficients, order);
ECElGamal eccegCI = new ECElGamal(ecceg.getECParams(), secret);
ECElGamal eccegMU = new ECElGamal(ecceg.getECParams(), first);
ECElGamal eccegSPU = new ECElGamal(ecceg.getECParams(), second);
eccegMU.setPartNumber(ECElGamal.PART_MU);
eccegSPU.setPartNumber(ECElGamal.PART_SPU);
eccegMU.initReverseDLTable();
eccegSPU.initReverseDLTable();
BigInteger message = BigInteger.valueOf(123L);
FragmentEncrypted encryptedMessage = eccegCI.encrypt(message);
FragmentPartial partialMU = eccegMU.partialDecrypt(encryptedMessage);
FragmentPartial partialSPU = eccegSPU.partialDecrypt(encryptedMessage);
BigInteger messageReassembled = eccegMU.fullDecrypt(partialMU, partialSPU);
System.out.println(message);
System.out.println(messageReassembled);
/*
* secret = 2 * SK_1 - SK_2 mod q
*/
// BigInteger recovered = first.multiply(BigInteger.valueOf(2)).subtract(second).mod(order);
//
// System.out.println(recovered.equals(secret));
}
public static BigInteger getPartialSecret(int partNumber, BigInteger[] coefficients, BigInteger order) {
BigInteger result = BigInteger.ZERO;
for (int i = 0; i < coefficients.length; i++) {
int xPow = (int) Math.pow(partNumber, i);
BigInteger term = coefficients[i].multiply(BigInteger.valueOf(xPow));
result = result.add(term);
}
return result.mod(order);
}
}

Event Timeline