package profiling.BigInteger;

import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.crypto.concrete.Util;
import meerkat.protobuf.ConcreteCrypto;
import org.factcenter.qilin.primitives.concrete.ECElGamal;
import org.factcenter.qilin.primitives.concrete.ECGroup;

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

/**
 * Created by Tzlil on 1/21/2016.
 */
public class Modulo {
    int tests;
    BigInteger[] randoms;
    BigInteger orderUpperBound;

    public void setup() throws Exception {
        Random rand = new Random();
        ECGroup group = new ECGroup("secp256k1");
        orderUpperBound = group.orderUpperBound();
        tests = 1<<17;
        randoms = new BigInteger[tests];

        BigInteger sk = ECElGamal.generateSecretKey(group, rand);
        ECElGamal.SK key = new ECElGamal.SK(group, sk);
        ConcreteCrypto.ElGamalPublicKey serializedPk = Util.encodePK(group, key);
        ECElGamalEncryption enc = new ECElGamalEncryption();
        enc.init(serializedPk);
        for (int i =0 ; i < tests ; i++){
            randoms[i] = new BigInteger(enc.generateRandomness(rand).getData().toByteArray());
        }
    }

    public void ModuloProfiling() throws InvalidProtocolBufferException {

        System.out.println("ModuloProfiling");
        System.out.println("#" + tests + " tests");
        long startTime = System.currentTimeMillis();
        for (int i = 0 ; i < randoms.length;i++) {
            randoms[i].mod(orderUpperBound);
        }
        long finishTime = System.currentTimeMillis();
        System.out.println("    that took: "+(finishTime-startTime)+ " ms");
        System.out.println("    avg of "+((double)(finishTime-startTime))/tests + " ms");
    }
}
