package ShamirSecretSharing; import org.factcenter.qilin.primitives.CyclicGroup; 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 CyclicGroup group; protected final int t; protected final int n; protected final Polynomial polynomial; public SecretSharing(CyclicGroup group, int t, int n, BigInteger s, Random random) { this.group = group; 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; BigInteger p = group.orderUpperBound(); int bits = p.bitLength(); for (int i = 1 ; i <= t; i++ ){ coefficients[i] = new BigInteger(bits,random).mod(p); // sample from Zp [0,... p-1] } return new Polynomial(coefficients); } //ToDo make it safe : permission to call this func 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; } }