summary refs log tree commit diff
path: root/crypto/test/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-10-31 11:40:29 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-10-31 11:40:29 +0700
commit421f33f93357fcf88d75eaf3e22c19528cce39cc (patch)
tree3c9b1b98c20c0f009aa1a756296d893287e42b07 /crypto/test/src
parentFix UnitTests resource paths (diff)
parentSupporing .NET 1.1 (diff)
downloadBouncyCastle.NET-ed25519-421f33f93357fcf88d75eaf3e22c19528cce39cc.tar.xz
Merge branch 'nist_ecc_test_pr' of https://github.com/BlackthornYugen/bc-csharp into BlackthornYugen-nist_ecc_test_pr
Diffstat (limited to 'crypto/test/src')
-rw-r--r--crypto/test/src/crypto/test/NistEccTest.cs106
1 files changed, 106 insertions, 0 deletions
diff --git a/crypto/test/src/crypto/test/NistEccTest.cs b/crypto/test/src/crypto/test/NistEccTest.cs
new file mode 100644

index 000000000..2b0edb63d --- /dev/null +++ b/crypto/test/src/crypto/test/NistEccTest.cs
@@ -0,0 +1,106 @@ +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 +{ + [TestFixture] + public class NistEccTest : SimpleTest + { + public override string Name { get; } = "NistEcc"; + + public override void PerformTest() + { + foreach (object[] testVector in CollectTestVectors()) + { + TestMultiply( + testVector[0] as string, + testVector[1] as BigInteger, + testVector[2] as BigInteger, + testVector[3] as BigInteger + ); + } + } + + public IEnumerable CollectTestVectors() + { + ArrayList testVectors = new ArrayList(); + 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())) + { + Regex capture = new Regex(@"^ ?(\w+):? =? ?(\w+)", RegexOptions.Compiled); + Match data = capture.Match(line); + + if (!data.Success) continue; + string nistKey = data.Groups[1].Value; + string 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) + { + 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 + X9ECParameters x9EcParameters = Asn1.Nist.NistNamedCurves.GetByName(curve); + + // Act + ECPoint ecPoint = x9EcParameters.G.Multiply(k).Normalize(); + + // Assert + 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