summary refs log tree commit diff
path: root/crypto/test/src/math
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-04-15 21:12:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-04-15 21:12:11 +0700
commitd79a501212d4012139c714e361577669c75171aa (patch)
treef78e8c7d34c9448698e17bc341fd8d293814dd3e /crypto/test/src/math
parentUpdate Readme.html for SHA-3 perf. opts. (diff)
downloadBouncyCastle.NET-ed25519-d79a501212d4012139c714e361577669c75171aa.tar.xz
Cache-safety for EC lookup tables
- creation of cache-safe lookup tables delegated to ECCurve
- FixedPointCombMultiplier uses cache-safe lookup table
- FixedPointCombMultiplier avoids BigInteger.TestBit
Diffstat (limited to 'crypto/test/src/math')
-rw-r--r--crypto/test/src/math/ec/test/AllTests.cs1
-rw-r--r--crypto/test/src/math/ec/test/FixedPointTest.cs66
2 files changed, 67 insertions, 0 deletions
diff --git a/crypto/test/src/math/ec/test/AllTests.cs b/crypto/test/src/math/ec/test/AllTests.cs
index 0517ac713..3d3f3939b 100644
--- a/crypto/test/src/math/ec/test/AllTests.cs
+++ b/crypto/test/src/math/ec/test/AllTests.cs
@@ -21,6 +21,7 @@ namespace Org.BouncyCastle.Math.EC.Tests
                 TestSuite suite = new TestSuite("EC Math tests");
                 suite.Add(new ECAlgorithmsTest());
                 suite.Add(new ECPointTest());
+                suite.Add(new FixedPointTest());
                 return suite;
             }
         }
diff --git a/crypto/test/src/math/ec/test/FixedPointTest.cs b/crypto/test/src/math/ec/test/FixedPointTest.cs
new file mode 100644
index 000000000..83e5fab8f
--- /dev/null
+++ b/crypto/test/src/math/ec/test/FixedPointTest.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1.X9;
+using Org.BouncyCastle.Crypto.EC;
+using Org.BouncyCastle.Math.EC.Multiplier;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities.Collections;
+
+namespace Org.BouncyCastle.Math.EC.Tests
+{
+    [TestFixture]
+    public class FixedPointTest
+    {
+        private static readonly SecureRandom Random = new SecureRandom();
+
+        private const int TestsPerCurve = 5;
+
+        [Test]
+        public void TestFixedPointMultiplier()
+        {
+            FixedPointCombMultiplier M = new FixedPointCombMultiplier();
+
+            ArrayList names = new ArrayList();
+            CollectionUtilities.AddRange(names, ECNamedCurveTable.Names);
+            CollectionUtilities.AddRange(names, CustomNamedCurves.Names);
+
+            ISet uniqNames = new HashSet(names);
+
+            foreach (string name in uniqNames)
+            {
+                X9ECParameters x9A = ECNamedCurveTable.GetByName(name);
+                X9ECParameters x9B = CustomNamedCurves.GetByName(name);
+
+                X9ECParameters x9 = x9B != null ? x9B : x9A;
+
+                for (int i = 0; i < TestsPerCurve; ++i)
+                {
+                    BigInteger k = new BigInteger(x9.N.BitLength, Random);
+                    ECPoint pRef = ECAlgorithms.ReferenceMultiply(x9.G, k);
+
+                    if (x9A != null)
+                    {
+                        ECPoint pA = M.Multiply(x9A.G, k);
+                        AssertPointsEqual("Standard curve fixed-point failure", pRef, pA);
+                    }
+
+                    if (x9B != null)
+                    {
+                        ECPoint pB = M.Multiply(x9B.G, k);
+                        AssertPointsEqual("Custom curve fixed-point failure", pRef, pB);
+                    }
+                }
+            }
+        }
+
+        private void AssertPointsEqual(string message, ECPoint a, ECPoint b)
+        {
+            // NOTE: We intentionally test points for equality in both directions
+            Assert.AreEqual(a, b, message);
+            Assert.AreEqual(b, a, message);
+        }
+    }
+}