package meerkat.crypto.secretsharing.shamir.PolynomialTests;
import meerkat.crypto.utils.GenerateRandomPolynomial;
import meerkat.crypto.utils.Z;
import meerkat.crypto.secretsharing.shamir.Polynomial;
import org.junit.Before;
import org.junit.Test;

import java.math.BigInteger;
import java.util.Random;

/**
 * Created by Tzlil on 1/27/2016.
 */
public class AddTest {

    Polynomial[] arr1;
    Polynomial[] arr2;
    int tests = 1 << 12;
    int maxDegree = 15;
    int bits = 128;
    Random random;

    @Before
    public void settings(){
        random = new Random();
        arr1 = new Polynomial[tests];
        arr2 = new Polynomial[tests];
        for (int i = 0; i < arr1.length; i++){
            arr1[i] = GenerateRandomPolynomial.generateRandomPolynomial(random.nextInt(maxDegree),bits,random,new Z());
            arr2[i] = GenerateRandomPolynomial.generateRandomPolynomial(random.nextInt(maxDegree),bits,random,new Z());
        }
    }

    public void oneTest(Polynomial p1, Polynomial p2){
        Polynomial sum = p1.add(p2);
        BigInteger x = new BigInteger(bits,random);
        assert(sum.evaluate(x).equals(p1.evaluate(x).add(p2.evaluate(x))));
    }

    @Test
    public void addTest(){
        for (int i = 0 ; i < arr1.length; i ++){
            oneTest(arr1[i],arr2[i]);
        }
    }
}
