120 lines
3.5 KiB
Java
120 lines
3.5 KiB
Java
|
package ShamirSecretSharing;
|
||
|
|
||
|
import org.bouncycastle.util.Arrays;
|
||
|
import org.factcenter.qilin.primitives.concrete.ECGroup;
|
||
|
|
||
|
import java.math.BigInteger;
|
||
|
|
||
|
/**
|
||
|
* Created by Tzlil on 1/27/2016.
|
||
|
*/
|
||
|
public class Polynomial {
|
||
|
|
||
|
private static final Polynomial ZERO = new Polynomial(new BigInteger[]{BigInteger.ZERO});
|
||
|
private 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 BigInteger image(BigInteger x){
|
||
|
BigInteger result = BigInteger.ZERO;
|
||
|
BigInteger power = BigInteger.ONE;
|
||
|
for(int i = 0 ; i <= degree ; i++){
|
||
|
result.add(coefficients[i].multiply(power));
|
||
|
power.multiply(x);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static Polynomial interpolation(Point[] points){
|
||
|
Polynomial[] factors = new Polynomial[points.length];
|
||
|
for (int i = 0 ; i < factors.length ; i++){
|
||
|
factors[i] = new Polynomial(new BigInteger[]{BigInteger.ONE,points[i].x}); // X - Xi
|
||
|
}
|
||
|
Polynomial result = ZERO;
|
||
|
BigInteger constant;
|
||
|
Polynomial product;
|
||
|
for (int i = 0 ; i < points.length; i++){
|
||
|
constant = points[i].y;
|
||
|
product = ONE;
|
||
|
for (int j = 0 ; j < points.length; j ++){
|
||
|
if(i != j ) {
|
||
|
constant = constant.divide(points[i].x.subtract(points[j].x));
|
||
|
product = product.mul(factors[j]);
|
||
|
}
|
||
|
}
|
||
|
result.add(product.mul(constant));
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|