From 3fc5f0e61be2f9f6169d88b143e8c196d5e63b5e Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 29 Jul 2019 22:15:23 +0700 Subject: Fix field reduction for custom secp128r1 curve - see https://github.com/bcgit/bc-java/issues/566 --- crypto/crypto.csproj | 5 +++ crypto/src/math/ec/custom/sec/SecP128R1Field.cs | 5 +++ crypto/test/UnitTests.csproj | 1 + .../math/ec/custom/sec/test/SecP128R1FieldTest.cs | 46 ++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 crypto/test/src/math/ec/custom/sec/test/SecP128R1FieldTest.cs diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj index 55fcf1704..1b4cccd52 100644 --- a/crypto/crypto.csproj +++ b/crypto/crypto.csproj @@ -12932,6 +12932,11 @@ SubType = "Code" BuildAction = "Compile" /> + = P3 && Nat128.Gte(z, P)) + { + AddPInvTo(z); + } } public static void Square(uint[] x, uint[] z) diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj index 1378034a6..0ab02f02e 100644 --- a/crypto/test/UnitTests.csproj +++ b/crypto/test/UnitTests.csproj @@ -337,6 +337,7 @@ + diff --git a/crypto/test/src/math/ec/custom/sec/test/SecP128R1FieldTest.cs b/crypto/test/src/math/ec/custom/sec/test/SecP128R1FieldTest.cs new file mode 100644 index 000000000..211d95c9c --- /dev/null +++ b/crypto/test/src/math/ec/custom/sec/test/SecP128R1FieldTest.cs @@ -0,0 +1,46 @@ +using System; + +using NUnit.Framework; + +using Org.BouncyCastle.Asn1.Sec; +using Org.BouncyCastle.Asn1.X9; +using Org.BouncyCastle.Crypto.EC; +using Org.BouncyCastle.Math.Raw; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Math.EC.Custom.Sec.Tests +{ + [TestFixture] + public class SecP128R1FieldTest + { + [Test] + public void Test_GitHub566() + { + uint[] x = new uint[]{ 0x4B1E2F5E, 0x09E29D21, 0xA58407ED, 0x6FC3C7CF }; + uint[] y = new uint[]{ 0x2FFE8892, 0x55CA61CA, 0x0AF780B5, 0x4BD7B797 }; + uint[] z = Nat128.Create(); + + SecP128R1Field.Multiply(x, y, z); + + uint[] expected = new uint[]{ 0x01FFFF01, 0, 0, 0 }; + Assert.IsTrue(Arrays.AreEqual(expected, z)); + } + + [Test] + public void TestReduce32() + { + uint[] z = Nat128.Create(); + //Arrays.Fill(z, 0xFFFFFFFF); + for (int i = 0; i < z.Length; ++i) + { + z[i] = 0xFFFFFFFF; + } + + SecP128R1Field.Reduce32(0xFFFFFFFF, z); + + uint[] expected = new uint[]{ 1, 1, 0, 4 }; + Assert.IsTrue(Arrays.AreEqual(expected, z)); + } + } +} -- cgit 1.4.1