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

57 lines
1.5 KiB
Java
Raw Normal View History

2016-01-27 06:41:24 -05:00
package ShamirSecretSharing;
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 {
protected final Zpstar zpstar;
protected final int t;
protected final int n;
protected final Polynomial polynomial;
public SecretSharing(Zpstar zpstar, int t, int n, BigInteger s, Random random) {
this.zpstar = zpstar;
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;
for (int i = 1 ; i <= t; i++ ){
coefficients[i] = zpstar.sample(random);
}
return new Polynomial(coefficients);
}
//ToDo make it safe : permission to call this func + modulo calc
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;
}
}