From 782382c6f665a022effa71a3f7738cf1e09d9866 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 7 Oct 2022 20:05:14 +0700 Subject: Span usage in Math.Raw --- crypto/src/util/BigIntegers.cs | 64 +++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 14 deletions(-) (limited to 'crypto/src/util/BigIntegers.cs') 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 m = stackalloc uint[len]; + Span x = stackalloc uint[len]; + Span 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 m = stackalloc uint[len]; + Span x = stackalloc uint[len]; + Span 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) -- cgit 1.4.1