Skip to content

Commit 94b8555

Browse files
committed
refactor: take use of secp256k1_scalar_{zero,one} constants
1 parent 908e02d commit 94b8555

File tree

4 files changed

+19
-41
lines changed

4 files changed

+19
-41
lines changed

src/bench_ecmult.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ static void bench_ecmult_1p_teardown(void* arg, int iters) {
138138

139139
static void bench_ecmult_0p_g(void* arg, int iters) {
140140
bench_data* data = (bench_data*)arg;
141-
secp256k1_scalar zero;
142141
int i;
143142

144-
secp256k1_scalar_set_int(&zero, 0);
145143
for (i = 0; i < iters; ++i) {
146-
secp256k1_ecmult(&data->output[i], NULL, &zero, &data->scalars[(data->offset1+i) % POINTS]);
144+
secp256k1_ecmult(&data->output[i], NULL, &secp256k1_scalar_zero, &data->scalars[(data->offset1+i) % POINTS]);
147145
}
148146
}
149147

src/eckey_impl.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp25
5959

6060
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak) {
6161
secp256k1_gej pt;
62-
secp256k1_scalar one;
6362
secp256k1_gej_set_ge(&pt, key);
64-
secp256k1_scalar_set_int(&one, 1);
65-
secp256k1_ecmult(&pt, &pt, &one, tweak);
63+
secp256k1_ecmult(&pt, &pt, &secp256k1_scalar_one, tweak);
6664

6765
if (secp256k1_gej_is_infinity(&pt)) {
6866
return 0;
@@ -80,15 +78,13 @@ static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp25
8078
}
8179

8280
static int secp256k1_eckey_pubkey_tweak_mul(secp256k1_ge *key, const secp256k1_scalar *tweak) {
83-
secp256k1_scalar zero;
8481
secp256k1_gej pt;
8582
if (secp256k1_scalar_is_zero(tweak)) {
8683
return 0;
8784
}
8885

89-
secp256k1_scalar_set_int(&zero, 0);
9086
secp256k1_gej_set_ge(&pt, key);
91-
secp256k1_ecmult(&pt, &pt, tweak, &zero);
87+
secp256k1_ecmult(&pt, &pt, tweak, &secp256k1_scalar_zero);
9288
secp256k1_ge_set_gej(key, &pt);
9389
return 1;
9490
}

src/ecmult_impl.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,12 @@ static size_t secp256k1_pippenger_max_points(const secp256k1_callback* error_cal
770770
* require a scratch space */
771771
static int secp256k1_ecmult_multi_simple_var(secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points) {
772772
size_t point_idx;
773-
secp256k1_scalar szero;
774773
secp256k1_gej tmpj;
775774

776-
secp256k1_scalar_set_int(&szero, 0);
777775
secp256k1_gej_set_infinity(r);
778776
secp256k1_gej_set_infinity(&tmpj);
779777
/* r = inp_g_sc*G */
780-
secp256k1_ecmult(r, &tmpj, &szero, inp_g_sc);
778+
secp256k1_ecmult(r, &tmpj, &secp256k1_scalar_zero, inp_g_sc);
781779
for (point_idx = 0; point_idx < n_points; point_idx++) {
782780
secp256k1_ge point;
783781
secp256k1_gej pointj;
@@ -825,9 +823,7 @@ static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback,
825823
if (inp_g_sc == NULL && n == 0) {
826824
return 1;
827825
} else if (n == 0) {
828-
secp256k1_scalar szero;
829-
secp256k1_scalar_set_int(&szero, 0);
830-
secp256k1_ecmult(r, r, &szero, inp_g_sc);
826+
secp256k1_ecmult(r, r, &secp256k1_scalar_zero, inp_g_sc);
831827
return 1;
832828
}
833829
if (scratch == NULL) {

src/tests.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,26 +2304,23 @@ static void scalar_test(void) {
23042304

23052305
{
23062306
/* Test multiplicative identity. */
2307-
secp256k1_scalar r1, v1;
2308-
secp256k1_scalar_set_int(&v1,1);
2309-
secp256k1_scalar_mul(&r1, &s1, &v1);
2307+
secp256k1_scalar r1;
2308+
secp256k1_scalar_mul(&r1, &s1, &secp256k1_scalar_one);
23102309
CHECK(secp256k1_scalar_eq(&r1, &s1));
23112310
}
23122311

23132312
{
23142313
/* Test additive identity. */
2315-
secp256k1_scalar r1, v0;
2316-
secp256k1_scalar_set_int(&v0,0);
2317-
secp256k1_scalar_add(&r1, &s1, &v0);
2314+
secp256k1_scalar r1;
2315+
secp256k1_scalar_add(&r1, &s1, &secp256k1_scalar_zero);
23182316
CHECK(secp256k1_scalar_eq(&r1, &s1));
23192317
}
23202318

23212319
{
23222320
/* Test zero product property. */
2323-
secp256k1_scalar r1, v0;
2324-
secp256k1_scalar_set_int(&v0,0);
2325-
secp256k1_scalar_mul(&r1, &s1, &v0);
2326-
CHECK(secp256k1_scalar_eq(&r1, &v0));
2321+
secp256k1_scalar r1;
2322+
secp256k1_scalar_mul(&r1, &s1, &secp256k1_scalar_zero);
2323+
CHECK(secp256k1_scalar_eq(&r1, &secp256k1_scalar_zero));
23272324
}
23282325

23292326
}
@@ -2356,11 +2353,9 @@ static void run_scalar_tests(void) {
23562353

23572354
{
23582355
/* (-1)+1 should be zero. */
2359-
secp256k1_scalar s, o;
2360-
secp256k1_scalar_set_int(&s, 1);
2361-
CHECK(secp256k1_scalar_is_one(&s));
2362-
secp256k1_scalar_negate(&o, &s);
2363-
secp256k1_scalar_add(&o, &o, &s);
2356+
secp256k1_scalar o;
2357+
secp256k1_scalar_negate(&o, &secp256k1_scalar_one);
2358+
secp256k1_scalar_add(&o, &o, &secp256k1_scalar_one);
23642359
CHECK(secp256k1_scalar_is_zero(&o));
23652360
secp256k1_scalar_negate(&o, &o);
23662361
CHECK(secp256k1_scalar_is_zero(&o));
@@ -2385,7 +2380,6 @@ static void run_scalar_tests(void) {
23852380
secp256k1_scalar y;
23862381
secp256k1_scalar z;
23872382
secp256k1_scalar zz;
2388-
secp256k1_scalar one;
23892383
secp256k1_scalar r1;
23902384
secp256k1_scalar r2;
23912385
secp256k1_scalar zzv;
@@ -2922,7 +2916,6 @@ static void run_scalar_tests(void) {
29222916
0x1e, 0x86, 0x5d, 0x89, 0x63, 0xe6, 0x0a, 0x46,
29232917
0x5c, 0x02, 0x97, 0x1b, 0x62, 0x43, 0x86, 0xf5}}
29242918
};
2925-
secp256k1_scalar_set_int(&one, 1);
29262919
for (i = 0; i < 33; i++) {
29272920
secp256k1_scalar_set_b32(&x, chal[i][0], &overflow);
29282921
CHECK(!overflow);
@@ -2945,7 +2938,7 @@ static void run_scalar_tests(void) {
29452938
CHECK(secp256k1_scalar_eq(&x, &z));
29462939
secp256k1_scalar_mul(&zz, &zz, &y);
29472940
CHECK(!secp256k1_scalar_check_overflow(&zz));
2948-
CHECK(secp256k1_scalar_eq(&one, &zz));
2941+
CHECK(secp256k1_scalar_eq(&secp256k1_scalar_one, &zz));
29492942
}
29502943
}
29512944
}
@@ -4643,7 +4636,7 @@ static int ecmult_multi_false_callback(secp256k1_scalar *sc, secp256k1_ge *pt, s
46434636

46444637
static void test_ecmult_multi(secp256k1_scratch *scratch, secp256k1_ecmult_multi_func ecmult_multi) {
46454638
int ncount;
4646-
secp256k1_scalar szero;
4639+
const secp256k1_scalar szero = secp256k1_scalar_zero;
46474640
secp256k1_scalar sc[32];
46484641
secp256k1_ge pt[32];
46494642
secp256k1_gej r;
@@ -4652,7 +4645,6 @@ static void test_ecmult_multi(secp256k1_scratch *scratch, secp256k1_ecmult_multi
46524645

46534646
data.sc = sc;
46544647
data.pt = pt;
4655-
secp256k1_scalar_set_int(&szero, 0);
46564648

46574649
/* No points to multiply */
46584650
CHECK(ecmult_multi(&CTX->error_callback, scratch, &r, NULL, ecmult_multi_callback, &data, 0));
@@ -5033,7 +5025,6 @@ static int test_ecmult_multi_random(secp256k1_scratch *scratch) {
50335025
}
50345026

50355027
static void test_ecmult_multi_batch_single(secp256k1_ecmult_multi_func ecmult_multi) {
5036-
secp256k1_scalar szero;
50375028
secp256k1_scalar sc;
50385029
secp256k1_ge pt;
50395030
secp256k1_gej r;
@@ -5044,11 +5035,10 @@ static void test_ecmult_multi_batch_single(secp256k1_ecmult_multi_func ecmult_mu
50445035
random_scalar_order(&sc);
50455036
data.sc = &sc;
50465037
data.pt = &pt;
5047-
secp256k1_scalar_set_int(&szero, 0);
50485038

50495039
/* Try to multiply 1 point, but scratch space is empty.*/
50505040
scratch_empty = secp256k1_scratch_create(&CTX->error_callback, 0);
5051-
CHECK(!ecmult_multi(&CTX->error_callback, scratch_empty, &r, &szero, ecmult_multi_callback, &data, 1));
5041+
CHECK(!ecmult_multi(&CTX->error_callback, scratch_empty, &r, &secp256k1_scalar_zero, ecmult_multi_callback, &data, 1));
50525042
secp256k1_scratch_destroy(&CTX->error_callback, scratch_empty);
50535043
}
50545044

@@ -5156,7 +5146,6 @@ static void test_ecmult_multi_batch_size_helper(void) {
51565146
static void test_ecmult_multi_batching(void) {
51575147
static const int n_points = 2*ECMULT_PIPPENGER_THRESHOLD;
51585148
secp256k1_scalar scG;
5159-
secp256k1_scalar szero;
51605149
secp256k1_scalar *sc = (secp256k1_scalar *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_scalar) * n_points);
51615150
secp256k1_ge *pt = (secp256k1_ge *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * n_points);
51625151
secp256k1_gej r;
@@ -5166,11 +5155,10 @@ static void test_ecmult_multi_batching(void) {
51665155
secp256k1_scratch *scratch;
51675156

51685157
secp256k1_gej_set_infinity(&r2);
5169-
secp256k1_scalar_set_int(&szero, 0);
51705158

51715159
/* Get random scalars and group elements and compute result */
51725160
random_scalar_order(&scG);
5173-
secp256k1_ecmult(&r2, &r2, &szero, &scG);
5161+
secp256k1_ecmult(&r2, &r2, &secp256k1_scalar_zero, &scG);
51745162
for(i = 0; i < n_points; i++) {
51755163
secp256k1_ge ptg;
51765164
secp256k1_gej ptgj;

0 commit comments

Comments
 (0)