Skip to content

Commit 115fdc7

Browse files
committed
Remove unused secp256k1_wnaf_const
1 parent aa9f3a3 commit 115fdc7

File tree

3 files changed

+1
-174
lines changed

3 files changed

+1
-174
lines changed

src/bench_internal.c

-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "field_impl.h"
1515
#include "group_impl.h"
1616
#include "scalar_impl.h"
17-
#include "ecmult_const_impl.h"
1817
#include "ecmult_impl.h"
1918
#include "bench.h"
2019

@@ -321,18 +320,6 @@ static void bench_ecmult_wnaf(void* arg, int iters) {
321320
CHECK(bits <= 256*iters);
322321
}
323322

324-
static void bench_wnaf_const(void* arg, int iters) {
325-
int i, bits = 0, overflow = 0;
326-
bench_inv *data = (bench_inv*)arg;
327-
328-
for (i = 0; i < iters; i++) {
329-
bits += secp256k1_wnaf_const(data->wnaf, &data->scalar[0], WINDOW_A, 256);
330-
overflow += secp256k1_scalar_add(&data->scalar[0], &data->scalar[0], &data->scalar[1]);
331-
}
332-
CHECK(overflow >= 0);
333-
CHECK(bits <= 256*iters);
334-
}
335-
336323
static void bench_sha256(void* arg, int iters) {
337324
int i;
338325
bench_inv *data = (bench_inv*)arg;
@@ -407,7 +394,6 @@ int main(int argc, char **argv) {
407394
if (d || have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_zinv_var", bench_group_add_zinv_var, bench_setup, NULL, &data, 10, iters*10);
408395
if (d || have_flag(argc, argv, "group") || have_flag(argc, argv, "to_affine")) run_benchmark("group_to_affine_var", bench_group_to_affine_var, bench_setup, NULL, &data, 10, iters);
409396

410-
if (d || have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, iters);
411397
if (d || have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, iters);
412398

413399
if (d || have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, iters);

src/ecmult_const_impl.h

-77
Original file line numberDiff line numberDiff line change
@@ -104,83 +104,6 @@ static void secp256k1_ecmult_const_odd_multiples_table_globalz(secp256k1_ge *pre
104104
secp256k1_fe_cmov(&(r)->y, &neg_y, negative); \
105105
} while(0)
106106

107-
/** Convert a number to WNAF notation.
108-
* The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
109-
* It has the following guarantees:
110-
* - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
111-
* - each wnaf[i] is nonzero
112-
* - the number of words set is always WNAF_SIZE(w) + 1
113-
*
114-
* Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
115-
* Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
116-
* CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlag Berlin Heidelberg 2003
117-
*
118-
* Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
119-
*/
120-
static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
121-
int global_sign;
122-
int skew;
123-
int word = 0;
124-
125-
/* 1 2 3 */
126-
int u_last;
127-
int u;
128-
129-
int flip;
130-
secp256k1_scalar s = *scalar;
131-
132-
VERIFY_CHECK(w > 0);
133-
VERIFY_CHECK(size > 0);
134-
135-
/* Note that we cannot handle even numbers by negating them to be odd, as is
136-
* done in other implementations, since if our scalars were specified to have
137-
* width < 256 for performance reasons, their negations would have width 256
138-
* and we'd lose any performance benefit. Instead, we use a variation of a
139-
* technique from Section 4.2 of the Okeya/Tagaki paper, which is to add 1 to the
140-
* number we are encoding when it is even, returning a skew value indicating
141-
* this, and having the caller compensate after doing the multiplication.
142-
*
143-
* In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
144-
* particular, to ensure that the outputs from the endomorphism-split fit into
145-
* 128 bits). If we negate, the parity of our number flips, affecting whether
146-
* we want to add to the scalar to ensure that it's odd. */
147-
flip = secp256k1_scalar_is_high(&s);
148-
skew = flip ^ secp256k1_scalar_is_even(&s);
149-
secp256k1_scalar_cadd_bit(&s, 0, skew);
150-
global_sign = secp256k1_scalar_cond_negate(&s, flip);
151-
152-
/* 4 */
153-
u_last = secp256k1_scalar_shr_int(&s, w);
154-
do {
155-
int even;
156-
157-
/* 4.1 4.4 */
158-
u = secp256k1_scalar_shr_int(&s, w);
159-
/* 4.2 */
160-
even = ((u & 1) == 0);
161-
/* In contrast to the original algorithm, u_last is always > 0 and
162-
* therefore we do not need to check its sign. In particular, it's easy
163-
* to see that u_last is never < 0 because u is never < 0. Moreover,
164-
* u_last is never = 0 because u is never even after a loop
165-
* iteration. The same holds analogously for the initial value of
166-
* u_last (in the first loop iteration). */
167-
VERIFY_CHECK(u_last > 0);
168-
VERIFY_CHECK((u_last & 1) == 1);
169-
u += even;
170-
u_last -= even * (1 << w);
171-
172-
/* 4.3, adapted for global sign change */
173-
wnaf[word++] = u_last * global_sign;
174-
175-
u_last = u;
176-
} while (word * w < size);
177-
wnaf[word] = u * global_sign;
178-
179-
VERIFY_CHECK(secp256k1_scalar_is_zero(&s));
180-
VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
181-
return skew;
182-
}
183-
184107
/* For K as defined in the comment of secp256k1_ecmult_const, we have several precomputed
185108
* formulas/constants.
186109
* - in exhaustive test mode, we give an explicit expression to compute it at compile time: */

src/tests.c

+1-83
Original file line numberDiff line numberDiff line change
@@ -5275,61 +5275,6 @@ static void test_wnaf(const secp256k1_scalar *number, int w) {
52755275
CHECK(secp256k1_scalar_eq(&x, number)); /* check that wnaf represents number */
52765276
}
52775277

5278-
static void test_constant_wnaf_negate(const secp256k1_scalar *number) {
5279-
secp256k1_scalar neg1 = *number;
5280-
secp256k1_scalar neg2 = *number;
5281-
int sign1 = 1;
5282-
int sign2 = 1;
5283-
5284-
if (!secp256k1_scalar_get_bits(&neg1, 0, 1)) {
5285-
secp256k1_scalar_negate(&neg1, &neg1);
5286-
sign1 = -1;
5287-
}
5288-
sign2 = secp256k1_scalar_cond_negate(&neg2, secp256k1_scalar_is_even(&neg2));
5289-
CHECK(sign1 == sign2);
5290-
CHECK(secp256k1_scalar_eq(&neg1, &neg2));
5291-
}
5292-
5293-
static void test_constant_wnaf(const secp256k1_scalar *number, int w) {
5294-
secp256k1_scalar x, shift;
5295-
int wnaf[256] = {0};
5296-
int i;
5297-
int skew;
5298-
int bits = 256;
5299-
secp256k1_scalar num = *number;
5300-
secp256k1_scalar scalar_skew;
5301-
5302-
secp256k1_scalar_set_int(&x, 0);
5303-
secp256k1_scalar_set_int(&shift, 1 << w);
5304-
for (i = 0; i < 16; ++i) {
5305-
secp256k1_scalar_shr_int(&num, 8);
5306-
}
5307-
bits = 128;
5308-
skew = secp256k1_wnaf_const(wnaf, &num, w, bits);
5309-
5310-
for (i = WNAF_SIZE_BITS(bits, w); i >= 0; --i) {
5311-
secp256k1_scalar t;
5312-
int v = wnaf[i];
5313-
CHECK(v != 0); /* check nonzero */
5314-
CHECK(v & 1); /* check parity */
5315-
CHECK(v > -(1 << w)); /* check range above */
5316-
CHECK(v < (1 << w)); /* check range below */
5317-
5318-
secp256k1_scalar_mul(&x, &x, &shift);
5319-
if (v >= 0) {
5320-
secp256k1_scalar_set_int(&t, v);
5321-
} else {
5322-
secp256k1_scalar_set_int(&t, -v);
5323-
secp256k1_scalar_negate(&t, &t);
5324-
}
5325-
secp256k1_scalar_add(&x, &x, &t);
5326-
}
5327-
/* Skew num because when encoding numbers as odd we use an offset */
5328-
secp256k1_scalar_set_int(&scalar_skew, skew);
5329-
secp256k1_scalar_add(&num, &num, &scalar_skew);
5330-
CHECK(secp256k1_scalar_eq(&x, &num));
5331-
}
5332-
53335278
static void test_fixed_wnaf(const secp256k1_scalar *number, int w) {
53345279
secp256k1_scalar x, shift;
53355280
int wnaf[256] = {0};
@@ -5433,41 +5378,14 @@ static void test_fixed_wnaf_small(void) {
54335378

54345379
static void run_wnaf(void) {
54355380
int i;
5436-
secp256k1_scalar n = {{0}};
5437-
5438-
test_constant_wnaf(&n, 4);
5439-
/* Sanity check: 1 and 2 are the smallest odd and even numbers and should
5440-
* have easier-to-diagnose failure modes */
5441-
n.d[0] = 1;
5442-
test_constant_wnaf(&n, 4);
5443-
n.d[0] = 2;
5444-
test_constant_wnaf(&n, 4);
5445-
/* Test -1, because it's a special case in wnaf_const */
5446-
n = secp256k1_scalar_one;
5447-
secp256k1_scalar_negate(&n, &n);
5448-
test_constant_wnaf(&n, 4);
5449-
5450-
/* Test -2, which may not lead to overflows in wnaf_const */
5451-
secp256k1_scalar_add(&n, &secp256k1_scalar_one, &secp256k1_scalar_one);
5452-
secp256k1_scalar_negate(&n, &n);
5453-
test_constant_wnaf(&n, 4);
5454-
5455-
/* Test (1/2) - 1 = 1/-2 and 1/2 = (1/-2) + 1
5456-
as corner cases of negation handling in wnaf_const */
5457-
secp256k1_scalar_inverse(&n, &n);
5458-
test_constant_wnaf(&n, 4);
5459-
5460-
secp256k1_scalar_add(&n, &n, &secp256k1_scalar_one);
5461-
test_constant_wnaf(&n, 4);
5381+
secp256k1_scalar n;
54625382

54635383
/* Test 0 for fixed wnaf */
54645384
test_fixed_wnaf_small();
54655385
/* Random tests */
54665386
for (i = 0; i < COUNT; i++) {
54675387
random_scalar_order(&n);
54685388
test_wnaf(&n, 4+(i%10));
5469-
test_constant_wnaf_negate(&n);
5470-
test_constant_wnaf(&n, 4 + (i % 10));
54715389
test_fixed_wnaf(&n, 4 + (i % 10));
54725390
}
54735391
secp256k1_scalar_set_int(&n, 0);

0 commit comments

Comments
 (0)