Skip to content

Commit c6c7c44

Browse files
committed
Remove secp256k1_num_mul from num.h
This function isn't used anywhere and will cause test failures if we implement the full num.h API for a fixed-width 256-bit numeric type. We lose the unit test for secp256k1_scalar_mul_shift_var; we compensate by improving the unit test for secp256k1_scalar_split_lambda (which is the only user of this function) to test that the algebraic relation `N = s_lam * lambda + s_1` actually holds for the lambda decomposition.
1 parent e38eb3b commit c6c7c44

File tree

3 files changed

+10
-66
lines changed

3 files changed

+10
-66
lines changed

src/num.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ static void secp256k1_num_add(secp256k1_num *r, const secp256k1_num *a, const se
4444
/** Subtract two (signed) numbers. */
4545
static void secp256k1_num_sub(secp256k1_num *r, const secp256k1_num *a, const secp256k1_num *b);
4646

47-
/** Multiply two (signed) numbers. */
48-
static void secp256k1_num_mul(secp256k1_num *r, const secp256k1_num *a, const secp256k1_num *b);
49-
5047
/** Replace a number by its remainder modulo m. M's sign is ignored. The result is a number between 0 and m-1,
5148
even if r was negative. */
5249
static void secp256k1_num_mod(secp256k1_num *r, const secp256k1_num *m);

src/num_gmp_impl.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -206,33 +206,6 @@ static void secp256k1_num_sub(secp256k1_num *r, const secp256k1_num *a, const se
206206
secp256k1_num_subadd(r, a, b, 1);
207207
}
208208

209-
static void secp256k1_num_mul(secp256k1_num *r, const secp256k1_num *a, const secp256k1_num *b) {
210-
mp_limb_t tmp[2*NUM_LIMBS+1];
211-
secp256k1_num_sanity(a);
212-
secp256k1_num_sanity(b);
213-
214-
VERIFY_CHECK(a->limbs + b->limbs <= 2*NUM_LIMBS+1);
215-
if ((a->limbs==1 && a->data[0]==0) || (b->limbs==1 && b->data[0]==0)) {
216-
r->limbs = 1;
217-
r->neg = 0;
218-
r->data[0] = 0;
219-
return;
220-
}
221-
if (a->limbs >= b->limbs) {
222-
mpn_mul(tmp, a->data, a->limbs, b->data, b->limbs);
223-
} else {
224-
mpn_mul(tmp, b->data, b->limbs, a->data, a->limbs);
225-
}
226-
r->limbs = a->limbs + b->limbs;
227-
if (r->limbs > 1 && tmp[r->limbs - 1]==0) {
228-
r->limbs--;
229-
}
230-
VERIFY_CHECK(r->limbs <= 2*NUM_LIMBS);
231-
mpn_copyi(r->data, tmp, r->limbs);
232-
r->neg = a->neg ^ b->neg;
233-
memset(tmp, 0, sizeof(tmp));
234-
}
235-
236209
static void secp256k1_num_shift(secp256k1_num *r, int bits) {
237210
if (bits % GMP_NUMB_BITS) {
238211
/* Shift within limbs. */

src/tests.c

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -593,23 +593,6 @@ void scalar_test(void) {
593593
CHECK(secp256k1_num_eq(&rnum, &r2num));
594594
}
595595

596-
{
597-
/* Test that multiplying the scalars is equal to multiplying their numbers modulo the order. */
598-
secp256k1_scalar r;
599-
secp256k1_num r2num;
600-
secp256k1_num rnum;
601-
secp256k1_num_mul(&rnum, &snum, &s2num);
602-
secp256k1_num_mod(&rnum, &order);
603-
secp256k1_scalar_mul(&r, &s, &s2);
604-
secp256k1_scalar_get_num(&r2num, &r);
605-
CHECK(secp256k1_num_eq(&rnum, &r2num));
606-
/* The result can only be zero if at least one of the factors was zero. */
607-
CHECK(secp256k1_scalar_is_zero(&r) == (secp256k1_scalar_is_zero(&s) || secp256k1_scalar_is_zero(&s2)));
608-
/* The results can only be equal to one of the factors if that factor was zero, or the other factor was one. */
609-
CHECK(secp256k1_num_eq(&rnum, &snum) == (secp256k1_scalar_is_zero(&s) || secp256k1_scalar_is_one(&s2)));
610-
CHECK(secp256k1_num_eq(&rnum, &s2num) == (secp256k1_scalar_is_zero(&s2) || secp256k1_scalar_is_one(&s)));
611-
}
612-
613596
{
614597
secp256k1_scalar neg;
615598
secp256k1_num negnum;
@@ -636,24 +619,6 @@ void scalar_test(void) {
636619
CHECK(secp256k1_scalar_is_zero(&neg));
637620
}
638621

639-
{
640-
/* Test secp256k1_scalar_mul_shift_var. */
641-
secp256k1_scalar r;
642-
secp256k1_num one;
643-
secp256k1_num rnum;
644-
secp256k1_num rnum2;
645-
unsigned char cone[1] = {0x01};
646-
unsigned int shift = 256 + secp256k1_rand_int(257);
647-
secp256k1_scalar_mul_shift_var(&r, &s1, &s2, shift);
648-
secp256k1_num_mul(&rnum, &s1num, &s2num);
649-
secp256k1_num_shift(&rnum, shift - 1);
650-
secp256k1_num_set_bin(&one, cone, 1);
651-
secp256k1_num_add(&rnum, &rnum, &one);
652-
secp256k1_num_shift(&rnum, 1);
653-
secp256k1_scalar_get_num(&rnum2, &r);
654-
CHECK(secp256k1_num_eq(&rnum, &rnum2));
655-
}
656-
657622
{
658623
/* test secp256k1_scalar_shr_int */
659624
secp256k1_scalar r;
@@ -2532,13 +2497,22 @@ void run_ecmult_gen_blind(void) {
25322497
/***** ENDOMORPHISH TESTS *****/
25332498
void test_scalar_split(void) {
25342499
secp256k1_scalar full;
2535-
secp256k1_scalar s1, slam;
2500+
secp256k1_scalar s1, slam, stmp;
25362501
const unsigned char zero[32] = {0};
25372502
unsigned char tmp[32];
2503+
secp256k1_scalar lambda = SECP256K1_SCALAR_CONST(
2504+
0x5363ad4c, 0xc05c30e0, 0xa5261c02, 0x8812645a,
2505+
0x122e22ea, 0x20816678, 0xdf02967c, 0x1b23bd72
2506+
);
25382507

25392508
random_scalar_order_test(&full);
25402509
secp256k1_scalar_split_lambda(&s1, &slam, &full);
25412510

2511+
/* check that they are a lambda decomposition */
2512+
secp256k1_scalar_mul(&stmp, &lambda, &slam);
2513+
secp256k1_scalar_add(&stmp, &stmp, &s1);
2514+
CHECK(secp256k1_scalar_eq(&stmp, &full));
2515+
25422516
/* check that both are <= 128 bits in size */
25432517
if (secp256k1_scalar_is_high(&s1)) {
25442518
secp256k1_scalar_negate(&s1, &s1);

0 commit comments

Comments
 (0)