diff --git a/destributed-key-generation/src/main/java/ShamirSecretSharing/LagrangePolynomial.java b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/LagrangePolynomial.java similarity index 97% rename from destributed-key-generation/src/main/java/ShamirSecretSharing/LagrangePolynomial.java rename to destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/LagrangePolynomial.java index d3815ff..6754822 100644 --- a/destributed-key-generation/src/main/java/ShamirSecretSharing/LagrangePolynomial.java +++ b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/LagrangePolynomial.java @@ -1,4 +1,4 @@ -package ShamirSecretSharing; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing; import java.math.BigInteger; diff --git a/destributed-key-generation/src/main/java/ShamirSecretSharing/Polynomial.java b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/Polynomial.java similarity index 89% rename from destributed-key-generation/src/main/java/ShamirSecretSharing/Polynomial.java rename to destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/Polynomial.java index 620e1e7..ac340b7 100644 --- a/destributed-key-generation/src/main/java/ShamirSecretSharing/Polynomial.java +++ b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/Polynomial.java @@ -1,8 +1,6 @@ -package ShamirSecretSharing; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing; import org.bouncycastle.util.Arrays; -import org.factcenter.qilin.primitives.concrete.ECGroup; -import org.factcenter.qilin.util.Pair; import java.math.BigInteger; @@ -46,7 +44,7 @@ public class Polynomial implements Comparable { @Override public String toString() { - return "Polynomial{" + + return "FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests{" + "degree=" + degree + ", coefficients=" + java.util.Arrays.toString(coefficients) + '}'; @@ -71,7 +69,7 @@ public class Polynomial implements Comparable { * @param points * @return polynomial of minimal degree which goes through all points */ - public static Polynomial interpolation(Point[] points){ + public static Polynomial interpolation(Point[] points) throws Exception { LagrangePolynomial[] l = LagrangePolynomial.lagrangePolynomials(points); // product = product of l[i].divisor @@ -102,7 +100,7 @@ public class Polynomial implements Comparable { /** * @param other - * @return new Polynomial of degree max(this degree,other degree) s.t for all x in Z + * @return new FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests of degree max(this degree,other degree) s.t for all x in Z * new.image(x) = this.image(x) + other.image(x) */ public Polynomial add(Polynomial other){ @@ -124,7 +122,7 @@ public class Polynomial implements Comparable { /** * @param constant - * @return new Polynomial of degree this.degree s.t for all x in Z + * @return new FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests of degree this.degree s.t for all x in Z * new.image(x) = constant * this.image(x) */ public Polynomial mul(BigInteger constant){ @@ -139,7 +137,7 @@ public class Polynomial implements Comparable { /** * @param other - * @return new Polynomial of degree this degree + other degree + 1 s.t for all x in Z + * @return new FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests of degree this degree + other degree + 1 s.t for all x in Z * new.image(x) = this.image(x) * other.image(x) */ public Polynomial mul(Polynomial other){ diff --git a/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharing.java similarity index 87% rename from destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java rename to destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharing.java index d3c48c9..f5629d4 100644 --- a/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java +++ b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharing.java @@ -1,9 +1,6 @@ -package ShamirSecretSharing; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing; -import org.factcenter.qilin.primitives.CyclicGroup; -import org.factcenter.qilin.primitives.concrete.Zpstar; - import java.math.BigInteger; import java.util.Random; @@ -14,6 +11,8 @@ import java.util.Random; public class SecretSharing { protected final int t; protected final int n; + + protected final BigInteger p; protected final Polynomial polynomial; @@ -36,7 +35,7 @@ public class SecretSharing { /** * @param s * @param random - * @return new Polynomial polynomial of degree t ,such that + * @return new FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests polynomial of degree t ,such that * 1. polynomial(0) = s * 2. polynomial coefficients randomly chosen from Zp (except of coefficients[0] = s) */ @@ -70,7 +69,7 @@ public class SecretSharing { * * @return image of interpolation(shares) at x = 0 */ - public static BigInteger getSecrete(Polynomial.Point[] shares){ + public static BigInteger getSecrete(Polynomial.Point[] shares) throws Exception { Polynomial polynomial = Polynomial.interpolation(shares); return polynomial.image(BigInteger.ZERO); } @@ -91,5 +90,11 @@ public class SecretSharing { return n; } - + /** + * getter + * @return the prime was given in the constructor + */ + public BigInteger getP() { + return p; + } } diff --git a/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharing.java b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharing.java index 1554bf6..082177f 100644 --- a/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharing.java +++ b/destributed-key-generation/src/main/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharing.java @@ -1,9 +1,8 @@ package FeldmanVerifiableSecretSharing; -import ShamirSecretSharing.SecretSharing; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.SecretSharing; import org.bouncycastle.util.Arrays; import org.factcenter.qilin.primitives.CyclicGroup; -import org.factcenter.qilin.primitives.concrete.Zpstar; import java.math.BigInteger; import java.util.Random; @@ -50,10 +49,11 @@ public class VerifiableSecretSharing extends SecretSharing { */ public static BigInteger verify(int i,BigInteger[] commitments,CyclicGroup group) { BigInteger v = group.zero(); - int power = 1; + BigInteger power = BigInteger.ONE; + BigInteger I = BigInteger.valueOf(i); for (int j = 0 ; j < commitments.length ; j ++){ - v = group.add(v,commitments[i].pow(power)); - power *=i; + v = group.add(v,group.multiply(commitments[j],power)); + power = power.multiply(I); } return v; } @@ -74,4 +74,13 @@ public class VerifiableSecretSharing extends SecretSharing { public BigInteger[] getCommitments() { return Arrays.clone(commitments); } + + + /** + * getter + * @return the cyclic group was given in the constructor + */ + public CyclicGroup getGroup() { + return group; + } } diff --git a/destributed-key-generation/src/test/java/Polynomial/AddTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/AddTest.java similarity index 87% rename from destributed-key-generation/src/test/java/Polynomial/AddTest.java rename to destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/AddTest.java index 7e002a1..1df4fef 100644 --- a/destributed-key-generation/src/test/java/Polynomial/AddTest.java +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/AddTest.java @@ -1,5 +1,5 @@ -package Polynomial; -import ShamirSecretSharing.Polynomial; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; import org.junit.Before; import org.junit.Test; diff --git a/destributed-key-generation/src/test/java/Polynomial/InterpolationTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/InterpolationTest.java similarity index 88% rename from destributed-key-generation/src/test/java/Polynomial/InterpolationTest.java rename to destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/InterpolationTest.java index d08b0f2..71c93fa 100644 --- a/destributed-key-generation/src/test/java/Polynomial/InterpolationTest.java +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/InterpolationTest.java @@ -1,6 +1,6 @@ -package Polynomial; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests; -import ShamirSecretSharing.Polynomial; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; import org.junit.Before; import org.junit.Test; @@ -47,13 +47,13 @@ public class InterpolationTest { return points; } - public void oneTest(Polynomial p, Polynomial.Point[] points){ + public void oneTest(Polynomial p, Polynomial.Point[] points) throws Exception { Polynomial interpolation = Polynomial.interpolation(points); assert (p.compareTo(interpolation) == 0); } @Test - public void interpolationTest(){ + public void interpolationTest() throws Exception { for (int i = 0; i < polynomials.length; i ++){ oneTest(polynomials[i],pointsArrays[i]); } diff --git a/destributed-key-generation/src/test/java/Polynomial/MulByConstTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulByConstTest.java similarity index 87% rename from destributed-key-generation/src/test/java/Polynomial/MulByConstTest.java rename to destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulByConstTest.java index b0ddad2..6d9669a 100644 --- a/destributed-key-generation/src/test/java/Polynomial/MulByConstTest.java +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulByConstTest.java @@ -1,6 +1,6 @@ -package Polynomial; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests; -import ShamirSecretSharing.Polynomial; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; import org.junit.Before; import org.junit.Test; diff --git a/destributed-key-generation/src/test/java/Polynomial/MulTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulTest.java similarity index 88% rename from destributed-key-generation/src/test/java/Polynomial/MulTest.java rename to destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulTest.java index a60c986..fd4d761 100644 --- a/destributed-key-generation/src/test/java/Polynomial/MulTest.java +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/MulTest.java @@ -1,6 +1,6 @@ -package Polynomial; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests; -import ShamirSecretSharing.Polynomial; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; import org.junit.Before; import org.junit.Test; diff --git a/destributed-key-generation/src/test/java/Polynomial/Utils.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/Utils.java similarity index 76% rename from destributed-key-generation/src/test/java/Polynomial/Utils.java rename to destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/Utils.java index e52dd9b..b43869d 100644 --- a/destributed-key-generation/src/test/java/Polynomial/Utils.java +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/PolynomialTests/Utils.java @@ -1,6 +1,6 @@ -package Polynomial; +package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests; -import ShamirSecretSharing.Polynomial; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; import java.math.BigInteger; import java.util.Random; diff --git a/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharingTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharingTest.java new file mode 100644 index 0000000..d5f8842 --- /dev/null +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/ShamirSecretSharing/SecretSharingTest.java @@ -0,0 +1,60 @@ +package FeldmanVerifiableSecretSharing.ShamirSecretSharing; + +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests.Utils; +import org.factcenter.qilin.primitives.CyclicGroup; +import org.factcenter.qilin.primitives.concrete.Zn; +import org.junit.Before; +import org.junit.Test; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Created by Tzlil on 1/29/2016. + */ +public class SecretSharingTest { + + SecretSharing[] secretSharingArray; + BigInteger[] secrets; + CyclicGroup group; + int tests = 1 << 10; + Random random; + + @Before + public void settings(){ + BigInteger p = BigInteger.valueOf(2903); + group = new Zn(p); + int t = 10; + int n = 20; + random = new Random(); + secretSharingArray = new SecretSharing[tests]; + secrets = new BigInteger[tests]; + for (int i = 0; i < secretSharingArray.length; i++){ + secrets[i] = group.sample(random); + secretSharingArray[i] = new SecretSharing(p,t,n,secrets[i],random); + } + } + + public void oneTest(SecretSharing secretSharing, BigInteger secret) throws Exception { + int t = secretSharing.getThreshold(); + int n = secretSharing.getN(); + Polynomial.Point[] shares = new Polynomial.Point[t + 1]; + List indexes = new ArrayList(n); + for (int i = 1 ; i <= n; i ++){ + indexes.add(i); + } + for (int i = 0 ; i < shares.length ; i++){ + shares[i] = secretSharing.getShare(indexes.remove(random.nextInt(indexes.size()))); + } + assert(secret.equals(SecretSharing.getSecrete(shares))); + } + + @Test + public void secretSharingTest() throws Exception { + for (int i = 0 ; i < secretSharingArray.length; i ++){ + oneTest(secretSharingArray[i],secrets[i]); + } + } +} diff --git a/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharingTest.java b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharingTest.java new file mode 100644 index 0000000..8846cd9 --- /dev/null +++ b/destributed-key-generation/src/test/java/FeldmanVerifiableSecretSharing/VerifiableSecretSharingTest.java @@ -0,0 +1,64 @@ +package FeldmanVerifiableSecretSharing; + +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial; +import FeldmanVerifiableSecretSharing.ShamirSecretSharing.SecretSharing; +import org.factcenter.qilin.primitives.CyclicGroup; +import org.factcenter.qilin.primitives.concrete.Zn; +import org.junit.Before; +import org.junit.Test; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Created by Tzlil on 1/29/2016. + */ +public class VerifiableSecretSharingTest { + + + VerifiableSecretSharing[] verifiableSecretSharingArray; + int tests = 1 << 10; + Random random; + + @Before + public void settings(){ + BigInteger p = BigInteger.valueOf(2903); + CyclicGroup group = new Zn(p); + int t = 10; + int n = 20; + random = new Random(); + verifiableSecretSharingArray = new VerifiableSecretSharing[tests]; + for (int i = 0; i < verifiableSecretSharingArray.length; i++){ + verifiableSecretSharingArray[i] = new VerifiableSecretSharing(group,t,n,group.sample(random),random); + } + } + + public void oneTest(VerifiableSecretSharing verifiableSecretSharing) throws Exception { + int n = verifiableSecretSharing.getN(); + BigInteger p = verifiableSecretSharing.getP(); + CyclicGroup group = verifiableSecretSharing.getGroup(); + BigInteger g = verifiableSecretSharing.getGenerator(); + Polynomial.Point[] shares = new Polynomial.Point[n]; + BigInteger[] commitments = verifiableSecretSharing.getCommitments(); + BigInteger[] verifications = new BigInteger[n]; + for (int i = 1 ; i <= shares.length; i ++){ + shares[i - 1] = verifiableSecretSharing.getShare(i); + verifications[i - 1] = VerifiableSecretSharing.verify(i,commitments,group); + } + BigInteger expected; + for (int i = 0 ; i < shares.length ; i++){ + expected = group.multiply(g,shares[i].y).mod(p); // problem with Zn, multiplication doesn't mod n as required + assert (expected.equals(verifications[i])); + } + + } + + @Test + public void secretSharingTest() throws Exception { + for (int i = 0 ; i < verifiableSecretSharingArray.length; i ++){ + oneTest(verifiableSecretSharingArray[i]); + } + } +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1360c1c..306cc66 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Wed Jan 27 00:30:26 IST 2016 +#Fri Jan 29 21:00:29 IST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME