2016-01-27 06:41:24 -05:00
|
|
|
package ShamirSecretSharing;
|
|
|
|
|
|
|
|
import org.bouncycastle.util.Arrays;
|
|
|
|
import org.factcenter.qilin.primitives.concrete.ECGroup;
|
2016-01-27 18:47:07 -05:00
|
|
|
import org.factcenter.qilin.util.Pair;
|
2016-01-27 06:41:24 -05:00
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Tzlil on 1/27/2016.
|
|
|
|
*/
|
|
|
|
public class Polynomial {
|
|
|
|
|
2016-01-27 18:47:07 -05:00
|
|
|
protected static final Polynomial ZERO = new Polynomial(new BigInteger[]{BigInteger.ZERO});
|
|
|
|
protected static final Polynomial ONE = new Polynomial(new BigInteger[]{BigInteger.ONE});
|
2016-01-27 06:41:24 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:47:07 -05:00
|
|
|
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) +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2016-01-27 06:41:24 -05:00
|
|
|
public BigInteger image(BigInteger x){
|
|
|
|
BigInteger result = BigInteger.ZERO;
|
|
|
|
BigInteger power = BigInteger.ONE;
|
|
|
|
for(int i = 0 ; i <= degree ; i++){
|
2016-01-27 18:47:07 -05:00
|
|
|
result = result.add(coefficients[i].multiply(power));
|
|
|
|
power = power.multiply(x);
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Polynomial interpolation(Point[] points){
|
2016-01-27 18:47:07 -05:00
|
|
|
LagrangePolynomial[] l = LagrangePolynomial.lagrangePolynomials(points);
|
|
|
|
|
|
|
|
BigInteger product = BigInteger.ONE;
|
|
|
|
for (int i = 0; i < l.length;i++){
|
|
|
|
product = product.multiply(l[i].divisor);
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
2016-01-27 18:47:07 -05:00
|
|
|
|
|
|
|
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]));
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
2016-01-27 18:47:07 -05:00
|
|
|
coefficients[j] = coefficients[j].divide(product);
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
2016-01-27 18:47:07 -05:00
|
|
|
return new Polynomial(coefficients);
|
2016-01-27 06:41:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|