Skip to content

Commit a1bd497

Browse files
committed
refactor: take use of secp256k1_scalar_{zero,one} constants (part 2)
1 parent 26392da commit a1bd497

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

src/bench_ecmult.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ static void generate_scalar(uint32_t num, secp256k1_scalar* scalar) {
244244

245245
static void run_ecmult_multi_bench(bench_data* data, size_t count, int includes_g, int num_iters) {
246246
char str[32];
247-
static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
248247
size_t iters = 1 + num_iters / count;
249248
size_t iter;
250249

@@ -262,7 +261,7 @@ static void run_ecmult_multi_bench(bench_data* data, size_t count, int includes_
262261
secp256k1_scalar_add(&total, &total, &tmp);
263262
}
264263
secp256k1_scalar_negate(&total, &total);
265-
secp256k1_ecmult(&data->expected_output[iter], NULL, &zero, &total);
264+
secp256k1_ecmult(&data->expected_output[iter], NULL, &secp256k1_scalar_zero, &total);
266265
}
267266

268267
/* Run the benchmark. */

src/tests.c

+24-32
Original file line numberDiff line numberDiff line change
@@ -4092,7 +4092,7 @@ static void run_gej(void) {
40924092
}
40934093

40944094
static void test_ec_combine(void) {
4095-
secp256k1_scalar sum = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
4095+
secp256k1_scalar sum = secp256k1_scalar_zero;
40964096
secp256k1_pubkey data[6];
40974097
const secp256k1_pubkey* d[6];
40984098
secp256k1_pubkey sd;
@@ -4264,8 +4264,8 @@ static void run_ecmult_chain(void) {
42644264
static const secp256k1_scalar xf = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0x1337);
42654265
static const secp256k1_scalar gf = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0x7113);
42664266
/* accumulators with the resulting coefficients to A and G */
4267-
secp256k1_scalar ae = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
4268-
secp256k1_scalar ge = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
4267+
secp256k1_scalar ae = secp256k1_scalar_one;
4268+
secp256k1_scalar ge = secp256k1_scalar_zero;
42694269
/* actual points */
42704270
secp256k1_gej x;
42714271
secp256k1_gej x2;
@@ -4306,8 +4306,6 @@ static void test_point_times_order(const secp256k1_gej *point) {
43064306
/* X * (point + G) + (order-X) * (pointer + G) = 0 */
43074307
secp256k1_scalar x;
43084308
secp256k1_scalar nx;
4309-
secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
4310-
secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
43114309
secp256k1_gej res1, res2;
43124310
secp256k1_ge res3;
43134311
unsigned char pub[65];
@@ -4325,13 +4323,13 @@ static void test_point_times_order(const secp256k1_gej *point) {
43254323
psize = 65;
43264324
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 1) == 0);
43274325
/* check zero/one edge cases */
4328-
secp256k1_ecmult(&res1, point, &zero, &zero);
4326+
secp256k1_ecmult(&res1, point, &secp256k1_scalar_zero, &secp256k1_scalar_zero);
43294327
secp256k1_ge_set_gej(&res3, &res1);
43304328
CHECK(secp256k1_ge_is_infinity(&res3));
4331-
secp256k1_ecmult(&res1, point, &one, &zero);
4329+
secp256k1_ecmult(&res1, point, &secp256k1_scalar_one, &secp256k1_scalar_zero);
43324330
secp256k1_ge_set_gej(&res3, &res1);
43334331
ge_equals_gej(&res3, point);
4334-
secp256k1_ecmult(&res1, point, &zero, &one);
4332+
secp256k1_ecmult(&res1, point, &secp256k1_scalar_zero, &secp256k1_scalar_one);
43354333
secp256k1_ge_set_gej(&res3, &res1);
43364334
ge_equals_ge(&res3, &secp256k1_ge_const_g);
43374335
}
@@ -4371,7 +4369,6 @@ static void test_ecmult_target(const secp256k1_scalar* target, int mode) {
43714369
secp256k1_scalar n1, n2;
43724370
secp256k1_ge p;
43734371
secp256k1_gej pj, p1j, p2j, ptj;
4374-
static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
43754372

43764373
/* Generate random n1,n2 such that n1+n2 = -target. */
43774374
random_scalar_order_test(&n1);
@@ -4390,9 +4387,9 @@ static void test_ecmult_target(const secp256k1_scalar* target, int mode) {
43904387
secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &p2j, &n2);
43914388
secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &ptj, target);
43924389
} else if (mode == 1) {
4393-
secp256k1_ecmult(&p1j, &pj, &n1, &zero);
4394-
secp256k1_ecmult(&p2j, &pj, &n2, &zero);
4395-
secp256k1_ecmult(&ptj, &pj, target, &zero);
4390+
secp256k1_ecmult(&p1j, &pj, &n1, &secp256k1_scalar_zero);
4391+
secp256k1_ecmult(&p2j, &pj, &n2, &secp256k1_scalar_zero);
4392+
secp256k1_ecmult(&ptj, &pj, target, &secp256k1_scalar_zero);
43964393
} else {
43974394
secp256k1_ecmult_const(&p1j, &p, &n1);
43984395
secp256k1_ecmult_const(&p2j, &p, &n2);
@@ -4487,19 +4484,17 @@ static void ecmult_const_commutativity(void) {
44874484
}
44884485

44894486
static void ecmult_const_mult_zero_one(void) {
4490-
secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
4491-
secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
44924487
secp256k1_scalar negone;
44934488
secp256k1_gej res1;
44944489
secp256k1_ge res2;
44954490
secp256k1_ge point;
4496-
secp256k1_scalar_negate(&negone, &one);
4491+
secp256k1_scalar_negate(&negone, &secp256k1_scalar_one);
44974492

44984493
random_group_element_test(&point);
4499-
secp256k1_ecmult_const(&res1, &point, &zero);
4494+
secp256k1_ecmult_const(&res1, &point, &secp256k1_scalar_zero);
45004495
secp256k1_ge_set_gej(&res2, &res1);
45014496
CHECK(secp256k1_ge_is_infinity(&res2));
4502-
secp256k1_ecmult_const(&res1, &point, &one);
4497+
secp256k1_ecmult_const(&res1, &point, &secp256k1_scalar_one);
45034498
secp256k1_ge_set_gej(&res2, &res1);
45044499
ge_equals_ge(&res2, &point);
45054500
secp256k1_ecmult_const(&res1, &point, &negone);
@@ -4854,7 +4849,7 @@ static int test_ecmult_multi_random(secp256k1_scratch *scratch) {
48544849
* scalars[0..filled-1] and gejs[0..filled-1] are the scalars and points
48554850
* which form its normal inputs. */
48564851
int filled = 0;
4857-
secp256k1_scalar g_scalar = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
4852+
secp256k1_scalar g_scalar = secp256k1_scalar_zero;
48584853
secp256k1_scalar scalars[128];
48594854
secp256k1_gej gejs[128];
48604855
/* The expected result, and the computed result. */
@@ -5465,16 +5460,15 @@ static void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar
54655460
/* Compute x*G in 6 different ways, serialize it uncompressed, and feed it into acc. */
54665461
secp256k1_gej rj1, rj2, rj3, rj4, rj5, rj6, gj, infj;
54675462
secp256k1_ge r;
5468-
const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
54695463
unsigned char bytes[65];
54705464
size_t size = 65;
54715465
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
54725466
secp256k1_gej_set_infinity(&infj);
54735467
secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &rj1, x);
5474-
secp256k1_ecmult(&rj2, &gj, x, &zero);
5475-
secp256k1_ecmult(&rj3, &infj, &zero, x);
5468+
secp256k1_ecmult(&rj2, &gj, x, &secp256k1_scalar_zero);
5469+
secp256k1_ecmult(&rj3, &infj, &secp256k1_scalar_zero, x);
54765470
secp256k1_ecmult_multi_var(NULL, scratch, &rj4, x, NULL, NULL, 0);
5477-
secp256k1_ecmult_multi_var(NULL, scratch, &rj5, &zero, test_ecmult_accumulate_cb, (void*)x, 1);
5471+
secp256k1_ecmult_multi_var(NULL, scratch, &rj5, &secp256k1_scalar_zero, test_ecmult_accumulate_cb, (void*)x, 1);
54785472
secp256k1_ecmult_const(&rj6, &secp256k1_ge_const_g, x);
54795473
secp256k1_ge_set_gej_var(&r, &rj1);
54805474
ge_equals_gej(&r, &rj2);
@@ -7599,33 +7593,31 @@ static void fe_storage_cmov_test(void) {
75997593
}
76007594

76017595
static void scalar_cmov_test(void) {
7602-
static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
7603-
static const secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
76047596
static const secp256k1_scalar max = SECP256K1_SCALAR_CONST(
76057597
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
76067598
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
76077599
);
76087600
secp256k1_scalar r = max;
7609-
secp256k1_scalar a = zero;
7601+
secp256k1_scalar a = secp256k1_scalar_zero;
76107602

76117603
secp256k1_scalar_cmov(&r, &a, 0);
76127604
CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
76137605

7614-
r = zero; a = max;
7606+
r = secp256k1_scalar_zero; a = max;
76157607
secp256k1_scalar_cmov(&r, &a, 1);
76167608
CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
76177609

7618-
a = zero;
7610+
a = secp256k1_scalar_zero;
76197611
secp256k1_scalar_cmov(&r, &a, 1);
7620-
CHECK(secp256k1_memcmp_var(&r, &zero, sizeof(r)) == 0);
7612+
CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_zero, sizeof(r)) == 0);
76217613

7622-
a = one;
7614+
a = secp256k1_scalar_one;
76237615
secp256k1_scalar_cmov(&r, &a, 1);
7624-
CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
7616+
CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
76257617

7626-
r = one; a = zero;
7618+
r = secp256k1_scalar_one; a = secp256k1_scalar_zero;
76277619
secp256k1_scalar_cmov(&r, &a, 0);
7628-
CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
7620+
CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
76297621
}
76307622

76317623
static void ge_storage_cmov_test(void) {

0 commit comments

Comments
 (0)