meerkat-java/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGTest.java

114 lines
4.0 KiB
Java
Raw Normal View History

2016-02-09 13:37:57 -05:00
package JointFeldmanProtocol;
2016-02-17 15:58:20 -05:00
import Communication.Network;
import ShamirSecretSharing.Polynomial;
import ShamirSecretSharing.SecretSharing;
import FeldmanVerifiableSecretSharing.VerifiableSecretSharing;
2016-02-23 12:02:49 -05:00
import UserInterface.DistributedKeyGenerationUser;
import org.factcenter.qilin.primitives.Group;
2016-02-09 13:37:57 -05:00
import org.factcenter.qilin.primitives.concrete.Zpstar;
import org.junit.Before;
import org.junit.Test;
import java.math.BigInteger;
import java.util.Random;
/**
* Created by Tzlil on 2/9/2016.
*/
public class DKGTest {
2016-02-23 12:02:49 -05:00
DistributedKeyGenerationUser[][] dkgsArrays;
2016-02-17 15:58:20 -05:00
Thread[][] threadsArrays;
int tests = 10;
BigInteger p = BigInteger.valueOf(2903);
BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
BigInteger[] secrets;
2016-02-09 13:37:57 -05:00
@Before
public void settings(){
Zpstar zpstar = new Zpstar(p);
2016-02-17 15:58:20 -05:00
Random random = new Random();
2016-02-09 13:37:57 -05:00
BigInteger g;
2016-02-17 15:58:20 -05:00
int t = 9;
int n = 20;
2016-02-09 13:37:57 -05:00
BigInteger ZERO = zpstar.zero();
2016-02-23 12:02:49 -05:00
dkgsArrays = new DistributedKeyGenerationUserImpl[tests][n];
2016-02-17 15:58:20 -05:00
threadsArrays = new Thread[tests][n];
secrets = new BigInteger[tests];
2016-02-23 12:02:49 -05:00
DistributedKeyGeneration dkg;
2016-02-17 15:58:20 -05:00
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*
secrets[test] = BigInteger.ZERO;
Network network = new Network(n);
for (int i = 0; i < n; i++) {
BigInteger secret = new BigInteger(q.bitLength(), random).mod(q);
secrets[test] = secrets[test].add(secret).mod(q);
2016-02-23 12:02:49 -05:00
dkg = new DistributedKeyGeneration(t,n,secret,random,q,g,zpstar,i + 1);
dkgsArrays[test][i] = new DistributedKeyGenerationUserImpl(dkg,network);
2016-02-17 15:58:20 -05:00
threadsArrays[test][i] = new Thread(dkgsArrays[test][i]);
}
2016-02-09 13:37:57 -05:00
}
}
2016-02-23 12:02:49 -05:00
public void oneTest(Thread[] threads, DistributedKeyGenerationUser[] dkgs,BigInteger secret) throws Exception {
2016-02-17 15:58:20 -05:00
for (int i = 0; i < threads.length ; i++){
2016-02-09 13:37:57 -05:00
threads[i].start();
}
2016-02-17 15:58:20 -05:00
for (int i = 0; i < threads.length ; i++){
2016-02-09 13:37:57 -05:00
threads[i].join();
}
2016-02-17 15:58:20 -05:00
int t = dkgs[0].getT();
int n = dkgs[0].getN();
2016-02-23 12:02:49 -05:00
Group<BigInteger> zpstar = dkgs[0].getGroup();
2016-02-17 15:58:20 -05:00
BigInteger g = dkgs[0].getGenerator();
2016-02-09 13:37:57 -05:00
2016-02-17 15:58:20 -05:00
// got the right public value
2016-02-23 12:02:49 -05:00
BigInteger publicValue = dkgs[0].getPublicValue();
assert(zpstar.multiply(g,secret).equals(publicValue));
2016-02-17 15:58:20 -05:00
// assert all players agreed on the same public value
2016-02-09 13:37:57 -05:00
for (int i = 0; i < dkgs.length - 1 ; i++){
2016-02-23 12:02:49 -05:00
assert (dkgs[i].getPublicValue().equals(dkgs[i+1].getPublicValue()));
2016-02-09 13:37:57 -05:00
}
2016-02-17 15:58:20 -05:00
// assert valid verification values
BigInteger expected,verification;
for (int j = 1; j <= dkgs.length ; j++){
expected = zpstar.multiply(g, dkgs[j - 1].getShare().y);
verification = VerifiableSecretSharing.verify(j, dkgs[j - 1].getCommitments(),zpstar);
assert (expected.equals(verification));
}
// restore the secret from t + 1 random shares
Polynomial.Point[] shares = new Polynomial.Point[t + 1];
for (int i = 0 ; i < shares.length; i++){
shares[i] = dkgs[i].getShare();
}
//List<Integer> indexes = new ArrayList<Integer>(n);
//for (int i = 1 ; i <= n; i ++){
// indexes.add(i);
//}
//Random random = new Random();
//int index;
//for (int i = 0 ; i < shares.length ; i++){
// index = indexes.remove(random.nextInt(indexes.size()));
// shares[i] = dkgs[index - 1].getShare();
//}
2016-02-23 12:02:49 -05:00
BigInteger calculatedSecret = SecretSharing.restoreSecrete(shares).mod(q);
2016-02-17 15:58:20 -05:00
assert (calculatedSecret.equals(secret));
}
@Test
2016-02-23 12:02:49 -05:00
public void DKGTest() throws Exception {
2016-02-17 15:58:20 -05:00
for (int i = 0 ; i < dkgsArrays.length; i ++){
oneTest(threadsArrays[i],dkgsArrays[i],secrets[i]);
}
2016-02-09 13:37:57 -05:00
}
}