Skip to content

Commit 7ef4a3e

Browse files
committed
Cleanup and comments
1 parent 4372e28 commit 7ef4a3e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/scalar_4x64_impl.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
493493
uint64_t p0, p1, p2, p3, p4;
494494

495495
/* Reduce 512 bits into 385. */
496-
/* m[0..6] = l[0..3] + n[0..3] * SECP256K1_N_C. */
496+
/* m[0..6] = l[0..3] + l[4..7] * SECP256K1_N_C. */
497497
c = (uint128_t)n4 * SECP256K1_N_C_0;
498498
c += l[0];
499499
m0 = (uint64_t)c; c >>= 64;
@@ -504,29 +504,29 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
504504
c += (uint64_t)u; u >>= 64;
505505
m1 = (uint64_t)c; c >>= 64;
506506

507-
c += n4;
507+
c += n4; /* SECP256K1_N_C_2 == 1 */
508508
u += (uint128_t)n5 * SECP256K1_N_C_1;
509509
u += l[2];
510510
v = (uint128_t)n6 * SECP256K1_N_C_0;
511511
c += (uint64_t)u; u >>= 64;
512512
c += (uint64_t)v; v >>= 64;
513513
m2 = (uint64_t)c; c >>= 64;
514514

515-
c += n5;
515+
c += n5; /* SECP256K1_N_C_2 == 1 */
516516
u += (uint128_t)n6 * SECP256K1_N_C_1;
517517
u += l[3];
518518
v += (uint128_t)n7 * SECP256K1_N_C_0;
519519
c += (uint64_t)u; u >>= 64;
520520
c += (uint64_t)v; v >>= 64;
521521
m3 = (uint64_t)c; c >>= 64;
522522

523-
c += n6;
523+
c += n6; /* SECP256K1_N_C_2 == 1 */
524524
u += (uint128_t)n7 * SECP256K1_N_C_1;
525525
c += (uint64_t)u; u >>= 64;
526526
c += (uint64_t)v;
527527
m4 = (uint64_t)c; c >>= 64;
528528

529-
c += n7;
529+
c += n7; /* SECP256K1_N_C_2 == 1 */
530530
c += (uint64_t)u;
531531
m5 = (uint64_t)c; c >>= 64;
532532

@@ -546,25 +546,25 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
546546
c += (uint64_t)u; u >>= 64;
547547
p1 = (uint64_t)c; c >>= 64;
548548

549-
c += m4;
549+
c += m4; /* SECP256K1_N_C_2 == 1 */
550550
u += (uint128_t)m5 * SECP256K1_N_C_1;
551551
u += m2;
552-
c += (m6 & SECP256K1_N_C_0);
552+
c += m6 & SECP256K1_N_C_0;
553553
c += (uint64_t)u; u >>= 64;
554554
p2 = (uint64_t)c; c >>= 64;
555555

556-
c += m5;
557-
c += (m6 & SECP256K1_N_C_1);
556+
c += m5; /* SECP256K1_N_C_2 == 1 */
557+
c += m6 & SECP256K1_N_C_1;
558558
c += m3;
559559
c += (uint64_t)u;
560560
p3 = (uint64_t)c; c >>= 64;
561561

562-
p4 = (uint64_t)c - m6;;
562+
p4 = (uint64_t)c - m6; /* SECP256K1_N_C_2 == 1 */
563563
VERIFY_CHECK(p4 <= 3);
564564

565565
/* Effectively add an extra SECP256K1_N_C during the next pass.
566-
* Values that would have landed in the range [SECP256K_N, 2^256)
567-
* will instead "wrap" and carry back to p4 */
566+
* Values that would have landed in the range [SECP256K_N, 2^256) will
567+
* instead "wrap" and carry back to p4 */
568568
++p4;
569569

570570
/* Reduce 258 bits into 256. */
@@ -575,19 +575,20 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
575575
c += (uint128_t)SECP256K1_N_C_1 * p4;
576576
c += p1;
577577
p1 = (uint64_t)c; c >>= 64;
578-
c += p4;
578+
c += p4; /* SECP256K1_N_C_2 == 1 */
579579
c += p2;
580580
p2 = (uint64_t)c; c >>= 64;
581581
c += p3;
582582
p3 = (uint64_t)c; c >>= 64;
583-
VERIFY_CHECK((uint64_t)c <= 1);
584583
p4 = (uint64_t)c;
584+
VERIFY_CHECK(p4 <= 1);
585585

586586
/* Recover the extra SECP256K1_N_C from the previous pass.
587587
* If p4 is 1, it becomes a 0 mask - the final pass is a no-op
588588
* If p4 is 0, the decrement creates a UINT64_MAX mask that enables the
589-
* addition of SECP256K_N in the final pass, which must result
590-
* in a final carry, which balances the accounts. */
589+
* addition of SECP256K_N in the final pass, which MUST result in a final
590+
* carry (because the current value in p[0..3] is >= SECP256K1_N_C), which
591+
* can then be dropped to balance the accounts. */
591592
--p4;
592593

593594
c = p4 & SECP256K1_N_0;

0 commit comments

Comments
 (0)