From acaeafd54619da93d6b42335ea5bc006f32af389 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Sun, 15 Dec 2019 17:10:15 +0200 Subject: [PATCH 1/4] testing different configurations for a portable project with tests --- CompactEC.Test/CompactEC.Test.csproj | 16 ++ CompactEC.Test/CryptoGroupAlgebraTests.cs | 240 ++++++++++++++++++ .../CompactEC.Tests_MS.csproj | 0 .../CryptoGroupAlgebraTests.cs | 0 .../CryptoGroupElementTests.cs | 0 .../CryptoGroupTests.cs | 0 .../DefaultECParrameterTests.cs | 0 .../ECGroupAlgebraTests.cs | 0 .../ECPointTests.cs | 0 ...ixedFactorLengthCryptoGroupAlgebraTests.cs | 0 ...ltiplicativeGroupAlgebraIntegratedTests.cs | 0 .../MultiplicativeGroupAlgebraTests.cs | 0 .../Properties/AssemblyInfo.cs | 0 .../packages.config | 0 CompactEC.sln | 14 +- Tests/CryptoGroupAlgebraTests.cs | 240 ++++++++++++++++++ Tests/Properties/AssemblyInfo.cs | 36 +++ Tests/Tests.csproj | 86 +++++++ Tests/packages.config | 9 + 19 files changed, 640 insertions(+), 1 deletion(-) create mode 100644 CompactEC.Test/CompactEC.Test.csproj create mode 100644 CompactEC.Test/CryptoGroupAlgebraTests.cs rename CompactEC.Tests/CompactEC.Tests.csproj => CompactEC.Tests_MS/CompactEC.Tests_MS.csproj (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/CryptoGroupAlgebraTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/CryptoGroupElementTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/CryptoGroupTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/DefaultECParrameterTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/ECGroupAlgebraTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/ECPointTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/FixedFactorLengthCryptoGroupAlgebraTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/MultiplicativeGroupAlgebraIntegratedTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/MultiplicativeGroupAlgebraTests.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/Properties/AssemblyInfo.cs (100%) rename {CompactEC.Tests => CompactEC.Tests_MS}/packages.config (100%) create mode 100644 Tests/CryptoGroupAlgebraTests.cs create mode 100644 Tests/Properties/AssemblyInfo.cs create mode 100644 Tests/Tests.csproj create mode 100644 Tests/packages.config diff --git a/CompactEC.Test/CompactEC.Test.csproj b/CompactEC.Test/CompactEC.Test.csproj new file mode 100644 index 0000000..594a588 --- /dev/null +++ b/CompactEC.Test/CompactEC.Test.csproj @@ -0,0 +1,16 @@ + + + + netstandard1.4 + + + + + + + + + + + + diff --git a/CompactEC.Test/CryptoGroupAlgebraTests.cs b/CompactEC.Test/CryptoGroupAlgebraTests.cs new file mode 100644 index 0000000..7fbc036 --- /dev/null +++ b/CompactEC.Test/CryptoGroupAlgebraTests.cs @@ -0,0 +1,240 @@ +using System; +using System.Numerics; + +using NUnit.Framework; +using Moq; +using Moq.Protected; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + interface CryptoGroupAlgebraProtectedMembers + { + int Multiplex(BigInteger selection, int left, int right); + int MultiplyScalarUnsafe(int e, BigInteger k, int factorBitLength); + } + + [TestFixture] + public class CryptoGroupAlgebraTests + { + [Test] + [TestCase(0, 0)] + [TestCase(1, 1)] + [TestCase(2, 2)] + [TestCase(15, 4)] + [TestCase(16, 5)] + public void TestBitLength(int valueInt, int expectedBitLength) + { + var value = new BigInteger(valueInt); + var result = CryptoGroupAlgebra.GetBitLength(value); + + Assert.AreEqual(expectedBitLength, result); + } + + [Test] + [TestCase(0, 0)] + [TestCase(1, 1)] + [TestCase(2, 2)] + [TestCase(15, 4)] + [TestCase(16, 5)] + public void TestOrderBitLength(int orderInt, int expectedBitLength) + { + var order = new BigInteger(orderInt); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + + var result = algebraMock.Object.OrderBitLength; + + Assert.AreEqual(expectedBitLength, result); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(3)] + [TestCase(4)] + [TestCase(5)] + [TestCase(6)] + [TestCase(12)] + public void TestMultiplyScalar(int scalarInt) + { + // this also tests the implementation in MultiplyScalarUnsafe + + var k = new BigInteger(scalarInt); + var order = new BigInteger(5); + int element = 3; + int expected = 3 * (scalarInt % 5); + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.NeutralElement).Returns(0); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => x); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => y); + + var result = algebraMock.Object.MultiplyScalar(element, k); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestMultiplyScalarRejectsNegativeScalar() + { + int element = 5; + var index = BigInteger.MinusOne; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.ThrowsException( + () => algebraMock.Object.MultiplyScalar(element, index) + ); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(4)] + [TestCase(7)] + public void TestMultiplyScalarWithSmallFactor(int factorInt) + { + int element = 5; + var k = new BigInteger(factorInt); + int factorBitLength = 3; + int expected = 5 * factorInt; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.NeutralElement).Returns(0); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => x); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => y); + + var result = algebraMock.Object.MultiplyScalar(element, k, factorBitLength); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() + { + int element = 5; + var index = BigInteger.MinusOne; + int factorBitLength = 3; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.ThrowsException( + () => algebraMock.Object.MultiplyScalar(element, index, factorBitLength) + ); + } + + [Test] + [TestCase(8)] + [TestCase(9)] + [TestCase(123)] + public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) + { + var k = new BigInteger(factorInt); + int element = 5; + int factorBitLength = 3; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.ThrowsException( + () => algebraMock.Object.MultiplyScalar(element, k, factorBitLength) + ); + } + + + [Test] + public void TestGenerateElement() + { + var order = new BigInteger(7); + int orderBitLength = 3; + int generator = 2; + var index = new BigInteger(3); + int expected = 3 * generator; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.Generator).Returns(generator); + + algebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)) + ) + .Returns(expected); + + var result = algebraMock.Object.GenerateElement(index); + + Assert.AreEqual(expected, result); + algebraMock.Protected().As() + .Verify(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)), + Times.Once() + ); + } + + [Test] + public void TestGenerateElementRejectsNegativeIndex() + { + var index = BigInteger.MinusOne; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Generator).Returns(1); + + Assert.ThrowsException( + () => algebraMock.Object.GenerateElement(index) + ); + } + + [Test] + public void TestNegate() + { + var order = new BigInteger(17); + int element = 7; + int expected = -7; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)) + ) + .Returns(expected); + + int result = algebraMock.Object.Negate(element); + + Assert.AreEqual(expected, result); + algebraMock.Protected().As() + .Verify(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)), + Times.Once() + ); + } + + } +} diff --git a/CompactEC.Tests/CompactEC.Tests.csproj b/CompactEC.Tests_MS/CompactEC.Tests_MS.csproj similarity index 100% rename from CompactEC.Tests/CompactEC.Tests.csproj rename to CompactEC.Tests_MS/CompactEC.Tests_MS.csproj diff --git a/CompactEC.Tests/CryptoGroupAlgebraTests.cs b/CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs similarity index 100% rename from CompactEC.Tests/CryptoGroupAlgebraTests.cs rename to CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs diff --git a/CompactEC.Tests/CryptoGroupElementTests.cs b/CompactEC.Tests_MS/CryptoGroupElementTests.cs similarity index 100% rename from CompactEC.Tests/CryptoGroupElementTests.cs rename to CompactEC.Tests_MS/CryptoGroupElementTests.cs diff --git a/CompactEC.Tests/CryptoGroupTests.cs b/CompactEC.Tests_MS/CryptoGroupTests.cs similarity index 100% rename from CompactEC.Tests/CryptoGroupTests.cs rename to CompactEC.Tests_MS/CryptoGroupTests.cs diff --git a/CompactEC.Tests/DefaultECParrameterTests.cs b/CompactEC.Tests_MS/DefaultECParrameterTests.cs similarity index 100% rename from CompactEC.Tests/DefaultECParrameterTests.cs rename to CompactEC.Tests_MS/DefaultECParrameterTests.cs diff --git a/CompactEC.Tests/ECGroupAlgebraTests.cs b/CompactEC.Tests_MS/ECGroupAlgebraTests.cs similarity index 100% rename from CompactEC.Tests/ECGroupAlgebraTests.cs rename to CompactEC.Tests_MS/ECGroupAlgebraTests.cs diff --git a/CompactEC.Tests/ECPointTests.cs b/CompactEC.Tests_MS/ECPointTests.cs similarity index 100% rename from CompactEC.Tests/ECPointTests.cs rename to CompactEC.Tests_MS/ECPointTests.cs diff --git a/CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs b/CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs similarity index 100% rename from CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs rename to CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs diff --git a/CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs b/CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs similarity index 100% rename from CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs rename to CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs diff --git a/CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs b/CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs similarity index 100% rename from CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs rename to CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs diff --git a/CompactEC.Tests/Properties/AssemblyInfo.cs b/CompactEC.Tests_MS/Properties/AssemblyInfo.cs similarity index 100% rename from CompactEC.Tests/Properties/AssemblyInfo.cs rename to CompactEC.Tests_MS/Properties/AssemblyInfo.cs diff --git a/CompactEC.Tests/packages.config b/CompactEC.Tests_MS/packages.config similarity index 100% rename from CompactEC.Tests/packages.config rename to CompactEC.Tests_MS/packages.config diff --git a/CompactEC.sln b/CompactEC.sln index 043435f..2766919 100644 --- a/CompactEC.sln +++ b/CompactEC.sln @@ -5,10 +5,14 @@ VisualStudioVersion = 15.0.28010.2046 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompactEC", "CompactEC\CompactEC.csproj", "{5F5CFCF8-677E-4028-B74A-E6AB2975360C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests", "CompactEC.Tests\CompactEC.Tests.csproj", "{22FD5C5A-FC60-4F60-B44E-47405CCED477}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests_MS", "CompactEC.Tests_MS\CompactEC.Tests_MS.csproj", "{22FD5C5A-FC60-4F60-B44E-47405CCED477}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "TestApp\Example.csproj", "{43DEFB14-5176-4863-9A51-D141802BA000}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompactEC.Test", "CompactEC.Test\CompactEC.Test.csproj", "{182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +31,14 @@ Global {43DEFB14-5176-4863-9A51-D141802BA000}.Debug|Any CPU.Build.0 = Debug|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Release|Any CPU.ActiveCfg = Release|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Release|Any CPU.Build.0 = Release|Any CPU + {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Release|Any CPU.Build.0 = Release|Any CPU + {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Tests/CryptoGroupAlgebraTests.cs b/Tests/CryptoGroupAlgebraTests.cs new file mode 100644 index 0000000..8d662f1 --- /dev/null +++ b/Tests/CryptoGroupAlgebraTests.cs @@ -0,0 +1,240 @@ +using System; +using System.Numerics; + +using NUnit.Framework; +using Moq; +using Moq.Protected; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + interface CryptoGroupAlgebraProtectedMembers + { + int Multiplex(BigInteger selection, int left, int right); + int MultiplyScalarUnsafe(int e, BigInteger k, int factorBitLength); + } + + [TestFixture] + public class CryptoGroupAlgebraTests + { + [Test] + [TestCase(0, 0)] + [TestCase(1, 1)] + [TestCase(2, 2)] + [TestCase(15, 4)] + [TestCase(16, 5)] + public void TestBitLength(int valueInt, int expectedBitLength) + { + var value = new BigInteger(valueInt); + var result = CryptoGroupAlgebra.GetBitLength(value); + + Assert.AreEqual(expectedBitLength, result); + } + + [Test] + [TestCase(0, 0)] + [TestCase(1, 1)] + [TestCase(2, 2)] + [TestCase(15, 4)] + [TestCase(16, 5)] + public void TestOrderBitLength(int orderInt, int expectedBitLength) + { + var order = new BigInteger(orderInt); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + + var result = algebraMock.Object.OrderBitLength; + + Assert.AreEqual(expectedBitLength, result); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(3)] + [TestCase(4)] + [TestCase(5)] + [TestCase(6)] + [TestCase(12)] + public void TestMultiplyScalar(int scalarInt) + { + // this also tests the implementation in MultiplyScalarUnsafe + + var k = new BigInteger(scalarInt); + var order = new BigInteger(5); + int element = 3; + int expected = 3 * (scalarInt % 5); + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.NeutralElement).Returns(0); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => x); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => y); + + var result = algebraMock.Object.MultiplyScalar(element, k); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestMultiplyScalarRejectsNegativeScalar() + { + int element = 5; + var index = BigInteger.MinusOne; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.Throws( + () => algebraMock.Object.MultiplyScalar(element, index) + ); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(4)] + [TestCase(7)] + public void TestMultiplyScalarWithSmallFactor(int factorInt) + { + int element = 5; + var k = new BigInteger(factorInt); + int factorBitLength = 3; + int expected = 5 * factorInt; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.NeutralElement).Returns(0); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => x); + + algebraMock.Protected().As() + .Setup(alg => alg.Multiplex( + It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) + ) + .Returns((BigInteger s, int x, int y) => y); + + var result = algebraMock.Object.MultiplyScalar(element, k, factorBitLength); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() + { + int element = 5; + var index = BigInteger.MinusOne; + int factorBitLength = 3; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.Throws( + () => algebraMock.Object.MultiplyScalar(element, index, factorBitLength) + ); + } + + [Test] + [TestCase(8)] + [TestCase(9)] + [TestCase(123)] + public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) + { + var k = new BigInteger(factorInt); + int element = 5; + int factorBitLength = 3; + + var algebraMock = new Mock>(MockBehavior.Strict); + + Assert.Throws( + () => algebraMock.Object.MultiplyScalar(element, k, factorBitLength) + ); + } + + + [Test] + public void TestGenerateElement() + { + var order = new BigInteger(7); + int orderBitLength = 3; + int generator = 2; + var index = new BigInteger(3); + int expected = 3 * generator; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.Generator).Returns(generator); + + algebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)) + ) + .Returns(expected); + + var result = algebraMock.Object.GenerateElement(index); + + Assert.AreEqual(expected, result); + algebraMock.Protected().As() + .Verify(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)), + Times.Once() + ); + } + + [Test] + public void TestGenerateElementRejectsNegativeIndex() + { + var index = BigInteger.MinusOne; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Generator).Returns(1); + + Assert.Throws( + () => algebraMock.Object.GenerateElement(index) + ); + } + + [Test] + public void TestNegate() + { + var order = new BigInteger(17); + int element = 7; + int expected = -7; + + var algebraMock = new Mock>() { CallBase = true }; + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)) + ) + .Returns(expected); + + int result = algebraMock.Object.Negate(element); + + Assert.AreEqual(expected, result); + algebraMock.Protected().As() + .Verify(alg => alg.MultiplyScalarUnsafe( + It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)), + Times.Once() + ); + } + + } +} diff --git a/Tests/Properties/AssemblyInfo.cs b/Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5e4e68f --- /dev/null +++ b/Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tests")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("fad092cb-98b1-4206-a8bc-77afdda6cebb")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..28e817c --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,86 @@ + + + + + + + Debug + AnyCPU + {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB} + Library + Properties + Tests + Tests + v4.6 + 512 + true + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll + + + ..\packages\Moq.4.13.1\lib\net45\Moq.dll + + + ..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll + + + + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + + + + + + + + + + + + + + + + {5f5cfcf8-677e-4028-b74a-e6ab2975360c} + CompactEC + + + + + + Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". + + + + + \ No newline at end of file diff --git a/Tests/packages.config b/Tests/packages.config new file mode 100644 index 0000000..e95bcdb --- /dev/null +++ b/Tests/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file From e750298503eeb7065eff5c6b8c602e74001b7476 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Sun, 15 Dec 2019 19:11:06 +0100 Subject: [PATCH 2/4] Full replication of MSTest suite with NUnit as .NET Framework 4.6 project. Refactoring of some files that were not properly renamed before (mostly for Example project) --- CompactEC.Test/CompactEC.Test.csproj | 16 - CompactEC.Test/CryptoGroupAlgebraTests.cs | 240 --------- .../CompactEC.Tests.csproj | 14 +- .../CryptoGroupAlgebraTests.cs | 0 CompactEC.Tests/CryptoGroupElementTests.cs | 408 +++++++++++++++ CompactEC.Tests/CryptoGroupTests.cs | 495 ++++++++++++++++++ CompactEC.Tests/DefaultECParrameterTests.cs | 28 + CompactEC.Tests/ECGroupAlgebraTests.cs | 368 +++++++++++++ CompactEC.Tests/ECPointTests.cs | 57 ++ ...ixedFactorLengthCryptoGroupAlgebraTests.cs | 367 +++++++++++++ ...ltiplicativeGroupAlgebraIntegratedTests.cs | 96 ++++ .../MultiplicativeGroupAlgebraTests.cs | 147 ++++++ .../Properties/AssemblyInfo.cs | 4 +- {Tests => CompactEC.Tests}/packages.config | 0 CompactEC.sln | 10 +- .../CryptoAlgebra.Tests.csproj | 68 --- .../Properties/AssemblyInfo.cs | 20 - CryptoAlgebra.Tests/UnitTest1.cs | 15 - CryptoAlgebra.Tests/packages.config | 5 - {TestApp => Example}/App.config | 0 {TestApp => Example}/Example.csproj | 0 {TestApp => Example}/Program.cs | 0 .../Properties/AssemblyInfo.cs | 4 +- 23 files changed, 1984 insertions(+), 378 deletions(-) delete mode 100644 CompactEC.Test/CompactEC.Test.csproj delete mode 100644 CompactEC.Test/CryptoGroupAlgebraTests.cs rename Tests/Tests.csproj => CompactEC.Tests/CompactEC.Tests.csproj (87%) rename {Tests => CompactEC.Tests}/CryptoGroupAlgebraTests.cs (100%) create mode 100644 CompactEC.Tests/CryptoGroupElementTests.cs create mode 100644 CompactEC.Tests/CryptoGroupTests.cs create mode 100644 CompactEC.Tests/DefaultECParrameterTests.cs create mode 100644 CompactEC.Tests/ECGroupAlgebraTests.cs create mode 100644 CompactEC.Tests/ECPointTests.cs create mode 100644 CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs create mode 100644 CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs create mode 100644 CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs rename {Tests => CompactEC.Tests}/Properties/AssemblyInfo.cs (93%) rename {Tests => CompactEC.Tests}/packages.config (100%) delete mode 100644 CryptoAlgebra.Tests/CryptoAlgebra.Tests.csproj delete mode 100644 CryptoAlgebra.Tests/Properties/AssemblyInfo.cs delete mode 100644 CryptoAlgebra.Tests/UnitTest1.cs delete mode 100644 CryptoAlgebra.Tests/packages.config rename {TestApp => Example}/App.config (100%) rename {TestApp => Example}/Example.csproj (100%) rename {TestApp => Example}/Program.cs (100%) rename {TestApp => Example}/Properties/AssemblyInfo.cs (94%) diff --git a/CompactEC.Test/CompactEC.Test.csproj b/CompactEC.Test/CompactEC.Test.csproj deleted file mode 100644 index 594a588..0000000 --- a/CompactEC.Test/CompactEC.Test.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - netstandard1.4 - - - - - - - - - - - - diff --git a/CompactEC.Test/CryptoGroupAlgebraTests.cs b/CompactEC.Test/CryptoGroupAlgebraTests.cs deleted file mode 100644 index 7fbc036..0000000 --- a/CompactEC.Test/CryptoGroupAlgebraTests.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System; -using System.Numerics; - -using NUnit.Framework; -using Moq; -using Moq.Protected; - -using CompactEC; - -namespace CompactEC.Tests.CryptoAlgebra -{ - interface CryptoGroupAlgebraProtectedMembers - { - int Multiplex(BigInteger selection, int left, int right); - int MultiplyScalarUnsafe(int e, BigInteger k, int factorBitLength); - } - - [TestFixture] - public class CryptoGroupAlgebraTests - { - [Test] - [TestCase(0, 0)] - [TestCase(1, 1)] - [TestCase(2, 2)] - [TestCase(15, 4)] - [TestCase(16, 5)] - public void TestBitLength(int valueInt, int expectedBitLength) - { - var value = new BigInteger(valueInt); - var result = CryptoGroupAlgebra.GetBitLength(value); - - Assert.AreEqual(expectedBitLength, result); - } - - [Test] - [TestCase(0, 0)] - [TestCase(1, 1)] - [TestCase(2, 2)] - [TestCase(15, 4)] - [TestCase(16, 5)] - public void TestOrderBitLength(int orderInt, int expectedBitLength) - { - var order = new BigInteger(orderInt); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - - var result = algebraMock.Object.OrderBitLength; - - Assert.AreEqual(expectedBitLength, result); - } - - [Test] - [TestCase(0)] - [TestCase(1)] - [TestCase(2)] - [TestCase(3)] - [TestCase(4)] - [TestCase(5)] - [TestCase(6)] - [TestCase(12)] - public void TestMultiplyScalar(int scalarInt) - { - // this also tests the implementation in MultiplyScalarUnsafe - - var k = new BigInteger(scalarInt); - var order = new BigInteger(5); - int element = 3; - int expected = 3 * (scalarInt % 5); - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.NeutralElement).Returns(0); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => x); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => y); - - var result = algebraMock.Object.MultiplyScalar(element, k); - - Assert.AreEqual(expected, result); - } - - [Test] - public void TestMultiplyScalarRejectsNegativeScalar() - { - int element = 5; - var index = BigInteger.MinusOne; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, index) - ); - } - - [Test] - [TestCase(0)] - [TestCase(1)] - [TestCase(2)] - [TestCase(4)] - [TestCase(7)] - public void TestMultiplyScalarWithSmallFactor(int factorInt) - { - int element = 5; - var k = new BigInteger(factorInt); - int factorBitLength = 3; - int expected = 5 * factorInt; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.NeutralElement).Returns(0); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => x); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => y); - - var result = algebraMock.Object.MultiplyScalar(element, k, factorBitLength); - - Assert.AreEqual(expected, result); - } - - [Test] - public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() - { - int element = 5; - var index = BigInteger.MinusOne; - int factorBitLength = 3; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, index, factorBitLength) - ); - } - - [Test] - [TestCase(8)] - [TestCase(9)] - [TestCase(123)] - public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) - { - var k = new BigInteger(factorInt); - int element = 5; - int factorBitLength = 3; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, k, factorBitLength) - ); - } - - - [Test] - public void TestGenerateElement() - { - var order = new BigInteger(7); - int orderBitLength = 3; - int generator = 2; - var index = new BigInteger(3); - int expected = 3 * generator; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.Generator).Returns(generator); - - algebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)) - ) - .Returns(expected); - - var result = algebraMock.Object.GenerateElement(index); - - Assert.AreEqual(expected, result); - algebraMock.Protected().As() - .Verify(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)), - Times.Once() - ); - } - - [Test] - public void TestGenerateElementRejectsNegativeIndex() - { - var index = BigInteger.MinusOne; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Generator).Returns(1); - - Assert.ThrowsException( - () => algebraMock.Object.GenerateElement(index) - ); - } - - [Test] - public void TestNegate() - { - var order = new BigInteger(17); - int element = 7; - int expected = -7; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)) - ) - .Returns(expected); - - int result = algebraMock.Object.Negate(element); - - Assert.AreEqual(expected, result); - algebraMock.Protected().As() - .Verify(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)), - Times.Once() - ); - } - - } -} diff --git a/Tests/Tests.csproj b/CompactEC.Tests/CompactEC.Tests.csproj similarity index 87% rename from Tests/Tests.csproj rename to CompactEC.Tests/CompactEC.Tests.csproj index 28e817c..19296fa 100644 --- a/Tests/Tests.csproj +++ b/CompactEC.Tests/CompactEC.Tests.csproj @@ -9,8 +9,8 @@ {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB} Library Properties - Tests - Tests + CompactEC.Tests + CompactEC.Tests v4.6 512 true @@ -52,6 +52,8 @@ ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll @@ -64,6 +66,14 @@ + + + + + + + + diff --git a/Tests/CryptoGroupAlgebraTests.cs b/CompactEC.Tests/CryptoGroupAlgebraTests.cs similarity index 100% rename from Tests/CryptoGroupAlgebraTests.cs rename to CompactEC.Tests/CryptoGroupAlgebraTests.cs diff --git a/CompactEC.Tests/CryptoGroupElementTests.cs b/CompactEC.Tests/CryptoGroupElementTests.cs new file mode 100644 index 0000000..57e3bb9 --- /dev/null +++ b/CompactEC.Tests/CryptoGroupElementTests.cs @@ -0,0 +1,408 @@ +using System; +using System.Numerics; + +using NUnit.Framework; +using Moq; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + [TestFixture] + public class CryptoGroupElementTests + { + + [Test] + public void TestConstructorRejectsInvalidValue() + { + int element = -3; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(false); + + Assert.Throws( + () => new CryptoGroupElement(element, algebraMock.Object) + ); + algebraMock.Verify(alg => alg.IsValid(It.Is(x => x == element)), Times.Once()); + } + + [Test] + public void TestConstructorRejectsNullAlgebra() + { + int element = -3; + + Assert.Throws( + () => new CryptoGroupElement(element, null) + ); + } + + [Test] + public void TestConstructorSetsValueCorrectly() + { + int elementRaw = -3; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(elementRaw, algebraMock.Object); + + Assert.AreEqual(elementRaw, element.Value); + algebraMock.Verify(alg => alg.IsValid(It.Is(x => x == elementRaw)), Times.Once()); + } + + [Test] + public void TestConstructorFromBytesCorrect() + { + byte[] buffer = new byte[0]; + int expectedValue = 9; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.FromBytes(It.IsAny())).Returns(expectedValue); + + var element = new CryptoGroupElement(buffer, algebraMock.Object); + + Assert.AreEqual(expectedValue, element.Value); + algebraMock.Verify(alg => alg.FromBytes(It.Is(x => x == buffer))); + } + + [Test] + public void TestConstructorFromBytesRejectsNullAlgebra() + { + byte[] buffer = new byte[0]; + + Assert.Throws( + () => new CryptoGroupElement(buffer, null) + ); + } + + [Test] + public void TestConstructorFromBytesRejectsInvalidValue() + { + byte[] buffer = new byte[0]; + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.FromBytes(It.IsAny())).Returns(0); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(false); + + Assert.Throws( + () => new CryptoGroupElement(buffer, algebraStub.Object) + ); + } + + [Test] + public void TestAddRejectsOtherCryptoGroupElementSubclasses() + { + var otherElementStub = new Mock(MockBehavior.Strict); + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + + Assert.Throws( + () => element.Add(otherElementStub.Object) + ); + } + + [Test] + public void TestAddRejectsNull() + { + ICryptoGroupElement otherElement = null; + + var algebraStub = new Mock>(); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + + Assert.Throws( + () => element.Add(otherElement) + ); + } + + [Test] + public void TestAddRejectsElementFromDifferentGroup() + { + var otherAlgebraStub = new Mock>(MockBehavior.Strict); + otherAlgebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + var otherElement = new CryptoGroupElement(0, otherAlgebraStub.Object); + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + + Assert.Throws( + () => element.Add(otherElement) + ); + } + + [Test] + public void TestAdd() + { + int otherValue = 3; + int value = 7; + int expected = value + otherValue; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); + + var element = new CryptoGroupElement(value, algebraMock.Object); + var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); + + element.Add(otherElement); + + Assert.AreEqual(expected, element.Value); + Assert.AreEqual(otherValue, otherElement.Value); + algebraMock.Verify(alg => alg.Add(It.Is(x => x == value), It.Is(x => x == otherValue)), Times.Once()); + } + + [Test] + public void TestMultiplyScalar() + { + int value = 3; + var scalar = new BigInteger(7); + int expected = value * (int)scalar; + var order = new BigInteger(10); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); + + var element = new CryptoGroupElement(value, algebraMock.Object); + + element.MultiplyScalar(scalar); + + Assert.AreEqual(expected, element.Value); + algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); + } + + [Test] + public void TestNegate() + { + int value = 3; + int expected = -value; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); + + var element = new CryptoGroupElement(value, algebraMock.Object); + + element.Negate(); + + Assert.AreEqual(expected, element.Value); + algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); + } + + [Test] + public void TestEqualsFalseForOtherCryptoGroupElementSubclasses() + { + var otherElementStub = new Mock(MockBehavior.Strict); + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + bool result = element.Equals(otherElementStub.Object); + + Assert.IsFalse(result); + } + + [Test] + public void TestEqualsFalseForNull() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + bool result = element.Equals(null); + + Assert.IsFalse(result); + } + + [Test] + public void TestEqualsFalseForDifferentValues() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var otherElement = new CryptoGroupElement(3, algebraStub.Object); + var element = new CryptoGroupElement(8, algebraStub.Object); + + bool result = element.Equals(otherElement); + + Assert.IsFalse(result); + } + + [Test] + public void TestEqualsFalseForDifferentAlgebras() + { + var otherAlgebraStub = new Mock>(MockBehavior.Strict); + otherAlgebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + var otherElement = new CryptoGroupElement(0, otherAlgebraStub.Object); + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(0, algebraStub.Object); + + bool result = element.Equals(otherElement); + + Assert.IsFalse(result); + } + + [Test] + public void TestEqualsTrue() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var otherElement = new CryptoGroupElement(5, algebraStub.Object); + var element = new CryptoGroupElement(5, algebraStub.Object); + + bool result = element.Equals(otherElement); + + Assert.IsTrue(result); + } + + [Test] + public void TestOperatorPlus() + { + int otherValue = 3; + int value = 7; + int expected = value + otherValue; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); + + var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); + var element = new CryptoGroupElement(value, algebraMock.Object); + + var result = element + otherElement; + + Assert.AreEqual(otherValue, otherElement.Value); + Assert.AreEqual(value, element.Value); + Assert.AreEqual(expected, result.Value); + algebraMock.Verify(alg => alg.Add(It.Is(x => x == value), It.Is(x => x == otherValue)), Times.Once()); + } + + [Test] + public void TestOperatorUnaryMinus() + { + int value = 3; + int expected = -value; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); + + var element = new CryptoGroupElement(value, algebraMock.Object); + + var result = -element; + + Assert.AreEqual(value, element.Value); + Assert.AreEqual(expected, result.Value); + algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); + } + + [Test] + public void TestOperatorMinus() + { + int otherValue = 7; + int value = 3; + int expected = otherValue - value; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); + algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); + + var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); + var element = new CryptoGroupElement(value, algebraMock.Object); + + var result = otherElement - element; + + Assert.AreEqual(otherValue, otherElement.Value); + Assert.AreEqual(value, element.Value); + Assert.AreEqual(expected, result.Value); + algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); + algebraMock.Verify(alg => alg.Add(It.Is(x => x == -value), It.Is(x => x == otherValue)), Times.Once()); + } + + [Test] + public void TestOperatorMulitplyLeft() + { + int value = 3; + var scalar = new BigInteger(7); + int expected = value * (int)scalar; + var order = new BigInteger(10); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); + + var element = new CryptoGroupElement(value, algebraMock.Object); + + var result = element * scalar; + + Assert.AreEqual(value, element.Value); + Assert.AreEqual(expected, result.Value); + algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); + } + + [Test] + public void TestOperatorMultiplyRight() + { + int value = 3; + var scalar = new BigInteger(7); + int expected = value * (int)scalar; + var order = new BigInteger(10); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); + + var element = new CryptoGroupElement(value, algebraMock.Object); + + var result = scalar * element; + + Assert.AreEqual(value, element.Value); + Assert.AreEqual(expected, result.Value); + algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); + } + + [Test] + public void TestCloneResultsInEqualButNotSameElement() + { + int value = 3; + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var element = new CryptoGroupElement(value, algebraStub.Object); + var clone = element.Clone(); + + Assert.AreEqual(element, clone); + Assert.AreNotSame(element, clone); + } + + [Test] + public void TestToBytesCallsAlgebra() + { + var value = 3; + var expected = new byte[0]; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(alg => alg.ToBytes(It.IsAny())).Returns(expected); + + var element = new CryptoGroupElement(value, algebraMock.Object); + var result = element.ToBytes(); + + algebraMock.Verify(alg => alg.ToBytes(It.Is(x => x == value)), Times.Once()); + } + } +} diff --git a/CompactEC.Tests/CryptoGroupTests.cs b/CompactEC.Tests/CryptoGroupTests.cs new file mode 100644 index 0000000..00b57ad --- /dev/null +++ b/CompactEC.Tests/CryptoGroupTests.cs @@ -0,0 +1,495 @@ +using System; +using System.Numerics; +using System.Security.Cryptography; +using System.Linq; + +using NUnit.Framework; +using Moq; +using Moq.Protected; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + interface CryptoGroupProtectedMembers + { + CryptoGroupElement CreateGroupElement(int value); + CryptoGroupElement CreateGroupElement(byte[] buffer); + } + + class CryptoGroupFake : CryptoGroup + { + public CryptoGroupFake(ICryptoGroupAlgebra algebra) : base(algebra) + { } + + protected override CryptoGroupElement CreateGroupElement(int value) + { + throw new NotImplementedException(); + } + + protected override CryptoGroupElement CreateGroupElement(byte[] buffer) + { + throw new NotImplementedException(); + } + } + + [TestFixture] + public class CryptoGroupTests + { + [Test] + public void TestConstructorRejectsNullAlgebra() + { + Assert.Throws( + () => new CryptoGroupFake(null) + ); + } + + [Test] + public void TestSpecificAddCallsAlgebraAndWrapsAsElement() + { + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.Add(2, 6)).Returns(8); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + + var leftStub = new CryptoGroupElement(2, algebraMock.Object); + var rightStub = new CryptoGroupElement(6, algebraMock.Object); + var resultStub = new CryptoGroupElement(8, algebraMock.Object); + + var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(resultStub); + + Assert.AreSame(resultStub, groupMock.Object.Add(leftStub, rightStub)); + algebraMock.Verify(algebra => algebra.Add(2, 6), Times.Once()); + groupMock.Protected().As() + .Verify(group => group.CreateGroupElement(It.Is(i => i == 8)), Times.Once()); + } + + [Test] + public void TestAddRejectsNullArgumentLeft() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraStub.Object); + var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); + + Assert.Throws( + () => groupMock.Add(null, otherElementStub) + ); + } + + [Test] + public void TestAddRejectsNullArgumentRight() + { + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraMock.Object); + var otherElementStub = new CryptoGroupElement(3, algebraMock.Object); + + Assert.Throws( + () => groupMock.Add(otherElementStub, null) + ); + } + + [Test] + public void TestFromBytesWrapsAsElement() + { + byte[] inputBuffer = new byte[0]; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + + var resultStub = new Mock>(MockBehavior.Strict, 0, algebraMock.Object); + + var groupMock = new Mock>(MockBehavior.Strict, algebraMock.Object); + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(resultStub.Object); + + Assert.AreSame(resultStub.Object, groupMock.Object.FromBytes(inputBuffer)); + groupMock.Protected().As() + .Verify(group => group.CreateGroupElement(It.Is(b => b == inputBuffer)), Times.Once()); + } + + [Test] + public void TestGenerateCallsAlgebraAndWrapsElement() + { + var index = new BigInteger(7); + int expectedRaw = 3; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(algebra => algebra.GenerateElement(It.IsAny())).Returns(expectedRaw); + + var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); + var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); + + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(expectedStub); + + Assert.AreSame(expectedStub, groupMock.Object.Generate(index)); + + algebraMock.Verify( + algebra => algebra.GenerateElement(It.Is(x => x == index)), + Times.Once() + ); + + groupMock.Protected().As().Verify( + group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), + Times.Once() + ); + } + + [Test] + public void TestSpecificMultiplyScalarCallsAlgebraAndWrapsElement() + { + var k = new BigInteger(7); + int expectedRaw = 3; + int elementRaw = 8; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(algebra => algebra.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expectedRaw); + + var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); + var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); + var elementStub = new CryptoGroupElement(elementRaw, algebraMock.Object); + + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(expectedStub); + + Assert.AreSame(expectedStub, groupMock.Object.MultiplyScalar(elementStub, k)); + + algebraMock.Verify( + algebra => algebra.MultiplyScalar(It.Is(x => x == elementRaw), It.Is(x => x == k)), + Times.Once() + ); + groupMock.Protected().As().Verify( + group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), + Times.Once() + ); + } + + [Test] + public void TestMultiplyScalarRejectsNullElement() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraStub.Object); + Assert.Throws( + () => groupMock.MultiplyScalar(null, new BigInteger(1)) + ); + } + + [Test] + public void TestSpecificNegateCallsAlgebraAndWrapsElement() + { + int expectedRaw = 3; + int elementRaw = 8; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + algebraMock.Setup(algebra => algebra.Negate(It.IsAny())).Returns(expectedRaw); + + var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); + var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); + var elementStub = new CryptoGroupElement(elementRaw, algebraMock.Object); + + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(expectedStub); + + Assert.AreSame(expectedStub, groupMock.Object.Negate(elementStub)); + + algebraMock.Verify( + algebra => algebra.Negate(It.Is(x => x == elementRaw)), + Times.Once() + ); + groupMock.Protected().As().Verify( + group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), + Times.Once() + ); + } + + [Test] + public void TestNegateRejectsNullElement() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraStub.Object); + Assert.Throws( + () => groupMock.Negate(null) + ); + } + + [Test] + public void TestAddRejectsDifferentGroupElementLeft() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraStub.Object); + var elementStub = new Mock(MockBehavior.Strict); + var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); + + Assert.Throws( + () => groupMock.Add(elementStub.Object, otherElementStub) + ); + } + + [Test] + public void TestAddRejectsDifferentGroupElementRight() + { + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); + var groupMock = new CryptoGroupFake(algebraStub.Object); + var elementStub = new Mock(MockBehavior.Strict); + var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); + + Assert.Throws( + () => groupMock.Add(otherElementStub, elementStub.Object) + ); + } + + [Test] + public void TestMultiplyScalarRejectsDifferentGroupElement() + { + var algebraStub = new Mock>(MockBehavior.Strict); + var groupMock = new CryptoGroupFake(algebraStub.Object); + var elementStub = new Mock(MockBehavior.Strict); + + Assert.Throws( + () => groupMock.MultiplyScalar(elementStub.Object, new BigInteger(8)) + ); + } + + [Test] + public void TestNegateRejectsDifferentGroupElement() + { + var algebraStub = new Mock>(MockBehavior.Strict); + var groupMock = new CryptoGroupFake(algebraStub.Object); + var elementStub = new Mock(MockBehavior.Strict); + + Assert.Throws( + () => groupMock.Negate(elementStub.Object) + ); + } + + [Test] + public void TestOrderCallsAlgebra() + { + var order = new BigInteger(29); + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + + var groupMock = new CryptoGroupFake(algebraMock.Object); + var result = groupMock.Order; + + Assert.AreEqual(order, result); + } + + [Test] + public void TestOrderBitLengthCallsAlgebra() + { + int rawBitLength = 11; + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.OrderBitLength).Returns(rawBitLength); + + var groupMock = new CryptoGroupFake(algebraStub.Object); + + int result = groupMock.OrderBitLength; + int expected = algebraStub.Object.OrderBitLength; + Assert.AreEqual(expected, result); + } + + [Test] + [TestCase(8, 1)] + [TestCase(9, 2)] + public void TestOrderByteLengthCallsAlgebra(int bitLength, int expectedByteLength) + { + var order = new BigInteger(1 << (bitLength - 1)); + + var algebraStub = new Mock>(MockBehavior.Strict); + algebraStub.Setup(alg => alg.OrderBitLength).Returns(bitLength); + + var groupMock = new CryptoGroupFake(algebraStub.Object); + + int result = groupMock.OrderByteLength; + Assert.AreEqual(expectedByteLength, result); + } + + [Test] + public void TestElementBitLengthCallsAlgebra() + { + int expected = 11; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.ElementBitLength).Returns(expected); + + var groupMock = new CryptoGroupFake(algebraMock.Object); + + int result = groupMock.ElementBitLength; + Assert.AreEqual(expected, result); + + algebraMock.Verify(alg => alg.ElementBitLength, Times.Once()); + } + + [Test] + [TestCase(8, 1)] + [TestCase(9, 2)] + public void TestElementByteLengthCallsAlgebra(int bitLength, int expectedByteLength) + { + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.ElementBitLength).Returns(bitLength); + + var groupMock = new CryptoGroupFake(algebraMock.Object); + + int result = groupMock.ElementByteLength; + Assert.AreEqual(expectedByteLength, result); + + algebraMock.Verify(alg => alg.ElementBitLength, Times.Once()); + } + + [Test] + public void TestGenerateRandom() + { + var order = new BigInteger(1021); + int orderByteLength = 2; + + var expectedRaw = 7; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.OrderBitLength).Returns(orderByteLength * 8); + algebraMock.Setup(alg => alg.GenerateElement(It.IsAny())).Returns(expectedRaw); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var expected = new Mock>(expectedRaw, algebraMock.Object); + + var groupMock = new Mock>(algebraMock.Object); + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(expected.Object); + + var index = new BigInteger(301); + byte[] rngResponse = index.ToByteArray(); + + var rngMock = new Mock(); + rngMock + .Setup(rng => rng.GetBytes(It.IsAny())) + .Callback( + new Action( + (buffer) => { Buffer.BlockCopy(rngResponse, 0, buffer, 0, orderByteLength); } + ) + ); + + var result = groupMock.Object.GenerateRandom(rngMock.Object); + var resultIndex = result.Item1; + var resultElement = result.Item2; + Assert.AreEqual(index, resultIndex); + Assert.AreSame(expected.Object, resultElement); + + algebraMock.Verify( + alg => alg.GenerateElement(It.Is(x => x == index)), + Times.Once() + ); + + groupMock.Protected().As() + .Verify(group => group.CreateGroupElement(It.Is(x => x == expectedRaw)), + Times.Once()); + + rngMock.Verify(rng => rng.GetBytes(It.Is(x => x.Length == orderByteLength)), Times.Once()); + } + + [Test] + [TestCase(-3)] + [TestCase(0)] + [TestCase(1)] + [TestCase(1020)] + [TestCase(1022)] + public void TestGenerateRandomDoesNotSampleInvalidIndices(int invalidIndexRaw) + { + // tests that invalid indices returned by rng are skipped: + // negative, 0, 1, order -1, order +1 + var order = new BigInteger(1021); + int orderByteLength = 2; + + var expectedRaw = 7; + + var algebraMock = new Mock>(MockBehavior.Strict); + algebraMock.Setup(alg => alg.Order).Returns(order); + algebraMock.Setup(alg => alg.OrderBitLength).Returns(orderByteLength * 8); + algebraMock.Setup(alg => alg.GenerateElement(It.IsAny())).Returns(expectedRaw); + algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + + var expected = new Mock>(expectedRaw, algebraMock.Object); + + var groupMock = new Mock>(algebraMock.Object); + groupMock.Protected().As() + .Setup(group => group.CreateGroupElement(It.IsAny())) + .Returns(expected.Object); + + var invalidIndex = new BigInteger(invalidIndexRaw); + byte[] invalidRngResponse = invalidIndex.ToByteArray(); + if (invalidRngResponse.Length < orderByteLength) + { + byte[] buffer = new byte[orderByteLength]; + Buffer.BlockCopy(invalidRngResponse, 0, buffer, 0, invalidRngResponse.Length); + if (invalidIndex < 0) + { + buffer[1] = 0xFF; + } + else + { + buffer[1] = 0x00; + } + invalidRngResponse = buffer; + } + + var validIndex = new BigInteger(301); + byte[] validRngResponse = validIndex.ToByteArray(); + + bool firstTime = true; + var rngMock = new Mock(); + rngMock + .Setup(rng => rng.GetBytes(It.IsAny())) + .Callback( + new Action( + (buffer) => { + if (firstTime) + { + Buffer.BlockCopy(invalidRngResponse, 0, buffer, 0, orderByteLength); + firstTime = false; + } + else + { + Buffer.BlockCopy(validRngResponse, 0, buffer, 0, orderByteLength); + } + } + ) + ); + + var result = groupMock.Object.GenerateRandom(rngMock.Object); + var resultIndex = result.Item1; + var resultElement = result.Item2; + Assert.AreEqual(validIndex, resultIndex); + Assert.AreSame(expected.Object, resultElement); + + algebraMock.Verify( + alg => alg.GenerateElement(It.Is(x => x == validIndex)), + Times.Once() + ); + + groupMock.Protected().As() + .Verify(group => group.CreateGroupElement(It.Is(x => x == expectedRaw)), + Times.Once()); + + rngMock.Verify(rng => rng.GetBytes(It.Is(x => x.Length == orderByteLength)), Times.Exactly(2)); + } + + } +} diff --git a/CompactEC.Tests/DefaultECParrameterTests.cs b/CompactEC.Tests/DefaultECParrameterTests.cs new file mode 100644 index 0000000..acd43aa --- /dev/null +++ b/CompactEC.Tests/DefaultECParrameterTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Numerics; + +using NUnit.Framework; + +using CompactEC; + +namespace CompactEC.Tests +{ + [TestFixture] + public class DefaultECParrameterTests + { + [Test] + public void TestNISTP256() + { + ECParameters parameters = ECParameters.CreateNISTP256(); + Assert.AreEqual(BigInteger.Parse("41058363725152142129326129780047268409114441015993725554835256314039467401291"), parameters.B); + Assert.AreEqual(BigInteger.Parse("48439561293906451759052585252797914202762949526041747995844080717082404635286"), parameters.Generator.X); + Assert.AreEqual(BigInteger.Parse("36134250956749795798585127919587881956611106672985015071877198253568414405109"), parameters.Generator.Y); + var groupAlgebra = new ECGroupAlgebra(parameters); + var elem = groupAlgebra.GenerateElement(parameters.Order); + Assert.AreEqual(groupAlgebra.NeutralElement, elem); + + elem = groupAlgebra.GenerateElement(BigInteger.Parse("28367823582636726875877070967237095880621")); + Assert.AreEqual(groupAlgebra.NeutralElement, groupAlgebra.Add(elem, groupAlgebra.Negate(elem))); + } + } +} diff --git a/CompactEC.Tests/ECGroupAlgebraTests.cs b/CompactEC.Tests/ECGroupAlgebraTests.cs new file mode 100644 index 0000000..3174215 --- /dev/null +++ b/CompactEC.Tests/ECGroupAlgebraTests.cs @@ -0,0 +1,368 @@ +using System; +using System.Numerics; + +using NUnit.Framework; + +namespace CompactEC.Tests +{ + [TestFixture] + public class ECGroupAlgebraTests + { + // reference results from https://trustica.cz/en/2018/04/26/elliptic-curves-prime-order-curves/ + // generator has order 16, i.e., OrderSize = 5 bits + + private readonly ECParameters ecParams; + + public ECGroupAlgebraTests() + { + ecParams = new ECParameters() + { + P = 23, + A = -2, + B = 2, + Generator = new CompactEC.ECPoint(1, 1), + Order = 16 + }; + } + + [Test] + public void TestAddDoublePoint() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = p.Clone(); + + var expectedQ = new CompactEC.ECPoint(15, 14); + var q = curve.Add(p, other); + + Assert.AreEqual(expectedQ, q); + } + + [Test] + public void TestAddDoublePointAtInfinity() + { + var curve = new ECGroupAlgebra(ecParams); + var p = CompactEC.ECPoint.PointAtInfinity; + var other = p.Clone(); + + var expectedQ = CompactEC.ECPoint.PointAtInfinity; + var q = curve.Add(p, other); + + Assert.AreEqual(expectedQ, q); + } + + [Test] + public void TestAddDifferentPoints() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = new CompactEC.ECPoint(15, 14); + + var expected = new CompactEC.ECPoint(16, 15); + var result = curve.Add(p, other); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestAddPointAtInfinityLeft() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = CompactEC.ECPoint.PointAtInfinity; + + var result = curve.Add(other, p); + + Assert.AreEqual(p, result); + Assert.AreNotSame(p, result); + } + + [Test] + public void TestAddPointAtInfinityRight() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = CompactEC.ECPoint.PointAtInfinity; + + var result = curve.Add(p, other); + + Assert.AreEqual(p, result); + Assert.AreNotSame(p, result); + } + + [Test] + public void TestAddNegated() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = curve.Negate(p); + + var expected = CompactEC.ECPoint.PointAtInfinity; + var result = curve.Add(p, other); + + Assert.AreEqual(expected, result); + } + + [Test] + public void TestAreNegationsFalseForEqualPoint() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = p.Clone(); + + Assert.IsFalse(curve.AreNegations(p, other)); + } + + [Test] + public void TestAreNegationsTrueForNegation() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var other = curve.Negate(p); + + Assert.IsTrue(curve.AreNegations(p, other)); + } + + [Test] + public void TestAreNegationsTrueForZeroYPoint() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(11, 0); + var other = p.Clone(); + + Assert.IsTrue(curve.AreNegations(p, other)); + } + + [Test] + public void TestAddAffine() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(11, 0); + + var q = curve.Add(p, p); + + Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, q); + } + + [Test] + [TestCase(5, 5)] + [TestCase(11, 0)] + [TestCase(16, 15)] + public void TestPointValidTrueForValidPoint(int xRaw, int yRaw) + { + var curve = new ECGroupAlgebra(ecParams); + var point = new CompactEC.ECPoint(xRaw, yRaw); + Assert.IsTrue(curve.IsValid(point)); + } + + [Test] + public void TestIsValidTrueForPointAtInfinity() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.IsTrue(curve.IsValid(CompactEC.ECPoint.PointAtInfinity)); + } + + [Test] + [TestCase(16, 1)] + [TestCase(5, 2)] + [TestCase(-2, 1)] + [TestCase(16, -15)] + [TestCase(78, 4)] + [TestCase(4, 78)] + public void TestIsvalidFalseForInvalidPoint(int xRaw, int yRaw) + { + var curve = new ECGroupAlgebra(ecParams); + var point = new CompactEC.ECPoint(xRaw, yRaw); + Assert.IsFalse(curve.IsValid(point)); + } + + [Test] + public void TestNegate() + { + var curve = new ECGroupAlgebra(ecParams); + + var p = new CompactEC.ECPoint(5, 5); + var expected = new CompactEC.ECPoint(5, 23 - 5); + + var result = curve.Negate(p); + Assert.AreEqual(expected, result); + } + + [Test] + public void TestNegateForZeroYPoint() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(11, 0); + + var result = curve.Negate(p); + Assert.AreEqual(p, result); + Assert.AreNotSame(p, result); + } + + [Test] + public void TestNegatePointAtInfinity() + { + var curve = new ECGroupAlgebra(ecParams); + var p = CompactEC.ECPoint.PointAtInfinity; + + var result = curve.Negate(p); + Assert.AreEqual(p, result); + Assert.AreNotSame(p, result); + } + + [Test] + [TestCase(2, 15, 14)] + [TestCase(5, 16, 8)] + [TestCase(7, 5, 18)] + [TestCase(9, 5, 5)] + public void TestMultiplyScalar(int kRaw, int expectedX, int expectedY) + { + var k = new BigInteger(kRaw); + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + + var q = curve.MultiplyScalar(p, k); + var expectedQ = new CompactEC.ECPoint(expectedX, expectedY); + Assert.AreEqual(expectedQ, q); + } + + [Test] + public void TestMultiplyScalarOrderResultsInNeutralElement() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var result = curve.MultiplyScalar(p, 8); + + Assert.AreEqual(curve.NeutralElement, result); + } + + [Test] + public void TestGroupElementBitLength() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.AreEqual(2*5, curve.ElementBitLength); + } + + [Test] + public void TestOrderIsAsSet() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.AreEqual(ecParams.Order, curve.Order); + } + + [Test] + public void TestOrderBitLength() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.AreEqual(5, curve.OrderBitLength); + } + + [Test] + public void TestNeutralElement() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, curve.NeutralElement); + } + + [Test] + public void TestGeneratorIsAsSet() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.AreEqual(ecParams.Generator, curve.Generator); + } + + [Test] + public void TestFromBytes() + { + ECParameters largeParams = new ECParameters() + { + P = 134217728, // == 2 ^ 27 + Generator = CompactEC.ECPoint.PointAtInfinity, + Order = 1 + }; + + var curve = new ECGroupAlgebra(largeParams); + var expected = new CompactEC.ECPoint(5, 5); + var buffer = new byte[] { 5, 0, 0, 0, 5, 0, 0, 0 }; + + var result = curve.FromBytes(buffer); + Assert.AreEqual(expected, result); + } + + [Test] + public void TestFromBytesRejectsNullArgument() + { + var curve = new ECGroupAlgebra(ecParams); + Assert.Throws( + () => curve.FromBytes(null) + ); + } + + [Test] + public void TestFromBytesRejectsTooShortBuffer() + { + ECParameters largeParams = new ECParameters() + { + P = 134217728, // == 2 ^ 27 + Generator = CompactEC.ECPoint.PointAtInfinity, + Order = 1 + }; + var curve = new ECGroupAlgebra(largeParams); + var buffer = new byte[7]; + Assert.Throws( + () => curve.FromBytes(buffer) + ); + } + + [Test] + public void TestToBytes() + { + ECParameters largeParams = new ECParameters() + { + P = 134217728, // == 2 ^ 27 + Generator = CompactEC.ECPoint.PointAtInfinity, + Order = 1 + }; + + var curve = new ECGroupAlgebra(largeParams); + var p = new CompactEC.ECPoint(5, 5); + var expected = new byte[] { 5, 0, 0, 0, 5, 0, 0, 0 }; + + var result = curve.ToBytes(p); + CollectionAssert.AreEqual(expected, result); + } + + [Test] + public void TestFromBytesWithLessThanOneByteLargeElements() + { + var curve = new ECGroupAlgebra(ecParams); + var expected = new CompactEC.ECPoint(5, 5); + var buffer = new byte[] { 5, 5 }; + + var result = curve.FromBytes(buffer); + Assert.AreEqual(expected, result); + } + + [Test] + public void TestToBytesWithLessThanOneByteLargeElements() + { + var curve = new ECGroupAlgebra(ecParams); + var p = new CompactEC.ECPoint(5, 5); + var expected = new byte[] { 5, 5 }; + + var result = curve.ToBytes(p); + CollectionAssert.AreEqual(expected, result); + } + + [Test] + public void TestInvalidElementRejectedAsGenerator() + { + var generator = new CompactEC.ECPoint(16, 1); + var invalidParams = ecParams; + invalidParams.Generator = generator; + Assert.Throws( + () => new ECGroupAlgebra(invalidParams) + ); + } + } +} diff --git a/CompactEC.Tests/ECPointTests.cs b/CompactEC.Tests/ECPointTests.cs new file mode 100644 index 0000000..b0bd6e4 --- /dev/null +++ b/CompactEC.Tests/ECPointTests.cs @@ -0,0 +1,57 @@ +using System; +using System.Numerics; + +using NUnit.Framework; + +namespace CompactEC.Tests +{ + [TestFixture] + public class ECPointTests + { + // reference results from https://trustica.cz/en/2018/04/26/elliptic-curves-prime-order-curves/ + // curve has order 32, i.e., OrderSize = 6 bits + + [Test] + public void TestClone() + { + var p = new CompactEC.ECPoint(5, 5); + var q = p.Clone(); + Assert.AreNotSame(p, q); + Assert.AreEqual(p, q); + } + + [Test] + public void TestClonePointAtInfinity() + { + var p = CompactEC.ECPoint.PointAtInfinity; + var q = p.Clone(); + Assert.AreNotSame(p, q); + Assert.AreEqual(p, q); + } + + [Test] + public void TestEquals() + { + var p = new CompactEC.ECPoint(11, 0); + var q = new CompactEC.ECPoint(3, 4); + + Assert.AreEqual(p, p, "same"); + Assert.AreEqual(p, p.Clone(), "equal"); + Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, CompactEC.ECPoint.PointAtInfinity, "at inf"); + + Assert.AreNotEqual(p, q, "not equal"); + Assert.AreNotEqual(q, p, "not equal reversed"); + } + + [Test] + public void TestPointEquality() + { + Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, CompactEC.ECPoint.PointAtInfinity); + + var p = new CompactEC.ECPoint(14, 3); + var q = new CompactEC.ECPoint(14, 3); + Assert.AreEqual(p, q); + Assert.AreEqual(q, p); + } + } +} diff --git a/CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs b/CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs new file mode 100644 index 0000000..982e234 --- /dev/null +++ b/CompactEC.Tests/FixedFactorLengthCryptoGroupAlgebraTests.cs @@ -0,0 +1,367 @@ +using System; +using System.Numerics; +using System.Diagnostics; + +using NUnit.Framework; +using Moq; +using Moq.Protected; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + + [TestFixture] + public class FixedFactorLengthGroupAlgebraTests + { + [Test] + [TestCase(-1)] + [TestCase(0)] + [TestCase(5)] + public void TestConstructorRejectsInvalidFactorBitLength(int factorBitLength) + { + var order = new BigInteger(8); + int orderBitLength = 4; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + Assert.Throws( + () => new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength) + ); + } + + [Test] + public void TestConstructorRejectsNullBaseAlgebra() + { + Assert.Throws( + () => new FixedFactorLengthCryptoGroupAlgebra(null, 2) + ); + } + + [Test] + public void TestFactorBitLengthCorrect() + { + var order = new BigInteger(8); + int orderBitLength = 4; + int factorBitLength = 2; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(baseAlgebraStub.Object.OrderBitLength == orderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(factorBitLength, fixedAlgebra.FactorBitLength); + } + + [Test] + public void TestOrderCallsBaseAlgebra() + { + var order = new BigInteger(7); + int orderBitLength = 3; + int factorBitLength = 2; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(order, fixedAlgebra.Order); + } + + [Test] + public void TestOrderBitLengthCallsBaseAlgebra() + { + var order = new BigInteger(7); + int orderBitLength = 3; + int factorBitLength = 1; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(baseAlgebraStub.Object.OrderBitLength, fixedAlgebra.OrderBitLength); + } + + [Test] + public void TestGeneratorCallsBaseAlgebra() + { + int generator = 23; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + baseAlgebraStub.Setup(alg => alg.Generator).Returns(generator); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(generator, fixedAlgebra.Generator); + } + + [Test] + public void TestNeutralElementCallsBaseAlgebra() + { + int neutralElement = 3; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + baseAlgebraStub.Setup(alg => alg.NeutralElement).Returns(neutralElement); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(neutralElement, fixedAlgebra.NeutralElement); + } + + [Test] + public void TestElementBitLengthCallsBaseAlgebra() + { + int elementBitLength = 3; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + baseAlgebraStub.Setup(alg => alg.ElementBitLength).Returns(elementBitLength); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.AreEqual(elementBitLength, fixedAlgebra.ElementBitLength); + } + + [Test] + public void TestAddCallsBaseAlgebra() + { + int leftElement = 11; + int rightElement = 6; + int expected = 17; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.Add(leftElement, rightElement); + + Assert.AreEqual(expected, result); + baseAlgebraMock.Verify( + alg => alg.Add(It.Is(x => x == leftElement), It.Is(x => x == rightElement)), + Times.Once() + ); + } + + [Test] + public void TestFromBytesCallsBaseAlgebra() + { + byte[] buffer = new byte[0]; + int expected = 17; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.FromBytes(It.IsAny())).Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.FromBytes(buffer); + + Assert.AreEqual(expected, result); + baseAlgebraMock.Verify( + alg => alg.FromBytes(It.Is(x => x == buffer)), + Times.Once() + ); + } + + [Test] + [TestCase(6)] + [TestCase(8)] + public void TestGenerateElementRejectsIndexWithDeviatingBitLength(int indexBitLength) + { + var order = new BigInteger(1024); + int orderBitLength = 11; + int factorBitLength = 7; + var index = new BigInteger(1 << (indexBitLength - 1)); + Debug.Assert(CryptoGroupAlgebra.GetBitLength(index) == indexBitLength); + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.Throws( + () => fixedAlgebra.GenerateElement(index) + ); + } + + [Test] + public void TestGenerateElementCallsBaseAlgebra() + { + var order = new BigInteger(1024); + int orderBitLength = 11; + int factorBitLength = 7; + var index = new BigInteger(1 << (factorBitLength - 1)); + int expected = 236; + int generator = 4; + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.Generator).Returns(generator); + baseAlgebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.GenerateElement(index); + + Assert.AreEqual(expected, result); + + baseAlgebraMock.Protected().As().Verify( + alg => alg.MultiplyScalarUnsafe(It.Is(x => x == generator), + It.Is(x => x == index), + It.Is(x => x == factorBitLength)), + Times.Once() + ); + } + + [Test] + public void TestIsValidCallsBaseAlgebra() + { + int element = 9; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + Assert.IsTrue(fixedAlgebra.IsValid(element)); + + baseAlgebraMock.Verify( + alg => alg.IsValid(It.Is(x => x == element)), + Times.Once() + ); + } + + [Test] + [TestCase(6)] + [TestCase(8)] + public void TestMultiplyScalarRejectsFactorWithDeviationBitLength(int bitLength) + { + var order = new BigInteger(1024); + int orderBitLength = 11; + int factorBitLength = 7; + int element = 2; + var k = new BigInteger(1 << (bitLength - 1)); + Debug.Assert(CryptoGroupAlgebra.GetBitLength(k) == bitLength); + + var baseAlgebraStub = new Mock>(MockBehavior.Strict); + baseAlgebraStub.Setup(alg => alg.Order).Returns(order); + Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); + Assert.Throws( + () => fixedAlgebra.MultiplyScalar(element, k) + ); + } + + [Test] + public void TestMultiplyScalarCallsBaseAlgebra() + { + var order = new BigInteger(1024); + int orderBitLength = 11; + int element = 2; + int expected = 124; + int factorBitLength = 7; + var factor = new BigInteger(1 << (factorBitLength - 1)); + Debug.Assert(CryptoGroupAlgebra.GetBitLength(factor) == factorBitLength); + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Protected().As() + .Setup(alg => alg.MultiplyScalarUnsafe(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.MultiplyScalar(element, factor); + + Assert.AreEqual(expected, result); + baseAlgebraMock.Protected().As().Verify( + alg => alg.MultiplyScalarUnsafe(It.Is(x => x == element), + It.Is(x => x == factor), + It.Is(x => x == factorBitLength)), + Times.Once() + ); + } + + [Test] + public void TestNegateCallsBaseAlgebra() + { + int element = 9; + int expected = 35; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.Negate(element); + + Assert.AreEqual(expected, result); + + baseAlgebraMock.Verify( + alg => alg.Negate(It.Is(x => x == element)), + Times.Once() + ); + } + + [Test] + public void TestToBytesCallsBaseAlgebra() + { + int element = 9; + var expected = new byte[0]; + var order = new BigInteger(24); + int orderBitLength = 5; + int factorBitLength = 1; + + + var baseAlgebraMock = new Mock>(MockBehavior.Strict); + baseAlgebraMock.Setup(alg => alg.Order).Returns(order); + baseAlgebraMock.Setup(alg => alg.ToBytes(It.IsAny())).Returns(expected); + Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); + + var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); + var result = fixedAlgebra.ToBytes(element); + + CollectionAssert.AreEqual(expected, result); + + baseAlgebraMock.Verify( + alg => alg.ToBytes(It.Is(x => x == element)), + Times.Once() + ); + } + } +} diff --git a/CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs b/CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs new file mode 100644 index 0000000..e282f5c --- /dev/null +++ b/CompactEC.Tests/MultiplicativeGroupAlgebraIntegratedTests.cs @@ -0,0 +1,96 @@ +using System; +using System.Numerics; + +using NUnit.Framework; + +using CompactEC; + +namespace CompactEC.Tests.CryptoAlgebra +{ + [TestFixture] + public class MultiplicativeGroupAlgebraIntegratedTest + { + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(6)] + [TestCase(9)] + [TestCase(10)] + [TestCase(11)] + [TestCase(232)] + public void TestGenerateIsGeneratorMultiplied(int idInt) + { + var id = new BigInteger(idInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + + Assert.AreEqual(groupAlgebra.MultiplyScalar(groupAlgebra.Generator, id), groupAlgebra.GenerateElement(id)); + } + + [Test] + public void TestGenerateRejectsNegativeIds() + { + var id = new BigInteger(-1); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + + Assert.Throws( + () => groupAlgebra.GenerateElement(id) + ); + } + + [Test] + public void TestMultiplyScalarRejectsNegativeScalars() + { + var k = new BigInteger(-1); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var x = new BigInteger(3); + + Assert.Throws( + () => groupAlgebra.MultiplyScalar(x, k) + ); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + [TestCase(4)] + [TestCase(7)] + public void TestMultiplyScalarWithSmallFactorSizeEqualToOrderFactorSize(int factorInt) + { + int factorBitLength = 3; + var k = new BigInteger(factorInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var x = new BigInteger(6); + Assert.AreEqual(groupAlgebra.MultiplyScalar(x, k), groupAlgebra.MultiplyScalar(x, k, factorBitLength)); + } + + [Test] + public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() + { + int factorBitLength = 3; + var k = new BigInteger(-1); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var x = new BigInteger(6); + + Assert.Throws( + () => groupAlgebra.MultiplyScalar(x, k, factorBitLength) + ); + } + + [Test] + [TestCase(8)] + [TestCase(9)] + [TestCase(123)] + public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) + { + int factorBitLength = 3; + var k = new BigInteger(factorInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var x = new BigInteger(6); + Assert.Throws( + () => groupAlgebra.MultiplyScalar(x, k, factorBitLength) + ); + } + } +} diff --git a/CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs b/CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs new file mode 100644 index 0000000..37f6f39 --- /dev/null +++ b/CompactEC.Tests/MultiplicativeGroupAlgebraTests.cs @@ -0,0 +1,147 @@ +using System; +using System.Numerics; + +using NUnit.Framework; + +using CompactEC; + +// Best Practices: https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices +namespace CompactEC.Tests.CryptoAlgebra +{ + [TestFixture] + public class MultiplicativeGroupAlgebraTests + { + [Test] + [TestCase(0, 1)] + [TestCase(1, 3)] + [TestCase(2, 9)] + [TestCase(3, 5)] + [TestCase(4, 4)] + [TestCase(5, 1)] + [TestCase(6, 3)] + [TestCase(12, 9)] + public void TestMultiplyScalar(int scalarInt, int expectedInt) + { + var k = new BigInteger(scalarInt); + var expected = new BigInteger(expectedInt); + + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + + var x = new BigInteger(3); + var result = groupAlgebra.MultiplyScalar(x, k); + Assert.AreEqual(expected, result); + } + + [Test] + [TestCase(2)] + [TestCase(3)] + [TestCase(7)] + public void TestGeneratorIsAsSet(int generatorInt) + { + var generator = new BigInteger(generatorInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, generator); + Assert.AreEqual(generator, groupAlgebra.Generator); + } + + [Test] + [TestCase(0)] + [TestCase(-3)] + [TestCase(11)] + [TestCase(136)] + public void TestInvalidElementRejectedAsGenerator(int generatorInt) + { + var generator = new BigInteger(generatorInt); + Assert.Throws( + () => new MultiplicativeGroupAlgebra(11, 10, generator) + ); + } + + [Test] + [TestCase(1)] + [TestCase(5)] + [TestCase(10)] + public void TestIsValidAcceptsValidElements(int elementInt) + { + var element = new BigInteger(elementInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.IsTrue(groupAlgebra.IsValid(element)); + } + + [Test] + [TestCase(0)] + [TestCase(-3)] + [TestCase(11)] + [TestCase(136)] + public void TestIsValidRejectsInvalidElements(int elementInt) + { + var element = new BigInteger(elementInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.IsFalse(groupAlgebra.IsValid(element)); + } + + [Test] + public void TestGroupElementBitLength() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.AreEqual(4, groupAlgebra.ElementBitLength); + } + + [Test] + public void TestOrderBitLength() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 5, 3); + Assert.AreEqual(3, groupAlgebra.OrderBitLength); + } + + [Test] + [TestCase(1)] + [TestCase(2)] + [TestCase(5)] + [TestCase(10)] + public void TestNegate(int elementInt) + { + var x = new BigInteger(elementInt); + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.AreEqual(groupAlgebra.MultiplyScalar(x, groupAlgebra.Order - 1), groupAlgebra.Negate(x)); + } + + [Test] + public void TestNeutralElement() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.AreEqual(BigInteger.One, groupAlgebra.NeutralElement); + } + + [Test] + public void TestFromBytes() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var expected = new BigInteger(7); + var buffer = expected.ToByteArray(); + + var result = groupAlgebra.FromBytes(buffer); + Assert.AreEqual(expected, result); + } + + [Test] + public void TestFromBytesRejectsNullArgument() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + Assert.Throws( + () => groupAlgebra.FromBytes(null) + ); + } + + [Test] + public void TestToBytes() + { + var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); + var element = new BigInteger(7); + var expected = element.ToByteArray(); + + var result = groupAlgebra.ToBytes(element); + + CollectionAssert.AreEqual(expected, result); + } + } +} diff --git a/Tests/Properties/AssemblyInfo.cs b/CompactEC.Tests/Properties/AssemblyInfo.cs similarity index 93% rename from Tests/Properties/AssemblyInfo.cs rename to CompactEC.Tests/Properties/AssemblyInfo.cs index 5e4e68f..14b41d9 100644 --- a/Tests/Properties/AssemblyInfo.cs +++ b/CompactEC.Tests/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // Allgemeine Informationen über eine Assembly werden über die folgenden // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, // die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("Tests")] +[assembly: AssemblyTitle("CompactEC.Tests")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Tests")] +[assembly: AssemblyProduct("CompactEC.Tests")] [assembly: AssemblyCopyright("Copyright © 2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Tests/packages.config b/CompactEC.Tests/packages.config similarity index 100% rename from Tests/packages.config rename to CompactEC.Tests/packages.config diff --git a/CompactEC.sln b/CompactEC.sln index 2766919..e941fa2 100644 --- a/CompactEC.sln +++ b/CompactEC.sln @@ -7,11 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompactEC", "CompactEC\Comp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests_MS", "CompactEC.Tests_MS\CompactEC.Tests_MS.csproj", "{22FD5C5A-FC60-4F60-B44E-47405CCED477}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "TestApp\Example.csproj", "{43DEFB14-5176-4863-9A51-D141802BA000}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{43DEFB14-5176-4863-9A51-D141802BA000}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompactEC.Test", "CompactEC.Test\CompactEC.Test.csproj", "{182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests", "CompactEC.Tests\CompactEC.Tests.csproj", "{FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -31,10 +29,6 @@ Global {43DEFB14-5176-4863-9A51-D141802BA000}.Debug|Any CPU.Build.0 = Debug|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Release|Any CPU.ActiveCfg = Release|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Release|Any CPU.Build.0 = Release|Any CPU - {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {182D8C60-C11A-4DEF-A0D1-A57B21CC19D0}.Release|Any CPU.Build.0 = Release|Any CPU {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/CryptoAlgebra.Tests/CryptoAlgebra.Tests.csproj b/CryptoAlgebra.Tests/CryptoAlgebra.Tests.csproj deleted file mode 100644 index d08f73a..0000000 --- a/CryptoAlgebra.Tests/CryptoAlgebra.Tests.csproj +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - Debug - AnyCPU - {08E15742-DC5C-4136-A035-24C6EB38F3A8} - Library - Properties - CryptoAlgebra.Tests - CryptoAlgebra.Tests - v4.6.1 - 512 - {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 15.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages - False - UnitTest - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll - - - ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll - - - - - - - - - - - - - - - - Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". - - - - - - \ No newline at end of file diff --git a/CryptoAlgebra.Tests/Properties/AssemblyInfo.cs b/CryptoAlgebra.Tests/Properties/AssemblyInfo.cs deleted file mode 100644 index 9ca6490..0000000 --- a/CryptoAlgebra.Tests/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("CryptoAlgebra.Tests")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("CryptoAlgebra.Tests")] -[assembly: AssemblyCopyright("Copyright © 2019")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -[assembly: ComVisible(false)] - -[assembly: Guid("08e15742-dc5c-4136-a035-24c6eb38f3a8")] - -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/CryptoAlgebra.Tests/UnitTest1.cs b/CryptoAlgebra.Tests/UnitTest1.cs deleted file mode 100644 index c6bf4f5..0000000 --- a/CryptoAlgebra.Tests/UnitTest1.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace CryptoAlgebra.Tests -{ - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - - } - } -} diff --git a/CryptoAlgebra.Tests/packages.config b/CryptoAlgebra.Tests/packages.config deleted file mode 100644 index 102a45c..0000000 --- a/CryptoAlgebra.Tests/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/TestApp/App.config b/Example/App.config similarity index 100% rename from TestApp/App.config rename to Example/App.config diff --git a/TestApp/Example.csproj b/Example/Example.csproj similarity index 100% rename from TestApp/Example.csproj rename to Example/Example.csproj diff --git a/TestApp/Program.cs b/Example/Program.cs similarity index 100% rename from TestApp/Program.cs rename to Example/Program.cs diff --git a/TestApp/Properties/AssemblyInfo.cs b/Example/Properties/AssemblyInfo.cs similarity index 94% rename from TestApp/Properties/AssemblyInfo.cs rename to Example/Properties/AssemblyInfo.cs index df7bdd3..029a8a7 100644 --- a/TestApp/Properties/AssemblyInfo.cs +++ b/Example/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // Allgemeine Informationen über eine Assembly werden über die folgenden // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, // die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("TestApp")] +[assembly: AssemblyTitle("Example")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TestApp")] +[assembly: AssemblyProduct("Example")] [assembly: AssemblyCopyright("Copyright © 2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] From 3c4d7ddcbf402199570173a4afdaa4141fe5bf01 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Sun, 15 Dec 2019 19:12:42 +0100 Subject: [PATCH 3/4] Removed old MSTest suite. --- CompactEC.Tests_MS/CompactEC.Tests_MS.csproj | 96 ---- CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs | 240 --------- CompactEC.Tests_MS/CryptoGroupElementTests.cs | 408 --------------- CompactEC.Tests_MS/CryptoGroupTests.cs | 494 ------------------ .../DefaultECParrameterTests.cs | 27 - CompactEC.Tests_MS/ECGroupAlgebraTests.cs | 367 ------------- CompactEC.Tests_MS/ECPointTests.cs | 56 -- ...ixedFactorLengthCryptoGroupAlgebraTests.cs | 367 ------------- ...ltiplicativeGroupAlgebraIntegratedTests.cs | 96 ---- .../MultiplicativeGroupAlgebraTests.cs | 147 ------ CompactEC.Tests_MS/Properties/AssemblyInfo.cs | 20 - CompactEC.Tests_MS/packages.config | 9 - CompactEC.sln | 6 - 13 files changed, 2333 deletions(-) delete mode 100644 CompactEC.Tests_MS/CompactEC.Tests_MS.csproj delete mode 100644 CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs delete mode 100644 CompactEC.Tests_MS/CryptoGroupElementTests.cs delete mode 100644 CompactEC.Tests_MS/CryptoGroupTests.cs delete mode 100644 CompactEC.Tests_MS/DefaultECParrameterTests.cs delete mode 100644 CompactEC.Tests_MS/ECGroupAlgebraTests.cs delete mode 100644 CompactEC.Tests_MS/ECPointTests.cs delete mode 100644 CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs delete mode 100644 CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs delete mode 100644 CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs delete mode 100644 CompactEC.Tests_MS/Properties/AssemblyInfo.cs delete mode 100644 CompactEC.Tests_MS/packages.config diff --git a/CompactEC.Tests_MS/CompactEC.Tests_MS.csproj b/CompactEC.Tests_MS/CompactEC.Tests_MS.csproj deleted file mode 100644 index 0dc9533..0000000 --- a/CompactEC.Tests_MS/CompactEC.Tests_MS.csproj +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - Debug - AnyCPU - {22FD5C5A-FC60-4F60-B44E-47405CCED477} - Library - Properties - CompactEC.Tests - CompactEC.Tests - v4.6.1 - 512 - {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 15.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages - False - UnitTest - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll - - - ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll - - - ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll - - - ..\packages\Moq.4.12.0\lib\net45\Moq.dll - - - - - - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll - - - ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll - - - - - - - - - - - - - - - - - - - - {5f5cfcf8-677e-4028-b74a-e6ab2975360c} - CompactEC - - - - - - - Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". - - - - - - \ No newline at end of file diff --git a/CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs b/CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs deleted file mode 100644 index 7c58c95..0000000 --- a/CompactEC.Tests_MS/CryptoGroupAlgebraTests.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System; -using System.Numerics; - -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; -using Moq.Protected; - -using CompactEC; - -namespace CompactEC.Tests.CryptoAlgebra -{ - interface CryptoGroupAlgebraProtectedMembers - { - int Multiplex(BigInteger selection, int left, int right); - int MultiplyScalarUnsafe(int e, BigInteger k, int factorBitLength); - } - - [TestClass] - public class CryptoGroupAlgebraTests - { - [TestMethod] - [DataRow(0, 0)] - [DataRow(1, 1)] - [DataRow(2, 2)] - [DataRow(15, 4)] - [DataRow(16, 5)] - public void TestBitLength(int valueInt, int expectedBitLength) - { - var value = new BigInteger(valueInt); - var result = CryptoGroupAlgebra.GetBitLength(value); - - Assert.AreEqual(expectedBitLength, result); - } - - [TestMethod] - [DataRow(0, 0)] - [DataRow(1, 1)] - [DataRow(2, 2)] - [DataRow(15, 4)] - [DataRow(16, 5)] - public void TestOrderBitLength(int orderInt, int expectedBitLength) - { - var order = new BigInteger(orderInt); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - - var result = algebraMock.Object.OrderBitLength; - - Assert.AreEqual(expectedBitLength, result); - } - - [TestMethod] - [DataRow(0)] - [DataRow(1)] - [DataRow(2)] - [DataRow(3)] - [DataRow(4)] - [DataRow(5)] - [DataRow(6)] - [DataRow(12)] - public void TestMultiplyScalar(int scalarInt) - { - // this also tests the implementation in MultiplyScalarUnsafe - - var k = new BigInteger(scalarInt); - var order = new BigInteger(5); - int element = 3; - int expected = 3 * (scalarInt % 5); - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.NeutralElement).Returns(0); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => x); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => y); - - var result = algebraMock.Object.MultiplyScalar(element, k); - - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestMultiplyScalarRejectsNegativeScalar() - { - int element = 5; - var index = BigInteger.MinusOne; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, index) - ); - } - - [TestMethod] - [DataRow(0)] - [DataRow(1)] - [DataRow(2)] - [DataRow(4)] - [DataRow(7)] - public void TestMultiplyScalarWithSmallFactor(int factorInt) - { - int element = 5; - var k = new BigInteger(factorInt); - int factorBitLength = 3; - int expected = 5 * factorInt; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.NeutralElement).Returns(0); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns((int x, int y) => x + y); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsZero), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => x); - - algebraMock.Protected().As() - .Setup(alg => alg.Multiplex( - It.Is(s => s.IsOne), It.IsAny(), It.IsAny()) - ) - .Returns((BigInteger s, int x, int y) => y); - - var result = algebraMock.Object.MultiplyScalar(element, k, factorBitLength); - - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() - { - int element = 5; - var index = BigInteger.MinusOne; - int factorBitLength = 3; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, index, factorBitLength) - ); - } - - [TestMethod] - [DataRow(8)] - [DataRow(9)] - [DataRow(123)] - public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) - { - var k = new BigInteger(factorInt); - int element = 5; - int factorBitLength = 3; - - var algebraMock = new Mock>(MockBehavior.Strict); - - Assert.ThrowsException( - () => algebraMock.Object.MultiplyScalar(element, k, factorBitLength) - ); - } - - - [TestMethod] - public void TestGenerateElement() - { - var order = new BigInteger(7); - int orderBitLength = 3; - int generator = 2; - var index = new BigInteger(3); - int expected = 3 * generator; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.Generator).Returns(generator); - - algebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)) - ) - .Returns(expected); - - var result = algebraMock.Object.GenerateElement(index); - - Assert.AreEqual(expected, result); - algebraMock.Protected().As() - .Verify(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == index), It.Is(i => i == orderBitLength)), - Times.Once() - ); - } - - [TestMethod] - public void TestGenerateElementRejectsNegativeIndex() - { - var index = BigInteger.MinusOne; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Generator).Returns(1); - - Assert.ThrowsException( - () => algebraMock.Object.GenerateElement(index) - ); - } - - [TestMethod] - public void TestNegate() - { - var order = new BigInteger(17); - int element = 7; - int expected = -7; - - var algebraMock = new Mock>() { CallBase = true }; - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)) - ) - .Returns(expected); - - int result = algebraMock.Object.Negate(element); - - Assert.AreEqual(expected, result); - algebraMock.Protected().As() - .Verify(alg => alg.MultiplyScalarUnsafe( - It.IsAny(), It.Is(i => i == order - 1), It.Is(i => i == 5)), - Times.Once() - ); - } - - } -} diff --git a/CompactEC.Tests_MS/CryptoGroupElementTests.cs b/CompactEC.Tests_MS/CryptoGroupElementTests.cs deleted file mode 100644 index e5b4909..0000000 --- a/CompactEC.Tests_MS/CryptoGroupElementTests.cs +++ /dev/null @@ -1,408 +0,0 @@ -using System; -using System.Numerics; - -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; - -using CompactEC; - -namespace CompactEC.Tests.CryptoAlgebra -{ - [TestClass] - public class CryptoGroupElementTests - { - - [TestMethod] - public void TestConstructorRejectsInvalidValue() - { - int element = -3; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(false); - - Assert.ThrowsException( - () => new CryptoGroupElement(element, algebraMock.Object) - ); - algebraMock.Verify(alg => alg.IsValid(It.Is(x => x == element)), Times.Once()); - } - - [TestMethod] - public void TestConstructorRejectsNullAlgebra() - { - int element = -3; - - Assert.ThrowsException( - () => new CryptoGroupElement(element, null) - ); - } - - [TestMethod] - public void TestConstructorSetsValueCorrectly() - { - int elementRaw = -3; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(elementRaw, algebraMock.Object); - - Assert.AreEqual(elementRaw, element.Value); - algebraMock.Verify(alg => alg.IsValid(It.Is(x => x == elementRaw)), Times.Once()); - } - - [TestMethod] - public void TestConstructorFromBytesCorrect() - { - byte[] buffer = new byte[0]; - int expectedValue = 9; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.FromBytes(It.IsAny())).Returns(expectedValue); - - var element = new CryptoGroupElement(buffer, algebraMock.Object); - - Assert.AreEqual(expectedValue, element.Value); - algebraMock.Verify(alg => alg.FromBytes(It.Is(x => x == buffer))); - } - - [TestMethod] - public void TestConstructorFromBytesRejectsNullAlgebra() - { - byte[] buffer = new byte[0]; - - Assert.ThrowsException( - () => new CryptoGroupElement(buffer, null) - ); - } - - [TestMethod] - public void TestConstructorFromBytesRejectsInvalidValue() - { - byte[] buffer = new byte[0]; - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.FromBytes(It.IsAny())).Returns(0); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(false); - - Assert.ThrowsException( - () => new CryptoGroupElement(buffer, algebraStub.Object) - ); - } - - [TestMethod] - public void TestAddRejectsOtherCryptoGroupElementSubclasses() - { - var otherElementStub = new Mock(MockBehavior.Strict); - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - - Assert.ThrowsException( - () => element.Add(otherElementStub.Object) - ); - } - - [TestMethod] - public void TestAddRejectsNull() - { - ICryptoGroupElement otherElement = null; - - var algebraStub = new Mock>(); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - - Assert.ThrowsException( - () => element.Add(otherElement) - ); - } - - [TestMethod] - public void TestAddRejectsElementFromDifferentGroup() - { - var otherAlgebraStub = new Mock>(MockBehavior.Strict); - otherAlgebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - var otherElement = new CryptoGroupElement(0, otherAlgebraStub.Object); - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - - Assert.ThrowsException( - () => element.Add(otherElement) - ); - } - - [TestMethod] - public void TestAdd() - { - int otherValue = 3; - int value = 7; - int expected = value + otherValue; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); - - var element = new CryptoGroupElement(value, algebraMock.Object); - var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); - - element.Add(otherElement); - - Assert.AreEqual(expected, element.Value); - Assert.AreEqual(otherValue, otherElement.Value); - algebraMock.Verify(alg => alg.Add(It.Is(x => x == value), It.Is(x => x == otherValue)), Times.Once()); - } - - [TestMethod] - public void TestMultiplyScalar() - { - int value = 3; - var scalar = new BigInteger(7); - int expected = value * (int)scalar; - var order = new BigInteger(10); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); - - var element = new CryptoGroupElement(value, algebraMock.Object); - - element.MultiplyScalar(scalar); - - Assert.AreEqual(expected, element.Value); - algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); - } - - [TestMethod] - public void TestNegate() - { - int value = 3; - int expected = -value; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); - - var element = new CryptoGroupElement(value, algebraMock.Object); - - element.Negate(); - - Assert.AreEqual(expected, element.Value); - algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); - } - - [TestMethod] - public void TestEqualsFalseForOtherCryptoGroupElementSubclasses() - { - var otherElementStub = new Mock(MockBehavior.Strict); - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - bool result = element.Equals(otherElementStub.Object); - - Assert.IsFalse(result); - } - - [TestMethod] - public void TestEqualsFalseForNull() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - bool result = element.Equals(null); - - Assert.IsFalse(result); - } - - [TestMethod] - public void TestEqualsFalseForDifferentValues() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var otherElement = new CryptoGroupElement(3, algebraStub.Object); - var element = new CryptoGroupElement(8, algebraStub.Object); - - bool result = element.Equals(otherElement); - - Assert.IsFalse(result); - } - - [TestMethod] - public void TestEqualsFalseForDifferentAlgebras() - { - var otherAlgebraStub = new Mock>(MockBehavior.Strict); - otherAlgebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - var otherElement = new CryptoGroupElement(0, otherAlgebraStub.Object); - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(0, algebraStub.Object); - - bool result = element.Equals(otherElement); - - Assert.IsFalse(result); - } - - [TestMethod] - public void TestEqualsTrue() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var otherElement = new CryptoGroupElement(5, algebraStub.Object); - var element = new CryptoGroupElement(5, algebraStub.Object); - - bool result = element.Equals(otherElement); - - Assert.IsTrue(result); - } - - [TestMethod] - public void TestOperatorPlus() - { - int otherValue = 3; - int value = 7; - int expected = value + otherValue; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); - - var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); - var element = new CryptoGroupElement(value, algebraMock.Object); - - var result = element + otherElement; - - Assert.AreEqual(otherValue, otherElement.Value); - Assert.AreEqual(value, element.Value); - Assert.AreEqual(expected, result.Value); - algebraMock.Verify(alg => alg.Add(It.Is(x => x == value), It.Is(x => x == otherValue)), Times.Once()); - } - - [TestMethod] - public void TestOperatorUnaryMinus() - { - int value = 3; - int expected = -value; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); - - var element = new CryptoGroupElement(value, algebraMock.Object); - - var result = -element; - - Assert.AreEqual(value, element.Value); - Assert.AreEqual(expected, result.Value); - algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); - } - - [TestMethod] - public void TestOperatorMinus() - { - int otherValue = 7; - int value = 3; - int expected = otherValue - value; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); - algebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns((int x) => -x); - - var otherElement = new CryptoGroupElement(otherValue, algebraMock.Object); - var element = new CryptoGroupElement(value, algebraMock.Object); - - var result = otherElement - element; - - Assert.AreEqual(otherValue, otherElement.Value); - Assert.AreEqual(value, element.Value); - Assert.AreEqual(expected, result.Value); - algebraMock.Verify(alg => alg.Negate(It.Is(x => x == value)), Times.Once()); - algebraMock.Verify(alg => alg.Add(It.Is(x => x == -value), It.Is(x => x == otherValue)), Times.Once()); - } - - [TestMethod] - public void TestOperatorMulitplyLeft() - { - int value = 3; - var scalar = new BigInteger(7); - int expected = value * (int)scalar; - var order = new BigInteger(10); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); - - var element = new CryptoGroupElement(value, algebraMock.Object); - - var result = element * scalar; - - Assert.AreEqual(value, element.Value); - Assert.AreEqual(expected, result.Value); - algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); - } - - [TestMethod] - public void TestOperatorMultiplyRight() - { - int value = 3; - var scalar = new BigInteger(7); - int expected = value * (int)scalar; - var order = new BigInteger(10); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expected); - - var element = new CryptoGroupElement(value, algebraMock.Object); - - var result = scalar * element; - - Assert.AreEqual(value, element.Value); - Assert.AreEqual(expected, result.Value); - algebraMock.Verify(alg => alg.MultiplyScalar(It.Is(x => x == value), It.Is(x => x == scalar)), Times.Once()); - } - - [TestMethod] - public void TestCloneResultsInEqualButNotSameElement() - { - int value = 3; - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var element = new CryptoGroupElement(value, algebraStub.Object); - var clone = element.Clone(); - - Assert.AreEqual(element, clone); - Assert.AreNotSame(element, clone); - } - - [TestMethod] - public void TestToBytesCallsAlgebra() - { - var value = 3; - var expected = new byte[0]; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(alg => alg.ToBytes(It.IsAny())).Returns(expected); - - var element = new CryptoGroupElement(value, algebraMock.Object); - var result = element.ToBytes(); - - algebraMock.Verify(alg => alg.ToBytes(It.Is(x => x == value)), Times.Once()); - } - } -} diff --git a/CompactEC.Tests_MS/CryptoGroupTests.cs b/CompactEC.Tests_MS/CryptoGroupTests.cs deleted file mode 100644 index b024933..0000000 --- a/CompactEC.Tests_MS/CryptoGroupTests.cs +++ /dev/null @@ -1,494 +0,0 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; -using Moq.Protected; - -using CompactEC; -using System.Numerics; -using System.Security.Cryptography; -using System.Linq; - -namespace CompactEC.Tests.CryptoAlgebra -{ - interface CryptoGroupProtectedMembers - { - CryptoGroupElement CreateGroupElement(int value); - CryptoGroupElement CreateGroupElement(byte[] buffer); - } - - class CryptoGroupFake : CryptoGroup - { - public CryptoGroupFake(ICryptoGroupAlgebra algebra) : base(algebra) - { } - - protected override CryptoGroupElement CreateGroupElement(int value) - { - throw new NotImplementedException(); - } - - protected override CryptoGroupElement CreateGroupElement(byte[] buffer) - { - throw new NotImplementedException(); - } - } - - [TestClass] - public class CryptoGroupTests - { - [TestMethod] - public void TestConstructorRejectsNullAlgebra() - { - Assert.ThrowsException( - () => new CryptoGroupFake(null) - ); - } - - [TestMethod] - public void TestSpecificAddCallsAlgebraAndWrapsAsElement() - { - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.Add(2, 6)).Returns(8); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - - var leftStub = new CryptoGroupElement(2, algebraMock.Object); - var rightStub = new CryptoGroupElement(6, algebraMock.Object); - var resultStub = new CryptoGroupElement(8, algebraMock.Object); - - var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(resultStub); - - Assert.AreSame(resultStub, groupMock.Object.Add(leftStub, rightStub)); - algebraMock.Verify(algebra => algebra.Add(2, 6), Times.Once()); - groupMock.Protected().As() - .Verify(group => group.CreateGroupElement(It.Is(i => i == 8)), Times.Once()); - } - - [TestMethod] - public void TestAddRejectsNullArgumentLeft() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraStub.Object); - var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); - - Assert.ThrowsException( - () => groupMock.Add(null, otherElementStub) - ); - } - - [TestMethod] - public void TestAddRejectsNullArgumentRight() - { - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraMock.Object); - var otherElementStub = new CryptoGroupElement(3, algebraMock.Object); - - Assert.ThrowsException( - () => groupMock.Add(otherElementStub, null) - ); - } - - [TestMethod] - public void TestFromBytesWrapsAsElement() - { - byte[] inputBuffer = new byte[0]; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - - var resultStub = new Mock>(MockBehavior.Strict, 0, algebraMock.Object); - - var groupMock = new Mock>(MockBehavior.Strict, algebraMock.Object); - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(resultStub.Object); - - Assert.AreSame(resultStub.Object, groupMock.Object.FromBytes(inputBuffer)); - groupMock.Protected().As() - .Verify(group => group.CreateGroupElement(It.Is(b => b == inputBuffer)), Times.Once()); - } - - [TestMethod] - public void TestGenerateCallsAlgebraAndWrapsElement() - { - var index = new BigInteger(7); - int expectedRaw = 3; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(algebra => algebra.GenerateElement(It.IsAny())).Returns(expectedRaw); - - var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); - var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); - - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(expectedStub); - - Assert.AreSame(expectedStub, groupMock.Object.Generate(index)); - - algebraMock.Verify( - algebra => algebra.GenerateElement(It.Is(x => x == index)), - Times.Once() - ); - - groupMock.Protected().As().Verify( - group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), - Times.Once() - ); - } - - [TestMethod] - public void TestSpecificMultiplyScalarCallsAlgebraAndWrapsElement() - { - var k = new BigInteger(7); - int expectedRaw = 3; - int elementRaw = 8; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(algebra => algebra.MultiplyScalar(It.IsAny(), It.IsAny())).Returns(expectedRaw); - - var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); - var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); - var elementStub = new CryptoGroupElement(elementRaw, algebraMock.Object); - - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(expectedStub); - - Assert.AreSame(expectedStub, groupMock.Object.MultiplyScalar(elementStub, k)); - - algebraMock.Verify( - algebra => algebra.MultiplyScalar(It.Is(x => x == elementRaw), It.Is(x => x == k)), - Times.Once() - ); - groupMock.Protected().As().Verify( - group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), - Times.Once() - ); - } - - [TestMethod] - public void TestMultiplyScalarRejectsNullElement() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraStub.Object); - Assert.ThrowsException( - () => groupMock.MultiplyScalar(null, new BigInteger(1)) - ); - } - - [TestMethod] - public void TestSpecificNegateCallsAlgebraAndWrapsElement() - { - int expectedRaw = 3; - int elementRaw = 8; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - algebraMock.Setup(algebra => algebra.Negate(It.IsAny())).Returns(expectedRaw); - - var groupMock = new Mock>(MockBehavior.Loose, algebraMock.Object); - var expectedStub = new CryptoGroupElement(expectedRaw, algebraMock.Object); - var elementStub = new CryptoGroupElement(elementRaw, algebraMock.Object); - - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(expectedStub); - - Assert.AreSame(expectedStub, groupMock.Object.Negate(elementStub)); - - algebraMock.Verify( - algebra => algebra.Negate(It.Is(x => x == elementRaw)), - Times.Once() - ); - groupMock.Protected().As().Verify( - group => group.CreateGroupElement(It.Is(i => i == expectedRaw)), - Times.Once() - ); - } - - [TestMethod] - public void TestNegateRejectsNullElement() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraStub.Object); - Assert.ThrowsException( - () => groupMock.Negate(null) - ); - } - - [TestMethod] - public void TestAddRejectsDifferentGroupElementLeft() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraStub.Object); - var elementStub = new Mock(MockBehavior.Strict); - var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); - - Assert.ThrowsException( - () => groupMock.Add(elementStub.Object, otherElementStub) - ); - } - - [TestMethod] - public void TestAddRejectsDifferentGroupElementRight() - { - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(algebra => algebra.IsValid(It.IsAny())).Returns(true); - var groupMock = new CryptoGroupFake(algebraStub.Object); - var elementStub = new Mock(MockBehavior.Strict); - var otherElementStub = new CryptoGroupElement(3, algebraStub.Object); - - Assert.ThrowsException( - () => groupMock.Add(otherElementStub, elementStub.Object) - ); - } - - [TestMethod] - public void TestMultiplyScalarRejectsDifferentGroupElement() - { - var algebraStub = new Mock>(MockBehavior.Strict); - var groupMock = new CryptoGroupFake(algebraStub.Object); - var elementStub = new Mock(MockBehavior.Strict); - - Assert.ThrowsException( - () => groupMock.MultiplyScalar(elementStub.Object, new BigInteger(8)) - ); - } - - [TestMethod] - public void TestNegateRejectsDifferentGroupElement() - { - var algebraStub = new Mock>(MockBehavior.Strict); - var groupMock = new CryptoGroupFake(algebraStub.Object); - var elementStub = new Mock(MockBehavior.Strict); - - Assert.ThrowsException( - () => groupMock.Negate(elementStub.Object) - ); - } - - [TestMethod] - public void TestOrderCallsAlgebra() - { - var order = new BigInteger(29); - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - - var groupMock = new CryptoGroupFake(algebraMock.Object); - var result = groupMock.Order; - - Assert.AreEqual(order, result); - } - - [TestMethod] - public void TestOrderBitLengthCallsAlgebra() - { - int rawBitLength = 11; - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.OrderBitLength).Returns(rawBitLength); - - var groupMock = new CryptoGroupFake(algebraStub.Object); - - int result = groupMock.OrderBitLength; - int expected = algebraStub.Object.OrderBitLength; - Assert.AreEqual(expected, result); - } - - [TestMethod] - [DataRow(8, 1)] - [DataRow(9, 2)] - public void TestOrderByteLengthCallsAlgebra(int bitLength, int expectedByteLength) - { - var order = new BigInteger(1 << (bitLength - 1)); - - var algebraStub = new Mock>(MockBehavior.Strict); - algebraStub.Setup(alg => alg.OrderBitLength).Returns(bitLength); - - var groupMock = new CryptoGroupFake(algebraStub.Object); - - int result = groupMock.OrderByteLength; - Assert.AreEqual(expectedByteLength, result); - } - - [TestMethod] - public void TestElementBitLengthCallsAlgebra() - { - int expected = 11; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.ElementBitLength).Returns(expected); - - var groupMock = new CryptoGroupFake(algebraMock.Object); - - int result = groupMock.ElementBitLength; - Assert.AreEqual(expected, result); - - algebraMock.Verify(alg => alg.ElementBitLength, Times.Once()); - } - - [TestMethod] - [DataRow(8, 1)] - [DataRow(9, 2)] - public void TestElementByteLengthCallsAlgebra(int bitLength, int expectedByteLength) - { - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.ElementBitLength).Returns(bitLength); - - var groupMock = new CryptoGroupFake(algebraMock.Object); - - int result = groupMock.ElementByteLength; - Assert.AreEqual(expectedByteLength, result); - - algebraMock.Verify(alg => alg.ElementBitLength, Times.Once()); - } - - [TestMethod] - public void TestGenerateRandom() - { - var order = new BigInteger(1021); - int orderByteLength = 2; - - var expectedRaw = 7; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.OrderBitLength).Returns(orderByteLength * 8); - algebraMock.Setup(alg => alg.GenerateElement(It.IsAny())).Returns(expectedRaw); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var expected = new Mock>(expectedRaw, algebraMock.Object); - - var groupMock = new Mock>(algebraMock.Object); - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(expected.Object); - - var index = new BigInteger(301); - byte[] rngResponse = index.ToByteArray(); - - var rngMock = new Mock(); - rngMock - .Setup(rng => rng.GetBytes(It.IsAny())) - .Callback( - new Action( - (buffer) => { Buffer.BlockCopy(rngResponse, 0, buffer, 0, orderByteLength); } - ) - ); - - var result = groupMock.Object.GenerateRandom(rngMock.Object); - var resultIndex = result.Item1; - var resultElement = result.Item2; - Assert.AreEqual(index, resultIndex); - Assert.AreSame(expected.Object, resultElement); - - algebraMock.Verify( - alg => alg.GenerateElement(It.Is(x => x == index)), - Times.Once() - ); - - groupMock.Protected().As() - .Verify(group => group.CreateGroupElement(It.Is(x => x == expectedRaw)), - Times.Once()); - - rngMock.Verify(rng => rng.GetBytes(It.Is(x => x.Length == orderByteLength)), Times.Once()); - } - - [TestMethod] - [DataRow(-3)] - [DataRow(0)] - [DataRow(1)] - [DataRow(1020)] - [DataRow(1022)] - public void TestGenerateRandomDoesNotSampleInvalidIndices(int invalidIndexRaw) - { - // tests that invalid indices returned by rng are skipped: - // negative, 0, 1, order -1, order +1 - var order = new BigInteger(1021); - int orderByteLength = 2; - - var expectedRaw = 7; - - var algebraMock = new Mock>(MockBehavior.Strict); - algebraMock.Setup(alg => alg.Order).Returns(order); - algebraMock.Setup(alg => alg.OrderBitLength).Returns(orderByteLength * 8); - algebraMock.Setup(alg => alg.GenerateElement(It.IsAny())).Returns(expectedRaw); - algebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - - var expected = new Mock>(expectedRaw, algebraMock.Object); - - var groupMock = new Mock>(algebraMock.Object); - groupMock.Protected().As() - .Setup(group => group.CreateGroupElement(It.IsAny())) - .Returns(expected.Object); - - var invalidIndex = new BigInteger(invalidIndexRaw); - byte[] invalidRngResponse = invalidIndex.ToByteArray(); - if (invalidRngResponse.Length < orderByteLength) - { - byte[] buffer = new byte[orderByteLength]; - Buffer.BlockCopy(invalidRngResponse, 0, buffer, 0, invalidRngResponse.Length); - if (invalidIndex < 0) - { - buffer[1] = 0xFF; - } - else - { - buffer[1] = 0x00; - } - invalidRngResponse = buffer; - } - - var validIndex = new BigInteger(301); - byte[] validRngResponse = validIndex.ToByteArray(); - - bool firstTime = true; - var rngMock = new Mock(); - rngMock - .Setup(rng => rng.GetBytes(It.IsAny())) - .Callback( - new Action( - (buffer) => { - if (firstTime) - { - Buffer.BlockCopy(invalidRngResponse, 0, buffer, 0, orderByteLength); - firstTime = false; - } - else - { - Buffer.BlockCopy(validRngResponse, 0, buffer, 0, orderByteLength); - } - } - ) - ); - - var result = groupMock.Object.GenerateRandom(rngMock.Object); - var resultIndex = result.Item1; - var resultElement = result.Item2; - Assert.AreEqual(validIndex, resultIndex); - Assert.AreSame(expected.Object, resultElement); - - algebraMock.Verify( - alg => alg.GenerateElement(It.Is(x => x == validIndex)), - Times.Once() - ); - - groupMock.Protected().As() - .Verify(group => group.CreateGroupElement(It.Is(x => x == expectedRaw)), - Times.Once()); - - rngMock.Verify(rng => rng.GetBytes(It.Is(x => x.Length == orderByteLength)), Times.Exactly(2)); - } - - } -} diff --git a/CompactEC.Tests_MS/DefaultECParrameterTests.cs b/CompactEC.Tests_MS/DefaultECParrameterTests.cs deleted file mode 100644 index 88c664c..0000000 --- a/CompactEC.Tests_MS/DefaultECParrameterTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Numerics; - -using CompactEC; - -namespace CompactEC.Tests -{ - [TestClass] - public class DefaultECParrameterTests - { - [TestMethod] - public void TestNISTP256() - { - ECParameters parameters = ECParameters.CreateNISTP256(); - Assert.AreEqual(BigInteger.Parse("41058363725152142129326129780047268409114441015993725554835256314039467401291"), parameters.B); - Assert.AreEqual(BigInteger.Parse("48439561293906451759052585252797914202762949526041747995844080717082404635286"), parameters.Generator.X); - Assert.AreEqual(BigInteger.Parse("36134250956749795798585127919587881956611106672985015071877198253568414405109"), parameters.Generator.Y); - var groupAlgebra = new ECGroupAlgebra(parameters); - var elem = groupAlgebra.GenerateElement(parameters.Order); - Assert.AreEqual(groupAlgebra.NeutralElement, elem); - - elem = groupAlgebra.GenerateElement(BigInteger.Parse("28367823582636726875877070967237095880621")); - Assert.AreEqual(groupAlgebra.NeutralElement, groupAlgebra.Add(elem, groupAlgebra.Negate(elem))); - } - } -} diff --git a/CompactEC.Tests_MS/ECGroupAlgebraTests.cs b/CompactEC.Tests_MS/ECGroupAlgebraTests.cs deleted file mode 100644 index 0389a62..0000000 --- a/CompactEC.Tests_MS/ECGroupAlgebraTests.cs +++ /dev/null @@ -1,367 +0,0 @@ -using System; -using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace CompactEC.Tests -{ - [TestClass] - public class ECGroupAlgebraTests - { - // reference results from https://trustica.cz/en/2018/04/26/elliptic-curves-prime-order-curves/ - // generator has order 16, i.e., OrderSize = 5 bits - - private readonly ECParameters ecParams; - - public ECGroupAlgebraTests() - { - ecParams = new ECParameters() - { - P = 23, - A = -2, - B = 2, - Generator = new CompactEC.ECPoint(1, 1), - Order = 16 - }; - } - - [TestMethod] - public void TestAddDoublePoint() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = p.Clone(); - - var expectedQ = new CompactEC.ECPoint(15, 14); - var q = curve.Add(p, other); - - Assert.AreEqual(expectedQ, q); - } - - [TestMethod] - public void TestAddDoublePointAtInfinity() - { - var curve = new ECGroupAlgebra(ecParams); - var p = CompactEC.ECPoint.PointAtInfinity; - var other = p.Clone(); - - var expectedQ = CompactEC.ECPoint.PointAtInfinity; - var q = curve.Add(p, other); - - Assert.AreEqual(expectedQ, q); - } - - [TestMethod] - public void TestAddDifferentPoints() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = new CompactEC.ECPoint(15, 14); - - var expected = new CompactEC.ECPoint(16, 15); - var result = curve.Add(p, other); - - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestAddPointAtInfinityLeft() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = CompactEC.ECPoint.PointAtInfinity; - - var result = curve.Add(other, p); - - Assert.AreEqual(p, result); - Assert.AreNotSame(p, result); - } - - [TestMethod] - public void TestAddPointAtInfinityRight() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = CompactEC.ECPoint.PointAtInfinity; - - var result = curve.Add(p, other); - - Assert.AreEqual(p, result); - Assert.AreNotSame(p, result); - } - - [TestMethod] - public void TestAddNegated() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = curve.Negate(p); - - var expected = CompactEC.ECPoint.PointAtInfinity; - var result = curve.Add(p, other); - - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestAreNegationsFalseForEqualPoint() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = p.Clone(); - - Assert.IsFalse(curve.AreNegations(p, other)); - } - - [TestMethod] - public void TestAreNegationsTrueForNegation() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var other = curve.Negate(p); - - Assert.IsTrue(curve.AreNegations(p, other)); - } - - [TestMethod] - public void TestAreNegationsTrueForZeroYPoint() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(11, 0); - var other = p.Clone(); - - Assert.IsTrue(curve.AreNegations(p, other)); - } - - [TestMethod] - public void TestAddAffine() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(11, 0); - - var q = curve.Add(p, p); - - Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, q); - } - - [TestMethod] - [DataRow(5, 5)] - [DataRow(11, 0)] - [DataRow(16, 15)] - public void TestPointValidTrueForValidPoint(int xRaw, int yRaw) - { - var curve = new ECGroupAlgebra(ecParams); - var point = new CompactEC.ECPoint(xRaw, yRaw); - Assert.IsTrue(curve.IsValid(point)); - } - - [TestMethod] - public void TestIsValidTrueForPointAtInfinity() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.IsTrue(curve.IsValid(CompactEC.ECPoint.PointAtInfinity)); - } - - [TestMethod] - [DataRow(16, 1)] - [DataRow(5, 2)] - [DataRow(-2, 1)] - [DataRow(16, -15)] - [DataRow(78, 4)] - [DataRow(4, 78)] - public void TestIsvalidFalseForInvalidPoint(int xRaw, int yRaw) - { - var curve = new ECGroupAlgebra(ecParams); - var point = new CompactEC.ECPoint(xRaw, yRaw); - Assert.IsFalse(curve.IsValid(point)); - } - - [TestMethod] - public void TestNegate() - { - var curve = new ECGroupAlgebra(ecParams); - - var p = new CompactEC.ECPoint(5, 5); - var expected = new CompactEC.ECPoint(5, 23 - 5); - - var result = curve.Negate(p); - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestNegateForZeroYPoint() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(11, 0); - - var result = curve.Negate(p); - Assert.AreEqual(p, result); - Assert.AreNotSame(p, result); - } - - [TestMethod] - public void TestNegatePointAtInfinity() - { - var curve = new ECGroupAlgebra(ecParams); - var p = CompactEC.ECPoint.PointAtInfinity; - - var result = curve.Negate(p); - Assert.AreEqual(p, result); - Assert.AreNotSame(p, result); - } - - [TestMethod] - [DataRow(2, 15, 14)] - [DataRow(5, 16, 8)] - [DataRow(7, 5, 18)] - [DataRow(9, 5, 5)] - public void TestMultiplyScalar(int kRaw, int expectedX, int expectedY) - { - var k = new BigInteger(kRaw); - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - - var q = curve.MultiplyScalar(p, k); - var expectedQ = new CompactEC.ECPoint(expectedX, expectedY); - Assert.AreEqual(expectedQ, q); - } - - [TestMethod] - public void TestMultiplyScalarOrderResultsInNeutralElement() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var result = curve.MultiplyScalar(p, 8); - - Assert.AreEqual(curve.NeutralElement, result); - } - - [TestMethod] - public void TestGroupElementBitLength() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.AreEqual(2*5, curve.ElementBitLength); - } - - [TestMethod] - public void TestOrderIsAsSet() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.AreEqual(ecParams.Order, curve.Order); - } - - [TestMethod] - public void TestOrderBitLength() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.AreEqual(5, curve.OrderBitLength); - } - - [TestMethod] - public void TestNeutralElement() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, curve.NeutralElement); - } - - [TestMethod] - public void TestGeneratorIsAsSet() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.AreEqual(ecParams.Generator, curve.Generator); - } - - [TestMethod] - public void TestFromBytes() - { - ECParameters largeParams = new ECParameters() - { - P = 134217728, // == 2 ^ 27 - Generator = CompactEC.ECPoint.PointAtInfinity, - Order = 1 - }; - - var curve = new ECGroupAlgebra(largeParams); - var expected = new CompactEC.ECPoint(5, 5); - var buffer = new byte[] { 5, 0, 0, 0, 5, 0, 0, 0 }; - - var result = curve.FromBytes(buffer); - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestFromBytesRejectsNullArgument() - { - var curve = new ECGroupAlgebra(ecParams); - Assert.ThrowsException( - () => curve.FromBytes(null) - ); - } - - [TestMethod] - public void TestFromBytesRejectsTooShortBuffer() - { - ECParameters largeParams = new ECParameters() - { - P = 134217728, // == 2 ^ 27 - Generator = CompactEC.ECPoint.PointAtInfinity, - Order = 1 - }; - var curve = new ECGroupAlgebra(largeParams); - var buffer = new byte[7]; - Assert.ThrowsException( - () => curve.FromBytes(buffer) - ); - } - - [TestMethod] - public void TestToBytes() - { - ECParameters largeParams = new ECParameters() - { - P = 134217728, // == 2 ^ 27 - Generator = CompactEC.ECPoint.PointAtInfinity, - Order = 1 - }; - - var curve = new ECGroupAlgebra(largeParams); - var p = new CompactEC.ECPoint(5, 5); - var expected = new byte[] { 5, 0, 0, 0, 5, 0, 0, 0 }; - - var result = curve.ToBytes(p); - CollectionAssert.AreEqual(expected, result); - } - - [TestMethod] - public void TestFromBytesWithLessThanOneByteLargeElements() - { - var curve = new ECGroupAlgebra(ecParams); - var expected = new CompactEC.ECPoint(5, 5); - var buffer = new byte[] { 5, 5 }; - - var result = curve.FromBytes(buffer); - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestToBytesWithLessThanOneByteLargeElements() - { - var curve = new ECGroupAlgebra(ecParams); - var p = new CompactEC.ECPoint(5, 5); - var expected = new byte[] { 5, 5 }; - - var result = curve.ToBytes(p); - CollectionAssert.AreEqual(expected, result); - } - - [TestMethod] - public void TestInvalidElementRejectedAsGenerator() - { - var generator = new CompactEC.ECPoint(16, 1); - var invalidParams = ecParams; - invalidParams.Generator = generator; - Assert.ThrowsException( - () => new ECGroupAlgebra(invalidParams) - ); - } - } -} diff --git a/CompactEC.Tests_MS/ECPointTests.cs b/CompactEC.Tests_MS/ECPointTests.cs deleted file mode 100644 index 557d6e8..0000000 --- a/CompactEC.Tests_MS/ECPointTests.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace CompactEC.Tests -{ - [TestClass] - public class ECPointTests - { - // reference results from https://trustica.cz/en/2018/04/26/elliptic-curves-prime-order-curves/ - // curve has order 32, i.e., OrderSize = 6 bits - - [TestMethod] - public void TestClone() - { - var p = new CompactEC.ECPoint(5, 5); - var q = p.Clone(); - Assert.AreNotSame(p, q); - Assert.AreEqual(p, q); - } - - [TestMethod] - public void TestClonePointAtInfinity() - { - var p = CompactEC.ECPoint.PointAtInfinity; - var q = p.Clone(); - Assert.AreNotSame(p, q); - Assert.AreEqual(p, q); - } - - [TestMethod] - public void TestEquals() - { - var p = new CompactEC.ECPoint(11, 0); - var q = new CompactEC.ECPoint(3, 4); - - Assert.AreEqual(p, p, "same"); - Assert.AreEqual(p, p.Clone(), "equal"); - Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, CompactEC.ECPoint.PointAtInfinity, "at inf"); - - Assert.AreNotEqual(p, q, "not equal"); - Assert.AreNotEqual(q, p, "not equal reversed"); - } - - [TestMethod] - public void TestPointEquality() - { - Assert.AreEqual(CompactEC.ECPoint.PointAtInfinity, CompactEC.ECPoint.PointAtInfinity); - - var p = new CompactEC.ECPoint(14, 3); - var q = new CompactEC.ECPoint(14, 3); - Assert.AreEqual(p, q); - Assert.AreEqual(q, p); - } - } -} diff --git a/CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs b/CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs deleted file mode 100644 index 7a89983..0000000 --- a/CompactEC.Tests_MS/FixedFactorLengthCryptoGroupAlgebraTests.cs +++ /dev/null @@ -1,367 +0,0 @@ -using System; -using System.Numerics; -using System.Diagnostics; - -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; -using Moq.Protected; - -using CompactEC; - -namespace CompactEC.Tests.CryptoAlgebra -{ - - [TestClass] - public class FixedFactorLengthGroupAlgebraTests - { - [TestMethod] - [DataRow(-1)] - [DataRow(0)] - [DataRow(5)] - public void TestConstructorRejectsInvalidFactorBitLength(int factorBitLength) - { - var order = new BigInteger(8); - int orderBitLength = 4; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - Assert.ThrowsException( - () => new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength) - ); - } - - [TestMethod] - public void TestConstructorRejectsNullBaseAlgebra() - { - Assert.ThrowsException( - () => new FixedFactorLengthCryptoGroupAlgebra(null, 2) - ); - } - - [TestMethod] - public void TestFactorBitLengthCorrect() - { - var order = new BigInteger(8); - int orderBitLength = 4; - int factorBitLength = 2; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(baseAlgebraStub.Object.OrderBitLength == orderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(factorBitLength, fixedAlgebra.FactorBitLength); - } - - [TestMethod] - public void TestOrderCallsBaseAlgebra() - { - var order = new BigInteger(7); - int orderBitLength = 3; - int factorBitLength = 2; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(order, fixedAlgebra.Order); - } - - [TestMethod] - public void TestOrderBitLengthCallsBaseAlgebra() - { - var order = new BigInteger(7); - int orderBitLength = 3; - int factorBitLength = 1; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(baseAlgebraStub.Object.OrderBitLength, fixedAlgebra.OrderBitLength); - } - - [TestMethod] - public void TestGeneratorCallsBaseAlgebra() - { - int generator = 23; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - baseAlgebraStub.Setup(alg => alg.Generator).Returns(generator); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(generator, fixedAlgebra.Generator); - } - - [TestMethod] - public void TestNeutralElementCallsBaseAlgebra() - { - int neutralElement = 3; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - baseAlgebraStub.Setup(alg => alg.NeutralElement).Returns(neutralElement); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(neutralElement, fixedAlgebra.NeutralElement); - } - - [TestMethod] - public void TestElementBitLengthCallsBaseAlgebra() - { - int elementBitLength = 3; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - baseAlgebraStub.Setup(alg => alg.ElementBitLength).Returns(elementBitLength); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.AreEqual(elementBitLength, fixedAlgebra.ElementBitLength); - } - - [TestMethod] - public void TestAddCallsBaseAlgebra() - { - int leftElement = 11; - int rightElement = 6; - int expected = 17; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.Add(It.IsAny(), It.IsAny())).Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.Add(leftElement, rightElement); - - Assert.AreEqual(expected, result); - baseAlgebraMock.Verify( - alg => alg.Add(It.Is(x => x == leftElement), It.Is(x => x == rightElement)), - Times.Once() - ); - } - - [TestMethod] - public void TestFromBytesCallsBaseAlgebra() - { - byte[] buffer = new byte[0]; - int expected = 17; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.FromBytes(It.IsAny())).Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.FromBytes(buffer); - - Assert.AreEqual(expected, result); - baseAlgebraMock.Verify( - alg => alg.FromBytes(It.Is(x => x == buffer)), - Times.Once() - ); - } - - [TestMethod] - [DataRow(6)] - [DataRow(8)] - public void TestGenerateElementRejectsIndexWithDeviatingBitLength(int indexBitLength) - { - var order = new BigInteger(1024); - int orderBitLength = 11; - int factorBitLength = 7; - var index = new BigInteger(1 << (indexBitLength - 1)); - Debug.Assert(CryptoGroupAlgebra.GetBitLength(index) == indexBitLength); - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.ThrowsException( - () => fixedAlgebra.GenerateElement(index) - ); - } - - [TestMethod] - public void TestGenerateElementCallsBaseAlgebra() - { - var order = new BigInteger(1024); - int orderBitLength = 11; - int factorBitLength = 7; - var index = new BigInteger(1 << (factorBitLength - 1)); - int expected = 236; - int generator = 4; - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.Generator).Returns(generator); - baseAlgebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.GenerateElement(index); - - Assert.AreEqual(expected, result); - - baseAlgebraMock.Protected().As().Verify( - alg => alg.MultiplyScalarUnsafe(It.Is(x => x == generator), - It.Is(x => x == index), - It.Is(x => x == factorBitLength)), - Times.Once() - ); - } - - [TestMethod] - public void TestIsValidCallsBaseAlgebra() - { - int element = 9; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.IsValid(It.IsAny())).Returns(true); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - Assert.IsTrue(fixedAlgebra.IsValid(element)); - - baseAlgebraMock.Verify( - alg => alg.IsValid(It.Is(x => x == element)), - Times.Once() - ); - } - - [TestMethod] - [DataRow(6)] - [DataRow(8)] - public void TestMultiplyScalarRejectsFactorWithDeviationBitLength(int bitLength) - { - var order = new BigInteger(1024); - int orderBitLength = 11; - int factorBitLength = 7; - int element = 2; - var k = new BigInteger(1 << (bitLength - 1)); - Debug.Assert(CryptoGroupAlgebra.GetBitLength(k) == bitLength); - - var baseAlgebraStub = new Mock>(MockBehavior.Strict); - baseAlgebraStub.Setup(alg => alg.Order).Returns(order); - Debug.Assert(orderBitLength == baseAlgebraStub.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraStub.Object, factorBitLength); - Assert.ThrowsException( - () => fixedAlgebra.MultiplyScalar(element, k) - ); - } - - [TestMethod] - public void TestMultiplyScalarCallsBaseAlgebra() - { - var order = new BigInteger(1024); - int orderBitLength = 11; - int element = 2; - int expected = 124; - int factorBitLength = 7; - var factor = new BigInteger(1 << (factorBitLength - 1)); - Debug.Assert(CryptoGroupAlgebra.GetBitLength(factor) == factorBitLength); - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Protected().As() - .Setup(alg => alg.MultiplyScalarUnsafe(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.MultiplyScalar(element, factor); - - Assert.AreEqual(expected, result); - baseAlgebraMock.Protected().As().Verify( - alg => alg.MultiplyScalarUnsafe(It.Is(x => x == element), - It.Is(x => x == factor), - It.Is(x => x == factorBitLength)), - Times.Once() - ); - } - - [TestMethod] - public void TestNegateCallsBaseAlgebra() - { - int element = 9; - int expected = 35; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.Negate(It.IsAny())).Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.Negate(element); - - Assert.AreEqual(expected, result); - - baseAlgebraMock.Verify( - alg => alg.Negate(It.Is(x => x == element)), - Times.Once() - ); - } - - [TestMethod] - public void TestToBytesCallsBaseAlgebra() - { - int element = 9; - var expected = new byte[0]; - var order = new BigInteger(24); - int orderBitLength = 5; - int factorBitLength = 1; - - - var baseAlgebraMock = new Mock>(MockBehavior.Strict); - baseAlgebraMock.Setup(alg => alg.Order).Returns(order); - baseAlgebraMock.Setup(alg => alg.ToBytes(It.IsAny())).Returns(expected); - Debug.Assert(orderBitLength == baseAlgebraMock.Object.OrderBitLength); - - var fixedAlgebra = new FixedFactorLengthCryptoGroupAlgebra(baseAlgebraMock.Object, factorBitLength); - var result = fixedAlgebra.ToBytes(element); - - CollectionAssert.AreEqual(expected, result); - - baseAlgebraMock.Verify( - alg => alg.ToBytes(It.Is(x => x == element)), - Times.Once() - ); - } - } -} diff --git a/CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs b/CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs deleted file mode 100644 index 1e41323..0000000 --- a/CompactEC.Tests_MS/MultiplicativeGroupAlgebraIntegratedTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Numerics; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using CompactEC; - -namespace CompactEC.Tests.CryptoAlgebra -{ - [TestClass] - public class MultiplicativeGroupAlgebraIntegratedTest - { - [TestMethod] - [DataRow(0)] - [DataRow(1)] - [DataRow(2)] - [DataRow(6)] - [DataRow(9)] - [DataRow(10)] - [DataRow(11)] - [DataRow(232)] - public void TestGenerateIsGeneratorMultiplied(int idInt) - { - var id = new BigInteger(idInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - - Assert.AreEqual(groupAlgebra.MultiplyScalar(groupAlgebra.Generator, id), groupAlgebra.GenerateElement(id)); - } - - [TestMethod] - public void TestGenerateRejectsNegativeIds() - { - var id = new BigInteger(-1); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - - Assert.ThrowsException( - () => groupAlgebra.GenerateElement(id) - ); - } - - [TestMethod] - public void TestMultiplyScalarRejectsNegativeScalars() - { - var k = new BigInteger(-1); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var x = new BigInteger(3); - - Assert.ThrowsException( - () => groupAlgebra.MultiplyScalar(x, k) - ); - } - - [TestMethod] - [DataRow(0)] - [DataRow(1)] - [DataRow(2)] - [DataRow(4)] - [DataRow(7)] - public void TestMultiplyScalarWithSmallFactorSizeEqualToOrderFactorSize(int factorInt) - { - int factorBitLength = 3; - var k = new BigInteger(factorInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var x = new BigInteger(6); - Assert.AreEqual(groupAlgebra.MultiplyScalar(x, k), groupAlgebra.MultiplyScalar(x, k, factorBitLength)); - } - - [TestMethod] - public void TestMultiplyScalarWithSmallFactorSizeRejectsNegativeScalars() - { - int factorBitLength = 3; - var k = new BigInteger(-1); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var x = new BigInteger(6); - - Assert.ThrowsException( - () => groupAlgebra.MultiplyScalar(x, k, factorBitLength) - ); - } - - [TestMethod] - [DataRow(8)] - [DataRow(9)] - [DataRow(123)] - public void TestMultiplyScalarWithSmallFactorSizeRejectsLargerFactors(int factorInt) - { - int factorBitLength = 3; - var k = new BigInteger(factorInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var x = new BigInteger(6); - Assert.ThrowsException( - () => groupAlgebra.MultiplyScalar(x, k, factorBitLength) - ); - } - } -} diff --git a/CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs b/CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs deleted file mode 100644 index 449522e..0000000 --- a/CompactEC.Tests_MS/MultiplicativeGroupAlgebraTests.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.Numerics; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using CompactEC; - -// Best Practices: https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices -namespace CompactEC.Tests.CryptoAlgebra -{ - [TestClass] - public class MultiplicativeGroupAlgebraTests - { - [TestMethod] - [DataRow(0, 1)] - [DataRow(1, 3)] - [DataRow(2, 9)] - [DataRow(3, 5)] - [DataRow(4, 4)] - [DataRow(5, 1)] - [DataRow(6, 3)] - [DataRow(12, 9)] - public void TestMultiplyScalar(int scalarInt, int expectedInt) - { - var k = new BigInteger(scalarInt); - var expected = new BigInteger(expectedInt); - - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - - var x = new BigInteger(3); - var result = groupAlgebra.MultiplyScalar(x, k); - Assert.AreEqual(expected, result); - } - - [TestMethod] - [DataRow(2)] - [DataRow(3)] - [DataRow(7)] - public void TestGeneratorIsAsSet(int generatorInt) - { - var generator = new BigInteger(generatorInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, generator); - Assert.AreEqual(generator, groupAlgebra.Generator); - } - - [TestMethod] - [DataRow(0)] - [DataRow(-3)] - [DataRow(11)] - [DataRow(136)] - public void TestInvalidElementRejectedAsGenerator(int generatorInt) - { - var generator = new BigInteger(generatorInt); - Assert.ThrowsException( - () => new MultiplicativeGroupAlgebra(11, 10, generator) - ); - } - - [TestMethod] - [DataRow(1)] - [DataRow(5)] - [DataRow(10)] - public void TestIsValidAcceptsValidElements(int elementInt) - { - var element = new BigInteger(elementInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.IsTrue(groupAlgebra.IsValid(element)); - } - - [TestMethod] - [DataRow(0)] - [DataRow(-3)] - [DataRow(11)] - [DataRow(136)] - public void TestIsValidRejectsInvalidElements(int elementInt) - { - var element = new BigInteger(elementInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.IsFalse(groupAlgebra.IsValid(element)); - } - - [TestMethod] - public void TestGroupElementBitLength() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.AreEqual(4, groupAlgebra.ElementBitLength); - } - - [TestMethod] - public void TestOrderBitLength() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 5, 3); - Assert.AreEqual(3, groupAlgebra.OrderBitLength); - } - - [TestMethod] - [DataRow(1)] - [DataRow(2)] - [DataRow(5)] - [DataRow(10)] - public void TestNegate(int elementInt) - { - var x = new BigInteger(elementInt); - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.AreEqual(groupAlgebra.MultiplyScalar(x, groupAlgebra.Order - 1), groupAlgebra.Negate(x)); - } - - [TestMethod] - public void TestNeutralElement() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.AreEqual(BigInteger.One, groupAlgebra.NeutralElement); - } - - [TestMethod] - public void TestFromBytes() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var expected = new BigInteger(7); - var buffer = expected.ToByteArray(); - - var result = groupAlgebra.FromBytes(buffer); - Assert.AreEqual(expected, result); - } - - [TestMethod] - public void TestFromBytesRejectsNullArgument() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - Assert.ThrowsException( - () => groupAlgebra.FromBytes(null) - ); - } - - [TestMethod] - public void TestToBytes() - { - var groupAlgebra = new MultiplicativeGroupAlgebra(11, 10, 2); - var element = new BigInteger(7); - var expected = element.ToByteArray(); - - var result = groupAlgebra.ToBytes(element); - - CollectionAssert.AreEqual(expected, result); - } - } -} diff --git a/CompactEC.Tests_MS/Properties/AssemblyInfo.cs b/CompactEC.Tests_MS/Properties/AssemblyInfo.cs deleted file mode 100644 index b6894e5..0000000 --- a/CompactEC.Tests_MS/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("CompactEC.Tests")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("CompactEC.Tests")] -[assembly: AssemblyCopyright("Copyright © 2019")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -[assembly: ComVisible(false)] - -[assembly: Guid("22fd5c5a-fc60-4f60-b44e-47405cced477")] - -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/CompactEC.Tests_MS/packages.config b/CompactEC.Tests_MS/packages.config deleted file mode 100644 index 300a3c3..0000000 --- a/CompactEC.Tests_MS/packages.config +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/CompactEC.sln b/CompactEC.sln index e941fa2..52ce759 100644 --- a/CompactEC.sln +++ b/CompactEC.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 15.0.28010.2046 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompactEC", "CompactEC\CompactEC.csproj", "{5F5CFCF8-677E-4028-B74A-E6AB2975360C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests_MS", "CompactEC.Tests_MS\CompactEC.Tests_MS.csproj", "{22FD5C5A-FC60-4F60-B44E-47405CCED477}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{43DEFB14-5176-4863-9A51-D141802BA000}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompactEC.Tests", "CompactEC.Tests\CompactEC.Tests.csproj", "{FAD092CB-98B1-4206-A8BC-77AFDDA6CEBB}" @@ -21,10 +19,6 @@ Global {5F5CFCF8-677E-4028-B74A-E6AB2975360C}.Debug|Any CPU.Build.0 = Debug|Any CPU {5F5CFCF8-677E-4028-B74A-E6AB2975360C}.Release|Any CPU.ActiveCfg = Release|Any CPU {5F5CFCF8-677E-4028-B74A-E6AB2975360C}.Release|Any CPU.Build.0 = Release|Any CPU - {22FD5C5A-FC60-4F60-B44E-47405CCED477}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {22FD5C5A-FC60-4F60-B44E-47405CCED477}.Debug|Any CPU.Build.0 = Debug|Any CPU - {22FD5C5A-FC60-4F60-B44E-47405CCED477}.Release|Any CPU.ActiveCfg = Release|Any CPU - {22FD5C5A-FC60-4F60-B44E-47405CCED477}.Release|Any CPU.Build.0 = Release|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Debug|Any CPU.Build.0 = Debug|Any CPU {43DEFB14-5176-4863-9A51-D141802BA000}.Release|Any CPU.ActiveCfg = Release|Any CPU From d4e60782f3543d2d235ae202f4381258217c6637 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Sun, 15 Dec 2019 20:58:25 +0100 Subject: [PATCH 4/4] Added System.Security.Cryptography.Algorithms as NuGet package for all projects. Fixed absolute path for documentation output. --- CompactEC.Tests/CompactEC.Tests.csproj | 7 ++++++- CompactEC.Tests/packages.config | 2 ++ CompactEC/CompactEC.csproj | 12 +++++++++++- Example/Example.csproj | 8 +++++++- Example/packages.config | 5 +++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 Example/packages.config diff --git a/CompactEC.Tests/CompactEC.Tests.csproj b/CompactEC.Tests/CompactEC.Tests.csproj index 19296fa..6a99a79 100644 --- a/CompactEC.Tests/CompactEC.Tests.csproj +++ b/CompactEC.Tests/CompactEC.Tests.csproj @@ -53,7 +53,12 @@ ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll - + + ..\packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net46\System.Security.Cryptography.Algorithms.dll + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll diff --git a/CompactEC.Tests/packages.config b/CompactEC.Tests/packages.config index e95bcdb..c9e5e99 100644 --- a/CompactEC.Tests/packages.config +++ b/CompactEC.Tests/packages.config @@ -5,5 +5,7 @@ + + \ No newline at end of file diff --git a/CompactEC/CompactEC.csproj b/CompactEC/CompactEC.csproj index 410bb30..e46ace8 100644 --- a/CompactEC/CompactEC.csproj +++ b/CompactEC/CompactEC.csproj @@ -5,7 +5,17 @@ - C:\Development\Projects\CompactEC\CompactEC\CompactEC.xml + bin/$(Configuration)/CompactEC.xml + bin/$(Configuration)/ + + bin/$(Configuration)/CompactEC.xml + bin/$(Configuration)/ + + + + + + diff --git a/Example/Example.csproj b/Example/Example.csproj index 892098f..3a09bcd 100644 --- a/Example/Example.csproj +++ b/Example/Example.csproj @@ -37,7 +37,12 @@ - + + ..\packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net46\System.Security.Cryptography.Algorithms.dll + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + @@ -51,6 +56,7 @@ + diff --git a/Example/packages.config b/Example/packages.config new file mode 100644 index 0000000..982d6e2 --- /dev/null +++ b/Example/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file