Make code of SDKG test prettier

mixer
Hai Brenner 2016-08-07 17:04:23 +03:00
parent 4ddd5f852a
commit ce40a04ac7
1 changed files with 35 additions and 36 deletions

View File

@ -10,6 +10,7 @@ import meerkat.crypto.secretsharing.shamir.Polynomial;
import meerkat.crypto.secretsharing.shamir.SecretSharing;
import meerkat.crypto.utils.BigIntegerByteEncoder;
import meerkat.crypto.utils.GenerateRandomPrime;
import meerkat.protobuf.Crypto;
import org.factcenter.qilin.primitives.Group;
import org.factcenter.qilin.primitives.concrete.Zpstar;
import org.factcenter.qilin.util.ByteEncoder;
@ -20,10 +21,7 @@ import org.junit.internal.runners.statements.Fail;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@ -42,22 +40,22 @@ public class SDKGTest {
BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
Group<BigInteger> group = new Zpstar(p);
Arithmetic<BigInteger> arithmetic = new Fp(q);
int t = 1;
int n = 20;
int initialT = 17;
int initialN = 20;
Random rand = new Random(1);
public void oneTest(Testable testable) throws Exception {
for (int i = 0; i < testable.sdkgs.length; i++){
testable.futures[i] = executorService.submit(testable.sdkgs[i]);
}
for (int i = 0; i < testable.futures.length ; i++){
testable.futures[i].get();
for (Future ftr : testable.futures) {
ftr.get();
}
// got the right public value
BigInteger publicValue = group.multiply(testable.g, testable.secret);
for (int i : testable.valids){
assert (testable.sdkgs[i - 1].getPublicValue().equals(publicValue));
assertEquals (testable.sdkgs[i-1].getPublicValue(), publicValue);
}
// assert valid verification values
@ -65,35 +63,34 @@ public class SDKGTest {
for (int i : testable.valids){
expected = group.multiply(testable.g, testable.sdkgs[i - 1].getShare().y);
verification = VerifiableSecretSharing.computeVerificationValue(i, testable.sdkgs[i - 1].getCommitments(), group);
assert (expected.equals(verification));
assertEquals (expected, verification);
}
// restore the secret from shares
ArrayList<Polynomial.Point> sharesList = new ArrayList<Polynomial.Point>();
ArrayList<Polynomial.Point> sharesList = new ArrayList<>();
for (int i : testable.valids){
sharesList.add(testable.sdkgs[i - 1].getShare());
}
Polynomial.Point[] shares = new Polynomial.Point[sharesList.size()];
for (int i = 0; i < shares.length; i ++){
shares[i] = sharesList.get(i);
}
shares = sharesList.toArray(shares);
BigInteger calculatedSecret = SecretSharing.recoverSecret(shares, arithmetic);
assert (calculatedSecret.equals(testable.secret));
assertEquals (calculatedSecret, testable.secret);
}
@Test
public void test() throws Exception {
public void runSharingProtocol() throws Exception {
Testable testable;
for (int i = 0; i < NUM_TESTS; i++) {
testable = new Testable(n, t, group, q, rand);
testable = new Testable(initialN+i, initialT+i, group, q, rand);
oneTest(testable);
}
}
static class Testable {
Set<Integer> valids;
Set<Integer> QUAL;
@ -118,14 +115,14 @@ public class SDKGTest {
this.q = q;
this.random = random;
this.sdkgs = new User[n];
this.valids = new HashSet<Integer>();
this.QUAL = new HashSet<Integer>();
this.aborted = new HashSet<Integer>();
this.malicious = new HashSet<Integer>();
this.valids = new HashSet<>();
this.QUAL = new HashSet<>();
this.aborted = new HashSet<>();
this.malicious = new HashSet<>();
this.futures = new Future[n];
this.g = sampleGenerator(random);
this.h = group.multiply(g, randomIntModQ(random));
ArrayList<Integer> ids = new ArrayList<Integer>();
List<Integer> ids = new ArrayList<>();
for (int id = 1; id<= n ; id++){
ids.add(id);
}
@ -139,7 +136,7 @@ public class SDKGTest {
id = ids.remove(random.nextInt(ids.size()));
s = randomIntModQ(random);
channel = channels.getChannel(id);
sdkg = new Protocol<BigInteger>(t, n, s, random, q, g , h, group, id,encoder);
sdkg = new Protocol<>(t, n, s, random, q, g , h, group, id, encoder);
sdkgs[id - 1] = randomSDKGUser(id, channel, sdkg);
if(QUAL.contains(id)){
this.secret = this.secret.add(s).mod(q);
@ -159,7 +156,7 @@ public class SDKGTest {
case HONEST:
valids.add(id);
QUAL.add(id);
return new User<BigInteger>(sdkg,channel);
return new User<>(sdkg,channel);
case FAILSTOP:
int abortStage = random.nextInt(3) + 1; // 1 or 2 or 3
@ -203,4 +200,6 @@ public class SDKGTest {
}
}
}