114 lines
4.2 KiB
Java
114 lines
4.2 KiB
Java
|
import Communication.Network;
|
||
|
import FeldmanVerifiableSecretSharing.VerifiableSecretSharing;
|
||
|
import SecureDistributedKeyGeneration.SecureDistributedKeyGeneration;
|
||
|
import SecureDistributedKeyGeneration.SecureDistributedKeyGenerationUserImpl;
|
||
|
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;
|
||
|
import java.util.Random;
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
@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);
|
||
|
for (int i = 0; i < n; i++) {
|
||
|
BigInteger secret = new BigInteger(q.bitLength(), random).mod(q);
|
||
|
secrets[test] = secrets[test].add(secret).mod(q);
|
||
|
sdkg = new SecureDistributedKeyGeneration(t,n,secret,random,q,g,h,zpstar,i + 1);
|
||
|
sdkgsArrays[test][i] = new SecureDistributedKeyGenerationUserImpl(sdkg,network);
|
||
|
threadsArrays[test][i] = new Thread(sdkgsArrays[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 SDKGTest() throws Exception {
|
||
|
for (int i = 0 ; i < sdkgsArrays.length; i ++){
|
||
|
oneTest(threadsArrays[i],sdkgsArrays[i],secrets[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|