diff --git a/distributed-key-generation/src/test/java/meerkat/crypto/dkg/gjkr/SDKGTest.java b/distributed-key-generation/src/test/java/meerkat/crypto/dkg/gjkr/SDKGTest.java index e4550f0..d172c23 100644 --- a/distributed-key-generation/src/test/java/meerkat/crypto/dkg/gjkr/SDKGTest.java +++ b/distributed-key-generation/src/test/java/meerkat/crypto/dkg/gjkr/SDKGTest.java @@ -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,58 +40,57 @@ public class SDKGTest { BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)); Group group = new Zpstar(p); Arithmetic 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++){ + 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)); + BigInteger publicValue = group.multiply(testable.g, testable.secret); + for (int i : testable.valids){ + assertEquals (testable.sdkgs[i-1].getPublicValue(), publicValue); } // assert valid verification values BigInteger expected,verification; - for (int i: testable.valids){ + 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 sharesList = new ArrayList(); + ArrayList sharesList = new ArrayList<>(); - for (int i: testable.valids){ + 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)); + BigInteger calculatedSecret = SecretSharing.recoverSecret(shares, arithmetic); + 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 valids; Set QUAL; @@ -118,14 +115,14 @@ public class SDKGTest { this.q = q; this.random = random; this.sdkgs = new User[n]; - this.valids = new HashSet(); - this.QUAL = new HashSet(); - this.aborted = new HashSet(); - this.malicious = new HashSet(); + 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 ids = new ArrayList(); + this.h = group.multiply(g, randomIntModQ(random)); + List ids = new ArrayList<>(); for (int id = 1; id<= n ; id++){ ids.add(id); } @@ -139,8 +136,8 @@ public class SDKGTest { id = ids.remove(random.nextInt(ids.size())); s = randomIntModQ(random); channel = channels.getChannel(id); - sdkg = new Protocol(t, n, s, random, q, g , h, group, id,encoder); - sdkgs[id - 1] = randomSDKGUser(id,channel,sdkg); + 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(sdkg,channel); + return new User<>(sdkg,channel); case FAILSTOP: int abortStage = random.nextInt(3) + 1; // 1 or 2 or 3 @@ -167,13 +164,13 @@ public class SDKGTest { if (abortStage > 1){ QUAL.add(id); } - return new SDKGUserImplAbort(sdkg,channel,abortStage); + return new SDKGUserImplAbort(sdkg, channel, abortStage); case MALICIOUS: malicious.add(id); - Set falls = DKGMaliciousUser.selectFallsRandomly(valids,random); - Protocol maliciousSDKG = SDKGMaliciousUserImpl.generateMaliciousSDKG(sdkg,channel,random); - return new SDKGMaliciousUserImpl(sdkg,maliciousSDKG,channel,falls); + Set falls = DKGMaliciousUser.selectFallsRandomly(valids, random); + Protocol maliciousSDKG = SDKGMaliciousUserImpl.generateMaliciousSDKG(sdkg, channel, random); + return new SDKGMaliciousUserImpl(sdkg, maliciousSDKG, channel, falls); } fail("Unknown user type"); @@ -203,4 +200,6 @@ public class SDKGTest { } } + + }