summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-03-04 11:12:43 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-03-04 11:12:43 +0700
commitecf5397a6edbe46248ef74a782e7443e4f5341c5 (patch)
treeadd85103823cd9edef723854e8215f396ca6123e
parentRefactoring in the Nat* classes and some new method variations (diff)
downloadBouncyCastle.NET-ed25519-ecf5397a6edbe46248ef74a782e7443e4f5341c5.tar.xz
Just use shift methods from Nat class evverywhere
-rw-r--r--crypto/src/math/ec/custom/sec/Curve25519Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/Nat192.cs86
-rw-r--r--crypto/src/math/ec/custom/sec/Nat224.cs86
-rw-r--r--crypto/src/math/ec/custom/sec/Nat256.cs86
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192K1Field.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192R1Field.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224K1Field.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224R1Field.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Field.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Field.cs6
10 files changed, 25 insertions, 271 deletions
diff --git a/crypto/src/math/ec/custom/sec/Curve25519Field.cs b/crypto/src/math/ec/custom/sec/Curve25519Field.cs
index a11659a60..c272cbc38 100644
--- a/crypto/src/math/ec/custom/sec/Curve25519Field.cs
+++ b/crypto/src/math/ec/custom/sec/Curve25519Field.cs
@@ -147,7 +147,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            Nat256.ShiftUpBit(x, 0, z);
+            Nat.ShiftUpBit(8, x, 0, z);
             if (Nat256.Gte(z, P))
             {
                 Nat256.AddWord(PInv, z, 0);
diff --git a/crypto/src/math/ec/custom/sec/Nat192.cs b/crypto/src/math/ec/custom/sec/Nat192.cs
index c7c09bbfc..1a6e557f8 100644
--- a/crypto/src/math/ec/custom/sec/Nat192.cs
+++ b/crypto/src/math/ec/custom/sec/Nat192.cs
@@ -704,88 +704,6 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return (uint)c;
         }
 
