summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-03-10 19:19:52 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-03-10 19:19:52 +0700
commit84e8dad1309ad2d247af6cfd9038bff5ac5ce941 (patch)
treecec106af064d73325907ce8309cecbc083f8a741
parentAdapt performance test to exclude outliers form average (diff)
downloadBouncyCastle.NET-ed25519-84e8dad1309ad2d247af6cfd9038bff5ac5ce941.tar.xz
Optimize some of the addition/doubling internals
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192K1Point.cs33
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192R1Point.cs27
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224K1Point.cs33
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224R1Point.cs27
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Point.cs33
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Point.cs27
-rw-r--r--crypto/src/math/ec/custom/sec/SecP384R1Point.cs22
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Point.cs4
8 files changed, 114 insertions, 92 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecP192K1Point.cs b/crypto/src/math/ec/custom/sec/SecP192K1Point.cs
index 1c6573aca..561324f8e 100644
--- a/crypto/src/math/ec/custom/sec/SecP192K1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192K1Point.cs
@@ -77,8 +77,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP192K1FieldElement Z1 = (SecP192K1FieldElement)this.RawZCoords[0];
             SecP192K1FieldElement Z2 = (SecP192K1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat192.CreateExt();
-            uint[] tt2 = Nat192.CreateExt();
+            uint[] t2 = Nat192.Create();
             uint[] t3 = Nat192.Create();
             uint[] t4 = Nat192.Create();
 
@@ -94,7 +95,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP192K1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP192K1Field.Multiply(S2, X2.x, U2);
 
                 SecP192K1Field.Multiply(S2, Z1.x, S2);
@@ -123,7 +124,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat192.Create();
             SecP192K1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP192K1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -148,19 +149,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP192K1Field.Multiply(HSquared, U1, V);
 
+            SecP192K1Field.Negate(G, G);
             Nat192.Mul(S1, G, tt1);
 
+            c = Nat192.AddBothTo(V, V, G);
+            SecP192K1Field.Reduce32(c, G);
+
             SecP192K1FieldElement X3 = new SecP192K1FieldElement(t4);
             SecP192K1Field.Square(R, X3.x);
-            SecP192K1Field.Add(X3.x, G, X3.x);
-            SecP192K1Field.Subtract(X3.x, V, X3.x);
-            SecP192K1Field.Subtract(X3.x, V, X3.x);
+            SecP192K1Field.Subtract(X3.x, G, X3.x);
 
             SecP192K1FieldElement Y3 = new SecP192K1FieldElement(G);
             SecP192K1Field.Subtract(V, X3.x, Y3.x);
-            Nat192.Mul(Y3.x, R, tt2);
-            SecP192K1Field.SubtractExt(tt2, tt1, tt2);
-            SecP192K1Field.Reduce(tt2, Y3.x);
+            SecP192K1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP192K1Field.Reduce(tt1, Y3.x);
 
             SecP192K1FieldElement Z3 = new SecP192K1FieldElement(H);
             if (!Z1IsOne)
@@ -190,24 +192,25 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP192K1FieldElement X1 = (SecP192K1FieldElement)this.RawXCoord, Z1 = (SecP192K1FieldElement)this.RawZCoords[0];
 
+            uint c;
+
             uint[] Y1Squared = Nat192.Create();
             SecP192K1Field.Square(Y1.x, Y1Squared);
 
             uint[] T = Nat192.Create();
             SecP192K1Field.Square(Y1Squared, T);
 
-            uint[] t1 = Nat192.Create();
-            SecP192K1Field.Square(X1.x, t1);
-
             uint[] M = Nat192.Create();
-            SecP192K1Field.Twice(t1, M);
-            SecP192K1Field.Add(M, t1, M);
+            SecP192K1Field.Square(X1.x, M);
+            c = Nat192.AddBothTo(M, M, M);
+            SecP192K1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP192K1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(6, S, 2, 0);
+            c = Nat.ShiftUpBits(6, S, 2, 0);
             SecP192K1Field.Reduce32(c, S);
 
+            uint[] t1 = Nat192.Create();
             c = Nat.ShiftUpBits(6, T, 3, 0, t1);
             SecP192K1Field.Reduce32(c, t1);
 
diff --git a/crypto/src/math/ec/custom/sec/SecP192R1Point.cs b/crypto/src/math/ec/custom/sec/SecP192R1Point.cs
index 29a26c941..c249c1269 100644
--- a/crypto/src/math/ec/custom/sec/SecP192R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192R1Point.cs
@@ -76,8 +76,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP192R1FieldElement Z1 = (SecP192R1FieldElement)this.RawZCoords[0];
             SecP192R1FieldElement Z2 = (SecP192R1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat192.CreateExt();
-            uint[] tt2 = Nat192.CreateExt();
+            uint[] t2 = Nat192.Create();
             uint[] t3 = Nat192.Create();
             uint[] t4 = Nat192.Create();
 
@@ -93,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP192R1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP192R1Field.Multiply(S2, X2.x, U2);
 
                 SecP192R1Field.Multiply(S2, Z1.x, S2);
@@ -122,7 +123,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat192.Create();
             SecP192R1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP192R1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -147,19 +148,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP192R1Field.Multiply(HSquared, U1, V);
 
+            SecP192R1Field.Negate(G, G);
             Nat192.Mul(S1, G, tt1);
 
+            c = Nat192.AddBothTo(V, V, G);
+            SecP192R1Field.Reduce32(c, G);
+
             SecP192R1FieldElement X3 = new SecP192R1FieldElement(t4);
             SecP192R1Field.Square(R, X3.x);
-            SecP192R1Field.Add(X3.x, G, X3.x);
-            SecP192R1Field.Subtract(X3.x, V, X3.x);
-            SecP192R1Field.Subtract(X3.x, V, X3.x);
+            SecP192R1Field.Subtract(X3.x, G, X3.x);
 
             SecP192R1FieldElement Y3 = new SecP192R1FieldElement(G);
             SecP192R1Field.Subtract(V, X3.x, Y3.x);
-            Nat192.Mul(Y3.x, R, tt2);
-            SecP192R1Field.SubtractExt(tt2, tt1, tt2);
-            SecP192R1Field.Reduce(tt2, Y3.x);
+            SecP192R1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP192R1Field.Reduce(tt1, Y3.x);
 
             SecP192R1FieldElement Z3 = new SecP192R1FieldElement(H);
             if (!Z1IsOne)
@@ -189,6 +191,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP192R1FieldElement X1 = (SecP192R1FieldElement)this.RawXCoord, Z1 = (SecP192R1FieldElement)this.RawZCoords[0];
 
+            uint c;
             uint[] t1 = Nat192.Create();
             uint[] t2 = Nat192.Create();
 
@@ -212,12 +215,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] M = t2;
             SecP192R1Field.Add(X1.x, Z1Squared, M);
             SecP192R1Field.Multiply(M, t1, M);
-            SecP192R1Field.Twice(M, t1);
-            SecP192R1Field.Add(M, t1, M);
+            c = Nat192.AddBothTo(M, M, M);
+            SecP192R1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP192R1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(6, S, 2, 0);
+            c = Nat.ShiftUpBits(6, S, 2, 0);
             SecP192R1Field.Reduce32(c, S);
 
             c = Nat.ShiftUpBits(6, T, 3, 0, t1);
diff --git a/crypto/src/math/ec/custom/sec/SecP224K1Point.cs b/crypto/src/math/ec/custom/sec/SecP224K1Point.cs
index c7119401d..dd6faa829 100644
--- a/crypto/src/math/ec/custom/sec/SecP224K1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224K1Point.cs
@@ -77,8 +77,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP224K1FieldElement Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
             SecP224K1FieldElement Z2 = (SecP224K1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat224.CreateExt();
-            uint[] tt2 = Nat224.CreateExt();
+            uint[] t2 = Nat224.Create();
             uint[] t3 = Nat224.Create();
             uint[] t4 = Nat224.Create();
 
@@ -94,7 +95,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP224K1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP224K1Field.Multiply(S2, X2.x, U2);
 
                 SecP224K1Field.Multiply(S2, Z1.x, S2);
@@ -123,7 +124,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat224.Create();
             SecP224K1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP224K1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -148,19 +149,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP224K1Field.Multiply(HSquared, U1, V);
 
+            SecP224K1Field.Negate(G, G);
             Nat224.Mul(S1, G, tt1);
 
+            c = Nat224.AddBothTo(V, V, G);
+            SecP224K1Field.Reduce32(c, G);
+
             SecP224K1FieldElement X3 = new SecP224K1FieldElement(t4);
             SecP224K1Field.Square(R, X3.x);
-            SecP224K1Field.Add(X3.x, G, X3.x);
-            SecP224K1Field.Subtract(X3.x, V, X3.x);
-            SecP224K1Field.Subtract(X3.x, V, X3.x);
+            SecP224K1Field.Subtract(X3.x, G, X3.x);
 
             SecP224K1FieldElement Y3 = new SecP224K1FieldElement(G);
             SecP224K1Field.Subtract(V, X3.x, Y3.x);
-            Nat224.Mul(Y3.x, R, tt2);
-            SecP224K1Field.SubtractExt(tt2, tt1, tt2);
-            SecP224K1Field.Reduce(tt2, Y3.x);
+            SecP224K1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP224K1Field.Reduce(tt1, Y3.x);
 
             SecP224K1FieldElement Z3 = new SecP224K1FieldElement(H);
             if (!Z1IsOne)
@@ -190,24 +192,25 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP224K1FieldElement X1 = (SecP224K1FieldElement)this.RawXCoord, Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
 
+            uint c;
+
             uint[] Y1Squared = Nat224.Create();
             SecP224K1Field.Square(Y1.x, Y1Squared);
 
             uint[] T = Nat224.Create();
             SecP224K1Field.Square(Y1Squared, T);
 
-            uint[] t1 = Nat224.Create();
-            SecP224K1Field.Square(X1.x, t1);
-
             uint[] M = Nat224.Create();
-            SecP224K1Field.Twice(t1, M);
-            SecP224K1Field.Add(M, t1, M);
+            SecP224K1Field.Square(X1.x, M);
+            c = Nat224.AddBothTo(M, M, M);
+            SecP224K1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP224K1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(7, S, 2, 0);
+            c = Nat.ShiftUpBits(7, S, 2, 0);
             SecP224K1Field.Reduce32(c, S);
 
+            uint[] t1 = Nat224.Create();
             c = Nat.ShiftUpBits(7, T, 3, 0, t1);
             SecP224K1Field.Reduce32(c, t1);
 
diff --git a/crypto/src/math/ec/custom/sec/SecP224R1Point.cs b/crypto/src/math/ec/custom/sec/SecP224R1Point.cs
index 31cef6929..3b339720d 100644
--- a/crypto/src/math/ec/custom/sec/SecP224R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224R1Point.cs
@@ -76,8 +76,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP224R1FieldElement Z1 = (SecP224R1FieldElement)this.RawZCoords[0];
             SecP224R1FieldElement Z2 = (SecP224R1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat224.CreateExt();
-            uint[] tt2 = Nat224.CreateExt();
+            uint[] t2 = Nat224.Create();
             uint[] t3 = Nat224.Create();
             uint[] t4 = Nat224.Create();
 
@@ -93,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP224R1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP224R1Field.Multiply(S2, X2.x, U2);
 
                 SecP224R1Field.Multiply(S2, Z1.x, S2);
@@ -122,7 +123,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat224.Create();
             SecP224R1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP224R1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -147,19 +148,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP224R1Field.Multiply(HSquared, U1, V);
 
+            SecP224R1Field.Negate(G, G);
             Nat224.Mul(S1, G, tt1);
 
+            c = Nat224.AddBothTo(V, V, G);
+            SecP224R1Field.Reduce32(c, G);
+
             SecP224R1FieldElement X3 = new SecP224R1FieldElement(t4);
             SecP224R1Field.Square(R, X3.x);
-            SecP224R1Field.Add(X3.x, G, X3.x);
-            SecP224R1Field.Subtract(X3.x, V, X3.x);
-            SecP224R1Field.Subtract(X3.x, V, X3.x);
+            SecP224R1Field.Subtract(X3.x, G, X3.x);
 
             SecP224R1FieldElement Y3 = new SecP224R1FieldElement(G);
             SecP224R1Field.Subtract(V, X3.x, Y3.x);
-            Nat224.Mul(Y3.x, R, tt2);
-            SecP224R1Field.SubtractExt(tt2, tt1, tt2);
-            SecP224R1Field.Reduce(tt2, Y3.x);
+            SecP224R1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP224R1Field.Reduce(tt1, Y3.x);
 
             SecP224R1FieldElement Z3 = new SecP224R1FieldElement(H);
             if (!Z1IsOne)
@@ -189,6 +191,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP224R1FieldElement X1 = (SecP224R1FieldElement)this.RawXCoord, Z1 = (SecP224R1FieldElement)this.RawZCoords[0];
 
+            uint c;
             uint[] t1 = Nat224.Create();
             uint[] t2 = Nat224.Create();
 
@@ -212,12 +215,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] M = t2;
             SecP224R1Field.Add(X1.x, Z1Squared, M);
             SecP224R1Field.Multiply(M, t1, M);
-            SecP224R1Field.Twice(M, t1);
-            SecP224R1Field.Add(M, t1, M);
+            c = Nat224.AddBothTo(M, M, M);
+            SecP224R1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP224R1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(7, S, 2, 0);
+            c = Nat.ShiftUpBits(7, S, 2, 0);
             SecP224R1Field.Reduce32(c, S);
 
             c = Nat.ShiftUpBits(7, T, 3, 0, t1);
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Point.cs b/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
index d8e1eb83a..b12eadb72 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
@@ -77,8 +77,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP256K1FieldElement Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
             SecP256K1FieldElement Z2 = (SecP256K1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat256.CreateExt();
-            uint[] tt2 = Nat256.CreateExt();
+            uint[] t2 = Nat256.Create();
             uint[] t3 = Nat256.Create();
             uint[] t4 = Nat256.Create();
 
@@ -94,7 +95,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP256K1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP256K1Field.Multiply(S2, X2.x, U2);
 
                 SecP256K1Field.Multiply(S2, Z1.x, S2);
@@ -123,7 +124,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat256.Create();
             SecP256K1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP256K1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -148,19 +149,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP256K1Field.Multiply(HSquared, U1, V);
 
+            SecP256K1Field.Negate(G, G);
             Nat256.Mul(S1, G, tt1);
 
+            c = Nat256.AddBothTo(V, V, G);
+            SecP256K1Field.Reduce32(c, G);
+
             SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
             SecP256K1Field.Square(R, X3.x);
-            SecP256K1Field.Add(X3.x, G, X3.x);
-            SecP256K1Field.Subtract(X3.x, V, X3.x);
-            SecP256K1Field.Subtract(X3.x, V, X3.x);
+            SecP256K1Field.Subtract(X3.x, G, X3.x);
 
             SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
             SecP256K1Field.Subtract(V, X3.x, Y3.x);
-            Nat256.Mul(Y3.x, R, tt2);
-            SecP256K1Field.SubtractExt(tt2, tt1, tt2);
-            SecP256K1Field.Reduce(tt2, Y3.x);
+            SecP256K1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP256K1Field.Reduce(tt1, Y3.x);
 
             SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
             if (!Z1IsOne)
@@ -190,24 +192,25 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
 
+            uint c;
+
             uint[] Y1Squared = Nat256.Create();
             SecP256K1Field.Square(Y1.x, Y1Squared);
 
             uint[] T = Nat256.Create();
             SecP256K1Field.Square(Y1Squared, T);
 
-            uint[] t1 = Nat256.Create();
-            SecP256K1Field.Square(X1.x, t1);
-
             uint[] M = Nat256.Create();
-            SecP256K1Field.Twice(t1, M);
-            SecP256K1Field.Add(M, t1, M);
+            SecP256K1Field.Square(X1.x, M);
+            c = Nat256.AddBothTo(M, M, M);
+            SecP256K1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP256K1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(8, S, 2, 0);
+            c = Nat.ShiftUpBits(8, S, 2, 0);
             SecP256K1Field.Reduce32(c, S);
 
+            uint[] t1 = Nat256.Create();
             c = Nat.ShiftUpBits(8, T, 3, 0, t1);
             SecP256K1Field.Reduce32(c, t1);
 
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Point.cs b/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
index e25ff5f7a..0e4b95a10 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
@@ -76,8 +76,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP256R1FieldElement Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
             SecP256R1FieldElement Z2 = (SecP256R1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat256.CreateExt();
-            uint[] tt2 = Nat256.CreateExt();
+            uint[] t2 = Nat256.Create();
             uint[] t3 = Nat256.Create();
             uint[] t4 = Nat256.Create();
 
@@ -93,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 S2 = t3;
                 SecP256R1Field.Square(Z1.x, S2);
 
-                U2 = tt2;
+                U2 = t2;
                 SecP256R1Field.Multiply(S2, X2.x, U2);
 
                 SecP256R1Field.Multiply(S2, Z1.x, S2);
@@ -122,7 +123,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat256.Create();
             SecP256R1Field.Subtract(U1, U2, H);
 
-            uint[] R = tt2;
+            uint[] R = t2;
             SecP256R1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -147,19 +148,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP256R1Field.Multiply(HSquared, U1, V);
 
+            SecP256R1Field.Negate(G, G);
             Nat256.Mul(S1, G, tt1);
 
+            c = Nat256.AddBothTo(V, V, G);
+            SecP256R1Field.Reduce32(c, G);
+
             SecP256R1FieldElement X3 = new SecP256R1FieldElement(t4);
             SecP256R1Field.Square(R, X3.x);
-            SecP256R1Field.Add(X3.x, G, X3.x);
-            SecP256R1Field.Subtract(X3.x, V, X3.x);
-            SecP256R1Field.Subtract(X3.x, V, X3.x);
+            SecP256R1Field.Subtract(X3.x, G, X3.x);
 
             SecP256R1FieldElement Y3 = new SecP256R1FieldElement(G);
             SecP256R1Field.Subtract(V, X3.x, Y3.x);
-            Nat256.Mul(Y3.x, R, tt2);
-            SecP256R1Field.SubtractExt(tt2, tt1, tt2);
-            SecP256R1Field.Reduce(tt2, Y3.x);
+            SecP256R1Field.MultiplyAddToExt(Y3.x, R, tt1);
+            SecP256R1Field.Reduce(tt1, Y3.x);
 
             SecP256R1FieldElement Z3 = new SecP256R1FieldElement(H);
             if (!Z1IsOne)
@@ -189,6 +191,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
 
+            uint c;
             uint[] t1 = Nat256.Create();
             uint[] t2 = Nat256.Create();
 
@@ -212,12 +215,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] M = t2;
             SecP256R1Field.Add(X1.x, Z1Squared, M);
             SecP256R1Field.Multiply(M, t1, M);
-            SecP256R1Field.Twice(M, t1);
-            SecP256R1Field.Add(M, t1, M);
+            c = Nat256.AddBothTo(M, M, M);
+            SecP256R1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP256R1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(8, S, 2, 0);
+            c = Nat.ShiftUpBits(8, S, 2, 0);
             SecP256R1Field.Reduce32(c, S);
 
             c = Nat.ShiftUpBits(8, T, 3, 0, t1);
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1Point.cs b/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
index 6547df20c..1ca8489dc 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
@@ -76,6 +76,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP384R1FieldElement Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
             SecP384R1FieldElement Z2 = (SecP384R1FieldElement)b.RawZCoords[0];
 
+            uint c;
             uint[] tt1 = Nat.Create(24);
             uint[] tt2 = Nat.Create(24);
             uint[] t3 = Nat.Create(12);
@@ -122,7 +123,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] H = Nat.Create(12);
             SecP384R1Field.Subtract(U1, U2, H);
 
-            uint[] R = Nat.Create(12);// tt2;
+            uint[] R = Nat.Create(12);
             SecP384R1Field.Subtract(S1, S2, R);
 
             // Check if b == this or b == -this
@@ -147,19 +148,21 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] V = t3;
             SecP384R1Field.Multiply(HSquared, U1, V);
 
+            SecP384R1Field.Negate(G, G);
             Nat384.Mul(S1, G, tt1);
 
+            c = Nat.AddBothTo(12, V, V, G);
+            SecP384R1Field.Reduce32(c, G);
+
             SecP384R1FieldElement X3 = new SecP384R1FieldElement(t4);
             SecP384R1Field.Square(R, X3.x);
-            SecP384R1Field.Add(X3.x, G, X3.x);
-            SecP384R1Field.Subtract(X3.x, V, X3.x);
-            SecP384R1Field.Subtract(X3.x, V, X3.x);
+            SecP384R1Field.Subtract(X3.x, G, X3.x);
 
             SecP384R1FieldElement Y3 = new SecP384R1FieldElement(G);
             SecP384R1Field.Subtract(V, X3.x, Y3.x);
             Nat384.Mul(Y3.x, R, tt2);
-            SecP384R1Field.SubtractExt(tt2, tt1, tt2);
-            SecP384R1Field.Reduce(tt2, Y3.x);
+            SecP384R1Field.AddExt(tt1, tt2, tt1);
+            SecP384R1Field.Reduce(tt1, Y3.x);
 
             SecP384R1FieldElement Z3 = new SecP384R1FieldElement(H);
             if (!Z1IsOne)
@@ -189,6 +192,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
 
+            uint c;
             uint[] t1 = Nat.Create(12);
             uint[] t2 = Nat.Create(12);
 
@@ -212,12 +216,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] M = t2;
             SecP384R1Field.Add(X1.x, Z1Squared, M);
             SecP384R1Field.Multiply(M, t1, M);
-            SecP384R1Field.Twice(M, t1);
-            SecP384R1Field.Add(M, t1, M);
+            c = Nat.AddBothTo(12, M, M, M);
+            SecP384R1Field.Reduce32(c, M);
 
             uint[] S = Y1Squared;
             SecP384R1Field.Multiply(Y1Squared, X1.x, S);
-            uint c = Nat.ShiftUpBits(12, S, 2, 0);
+            c = Nat.ShiftUpBits(12, S, 2, 0);
             SecP384R1Field.Reduce32(c, S);
 
             c = Nat.ShiftUpBits(12, T, 3, 0, t1);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Point.cs b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
index 2e3a7eccb..44d590f08 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
@@ -211,8 +211,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] M = t2;
             SecP521R1Field.Add(X1.x, Z1Squared, M);
             SecP521R1Field.Multiply(M, t1, M);
-            SecP521R1Field.Twice(M, t1);
-            SecP521R1Field.Add(M, t1, M);
+            Nat.AddBothTo(17, M, M, M);
+            SecP521R1Field.Reduce23(M);
 
             uint[] S = Y1Squared;
             SecP521R1Field.Multiply(Y1Squared, X1.x, S);