package ShamirSecretSharing; import org.bouncycastle.util.Arrays; import org.factcenter.qilin.primitives.concrete.ECGroup; import org.factcenter.qilin.util.Pair; import java.math.BigInteger; /** * Created by Tzlil on 1/27/2016. */ public class Polynomial { protected static final Polynomial ZERO = new Polynomial(new BigInteger[]{BigInteger.ZERO}); protected static final Polynomial ONE = new Polynomial(new BigInteger[]{BigInteger.ONE}); private final int degree; private final BigInteger[] coefficients; public Polynomial(BigInteger[] coefficients) { this.degree = coefficients.length - 1; this.coefficients = coefficients; } public Polynomial(Polynomial polynomial) { this.degree = polynomial.getDegree(); this.coefficients = polynomial.getCoefficients(); } public boolean isEquals(Polynomial other) { if (this.degree != other.degree) return false; return Arrays.areEqual(this.coefficients,other.coefficients); } @Override public String toString() { return "Polynomial{" + "degree=" + degree + ", coefficients=" + java.util.Arrays.toString(coefficients) + '}'; } public BigInteger image(BigInteger x){ BigInteger result = BigInteger.ZERO; BigInteger power = BigInteger.ONE; for(int i = 0 ; i <= degree ; i++){ result = result.add(coefficients[i].multiply(power)); power = power.multiply(x); } return result; } public static Polynomial interpolation(Point[] points){ LagrangePolynomial[] l = LagrangePolynomial.lagrangePolynomials(points); BigInteger product = BigInteger.ONE; for (int i = 0; i < l.length;i++){ product = product.multiply(l[i].divisor); } BigInteger[] factors = new BigInteger[l.length]; for (int i = 0; i < l.length;i++){ factors[i] = product.divide(l[i].divisor); } int degree = l[0].polynomial.degree; BigInteger[] coefficients = new BigInteger[degree + 1]; for (int j = 0; j < coefficients.length;j++){ coefficients[j] = BigInteger.ZERO; for (int i = 0; i < l.length; i++){ coefficients[j] = coefficients[j].add(l[i].image.multiply(factors[i]).multiply(l[i].polynomial.coefficients[j])); } coefficients[j] = coefficients[j].divide(product); } return new Polynomial(coefficients); } public Polynomial add(Polynomial other){ Polynomial bigger,smaller; if(this.degree < other.degree){ bigger = other; smaller = this; }else{ bigger = this; smaller = other; } BigInteger[] coefficients = bigger.getCoefficients(); for (int i = 0; i <= smaller.degree ; i++){ coefficients[i] = smaller.coefficients[i].add(bigger.coefficients[i]); } return new Polynomial(coefficients); } public Polynomial mul(BigInteger constant){ BigInteger[] coefficients = this.getCoefficients(); for (int i = 0; i <= this.degree ; i++){ coefficients[i] = constant.multiply(coefficients[i]); } return new Polynomial(coefficients); } public Polynomial mul(Polynomial other){ BigInteger[] coefficients = new BigInteger[this.degree + other.degree + 1]; java.util.Arrays.fill(coefficients,BigInteger.ZERO); for (int i = 0; i <= this.degree ; i++){ for (int j = 0; j <= other.degree; j++){ coefficients[i+j] = coefficients[i+j].add(this.coefficients[i].multiply(other.coefficients[j])); } } return new Polynomial(coefficients); } public BigInteger[] getCoefficients() { return Arrays.clone(coefficients); } public int getDegree() { return degree; } public static class Point{ public final BigInteger x; public final BigInteger y; public Point(BigInteger x, BigInteger y) { this.x = x; this.y = y; } } }