meerkat-java/destributed-key-generation/src/main/java/ShamirSecretSharing/SecretSharing.java

107 lines
2.7 KiB
Java
Raw Normal View History

2016-02-17 15:58:20 -05:00
package ShamirSecretSharing;
2016-01-27 06:41:24 -05:00
2016-02-17 15:58:20 -05:00
import Communication.Network;
import Communication.User;
import meerkat.protobuf.DKGMessages;
2016-01-27 06:41:24 -05:00
import java.math.BigInteger;
import java.util.Random;
/**
* Created by Tzlil on 1/27/2016.
2016-01-28 06:57:47 -05:00
* an implementation of Shamire's secret sharing scheme
2016-01-27 06:41:24 -05:00
*/
2016-02-23 12:02:49 -05:00
public class SecretSharing{
2016-02-08 08:20:43 -05:00
protected final int t;
protected final int n;
protected final BigInteger q;
2016-02-23 12:02:49 -05:00
protected final Polynomial polynomial;
2016-01-27 06:41:24 -05:00
2016-01-28 06:57:47 -05:00
/**
* constructor
2016-02-05 06:30:16 -05:00
* @param q a large prime.
2016-01-28 06:57:47 -05:00
* @param t threshold. Any t+1 share holders can recover the secret,
* but any set of at most t share holders cannot
* @param n number of share holders
2016-02-05 06:30:16 -05:00
* @param x secret, chosen from Zq
2016-01-28 06:57:47 -05:00
* @param random use for generate random polynomial
*/
2016-02-23 12:02:49 -05:00
public SecretSharing(int t, int n, BigInteger x, Random random, BigInteger q) {
2016-02-05 06:30:16 -05:00
this.q = q;
2016-01-27 06:41:24 -05:00
this.t = t;
this.n = n;
2016-02-05 06:30:16 -05:00
this.polynomial = generateRandomPolynomial(x,random);
2016-01-27 06:41:24 -05:00
}
2016-01-28 06:57:47 -05:00
/**
2016-02-05 06:30:16 -05:00
* @param x
2016-01-28 06:57:47 -05:00
* @param random
2016-02-05 06:30:16 -05:00
* @return new Polynomial polynomial of degree t ,such that
* 1. polynomial(0) = x
* 2. polynomial coefficients randomly chosen from Zq (except of coefficients[0] = x)
2016-01-28 06:57:47 -05:00
*/
2016-02-05 06:30:16 -05:00
private Polynomial generateRandomPolynomial(BigInteger x, Random random) {
2016-01-27 06:41:24 -05:00
BigInteger[] coefficients = new BigInteger[t + 1];
2016-02-05 06:30:16 -05:00
coefficients[0] = x.mod(q);
int bits = q.bitLength();
2016-01-27 06:41:24 -05:00
for (int i = 1 ; i <= t; i++ ){
2016-02-05 06:30:16 -05:00
coefficients[i] = new BigInteger(bits,random).mod(q);
2016-01-27 06:41:24 -05:00
}
return new Polynomial(coefficients);
}
2016-01-28 06:57:47 -05:00
/**
* @param i in range of [1,...n]
*
2016-02-05 06:30:16 -05:00
* @return polynomial.image(i)%q
2016-01-28 06:57:47 -05:00
*/
2016-02-17 15:58:20 -05:00
public Polynomial.Point getShare(int i){
2016-02-05 06:30:16 -05:00
assert (i > 0 && i <= n);
return new Polynomial.Point(BigInteger.valueOf(i), polynomial, q);
2016-01-27 06:41:24 -05:00
}
2016-01-28 06:57:47 -05:00
/**
* @param shares - subset of the original shares
*
* @return image of interpolation(shares) at x = 0
*/
2016-02-23 12:02:49 -05:00
public static BigInteger restoreSecrete(Polynomial.Point[] shares) throws Exception {
2016-01-27 06:41:24 -05:00
Polynomial polynomial = Polynomial.interpolation(shares);
return polynomial.image(BigInteger.ZERO);
}
2016-01-28 06:57:47 -05:00
/**
* getter
* @return threshold
*/
2016-02-05 06:30:16 -05:00
public int getT() {
2016-01-27 06:41:24 -05:00
return t;
}
2016-01-28 06:57:47 -05:00
/**
* getter
* @return number of share holders
*/
2016-01-27 06:41:24 -05:00
public int getN() {
return n;
}
2016-01-29 15:08:13 -05:00
/**
* getter
* @return the prime was given in the constructor
*/
2016-02-05 06:30:16 -05:00
public BigInteger getQ() {
return q;
}
2016-02-23 12:02:49 -05:00
/**
* getter
* @return the polynomial was generated in constructor
*/
public Polynomial getPolynomial() {
2016-02-05 06:30:16 -05:00
return polynomial;
2016-01-29 15:08:13 -05:00
}
2016-01-27 06:41:24 -05:00
}