61 lines
1.8 KiB
Java
61 lines
1.8 KiB
Java
|
package FeldmanVerifiableSecretSharing.ShamirSecretSharing.PolynomialTests;
|
||
|
|
||
|
import FeldmanVerifiableSecretSharing.ShamirSecretSharing.Polynomial;
|
||
|
import org.junit.Before;
|
||
|
import org.junit.Test;
|
||
|
|
||
|
import java.math.BigInteger;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Random;
|
||
|
import java.util.Set;
|
||
|
|
||
|
/**
|
||
|
* Created by Tzlil on 1/27/2016.
|
||
|
*/
|
||
|
public class InterpolationTest {
|
||
|
Polynomial[] polynomials;
|
||
|
int tests = 1 << 10;
|
||
|
int maxDegree = 15;
|
||
|
int bits = 128;
|
||
|
Random random;
|
||
|
Polynomial.Point[][] pointsArrays;
|
||
|
@Before
|
||
|
public void settings(){
|
||
|
random = new Random();
|
||
|
polynomials = new Polynomial[tests];
|
||
|
pointsArrays = new Polynomial.Point[tests][];
|
||
|
for (int i = 0; i < polynomials.length; i++){
|
||
|
polynomials[i] = Utils.generateRandomPolynomial(random.nextInt(maxDegree),bits,random);
|
||
|
pointsArrays[i] = randomPoints(polynomials[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Polynomial.Point[] randomPoints(Polynomial p){
|
||
|
Polynomial.Point[] points = new Polynomial.Point[p.getDegree() + 1];
|
||
|
BigInteger x;
|
||
|
Set<BigInteger> set = new HashSet();
|
||
|
for (int i = 0; i < points.length; i++){
|
||
|
x = new BigInteger(bits,random);
|
||
|
if(set.contains(x)){
|
||
|
i--;
|
||
|
continue;
|
||
|
}
|
||
|
set.add(x);
|
||
|
points[i] = new Polynomial.Point(x,p);
|
||
|
}
|
||
|
return points;
|
||
|
}
|
||
|
|
||
|
public void oneTest(Polynomial p, Polynomial.Point[] points) throws Exception {
|
||
|
Polynomial interpolation = Polynomial.interpolation(points);
|
||
|
assert (p.compareTo(interpolation) == 0);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void interpolationTest() throws Exception {
|
||
|
for (int i = 0; i < polynomials.length; i ++){
|
||
|
oneTest(polynomials[i],pointsArrays[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|