summary refs log tree commit diff
path: root/crypto/test
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
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')
-rw-r--r--crypto/test/UnitTests.csproj1
-rw-r--r--crypto/test/src/math/ec/test/AllTests.cs1
-rw-r--r--crypto/test/src/math/ec/test/FixedPointTest.cs66
3 files changed, 68 insertions, 0 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj
index be8b7d3c5..922507468 100644
--- a/crypto/test/UnitTests.csproj
+++ b/crypto/test/UnitTests.csproj
@@ -328,6 +328,7 @@
     <Compile Include="src\math\ec\test\ECPointPerformanceTest.cs" />
     <Compile Include="src\math\ec\test\ECPointTest.cs" />
     <Compile Include="src\math\ec\test\F2mProofer.cs" />
+    <Compile Include="src\math\ec\test\FixedPointTest.cs" />
     <Compile Include="src\math\ec\test\TnafTest.cs" />
     <Compile Include="src\math\test\AllTests.cs" />
     <Compile Include="src\math\test\BigIntegerTest.cs" />
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);
+        }
+    }
+}