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>2020-09-11 11:54:39 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-09-11 11:54:39 +0700
commit5e4ffd78e073bf15b255c1465fa211a13105b65c (patch)
tree7d3d23488573a45cb31dd3477ee9995b49523f2c /crypto/src/math/raw/Nat.cs
parentFurther Divsteps30 improvement (diff)
downloadBouncyCastle.NET-ed25519-5e4ffd78e073bf15b255c1465fa211a13105b65c.tar.xz
Fixed loop count for BigInteger conversion
- remove length-specific variants of FromBigInteger
Diffstat (limited to 'crypto/src/math/raw/Nat.cs')
-rw-r--r--crypto/src/math/raw/Nat.cs22
1 files changed, 16 insertions, 6 deletions
diff --git a/crypto/src/math/raw/Nat.cs b/crypto/src/math/raw/Nat.cs
index 9f2412580..d67de0a5c 100644
--- a/crypto/src/math/raw/Nat.cs
+++ b/crypto/src/math/raw/Nat.cs
@@ -515,32 +515,42 @@ namespace Org.BouncyCastle.Math.Raw
 
         public static uint[] FromBigInteger(int bits, BigInteger x)
         {
+            if (bits < 1)
+                throw new ArgumentException();
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
 
             int len = (bits + 31) >> 5;
+            Debug.Assert(len > 0);
             uint[] z = Create(len);
-            int i = 0;
-            while (x.SignValue != 0)
+
+            // NOTE: Use a fixed number of loop iterations
+            z[0] = (uint)x.IntValue;
+            for (int i = 1; i < len; ++i)
             {
-                z[i++] = (uint)x.IntValue;
                 x = x.ShiftRight(32);
+                z[i] = (uint)x.IntValue;
             }
             return z;
         }
 
         public static ulong[] FromBigInteger64(int bits, BigInteger x)
         {
+            if (bits < 1)
+                throw new ArgumentException();
             if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
 
             int len = (bits + 63) >> 6;
+            Debug.Assert(len > 0);
             ulong[] z = Create64(len);
-            int i = 0;
-            while (x.SignValue != 0)
+
+            // NOTE: Use a fixed number of loop iterations
+            z[0] = (ulong)x.LongValue;
+            for (int i = 1; i < len; ++i)
             {
-                z[i++] = (ulong)x.LongValue;
                 x = x.ShiftRight(64);
+                z[i] = (ulong)x.LongValue;
             }
             return z;
         }