114 lines
4.0 KiB
Java
114 lines
4.0 KiB
Java
package JointFeldmanProtocol;
|
|
|
|
import Communication.Network;
|
|
import ShamirSecretSharing.Polynomial;
|
|
import ShamirSecretSharing.SecretSharing;
|
|
import FeldmanVerifiableSecretSharing.VerifiableSecretSharing;
|
|
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;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* Created by Tzlil on 2/9/2016.
|
|
*/
|
|
public class DKGTest {
|
|
|
|
|
|
DistributedKeyGenerationUser[][] dkgsArrays;
|
|
Thread[][] threadsArrays;
|
|
int tests = 10;
|
|
BigInteger p = BigInteger.valueOf(2903);
|
|
BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
|
|
BigInteger[] secrets;
|
|
@Before
|
|
public void settings(){
|
|
Zpstar zpstar = new Zpstar(p);
|
|
Random random = new Random();
|
|
BigInteger g;
|
|
int t = 9;
|
|
int n = 20;
|
|
BigInteger ZERO = zpstar.zero();
|
|
dkgsArrays = new DistributedKeyGenerationUserImpl[tests][n];
|
|
threadsArrays = new Thread[tests][n];
|
|
secrets = new BigInteger[tests];
|
|
DistributedKeyGeneration dkg;
|
|
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);
|
|
dkg = new DistributedKeyGeneration(t,n,secret,random,q,g,zpstar,i + 1);
|
|
dkgsArrays[test][i] = new DistributedKeyGenerationUserImpl(dkg,network);
|
|
threadsArrays[test][i] = new Thread(dkgsArrays[test][i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
BigInteger publicValue = dkgs[0].getPublicValue();
|
|
assert(zpstar.multiply(g,secret).equals(publicValue));
|
|
|
|
// assert all players agreed on the same public value
|
|
for (int i = 0; i < dkgs.length - 1 ; i++){
|
|
assert (dkgs[i].getPublicValue().equals(dkgs[i+1].getPublicValue()));
|
|
}
|
|
|
|
// 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();
|
|
//}
|
|
BigInteger calculatedSecret = SecretSharing.restoreSecrete(shares).mod(q);
|
|
assert (calculatedSecret.equals(secret));
|
|
|
|
}
|
|
|
|
@Test
|
|
public void DKGTest() throws Exception {
|
|
for (int i = 0 ; i < dkgsArrays.length; i ++){
|
|
oneTest(threadsArrays[i],dkgsArrays[i],secrets[i]);
|
|
}
|
|
}
|
|
}
|