summary refs log tree commit diff
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
parentFurther Divsteps30 improvement (diff)
downloadBouncyCastle.NET-ed25519-5e4ffd78e073bf15b255c1465fa211a13105b65c.tar.xz
Fixed loop count for BigInteger conversion
- remove length-specific variants of FromBigInteger
-rw-r--r--crypto/src/math/ec/custom/djb/Curve25519Field.cs2
-rw-r--r--crypto/src/math/ec/custom/gm/SM2P256V1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP128R1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP160R1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP160R2Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192R1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224R1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Field.cs2
-rw-r--r--crypto/src/math/raw/Nat.cs22
-rw-r--r--crypto/src/math/raw/Nat128.cs30
-rw-r--r--crypto/src/math/raw/Nat160.cs15
-rw-r--r--crypto/src/math/raw/Nat192.cs30
-rw-r--r--crypto/src/math/raw/Nat224.cs15
-rw-r--r--crypto/src/math/raw/Nat256.cs30
-rw-r--r--crypto/src/math/raw/Nat320.cs15
-rw-r--r--crypto/src/math/raw/Nat448.cs15
-rw-r--r--crypto/src/math/raw/Nat576.cs15
20 files changed, 27 insertions, 182 deletions
diff --git a/crypto/src/math/ec/custom/djb/Curve25519Field.cs b/crypto/src/math/ec/custom/djb/Curve25519Field.cs
index 0006acd94..31416249d 100644
--- a/crypto/src/math/ec/custom/djb/Curve25519Field.cs
+++ b/crypto/src/math/ec/custom/djb/Curve25519Field.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Djb
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat256.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(256, x);
             while (Nat256.Gte(z, P))
             {
                 Nat256.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs b/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
index 38743189a..6fbe849a8 100644
--- a/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
+++ b/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.GM
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat256.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] >= P7 && Nat256.Gte(z, P))
             {
                 Nat256.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP128R1Field.cs b/crypto/src/math/ec/custom/sec/SecP128R1Field.cs
index 03a07f79b..838f4dfec 100644
--- a/crypto/src/math/ec/custom/sec/SecP128R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP128R1Field.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat128.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(128, x);
             if (z[3] >= P3 && Nat128.Gte(z, P))
             {
                 Nat128.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP160R1Field.cs b/crypto/src/math/ec/custom/sec/SecP160R1Field.cs
index 31c957301..f4b1e2a23 100644
--- a/crypto/src/math/ec/custom/sec/SecP160R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP160R1Field.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat160.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(160, x);
             if (z[4] == P4 && Nat160.Gte(z, P))
             {
                 Nat160.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP160R2Field.cs b/crypto/src/math/ec/custom/sec/SecP160R2Field.cs
index 55f02e438..9ad594376 100644
--- a/crypto/src/math/ec/custom/sec/SecP160R2Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP160R2Field.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat160.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(160, x);
             if (z[4] == P4 && Nat160.Gte(z, P))
             {
                 Nat160.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
index 23bd732bd..46b7c4ebd 100644
--- a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat192.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(192, x);
             if (z[5] == P5 && Nat192.Gte(z, P))
             {
                 Nat192.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
index a4fb4bb76..10e204677 100644
--- a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat192.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(192, x);
             if (z[5] == P5 && Nat192.Gte(z, P))
             {
                 Nat192.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP224K1Field.cs b/crypto/src/math/ec/custom/sec/SecP224K1Field.cs
index 5d4237708..36e5364a4 100644
--- a/crypto/src/math/ec/custom/sec/SecP224K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224K1Field.cs
@@ -53,7 +53,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat224.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(224, x);
             if (z[6] == P6 && Nat224.Gte(z, P))
             {
                 Nat224.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
index dde291d5e..242f8f352 100644
--- a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat224.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(224, x);
             if (z[6] == P6 && Nat224.Gte(z, P))
             {
                 Nat224.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
index acdb1f362..9a8915c01 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -53,7 +53,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat256.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] == P7 && Nat256.Gte(z, P))
             {
                 Nat256.SubFrom(P, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index 668efc895..a1079f39c 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static uint[] FromBigInteger(BigInteger x)
         {
-            uint[] z = Nat256.FromBigInteger(x);
+            uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] == P7 && Nat256.Gte(z, P))
             {
                 Nat256.SubFrom(P, z);
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;
         }
diff --git a/crypto/src/math/raw/Nat128.cs b/crypto/src/math/raw/Nat128.cs
index 7617a9ee9..d336b320a 100644
--- a/crypto/src/math/raw/Nat128.cs
+++ b/crypto/src/math/raw/Nat128.cs
@@ -185,36 +185,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static uint[] FromBigInteger(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 128)
-                throw new ArgumentException();
-
-            uint[] z = Create();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (uint)x.IntValue;
-                x = x.ShiftRight(32);
-            }
-            return z;
-        }
-
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 128)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static uint GetBit(uint[] x, int bit)
         {
             if (bit == 0)
diff --git a/crypto/src/math/raw/Nat160.cs b/crypto/src/math/raw/Nat160.cs
index f5514d7b4..f862700b1 100644
--- a/crypto/src/math/raw/Nat160.cs
+++ b/crypto/src/math/raw/Nat160.cs
@@ -172,21 +172,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static uint[] FromBigInteger(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 160)
-                throw new ArgumentException();
-
-            uint[] z = Create();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (uint)x.IntValue;
-                x = x.ShiftRight(32);
-            }
-            return z;
-        }
-
         public static uint GetBit(uint[] x, int bit)
         {
             if (bit == 0)
diff --git a/crypto/src/math/raw/Nat192.cs b/crypto/src/math/raw/Nat192.cs
index 1311dee04..752290747 100644
--- a/crypto/src/math/raw/Nat192.cs
+++ b/crypto/src/math/raw/Nat192.cs
@@ -225,36 +225,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static uint[] FromBigInteger(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 192)
-                throw new ArgumentException();
-
-            uint[] z = Create();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (uint)x.IntValue;
-                x = x.ShiftRight(32);
-            }
-            return z;
-        }
-
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 192)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static uint GetBit(uint[] x, int bit)
         {
             if (bit == 0)
diff --git a/crypto/src/math/raw/Nat224.cs b/crypto/src/math/raw/Nat224.cs
index 565abcb9c..1aabd3f17 100644
--- a/crypto/src/math/raw/Nat224.cs
+++ b/crypto/src/math/raw/Nat224.cs
@@ -261,21 +261,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static uint[] FromBigInteger(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 224)
-                throw new ArgumentException();
-
-            uint[] z = Create();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (uint)x.IntValue;
-                x = x.ShiftRight(32);
-            }
-            return z;
-        }
-
         public static uint GetBit(uint[] x, int bit)
         {
             if (bit == 0)
diff --git a/crypto/src/math/raw/Nat256.cs b/crypto/src/math/raw/Nat256.cs
index 5c473c405..710060bee 100644
--- a/crypto/src/math/raw/Nat256.cs
+++ b/crypto/src/math/raw/Nat256.cs
@@ -323,36 +323,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static uint[] FromBigInteger(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 256)
-                throw new ArgumentException();
-
-            uint[] z = Create();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (uint)x.IntValue;
-                x = x.ShiftRight(32);
-            }
-            return z;
-        }
-
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 256)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static uint GetBit(uint[] x, int bit)
         {
             if (bit == 0)
diff --git a/crypto/src/math/raw/Nat320.cs b/crypto/src/math/raw/Nat320.cs
index 0ad677db4..0b250aa77 100644
--- a/crypto/src/math/raw/Nat320.cs
+++ b/crypto/src/math/raw/Nat320.cs
@@ -47,21 +47,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 320)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static bool IsOne64(ulong[] x)
         {
             if (x[0] != 1UL)
diff --git a/crypto/src/math/raw/Nat448.cs b/crypto/src/math/raw/Nat448.cs
index b0774b37a..8c7f3244d 100644
--- a/crypto/src/math/raw/Nat448.cs
+++ b/crypto/src/math/raw/Nat448.cs
@@ -51,21 +51,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 448)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static bool IsOne64(ulong[] x)
         {
             if (x[0] != 1UL)
diff --git a/crypto/src/math/raw/Nat576.cs b/crypto/src/math/raw/Nat576.cs
index 14279b61a..174d52bcf 100644
--- a/crypto/src/math/raw/Nat576.cs
+++ b/crypto/src/math/raw/Nat576.cs
@@ -55,21 +55,6 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
-        public static ulong[] FromBigInteger64(BigInteger x)
-        {
-            if (x.SignValue < 0 || x.BitLength > 576)
-                throw new ArgumentException();
-
-            ulong[] z = Create64();
-            int i = 0;
-            while (x.SignValue != 0)
-            {
-                z[i++] = (ulong)x.LongValue;
-                x = x.ShiftRight(64);
-            }
-            return z;
-        }
-
         public static bool IsOne64(ulong[] x)
         {
             if (x[0] != 1UL)