END_TEST /* Create random key pairs, sign random messages, and verify the signatures. */ START_TEST (test_random_sign) { EC_POINT pubkey; FF_NUM privkey, hash, r, s; const FF_NUM max_hash = {0}; // Any 256-bit number for (int passes = 0; passes < 10; ++passes) { ec_create_key (&privkey, &pubkey); ff_rand (&hash, &max_hash); ec_sign (&r, &s, &hash, &privkey); if (!ec_verify (&hash, &pubkey, &r, &s)) { printf ("ec_sign failed to create a signature that could be verified with ec_verify."); printf ("Private Key: "); print_ff (&privkey); printf ("\nPublic Key: 04"); print_ff (&(pubkey.x)); print_ff (&(pubkey.y)); printf ("\nHash: "); print_ff (&hash); printf ("\nr: "); print_ff (&r); printf ("\ns: "); print_ff (&s); printf ("\n"); return "ec_sign should create a signature that can be verified with ec_verify for all possible hash values."; } } }
END_TEST /* Test corner cases */ START_TEST (test_sign_corner) { EC_POINT pubkey; FF_NUM privkey = {0x00000001,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000}; const FF_NUM hash = {0}; FF_NUM r, s; const FF_NUM hash2 = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}; ec_mul (&pubkey, &privkey, &ec_G); ec_sign (&r, &s, &hash, &privkey); mu_assert (ec_verify (&hash, &pubkey, &r, &s), "ec_sign should create a signature that can be verified with ec_verify when the private key is 1."); ec_create_key (&privkey, &pubkey); ec_sign (&r, &s, &hash, &privkey); mu_assert (ec_verify (&hash, &pubkey, &r, &s), "ec_sign should create a signature that can be verified with ec_verify when the hash is 0."); ec_sign (&r, &s, &hash2, &privkey); mu_assert (ec_verify (&hash2, &pubkey, &r, &s), "ec_sign should create a signature that can be verified with ec_verify when the hash is 2^256-1."); }
/** * Performance testing of ECC signature generations and verifications */ int main(void) { elem_t rho; uint8_t b; unsigned char hash[PLA_HASH_LENGTH + 1]; hash[PLA_HASH_LENGTH] = 0; unsigned char id_hash[PLA_HASH_LENGTH + 1]; id_hash[PLA_HASH_LENGTH] = 0; unsigned char c = 'x'; unsigned char c2 = 'y'; int i; time_t initial, final; ec_init(); /* Some hashes */ RIPEMD160(&c, 1, hash); RIPEMD160(&c2, 1, id_hash); /* Generate a new key */ mpz_t tmp_sigma; mpz_init(tmp_sigma); mpz_t sigma; mpz_init(sigma); ec_keygen(&(e.y), tmp_sigma); ec_create_key(&rho, &b, &sigma, id_hash, &tmp_sigma); /* Test signing */ ecselfsig_t sig; initial = time(&initial); for (i=0; i<COUNT; i++) ec_sign_self(&sig, hash, sigma); final = time(&final);