-        public static uint ShiftDownBit(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBit(uint[] x, uint c, uint[] z)
-        {
-            int i = 6;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                z[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBits(uint[] x, int xLen, int bits, uint c)
-        {
-            Debug.Assert(bits > 0 && bits < 32);
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> bits) | (c << -bits);
-                c = next;
-            }
-            return c << -bits;
-        }
-
-        public static uint ShiftDownWord(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = c;
-                c = next;
-            }
-            return c;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[i];
-                x[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xOff, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[xOff + i];
-                x[xOff + i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, uint c, uint[] z)
-        {
-            for (int i = 0; i < 6; ++i)
-            {
-                uint next = x[i];
-                z[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
         public static void Square(uint[] x, uint[] zz)
         {
             ulong x_0 = x[0];
@@ -879,7 +797,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[10] = (uint)zz_10;
             zz[11] += (uint)(zz_10 >> 32);
 
-            ShiftUpBit(zz, 12, (uint)x_0 << 31);
+            Nat.ShiftUpBit(12, zz, (uint)x_0 << 31);
         }
 
         public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
@@ -975,7 +893,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[zzOff + 10] = (uint)zz_10;
             zz[zzOff + 11] += (uint)(zz_10 >> 32);
 
-            ShiftUpBit(zz, zzOff, 12, (uint)x_0 << 31);
+            Nat.ShiftUpBit(12, zz, zzOff, (uint)x_0 << 31);
         }
 
         public static int Sub(uint[] x, uint[] y, uint[] z)
diff --git a/crypto/src/math/ec/custom/sec/Nat224.cs b/crypto/src/math/ec/custom/sec/Nat224.cs
index 9ea85cba2..7027f1162 100644
--- a/crypto/src/math/ec/custom/sec/Nat224.cs
+++ b/crypto/src/math/ec/custom/sec/Nat224.cs
@@ -856,88 +856,6 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return (uint)c;
         }
 
-        public static uint ShiftDownBit(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBit(uint[] x, uint c, uint[] z)
-        {
-            int i = 7;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                z[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBits(uint[] x, int xLen, int bits, uint c)
-        {
-            Debug.Assert(bits > 0 && bits < 32);
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> bits) | (c << -bits);
-                c = next;
-            }
-            return c << -bits;
-        }
-
-        public static uint ShiftDownWord(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = c;
-                c = next;
-            }
-            return c;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[i];
-                x[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xOff, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[xOff + i];
-                x[xOff + i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, uint c, uint[] z)
-        {
-            for (int i = 0; i < 7; ++i)
-            {
-                uint next = x[i];
-                z[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
         public static void Square(uint[] x, uint[] zz)
         {
             ulong x_0 = x[0];
@@ -1050,7 +968,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[12] = (uint)zz_12;
             zz[13] += (uint)(zz_12 >> 32);
 
-            ShiftUpBit(zz, 14, (uint)x_0 << 31);
+            Nat.ShiftUpBit(14, zz, (uint)x_0 << 31);
         }
 
         public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
@@ -1165,7 +1083,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[zzOff + 12] = (uint)zz_12;
             zz[zzOff + 13] += (uint)(zz_12 >> 32);
 
-            ShiftUpBit(zz, zzOff, 16, (uint)x_0 << 31);
+            Nat.ShiftUpBit(14, zz, zzOff, (uint)x_0 << 31);
         }
 
         public static int Sub(uint[] x, uint[] y, uint[] z)
diff --git a/crypto/src/math/ec/custom/sec/Nat256.cs b/crypto/src/math/ec/custom/sec/Nat256.cs
index 335c181fa..f4599b988 100644
--- a/crypto/src/math/ec/custom/sec/Nat256.cs
+++ b/crypto/src/math/ec/custom/sec/Nat256.cs
@@ -923,88 +923,6 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return (uint)c;
         }
 
-        public static uint ShiftDownBit(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBit(uint[] x, uint c, uint[] z)
-        {
-            int i = 8;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                z[i] = (next >> 1) | (c << 31);
-                c = next;
-            }
-            return c << 31;
-        }
-
-        public static uint ShiftDownBits(uint[] x, int xLen, int bits, uint c)
-        {
-            Debug.Assert(bits > 0 && bits < 32);
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = (next >> bits) | (c << -bits);
-                c = next;
-            }
-            return c << -bits;
-        }
-
-        public static uint ShiftDownWord(uint[] x, int xLen, uint c)
-        {
-            int i = xLen;
-            while (--i >= 0)
-            {
-                uint next = x[i];
-                x[i] = c;
-                c = next;
-            }
-            return c;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[i];
-                x[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, int xOff, int xLen, uint c)
-        {
-            for (int i = 0; i < xLen; ++i)
-            {
-                uint next = x[xOff + i];
-                x[xOff + i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
-        public static uint ShiftUpBit(uint[] x, uint c, uint[] z)
-        {
-            for (int i = 0; i < 8; ++i)
-            {
-                uint next = x[i];
-                z[i] = (next << 1) | (c >> 31);
-                c = next;
-            }
-            return c >> 31;
-        }
-
         public static void Square(uint[] x, uint[] zz)
         {
             ulong x_0 = x[0];
@@ -1138,7 +1056,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[14] = (uint)zz_14;
             zz[15] += (uint)(zz_14 >> 32);
 
-            ShiftUpBit(zz, 16, (uint)x_0 << 31);
+            Nat.ShiftUpBit(16, zz, (uint)x_0 << 31);
         }
 
         public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
@@ -1274,7 +1192,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             zz[zzOff + 14] = (uint)zz_14;
             zz[zzOff + 15] += (uint)(zz_14 >> 32);
 
-            ShiftUpBit(zz, zzOff, 16, (uint)x_0 << 31);
+            Nat.ShiftUpBit(16, zz, zzOff, (uint)x_0 << 31);
         }
 
         public static int Sub(uint[] x, uint[] y, uint[] z)
diff --git a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
index 9b3d12536..99e4cf999 100644
--- a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
@@ -56,12 +56,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat192.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(6, x, 0, z);
             }
             else
             {
                 uint c = Nat192.Add(x, P, z);
-                Nat192.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(6, z, c);
             }
         }
 
@@ -148,7 +148,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat192.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(6, x, 0, z);
             if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
             {
                 Nat192.AddDWord(PInv, z, 0);
diff --git a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
index b4c33d4e7..71ccfc4b7 100644
--- a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
@@ -54,12 +54,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat192.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(6, x, 0, z);
             }
             else
             {
                 uint c = Nat192.Add(x, P, z);
-                Nat192.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(6, z, c);
             }
         }
 
@@ -172,7 +172,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat192.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(6, x, 0, z);
             if (c != 0 || (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 13fb4e557..03df35d36 100644
--- a/crypto/src/math/ec/custom/sec/SecP224K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224K1Field.cs
@@ -57,12 +57,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat224.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(7, x, 0, z);
             }
             else
             {
                 uint c = Nat224.Add(x, P, z);
-                Nat224.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(7, z, c);
             }
         }
 
@@ -149,7 +149,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat224.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(7, x, 0, z);
             if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
             {
                 Nat224.AddDWord(PInv, z, 0);
diff --git a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
index ee5407beb..5447ce697 100644
--- a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
@@ -54,12 +54,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat224.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(7, x, 0, z);
             }
             else
             {
                 uint c = Nat224.Add(x, P, z);
-                Nat224.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(7, z, c);
             }
         }
 
@@ -176,7 +176,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat224.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(7, x, 0, z);
             if (c != 0 || (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 91144c017..6e834beb3 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -62,12 +62,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat256.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(8, x, 0, z);
             }
             else
             {
                 uint c = Nat256.Add(x, P, z);
-                Nat256.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(8, z, c);
             }
         }
 
@@ -157,7 +157,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat256.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(8, x, 0, z);
             if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
             {
                 Nat256.Add33To(PInv33, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index 253f7b489..9bcb0e98e 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -57,12 +57,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             if ((x[0] & 1) == 0)
             {
-                Nat256.ShiftDownBit(x, 0, z);
+                Nat.ShiftDownBit(8, x, 0, z);
             }
             else
             {
                 uint c = Nat256.Add(x, P, z);
-                Nat256.ShiftDownBit(z, c, z);
+                Nat.ShiftDownBit(8, z, c);
             }
         }
 
@@ -226,7 +226,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Twice(uint[] x, uint[] z)
         {
-            uint c = Nat256.ShiftUpBit(x, 0, z);
+            uint c = Nat.ShiftUpBit(8, x, 0, z);
             if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
             {
                 Nat256.SubFrom(P, z);