Skip to content

Commit b1c75bf

Browse files
committed
Add ECAlgorithms.sumOfTwoMultipliesSecret placeholders
- improve ECAlgorithmsTest
1 parent a0aba13 commit b1c75bf

4 files changed

Lines changed: 82 additions & 16 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/agreement/ECMQVBasicAgreement.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ private static ECPoint calculateMqvAgreement(
104104
* depends on them. Two constant-time multiplications and an addition instead; that gives
105105
* up Shamir's trick, so this is roughly twice the point arithmetic it was.
106106
*/
107-
ECPoint p1 = ECAlgorithms.multiplySecret(q1v, Q2VBar.multiply(hs).mod(n), n);
108-
ECPoint p2 = ECAlgorithms.multiplySecret(q2v, hs, n);
109-
110-
return p1.add(p2);
107+
return ECAlgorithms.sumOfTwoMultipliesSecret(q1v, Q2VBar.multiply(hs).mod(n), q2v, hs, n);
111108
}
112109
}

‎core/src/main/java/org/bouncycastle/crypto/agreement/SM2KeyExchange.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ private ECPoint calculateU(SM2KeyExchangePublicParameters otherPub)
177177
* depends on them. Two constant-time multiplications and an addition instead; that gives
178178
* up Shamir's trick, so this is roughly twice the point arithmetic it was.
179179
*/
180-
ECPoint u1 = ECAlgorithms.multiplySecret(p1, k1, ecParams.getN());
181-
ECPoint u2 = ECAlgorithms.multiplySecret(p2, k2, ecParams.getN());
182-
183-
return u1.add(u2).normalize();
180+
return ECAlgorithms.sumOfTwoMultipliesSecret(p1, k1, p2, k2, ecParams.getN()).normalize();
184181
}
185182

186183
private byte[] kdf(ECPoint u, byte[] za, byte[] zb, int klen)

‎core/src/main/java/org/bouncycastle/math/ec/ECAlgorithms.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ public static ECPoint sumOfTwoMultiplies(ECPoint P, BigInteger a,
9595
return implCheckResult(implShamirsTrickWNaf(P, a, Q, b));
9696
}
9797

