summary refs log tree commit diff
path: root/crypto/src/math/ec
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-02-24 19:44:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-02-24 19:44:57 +0700
commit9beb3a6ea61b10349e348c1216f52b187d172d2c (patch)
tree2331137e3b37cd3f48f5ec3a3d6fe0fa20ab66cb /crypto/src/math/ec
parentTabs -> spaces (diff)
downloadBouncyCastle.NET-ed25519-9beb3a6ea61b10349e348c1216f52b187d172d2c.tar.xz
Refactoring in Nat* classes
Diffstat (limited to '')
-rw-r--r--crypto/src/math/ec/Nat.cs54
-rw-r--r--crypto/src/math/ec/custom/sec/Nat192.cs64
-rw-r--r--crypto/src/math/ec/custom/sec/Nat256.cs76
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Field.cs2
6 files changed, 99 insertions, 101 deletions
diff --git a/crypto/src/math/ec/Nat.cs b/crypto/src/math/ec/Nat.cs
index 91d86b674..a5a496fad 100644
--- a/crypto/src/math/ec/Nat.cs
+++ b/crypto/src/math/ec/Nat.cs
@@ -215,11 +215,21 @@ namespace Org.BouncyCastle.Math.EC
 
         public static void Mul(int len, uint[] x, uint[] y, uint[] zz)
         {
-            zz[len] = (uint)MulWord(len, x[0], y, zz, 0);
+            zz[len] = (uint)MulWord(len, x[0], y, zz);
 
             for (int i = 1; i < len; ++i)
             {
-                zz[i + len] = (uint)MulWordAdd(len, x[i], y, zz, i);
+                zz[i + len] = (uint)MulWordAddTo(len, x[i], y, 0, zz, i);
+            }
+        }
+
+        public static void Mul(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
+        {
+            zz[len] = (uint)MulWord(len, x[xOff + 0], y, yOff, zz, zzOff);
+
+            for (int i = 1; i < len; ++i)
+            {
+                zz[i + len] = (uint)MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff + i);
             }
         }
 
@@ -237,13 +247,27 @@ namespace Org.BouncyCastle.Math.EC
             return (uint)c;
         }
 
-        public static uint MulWord(int len, uint x, uint[] y, uint[] z, int zOff)
+        public static uint MulWord(int len, uint x, uint[] y, uint[] z)
         {
             ulong c = 0, xVal = (ulong)x;
             int i = 0;
             do
             {
                 c += xVal * y[i];
+                z[i] = (uint)c;
+                c >>= 32;
+            }
+            while (++i < len);
+            return (uint)c;
+        }
+
+        public static uint MulWord(int len, uint x, uint[] y, int yOff, uint[] z, int zOff)
+        {
+            ulong c = 0, xVal = (ulong)x;
+            int i = 0;
+            do
+            {
+                c += xVal * y[yOff + i];
                 z[zOff + i] = (uint)c;
                 c >>= 32;
             }
@@ -251,13 +275,13 @@ namespace Org.BouncyCastle.Math.EC
             return (uint)c;
         }
 
-        public static uint MulWordAdd(int len, uint x, uint[] y, uint[] z, int zOff)
+        public static uint MulWordAddTo(int len, uint x, uint[] y, int yOff, uint[] z, int zOff)
         {
             ulong c = 0, xVal = (ulong)x;
             int i = 0;
             do
             {
-                c += xVal * y[i] + z[zOff + i];
+                c += xVal * y[yOff + i] + z[zOff + i];
                 z[zOff + i] = (uint)c;
                 c >>= 32;
             }
@@ -354,6 +378,17 @@ namespace Org.BouncyCastle.Math.EC
             return c >> 31;
         }
 
