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