package meerkat.crypto.utils;

import meerkat.crypto.secretsharing.shamir.Polynomial;
import meerkat.crypto.utils.concrete.Fp;

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

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

    public static Polynomial generateRandomPolynomial(int degree, int bits, Random random, Arithmetic<BigInteger> arithmetic) {
        BigInteger[] coefficients = new BigInteger[degree + 1];

        for (int i = 0 ; i <= degree; i++ ){
            coefficients[i] = new BigInteger(bits,random); // sample from Zp [0,... q-1]
        }
        return new Polynomial(coefficients,arithmetic);
    }

    public static Polynomial generateRandomPolynomial(int degree,int bits,Random random,BigInteger p) {
        BigInteger[] coefficients = generateRandomPolynomial(degree,bits,random,new Fp(p)).getCoefficients();
        for (int i = 0; i<coefficients.length;i++){
            coefficients[i] = coefficients[i].mod(p);
        }
        return new Polynomial(coefficients,new Fp(p));
    }
}
