summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2019-07-31 17:42:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2019-07-31 17:42:12 +0700
commite7e89cce762e32917c1eef745b05da636ed03bbb (patch)
tree063c1ad6b42b21b0c10afc8ed173a4675f286e13
parentFix CMP tests failing to set body (diff)
downloadBouncyCastle.NET-ed25519-e7e89cce762e32917c1eef745b05da636ed03bbb.tar.xz
Accept only properly-sized BigInteger (no auto-reduction)
-rw-r--r--crypto/src/math/ec/custom/sec/SecT113Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT131Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT163Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT193Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT233Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT239Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT283Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT409Field.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecT571Field.cs4
-rw-r--r--crypto/src/math/raw/Nat.cs16
10 files changed, 25 insertions, 27 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecT113Field.cs b/crypto/src/math/ec/custom/sec/SecT113Field.cs
index 49773b66d..1c4576062 100644
--- a/crypto/src/math/ec/custom/sec/SecT113Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT113Field.cs
@@ -32,9 +32,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat128.FromBigInteger64(x);
-            Reduce15(z, 0);
-            return z;
+            return Nat.FromBigInteger64(113, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT131Field.cs b/crypto/src/math/ec/custom/sec/SecT131Field.cs
index 1b6697afe..b59b0181b 100644
--- a/crypto/src/math/ec/custom/sec/SecT131Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT131Field.cs
@@ -37,9 +37,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat192.FromBigInteger64(x);
-            Reduce61(z, 0);
-            return z;
+            return Nat.FromBigInteger64(131, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT163Field.cs b/crypto/src/math/ec/custom/sec/SecT163Field.cs
index b1e9aa725..e76e57b43 100644
--- a/crypto/src/math/ec/custom/sec/SecT163Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT163Field.cs
@@ -38,9 +38,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat192.FromBigInteger64(x);
-            Reduce29(z, 0);
-            return z;
+            return Nat.FromBigInteger64(163, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT193Field.cs b/crypto/src/math/ec/custom/sec/SecT193Field.cs
index 41acb4f94..a186da7b6 100644
--- a/crypto/src/math/ec/custom/sec/SecT193Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT193Field.cs
@@ -39,9 +39,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat256.FromBigInteger64(x);
-            Reduce63(z, 0);
-            return z;
+            return Nat.FromBigInteger64(193, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT233Field.cs b/crypto/src/math/ec/custom/sec/SecT233Field.cs
index 870dade50..bd493c586 100644
--- a/crypto/src/math/ec/custom/sec/SecT233Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT233Field.cs
@@ -40,9 +40,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat256.FromBigInteger64(x);
-            Reduce23(z, 0);
-            return z;
+            return Nat.FromBigInteger64(233, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT239Field.cs b/crypto/src/math/ec/custom/sec/SecT239Field.cs
index 2e6ed2ad6..61b2ed6b7 100644
--- a/crypto/src/math/ec/custom/sec/SecT239Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT239Field.cs
@@ -40,9 +40,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat256.FromBigInteger64(x);
-            Reduce17(z, 0);
-            return z;
+            return Nat.FromBigInteger64(239, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT283Field.cs b/crypto/src/math/ec/custom/sec/SecT283Field.cs
index 22b7eaaab..64fbc966d 100644
--- a/crypto/src/math/ec/custom/sec/SecT283Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT283Field.cs
@@ -45,9 +45,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat320.FromBigInteger64(x);
-            Reduce37(z, 0);
-            return z;
+            return Nat.FromBigInteger64(283, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT409Field.cs b/crypto/src/math/ec/custom/sec/SecT409Field.cs
index 861b77aa1..f9962de5b 100644
--- a/crypto/src/math/ec/custom/sec/SecT409Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT409Field.cs
@@ -42,9 +42,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat448.FromBigInteger64(x);
-            Reduce39(z, 0);
-            return z;
+            return Nat.FromBigInteger64(409, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecT571Field.cs b/crypto/src/math/ec/custom/sec/SecT571Field.cs
index 98f4f7fc2..9596aa5af 100644
--- a/crypto/src/math/ec/custom/sec/SecT571Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT571Field.cs
@@ -57,9 +57,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static ulong[] FromBigInteger(BigInteger x)
         {
-            ulong[] z = Nat576.FromBigInteger64(x);
-            Reduce5(z, 0);
-            return z;
+            return Nat.FromBigInteger64(571, x);
         }
 
         public static void Invert(ulong[] x, ulong[] z)
diff --git a/crypto/src/math/raw/Nat.cs b/crypto/src/math/raw/Nat.cs
index f9e4e6714..5cf5fe218 100644
--- a/crypto/src/math/raw/Nat.cs
+++ b/crypto/src/math/raw/Nat.cs
@@ -384,6 +384,22 @@ namespace Org.BouncyCastle.Math.Raw
             return z;
         }
 
+        public static ulong[] FromBigInteger64(int bits, BigInteger x)
+        {
+            if (x.SignValue < 0 || x.BitLength > bits)
+                throw new ArgumentException();
+
+            int len = (bits + 63) >> 6;
+            ulong[] z = Create64(len);
+            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)