package profiling.BigInteger;

import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.crypto.concrete.Util;
import meerkat.protobuf.ConcreteCrypto;
import meerkat.mixer.Utils;
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/25/2016.
 */
public class Mul {

    int tests;
    BigInteger[] randoms;
    BigInteger[] randoms2;

    public void setup() throws Exception {
        Random rand = new Random();
        tests = 1 << 17;
        randoms = new BigInteger[tests];
        rand = new Random();

        ECGroup group = new ECGroup("secp256k1");
        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());
        }
        randoms2 = new BigInteger[tests];
        for (int i =0 ; i < tests ; i++){
            randoms2[i] = new BigInteger(enc.generateRandomness(rand).getData().toByteArray());
        }

    }

    public void MulProfiling() throws InvalidProtocolBufferException {

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