summary refs log tree commit diff
path: root/crypto/src/bcpg/MPInteger.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-06 12:19:49 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-06 12:19:49 +0700
commit2d28fafa7fe1becdada43f939b5121946468052c (patch)
treeab0b57beefd9976b7d3091fbefc0fab4635f3d04 /crypto/src/bcpg/MPInteger.cs
parentRefactor RSACoreEngine.ConvertOutput (diff)
downloadBouncyCastle.NET-ed25519-2d28fafa7fe1becdada43f939b5121946468052c.tar.xz
Refactor stackalloc usage
Diffstat (limited to 'crypto/src/bcpg/MPInteger.cs')
-rw-r--r--crypto/src/bcpg/MPInteger.cs22
1 files changed, 11 insertions, 11 deletions
diff --git a/crypto/src/bcpg/MPInteger.cs b/crypto/src/bcpg/MPInteger.cs
index 2f564b00c..eb59be6e5 100644
--- a/crypto/src/bcpg/MPInteger.cs
+++ b/crypto/src/bcpg/MPInteger.cs
@@ -20,16 +20,13 @@ namespace Org.BouncyCastle.Bcpg
             int lengthInBytes = (lengthInBits + 7) / 8;
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-            if (lengthInBytes <= 512)
-            {
-                Span<byte> span = stackalloc byte[lengthInBytes];
-                bcpgIn.ReadFully(span);
-                m_val = new BigInteger(1, span);
-                return;
-            }
+            Span<byte> bytes = lengthInBytes <= 512
+                ? stackalloc byte[lengthInBytes]
+                : new byte[lengthInBytes];
+#else
+            byte[] bytes = new byte[lengthInBytes];
 #endif
 
-            byte[] bytes = new byte[lengthInBytes];
             bcpgIn.ReadFully(bytes);
             m_val = new BigInteger(1, bytes);
         }
@@ -55,12 +52,15 @@ namespace Org.BouncyCastle.Bcpg
         internal static BigInteger ToMpiBigInteger(ECPoint point)
         {
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-            Span<byte> encoding = stackalloc byte[point.GetEncodedLength(false)];
+            int encodedLength = point.GetEncodedLength(false);
+            Span<byte> encoding = encodedLength <= 512
+                ? stackalloc byte[encodedLength]
+                : new byte[encodedLength];
             point.EncodeTo(false, encoding);
-            return new BigInteger(1, encoding);
 #else
-            return new BigInteger(1, point.GetEncoded(false));
+            byte[] encoding = point.GetEncoded(false);
 #endif
+            return new BigInteger(1, encoding);
         }
     }
 }