package profiling.BigInteger;

import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.protobuf.Voting;
import org.factcenter.qilin.primitives.RandomOracle;
import org.factcenter.qilin.primitives.concrete.DigestOracle;

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

/**
 * Created by Tzlil on 1/25/2016.
 */
public class SHA256 {
    final int LENGTH = 420;
    int tests;
    BigInteger[] randoms;
    RandomOracle randomOracle;

    public void setup() throws Exception {
        Random rand = new Random();
        randomOracle = new DigestOracle();
        tests = 1<<15;
        Voting.PlaintextBallot msg;
        randoms = new BigInteger[tests];
        byte[] arr = new byte[LENGTH];
        for (int i =0 ; i < tests ; i++){
            rand.nextBytes(arr);
            randoms[i] = new BigInteger(arr);
        }
    }

    public void SHA256Profiling() throws InvalidProtocolBufferException {

        System.out.println("SHA256Profiling");
        System.out.println("#" + tests + " tests");
        long startTime = System.currentTimeMillis();
        for (int i = 0 ; i < randoms.length;i++) {
            byte[] arr = randoms[i].toByteArray();
            new BigInteger(this.randomOracle.hash(arr, arr.length));
        }
        long finishTime = System.currentTimeMillis();
        System.out.println("    that took: "+(finishTime-startTime)+ " ms");
        System.out.println("    avg of "+((double)(finishTime-startTime))/tests + " ms");
    }
}
