package profiling.Convert;

import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.crypto.concrete.Util;
import meerkat.protobuf.ConcreteCrypto;
import meerkat.protobuf.Crypto;
import meerkat.protobuf.Voting;
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 RerandomizableEncryptedMessage2ElGamalCiphertext {
    Random rand;
    ECElGamal.SK key;
    ECGroup group;
    ECElGamalEncryption enc;
    ConcreteCrypto.ElGamalPublicKey serializedPk;
    int tests;
    Crypto.RerandomizableEncryptedMessage[] encryptedMessage;

    public void setup() throws Exception {
        rand = new Random();
        group = new ECGroup("secp256k1");
        BigInteger sk = ECElGamal.generateSecretKey(group, rand);
        key = new ECElGamal.SK(group, sk);
        serializedPk = Util.encodePK(group, key);
        enc = new ECElGamalEncryption();
        enc.init(serializedPk);
        tests = 1024 * 18;
        encryptedMessage = new Crypto.RerandomizableEncryptedMessage[tests];

        Voting.PlaintextBallot msg;

        for (int i = 0; i < tests; i ++){
            msg = Utils.genRandomBallot(2,3,16);

            encryptedMessage[i] = enc.encrypt(msg, enc.generateRandomness(rand));
        }
    }

    public void RerandomizableEncryptedMessage2ElGamalCiphertext() throws InvalidProtocolBufferException {

        System.out.println("RerandomizableEncryptedMessage2ElGamalCiphertext");
        System.out.println("#"+ tests + " tests");
        System.out.println("start tests operations");
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < tests; i ++){
            ECElGamalEncryption.RerandomizableEncryptedMessage2ElGamalCiphertext(encryptedMessage[i]);
        }

        long finishTime = System.currentTimeMillis();
        System.out.println("    that took: "+(finishTime-startTime)+ " ms");
        System.out.println("    avg of "+((double)(finishTime-startTime))/ tests + " ms");
    }
}
