Skip to content

Commit dbd841a

Browse files
Change Scalar.Mult to use uint again
1 parent 6332a42 commit dbd841a

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/Scalar8x32.cs

+26-27
Original file line numberDiff line numberDiff line change
@@ -648,74 +648,73 @@ public unsafe Scalar8x32 Multiply(in Scalar8x32 b)
648648

649649

650650
[MethodImpl(MethodImplOptions.AggressiveInlining)]
651-
private static void Muladd(uint a, uint b, ref ulong c0, ref ulong c1, ref ulong c2)
651+
private static void Muladd(uint a, uint b, ref uint c0, ref uint c1, ref uint c2)
652652
{
653653
ulong t = (ulong)a * b;
654654
uint th = (uint)(t >> 32);
655655
uint tl = (uint)t;
656656

657-
c0 = (c0 & uint.MaxValue) + tl; // overflow is handled on the next line
658-
th += (uint)(c0 >> 32); // at most 0xFFFFFFFF
659-
c1 += th; // overflow is handled on the next line
660-
c2 += (uint)(c1 >> 32); // never overflows by contract (verified in the next line)
657+
c0 += tl; // overflow is handled on the next line
658+
th += (c0 < tl) ? 1U : 0U; // at most 0xFFFFFFFF
659+
c1 += th; // overflow is handled on the next line
660+
c2 += (c1 < th) ? 1U : 0U; // never overflows by contract (verified in the next line)
661661

662-
c1 &= uint.MaxValue;
663662
Debug.Assert((c1 >= th) || (c2 != 0));
664663
}
665664

666665
// Add a*b to the number defined by (c0,c1). c1 must never overflow.
667666
[MethodImpl(MethodImplOptions.AggressiveInlining)]
668-
private static void MuladdFast(uint a, uint b, ref ulong c0, ref ulong c1)
667+
private static void MuladdFast(uint a, uint b, ref uint c0, ref uint c1)
669668
{
670669
ulong t = (ulong)a * b;
671-
uint th = (uint)(t >> 32); // at most 0xFFFFFFFE
670+
uint th = (uint)(t >> 32); // at most 0xFFFFFFFE
672671
uint tl = (uint)t;
673672

674-
c0 = (c0 & uint.MaxValue) + tl; // overflow is handled on the next line
675-
th += (uint)(c0 >> 32); // at most 0xFFFFFFFF
676-
c1 += th; // never overflows by contract (verified in the next line)
673+
c0 += tl; // overflow is handled on the next line
674+
th += (c0 < tl) ? 1U : 0U; // at most 0xFFFFFFFF
675+
c1 += th; // never overflows by contract (verified in the next line)
677676

678677
Debug.Assert(c1 >= th);
679678
}
680679

681680
// Add a to the number defined by (c0,c1,c2). c2 must never overflow.
682681
[MethodImpl(MethodImplOptions.AggressiveInlining)]
683-
private static void SumAdd(uint a, ref ulong c0, ref ulong c1, ref ulong c2)
682+
private static void SumAdd(uint a, ref uint c0, ref uint c1, ref uint c2)
684683
{
685-
c0 = (c0 & uint.MaxValue) + a; // overflow is handled on the next line
686-
uint over = (uint)(c0 >> 32);
684+
c0 += a; // overflow is handled on the next line
685+
uint over = (c0 < a) ? 1U : 0U;
687686
c1 += over; // overflow is handled on the next line
688-
c2 += (uint)(c1 >> 32); // never overflows by contract
687+
c2 += (c1 < over) ? 1U : 0U; // never overflows by contract
689688

690689
c1 &= uint.MaxValue;
691690
}
692691

