From f693a6cd892ea8a4747d5cff60ad2051458ad5f5 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 1 Dec 2022 00:58:27 +0700 Subject: Tnaf perf. opts. --- crypto/src/math/raw/Nat.cs | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'crypto/src/math/raw') diff --git a/crypto/src/math/raw/Nat.cs b/crypto/src/math/raw/Nat.cs index 4600f247f..fa76f305e 100644 --- a/crypto/src/math/raw/Nat.cs +++ b/crypto/src/math/raw/Nat.cs @@ -14,7 +14,7 @@ namespace Org.BouncyCastle.Math.Raw public static uint Add(int len, uint[] x, uint[] y, uint[] z) { - ulong c = 0; + ulong c = 0UL; for (int i = 0; i < len; ++i) { c += (ulong)x[i] + y[i]; @@ -27,7 +27,7 @@ namespace Org.BouncyCastle.Math.Raw #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER public static uint Add(int len, ReadOnlySpan x, ReadOnlySpan y, Span z) { - ulong c = 0; + ulong c = 0UL; for (int i = 0; i < len; ++i) { c += (ulong)x[i] + y[i]; @@ -1398,6 +1398,32 @@ namespace Org.BouncyCastle.Math.Raw } #endif + public static int Negate(int len, uint[] x, uint[] z) + { + long c = 0L; + for (int i = 0; i < len; ++i) + { + c -= x[i]; + z[i] = (uint)c; + c >>= 32; + } + return (int)c; + } + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static int Negate(int len, ReadOnlySpan x, Span z) + { + long c = 0L; + for (int i = 0; i < len; ++i) + { + c -= x[i]; + z[i] = (uint)c; + c >>= 32; + } + return (int)c; + } +#endif + public static uint ShiftDownBit(int len, uint[] z, uint c) { int i = len; @@ -2610,6 +2636,25 @@ namespace Org.BouncyCastle.Math.Raw } #endif +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static int SubInt32From(int len, int x, Span z) + { + long c = (long)z[0] - x; + z[0] = (uint)c; + c >>= 32; + + int i = 1; + while (c != 0L && i < len) + { + c += z[i]; + z[i++] = (uint)c; + c >>= 32; + } + + return (int)c; + } +#endif + public static int SubWordAt(int len, uint x, uint[] z, int zPos) { Debug.Assert(zPos <= (len - 1)); -- cgit 1.5.1