+        public static uint ShiftUpBit(int len, uint[] z, int zOff, uint c)
+        {
+            for (int i = 0; i < len; ++i)
+            {
+                uint next = z[zOff + i];
+                z[zOff + i] = (next << 1) | (c >> 31);
+                c = next;
+            }
+            return c >> 31;
+        }
+
         public static uint ShiftUpBit(int len, uint[] x, uint c, uint[] z)
         {
             for (int i = 0; i < len; ++i)
@@ -417,22 +452,21 @@ namespace Org.BouncyCastle.Math.EC
 
             for (int i = 1; i < len; ++i)
             {
-                c = SquareWordAddExt(len, x, i, zz);
+                c = SquareWordAdd(x, i, zz);
                 AddWordExt(len, c, zz, i << 1);
             }
 
             ShiftUpBit(extLen, zz, x[0] << 31);
         }
 
-        public static uint SquareWordAddExt(int len, uint[] x, int xPos, uint[] zz)
+        public static uint SquareWordAdd(uint[] x, int xPos, uint[] z)
         {
-            Debug.Assert(xPos > 0 && xPos < len);
             ulong c = 0, xVal = (ulong)x[xPos];
             int i = 0;
             do
             {
-                c += xVal * x[i] + zz[xPos + i];
-                zz[xPos + i] = (uint)c;
+                c += xVal * x[i] + z[xPos + i];
+                z[xPos + i] = (uint)c;
                 c >>= 32;
             }
             while (++i < xPos);
diff --git a/crypto/src/math/ec/custom/sec/Nat192.cs b/crypto/src/math/ec/custom/sec/Nat192.cs
index b2f29221c..6d908d1ca 100644
--- a/crypto/src/math/ec/custom/sec/Nat192.cs
+++ b/crypto/src/math/ec/custom/sec/Nat192.cs
@@ -319,38 +319,36 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
-        public static ulong Mul33AddExt(uint w, uint[] xx, int xxOff, uint[] yy, int yyOff, uint[] zz, int zzOff)
+        public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
         {
             Debug.Assert(w >> 31 == 0);
-            Debug.Assert(xxOff <= 6);
-            Debug.Assert(yyOff <= 6);
-            Debug.Assert(zzOff <= 6);
+
             ulong c = 0, wVal = w;
-            ulong xx00 = xx[xxOff + 0];
-            c += wVal * xx00 + yy[yyOff + 0];
-            zz[zzOff + 0] = (uint)c;
+            ulong x0 = x[xOff + 0];
+            c += wVal * x0 + y[yOff + 0];
+            z[zOff + 0] = (uint)c;
             c >>= 32;
-            ulong xx01 = xx[xxOff + 1];
-            c += wVal * xx01 + xx00 + yy[yyOff + 1];
-            zz[zzOff + 1] = (uint)c;
+            ulong x1 = x[xOff + 1];
+            c += wVal * x1 + x0 + y[yOff + 1];
+            z[zOff + 1] = (uint)c;
             c >>= 32;
-            ulong xx02 = xx[xxOff + 2];
-            c += wVal * xx02 + xx01 + yy[yyOff + 2];
-            zz[zzOff + 2] = (uint)c;
+            ulong x2 = x[xOff + 2];
+            c += wVal * x2 + x1 + y[yOff + 2];
+            z[zOff + 2] = (uint)c;
             c >>= 32;
-            ulong xx03 = xx[xxOff + 3];
-            c += wVal * xx03 + xx02 + yy[yyOff + 3];
-            zz[zzOff + 3] = (uint)c;
+            ulong x3 = x[xOff + 3];
+            c += wVal * x3 + x2 + y[yOff + 3];
+            z[zOff + 3] = (uint)c;
             c >>= 32;
-            ulong xx04 = xx[xxOff + 4];
-            c += wVal * xx04 + xx03 + yy[yyOff + 4];
-            zz[zzOff + 4] = (uint)c;
+            ulong x4 = x[xOff + 4];
+            c += wVal * x4 + x3 + y[yOff + 4];
+            z[zOff + 4] = (uint)c;
             c >>= 32;
-            ulong xx05 = xx[xxOff + 5];
-            c += wVal * xx05 + xx04 + yy[yyOff + 5];
-            zz[zzOff + 5] = (uint)c;
+            ulong x5 = x[xOff + 5];
+            c += wVal * x5 + x4 + y[yOff + 5];
+            z[zOff + 5] = (uint)c;
             c >>= 32;
-            c += xx05;
+            c += x5;
             return c;
         }
 
@@ -418,15 +416,14 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return c == 0 ? 0 : Inc(z, zOff + 3);
         }
 
