Skip to content

Commit 59a40bb

Browse files
roconnor-blockstreamFabcien
authored andcommitted
[SECP256K1] Add secp256k1_split_lambda_verify
Summary: Partial backport 4/11 of secp256k1 [[bitcoin-core/secp256k1#830 | PR830]]: bitcoin-core/secp256k1@9aca2f7 Depends on D8039. Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, deadalnix Reviewed By: #bitcoin_abc, deadalnix Differential Revision: https://reviews.bitcoinabc.org/D8040
1 parent 8d2fa28 commit 59a40bb

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/scalar_impl.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#ifndef SECP256K1_SCALAR_IMPL_H
88
#define SECP256K1_SCALAR_IMPL_H
99

10+
#ifdef VERIFY
11+
#include <string.h>
12+
#endif
13+
1014
#include "scalar.h"
1115
#include "util.h"
1216

@@ -430,6 +434,45 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
430434
* Q.E.D.
431435
*/
432436

437+
#ifdef VERIFY
438+
static void secp256k1_scalar_split_lambda_verify(const secp256k1_scalar *r1, const secp256k1_scalar *r2, const secp256k1_scalar *k) {
439+
secp256k1_scalar s;
440+
unsigned char buf1[32];
441+
unsigned char buf2[32];
442+
443+
static const secp256k1_scalar lambda = SECP256K1_SCALAR_CONST(
444+
0x5363AD4CUL, 0xC05C30E0UL, 0xA5261C02UL, 0x8812645AUL,
445+
0x122E22EAUL, 0x20816678UL, 0xDF02967CUL, 0x1B23BD72UL
446+
);
447+
448+
/* (a1 + a2 + 1)/2 is 0xa2a8918ca85bafe22016d0b917e4dd77 */
449+
static const unsigned char k1_bound[32] = {
450+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
451+
0xa2, 0xa8, 0x91, 0x8c, 0xa8, 0x5b, 0xaf, 0xe2, 0x20, 0x16, 0xd0, 0xb9, 0x17, 0xe4, 0xdd, 0x77
452+
};
453+
454+
/* (-b1 + b2)/2 + 1 is 0x8a65287bd47179fb2be08846cea267ed */
455+
static const unsigned char k2_bound[32] = {
456+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
457+
0x8a, 0x65, 0x28, 0x7b, 0xd4, 0x71, 0x79, 0xfb, 0x2b, 0xe0, 0x88, 0x46, 0xce, 0xa2, 0x67, 0xed
458+
};
459+
460+
secp256k1_scalar_mul(&s, &lambda, r2);
461+
secp256k1_scalar_add(&s, &s, r1);
462+
VERIFY_CHECK(secp256k1_scalar_eq(&s, k));
463+
464+
secp256k1_scalar_negate(&s, r1);
465+
secp256k1_scalar_get_b32(buf1, r1);
466+
secp256k1_scalar_get_b32(buf2, &s);
467+
VERIFY_CHECK(secp256k1_memcmp_var(buf1, k1_bound, 32) < 0 || secp256k1_memcmp_var(buf2, k1_bound, 32) < 0);
468+
469+
secp256k1_scalar_negate(&s, r2);
470+
secp256k1_scalar_get_b32(buf1, r2);
471+
secp256k1_scalar_get_b32(buf2, &s);
472+
VERIFY_CHECK(secp256k1_memcmp_var(buf1, k2_bound, 32) < 0 || secp256k1_memcmp_var(buf2, k2_bound, 32) < 0);
473+
}
474+
#endif
475+
433476
static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k) {
434477
secp256k1_scalar c1, c2;
435478
static const secp256k1_scalar minus_lambda = SECP256K1_SCALAR_CONST(
@@ -462,6 +505,10 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
462505
secp256k1_scalar_add(r2, &c1, &c2);
463506
secp256k1_scalar_mul(r1, r2, &minus_lambda);
464507
secp256k1_scalar_add(r1, r1, k);
508+
509+
#ifdef VERIFY
510+
secp256k1_scalar_split_lambda_verify(r1, r2, k);
511+
#endif
465512
}
466513
#endif
467514
#endif

0 commit comments

Comments
 (0)