diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-06-30 18:07:36 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-06-30 18:07:36 +0400 |
commit | d68a9ab9534d1d732058986acc2bbba8b8a6bfae (patch) | |
tree | 81a81c9118fc490d23dbaae20ca6a27d6ec3b838 /core | |
parent | 144d6dbf2df00d3fd98f46838b040b215a995c72 (diff) |
Add test coverage for ECAlgorithms class
Diffstat (limited to 'core')
-rw-r--r-- | core/src/test/java/org/bouncycastle/math/ec/test/AllTests.java | 1 | ||||
-rw-r--r-- | core/src/test/java/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java | 170 |
2 files changed, 171 insertions, 0 deletions
diff --git a/core/src/test/java/org/bouncycastle/math/ec/test/AllTests.java b/core/src/test/java/org/bouncycastle/math/ec/test/AllTests.java index 17eddab2..1101c2dc 100644 --- a/core/src/test/java/org/bouncycastle/math/ec/test/AllTests.java +++ b/core/src/test/java/org/bouncycastle/math/ec/test/AllTests.java @@ -16,6 +16,7 @@ public class AllTests { TestSuite suite = new TestSuite("EC Math tests"); + suite.addTest(ECAlgorithmsTest.suite()); suite.addTest(ECPointTest.suite()); return suite; diff --git a/core/src/test/java/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java b/core/src/test/java/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java new file mode 100644 index 00000000..2699fc34 --- /dev/null +++ b/core/src/test/java/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java @@ -0,0 +1,170 @@ +package org.bouncycastle.math.ec.test; + +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.bouncycastle.asn1.x9.ECNamedCurveTable; +import org.bouncycastle.asn1.x9.X9ECParameters; +import org.bouncycastle.crypto.ec.CustomNamedCurves; +import org.bouncycastle.math.ec.ECAlgorithms; +import org.bouncycastle.math.ec.ECCurve; +import org.bouncycastle.math.ec.ECPoint; + +public class ECAlgorithmsTest extends TestCase +{ + private static final int SCALE = 8; + private static final SecureRandom RND = new SecureRandom(); + + public void testSumOfMultiplies() + { + ArrayList x9s = getTestCurves(); + Iterator it = x9s.iterator(); + while (it.hasNext()) + { + X9ECParameters x9 = (X9ECParameters)it.next(); + + ECPoint[] points = new ECPoint[SCALE]; + BigInteger[] scalars = new BigInteger[SCALE]; + for (int i = 0; i < SCALE; ++i) + { + points[i] = getRandomPoint(x9); + scalars[i] = getRandomScalar(x9); + } + + ECPoint u = x9.getCurve().getInfinity(); + for (int i = 0; i < SCALE; ++i) + { + u = u.add(points[i].multiply(scalars[i])); + + ECPoint v = ECAlgorithms.sumOfMultiplies(copyPoints(points, i + 1), copyScalars(scalars, i + 1)); + + ECPoint[] results = new ECPoint[]{ u, v }; + x9.getCurve().normalizeAll(results); + + assertPointsEqual("ECAlgorithms.sumOfMultiplies is incorrect", results[0], results[1]); + } + } + } + + public void testSumOfTwoMultiplies() + { + ArrayList x9s = getTestCurves(); + Iterator it = x9s.iterator(); + while (it.hasNext()) + { + X9ECParameters x9 = (X9ECParameters)it.next(); + + ECPoint p = getRandomPoint(x9); + BigInteger a = getRandomScalar(x9); + + for (int i = 0; i < SCALE; ++i) + { + ECPoint q = getRandomPoint(x9); + BigInteger b = getRandomScalar(x9); + + ECPoint u = p.multiply(a).add(q.multiply(b)); + ECPoint v = ECAlgorithms.shamirsTrick(p, a, q, b); + ECPoint w = ECAlgorithms.sumOfTwoMultiplies(p, a, q, b); + + ECPoint[] results = new ECPoint[]{ u, v, w }; + x9.getCurve().normalizeAll(results); + + assertPointsEqual("ECAlgorithms.shamirsTrick is incorrect", results[0], results[1]); + assertPointsEqual("ECAlgorithms.sumOfTwoMultiplies is incorrect", results[0], results[2]); + + p = q; + a = b; + } + } + } + + private void assertPointsEqual(String message, ECPoint a, ECPoint b) + { + assertEquals(message, a, b); + } + + private ECPoint[] copyPoints(ECPoint[] ps, int len) + { + ECPoint[] result = new ECPoint[len]; + System.arraycopy(ps, 0, result, 0, len); + return result; + } + + private BigInteger[] copyScalars(BigInteger[] ks, int len) + { + BigInteger[] result = new BigInteger[len]; + System.arraycopy(ks, 0, result, 0, len); + return result; + } + + private ECPoint getRandomPoint(X9ECParameters x9) + { + return x9.getG().multiply(getRandomScalar(x9)); + } + + private BigInteger getRandomScalar(X9ECParameters x9) + { + return new BigInteger(x9.getN().bitLength(), RND); + } + + private ArrayList getTestCurves() + { + ArrayList x9s = new ArrayList(); + Set names = new HashSet(Collections.list(ECNamedCurveTable.getNames())); + names.addAll(Collections.list(CustomNamedCurves.getNames())); + + Iterator it = names.iterator(); + while (it.hasNext()) + { + String name = (String)it.next(); + + X9ECParameters x9 = ECNamedCurveTable.getByName(name); + if (x9 != null) + { + addTestCurves(x9s, x9); + } + + x9 = CustomNamedCurves.getByName(name); + if (x9 != null) + { + addTestCurves(x9s, x9); + } + } + return x9s; + } + + private void addTestCurves(ArrayList x9s, X9ECParameters x9) + { + ECCurve curve = x9.getCurve(); + + int[] coords = ECCurve.getAllCoordinateSystems(); + for (int i = 0; i < coords.length; ++i) + { + int coord = coords[i]; + if (curve.getCoordinateSystem() == coord) + { + x9s.add(x9); + } + else if (curve.supportsCoordinateSystem(coord)) + { + ECCurve c = curve.configure().setCoordinateSystem(coord).create(); + x9s.add(new X9ECParameters(c, c.importPoint(x9.getG()), x9.getN(), x9.getH())); + } + } + } + + public static Test suite() + { + return new TestSuite(ECAlgorithmsTest.class); + } + +} |