meerkat-java/destributed-key-generation/src/test/java/SDKGTest.java

123 lines
4.4 KiB
Java
Raw Normal View History

2016-03-18 08:20:47 -04:00
import Arithmetics.Z;
2016-02-23 12:02:49 -05:00
import Communication.Network;
import FeldmanVerifiableSecretSharing.VerifiableSecretSharing;
2016-03-01 09:49:55 -05:00
import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGeneration;
import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGenerationUserImpl;
2016-02-23 12:02:49 -05:00
import ShamirSecretSharing.Polynomial;
import ShamirSecretSharing.SecretSharing;
import UserInterface.DistributedKeyGenerationUser;
import org.factcenter.qilin.primitives.Group;
import org.factcenter.qilin.primitives.concrete.Zpstar;
import org.junit.Before;
import org.junit.Test;
import java.math.BigInteger;
2016-03-18 08:20:47 -04:00
import java.util.ArrayList;
import java.util.HashSet;
2016-02-23 12:02:49 -05:00
import java.util.Random;
2016-03-18 08:20:47 -04:00
import java.util.Set;
2016-02-23 12:02:49 -05:00
/**
* Created by Tzlil on 2/23/2016.
*/
public class SDKGTest {
DistributedKeyGenerationUser[][] sdkgsArrays;
Thread[][] threadsArrays;
int tests = 10;
BigInteger p = BigInteger.valueOf(2903);
BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
BigInteger[] secrets;
2016-03-18 08:20:47 -04:00
Set<Integer> QUAL = new HashSet<Integer>();
2016-02-23 12:02:49 -05:00
@Before
public void settings(){
Zpstar zpstar = new Zpstar(p);
Random random = new Random();
BigInteger g,h;
int t = 9;
int n = 20;
BigInteger ZERO = zpstar.zero();
sdkgsArrays = new SecureDistributedKeyGenerationUserImpl[tests][n];
threadsArrays = new Thread[tests][n];
secrets = new BigInteger[tests];
SecureDistributedKeyGeneration sdkg;
for (int test = 0; test < tests; test++) {
do {
g = zpstar.sample(random);
} while (!g.equals(ZERO) && !zpstar.multiply(g, q).equals(ZERO));// sample from QRZp*
h = zpstar.multiply(g,BigInteger.valueOf(2));
secrets[test] = BigInteger.ZERO;
Network network = new Network(n);
2016-03-18 08:20:47 -04:00
int abortedStage = 2;
for (int i = 1; i <= n; i++) {
2016-02-23 12:02:49 -05:00
BigInteger secret = new BigInteger(q.bitLength(), random).mod(q);
2016-03-18 08:20:47 -04:00
sdkg = new SecureDistributedKeyGeneration(t,n,secret,random,q,g,h,zpstar,i);
if(i == n) {
sdkgsArrays[test][i - 1] = new SDKGUserImplAbort(sdkg, network, abortedStage);
}
else {
sdkgsArrays[test][i - 1] = new SecureDistributedKeyGenerationUserImpl(sdkg, network);
QUAL.add(i);
}
if (abortedStage > 1 || (abortedStage == 1 && i != n)){
secrets[test] = secrets[test].add(secret).mod(q);
}
threadsArrays[test][i - 1] = new Thread(sdkgsArrays[test][i - 1]);
2016-02-23 12:02:49 -05:00
}
}
}
2016-03-18 08:20:47 -04:00
2016-02-23 12:02:49 -05:00
public void oneTest(Thread[] threads, DistributedKeyGenerationUser[] dkgs,BigInteger secret) throws Exception {
for (int i = 0; i < threads.length ; i++){
threads[i].start();
}
for (int i = 0; i < threads.length ; i++){
threads[i].join();
}
int t = dkgs[0].getT();
int n = dkgs[0].getN();
Group<BigInteger> zpstar = dkgs[0].getGroup();
BigInteger g = dkgs[0].getGenerator();
// got the right public value
2016-03-18 08:20:47 -04:00
BigInteger publicValue = zpstar.multiply(g,secret);
for (int i: QUAL){
assert (dkgs[i - 1].getPublicValue().equals(publicValue));
2016-02-23 12:02:49 -05:00
}
// assert valid verification values
BigInteger expected,verification;
2016-03-18 08:20:47 -04:00
for (int i: QUAL){
expected = zpstar.multiply(g, dkgs[i - 1].getShare().y);
verification = VerifiableSecretSharing.verify(i, dkgs[i - 1].getCommitments(),zpstar);
2016-02-23 12:02:49 -05:00
assert (expected.equals(verification));
}
2016-03-18 08:20:47 -04:00
// restore the secret from shares
ArrayList<Polynomial.Point> sharesList = new ArrayList<Polynomial.Point>();
Polynomial.Point[] shares = new Polynomial.Point[QUAL.size()];
for(int i : QUAL){
sharesList.add(dkgs[i - 1].getShare());
}
for (int i = 0; i < shares.length; i ++){
shares[i] = sharesList.get(i);
2016-02-23 12:02:49 -05:00
}
2016-03-18 08:20:47 -04:00
BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,new Z()).mod(q);
2016-02-23 12:02:49 -05:00
assert (calculatedSecret.equals(secret));
}
@Test
public void SDKGTest() throws Exception {
for (int i = 0 ; i < sdkgsArrays.length; i ++){
oneTest(threadsArrays[i],sdkgsArrays[i],secrets[i]);
}
}
}