Skip to content

Commit 551870e

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 04bb703 commit 551870e

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
@@ -591,23 +591,6 @@ void scalar_test(void) {
591591
CHECK(secp256k1_num_eq(&rnum, &r2num));
592592
}
593593

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

637-
{
638-
/* Test secp256k1_scalar_mul_shift_var. */
639-
secp256k1_scalar r;
640-
secp256k1_num one;
641-
secp256k1_num rnum;
642-
secp256k1_num rnum2;
643-
unsigned char cone[1] = {0x01};
644-
unsigned int shift = 256 + secp256k1_rand_int(257);
645-
secp256k1_scalar_mul_shift_var(&r, &s1, &s2, shift);
646-
secp256k1_num_mul(&rnum, &s1num, &s2num);
647-
secp256k1_num_shift(&rnum, shift - 1);
648-
secp256k1_num_set_bin(&one, cone, 1);
649-
secp256k1_num_add(&rnum, &rnum, &one);
650-
secp256k1_num_shift(&rnum, 1);
651-
secp256k1_scalar_get_num(&rnum2, &r);
652-
CHECK(secp256k1_num_eq(&rnum, &rnum2));
653-
}
654-
655620
{
656621
/* test secp256k1_scalar_shr_int */
657622
secp256k1_scalar r;
@@ -1886,13 +1851,22 @@ void run_ecmult_gen_blind(void) {
18861851
/***** ENDOMORPHISH TESTS *****/
18871852
void test_scalar_split(void) {
18881853
secp256k1_scalar full;
1889-
secp256k1_scalar s1, slam;
1854+
secp256k1_scalar s1, slam, stmp;
18901855
const unsigned char zero[32] = {0};
18911856
unsigned char tmp[32];
1857+
secp256k1_scalar lambda = SECP256K1_SCALAR_CONST(
1858+
0x5363ad4c, 0xc05c30e0, 0xa5261c02, 0x8812645a,
1859+
0x122e22ea, 0x20816678, 0xdf02967c, 0x1b23bd72
1860+
);
18921861

18931862
random_scalar_order_test(&full);
18941863
secp256k1_scalar_split_lambda(&s1, &slam, &full);
18951864

1865+
/* check that they are a lambda decomposition */
1866+
secp256k1_scalar_mul(&stmp, &lambda, &slam);
1867+
secp256k1_scalar_add(&stmp, &stmp, &s1);
1868+
CHECK(secp256k1_scalar_eq(&stmp, &full));
1869+
18961870
/* check that both are <= 128 bits in size */
18971871
if (secp256k1_scalar_is_high(&s1)) {
18981872
secp256k1_scalar_negate(&s1, &s1);

0 commit comments

Comments
 (0)