package profiling.ECGroup;

import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.crypto.concrete.Util;
import org.spongycastle.math.ec.ECPoint;
import org.factcenter.qilin.primitives.concrete.ECElGamal;
import org.factcenter.qilin.primitives.concrete.ECGroup;

import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Tzlil on 1/20/2016.
 */
public class Add {

    ECElGamalEncryption encryptor;
    ECGroup group;
    Random random;
    private int n;
    List<ECPoint> members1;
    List<ECPoint> members2;

    public void setup() throws InvalidKeySpecException {
        // initialization
        random = new Random();
        group = new ECGroup("secp256k1");
        encryptor = new ECElGamalEncryption();
        encryptor.init(Util.encodePK(group, new ECElGamal.SK(group, ECElGamal.generateSecretKey(group, random))));
        // generate n;
        int sqrtn  = 128;
        n = sqrtn*sqrtn;

        members1 = new ArrayList<ECPoint>(sqrtn);
        members2 = new ArrayList<ECPoint>(sqrtn);

        for (int i = 0 ; i < sqrtn; i ++){
            members1.add(group.sample(random));
            members2.add(group.sample(random));
        }
    }

    public void addProfiling() throws InvalidProtocolBufferException {
        System.out.println("AddSub");
        System.out.println("n is : " + n);
        System.out.println("start n operations");
        long startTime = System.currentTimeMillis();
        for (ECPoint member1: members1) {
            for (ECPoint member2: members2) {
                group.add(member1,member2);
            }
        }

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