1818import org .bouncycastle .math .ec .ECAlgorithms ;
1919import org .bouncycastle .math .ec .ECCurve ;
2020import org .bouncycastle .math .ec .ECPoint ;
21+ import org .bouncycastle .math .ec .FixedPointCombMultiplier ;
22+ import org .bouncycastle .util .BigIntegers ;
2123
2224public 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