2013-05-09 09:24:32 -04:00
|
|
|
// Copyright (c) 2013 Pieter Wuille
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-01-18 00:52:33 -03:00
|
|
|
#if defined HAVE_CONFIG_H
|
|
|
|
#include "libsecp256k1-config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-01-18 00:52:33 -03:00
|
|
|
#include <stdio.h>
|
2014-06-15 19:30:17 -04:00
|
|
|
#include <stdlib.h>
|
2013-03-10 17:25:19 -03:00
|
|
|
|
2014-03-06 21:11:01 -03:00
|
|
|
#include "util_impl.h"
|
2014-08-03 13:54:41 -04:00
|
|
|
#include "secp256k1.c"
|
2013-03-10 17:25:19 -03:00
|
|
|
|
2013-05-05 10:55:05 -04:00
|
|
|
#ifdef ENABLE_OPENSSL_TESTS
|
|
|
|
#include "openssl/bn.h"
|
|
|
|
#include "openssl/ec.h"
|
|
|
|
#include "openssl/ecdsa.h"
|
|
|
|
#include "openssl/obj_mac.h"
|
|
|
|
#endif
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
static int count = 100;
|
2013-03-24 06:38:35 -03:00
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
/***** NUM TESTS *****/
|
|
|
|
|
2013-04-21 20:52:56 -03:00
|
|
|
void random_num_negate(secp256k1_num_t *num) {
|
|
|
|
if (secp256k1_rand32() & 1)
|
|
|
|
secp256k1_num_negate(num);
|
|
|
|
}
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void random_num_order_test(secp256k1_num_t *num) {
|
2013-04-20 18:34:41 -03:00
|
|
|
do {
|
|
|
|
unsigned char b32[32];
|
|
|
|
secp256k1_rand256_test(b32);
|
|
|
|
secp256k1_num_set_bin(num, b32, 32);
|
|
|
|
if (secp256k1_num_is_zero(num))
|
|
|
|
continue;
|
|
|
|
if (secp256k1_num_cmp(num, &secp256k1_ge_consts->order) >= 0)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void random_num_order(secp256k1_num_t *num) {
|
|
|
|
do {
|
|
|
|
unsigned char b32[32];
|
|
|
|
secp256k1_rand256(b32);
|
|
|
|
secp256k1_num_set_bin(num, b32, 32);
|
|
|
|
if (secp256k1_num_is_zero(num))
|
|
|
|
continue;
|
|
|
|
if (secp256k1_num_cmp(num, &secp256k1_ge_consts->order) >= 0)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_num_copy_inc_cmp() {
|
|
|
|
secp256k1_num_t n1,n2;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
secp256k1_num_init(&n2);
|
|
|
|
random_num_order(&n1);
|
|
|
|
secp256k1_num_copy(&n2, &n1);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
|
|
|
CHECK(secp256k1_num_eq(&n2, &n1));
|
2013-04-21 14:07:21 -03:00
|
|
|
secp256k1_num_inc(&n2);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(!secp256k1_num_eq(&n1, &n2));
|
|
|
|
CHECK(!secp256k1_num_eq(&n2, &n1));
|
2013-04-21 14:07:21 -03:00
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
secp256k1_num_free(&n2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_num_get_set_hex() {
|
|
|
|
secp256k1_num_t n1,n2;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
secp256k1_num_init(&n2);
|
|
|
|
random_num_order_test(&n1);
|
|
|
|
char c[64];
|
|
|
|
secp256k1_num_get_hex(c, 64, &n1);
|
|
|
|
secp256k1_num_set_hex(&n2, c, 64);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
2013-04-21 14:07:21 -03:00
|
|
|
for (int i=0; i<64; i++) {
|
|
|
|
// check whether the lower 4 bits correspond to the last hex character
|
|
|
|
int low1 = secp256k1_num_shift(&n1, 4);
|
|
|
|
int lowh = c[63];
|
|
|
|
int low2 = (lowh>>6)*9+(lowh-'0')&15;
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(low1 == low2);
|
2013-04-21 14:07:21 -03:00
|
|
|
// shift bits off the hex representation, and compare
|
|
|
|
memmove(c+1, c, 63);
|
|
|
|
c[0] = '0';
|
|
|
|
secp256k1_num_set_hex(&n2, c, 64);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
2013-04-21 14:07:21 -03:00
|
|
|
}
|
|
|
|
secp256k1_num_free(&n2);
|
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_num_get_set_bin() {
|
|
|
|
secp256k1_num_t n1,n2;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
secp256k1_num_init(&n2);
|
|
|
|
random_num_order_test(&n1);
|
|
|
|
unsigned char c[32];
|
|
|
|
secp256k1_num_get_bin(c, 32, &n1);
|
|
|
|
secp256k1_num_set_bin(&n2, c, 32);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
2013-04-21 14:07:21 -03:00
|
|
|
for (int i=0; i<32; i++) {
|
|
|
|
// check whether the lower 8 bits correspond to the last byte
|
|
|
|
int low1 = secp256k1_num_shift(&n1, 8);
|
|
|
|
int low2 = c[31];
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(low1 == low2);
|
2013-04-21 14:07:21 -03:00
|
|
|
// shift bits off the byte representation, and compare
|
|
|
|
memmove(c+1, c, 31);
|
|
|
|
c[0] = 0;
|
|
|
|
secp256k1_num_set_bin(&n2, c, 32);
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
2013-04-21 14:07:21 -03:00
|
|
|
}
|
|
|
|
secp256k1_num_free(&n2);
|
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_num_int() {
|
|
|
|
secp256k1_num_t n1;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
for (int i=-255; i<256; i++) {
|
|
|
|
unsigned char c1[3] = {};
|
|
|
|
c1[2] = abs(i);
|
|
|
|
unsigned char c2[3] = {0x11,0x22,0x33};
|
|
|
|
secp256k1_num_set_int(&n1, i);
|
|
|
|
secp256k1_num_get_bin(c2, 3, &n1);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(memcmp(c1, c2, 3) == 0);
|
2013-04-21 14:07:21 -03:00
|
|
|
}
|
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
}
|
|
|
|
|
2013-04-21 20:52:56 -03:00
|
|
|
void test_num_negate() {
|
|
|
|
secp256k1_num_t n1;
|
|
|
|
secp256k1_num_t n2;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
secp256k1_num_init(&n2);
|
|
|
|
random_num_order_test(&n1); // n1 = R
|
|
|
|
random_num_negate(&n1);
|
|
|
|
secp256k1_num_copy(&n2, &n1); // n2 = R
|
|
|
|
secp256k1_num_sub(&n1, &n2, &n1); // n1 = n2-n1 = 0
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_num_is_zero(&n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_copy(&n1, &n2); // n1 = R
|
|
|
|
secp256k1_num_negate(&n1); // n1 = -R
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(!secp256k1_num_is_zero(&n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_add(&n1, &n2, &n1); // n1 = n2+n1 = 0
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_num_is_zero(&n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_copy(&n1, &n2); // n1 = R
|
|
|
|
secp256k1_num_negate(&n1); // n1 = -R
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_num_is_neg(&n1) != secp256k1_num_is_neg(&n2));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_negate(&n1); // n1 = R
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1, &n2));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_free(&n2);
|
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_num_add_sub() {
|
2014-08-09 13:22:42 -04:00
|
|
|
int r = secp256k1_rand32();
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_t n1;
|
|
|
|
secp256k1_num_t n2;
|
|
|
|
secp256k1_num_init(&n1);
|
|
|
|
secp256k1_num_init(&n2);
|
|
|
|
random_num_order_test(&n1); // n1 = R1
|
2014-08-09 13:22:42 -04:00
|
|
|
if (r & 1) {
|
|
|
|
random_num_negate(&n1);
|
|
|
|
}
|
2013-04-21 20:52:56 -03:00
|
|
|
random_num_order_test(&n2); // n2 = R2
|
2014-08-09 13:22:42 -04:00
|
|
|
if (r & 2) {
|
|
|
|
random_num_negate(&n2);
|
|
|
|
}
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_t n1p2, n2p1, n1m2, n2m1;
|
|
|
|
secp256k1_num_init(&n1p2);
|
|
|
|
secp256k1_num_init(&n2p1);
|
|
|
|
secp256k1_num_init(&n1m2);
|
|
|
|
secp256k1_num_init(&n2m1);
|
|
|
|
secp256k1_num_add(&n1p2, &n1, &n2); // n1p2 = R1 + R2
|
|
|
|
secp256k1_num_add(&n2p1, &n2, &n1); // n2p1 = R2 + R1
|
|
|
|
secp256k1_num_sub(&n1m2, &n1, &n2); // n1m2 = R1 - R2
|
|
|
|
secp256k1_num_sub(&n2m1, &n2, &n1); // n2m1 = R2 - R1
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n1p2, &n2p1));
|
|
|
|
CHECK(!secp256k1_num_eq(&n1p2, &n1m2));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_negate(&n2m1); // n2m1 = -R2 + R1
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n2m1, &n1m2));
|
|
|
|
CHECK(!secp256k1_num_eq(&n2m1, &n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_add(&n2m1, &n2m1, &n2); // n2m1 = -R2 + R1 + R2 = R1
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n2m1, &n1));
|
|
|
|
CHECK(!secp256k1_num_eq(&n2p1, &n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_sub(&n2p1, &n2p1, &n2); // n2p1 = R2 + R1 - R2 = R1
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&n2p1, &n1));
|
2013-04-21 20:52:56 -03:00
|
|
|
secp256k1_num_free(&n2m1);
|
|
|
|
secp256k1_num_free(&n1m2);
|
|
|
|
secp256k1_num_free(&n2p1);
|
|
|
|
secp256k1_num_free(&n1p2);
|
|
|
|
secp256k1_num_free(&n2);
|
|
|
|
secp256k1_num_free(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_num_smalltests() {
|
|
|
|
for (int i=0; i<100*count; i++) {
|
|
|
|
test_num_copy_inc_cmp();
|
|
|
|
test_num_get_set_hex();
|
|
|
|
test_num_get_set_bin();
|
|
|
|
test_num_negate();
|
|
|
|
test_num_add_sub();
|
|
|
|
}
|
|
|
|
run_num_int();
|
|
|
|
}
|
|
|
|
|
2014-05-20 23:22:14 -04:00
|
|
|
/***** FIELD TESTS *****/
|
|
|
|
|
|
|
|
void random_fe(secp256k1_fe_t *x) {
|
|
|
|
unsigned char bin[32];
|
|
|
|
secp256k1_rand256(bin);
|
|
|
|
secp256k1_fe_set_b32(x, bin);
|
|
|
|
}
|
|
|
|
|
2014-06-03 06:50:27 -04:00
|
|
|
void random_fe_non_zero(secp256k1_fe_t *nz) {
|
|
|
|
int tries = 10;
|
2014-05-20 23:22:14 -04:00
|
|
|
while (--tries >= 0) {
|
2014-06-03 06:50:27 -04:00
|
|
|
random_fe(nz);
|
|
|
|
secp256k1_fe_normalize(nz);
|
|
|
|
if (!secp256k1_fe_is_zero(nz))
|
2014-05-20 23:22:14 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-06-03 06:50:27 -04:00
|
|
|
// Infinitesimal probability of spurious failure here
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(tries >= 0);
|
2014-05-20 23:22:14 -04:00
|
|
|
}
|
|
|
|
|
2014-06-03 06:50:27 -04:00
|
|
|
void random_fe_non_square(secp256k1_fe_t *ns) {
|
|
|
|
random_fe_non_zero(ns);
|
|
|
|
secp256k1_fe_t r;
|
|
|
|
if (secp256k1_fe_sqrt(&r, ns)) {
|
|
|
|
secp256k1_fe_negate(ns, ns, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 05:01:26 -04:00
|
|
|
int check_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
|
|
|
|
secp256k1_fe_t an = *a; secp256k1_fe_normalize(&an);
|
|
|
|
secp256k1_fe_t bn = *b; secp256k1_fe_normalize(&bn);
|
|
|
|
return secp256k1_fe_equal(&an, &bn);
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_fe_inverse(const secp256k1_fe_t *a, const secp256k1_fe_t *ai) {
|
|
|
|
secp256k1_fe_t x; secp256k1_fe_mul(&x, a, ai);
|
|
|
|
secp256k1_fe_t one; secp256k1_fe_set_int(&one, 1);
|
|
|
|
return check_fe_equal(&x, &one);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_field_inv() {
|
|
|
|
secp256k1_fe_t x, xi, xii;
|
|
|
|
for (int i=0; i<10*count; i++) {
|
|
|
|
random_fe_non_zero(&x);
|
|
|
|
secp256k1_fe_inv(&xi, &x);
|
|
|
|
CHECK(check_fe_inverse(&x, &xi));
|
|
|
|
secp256k1_fe_inv(&xii, &xi);
|
|
|
|
CHECK(check_fe_equal(&x, &xii));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_field_inv_var() {
|
|
|
|
secp256k1_fe_t x, xi, xii;
|
|
|
|
for (int i=0; i<10*count; i++) {
|
|
|
|
random_fe_non_zero(&x);
|
|
|
|
secp256k1_fe_inv_var(&xi, &x);
|
|
|
|
CHECK(check_fe_inverse(&x, &xi));
|
|
|
|
secp256k1_fe_inv_var(&xii, &xi);
|
|
|
|
CHECK(check_fe_equal(&x, &xii));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_field_inv_all() {
|
|
|
|
secp256k1_fe_t x[16], xi[16], xii[16];
|
|
|
|
// Check it's safe to call for 0 elements
|
|
|
|
secp256k1_fe_inv_all(0, xi, x);
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
size_t len = (secp256k1_rand32() & 15) + 1;
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
random_fe_non_zero(&x[j]);
|
|
|
|
secp256k1_fe_inv_all(len, xi, x);
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
CHECK(check_fe_inverse(&x[j], &xi[j]));
|
|
|
|
secp256k1_fe_inv_all(len, xii, xi);
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
CHECK(check_fe_equal(&x[j], &xii[j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_field_inv_all_var() {
|
|
|
|
secp256k1_fe_t x[16], xi[16], xii[16];
|
|
|
|
// Check it's safe to call for 0 elements
|
|
|
|
secp256k1_fe_inv_all_var(0, xi, x);
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
size_t len = (secp256k1_rand32() & 15) + 1;
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
random_fe_non_zero(&x[j]);
|
|
|
|
secp256k1_fe_inv_all_var(len, xi, x);
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
CHECK(check_fe_inverse(&x[j], &xi[j]));
|
|
|
|
secp256k1_fe_inv_all_var(len, xii, xi);
|
|
|
|
for (int j=0; j<len; j++)
|
|
|
|
CHECK(check_fe_equal(&x[j], &xii[j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 04:03:29 -04:00
|
|
|
void run_sqr() {
|
|
|
|
secp256k1_fe_t x, s;
|
|
|
|
|
|
|
|
{
|
|
|
|
secp256k1_fe_set_int(&x, 1);
|
|
|
|
secp256k1_fe_negate(&x, &x, 1);
|
|
|
|
|
|
|
|
for (int i=1; i<=512; ++i) {
|
|
|
|
secp256k1_fe_mul_int(&x, 2);
|
|
|
|
secp256k1_fe_normalize(&x);
|
|
|
|
secp256k1_fe_sqr(&s, &x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 23:22:14 -04:00
|
|
|
void test_sqrt(const secp256k1_fe_t *a, const secp256k1_fe_t *k) {
|
|
|
|
secp256k1_fe_t r1, r2;
|
|
|
|
int v = secp256k1_fe_sqrt(&r1, a);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK((v == 0) == (k == NULL));
|
2014-05-20 23:22:14 -04:00
|
|
|
|
|
|
|
if (k != NULL) {
|
|
|
|
// Check that the returned root is +/- the given known answer
|
|
|
|
secp256k1_fe_negate(&r2, &r1, 1);
|
|
|
|
secp256k1_fe_add(&r1, k); secp256k1_fe_add(&r2, k);
|
|
|
|
secp256k1_fe_normalize(&r1); secp256k1_fe_normalize(&r2);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_fe_is_zero(&r1) || secp256k1_fe_is_zero(&r2));
|
2014-05-20 23:22:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_sqrt() {
|
|
|
|
secp256k1_fe_t ns, x, s, t;
|
2014-06-03 06:50:27 -04:00
|
|
|
|
|
|
|
// Check sqrt(0) is 0
|
|
|
|
secp256k1_fe_set_int(&x, 0);
|
|
|
|
secp256k1_fe_sqr(&s, &x);
|
|
|
|
test_sqrt(&s, &x);
|
|
|
|
|
|
|
|
// Check sqrt of small squares (and their negatives)
|
|
|
|
for (int i=1; i<=100; i++) {
|
|
|
|
secp256k1_fe_set_int(&x, i);
|
2014-05-20 23:22:14 -04:00
|
|
|
secp256k1_fe_sqr(&s, &x);
|
|
|
|
test_sqrt(&s, &x);
|
2014-06-03 06:50:27 -04:00
|
|
|
secp256k1_fe_negate(&t, &s, 1);
|
2014-05-20 23:22:14 -04:00
|
|
|
test_sqrt(&t, NULL);
|
|
|
|
}
|
2014-06-03 06:50:27 -04:00
|
|
|
|
|
|
|
// Consistency checks for large random values
|
|
|
|
for (int i=0; i<10; i++) {
|
|
|
|
random_fe_non_square(&ns);
|
|
|
|
for (int j=0; j<count; j++) {
|
|
|
|
random_fe(&x);
|
|
|
|
secp256k1_fe_sqr(&s, &x);
|
|
|
|
test_sqrt(&s, &x);
|
|
|
|
secp256k1_fe_negate(&t, &s, 1);
|
|
|
|
test_sqrt(&t, NULL);
|
|
|
|
secp256k1_fe_mul(&t, &s, &ns);
|
|
|
|
test_sqrt(&t, NULL);
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 23:22:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***** ECMULT TESTS *****/
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void run_ecmult_chain() {
|
2013-03-10 18:23:33 -03:00
|
|
|
// random starting point A (on the curve)
|
2013-03-30 18:32:16 -03:00
|
|
|
secp256k1_fe_t ax; secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004", 64);
|
|
|
|
secp256k1_fe_t ay; secp256k1_fe_set_hex(&ay, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f", 64);
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_gej_t a; secp256k1_gej_set_xy(&a, &ax, &ay);
|
2013-03-10 18:23:33 -03:00
|
|
|
// two random initial factors xn and gn
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_t xn;
|
|
|
|
secp256k1_num_init(&xn);
|
|
|
|
secp256k1_num_set_hex(&xn, "84cc5452f7fde1edb4d38a8ce9b1b84ccef31f146e569be9705d357a42985407", 64);
|
|
|
|
secp256k1_num_t gn;
|
|
|
|
secp256k1_num_init(&gn);
|
|
|
|
secp256k1_num_set_hex(&gn, "a1e58d22553dcd42b23980625d4c57a96e9323d42b3152e5ca2c3990edc7c9de", 64);
|
2013-03-10 18:23:33 -03:00
|
|
|
// two small multipliers to be applied to xn and gn in every iteration:
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_t xf;
|
|
|
|
secp256k1_num_init(&xf);
|
|
|
|
secp256k1_num_set_hex(&xf, "1337", 4);
|
|
|
|
secp256k1_num_t gf;
|
|
|
|
secp256k1_num_init(&gf);
|
|
|
|
secp256k1_num_set_hex(&gf, "7113", 4);
|
2013-03-10 18:23:33 -03:00
|
|
|
// accumulators with the resulting coefficients to A and G
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_t ae;
|
|
|
|
secp256k1_num_init(&ae);
|
|
|
|
secp256k1_num_set_int(&ae, 1);
|
|
|
|
secp256k1_num_t ge;
|
|
|
|
secp256k1_num_init(&ge);
|
|
|
|
secp256k1_num_set_int(&ge, 0);
|
2013-03-10 18:23:33 -03:00
|
|
|
// the point being computed
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_gej_t x = a;
|
|
|
|
const secp256k1_num_t *order = &secp256k1_ge_consts->order;
|
2013-04-21 14:07:21 -03:00
|
|
|
for (int i=0; i<200*count; i++) {
|
2013-03-10 17:41:54 -03:00
|
|
|
// in each iteration, compute X = xn*X + gn*G;
|
2013-04-01 01:29:30 -03:00
|
|
|
secp256k1_ecmult(&x, &x, &xn, &gn);
|
2013-03-10 17:41:54 -03:00
|
|
|
// also compute ae and ge: the actual accumulated factors for A and G
|
|
|
|
// if X was (ae*A+ge*G), xn*X + gn*G results in (xn*ae*A + (xn*ge+gn)*G)
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_num_mod_mul(&ae, &ae, &xn, order);
|
|
|
|
secp256k1_num_mod_mul(&ge, &ge, &xn, order);
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_add(&ge, &ge, &gn);
|
2013-04-14 17:17:21 -03:00
|
|
|
secp256k1_num_mod(&ge, order);
|
2013-03-10 17:41:54 -03:00
|
|
|
// modify xn and gn
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_num_mod_mul(&xn, &xn, &xf, order);
|
|
|
|
secp256k1_num_mod_mul(&gn, &gn, &gf, order);
|
2013-04-21 14:07:21 -03:00
|
|
|
|
|
|
|
// verify
|
|
|
|
if (i == 19999) {
|
|
|
|
char res[132]; int resl = 132;
|
|
|
|
secp256k1_gej_get_hex(res, &resl, &x);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(strcmp(res, "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)") == 0);
|
2013-04-21 14:07:21 -03:00
|
|
|
}
|
2013-03-24 06:38:35 -03:00
|
|
|
}
|
2013-03-10 17:41:54 -03:00
|
|
|
// redo the computation, but directly with the resulting ae and ge coefficients:
|
2013-04-01 01:29:30 -03:00
|
|
|
secp256k1_gej_t x2; secp256k1_ecmult(&x2, &a, &ae, &ge);
|
2013-04-21 14:07:21 -03:00
|
|
|
char res[132]; int resl = 132;
|
2013-03-31 12:02:52 -03:00
|
|
|
char res2[132]; int resl2 = 132;
|
2013-04-21 14:07:21 -03:00
|
|
|
secp256k1_gej_get_hex(res, &resl, &x);
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_gej_get_hex(res2, &resl2, &x2);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(strcmp(res, res2) == 0);
|
|
|
|
CHECK(strlen(res) == 131);
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_free(&xn);
|
|
|
|
secp256k1_num_free(&gn);
|
|
|
|
secp256k1_num_free(&xf);
|
|
|
|
secp256k1_num_free(&gf);
|
|
|
|
secp256k1_num_free(&ae);
|
|
|
|
secp256k1_num_free(&ge);
|
2013-03-10 17:25:19 -03:00
|
|
|
}
|
|
|
|
|
2013-04-01 02:52:58 -03:00
|
|
|
void test_point_times_order(const secp256k1_gej_t *point) {
|
2014-05-20 23:22:14 -04:00
|
|
|
// multiplying a point by the order results in O
|
2013-03-31 12:02:52 -03:00
|
|
|
const secp256k1_num_t *order = &secp256k1_ge_consts->order;
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_t zero;
|
|
|
|
secp256k1_num_init(&zero);
|
|
|
|
secp256k1_num_set_int(&zero, 0);
|
2013-03-31 12:02:52 -03:00
|
|
|
secp256k1_gej_t res;
|
2013-04-01 02:52:58 -03:00
|
|
|
secp256k1_ecmult(&res, point, order, order); // calc res = order * point + order * G;
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_gej_is_infinity(&res));
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_free(&zero);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void run_point_times_order() {
|
2013-03-30 18:32:16 -03:00
|
|
|
secp256k1_fe_t x; secp256k1_fe_set_hex(&x, "02", 2);
|
2013-03-10 21:19:24 -03:00
|
|
|
for (int i=0; i<500; i++) {
|
2014-05-20 23:22:14 -04:00
|
|
|
secp256k1_ge_t p;
|
|
|
|
if (secp256k1_ge_set_xo(&p, &x, 1)) {
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_ge_is_valid(&p));
|
2014-05-20 23:22:14 -04:00
|
|
|
secp256k1_gej_t j;
|
|
|
|
secp256k1_gej_set_ge(&j, &p);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_gej_is_valid(&j));
|
2014-05-20 23:22:14 -04:00
|
|
|
test_point_times_order(&j);
|
|
|
|
}
|
2013-03-30 18:32:16 -03:00
|
|
|
secp256k1_fe_sqr(&x, &x);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
2013-03-30 18:32:16 -03:00
|
|
|
char c[65]; int cl=65;
|
|
|
|
secp256k1_fe_get_hex(c, &cl, &x);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(strcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45") == 0);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
|
|
|
|
2013-04-01 02:52:58 -03:00
|
|
|
void test_wnaf(const secp256k1_num_t *number, int w) {
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_t x, two, t;
|
|
|
|
secp256k1_num_init(&x);
|
|
|
|
secp256k1_num_init(&two);
|
|
|
|
secp256k1_num_init(&t);
|
|
|
|
secp256k1_num_set_int(&x, 0);
|
|
|
|
secp256k1_num_set_int(&two, 2);
|
2013-04-13 12:15:17 -03:00
|
|
|
int wnaf[257];
|
2013-04-01 02:52:58 -03:00
|
|
|
int bits = secp256k1_ecmult_wnaf(wnaf, number, w);
|
2013-03-10 21:19:24 -03:00
|
|
|
int zeroes = -1;
|
2013-04-01 01:29:30 -03:00
|
|
|
for (int i=bits-1; i>=0; i--) {
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_mul(&x, &x, &two);
|
2013-04-01 01:29:30 -03:00
|
|
|
int v = wnaf[i];
|
2013-03-10 21:19:24 -03:00
|
|
|
if (v) {
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(zeroes == -1 || zeroes >= w-1); // check that distance between non-zero elements is at least w-1
|
2013-03-10 21:19:24 -03:00
|
|
|
zeroes=0;
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK((v & 1) == 1); // check non-zero elements are odd
|
|
|
|
CHECK(v <= (1 << (w-1)) - 1); // check range below
|
|
|
|
CHECK(v >= -(1 << (w-1)) - 1); // check range above
|
2013-03-10 21:19:24 -03:00
|
|
|
} else {
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(zeroes != -1); // check that no unnecessary zero padding exists
|
2013-03-10 21:19:24 -03:00
|
|
|
zeroes++;
|
|
|
|
}
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_set_int(&t, v);
|
|
|
|
secp256k1_num_add(&x, &x, &t);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
2014-08-09 13:22:42 -04:00
|
|
|
CHECK(secp256k1_num_eq(&x, number)); // check that wnaf represents number
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_free(&x);
|
|
|
|
secp256k1_num_free(&two);
|
|
|
|
secp256k1_num_free(&t);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void run_wnaf() {
|
2013-04-20 18:34:41 -03:00
|
|
|
secp256k1_num_t n;
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_init(&n);
|
2013-04-21 14:07:21 -03:00
|
|
|
for (int i=0; i<count; i++) {
|
2013-04-20 18:34:41 -03:00
|
|
|
random_num_order(&n);
|
|
|
|
if (i % 1)
|
|
|
|
secp256k1_num_negate(&n);
|
2013-04-01 02:52:58 -03:00
|
|
|
test_wnaf(&n, 4+(i%10));
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_free(&n);
|
2013-03-10 21:19:24 -03:00
|
|
|
}
|
2013-03-10 17:25:19 -03:00
|
|
|
|
2013-05-05 10:55:05 -04:00
|
|
|
void random_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *key, const secp256k1_num_t *msg, int *recid) {
|
|
|
|
secp256k1_num_t nonce;
|
|
|
|
secp256k1_num_init(&nonce);
|
|
|
|
do {
|
|
|
|
random_num_order_test(&nonce);
|
|
|
|
} while(!secp256k1_ecdsa_sig_sign(sig, key, msg, &nonce, recid));
|
|
|
|
secp256k1_num_free(&nonce);
|
|
|
|
}
|
|
|
|
|
2013-03-17 22:41:01 -03:00
|
|
|
void test_ecdsa_sign_verify() {
|
2013-04-01 02:52:58 -03:00
|
|
|
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
|
2013-05-05 10:55:05 -04:00
|
|
|
secp256k1_num_t msg, key;
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_init(&msg);
|
2013-04-21 14:07:21 -03:00
|
|
|
random_num_order_test(&msg);
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_init(&key);
|
2013-04-21 14:07:21 -03:00
|
|
|
random_num_order_test(&key);
|
2013-05-04 19:18:23 -04:00
|
|
|
secp256k1_gej_t pubj; secp256k1_ecmult_gen(&pubj, &key);
|
|
|
|
secp256k1_ge_t pub; secp256k1_ge_set_gej(&pub, &pubj);
|
2013-04-01 02:21:05 -03:00
|
|
|
secp256k1_ecdsa_sig_t sig;
|
|
|
|
secp256k1_ecdsa_sig_init(&sig);
|
2013-05-05 10:55:05 -04:00
|
|
|
random_sign(&sig, &key, &msg, NULL);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_inc(&msg);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(!secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
|
2013-04-01 02:21:05 -03:00
|
|
|
secp256k1_ecdsa_sig_free(&sig);
|
2013-03-24 06:38:35 -03:00
|
|
|
secp256k1_num_free(&msg);
|
|
|
|
secp256k1_num_free(&key);
|
2013-03-17 22:41:01 -03:00
|
|
|
}
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
void run_ecdsa_sign_verify() {
|
|
|
|
for (int i=0; i<10*count; i++) {
|
2013-03-17 22:41:01 -03:00
|
|
|
test_ecdsa_sign_verify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:54:41 -04:00
|
|
|
void test_ecdsa_end_to_end() {
|
|
|
|
unsigned char privkey[32];
|
|
|
|
unsigned char message[32];
|
|
|
|
|
|
|
|
// Generate a random key and message.
|
|
|
|
{
|
|
|
|
secp256k1_num_t msg, key;
|
|
|
|
secp256k1_num_init(&msg);
|
|
|
|
random_num_order_test(&msg);
|
|
|
|
secp256k1_num_init(&key);
|
|
|
|
random_num_order_test(&key);
|
|
|
|
secp256k1_num_get_bin(privkey, 32, &key);
|
|
|
|
secp256k1_num_get_bin(message, 32, &msg);
|
|
|
|
secp256k1_num_free(&msg);
|
|
|
|
secp256k1_num_free(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct and verify corresponding public key.
|
|
|
|
CHECK(secp256k1_ecdsa_seckey_verify(privkey) == 1);
|
|
|
|
char pubkey[65]; int pubkeylen = 65;
|
|
|
|
CHECK(secp256k1_ecdsa_pubkey_create(pubkey, &pubkeylen, privkey, secp256k1_rand32() % 2) == 1);
|
|
|
|
CHECK(secp256k1_ecdsa_pubkey_verify(pubkey, pubkeylen));
|
|
|
|
|
|
|
|
// Verify private key import and export.
|
|
|
|
unsigned char seckey[300]; int seckeylen = 300;
|
|
|
|
CHECK(secp256k1_ecdsa_privkey_export(privkey, seckey, &seckeylen, secp256k1_rand32() % 2) == 1);
|
|
|
|
unsigned char privkey2[32];
|
|
|
|
CHECK(secp256k1_ecdsa_privkey_import(privkey2, seckey, seckeylen) == 1);
|
|
|
|
CHECK(memcmp(privkey, privkey2, 32) == 0);
|
|
|
|
|
|
|
|
// Optionally tweak the keys using addition.
|
|
|
|
if (secp256k1_rand32() % 3 == 0) {
|
|
|
|
unsigned char rnd[32];
|
|
|
|
secp256k1_rand256_test(rnd);
|
|
|
|
int ret1 = secp256k1_ecdsa_privkey_tweak_add(privkey, rnd);
|
|
|
|
int ret2 = secp256k1_ecdsa_pubkey_tweak_add(pubkey, pubkeylen, rnd);
|
|
|
|
CHECK(ret1 == ret2);
|
|
|
|
if (ret1 == 0) return;
|
|
|
|
char pubkey2[65]; int pubkeylen2 = 65;
|
|
|
|
CHECK(secp256k1_ecdsa_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1);
|
|
|
|
CHECK(memcmp(pubkey, pubkey2, pubkeylen) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally tweak the keys using multiplication.
|
|
|
|
if (secp256k1_rand32() % 3 == 0) {
|
|
|
|
unsigned char rnd[32];
|
|
|
|
secp256k1_rand256_test(rnd);
|
|
|
|
int ret1 = secp256k1_ecdsa_privkey_tweak_mul(privkey, rnd);
|
|
|
|
int ret2 = secp256k1_ecdsa_pubkey_tweak_mul(pubkey, pubkeylen, rnd);
|
|
|
|
CHECK(ret1 == ret2);
|
|
|
|
if (ret1 == 0) return;
|
|
|
|
char pubkey2[65]; int pubkeylen2 = 65;
|
|
|
|
CHECK(secp256k1_ecdsa_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1);
|
|
|
|
CHECK(memcmp(pubkey, pubkey2, pubkeylen) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign.
|
|
|
|
unsigned char signature[72]; unsigned int signaturelen = 72;
|
|
|
|
while(1) {
|
|
|
|
unsigned char rnd[32];
|
|
|
|
secp256k1_rand256_test(rnd);
|
|
|
|
if (secp256k1_ecdsa_sign(message, 32, signature, &signaturelen, privkey, rnd) == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Verify.
|
|
|
|
CHECK(secp256k1_ecdsa_verify(message, 32, signature, signaturelen, pubkey, pubkeylen) == 1);
|
|
|
|
// Destroy signature and verify again.
|
|
|
|
signature[signaturelen - 1 - secp256k1_rand32() % 20] += 1 + (secp256k1_rand32() % 255);
|
|
|
|
CHECK(secp256k1_ecdsa_verify(message, 32, signature, signaturelen, pubkey, pubkeylen) != 1);
|
|
|
|
|
|
|
|
// Compact sign.
|
|
|
|
unsigned char csignature[64]; unsigned int recid = 0;
|
|
|
|
while(1) {
|
|
|
|
unsigned char rnd[32];
|
|
|
|
secp256k1_rand256_test(rnd);
|
|
|
|
if (secp256k1_ecdsa_sign_compact(message, 32, csignature, privkey, rnd, &recid) == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Recover.
|
|
|
|
unsigned char recpubkey[65]; unsigned recpubkeylen = 0;
|
|
|
|
CHECK(secp256k1_ecdsa_recover_compact(message, 32, csignature, recpubkey, &recpubkeylen, pubkeylen == 33, recid) == 1);
|
|
|
|
CHECK(recpubkeylen == pubkeylen);
|
|
|
|
CHECK(memcmp(pubkey, recpubkey, pubkeylen) == 0);
|
|
|
|
// Destroy signature and verify again.
|
|
|
|
csignature[secp256k1_rand32() % 64] += 1 + (secp256k1_rand32() % 255);
|
|
|
|
CHECK(secp256k1_ecdsa_recover_compact(message, 32, csignature, recpubkey, &recpubkeylen, pubkeylen == 33, recid) != 1 ||
|
|
|
|
memcmp(pubkey, recpubkey, pubkeylen) != 0);
|
|
|
|
CHECK(recpubkeylen == pubkeylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_ecdsa_end_to_end() {
|
|
|
|
for (int i=0; i<64*count; i++) {
|
|
|
|
test_ecdsa_end_to_end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-05 10:55:05 -04:00
|
|
|
#ifdef ENABLE_OPENSSL_TESTS
|
|
|
|
EC_KEY *get_openssl_key(const secp256k1_num_t *key) {
|
2013-05-09 18:53:47 -04:00
|
|
|
unsigned char privkey[300];
|
|
|
|
int privkeylen;
|
|
|
|
int compr = secp256k1_rand32() & 1;
|
|
|
|
const unsigned char* pbegin = privkey;
|
2013-05-05 10:55:05 -04:00
|
|
|
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_ecdsa_privkey_serialize(privkey, &privkeylen, key, compr));
|
|
|
|
CHECK(d2i_ECPrivateKey(&ec_key, &pbegin, privkeylen));
|
|
|
|
CHECK(EC_KEY_check_key(ec_key));
|
2013-05-05 10:55:05 -04:00
|
|
|
return ec_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_ecdsa_openssl() {
|
|
|
|
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
|
|
|
|
secp256k1_num_t key, msg;
|
|
|
|
secp256k1_num_init(&msg);
|
|
|
|
unsigned char message[32];
|
|
|
|
secp256k1_rand256_test(message);
|
|
|
|
secp256k1_num_set_bin(&msg, message, 32);
|
|
|
|
secp256k1_num_init(&key);
|
|
|
|
random_num_order_test(&key);
|
|
|
|
secp256k1_gej_t qj;
|
|
|
|
secp256k1_ecmult_gen(&qj, &key);
|
|
|
|
secp256k1_ge_t q;
|
|
|
|
secp256k1_ge_set_gej(&q, &qj);
|
|
|
|
EC_KEY *ec_key = get_openssl_key(&key);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(ec_key);
|
2013-05-05 10:55:05 -04:00
|
|
|
unsigned char signature[80];
|
|
|
|
int sigsize = 80;
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(ECDSA_sign(0, message, sizeof(message), signature, &sigsize, ec_key));
|
2013-05-05 10:55:05 -04:00
|
|
|
secp256k1_ecdsa_sig_t sig;
|
|
|
|
secp256k1_ecdsa_sig_init(&sig);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_ecdsa_sig_parse(&sig, signature, sigsize));
|
|
|
|
CHECK(secp256k1_ecdsa_sig_verify(&sig, &q, &msg));
|
2013-05-05 10:55:05 -04:00
|
|
|
secp256k1_num_inc(&sig.r);
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(!secp256k1_ecdsa_sig_verify(&sig, &q, &msg));
|
2013-05-05 10:55:05 -04:00
|
|
|
|
|
|
|
random_sign(&sig, &key, &msg, NULL);
|
|
|
|
sigsize = 80;
|
2014-06-15 19:30:17 -04:00
|
|
|
CHECK(secp256k1_ecdsa_sig_serialize(signature, &sigsize, &sig));
|
|
|
|
CHECK(ECDSA_verify(0, message, sizeof(message), signature, sigsize, ec_key) == 1);
|
2013-05-05 10:55:05 -04:00
|
|
|
|
|
|
|
secp256k1_ecdsa_sig_free(&sig);
|
|
|
|
EC_KEY_free(ec_key);
|
|
|
|
secp256k1_num_free(&key);
|
|
|
|
secp256k1_num_free(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_ecdsa_openssl() {
|
|
|
|
for (int i=0; i<10*count; i++) {
|
|
|
|
test_ecdsa_openssl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc > 1)
|
2013-04-21 20:52:56 -03:00
|
|
|
count = strtol(argv[1], NULL, 0)*47;
|
2013-04-21 14:07:21 -03:00
|
|
|
|
2013-05-05 10:55:05 -04:00
|
|
|
printf("test count = %i\n", count);
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
// initialize
|
2014-08-03 13:54:41 -04:00
|
|
|
secp256k1_start();
|
2013-03-24 06:38:35 -03:00
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
// num tests
|
2013-04-21 20:52:56 -03:00
|
|
|
run_num_smalltests();
|
2013-04-21 14:07:21 -03:00
|
|
|
|
2014-05-20 23:22:14 -04:00
|
|
|
// field tests
|
2014-07-02 05:01:26 -04:00
|
|
|
run_field_inv();
|
|
|
|
run_field_inv_var();
|
|
|
|
run_field_inv_all();
|
|
|
|
run_field_inv_all_var();
|
2014-06-22 04:03:29 -04:00
|
|
|
run_sqr();
|
2014-05-20 23:22:14 -04:00
|
|
|
run_sqrt();
|
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
// ecmult tests
|
|
|
|
run_wnaf();
|
|
|
|
run_point_times_order();
|
|
|
|
run_ecmult_chain();
|
|
|
|
|
|
|
|
// ecdsa tests
|
|
|
|
run_ecdsa_sign_verify();
|
2014-08-03 13:54:41 -04:00
|
|
|
run_ecdsa_end_to_end();
|
2013-05-05 10:55:05 -04:00
|
|
|
#ifdef ENABLE_OPENSSL_TESTS
|
|
|
|
run_ecdsa_openssl();
|
|
|
|
#endif
|
2013-03-30 18:32:16 -03:00
|
|
|
|
2013-04-21 14:07:21 -03:00
|
|
|
// shutdown
|
2014-08-03 13:54:41 -04:00
|
|
|
secp256k1_stop();
|
2013-03-10 17:25:19 -03:00
|
|
|
return 0;
|
|
|
|
}
|