FeldmanVSS tests

DKG
tzlil.gon 2016-01-29 22:08:13 +02:00
parent 8ba55bacd2
commit f8d31d16a3
12 changed files with 170 additions and 34 deletions

View File

@ -1,4 +1,4 @@
package ShamirSecretSharing; package FeldmanVerifiableSecretSharing.ShamirSecretSharing;
import java.math.BigInteger; import java.math.BigInteger;

View File

@ -1,8 +1,6 @@
package ShamirSecretSharing; package FeldmanVerifiableSecretSharing.ShamirSecretSharing;
import org.bouncycastle.util.Arrays; import org.bouncycastle.util.Arrays;
import org.factcenter.qilin.primitives.concrete.ECGroup;
import org.factcenter.qilin.util.Pair;
import java.math.BigInteger; import java.math.BigInteger;
@ -46,7 +44,7 @@ public class Polynomial implements Comparable<Polynomial> {
@Override @Override
public String toString() { public String toString() {
return "Polynomial{" + return "FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests{" +
"degree=" + degree + "degree=" + degree +
", coefficients=" + java.util.Arrays.toString(coefficients) + ", coefficients=" + java.util.Arrays.toString(coefficients) +
'}'; '}';
@ -71,7 +69,7 @@ public class Polynomial implements Comparable<Polynomial> {
* @param points * @param points
* @return polynomial of minimal degree which goes through all 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); LagrangePolynomial[] l = LagrangePolynomial.lagrangePolynomials(points);
// product = product of l[i].divisor // product = product of l[i].divisor
@ -102,7 +100,7 @@ public class Polynomial implements Comparable<Polynomial> {
/** /**
* @param other * @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) * new.image(x) = this.image(x) + other.image(x)
*/ */
public Polynomial add(Polynomial other){ public Polynomial add(Polynomial other){
@ -124,7 +122,7 @@ public class Polynomial implements Comparable<Polynomial> {
/** /**
* @param constant * @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) * new.image(x) = constant * this.image(x)
*/ */
public Polynomial mul(BigInteger constant){ public Polynomial mul(BigInteger constant){
@ -139,7 +137,7 @@ public class Polynomial implements Comparable<Polynomial> {
/** /**
* @param other * @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) * new.image(x) = this.image(x) * other.image(x)
*/ */
public Polynomial mul(Polynomial other){ public Polynomial mul(Polynomial other){

View File

@ -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.math.BigInteger;
import java.util.Random; import java.util.Random;
@ -14,6 +11,8 @@ import java.util.Random;
public class SecretSharing { public class SecretSharing {
protected final int t; protected final int t;
protected final int n; protected final int n;
protected final BigInteger p; protected final BigInteger p;
protected final Polynomial polynomial; protected final Polynomial polynomial;
@ -36,7 +35,7 @@ public class SecretSharing {
/** /**
* @param s * @param s
* @param random * @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 * 1. polynomial(0) = s
* 2. polynomial coefficients randomly chosen from Zp (except of coefficients[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 * @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); Polynomial polynomial = Polynomial.interpolation(shares);
return polynomial.image(BigInteger.ZERO); return polynomial.image(BigInteger.ZERO);
} }
@ -91,5 +90,11 @@ public class SecretSharing {
return n; return n;
} }
/**
* getter
* @return the prime was given in the constructor
*/
public BigInteger getP() {
return p;
}
} }

View File

@ -1,9 +1,8 @@
package FeldmanVerifiableSecretSharing; package FeldmanVerifiableSecretSharing;
import ShamirSecretSharing.SecretSharing; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.SecretSharing;
import org.bouncycastle.util.Arrays; import org.bouncycastle.util.Arrays;
import org.factcenter.qilin.primitives.CyclicGroup; import org.factcenter.qilin.primitives.CyclicGroup;
import org.factcenter.qilin.primitives.concrete.Zpstar;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Random; import java.util.Random;
@ -50,10 +49,11 @@ public class VerifiableSecretSharing extends SecretSharing {
*/ */
public static BigInteger verify(int i,BigInteger[] commitments,CyclicGroup<BigInteger> group) { public static BigInteger verify(int i,BigInteger[] commitments,CyclicGroup<BigInteger> group) {
BigInteger v = group.zero(); BigInteger v = group.zero();
int power = 1; BigInteger power = BigInteger.ONE;
BigInteger I = BigInteger.valueOf(i);
for (int j = 0 ; j < commitments.length ; j ++){ for (int j = 0 ; j < commitments.length ; j ++){
v = group.add(v,commitments[i].pow(power)); v = group.add(v,group.multiply(commitments[j],power));
power *=i; power = power.multiply(I);
} }
return v; return v;
} }
@ -74,4 +74,13 @@ public class VerifiableSecretSharing extends SecretSharing {
public BigInteger[] getCommitments() { public BigInteger[] getCommitments() {
return Arrays.clone(commitments); return Arrays.clone(commitments);
} }
/**
* getter
* @return the cyclic group was given in the constructor
*/
public CyclicGroup<BigInteger> getGroup() {
return group;
}
} }

View File

@ -1,5 +1,5 @@
package Polynomial; package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
import ShamirSecretSharing.Polynomial; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -1,6 +1,6 @@
package Polynomial; package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
import ShamirSecretSharing.Polynomial; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -47,13 +47,13 @@ public class InterpolationTest {
return points; 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); Polynomial interpolation = Polynomial.interpolation(points);
assert (p.compareTo(interpolation) == 0); assert (p.compareTo(interpolation) == 0);
} }
@Test @Test
public void interpolationTest(){ public void interpolationTest() throws Exception {
for (int i = 0; i < polynomials.length; i ++){ for (int i = 0; i < polynomials.length; i ++){
oneTest(polynomials[i],pointsArrays[i]); oneTest(polynomials[i],pointsArrays[i]);
} }

View File

@ -1,6 +1,6 @@
package Polynomial; package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
import ShamirSecretSharing.Polynomial; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -1,6 +1,6 @@
package Polynomial; package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
import ShamirSecretSharing.Polynomial; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -1,6 +1,6 @@
package Polynomial; package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
import ShamirSecretSharing.Polynomial; import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Random; import java.util.Random;

View File

@ -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<BigInteger> 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<Integer> indexes = new ArrayList<Integer>(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]);
}
}
}

View File

@ -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<BigInteger> 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<BigInteger> 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]);
}
}
}

View File

@ -1,4 +1,4 @@
#Wed Jan 27 00:30:26 IST 2016 #Fri Jan 29 21:00:29 IST 2016
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME