summary refs log tree commit diff
path: root/crypto/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-07 20:05:14 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-07 20:05:14 +0700
commit782382c6f665a022effa71a3f7738cf1e09d9866 (patch)
tree66311deab21b35bc671561cbb419a0122c1b14f5 /crypto/src/util
parentFix exception type (diff)
downloadBouncyCastle.NET-ed25519-782382c6f665a022effa71a3f7738cf1e09d9866.tar.xz
Span usage in Math.Raw
Diffstat (limited to 'crypto/src/util')
-rw-r--r--crypto/src/util/BigIntegers.cs64
1 files changed, 50 insertions, 14 deletions
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index 44c9e32a7..93dc8e8eb 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -168,13 +168,31 @@ namespace Org.BouncyCastle.Utilities
             }
 
             int bits = M.BitLength;
-            uint[] m = Nat.FromBigInteger(bits, M);
-            uint[] x = Nat.FromBigInteger(bits, X);
-            int len = m.Length;
-            uint[] z = Nat.Create(len);
-            if (0 == Mod.ModOddInverse(m, x, z))
-                throw new ArithmeticException("BigInteger not invertible");
-            return Nat.ToBigInteger(len, z);
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            if (bits <= 2048)
+            {
+                int len = Nat.GetLengthForBits(bits);
+                Span<uint> m = stackalloc uint[len];
+                Span<uint> x = stackalloc uint[len];
+                Span<uint> z = stackalloc uint[len];
+                Nat.FromBigInteger(bits, M, m);
+                Nat.FromBigInteger(bits, X, x);
+                if (0 == Mod.ModOddInverse(m, x, z))
+                    throw new ArithmeticException("BigInteger not invertible");
+                return Nat.ToBigInteger(len, z);
+            }
+            else
+#endif
+            {
+                uint[] m = Nat.FromBigInteger(bits, M);
+                uint[] x = Nat.FromBigInteger(bits, X);
+                int len = m.Length;
+                uint[] z = Nat.Create(len);
+                if (0 == Mod.ModOddInverse(m, x, z))
+                    throw new ArithmeticException("BigInteger not invertible");
+                return Nat.ToBigInteger(len, z);
+            }
         }
 
         public static BigInteger ModOddInverseVar(BigInteger M, BigInteger X)
@@ -193,13 +211,31 @@ namespace Org.BouncyCastle.Utilities
                 return One;
 
             int bits = M.BitLength;
-            uint[] m = Nat.FromBigInteger(bits, M);
-            uint[] x = Nat.FromBigInteger(bits, X);
-            int len = m.Length;
-            uint[] z = Nat.Create(len);
-            if (!Mod.ModOddInverseVar(m, x, z))
-                throw new ArithmeticException("BigInteger not invertible");
-            return Nat.ToBigInteger(len, z);
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            if (bits <= 2048)
+            {
+                int len = Nat.GetLengthForBits(bits);
+                Span<uint> m = stackalloc uint[len];
+                Span<uint> x = stackalloc uint[len];
+                Span<uint> z = stackalloc uint[len];
+                Nat.FromBigInteger(bits, M, m);
+                Nat.FromBigInteger(bits, X, x);
+                if (!Mod.ModOddInverseVar(m, x, z))
+                    throw new ArithmeticException("BigInteger not invertible");
+                return Nat.ToBigInteger(len, z);
+            }
+            else
+#endif
+            {
+                uint[] m = Nat.FromBigInteger(bits, M);
+                uint[] x = Nat.FromBigInteger(bits, X);
+                int len = m.Length;
+                uint[] z = Nat.Create(len);
+                if (!Mod.ModOddInverseVar(m, x, z))
+                    throw new ArithmeticException("BigInteger not invertible");
+                return Nat.ToBigInteger(len, z);
+            }
         }
 
         public static int GetByteLength(BigInteger n)