From 2c6c7ebd4b95663e839729ef7f1ee0b1a94c36af Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 10 Mar 2014 11:12:13 +0700 Subject: Inline Reduce32 calls and registerize some values to avoid extra writes --- crypto/src/math/ec/custom/sec/SecP192R1Field.cs | 36 ++++++++++++++++++------- crypto/src/math/ec/custom/sec/SecP224R1Field.cs | 29 +++++++++++++++++--- 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) -- cgit 1.4.1