123 lines
4.4 KiB
Java
123 lines
4.4 KiB
Java
import Arithmetics.Z;
|
|
import Communication.Network;
|
|
import FeldmanVerifiableSecretSharing.VerifiableSecretSharing;
|
|
import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGeneration;
|
|
import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.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.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.Random;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* 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;
|
|
|
|
Set<Integer> QUAL = new HashSet<Integer>();
|
|
|
|
@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);
|
|
int abortedStage = 2;
|
|
for (int i = 1; i <= n; i++) {
|
|
BigInteger secret = new BigInteger(q.bitLength(), random).mod(q);
|
|
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]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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 = zpstar.multiply(g,secret);
|
|
for (int i: QUAL){
|
|
assert (dkgs[i - 1].getPublicValue().equals(publicValue));
|
|
}
|
|
|
|
// assert valid verification values
|
|
BigInteger expected,verification;
|
|
for (int i: QUAL){
|
|
expected = zpstar.multiply(g, dkgs[i - 1].getShare().y);
|
|
verification = VerifiableSecretSharing.verify(i, dkgs[i - 1].getCommitments(),zpstar);
|
|
assert (expected.equals(verification));
|
|
}
|
|
|
|
|
|
// 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);
|
|
}
|
|
|
|
BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,new Z()).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]);
|
|
}
|
|
}
|
|
}
|