summary refs log tree commit diff
path: root/crypto/src/math/raw/Nat.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-13 18:15:26 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-13 18:15:26 +0700
commit7703a5bf539150be76f2cf330080c5ef0c597707 (patch)
treea1279f6bc2ffd9af78fa74ea9e35fe82582ce098 /crypto/src/math/raw/Nat.cs
parentX448 perf. opts. (diff)
downloadBouncyCastle.NET-ed25519-7703a5bf539150be76f2cf330080c5ef0c597707.tar.xz
BigInteger direct conversion to Span<uint>
Diffstat (limited to 'crypto/src/math/raw/Nat.cs')
-rw-r--r--crypto/src/math/raw/Nat.cs31
1 files changed, 16 insertions, 15 deletions
diff --git a/crypto/src/math/raw/Nat.cs b/crypto/src/math/raw/Nat.cs
index b3b670954..4600f247f 100644
--- a/crypto/src/math/raw/Nat.cs
+++ b/crypto/src/math/raw/Nat.cs
@@ -818,13 +818,17 @@ namespace Org.BouncyCastle.Math.Raw
 
         public static uint[] FromBigInteger(int bits, BigInteger x)
         {
-            int len = GetLengthForBits(bits);
-
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
 
+            int len = GetLengthForBits(bits);
             uint[] z = Create(len);
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            int xLen = x.GetLengthofUInt32ArrayUnsigned();
+            x.ToUInt32ArrayLittleEndianUnsigned(z.AsSpan(0, xLen));
+            //z.AsSpan(xLen).Fill(0x00);
+#else
             // NOTE: Use a fixed number of loop iterations
             z[0] = (uint)x.IntValue;
             for (int i = 1; i < len; ++i)
@@ -832,36 +836,33 @@ namespace Org.BouncyCastle.Math.Raw
                 x = x.ShiftRight(32);
                 z[i] = (uint)x.IntValue;
             }
+#endif
+
             return z;
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public static void FromBigInteger(int bits, BigInteger x, Span<uint> z)
         {
-            int len = GetLengthForBits(bits);
-
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
+
+            int len = GetLengthForBits(bits);
             if (z.Length < len)
                 throw new ArgumentException();
 
-            // NOTE: Use a fixed number of loop iterations
-            z[0] = (uint)x.IntValue;
-            for (int i = 1; i < len; ++i)
-            {
-                x = x.ShiftRight(32);
-                z[i] = (uint)x.IntValue;
-            }
+            int xLen = x.GetLengthofUInt32ArrayUnsigned();
+            x.ToUInt32ArrayLittleEndianUnsigned(z[..xLen]);
+            z[xLen..].Fill(0x00);
         }
 #endif
 
         public static ulong[] FromBigInteger64(int bits, BigInteger x)
         {
-            int len = GetLengthForBits64(bits);
-
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
 
+            int len = GetLengthForBits64(bits);
             ulong[] z = Create64(len);
 
             // NOTE: Use a fixed number of loop iterations
@@ -877,10 +878,10 @@ namespace Org.BouncyCastle.Math.Raw
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public static void FromBigInteger64(int bits, BigInteger x, Span<ulong> z)
         {
-            int len = GetLengthForBits64(bits);
-
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
+
+            int len = GetLengthForBits64(bits);
             if (z.Length < len)
                 throw new ArgumentException();