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. * an implementation of Shamire's secret sharing scheme */ public class SecretSharing { protected final int t; protected final int n; protected final BigInteger p; protected final Polynomial polynomial; /** * constructor * @param p prime * @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 * @param s secret, chosen from Zp * @param random use for generate random polynomial */ public SecretSharing(BigInteger p, int t, int n, BigInteger s, Random random) { this.p = p; this.t = t; this.n = n; this.polynomial = generateRandomPolynomial(s,random); } /** * @param s * @param random * @return new Polynomial polynomial of degree t ,such that * 1. polynomial(0) = s * 2. polynomial coefficients randomly chosen from Zp (except of coefficients[0] = s) */ private Polynomial generateRandomPolynomial(BigInteger s, Random random) { BigInteger[] coefficients = new BigInteger[t + 1]; coefficients[0] = s; int bits = p.bitLength(); for (int i = 1 ; i <= t; i++ ){ coefficients[i] = new BigInteger(bits,random).mod(p); } return new Polynomial(coefficients); } //ToDo make it safe : permission to call this func /** * @param i in range of [1,...n] * * @return polynomial.image(i) * * @throws Exception i out of range */ public Polynomial.Point getShare(int i) throws Exception { if(i < 1 || i > n){ throw new Exception(); } return new Polynomial.Point(BigInteger.valueOf(i), polynomial); } /** * @param shares - subset of the original shares * * @return image of interpolation(shares) at x = 0 */ public static BigInteger getSecrete(Polynomial.Point[] shares){ Polynomial polynomial = Polynomial.interpolation(shares); return polynomial.image(BigInteger.ZERO); } /** * getter * @return threshold */ public int getThreshold() { return t; } /** * getter * @return number of share holders */ public int getN() { return n; } }