diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
index 9a8915c01..a065ee790 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -97,6 +97,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
+ {
+ Nat256.Mul(x, y, tt);
+ Reduce(tt, z);
+ }
+
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
{
uint c = Nat256.MulAddTo(x, y, zz);
@@ -170,6 +176,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Square(uint[] x, uint[] z, uint[] tt)
+ {
+ Nat256.Square(x, tt);
+ Reduce(tt, z);
+ }
+
public static void SquareN(uint[] x, int n, uint[] z)
{
Debug.Assert(n > 0);
@@ -185,6 +197,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
}
+ public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
+ {
+ Debug.Assert(n > 0);
+
+ Nat256.Square(x, tt);
+ Reduce(tt, z);
+
+ while (--n > 0)
+ {
+ Nat256.Square(z, tt);
+ Reduce(tt, z);
+ }
+ }
+
public static void Subtract(uint[] x, uint[] y, uint[] z)
{
int c = Nat256.Sub(x, y, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
index d2ff05cb7..2bb83d5e9 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
@@ -141,49 +141,51 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
return this;
+ uint[] tt0 = Nat256.CreateExt();
+
uint[] x2 = Nat256.Create();
- SecP256K1Field.Square(x1, x2);
- SecP256K1Field.Multiply(x2, x1, x2);
+ SecP256K1Field.Square(x1, x2, tt0);
+ SecP256K1Field.Multiply(x2, x1, x2, tt0);
uint[] x3 = Nat256.Create();
- SecP256K1Field.Square(x2, x3);
- SecP256K1Field.Multiply(x3, x1, x3);
+ SecP256K1Field.Square(x2, x3, tt0);
+ SecP256K1Field.Multiply(x3, x1, x3, tt0);
uint[] x6 = Nat256.Create();
- SecP256K1Field.SquareN(x3, 3, x6);
- SecP256K1Field.Multiply(x6, x3, x6);
+ SecP256K1Field.SquareN(x3, 3, x6, tt0);
+ SecP256K1Field.Multiply(x6, x3, x6, tt0);
uint[] x9 = x6;
- SecP256K1Field.SquareN(x6, 3, x9);
- SecP256K1Field.Multiply(x9, x3, x9);
+ SecP256K1Field.SquareN(x6, 3, x9, tt0);
+ SecP256K1Field.Multiply(x9, x3, x9, tt0);
uint[] x11 = x9;
- SecP256K1Field.SquareN(x9, 2, x11);
- SecP256K1Field.Multiply(x11, x2, x11);
+ SecP256K1Field.SquareN(x9, 2, x11, tt0);
+ SecP256K1Field.Multiply(x11, x2, x11, tt0);
uint[] x22 = Nat256.Create();
- SecP256K1Field.SquareN(x11, 11, x22);
- SecP256K1Field.Multiply(x22, x11, x22);
+ SecP256K1Field.SquareN(x11, 11, x22, tt0);
+ SecP256K1Field.Multiply(x22, x11, x22, tt0);
uint[] x44 = x11;
- SecP256K1Field.SquareN(x22, 22, x44);
- SecP256K1Field.Multiply(x44, x22, x44);
+ SecP256K1Field.SquareN(x22, 22, x44, tt0);
+ SecP256K1Field.Multiply(x44, x22, x44, tt0);
uint[] x88 = Nat256.Create();
- SecP256K1Field.SquareN(x44, 44, x88);
- SecP256K1Field.Multiply(x88, x44, x88);
+ SecP256K1Field.SquareN(x44, 44, x88, tt0);
+ SecP256K1Field.Multiply(x88, x44, x88, tt0);
uint[] x176 = Nat256.Create();
- SecP256K1Field.SquareN(x88, 88, x176);
- SecP256K1Field.Multiply(x176, x88, x176);
+ SecP256K1Field.SquareN(x88, 88, x176, tt0);
+ SecP256K1Field.Multiply(x176, x88, x176, tt0);
uint[] x220 = x88;
- SecP256K1Field.SquareN(x176, 44, x220);
- SecP256K1Field.Multiply(x220, x44, x220);
+ SecP256K1Field.SquareN(x176, 44, x220, tt0);
+ SecP256K1Field.Multiply(x220, x44, x220, tt0);
uint[] x223 = x44;
- SecP256K1Field.SquareN(x220, 3, x223);
- SecP256K1Field.Multiply(x223, x3, x223);
+ SecP256K1Field.SquareN(x220, 3, x223, tt0);
+ SecP256K1Field.Multiply(x223, x3, x223, tt0);
uint[] t1 = x223;
- SecP256K1Field.SquareN(t1, 23, t1);
- SecP256K1Field.Multiply(t1, x22, t1);
- SecP256K1Field.SquareN(t1, 6, t1);
- SecP256K1Field.Multiply(t1, x2, t1);
- SecP256K1Field.SquareN(t1, 2, t1);
+ SecP256K1Field.SquareN(t1, 23, t1, tt0);
+ SecP256K1Field.Multiply(t1, x22, t1, tt0);
+ SecP256K1Field.SquareN(t1, 6, t1, tt0);
+ SecP256K1Field.Multiply(t1, x2, t1, tt0);
+ SecP256K1Field.SquareN(t1, 2, t1, tt0);
uint[] t2 = x2;
- SecP256K1Field.Square(t1, t2);
+ SecP256K1Field.Square(t1, t2, tt0);
return Nat256.Eq(x1, t2) ? new SecP256K1FieldElement(t1) : null;
}
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Point.cs b/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
index 072a0b969..689be0117 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Point.cs
@@ -75,6 +75,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256K1FieldElement Z2 = (SecP256K1FieldElement)b.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat256.CreateExt();
uint[] tt1 = Nat256.CreateExt();
uint[] t2 = Nat256.Create();
uint[] t3 = Nat256.Create();
@@ -90,13 +91,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S2 = t3;
- SecP256K1Field.Square(Z1.x, S2);
+ SecP256K1Field.Square(Z1.x, S2, tt0);
U2 = t2;
- SecP256K1Field.Multiply(S2, X2.x, U2);
+ SecP256K1Field.Multiply(S2, X2.x, U2, tt0);
- SecP256K1Field.Multiply(S2, Z1.x, S2);
- SecP256K1Field.Multiply(S2, Y2.x, S2);
+ SecP256K1Field.Multiply(S2, Z1.x, S2, tt0);
+ SecP256K1Field.Multiply(S2, Y2.x, S2, tt0);
}
bool Z2IsOne = Z2.IsOne;
@@ -109,13 +110,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S1 = t4;
- SecP256K1Field.Square(Z2.x, S1);
+ SecP256K1Field.Square(Z2.x, S1, tt0);
U1 = tt1;
- SecP256K1Field.Multiply(S1, X1.x, U1);
+ SecP256K1Field.Multiply(S1, X1.x, U1, tt0);
- SecP256K1Field.Multiply(S1, Z2.x, S1);
- SecP256K1Field.Multiply(S1, Y1.x, S1);
+ SecP256K1Field.Multiply(S1, Z2.x, S1, tt0);
+ SecP256K1Field.Multiply(S1, Y1.x, S1, tt0);
}
uint[] H = Nat256.Create();
@@ -138,13 +139,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
uint[] HSquared = t3;
- SecP256K1Field.Square(H, HSquared);
+ SecP256K1Field.Square(H, HSquared, tt0);
uint[] G = Nat256.Create();
- SecP256K1Field.Multiply(HSquared, H, G);
+ SecP256K1Field.Multiply(HSquared, H, G, tt0);
uint[] V = t3;
- SecP256K1Field.Multiply(HSquared, U1, V);
+ SecP256K1Field.Multiply(HSquared, U1, V, tt0);
SecP256K1Field.Negate(G, G);
Nat256.Mul(S1, G, tt1);
@@ -153,7 +154,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256K1Field.Reduce32(c, G);
SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
- SecP256K1Field.Square(R, X3.x);
+ SecP256K1Field.Square(R, X3.x, tt0);
SecP256K1Field.Subtract(X3.x, G, X3.x);
SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
@@ -164,11 +165,11 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
if (!Z1IsOne)
{
- SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
if (!Z2IsOne)
{
- SecP256K1Field.Multiply(Z3.x, Z2.x, Z3.x);
+ SecP256K1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
@@ -190,20 +191,21 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat256.CreateExt();
uint[] Y1Squared = Nat256.Create();
- SecP256K1Field.Square(Y1.x, Y1Squared);
+ SecP256K1Field.Square(Y1.x, Y1Squared, tt0);
uint[] T = Nat256.Create();
- SecP256K1Field.Square(Y1Squared, T);
+ SecP256K1Field.Square(Y1Squared, T, tt0);
uint[] M = Nat256.Create();
- SecP256K1Field.Square(X1.x, M);
+ SecP256K1Field.Square(X1.x, M, tt0);
c = Nat256.AddBothTo(M, M, M);
SecP256K1Field.Reduce32(c, M);
uint[] S = Y1Squared;
- SecP256K1Field.Multiply(Y1Squared, X1.x, S);
+ SecP256K1Field.Multiply(Y1Squared, X1.x, S, tt0);
c = Nat.ShiftUpBits(8, S, 2, 0);
SecP256K1Field.Reduce32(c, S);
@@ -212,20 +214,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256K1Field.Reduce32(c, t1);
SecP256K1FieldElement X3 = new SecP256K1FieldElement(T);
- SecP256K1Field.Square(M, X3.x);
+ SecP256K1Field.Square(M, X3.x, tt0);
SecP256K1Field.Subtract(X3.x, S, X3.x);
SecP256K1Field.Subtract(X3.x, S, X3.x);
SecP256K1FieldElement Y3 = new SecP256K1FieldElement(S);
SecP256K1Field.Subtract(S, X3.x, Y3.x);
- SecP256K1Field.Multiply(Y3.x, M, Y3.x);
+ SecP256K1Field.Multiply(Y3.x, M, Y3.x, tt0);
SecP256K1Field.Subtract(Y3.x, t1, Y3.x);
SecP256K1FieldElement Z3 = new SecP256K1FieldElement(M);
SecP256K1Field.Twice(Y1.x, Z3.x);
if (!Z1.IsOne)
{
- SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
return new SecP256K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index a1079f39c..10465dc6f 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -91,6 +91,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
+ {
+ Nat256.Mul(x, y, tt);
+ Reduce(tt, z);
+ }
+
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
{
uint c = Nat256.MulAddTo(x, y, zz);
@@ -237,6 +243,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Square(uint[] x, uint[] z, uint[] tt)
+ {
+ Nat256.Square(x, tt);
+ Reduce(tt, z);
+ }
+
public static void SquareN(uint[] x, int n, uint[] z)
{
Debug.Assert(n > 0);
@@ -252,6 +264,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
}
+ public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
+ {
+ Debug.Assert(n > 0);
+
+ Nat256.Square(x, tt);
+ Reduce(tt, z);
+
+ while (--n > 0)
+ {
+ Nat256.Square(z, tt);
+ Reduce(tt, z);
+ }
+ }
+
public static void Subtract(uint[] x, uint[] y, uint[] z)
{
int c = Nat256.Sub(x, y, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
index bee322091..928461ec6 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
@@ -133,32 +133,33 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
return this;
+ uint[] tt0 = Nat256.CreateExt();
uint[] t1 = Nat256.Create();
uint[] t2 = Nat256.Create();
- SecP256R1Field.Square(x1, t1);
- SecP256R1Field.Multiply(t1, x1, t1);
+ SecP256R1Field.Square(x1, t1, tt0);
+ SecP256R1Field.Multiply(t1, x1, t1, tt0);
- SecP256R1Field.SquareN(t1, 2, t2);
- SecP256R1Field.Multiply(t2, t1, t2);
+ SecP256R1Field.SquareN(t1, 2, t2, tt0);
+ SecP256R1Field.Multiply(t2, t1, t2, tt0);
- SecP256R1Field.SquareN(t2, 4, t1);
- SecP256R1Field.Multiply(t1, t2, t1);
+ SecP256R1Field.SquareN(t2, 4, t1, tt0);
+ SecP256R1Field.Multiply(t1, t2, t1, tt0);
- SecP256R1Field.SquareN(t1, 8, t2);
- SecP256R1Field.Multiply(t2, t1, t2);
+ SecP256R1Field.SquareN(t1, 8, t2, tt0);
+ SecP256R1Field.Multiply(t2, t1, t2, tt0);
- SecP256R1Field.SquareN(t2, 16, t1);
- SecP256R1Field.Multiply(t1, t2, t1);
+ SecP256R1Field.SquareN(t2, 16, t1, tt0);
+ SecP256R1Field.Multiply(t1, t2, t1, tt0);
- SecP256R1Field.SquareN(t1, 32, t1);
- SecP256R1Field.Multiply(t1, x1, t1);
+ SecP256R1Field.SquareN(t1, 32, t1, tt0);
+ SecP256R1Field.Multiply(t1, x1, t1, tt0);
- SecP256R1Field.SquareN(t1, 96, t1);
- SecP256R1Field.Multiply(t1, x1, t1);
+ SecP256R1Field.SquareN(t1, 96, t1, tt0);
+ SecP256R1Field.Multiply(t1, x1, t1, tt0);
- SecP256R1Field.SquareN(t1, 94, t1);
- SecP256R1Field.Multiply(t1, t1, t2);
+ SecP256R1Field.SquareN(t1, 94, t1, tt0);
+ SecP256R1Field.Multiply(t1, t1, t2, tt0);
return Nat256.Eq(x1, t2) ? new SecP256R1FieldElement(t1) : null;
}
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Point.cs b/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
index 83320824d..d487aad87 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Point.cs
@@ -74,6 +74,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256R1FieldElement Z2 = (SecP256R1FieldElement)b.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat256.CreateExt();
uint[] tt1 = Nat256.CreateExt();
uint[] t2 = Nat256.Create();
uint[] t3 = Nat256.Create();
@@ -89,13 +90,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S2 = t3;
- SecP256R1Field.Square(Z1.x, S2);
+ SecP256R1Field.Square(Z1.x, S2, tt0);
U2 = t2;
- SecP256R1Field.Multiply(S2, X2.x, U2);
+ SecP256R1Field.Multiply(S2, X2.x, U2, tt0);
- SecP256R1Field.Multiply(S2, Z1.x, S2);
- SecP256R1Field.Multiply(S2, Y2.x, S2);
+ SecP256R1Field.Multiply(S2, Z1.x, S2, tt0);
+ SecP256R1Field.Multiply(S2, Y2.x, S2, tt0);
}
bool Z2IsOne = Z2.IsOne;
@@ -108,13 +109,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S1 = t4;
- SecP256R1Field.Square(Z2.x, S1);
+ SecP256R1Field.Square(Z2.x, S1, tt0);
U1 = tt1;
- SecP256R1Field.Multiply(S1, X1.x, U1);
+ SecP256R1Field.Multiply(S1, X1.x, U1, tt0);
- SecP256R1Field.Multiply(S1, Z2.x, S1);
- SecP256R1Field.Multiply(S1, Y1.x, S1);
+ SecP256R1Field.Multiply(S1, Z2.x, S1, tt0);
+ SecP256R1Field.Multiply(S1, Y1.x, S1, tt0);
}
uint[] H = Nat256.Create();
@@ -137,13 +138,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
uint[] HSquared = t3;
- SecP256R1Field.Square(H, HSquared);
+ SecP256R1Field.Square(H, HSquared, tt0);
uint[] G = Nat256.Create();
- SecP256R1Field.Multiply(HSquared, H, G);
+ SecP256R1Field.Multiply(HSquared, H, G, tt0);
uint[] V = t3;
- SecP256R1Field.Multiply(HSquared, U1, V);
+ SecP256R1Field.Multiply(HSquared, U1, V, tt0);
SecP256R1Field.Negate(G, G);
Nat256.Mul(S1, G, tt1);
@@ -152,7 +153,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256R1Field.Reduce32(c, G);
SecP256R1FieldElement X3 = new SecP256R1FieldElement(t4);
- SecP256R1Field.Square(R, X3.x);
+ SecP256R1Field.Square(R, X3.x, tt0);
SecP256R1Field.Subtract(X3.x, G, X3.x);
SecP256R1FieldElement Y3 = new SecP256R1FieldElement(G);
@@ -163,11 +164,11 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256R1FieldElement Z3 = new SecP256R1FieldElement(H);
if (!Z1IsOne)
{
- SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
if (!Z2IsOne)
{
- SecP256R1Field.Multiply(Z3.x, Z2.x, Z3.x);
+ SecP256R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
}
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
@@ -189,14 +190,15 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat256.CreateExt();
uint[] t1 = Nat256.Create();
uint[] t2 = Nat256.Create();
uint[] Y1Squared = Nat256.Create();
- SecP256R1Field.Square(Y1.x, Y1Squared);
+ SecP256R1Field.Square(Y1.x, Y1Squared, tt0);
uint[] T = Nat256.Create();
- SecP256R1Field.Square(Y1Squared, T);
+ SecP256R1Field.Square(Y1Squared, T, tt0);
bool Z1IsOne = Z1.IsOne;
@@ -204,19 +206,19 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (!Z1IsOne)
{
Z1Squared = t2;
- SecP256R1Field.Square(Z1.x, Z1Squared);
+ SecP256R1Field.Square(Z1.x, Z1Squared, tt0);
}
SecP256R1Field.Subtract(X1.x, Z1Squared, t1);
uint[] M = t2;
SecP256R1Field.Add(X1.x, Z1Squared, M);
- SecP256R1Field.Multiply(M, t1, M);
+ SecP256R1Field.Multiply(M, t1, M, tt0);
c = Nat256.AddBothTo(M, M, M);
SecP256R1Field.Reduce32(c, M);
uint[] S = Y1Squared;
- SecP256R1Field.Multiply(Y1Squared, X1.x, S);
+ SecP256R1Field.Multiply(Y1Squared, X1.x, S, tt0);
c = Nat.ShiftUpBits(8, S, 2, 0);
SecP256R1Field.Reduce32(c, S);
@@ -224,20 +226,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP256R1Field.Reduce32(c, t1);
SecP256R1FieldElement X3 = new SecP256R1FieldElement(T);
- SecP256R1Field.Square(M, X3.x);
+ SecP256R1Field.Square(M, X3.x, tt0);
SecP256R1Field.Subtract(X3.x, S, X3.x);
SecP256R1Field.Subtract(X3.x, S, X3.x);
SecP256R1FieldElement Y3 = new SecP256R1FieldElement(S);
SecP256R1Field.Subtract(S, X3.x, Y3.x);
- SecP256R1Field.Multiply(Y3.x, M, Y3.x);
+ SecP256R1Field.Multiply(Y3.x, M, Y3.x, tt0);
SecP256R1Field.Subtract(Y3.x, t1, Y3.x);
SecP256R1FieldElement Z3 = new SecP256R1FieldElement(M);
SecP256R1Field.Twice(Y1.x, Z3.x);
if (!Z1IsOne)
{
- SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
return new SecP256R1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 }, IsCompressed);
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1Field.cs b/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
index cddb46895..16b60af55 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
@@ -98,6 +98,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
+ {
+ Nat384.Mul(x, y, tt);
+ Reduce(tt, z);
+ }
+
public static void Negate(uint[] x, uint[] z)
{
if (0 != IsZero(x))
@@ -234,6 +240,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Square(uint[] x, uint[] z, uint[] tt)
+ {
+ Nat384.Square(x, tt);
+ Reduce(tt, z);
+ }
+
public static void SquareN(uint[] x, int n, uint[] z)
{
Debug.Assert(n > 0);
@@ -249,6 +261,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
}
+ public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
+ {
+ Debug.Assert(n > 0);
+
+ Nat384.Square(x, tt);
+ Reduce(tt, z);
+
+ while (--n > 0)
+ {
+ Nat384.Square(z, tt);
+ Reduce(tt, z);
+ }
+ }
+
public static void Subtract(uint[] x, uint[] y, uint[] z)
{
int c = Nat.Sub(12, x, y, z);
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
index 3fc1fb851..d190c4ae9 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
@@ -133,54 +133,55 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (Nat.IsZero(12, x1) || Nat.IsOne(12, x1))
return this;
+ uint[] tt0 = Nat.Create(24);
uint[] t1 = Nat.Create(12);
uint[] t2 = Nat.Create(12);
uint[] t3 = Nat.Create(12);
uint[] t4 = Nat.Create(12);
- SecP384R1Field.Square(x1, t1);
- SecP384R1Field.Multiply(t1, x1, t1);
+ SecP384R1Field.Square(x1, t1, tt0);
+ SecP384R1Field.Multiply(t1, x1, t1, tt0);
- SecP384R1Field.SquareN(t1, 2, t2);
- SecP384R1Field.Multiply(t2, t1, t2);
+ SecP384R1Field.SquareN(t1, 2, t2, tt0);
+ SecP384R1Field.Multiply(t2, t1, t2, tt0);
- SecP384R1Field.Square(t2, t2);
- SecP384R1Field.Multiply(t2, x1, t2);
+ SecP384R1Field.Square(t2, t2, tt0);
+ SecP384R1Field.Multiply(t2, x1, t2, tt0);
- SecP384R1Field.SquareN(t2, 5, t3);
- SecP384R1Field.Multiply(t3, t2, t3);
+ SecP384R1Field.SquareN(t2, 5, t3, tt0);
+ SecP384R1Field.Multiply(t3, t2, t3, tt0);
- SecP384R1Field.SquareN(t3, 5, t4);
- SecP384R1Field.Multiply(t4, t2, t4);
+ SecP384R1Field.SquareN(t3, 5, t4, tt0);
+ SecP384R1Field.Multiply(t4, t2, t4, tt0);
- SecP384R1Field.SquareN(t4, 15, t2);
- SecP384R1Field.Multiply(t2, t4, t2);
+ SecP384R1Field.SquareN(t4, 15, t2, tt0);
+ SecP384R1Field.Multiply(t2, t4, t2, tt0);
- SecP384R1Field.SquareN(t2, 2, t3);
- SecP384R1Field.Multiply(t1, t3, t1);
+ SecP384R1Field.SquareN(t2, 2, t3, tt0);
+ SecP384R1Field.Multiply(t1, t3, t1, tt0);
- SecP384R1Field.SquareN(t3, 28, t3);
- SecP384R1Field.Multiply(t2, t3, t2);
+ SecP384R1Field.SquareN(t3, 28, t3, tt0);
+ SecP384R1Field.Multiply(t2, t3, t2, tt0);
- SecP384R1Field.SquareN(t2, 60, t3);
- SecP384R1Field.Multiply(t3, t2, t3);
+ SecP384R1Field.SquareN(t2, 60, t3, tt0);
+ SecP384R1Field.Multiply(t3, t2, t3, tt0);
uint[] r = t2;
- SecP384R1Field.SquareN(t3, 120, r);
- SecP384R1Field.Multiply(r, t3, r);
+ SecP384R1Field.SquareN(t3, 120, r, tt0);
+ SecP384R1Field.Multiply(r, t3, r, tt0);
- SecP384R1Field.SquareN(r, 15, r);
- SecP384R1Field.Multiply(r, t4, r);
+ SecP384R1Field.SquareN(r, 15, r, tt0);
+ SecP384R1Field.Multiply(r, t4, r, tt0);
- SecP384R1Field.SquareN(r, 33, r);
- SecP384R1Field.Multiply(r, t1, r);
+ SecP384R1Field.SquareN(r, 33, r, tt0);
+ SecP384R1Field.Multiply(r, t1, r, tt0);
- SecP384R1Field.SquareN(r, 64, r);
- SecP384R1Field.Multiply(r, x1, r);
+ SecP384R1Field.SquareN(r, 64, r, tt0);
+ SecP384R1Field.Multiply(r, x1, r, tt0);
- SecP384R1Field.SquareN(r, 30, t1);
- SecP384R1Field.Square(t1, t2);
+ SecP384R1Field.SquareN(r, 30, t1, tt0);
+ SecP384R1Field.Square(t1, t2, tt0);
return Nat.Eq(12, x1, t2) ? new SecP384R1FieldElement(t1) : null;
}
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1Point.cs b/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
index 83159ce61..c2c364be1 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1Point.cs
@@ -74,6 +74,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP384R1FieldElement Z2 = (SecP384R1FieldElement)b.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat.Create(24);
uint[] tt1 = Nat.Create(24);
uint[] tt2 = Nat.Create(24);
uint[] t3 = Nat.Create(12);
@@ -89,13 +90,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S2 = t3;
- SecP384R1Field.Square(Z1.x, S2);
+ SecP384R1Field.Square(Z1.x, S2, tt0);
U2 = tt2;
- SecP384R1Field.Multiply(S2, X2.x, U2);
+ SecP384R1Field.Multiply(S2, X2.x, U2, tt0);
- SecP384R1Field.Multiply(S2, Z1.x, S2);
- SecP384R1Field.Multiply(S2, Y2.x, S2);
+ SecP384R1Field.Multiply(S2, Z1.x, S2, tt0);
+ SecP384R1Field.Multiply(S2, Y2.x, S2, tt0);
}
bool Z2IsOne = Z2.IsOne;
@@ -108,13 +109,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S1 = t4;
- SecP384R1Field.Square(Z2.x, S1);
+ SecP384R1Field.Square(Z2.x, S1, tt0);
U1 = tt1;
- SecP384R1Field.Multiply(S1, X1.x, U1);
+ SecP384R1Field.Multiply(S1, X1.x, U1, tt0);
- SecP384R1Field.Multiply(S1, Z2.x, S1);
- SecP384R1Field.Multiply(S1, Y1.x, S1);
+ SecP384R1Field.Multiply(S1, Z2.x, S1, tt0);
+ SecP384R1Field.Multiply(S1, Y1.x, S1, tt0);
}
uint[] H = Nat.Create(12);
@@ -137,13 +138,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
uint[] HSquared = t3;
- SecP384R1Field.Square(H, HSquared);
+ SecP384R1Field.Square(H, HSquared, tt0);
uint[] G = Nat.Create(12);
- SecP384R1Field.Multiply(HSquared, H, G);
+ SecP384R1Field.Multiply(HSquared, H, G, tt0);
uint[] V = t3;
- SecP384R1Field.Multiply(HSquared, U1, V);
+ SecP384R1Field.Multiply(HSquared, U1, V, tt0);
SecP384R1Field.Negate(G, G);
Nat384.Mul(S1, G, tt1);
@@ -152,7 +153,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP384R1Field.Reduce32(c, G);
SecP384R1FieldElement X3 = new SecP384R1FieldElement(t4);
- SecP384R1Field.Square(R, X3.x);
+ SecP384R1Field.Square(R, X3.x, tt0);
SecP384R1Field.Subtract(X3.x, G, X3.x);
SecP384R1FieldElement Y3 = new SecP384R1FieldElement(G);
@@ -164,11 +165,11 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP384R1FieldElement Z3 = new SecP384R1FieldElement(H);
if (!Z1IsOne)
{
- SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
if (!Z2IsOne)
{
- SecP384R1Field.Multiply(Z3.x, Z2.x, Z3.x);
+ SecP384R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
@@ -190,14 +191,15 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
uint c;
+ uint[] tt0 = Nat.Create(24);
uint[] t1 = Nat.Create(12);
uint[] t2 = Nat.Create(12);
uint[] Y1Squared = Nat.Create(12);
- SecP384R1Field.Square(Y1.x, Y1Squared);
+ SecP384R1Field.Square(Y1.x, Y1Squared, tt0);
uint[] T = Nat.Create(12);
- SecP384R1Field.Square(Y1Squared, T);
+ SecP384R1Field.Square(Y1Squared, T, tt0);
bool Z1IsOne = Z1.IsOne;
@@ -205,19 +207,19 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (!Z1IsOne)
{
Z1Squared = t2;
- SecP384R1Field.Square(Z1.x, Z1Squared);
+ SecP384R1Field.Square(Z1.x, Z1Squared, tt0);
}
SecP384R1Field.Subtract(X1.x, Z1Squared, t1);
uint[] M = t2;
SecP384R1Field.Add(X1.x, Z1Squared, M);
- SecP384R1Field.Multiply(M, t1, M);
+ SecP384R1Field.Multiply(M, t1, M, tt0);
c = Nat.AddBothTo(12, M, M, M);
SecP384R1Field.Reduce32(c, M);
uint[] S = Y1Squared;
- SecP384R1Field.Multiply(Y1Squared, X1.x, S);
+ SecP384R1Field.Multiply(Y1Squared, X1.x, S, tt0);
c = Nat.ShiftUpBits(12, S, 2, 0);
SecP384R1Field.Reduce32(c, S);
@@ -225,20 +227,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP384R1Field.Reduce32(c, t1);
SecP384R1FieldElement X3 = new SecP384R1FieldElement(T);
- SecP384R1Field.Square(M, X3.x);
+ SecP384R1Field.Square(M, X3.x, tt0);
SecP384R1Field.Subtract(X3.x, S, X3.x);
SecP384R1Field.Subtract(X3.x, S, X3.x);
SecP384R1FieldElement Y3 = new SecP384R1FieldElement(S);
SecP384R1Field.Subtract(S, X3.x, Y3.x);
- SecP384R1Field.Multiply(Y3.x, M, Y3.x);
+ SecP384R1Field.Multiply(Y3.x, M, Y3.x, tt0);
SecP384R1Field.Subtract(Y3.x, t1, Y3.x);
SecP384R1FieldElement Z3 = new SecP384R1FieldElement(M);
SecP384R1Field.Twice(Y1.x, Z3.x);
if (!Z1IsOne)
{
- SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
return new SecP384R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
index 0f1922f36..293c7abf8 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
@@ -77,6 +77,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
+ {
+ ImplMultiply(x, y, tt);
+ Reduce(tt, z);
+ }
+
public static void Negate(uint[] x, uint[] z)
{
if (0 != IsZero(x))
@@ -144,9 +150,16 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
Reduce(tt, z);
}
+ public static void Square(uint[] x, uint[] z, uint[] tt)
+ {
+ ImplSquare(x, tt);
+ Reduce(tt, z);
+ }
+
public static void SquareN(uint[] x, int n, uint[] z)
{
Debug.Assert(n > 0);
+
uint[] tt = Nat.Create(33);
ImplSquare(x, tt);
Reduce(tt, z);
@@ -158,6 +171,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
}
+ public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
+ {
+ Debug.Assert(n > 0);
+
+ ImplSquare(x, tt);
+ Reduce(tt, z);
+
+ while (--n > 0)
+ {
+ ImplSquare(z, tt);
+ Reduce(tt, z);
+ }
+ }
+
public static void Subtract(uint[] x, uint[] y, uint[] z)
{
int c = Nat.Sub(16, x, y, z) + (int)(x[16] - y[16]);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
index adfc89e9d..409352586 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
@@ -133,11 +133,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (Nat.IsZero(17, x1) || Nat.IsOne(17, x1))
return this;
+ uint[] tt0 = Nat.Create(33);
uint[] t1 = Nat.Create(17);
uint[] t2 = Nat.Create(17);
- SecP521R1Field.SquareN(x1, 519, t1);
- SecP521R1Field.Square(t1, t2);
+ SecP521R1Field.SquareN(x1, 519, t1, tt0);
+ SecP521R1Field.Square(t1, t2, tt0);
return Nat.Eq(17, x1, t2) ? new SecP521R1FieldElement(t1) : null;
}
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Point.cs b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
index 7ad97f76f..7fc1927cf 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
@@ -73,6 +73,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP521R1FieldElement Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
SecP521R1FieldElement Z2 = (SecP521R1FieldElement)b.RawZCoords[0];
+ uint[] tt0 = Nat.Create(33);
uint[] t1 = Nat.Create(17);
uint[] t2 = Nat.Create(17);
uint[] t3 = Nat.Create(17);
@@ -88,13 +89,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S2 = t3;
- SecP521R1Field.Square(Z1.x, S2);
+ SecP521R1Field.Square(Z1.x, S2, tt0);
U2 = t2;
- SecP521R1Field.Multiply(S2, X2.x, U2);
+ SecP521R1Field.Multiply(S2, X2.x, U2, tt0);
- SecP521R1Field.Multiply(S2, Z1.x, S2);
- SecP521R1Field.Multiply(S2, Y2.x, S2);
+ SecP521R1Field.Multiply(S2, Z1.x, S2, tt0);
+ SecP521R1Field.Multiply(S2, Y2.x, S2, tt0);
}
bool Z2IsOne = Z2.IsOne;
@@ -107,13 +108,13 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
else
{
S1 = t4;
- SecP521R1Field.Square(Z2.x, S1);
+ SecP521R1Field.Square(Z2.x, S1, tt0);
U1 = t1;
- SecP521R1Field.Multiply(S1, X1.x, U1);
+ SecP521R1Field.Multiply(S1, X1.x, U1, tt0);
- SecP521R1Field.Multiply(S1, Z2.x, S1);
- SecP521R1Field.Multiply(S1, Y1.x, S1);
+ SecP521R1Field.Multiply(S1, Z2.x, S1, tt0);
+ SecP521R1Field.Multiply(S1, Y1.x, S1, tt0);
}
uint[] H = Nat.Create(17);
@@ -136,35 +137,35 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
}
uint[] HSquared = t3;
- SecP521R1Field.Square(H, HSquared);
+ SecP521R1Field.Square(H, HSquared, tt0);
uint[] G = Nat.Create(17);
- SecP521R1Field.Multiply(HSquared, H, G);
+ SecP521R1Field.Multiply(HSquared, H, G, tt0);
uint[] V = t3;
- SecP521R1Field.Multiply(HSquared, U1, V);
+ SecP521R1Field.Multiply(HSquared, U1, V, tt0);
- SecP521R1Field.Multiply(S1, G, t1);
+ SecP521R1Field.Multiply(S1, G, t1, tt0);
SecP521R1FieldElement X3 = new SecP521R1FieldElement(t4);
- SecP521R1Field.Square(R, X3.x);
+ SecP521R1Field.Square(R, X3.x, tt0);
SecP521R1Field.Add(X3.x, G, X3.x);
SecP521R1Field.Subtract(X3.x, V, X3.x);
SecP521R1Field.Subtract(X3.x, V, X3.x);
SecP521R1FieldElement Y3 = new SecP521R1FieldElement(G);
SecP521R1Field.Subtract(V, X3.x, Y3.x);
- SecP521R1Field.Multiply(Y3.x, R, t2);
+ SecP521R1Field.Multiply(Y3.x, R, t2, tt0);
SecP521R1Field.Subtract(t2, t1, Y3.x);
SecP521R1FieldElement Z3 = new SecP521R1FieldElement(H);
if (!Z1IsOne)
{
- SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
if (!Z2IsOne)
{
- SecP521R1Field.Multiply(Z3.x, Z2.x, Z3.x);
+ SecP521R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
@@ -185,14 +186,15 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP521R1FieldElement X1 = (SecP521R1FieldElement)this.RawXCoord, Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
+ uint[] tt0 = Nat.Create(33);
uint[] t1 = Nat.Create(17);
uint[] t2 = Nat.Create(17);
uint[] Y1Squared = Nat.Create(17);
- SecP521R1Field.Square(Y1.x, Y1Squared);
+ SecP521R1Field.Square(Y1.x, Y1Squared, tt0);
uint[] T = Nat.Create(17);
- SecP521R1Field.Square(Y1Squared, T);
+ SecP521R1Field.Square(Y1Squared, T, tt0);
bool Z1IsOne = Z1.IsOne;
@@ -200,19 +202,19 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
if (!Z1IsOne)
{
Z1Squared = t2;
- SecP521R1Field.Square(Z1.x, Z1Squared);
+ SecP521R1Field.Square(Z1.x, Z1Squared, tt0);
}
SecP521R1Field.Subtract(X1.x, Z1Squared, t1);
uint[] M = t2;
SecP521R1Field.Add(X1.x, Z1Squared, M);
- SecP521R1Field.Multiply(M, t1, M);
+ SecP521R1Field.Multiply(M, t1, M, tt0);
Nat.AddBothTo(17, M, M, M);
SecP521R1Field.Reduce23(M);
uint[] S = Y1Squared;
- SecP521R1Field.Multiply(Y1Squared, X1.x, S);
+ SecP521R1Field.Multiply(Y1Squared, X1.x, S, tt0);
Nat.ShiftUpBits(17, S, 2, 0);
SecP521R1Field.Reduce23(S);
@@ -220,20 +222,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
SecP521R1Field.Reduce23(t1);
SecP521R1FieldElement X3 = new SecP521R1FieldElement(T);
- SecP521R1Field.Square(M, X3.x);
+ SecP521R1Field.Square(M, X3.x, tt0);
SecP521R1Field.Subtract(X3.x, S, X3.x);
SecP521R1Field.Subtract(X3.x, S, X3.x);
SecP521R1FieldElement Y3 = new SecP521R1FieldElement(S);
SecP521R1Field.Subtract(S, X3.x, Y3.x);
- SecP521R1Field.Multiply(Y3.x, M, Y3.x);
+ SecP521R1Field.Multiply(Y3.x, M, Y3.x, tt0);
SecP521R1Field.Subtract(Y3.x, t1, Y3.x);
SecP521R1FieldElement Z3 = new SecP521R1FieldElement(M);
SecP521R1Field.Twice(Y1.x, Z3.x);
if (!Z1IsOne)
{
- SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+ SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
}
return new SecP521R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
|