-        public static uint MulWordExt(uint x, uint[] y, uint[] zz, int zzOff)
+        public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
         {
-            Debug.Assert(zzOff <= 6);
             ulong c = 0, xVal = x;
             int i = 0;
             do
             {
                 c += xVal * y[i];
-                zz[zzOff + i] = (uint)c;
+                z[zOff + i] = (uint)c;
                 c >>= 32;
             }
             while (++i < 6);
@@ -600,21 +597,6 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             ShiftUpBit(zz, 12, (uint)x_0 << 31);
         }
 
-        public static uint SquareWordAddExt(uint[] x, int xPos, uint[] zz)
-        {
-            Debug.Assert(xPos > 0 && xPos < 6);
-            ulong c = 0, xVal = x[xPos];
-            int i = 0;
-            do
-            {
-                c += xVal * x[i] + zz[xPos + i];
-                zz[xPos + i] = (uint)c;
-                c >>= 32;
-            }
-            while (++i < xPos);
-            return (uint)c;
-        }
-
         public static int Sub(uint[] x, uint[] y, uint[] z)
         {
             long c = 0;
diff --git a/crypto/src/math/ec/custom/sec/Nat256.cs b/crypto/src/math/ec/custom/sec/Nat256.cs
index 4e3741886..c846f56fb 100644
--- a/crypto/src/math/ec/custom/sec/Nat256.cs
+++ b/crypto/src/math/ec/custom/sec/Nat256.cs
@@ -628,46 +628,44 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return (uint)zc;
         }
 
-        public static ulong Mul33AddExt(uint w, uint[] xx, int xxOff, uint[] yy, int yyOff, uint[] zz, int zzOff)
+        public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
         {
             Debug.Assert(w >> 31 == 0);
-            Debug.Assert(xxOff <= 8);
-            Debug.Assert(yyOff <= 8);
-            Debug.Assert(zzOff <= 8);
+
             ulong c = 0, wVal = w;
-            ulong xx00 = xx[xxOff + 0];
-            c += wVal * xx00 + yy[yyOff + 0];
-            zz[zzOff + 0] = (uint)c;
+            ulong x0 = x[xOff + 0];
+            c += wVal * x0 + y[yOff + 0];
+            z[zOff + 0] = (uint)c;
             c >>= 32;
-            ulong xx01 = xx[xxOff + 1];
-            c += wVal * xx01 + xx00 + yy[yyOff + 1];
-            zz[zzOff + 1] = (uint)c;
+            ulong x1 = x[xOff + 1];
+            c += wVal * x1 + x0 + y[yOff + 1];
+            z[zOff + 1] = (uint)c;
             c >>= 32;
-            ulong xx02 = xx[xxOff + 2];
-            c += wVal * xx02 + xx01 + yy[yyOff + 2];
-            zz[zzOff + 2] = (uint)c;
+            ulong x2 = x[xOff + 2];
+            c += wVal * x2 + x1 + y[yOff + 2];
+            z[zOff + 2] = (uint)c;
             c >>= 32;
-            ulong xx03 = xx[xxOff + 3];
-            c += wVal * xx03 + xx02 + yy[yyOff + 3];
-            zz[zzOff + 3] = (uint)c;
+            ulong x3 = x[xOff + 3];
+            c += wVal * x3 + x2 + y[yOff + 3];
+            z[zOff + 3] = (uint)c;
             c >>= 32;
-            ulong xx04 = xx[xxOff + 4];
-            c += wVal * xx04 + xx03 + yy[yyOff + 4];
-            zz[zzOff + 4] = (uint)c;
+            ulong x4 = x[xOff + 4];
+            c += wVal * x4 + x3 + y[yOff + 4];
+            z[zOff + 4] = (uint)c;
             c >>= 32;
-            ulong xx05 = xx[xxOff + 5];
-            c += wVal * xx05 + xx04 + yy[yyOff + 5];
-            zz[zzOff + 5] = (uint)c;
+            ulong x5 = x[xOff + 5];
+            c += wVal * x5 + x4 + y[yOff + 5];
+            z[zOff + 5] = (uint)c;
             c >>= 32;
-            ulong xx06 = xx[xxOff + 6];
-            c += wVal * xx06 + xx05 + yy[yyOff + 6];
-            zz[zzOff + 6] = (uint)c;
+            ulong x6 = x[xOff + 6];
+            c += wVal * x6 + x5 + y[yOff + 6];
+            z[zOff + 6] = (uint)c;
             c >>= 32;
-            ulong xx07 = xx[xxOff + 7];
-            c += wVal * xx07 + xx06 + yy[yyOff + 7];
-            zz[zzOff + 7] = (uint)c;
+            ulong x7 = x[xOff + 7];
+            c += wVal * x7 + x6 + y[yOff + 7];
+            z[zOff + 7] = (uint)c;
             c >>= 32;
-            c += xx07;
+            c += x7;
             return c;
         }
 
