2016-01-27 06:41:24 -05:00
|
|
|
package ShamirSecretSharing;
|
|
|
|
|
|
|
|
|
2016-01-27 18:47:07 -05:00
|
|
|
import org.factcenter.qilin.primitives.CyclicGroup;
|
2016-01-27 06:41:24 -05:00
|
|
|
import org.factcenter.qilin.primitives.concrete.Zpstar;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Tzlil on 1/27/2016.
|
|
|
|
*/
|
|
|
|
public class SecretSharing {
|
2016-01-27 18:47:07 -05:00
|
|
|
protected final CyclicGroup<BigInteger> group;
|
2016-01-27 06:41:24 -05:00
|
|
|
protected final int t;
|
|
|
|
protected final int n;
|
|
|
|
protected final Polynomial polynomial;
|
|
|
|
|
2016-01-27 18:47:07 -05:00
|
|
|
public SecretSharing(CyclicGroup<BigInteger> group, int t, int n, BigInteger s, Random random) {
|
|
|
|
this.group = group;
|
2016-01-27 06:41:24 -05:00
|
|
|
this.t = t;
|
|
|
|
this.n = n;
|
|
|
|
this.polynomial = generateRandomPolynomial(s,random);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Polynomial generateRandomPolynomial(BigInteger s, Random random) {
|
|
|
|
BigInteger[] coefficients = new BigInteger[t + 1];
|
|
|
|
coefficients[0] = s;
|
2016-01-27 18:47:07 -05:00
|
|
|
BigInteger p = group.orderUpperBound();
|
|
|
|
int bits = p.bitLength();
|
2016-01-27 06:41:24 -05:00
|
|
|
for (int i = 1 ; i <= t; i++ ){
|
2016-01-27 18:47:07 -05:00
|
|
|
coefficients[i] = new BigInteger(bits,random).mod(p); // sample from Zp [0,... p-1]
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
|
|
|
return new Polynomial(coefficients);
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:47:07 -05:00
|
|
|
//ToDo make it safe : permission to call this func
|
2016-01-27 06:41:24 -05:00
|
|
|
public Polynomial.Point getShare(int i) throws Exception {
|
|
|
|
if(i < 1 || i > n){
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
return new Polynomial.Point(BigInteger.valueOf(i), polynomial.image(BigInteger.valueOf(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BigInteger getSecrete(Polynomial.Point[] shares){
|
|
|
|
Polynomial polynomial = Polynomial.interpolation(shares);
|
|
|
|
return polynomial.image(BigInteger.ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getThreshold() {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getN() {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|