98+
public static ECPoint sumOfTwoMultipliesSecret(ECPoint P, BigInteger a, ECPoint Q, BigInteger b)
99+
{
100+
// TODO[ec] Replace with constant-time Straus ladder
101+
ECPoint R1 = multiplySecret(P, a);
102+
ECPoint R2 = multiplySecret(Q, b);
103+
return R1.add(R2);
104+
}
105+
106+
public static ECPoint sumOfTwoMultipliesSecret(ECPoint P, BigInteger a, ECPoint Q, BigInteger b, BigInteger order)
107+
{
108+
// TODO[ec] Replace with constant-time Straus ladder
109+
ECPoint R1 = multiplySecret(P, a, order);
110+
ECPoint R2 = multiplySecret(Q, b, order);
111+
return R1.add(R2);
112+
}
113+
98114
/*
99115
* "Shamir's Trick", originally due to E. G. Straus
100116
* (Addition chains of vectors. American Mathematical Monthly,
@@ -210,6 +226,11 @@ public static ECPoint referenceMultiply(ECPoint p, BigInteger k)
210226
return k.signum() < 0 ? q.negate() : q;
211227
}
212228

229+
public static ECPoint multiply(ECPoint p, BigInteger k)
230+
{
231+
return p.multiply(k);
232+
}
233+
213234
/**
214235
* Multiply a point by a scalar that must not be leaked - a private key, or any value derived
215236
* from one - without exposing the scalar through control flow or memory-access pattern.

‎core/src/test/java/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java‎

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,33 @@
1818
import org.bouncycastle.math.ec.ECAlgorithms;
1919
import org.bouncycastle.math.ec.ECCurve;
2020
import org.bouncycastle.math.ec.ECPoint;
21+
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
22+
import org.bouncycastle.util.BigIntegers;
2123

2224
public class ECAlgorithmsTest extends TestCase
2325
{
2426
private static final int SCALE = 4;
2527
private static final SecureRandom RND = new SecureRandom();
2628

29+
public void testMultiply()
30+
{
31+
X9ECParameters x9 = CustomNamedCurves.getByName("secp256r1");
32+
assertNotNull(x9);
33+
doTestMultiply(x9);
34+
}
35+
36+
// TODO Ideally, mark this test not to run by default
37+
public void testMultiplyComplete()
38+
{
39+
List x9s = getTestCurves();
40+
Iterator it = x9s.iterator();
41+
while (it.hasNext())
42+
{
43+
X9ECParameters x9 = (X9ECParameters)it.next();
44+
doTestMultiply(x9);
45+
}
46+
}
47+
2748
public void testSumOfMultiplies()
2849
{
2950
X9ECParameters x9 = CustomNamedCurves.getByName("secp256r1");
@@ -62,6 +83,27 @@ public void testSumOfTwoMultipliesComplete()
6283
}
6384
}
6485

86+
private void doTestMultiply(X9ECParameters x9)
87+
{
88+
for (int i = 0; i < SCALE; ++i)
89+
{
90+
ECPoint p = getRandomPoint(x9);
91+
BigInteger a = getRandomScalar(x9);
92+
93+
ECPoint u = referenceMultiply(p, a);
94+
ECPoint v1 = ECAlgorithms.multiply(p, a);
95+
ECPoint v2 = ECAlgorithms.multiplySecret(p, a);
96+
ECPoint v3 = ECAlgorithms.multiplySecret(p, a, x9.getN());
97+
98+
ECPoint[] results = new ECPoint[]{ u, v1, v2, v3 };
99+
x9.getCurve().normalizeAll(results);
100+
101+
assertPointsEqual("ECAlgorithms.multiply is incorrect", results[0], results[1]);
102+
assertPointsEqual("ECAlgorithms.multiplySecret is incorrect", results[0], results[2]);
103+
assertPointsEqual("ECAlgorithms.multiplySecret (w/ order) is incorrect", results[0], results[3]);
104+
}
105+
}
106+
65107
private void doTestSumOfMultiplies(X9ECParameters x9)
66108
{
67109
ECPoint[] points = new ECPoint[SCALE];
@@ -75,7 +117,7 @@ private void doTestSumOfMultiplies(X9ECParameters x9)
75117
ECPoint u = x9.getCurve().getInfinity();
76118
for (int i = 0; i < SCALE; ++i)
77119
{
78-
u = u.add(points[i].multiply(scalars[i]));
120+
u = u.add(referenceMultiply(points[i], scalars[i]));
79121

80122
ECPoint v = ECAlgorithms.sumOfMultiplies(copyPoints(points, i + 1), copyScalars(scalars, i + 1));
81123

@@ -95,16 +137,20 @@ private void doTestSumOfTwoMultiplies(X9ECParameters x9)
95137
{
96138
ECPoint q = getRandomPoint(x9);
97139
BigInteger b = getRandomScalar(x9);
98-
99-
ECPoint u = p.multiply(a).add(q.multiply(b));
100-
ECPoint v = ECAlgorithms.shamirsTrick(p, a, q, b);
101-
ECPoint w = ECAlgorithms.sumOfTwoMultiplies(p, a, q, b);
102140

103-
ECPoint[] results = new ECPoint[]{ u, v, w };
141+
ECPoint u = referenceMultiply(p, a).add(referenceMultiply(q, b));
142+
ECPoint v1 = ECAlgorithms.shamirsTrick(p, a, q, b);
143+
ECPoint v2 = ECAlgorithms.sumOfTwoMultiplies(p, a, q, b);
144+
ECPoint v3 = ECAlgorithms.sumOfTwoMultipliesSecret(p, a, q, b);
145+
ECPoint v4 = ECAlgorithms.sumOfTwoMultipliesSecret(p, a, q, b, x9.getN());
146+
147+
ECPoint[] results = new ECPoint[]{ u, v1, v2, v3, v4 };
104148
x9.getCurve().normalizeAll(results);
105149

106150
assertPointsEqual("ECAlgorithms.shamirsTrick is incorrect", results[0], results[1]);
107151
assertPointsEqual("ECAlgorithms.sumOfTwoMultiplies is incorrect", results[0], results[2]);
152+
assertPointsEqual("ECAlgorithms.sumOfTwoMultipliesSecret is incorrect", results[0], results[3]);
153+
assertPointsEqual("ECAlgorithms.sumOfTwoMultipliesSecret (w/ order) is incorrect", results[0], results[4]);
108154

109155
p = q;
110156
a = b;
@@ -132,12 +178,12 @@ private BigInteger[] copyScalars(BigInteger[] ks, int len)
132178

133179
private ECPoint getRandomPoint(X9ECParameters x9)
134180
{
135-
return x9.getG().multiply(getRandomScalar(x9));
181+
return new FixedPointCombMultiplier().multiply(x9.getG(), getRandomScalar(x9));
136182
}
137183

138184
private BigInteger getRandomScalar(X9ECParameters x9)
139185
{
140-
return new BigInteger(x9.getN().bitLength(), RND);
186+
return BigIntegers.createRandomBigInteger(x9.getN().bitLength(), RND);
141187
}
142188

143189
private List getTestCurves()
@@ -186,6 +232,11 @@ else if (curve.supportsCoordinateSystem(coord))
186232
}
187233
}
188234

235+
private static ECPoint referenceMultiply(ECPoint p, BigInteger k)
236+
{
237+
return ECAlgorithms.referenceMultiply(p, k);
238+
}
239+
189240
public static Test suite()
190241
{
191242
return new TestSuite(ECAlgorithmsTest.class);

0 commit comments

Comments
 (0)