Merge commits '3bab71cf 4258c54f 566faa17 9ce9984f 04bf3f67 5be353d6 2e035af2 5b0444a3 a6f4bcf6 5ec1333d f6bef03c 1f33bb2b 1c895367 6b7e5b71 596b336f 4b84f4bf 024a4094 222ecaf6 4b0f711d 3c818388 f30c7486 1cf15ebd 24c768ae 341cc197 c63ec88e 54d34b6c 073d98a0 9eb6934f ab5a9171 fb3a8063 006ddc1f 3353d3c7 b54a0672 7d4f86d2 e8295d07 3e3d125b acf5c55a ' into temp-merge-1312
This commit is contained in:
120
src/tests.c
120
src/tests.c
@@ -10,7 +10,12 @@
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#ifdef USE_EXTERNAL_DEFAULT_CALLBACKS
|
||||
#pragma message("Ignoring USE_EXTERNAL_CALLBACKS in tests.")
|
||||
#undef USE_EXTERNAL_DEFAULT_CALLBACKS
|
||||
#endif
|
||||
#include "secp256k1.c"
|
||||
|
||||
#include "../include/secp256k1.h"
|
||||
#include "../include/secp256k1_preallocated.h"
|
||||
#include "testrand_impl.h"
|
||||
@@ -85,7 +90,7 @@ static void random_field_element_test(secp256k1_fe *fe) {
|
||||
do {
|
||||
unsigned char b32[32];
|
||||
secp256k1_testrand256_test(b32);
|
||||
if (secp256k1_fe_set_b32(fe, b32)) {
|
||||
if (secp256k1_fe_set_b32_limit(fe, b32)) {
|
||||
break;
|
||||
}
|
||||
} while(1);
|
||||
@@ -2267,7 +2272,7 @@ static void scalar_test(void) {
|
||||
for (i = 0; i < 100; ++i) {
|
||||
int low;
|
||||
int shift = 1 + secp256k1_testrand_int(15);
|
||||
int expected = r.d[0] % (1 << shift);
|
||||
int expected = r.d[0] % (1ULL << shift);
|
||||
low = secp256k1_scalar_shr_int(&r, shift);
|
||||
CHECK(expected == low);
|
||||
}
|
||||
@@ -3122,7 +3127,7 @@ static void random_fe(secp256k1_fe *x) {
|
||||
unsigned char bin[32];
|
||||
do {
|
||||
secp256k1_testrand256(bin);
|
||||
if (secp256k1_fe_set_b32(x, bin)) {
|
||||
if (secp256k1_fe_set_b32_limit(x, bin)) {
|
||||
return;
|
||||
}
|
||||
} while(1);
|
||||
@@ -3132,7 +3137,7 @@ static void random_fe_test(secp256k1_fe *x) {
|
||||
unsigned char bin[32];
|
||||
do {
|
||||
secp256k1_testrand256_test(bin);
|
||||
if (secp256k1_fe_set_b32(x, bin)) {
|
||||
if (secp256k1_fe_set_b32_limit(x, bin)) {
|
||||
return;
|
||||
}
|
||||
} while(1);
|
||||
@@ -3186,7 +3191,7 @@ static void run_field_convert(void) {
|
||||
unsigned char b322[32];
|
||||
secp256k1_fe_storage fes2;
|
||||
/* Check conversions to fe. */
|
||||
CHECK(secp256k1_fe_set_b32(&fe2, b32));
|
||||
CHECK(secp256k1_fe_set_b32_limit(&fe2, b32));
|
||||
CHECK(secp256k1_fe_equal_var(&fe, &fe2));
|
||||
secp256k1_fe_from_storage(&fe2, &fes);
|
||||
CHECK(secp256k1_fe_equal_var(&fe, &fe2));
|
||||
@@ -3197,6 +3202,72 @@ static void run_field_convert(void) {
|
||||
CHECK(secp256k1_memcmp_var(&fes2, &fes, sizeof(fes)) == 0);
|
||||
}
|
||||
|
||||
static void run_field_be32_overflow(void) {
|
||||
{
|
||||
static const unsigned char zero_overflow[32] = {
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F,
|
||||
};
|
||||
static const unsigned char zero[32] = { 0x00 };
|
||||
unsigned char out[32];
|
||||
secp256k1_fe fe;
|
||||
CHECK(secp256k1_fe_set_b32_limit(&fe, zero_overflow) == 0);
|
||||
secp256k1_fe_set_b32_mod(&fe, zero_overflow);
|
||||
CHECK(secp256k1_fe_normalizes_to_zero(&fe) == 1);
|
||||
secp256k1_fe_normalize(&fe);
|
||||
CHECK(secp256k1_fe_is_zero(&fe) == 1);
|
||||
secp256k1_fe_get_b32(out, &fe);
|
||||
CHECK(secp256k1_memcmp_var(out, zero, 32) == 0);
|
||||
}
|
||||
{
|
||||
static const unsigned char one_overflow[32] = {
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x30,
|
||||
};
|
||||
static const unsigned char one[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
};
|
||||
unsigned char out[32];
|
||||
secp256k1_fe fe;
|
||||
CHECK(secp256k1_fe_set_b32_limit(&fe, one_overflow) == 0);
|
||||
secp256k1_fe_set_b32_mod(&fe, one_overflow);
|
||||
secp256k1_fe_normalize(&fe);
|
||||
CHECK(secp256k1_fe_cmp_var(&fe, &secp256k1_fe_one) == 0);
|
||||
secp256k1_fe_get_b32(out, &fe);
|
||||
CHECK(secp256k1_memcmp_var(out, one, 32) == 0);
|
||||
}
|
||||
{
|
||||
static const unsigned char ff_overflow[32] = {
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
};
|
||||
static const unsigned char ff[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0xD0,
|
||||
};
|
||||
unsigned char out[32];
|
||||
secp256k1_fe fe;
|
||||
const secp256k1_fe fe_ff = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0x01, 0x000003d0);
|
||||
CHECK(secp256k1_fe_set_b32_limit(&fe, ff_overflow) == 0);
|
||||
secp256k1_fe_set_b32_mod(&fe, ff_overflow);
|
||||
secp256k1_fe_normalize(&fe);
|
||||
CHECK(secp256k1_fe_cmp_var(&fe, &fe_ff) == 0);
|
||||
secp256k1_fe_get_b32(out, &fe);
|
||||
CHECK(secp256k1_memcmp_var(out, ff, 32) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns true if two field elements have the same representation. */
|
||||
static int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
|
||||
int ret = 1;
|
||||
@@ -3775,7 +3846,7 @@ static void run_inverse_tests(void)
|
||||
b32[31] = i & 0xff;
|
||||
b32[30] = (i >> 8) & 0xff;
|
||||
secp256k1_scalar_set_b32(&x_scalar, b32, NULL);
|
||||
secp256k1_fe_set_b32(&x_fe, b32);
|
||||
secp256k1_fe_set_b32_mod(&x_fe, b32);
|
||||
for (var = 0; var <= 1; ++var) {
|
||||
test_inverse_scalar(NULL, &x_scalar, var);
|
||||
test_inverse_field(NULL, &x_fe, var);
|
||||
@@ -3792,7 +3863,7 @@ static void run_inverse_tests(void)
|
||||
for (i = 0; i < 64 * COUNT; ++i) {
|
||||
(testrand ? secp256k1_testrand256_test : secp256k1_testrand256)(b32);
|
||||
secp256k1_scalar_set_b32(&x_scalar, b32, NULL);
|
||||
secp256k1_fe_set_b32(&x_fe, b32);
|
||||
secp256k1_fe_set_b32_mod(&x_fe, b32);
|
||||
for (var = 0; var <= 1; ++var) {
|
||||
test_inverse_scalar(NULL, &x_scalar, var);
|
||||
test_inverse_field(NULL, &x_fe, var);
|
||||
@@ -4352,7 +4423,7 @@ static void test_group_decompress(const secp256k1_fe* x) {
|
||||
CHECK(secp256k1_fe_equal_var(&ge_odd.x, x));
|
||||
|
||||
/* Check that the Y coordinate result in ge_quad is a square. */
|
||||
CHECK(secp256k1_fe_is_quad_var(&ge_quad.y));
|
||||
CHECK(secp256k1_fe_is_square_var(&ge_quad.y));
|
||||
|
||||
/* Check odd/even Y in ge_odd, ge_even. */
|
||||
CHECK(secp256k1_fe_is_odd(&ge_odd.y));
|
||||
@@ -4614,9 +4685,9 @@ static void test_ecmult_target(const secp256k1_scalar* target, int mode) {
|
||||
secp256k1_ecmult(&p2j, &pj, &n2, &zero);
|
||||
secp256k1_ecmult(&ptj, &pj, target, &zero);
|
||||
} else {
|
||||
secp256k1_ecmult_const(&p1j, &p, &n1, 256);
|
||||
secp256k1_ecmult_const(&p2j, &p, &n2, 256);
|
||||
secp256k1_ecmult_const(&ptj, &p, target, 256);
|
||||
secp256k1_ecmult_const(&p1j, &p, &n1);
|
||||
secp256k1_ecmult_const(&p2j, &p, &n2);
|
||||
secp256k1_ecmult_const(&ptj, &p, target);
|
||||
}
|
||||
|
||||
/* Add them all up: n1*P + n2*P + target*P = (n1+n2+target)*P = (n1+n1-n1-n2)*P = 0. */
|
||||
@@ -4679,7 +4750,7 @@ static void ecmult_const_random_mult(void) {
|
||||
0xb84e4e1b, 0xfb77e21f, 0x96baae2a, 0x63dec956
|
||||
);
|
||||
secp256k1_gej b;
|
||||
secp256k1_ecmult_const(&b, &a, &xn, 256);
|
||||
secp256k1_ecmult_const(&b, &a, &xn);
|
||||
|
||||
CHECK(secp256k1_ge_is_valid_var(&a));
|
||||
ge_equals_gej(&expected_b, &b);
|
||||
@@ -4695,12 +4766,12 @@ static void ecmult_const_commutativity(void) {
|
||||
random_scalar_order_test(&a);
|
||||
random_scalar_order_test(&b);
|
||||
|
||||
secp256k1_ecmult_const(&res1, &secp256k1_ge_const_g, &a, 256);
|
||||
secp256k1_ecmult_const(&res2, &secp256k1_ge_const_g, &b, 256);
|
||||
secp256k1_ecmult_const(&res1, &secp256k1_ge_const_g, &a);
|
||||
secp256k1_ecmult_const(&res2, &secp256k1_ge_const_g, &b);
|
||||
secp256k1_ge_set_gej(&mid1, &res1);
|
||||
secp256k1_ge_set_gej(&mid2, &res2);
|
||||
secp256k1_ecmult_const(&res1, &mid1, &b, 256);
|
||||
secp256k1_ecmult_const(&res2, &mid2, &a, 256);
|
||||
secp256k1_ecmult_const(&res1, &mid1, &b);
|
||||
secp256k1_ecmult_const(&res2, &mid2, &a);
|
||||
secp256k1_ge_set_gej(&mid1, &res1);
|
||||
secp256k1_ge_set_gej(&mid2, &res2);
|
||||
ge_equals_ge(&mid1, &mid2);
|
||||
@@ -4716,13 +4787,13 @@ static void ecmult_const_mult_zero_one(void) {
|
||||
secp256k1_scalar_negate(&negone, &one);
|
||||
|
||||
random_group_element_test(&point);
|
||||
secp256k1_ecmult_const(&res1, &point, &zero, 3);
|
||||
secp256k1_ecmult_const(&res1, &point, &zero);
|
||||
secp256k1_ge_set_gej(&res2, &res1);
|
||||
CHECK(secp256k1_ge_is_infinity(&res2));
|
||||
secp256k1_ecmult_const(&res1, &point, &one, 2);
|
||||
secp256k1_ecmult_const(&res1, &point, &one);
|
||||
secp256k1_ge_set_gej(&res2, &res1);
|
||||
ge_equals_ge(&res2, &point);
|
||||
secp256k1_ecmult_const(&res1, &point, &negone, 256);
|
||||
secp256k1_ecmult_const(&res1, &point, &negone);
|
||||
secp256k1_gej_neg(&res1, &res1);
|
||||
secp256k1_ge_set_gej(&res2, &res1);
|
||||
ge_equals_ge(&res2, &point);
|
||||
@@ -4752,7 +4823,7 @@ static void ecmult_const_mult_xonly(void) {
|
||||
n = base.x;
|
||||
}
|
||||
/* Perform x-only multiplication. */
|
||||
res = secp256k1_ecmult_const_xonly(&resx, &n, (i & 1) ? &d : NULL, &q, 256, i & 2);
|
||||
res = secp256k1_ecmult_const_xonly(&resx, &n, (i & 1) ? &d : NULL, &q, i & 2);
|
||||
CHECK(res);
|
||||
/* Perform normal multiplication. */
|
||||
secp256k1_gej_set_ge(&basej, &base);
|
||||
@@ -4774,7 +4845,7 @@ static void ecmult_const_mult_xonly(void) {
|
||||
random_field_element_test(&x);
|
||||
secp256k1_fe_sqr(&c, &x);
|
||||
secp256k1_fe_mul(&c, &c, &x);
|
||||
secp256k1_fe_add(&c, &secp256k1_fe_const_b);
|
||||
secp256k1_fe_add_int(&c, SECP256K1_B);
|
||||
} while (secp256k1_fe_is_square_var(&c));
|
||||
/* If i is odd, n=d*x for random non-zero d. */
|
||||
if (i & 1) {
|
||||
@@ -4785,7 +4856,7 @@ static void ecmult_const_mult_xonly(void) {
|
||||
} else {
|
||||
n = x;
|
||||
}
|
||||
res = secp256k1_ecmult_const_xonly(&r, &n, (i & 1) ? &d : NULL, &q, 256, 0);
|
||||
res = secp256k1_ecmult_const_xonly(&r, &n, (i & 1) ? &d : NULL, &q, 0);
|
||||
CHECK(res == 0);
|
||||
}
|
||||
}
|
||||
@@ -4810,7 +4881,7 @@ static void ecmult_const_chain_multiply(void) {
|
||||
for (i = 0; i < 100; ++i) {
|
||||
secp256k1_ge tmp;
|
||||
secp256k1_ge_set_gej(&tmp, &point);
|
||||
secp256k1_ecmult_const(&point, &tmp, &scalar, 256);
|
||||
secp256k1_ecmult_const(&point, &tmp, &scalar);
|
||||
}
|
||||
secp256k1_ge_set_gej(&res, &point);
|
||||
ge_equals_gej(&res, &expected_point);
|
||||
@@ -5708,7 +5779,7 @@ static void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar
|
||||
secp256k1_ecmult(&rj3, &infj, &zero, x);
|
||||
secp256k1_ecmult_multi_var(NULL, scratch, &rj4, x, NULL, NULL, 0);
|
||||
secp256k1_ecmult_multi_var(NULL, scratch, &rj5, &zero, test_ecmult_accumulate_cb, (void*)x, 1);
|
||||
secp256k1_ecmult_const(&rj6, &secp256k1_ge_const_g, x, 256);
|
||||
secp256k1_ecmult_const(&rj6, &secp256k1_ge_const_g, x);
|
||||
secp256k1_ge_set_gej_var(&r, &rj1);
|
||||
ge_equals_gej(&r, &rj2);
|
||||
ge_equals_gej(&r, &rj3);
|
||||
@@ -8002,6 +8073,7 @@ int main(int argc, char **argv) {
|
||||
run_field_half();
|
||||
run_field_misc();
|
||||
run_field_convert();
|
||||
run_field_be32_overflow();
|
||||
run_fe_mul();
|
||||
run_sqr();
|
||||
run_sqrt();
|
||||
|
||||
Reference in New Issue
Block a user