|
7 | 7 | #ifndef SECP256K1_SCALAR_IMPL_H
|
8 | 8 | #define SECP256K1_SCALAR_IMPL_H
|
9 | 9 |
|
| 10 | +#ifdef VERIFY |
| 11 | +#include <string.h> |
| 12 | +#endif |
| 13 | + |
10 | 14 | #include "scalar.h"
|
11 | 15 | #include "util.h"
|
12 | 16 |
|
@@ -430,6 +434,45 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
|
430 | 434 | * Q.E.D.
|
431 | 435 | */
|
432 | 436 |
|
| 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 | + |
433 | 476 | static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k) {
|
434 | 477 | secp256k1_scalar c1, c2;
|
435 | 478 | static const secp256k1_scalar minus_lambda = SECP256K1_SCALAR_CONST(
|
@@ -462,6 +505,10 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
|
462 | 505 | secp256k1_scalar_add(r2, &c1, &c2);
|
463 | 506 | secp256k1_scalar_mul(r1, r2, &minus_lambda);
|
464 | 507 | secp256k1_scalar_add(r1, r1, k);
|
| 508 | + |
| 509 | +#ifdef VERIFY |
| 510 | + secp256k1_scalar_split_lambda_verify(r1, r2, k); |
| 511 | +#endif |
465 | 512 | }
|
466 | 513 | #endif
|
467 | 514 | #endif
|
|
0 commit comments