From cffddc331374f3915e70b738dbef4cb2f505581e Mon Sep 17 00:00:00 2001 From: John Steel Date: Sat, 17 Oct 2020 11:14:15 -0400 Subject: Test Vectors for EC Point Multiply Test multiply with the test vectors downloaded from: http://point-at-infinity.org/ecc/nisttv --- crypto/test/src/crypto/test/NistEccTest.cs | 72 ++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 crypto/test/src/crypto/test/NistEccTest.cs (limited to 'crypto/test/src') diff --git a/crypto/test/src/crypto/test/NistEccTest.cs b/crypto/test/src/crypto/test/NistEccTest.cs new file mode 100644 index 000000000..7e658ff3e --- /dev/null +++ b/crypto/test/src/crypto/test/NistEccTest.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; +using NUnit.Framework; +using Org.BouncyCastle.Math; +using Org.BouncyCastle.Utilities.Test; + +namespace Org.BouncyCastle.Crypto.Tests +{ + public class NistEccTest + { + public IEnumerable CollectTestVectors() + { + string curve = null; + BigInteger k = null; + BigInteger x = null; + BigInteger y = null; + + using (StreamReader r = new StreamReader(SimpleTest.GetTestDataAsStream("crypto.nist_ecc.txt"))) + { + string line; + while (null != (line = r.ReadLine())) + { + var capture = new Regex(@"^ ?(\w+):? =? ?(\w+)", RegexOptions.Compiled); + var data = capture.Match(line); + + if (!data.Success) continue; + var nistKey = data.Groups[1].Value; + var nistValue = data.Groups[2].Value; + switch (nistKey) + { + case "Curve": + // Change curve name from LNNN to L-NNN ie: P256 to P-256 + curve = $"{nistValue.Substring(0, 1)}-{nistValue.Substring(1)}"; + break; + case "k": + k = new BigInteger(nistValue, 10); + break; + case "x": + x = new BigInteger(nistValue, radix: 16); + break; + case "y": + y = new BigInteger(nistValue, radix: 16); + break; + } + + if (null != curve && null != k && null != x && null != y) + { + yield return new object[] {curve, k, x, y}; + k = null; + x = null; + y = null; + } + } + } + } + + [TestCaseSource(nameof(CollectTestVectors))] + public void TestMultiply(string curve, BigInteger k, BigInteger expectedX, BigInteger expectedY) + { + // Arrange + var x9EcParameters = Asn1.Nist.NistNamedCurves.GetByName(curve); + + // Act + var ecPoint = x9EcParameters.G.Multiply(k).Normalize(); + + // Assert + Assert.AreEqual(expectedX, ecPoint.XCoord.ToBigInteger(), "Unexpected X Coordinate"); + Assert.AreEqual(expectedY, ecPoint.YCoord.ToBigInteger(), "Unexpected Y Coordinate"); + } + } +} \ No newline at end of file -- cgit 1.5.1 From 23e2e6565aa4f368a2851a6683c2c0c6e260ab1f Mon Sep 17 00:00:00 2001 From: John Steel Date: Sat, 17 Oct 2020 12:08:00 -0400 Subject: Updating test structure to match existing. --- crypto/test/src/crypto/test/NistEccTest.cs | 36 ++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'crypto/test/src') diff --git a/crypto/test/src/crypto/test/NistEccTest.cs b/crypto/test/src/crypto/test/NistEccTest.cs index 7e658ff3e..8f7d73425 100644 --- a/crypto/test/src/crypto/test/NistEccTest.cs +++ b/crypto/test/src/crypto/test/NistEccTest.cs @@ -7,8 +7,24 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - public class NistEccTest + [TestFixture] + public class NistEccTest : SimpleTest { + public override string Name { get; } = "NistEcc"; + + public override void PerformTest() + { + foreach (var testVector in CollectTestVectors()) + { + TestMultiply( + curve: testVector[0] as string, + k: testVector[1] as BigInteger, + expectedX:testVector[2] as BigInteger, + expectedY: testVector[3] as BigInteger + ); + } + } + public IEnumerable CollectTestVectors() { string curve = null; @@ -55,7 +71,6 @@ namespace Org.BouncyCastle.Crypto.Tests } } - [TestCaseSource(nameof(CollectTestVectors))] public void TestMultiply(string curve, BigInteger k, BigInteger expectedX, BigInteger expectedY) { // Arrange @@ -65,8 +80,21 @@ namespace Org.BouncyCastle.Crypto.Tests var ecPoint = x9EcParameters.G.Multiply(k).Normalize(); // Assert - Assert.AreEqual(expectedX, ecPoint.XCoord.ToBigInteger(), "Unexpected X Coordinate"); - Assert.AreEqual(expectedY, ecPoint.YCoord.ToBigInteger(), "Unexpected Y Coordinate"); + IsEquals("Unexpected X Coordinate", expectedX, ecPoint.AffineXCoord.ToBigInteger()); + IsEquals("Unexpected Y Coordinate", expectedY, ecPoint.AffineYCoord.ToBigInteger()); + } + + public static void Main(string[] args) + { + RunTest(new NistEccTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); } } } \ No newline at end of file -- cgit 1.5.1 From b536fe17d2464bb91edfead7e51ff69f9866761e Mon Sep 17 00:00:00 2001 From: John Steel Date: Tue, 20 Oct 2020 20:56:08 -0400 Subject: Supporing .NET 1.1 - removing generics - removing named params - replacing 'var' with types --- crypto/test/src/crypto/test/NistEccTest.cs | 34 ++++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'crypto/test/src') diff --git a/crypto/test/src/crypto/test/NistEccTest.cs b/crypto/test/src/crypto/test/NistEccTest.cs index 8f7d73425..2b0edb63d 100644 --- a/crypto/test/src/crypto/test/NistEccTest.cs +++ b/crypto/test/src/crypto/test/NistEccTest.cs @@ -1,8 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections; using System.IO; using System.Text.RegularExpressions; using NUnit.Framework; +using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests @@ -14,19 +17,20 @@ namespace Org.BouncyCastle.Crypto.Tests public override void PerformTest() { - foreach (var testVector in CollectTestVectors()) + foreach (object[] testVector in CollectTestVectors()) { TestMultiply( - curve: testVector[0] as string, - k: testVector[1] as BigInteger, - expectedX:testVector[2] as BigInteger, - expectedY: testVector[3] as BigInteger + testVector[0] as string, + testVector[1] as BigInteger, + testVector[2] as BigInteger, + testVector[3] as BigInteger ); } } - public IEnumerable CollectTestVectors() + public IEnumerable CollectTestVectors() { + ArrayList testVectors = new ArrayList(); string curve = null; BigInteger k = null; BigInteger x = null; @@ -37,12 +41,12 @@ namespace Org.BouncyCastle.Crypto.Tests string line; while (null != (line = r.ReadLine())) { - var capture = new Regex(@"^ ?(\w+):? =? ?(\w+)", RegexOptions.Compiled); - var data = capture.Match(line); + Regex capture = new Regex(@"^ ?(\w+):? =? ?(\w+)", RegexOptions.Compiled); + Match data = capture.Match(line); if (!data.Success) continue; - var nistKey = data.Groups[1].Value; - var nistValue = data.Groups[2].Value; + string nistKey = data.Groups[1].Value; + string nistValue = data.Groups[2].Value; switch (nistKey) { case "Curve": @@ -62,22 +66,24 @@ namespace Org.BouncyCastle.Crypto.Tests if (null != curve && null != k && null != x && null != y) { - yield return new object[] {curve, k, x, y}; + testVectors.Add(new object[] {curve, k, x, y}); k = null; x = null; y = null; } } } + + return testVectors; } public void TestMultiply(string curve, BigInteger k, BigInteger expectedX, BigInteger expectedY) { // Arrange - var x9EcParameters = Asn1.Nist.NistNamedCurves.GetByName(curve); + X9ECParameters x9EcParameters = Asn1.Nist.NistNamedCurves.GetByName(curve); // Act - var ecPoint = x9EcParameters.G.Multiply(k).Normalize(); + ECPoint ecPoint = x9EcParameters.G.Multiply(k).Normalize(); // Assert IsEquals("Unexpected X Coordinate", expectedX, ecPoint.AffineXCoord.ToBigInteger()); -- cgit 1.5.1