summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-03-10 11:12:13 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-03-10 11:12:13 +0700
commit2c6c7ebd4b95663e839729ef7f1ee0b1a94c36af (patch)
tree709b7d41aa50592ee2d528c94f7619b164b25435
parentUse more specific Nat methods (diff)
downloadBouncyCastle.NET-ed25519-2c6c7ebd4b95663e839729ef7f1ee0b1a94c36af.tar.xz
Inline Reduce32 calls and registerize some values to avoid extra writes
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192R1Field.cs36
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224R1Field.cs29
2 files changed, 53 insertions, 12 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
index 078ef94f8..5878749cf 100644
--- a/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192R1Field.cs
@@ -96,7 +96,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             ulong cc = 0;
             cc += (ulong)xx[0] + t0;
-            z[0] = (uint)cc;
+            uint z0 = (uint)cc;
             cc >>= 32;
             cc += (ulong)xx[1] + t1;
             z[1] = (uint)cc;
@@ -106,7 +106,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             t1 += xx09;
 
             cc += (ulong)xx[2] + t0;
-            z[2] = (uint)cc;
+            ulong z2 = (uint)cc;
             cc >>= 32;
             cc += (ulong)xx[3] + t1;
             z[3] = (uint)cc;
@@ -122,27 +122,45 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             z[5] = (uint)cc;
             cc >>= 32;
 
-            Reduce32((uint)cc, z);
+            z2 += cc;
+
+            cc += z0;
+            z[0] = (uint)cc;
+            cc >>= 32;
+            if (cc != 0)
+            {
+                cc += z[1];
+                z[1] = (uint)cc;
+                z2 += cc >> 32;
+            }
+            z[2] = (uint)z2;
+            cc  = z2 >> 32;
+
+            Debug.Assert(cc == 0 || cc == 1);
+
+            if ((cc != 0 && Nat.IncAt(6, z, 3) != 0)
+                || (z[5] == P5 && Nat192.Gte(z, P)))
+            {
+                AddPInvTo(z);
+            }
         }
 
         public static void Reduce32(uint x, uint[] z)
         {
-            long cc = 0;
+            ulong cc = 0;
 
             if (x != 0)
             {
-                long xx06 = x;
-
-                cc += (long)z[0] + xx06;
+                cc += (ulong)z[0] + x;
                 z[0] = (uint)cc;
                 cc >>= 32;
                 if (cc != 0)
                 {
-                    cc += (long)z[1];
+                    cc += (ulong)z[1];
                     z[1] = (uint)cc;
                     cc >>= 32;
                 }
-                cc += (long)z[2] + xx06;
+                cc += (ulong)z[2] + x;
                 z[2] = (uint)cc;
                 cc >>= 32;
 
diff --git a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
index 3f9f79fc3..17c9b92a5 100644
--- a/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224R1Field.cs
@@ -101,7 +101,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             long cc = 0;
             cc += (long)xx[0] - t0;
-            z[0] = (uint)cc;
+            long z0 = (uint)cc;
             cc >>= 32;
             cc += (long)xx[1] - t1;
             z[1] = (uint)cc;
@@ -110,7 +110,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             z[2] = (uint)cc;
             cc >>= 32;
             cc += (long)xx[3] + t0 - xx10;
-            z[3] = (uint)cc;
+            long z3 = (uint)cc;
             cc >>= 32;
             cc += (long)xx[4] + t1 - xx11;
             z[4] = (uint)cc;
@@ -125,7 +125,30 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             Debug.Assert(cc >= 0);
 
-            Reduce32((uint)cc, z);
+            z3 += cc;
+
+            z0 -= cc;
+            z[0] = (uint)z0;
+            cc = z0 >> 32;
+            if (cc != 0)
+            {
+                cc += (long)z[1];
+                z[1] = (uint)cc;
+                cc >>= 32;
+                cc += (long)z[2];
+                z[2] = (uint)cc;
+                z3 += cc >> 32;
+            }
+            z[3] = (uint)z3;
+            cc = z3 >> 32;
+
+            Debug.Assert(cc == 0 || cc == 1);
+
+            if ((cc != 0 && Nat.IncAt(7, z, 4) != 0)
+                || (z[6] == P6 && Nat224.Gte(z, P)))
+            {
+                AddPInvTo(z);
+            }
         }
 
         public static void Reduce32(uint x, uint[] z)