package meerkat.crypto.concrete;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;

import meerkat.protobuf.Crypto;
import org.junit.Test;

import java.math.BigInteger;
import static org.junit.Assert.*;

/**
 * Created by talm on 12/11/15.
 */
public class ECDSADeterministicSignatureTest extends ECDSASignatureTest {

    @Override
    protected ECDSASignature getSigner() { return new ECDSADeterministicSignature(); }


    /**
     * Make sure signatures don't vary
     */
    @Test
    public void testDeterministicSigning() throws Exception {
        loadSigningKeys();


        for (int i = 0; i < REPEAT_COUNT; ++i) {
            BigInteger rawMsg = new BigInteger(50, rand);
            Message msg = Crypto.BigInteger.newBuilder()
                    .setData(ByteString.copyFrom(rawMsg.toByteArray())).build();
            Crypto.Signature[] sigs = new Crypto.Signature[REPEAT_COUNT];

            signer.updateContent(msg);
            sigs[0] = signer.sign();
            byte[] canonicalSig = sigs[0].toByteArray();

            for (int j = 1; j < sigs.length; ++j) {
                signer.updateContent(msg);
                sigs[j] = signer.sign();

                byte[] newSig = sigs[j].toByteArray();

                assertArrayEquals("Signatures on same message differ (i="+i+",j="+j+")", canonicalSig, newSig);
            }
        }
    }
}
