Fix test_constant_wnaf for -1 and add a test for it.
Before, test_constant_wnaf used scalar_cadd_bit to correct for the skew. But this function does not correctly deal with overflows which is why num = -1 couldn't be tested. This commit also adds tests for 0, 1/2 and 1/2-1 as they are corner cases in constant_wnaf.
This commit is contained in:
parent
3b7d26b23c
commit
6bb0b77e15
25
src/tests.c
25
src/tests.c
@ -3190,6 +3190,7 @@ void test_constant_wnaf(const secp256k1_scalar *number, int w) {
|
||||
int skew;
|
||||
int bits = 256;
|
||||
secp256k1_scalar num = *number;
|
||||
secp256k1_scalar scalar_skew;
|
||||
|
||||
secp256k1_scalar_set_int(&x, 0);
|
||||
secp256k1_scalar_set_int(&shift, 1 << w);
|
||||
@ -3220,7 +3221,8 @@ void test_constant_wnaf(const secp256k1_scalar *number, int w) {
|
||||
secp256k1_scalar_add(&x, &x, &t);
|
||||
}
|
||||
/* Skew num because when encoding numbers as odd we use an offset */
|
||||
secp256k1_scalar_cadd_bit(&num, skew == 2, 1);
|
||||
secp256k1_scalar_set_int(&scalar_skew, 1 << (skew == 2));
|
||||
secp256k1_scalar_add(&num, &num, &scalar_skew);
|
||||
CHECK(secp256k1_scalar_eq(&x, &num));
|
||||
}
|
||||
|
||||
@ -3332,13 +3334,32 @@ void run_wnaf(void) {
|
||||
int i;
|
||||
secp256k1_scalar n = {{0}};
|
||||
|
||||
test_constant_wnaf(&n, 4);
|
||||
/* Sanity check: 1 and 2 are the smallest odd and even numbers and should
|
||||
* have easier-to-diagnose failure modes */
|
||||
n.d[0] = 1;
|
||||
test_constant_wnaf(&n, 4);
|
||||
n.d[0] = 2;
|
||||
test_constant_wnaf(&n, 4);
|
||||
/* Test 0 */
|
||||
/* Test -1, because it's a special case in wnaf_const */
|
||||
n = secp256k1_scalar_one;
|
||||
secp256k1_scalar_negate(&n, &n);
|
||||
test_constant_wnaf(&n, 4);
|
||||
|
||||
/* Test -2, which may not lead to overflows in wnaf_const */
|
||||
secp256k1_scalar_add(&n, &secp256k1_scalar_one, &secp256k1_scalar_one);
|
||||
secp256k1_scalar_negate(&n, &n);
|
||||
test_constant_wnaf(&n, 4);
|
||||
|
||||
/* Test (1/2) - 1 = 1/-2 and 1/2 = (1/-2) + 1
|
||||
as corner cases of negation handling in wnaf_const */
|
||||
secp256k1_scalar_inverse(&n, &n);
|
||||
test_constant_wnaf(&n, 4);
|
||||
|
||||
secp256k1_scalar_add(&n, &n, &secp256k1_scalar_one);
|
||||
test_constant_wnaf(&n, 4);
|
||||
|
||||
/* Test 0 for fixed wnaf */
|
||||
test_fixed_wnaf_small();
|
||||
/* Random tests */
|
||||
for (i = 0; i < count; i++) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user