693692
// Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero.
694693
[MethodImpl(MethodImplOptions.AggressiveInlining)]
695-
private static void SumaddFast(uint a, ref ulong c0, ref ulong c1, ref ulong c2)
694+
private static void SumaddFast(uint a, ref uint c0, ref uint c1, ref uint c2)
696695
{
697-
c0 = (c0 & uint.MaxValue) + a; // overflow is handled on the next line
698-
c1 += (uint)(c0 >> 32); // never overflows by contract (verified the next line)
696+
c0 += a; // overflow is handled on the next line
697+
c1 += (c0 < a) ? 1U : 0U; // never overflows by contract (verified the next line)
699698

700699
Debug.Assert((c1 != 0) | (c0 >= a));
701700
Debug.Assert(c2 == 0);
702701
}
703702

704703
// Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits.
705704
[MethodImpl(MethodImplOptions.AggressiveInlining)]
706-
private static void Extract(ref uint n, ref ulong c0, ref ulong c1, ref ulong c2)
705+
private static void Extract(ref uint n, ref uint c0, ref uint c1, ref uint c2)
707706
{
708-
n = (uint)c0;
707+
n = c0;
709708
c0 = c1;
710709
c1 = c2;
711710
c2 = 0;
712711
}
713712

714713
// Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. c2 is required to be zero.
715714
[MethodImpl(MethodImplOptions.AggressiveInlining)]
716-
private static void ExtractFast(ref uint n, ref ulong c0, ref ulong c1, ref ulong c2)
715+
private static void ExtractFast(ref uint n, ref uint c0, ref uint c1, ref uint c2)
717716
{
718-
n = (uint)c0;
717+
n = c0;
719718
c0 = c1;
720719
c1 = 0;
721720

@@ -725,7 +724,7 @@ private static void ExtractFast(ref uint n, ref ulong c0, ref ulong c1, ref ulon
725724
private static unsafe void Mult512(uint* l, in Scalar8x32 a, in Scalar8x32 b)
726725
{
727726
// 96 bit accumulator
728-
ulong c0 = 0, c1 = 0, c2 = 0;
727+
uint c0 = 0, c1 = 0, c2 = 0;
729728

730729
// l[0..15] = a[0..7] * b[0..7]
731730
MuladdFast(a.b0, b.b0, ref c0, ref c1);
@@ -808,7 +807,7 @@ private static unsafe void Mult512(uint* l, in Scalar8x32 a, in Scalar8x32 b)
808807
MuladdFast(a.b7, b.b7, ref c0, ref c1);
809808
ExtractFast(ref l[14], ref c0, ref c1, ref c2);
810809
Debug.Assert(c1 == 0);
811-
l[15] = (uint)c0;
810+
l[15] = c0;
812811
}
813812

814813
private static unsafe Scalar8x32 Reduce512(uint* l)
@@ -819,7 +818,7 @@ private static unsafe Scalar8x32 Reduce512(uint* l)
819818
uint p0 = 0, p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0, p8 = 0;
820819

821820
// 96 bit accumulator
822-
ulong c0, c1, c2;
821+
uint c0, c1, c2;
823822

824823
// Reduce 512 bits into 385
825824
// m[0..12] = l[0..7] + n[0..7] * NC
@@ -884,7 +883,7 @@ private static unsafe Scalar8x32 Reduce512(uint* l)
884883
SumaddFast(n7, ref c0, ref c1, ref c2);
885884
ExtractFast(ref m11, ref c0, ref c1, ref c2);
886885
Debug.Assert(c0 <= 1);
887-
m12 = (uint)c0;
886+
m12 = c0;
888887

889888
// Reduce 385 bits into 258
890889
// p[0..8] = m[0..7] + m[8..12] * NC
@@ -928,7 +927,7 @@ private static unsafe Scalar8x32 Reduce512(uint* l)
928927
MuladdFast(m12, NC3, ref c0, ref c1);
929928
SumaddFast(m11, ref c0, ref c1, ref c2);
930929
ExtractFast(ref p7, ref c0, ref c1, ref c2);
931-
p8 = (uint)c0 + m12;
930+
p8 = c0 + m12;
932931
Debug.Assert(p8 <= 2);
933932

934933
// Reduce 258 bits into 256

0 commit comments

Comments
 (0)