summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-01-09 14:04:06 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-01-09 14:04:06 +0700
commitda32e5f1e8d71b8bc1d573eb5ab41a6da26f49b3 (patch)
treedeaf6fadee14b1859696388229363287eacfbefa
parentAvoid intermediate allocations (diff)
downloadBouncyCastle.NET-ed25519-da32e5f1e8d71b8bc1d573eb5ab41a6da26f49b3.tar.xz
Add FromUnsignedByteArray methods
-rw-r--r--crypto/src/util/BigIntegers.cs40
1 files changed, 21 insertions, 19 deletions
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index 668d92246..9c5477e6a 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -139,11 +139,7 @@ namespace Org.BouncyCastle.Utilities
         * @param random the source of randomness
         * @return a random BigInteger value in the range [min,max]
         */
-        public static BigInteger CreateRandomInRange(
-            BigInteger		min,
-            BigInteger		max,
-            // TODO Should have been just Random class
-            SecureRandom	random)
+        public static BigInteger CreateRandomInRange(BigInteger min, BigInteger max, SecureRandom random)
         {
             int cmp = min.CompareTo(max);
             if (cmp >= 0)
@@ -155,23 +151,39 @@ namespace Org.BouncyCastle.Utilities
             }
 
             if (min.BitLength > max.BitLength / 2)
-            {
                 return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
-            }
 
             for (int i = 0; i < MaxIterations; ++i)
             {
                 BigInteger x = new BigInteger(max.BitLength, random);
                 if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0)
-                {
                     return x;
-                }
             }
 
             // fall back to a faster (restricted) method
             return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
         }
 
+        public static BigInteger FromUnsignedByteArray(byte[] buf)
+        {
+            return new BigInteger(1, buf);
+        }
+
+        public static BigInteger FromUnsignedByteArray(byte[] buf, int off, int length)
+        {
+            return new BigInteger(1, buf, off, length);
+        }
+
+        public static int GetByteLength(BigInteger n)
+        {
+            return n.GetLengthofByteArray();
+        }
+
+        public static int GetUnsignedByteLength(BigInteger n)
+        {
+            return n.GetLengthofByteArrayUnsigned();
+        }
+
         public static BigInteger ModOddInverse(BigInteger M, BigInteger X)
         {
             if (!M.TestBit(0))
@@ -253,15 +265,5 @@ namespace Org.BouncyCastle.Utilities
                 return Nat.ToBigInteger(len, z);
             }
         }
-
-        public static int GetByteLength(BigInteger n)
-        {
-            return n.GetLengthofByteArray();
-        }
-
-        public static int GetUnsignedByteLength(BigInteger n)
-        {
-            return n.GetLengthofByteArrayUnsigned();
-        }
     }
 }