summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 09:27:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 09:27:57 +0700
commit4b257e26ddb57fb73700f8c6f64185a63310b6d2 (patch)
treecfa344dc59605921c8cb41327f03007bbf13f14e /crypto
parentRound out the Nat192 methods (diff)
downloadBouncyCastle.NET-ed25519-4b257e26ddb57fb73700f8c6f64185a63310b6d2.tar.xz
Optimize final adjustments in Reduce()
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Field.cs19
1 files changed, 8 insertions, 11 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index dd37820b6..a01cb5840 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -8,6 +8,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         // 2^256 - 2^224 + 2^192 + 2^96 - 1
         internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000,
             0x00000001, 0xFFFFFFFF };
+        private static readonly uint[] _2P = new uint[]{ 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001, 0x00000000, 0x00000000,
+            0x00000002, 0xFFFFFFFE, 0x00000001 };
         private const uint P7 = 0xFFFFFFFF;
         private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
             0xFFFFFFFF, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0x00000001, 0xFFFFFFFE,
@@ -123,24 +125,19 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             cc >>= 32;
 
             int c = (int)cc;
-            if (c < 0)
+            if (c > 0)
             {
-                do
-                {
-                    c += (int)Nat256.Add(z, P, z);
-                }
-                while (c < 0);
+                Reduce32((uint)c, z);
             }
             else
             {
-                while (c > 0)
+                while (c < -1)
                 {
-                    c += Nat256.Sub(z, P, z);
+                    c += (int)Nat256.Add(z, _2P, z) + 1;
                 }
-
-                if (z[7] == P7 && Nat256.Gte(z, P))
+                while (c < 0)
                 {
-                    Nat256.Sub(z, P, z);
+                    c += (int)Nat256.Add(z, P, z);
                 }
             }
         }