diff --git a/destributed-key-generation/src/main/java/Communication/Network.java b/destributed-key-generation/src/main/java/Communication/Network.java index af7e5d6..ca53991 100644 --- a/destributed-key-generation/src/main/java/Communication/Network.java +++ b/destributed-key-generation/src/main/java/Communication/Network.java @@ -3,7 +3,10 @@ package Communication; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Message; import meerkat.protobuf.DKGMessages.*; + +import java.util.HashSet; import java.util.Queue; +import java.util.Set; import java.util.concurrent.ArrayBlockingQueue; /** * Created by Tzlil on 2/7/2016. @@ -15,23 +18,23 @@ public class Network { protected final User[] users; protected final int n; - protected final Queue availableIDs; + protected final Set availableIDs; public static final int BROADCAST = 0; public Network(int n) { this.n = n; this.users = new User[n]; - this.availableIDs = new ArrayBlockingQueue(n); + this.availableIDs = new HashSet(); for (int id = 1; id <= n; id++){ availableIDs.add(id); } } - public User connect(MailHandler mailHandler){ - Integer id = availableIDs.poll(); - if (id == null) + public User connect(MailHandler mailHandler,int id){ + if (!availableIDs.contains(id)) return null; + availableIDs.remove(id); users[id - 1] = new User(id,this,mailHandler); return users[id - 1]; } diff --git a/destributed-key-generation/src/main/java/JointFeldmanProtocol/DistributedKeyGenerationUserImpl.java b/destributed-key-generation/src/main/java/JointFeldmanProtocol/DistributedKeyGenerationUserImpl.java index f633258..578b1e5 100644 --- a/destributed-key-generation/src/main/java/JointFeldmanProtocol/DistributedKeyGenerationUserImpl.java +++ b/destributed-key-generation/src/main/java/JointFeldmanProtocol/DistributedKeyGenerationUserImpl.java @@ -50,7 +50,7 @@ public class DistributedKeyGenerationUserImpl implements DistributedKeyGeneratio this.messageHandler = new MessageHandler(); mailHandler.setMessageHandler(this.messageHandler); - this.user = network.connect(mailHandler); + this.user = network.connect(mailHandler,dkg.getId()); this.parties = dkg.getParties(); this.parties[id - 1].share = dkg.getShare(id); @@ -230,7 +230,7 @@ public class DistributedKeyGenerationUserImpl implements DistributedKeyGeneratio if(isValidComplaintMessage(sender,isBroadcast,complaintMessage)){ int i = sender; int j = complaintMessage.getId(); - parties[i - 1].complaints[j - 1] = ComplainState.Waiting; + parties[j - 1].complaints[i - 1] = ComplainState.Waiting; } } diff --git a/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java b/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java index cdd631d..877d272 100644 --- a/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java +++ b/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java @@ -1,6 +1,7 @@ package ShamirSecretSharing; import Arithmetics.Arithmetic; +import Arithmetics.Fp; import java.math.BigInteger; import java.util.Random; @@ -45,7 +46,7 @@ public class SecretSharing{ for (int i = 1 ; i <= t; i++ ){ coefficients[i] = new BigInteger(bits,random).mod(q); } - return new Polynomial(coefficients); + return new Polynomial(coefficients,new Fp(q)); } /** diff --git a/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGDeepTest.java b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGDeepTest.java new file mode 100644 index 0000000..a50c75c --- /dev/null +++ b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGDeepTest.java @@ -0,0 +1,174 @@ +package JointFeldmanProtocol; + +import Arithmetics.Arithmetic; +import Arithmetics.Fp; +import Communication.Network; +import FeldmanVerifiableSecretSharing.VerifiableSecretSharing; +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.lang.reflect.Array; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +/** + * Created by Tzlil on 3/21/2016. + */ +public class DKGDeepTest { + + int tests = 10; + BigInteger p = BigInteger.valueOf(2903); + BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)); + Group group = new Zpstar(p); + Arithmetic arithmetic = new Fp(q); + int t = 9; + int n = 20; + + Testable[] testables; + + @Before + public void settings(){ + testables = new Testable[n]; + for (int i = 0; i < tests; i++){ + testables[i] = new Testable(new Random()); + } + } + + public void oneTest(int test) throws Exception { + Testable testable = testables[test]; + for (int i = 0; i < testable.threads.length ; i++){ + testable.threads[i].start(); + } + for (int i = 0; i < testable.threads.length ; i++){ + testable.threads[i].join(); + } + + // got the right public value + BigInteger publicValue = group.multiply(testable.g,testable.secret); + for (int i: testable.QUAL){ + if(!testable.aborted.contains(i)) + assert (testable.dkgs[i - 1].getPublicValue().equals(publicValue)); + } + + // assert valid verification values + BigInteger expected,verification; + for (int i: testable.QUAL){ + if(!testable.aborted.contains(i)) { + expected = group.multiply(testable.g, testable.dkgs[i - 1].getShare().y); + verification = VerifiableSecretSharing.verify(i, testable.dkgs[i - 1].getCommitments(), group); + assert (expected.equals(verification)); + } + } + + + // restore the secret from shares + ArrayList sharesList = new ArrayList(); + + for(int i : testable.QUAL){ + if(!testable.aborted.contains(i)) + sharesList.add(testable.dkgs[i - 1].getShare()); + } + Polynomial.Point[] shares = new Polynomial.Point[sharesList.size()]; + for (int i = 0; i < shares.length; i ++){ + shares[i] = sharesList.get(i); + } + + BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,arithmetic); + assert (calculatedSecret.equals(testable.secret)); + } + + @Test + public void test() throws Exception { + for (int i = 0; i < tests; i++){ + oneTest(i); + } + } + + class Testable{ + Set QUAL; + Set aborted; + Set malicious; + DistributedKeyGenerationUser[] dkgs; + Thread[] threads; + BigInteger g; + BigInteger secret; + public Testable(Random random) { + + this.dkgs = new DistributedKeyGenerationUserImpl[n]; + this.QUAL = new HashSet(); + this.aborted = new HashSet(); + this.malicious = new HashSet(); + this.threads = new Thread[n]; + this.g = sampleGenerator(random); + ArrayList ids = new ArrayList(); + for (int id = 1; id<= n ; id++){ + ids.add(id); + } + Network network = new Network(n); + int id; + BigInteger s; + DistributedKeyGeneration dkg; + this.secret = BigInteger.ZERO; + while (!ids.isEmpty()) { + id = ids.remove(random.nextInt(ids.size())); + s = randomIntModQ(random); + dkg = new DistributedKeyGeneration(t, n, s, random, q, g, group, id); + dkgs[id - 1] = randomDKGUser(id,network,dkg,random); + threads[id - 1] = new Thread(dkgs[id - 1]); + if(QUAL.contains(id)){ + this.secret = this.secret.add(s).mod(q); + } + } + + } + + public DistributedKeyGenerationUser randomDKGUser(int id,Network network, DistributedKeyGeneration dkg,Random random){ + if (QUAL.size() <= t) { + QUAL.add(id); + return new DistributedKeyGenerationUserImpl(dkg,network); + }else{ + int type = random.nextInt(3); + switch (type){ + case 0:// regular + QUAL.add(id); + return new DistributedKeyGenerationUserImpl(dkg,network); + case 1:// abort + int abortStage = random.nextInt(2) + 1; // 1 or 2 + aborted.add(id); + if (abortStage == 2){ + QUAL.add(id); + } + return new DKGUserImplAbort(dkg,network,abortStage); + case 2:// malicious + malicious.add(id); + return new DKGMaliciousUserImpl(dkg,network,random); + default: + return null; + } + } + } + + public BigInteger sampleGenerator(Random random){ + BigInteger ZERO = group.zero(); + BigInteger g; + do { + g = group.sample(random); + } while (!g.equals(ZERO) && !group.multiply(g, q).equals(ZERO)); + return g; + } + + public BigInteger randomIntModQ(Random random){ + return new BigInteger(q.bitLength(), random).mod(q); + } + + + } +} diff --git a/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGMaliciousUserImpl.java b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGMaliciousUserImpl.java new file mode 100644 index 0000000..e3261f8 --- /dev/null +++ b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGMaliciousUserImpl.java @@ -0,0 +1,72 @@ +package JointFeldmanProtocol; + +import Communication.MailHandler; +import Communication.Network; + +import java.math.BigInteger; +import java.util.*; + +/** + * Created by Tzlil on 3/21/2016. + */ +public class DKGMaliciousUserImpl extends DistributedKeyGenerationUserImpl { + + private final DistributedKeyGeneration maliciousDkg; + private final Set falls; + public DKGMaliciousUserImpl(DistributedKeyGeneration dkg, Network network, Random random) { + super(dkg, network); + this.falls = selectFalls(random); + this.maliciousDkg = new DistributedKeyGeneration(t,n,randomInt(random),random,dkg.getQ(),g,group,id); + maliciousDkg.setParties(parties); + } + + public Set selectFalls(Random random){ + ArrayList ids = new ArrayList(); + for (int i = 1; i<= n ; i++){ + if(i!=id) { + ids.add(i); + } + } + Set falls = new HashSet(); + int fallsSize = random.nextInt(ids.size()) + 1;// 1 - (n-1) + while (falls.size() < fallsSize){ + falls.add(ids.remove(random.nextInt(ids.size()))); + } + return falls; + } + + @Override + public void stage1() { + dkg.broadcastCommitments(user); + sendSecrets(); //insteadof dkg.sendSecrets(user); + } + + @Override + public void stage3() { + maliciousDkg.answerAllComplainingPlayers(user); + } + + @Override + public void stage4(){ + // do nothing + } + + private BigInteger randomInt(Random random){ + BigInteger q = dkg.getQ(); + return new BigInteger(q.bitLength(), random).mod(q); + } + + private void sendSecrets(){ + for (int j = 1; j <= n ; j++){ + if(j != id){ + if(falls.contains(j)){ + maliciousDkg.sendSecret(user,j); + }else { + dkg.sendSecret(user, j); + } + } + } + } + + +} diff --git a/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGTest.java b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGTest.java index b74f258..b50c46e 100644 --- a/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGTest.java +++ b/destributed-key-generation/src/test/java/JointFeldmanProtocol/DKGTest.java @@ -1,5 +1,7 @@ package JointFeldmanProtocol; +import Arithmetics.Arithmetic; +import Arithmetics.Fp; import Arithmetics.Z; import Communication.Network; import ShamirSecretSharing.Polynomial; @@ -27,10 +29,12 @@ public class DKGTest { BigInteger q = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)); BigInteger[] secrets; Set QUAL = new HashSet(); + Arithmetic arithmetic; @Before public void settings(){ Zpstar zpstar = new Zpstar(p); Random random = new Random(); + arithmetic = new Fp(q); BigInteger g; int t = 9; int n = 20; @@ -39,7 +43,7 @@ public class DKGTest { threadsArrays = new Thread[tests][n]; secrets = new BigInteger[tests]; DistributedKeyGeneration dkg; - int abortedStage = 2; + int abortedStage = 1; for (int test = 0; test < tests; test++) { do { g = zpstar.sample(random); @@ -51,7 +55,7 @@ public class DKGTest { dkg = new DistributedKeyGeneration(t,n,secret,random,q,g,zpstar,i); if(i == n) { - dkgsArrays[test][i - 1] = new DKGUserImplAbort(dkg, network, abortedStage); + dkgsArrays[test][i - 1] = new DKGMaliciousUserImpl(dkg,network,random);//new DKGUserImplAbort(dkg, network, abortedStage); } else { dkgsArrays[test][i - 1] = new DistributedKeyGenerationUserImpl(dkg, network); @@ -104,7 +108,7 @@ public class DKGTest { shares[i] = sharesList.get(i); } - BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,new Z()).mod(q); + BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,arithmetic); assert (calculatedSecret.equals(secret)); } diff --git a/destributed-key-generation/src/test/java/SDKGTest.java b/destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGTest.java similarity index 94% rename from destributed-key-generation/src/test/java/SDKGTest.java rename to destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGTest.java index 5866063..107f87c 100644 --- a/destributed-key-generation/src/test/java/SDKGTest.java +++ b/destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGTest.java @@ -1,8 +1,10 @@ +package SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem; + +import Arithmetics.Arithmetic; +import Arithmetics.Fp; import Arithmetics.Z; import Communication.Network; import FeldmanVerifiableSecretSharing.VerifiableSecretSharing; -import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGeneration; -import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGenerationUserImpl; import ShamirSecretSharing.Polynomial; import ShamirSecretSharing.SecretSharing; import UserInterface.DistributedKeyGenerationUser; @@ -31,10 +33,13 @@ public class SDKGTest { Set QUAL = new HashSet(); + Arithmetic arithmetic; + @Before public void settings(){ Zpstar zpstar = new Zpstar(p); Random random = new Random(); + arithmetic = new Fp(q); BigInteger g,h; int t = 9; int n = 20; @@ -108,7 +113,7 @@ public class SDKGTest { shares[i] = sharesList.get(i); } - BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,new Z()).mod(q); + BigInteger calculatedSecret = SecretSharing.restoreSecret(shares,arithmetic); assert (calculatedSecret.equals(secret)); } diff --git a/destributed-key-generation/src/test/java/SDKGUserImplAbort.java b/destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGUserImplAbort.java similarity index 95% rename from destributed-key-generation/src/test/java/SDKGUserImplAbort.java rename to destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGUserImplAbort.java index bc30eb5..fc777d3 100644 --- a/destributed-key-generation/src/test/java/SDKGUserImplAbort.java +++ b/destributed-key-generation/src/test/java/SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem/SDKGUserImplAbort.java @@ -1,3 +1,5 @@ +package SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem; + import Communication.Network; import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGeneration; import SecureDistributedKeyGenerationForDiscreteLogBasedCryptosystem.SecureDistributedKeyGenerationUserImpl;