@@ -799,15 +797,14 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return c == 0 ? 0 : Inc(z, zOff + 3);
         }
 
-        public static uint MulWordExt(uint x, uint[] y, uint[] zz, int zzOff)
+        public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
         {
-            Debug.Assert(zzOff <= 8);
             ulong c = 0, xVal = x;
             int i = 0;
             do
             {
                 c += xVal * y[i];
-                zz[zzOff + i] = (uint)c;
+                z[zOff + i] = (uint)c;
                 c >>= 32;
             }
             while (++i < 8);
@@ -1168,21 +1165,6 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             ShiftUpBit(zz, zzOff, 16, (uint)x_0 << 31);
         }
 
-        public static uint SquareWordAddExt(uint[] x, int xPos, uint[] zz)
-        {
-            Debug.Assert(xPos > 0 && xPos < 8);
-            ulong c = 0, xVal = x[xPos];
-            int i = 0;
-            do
-            {
-                c += xVal * x[i] + zz[xPos + i];
-                zz[xPos + i] = (uint)c;
-                c >>= 32;
-            }
-            while (++i < xPos);
-            return (uint)c;
-        }
-
         public static int Sub(uint[] x, uint[] y, uint[] z)
         {
             long c = 0;
diff --git a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
index 11bc11d8d..832db0fbe 100644
--- a/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192K1Field.cs
@@ -86,7 +86,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Reduce(uint[] xx, uint[] z)
         {
-            ulong c = Nat192.Mul33AddExt(PInv33, xx, 6, xx, 0, z, 0);
+            ulong c = Nat192.Mul33Add(PInv33, xx, 6, xx, 0, z, 0);
             c = Nat192.Mul33DWordAdd(PInv33, c, z, 0);
 
             Debug.Assert(c == 0 || c == 1);
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
index 3f5437d4d..b3c964982 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -88,7 +88,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Reduce(uint[] xx, uint[] z)
         {
-            ulong c = Nat256.Mul33AddExt(PInv33, xx, 8, xx, 0, z, 0);
+            ulong c = Nat256.Mul33Add(PInv33, xx, 8, xx, 0, z, 0);
             c = Nat256.Mul33DWordAdd(PInv33, c, z, 0);
 
             Debug.Assert(c == 0 || c == 1);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
index 38b177c0d..cfe3202cd 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
@@ -152,7 +152,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             Nat512.Square(x, zz);
 
             uint x16 = x[16];
-            zz[32] = Nat.MulWordAdd(16, x16 << 1, x, zz, 16) + (x16 * x16);
+            zz[32] = Nat.MulWordAddTo(16, x16 << 1, x, 0, zz, 16) + (x16 * x16);
         }
     }
 }