diff --git a/src/libraries/Common/src/System/Security/Cryptography/MLDsaAlgorithm.cs b/src/libraries/Common/src/System/Security/Cryptography/MLDsaAlgorithm.cs index e5fc7bbbc21cc3..c058daab3bb2e3 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/MLDsaAlgorithm.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/MLDsaAlgorithm.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace System.Security.Cryptography @@ -8,8 +9,9 @@ namespace System.Security.Cryptography /// /// Represents a specific algorithm within the ML-DSA family. /// + [DebuggerDisplay("{Name,nq}")] [Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)] - public sealed class MLDsaAlgorithm + public sealed class MLDsaAlgorithm : IEquatable { /// /// Gets the underlying string representation of the algorithm name. @@ -117,5 +119,60 @@ private MLDsaAlgorithm(string name, int secretKeySizeInBytes, int publicKeySizeI _ => null, }; } + + /// + /// Compares two objects. + /// + /// + /// An object to be compared to the current object. + /// + /// + /// if the objects are considered equal; otherwise, . + /// + // This is a closed type, so all we need to compare are the names. + public bool Equals([NotNullWhen(true)] MLDsaAlgorithm? other) => other is not null && other.Name == Name; + + /// + public override bool Equals([NotNullWhen(true)] object? obj) => obj is MLDsaAlgorithm alg && alg.Name == Name; + + /// + public override int GetHashCode() => Name.GetHashCode(); + + /// + public override string ToString() => Name; + + /// + /// Determines whether two objects specify the same algorithm name. + /// + /// + /// An object that specifies an algorithm name. + /// + /// + /// A second object, to be compared to the object that is identified by the parameter. + /// + /// + /// if the objects are considered equal; otherwise, . + /// + public static bool operator ==(MLDsaAlgorithm? left, MLDsaAlgorithm? right) + { + return left is null ? right is null : left.Equals(right); + } + + /// + /// Determines whether two objects do not specify the same algorithm name. + /// + /// + /// An object that specifies an algorithm name. + /// + /// + /// A second object, to be compared to the object that is identified by the parameter. + /// + /// + /// if the objects are not considered equal; otherwise, . + /// + public static bool operator !=(MLDsaAlgorithm? left, MLDsaAlgorithm? right) + { + return !(left == right); + } } } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/MLDsaAlgorithmTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/MLDsaAlgorithmTests.cs new file mode 100644 index 00000000000000..7bbe01af87f2ee --- /dev/null +++ b/src/libraries/Common/tests/System/Security/Cryptography/MLDsaAlgorithmTests.cs @@ -0,0 +1,75 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Globalization; +using Microsoft.DotNet.XUnitExtensions; +using Xunit; + +namespace System.Security.Cryptography.Tests +{ + public static class MLDsaAlgorithmTests + { + [Fact] + public static void Algorithms_AreSame() + { + Assert.Same(MLDsaAlgorithm.MLDsa44, MLDsaAlgorithm.MLDsa44); + Assert.Same(MLDsaAlgorithm.MLDsa65, MLDsaAlgorithm.MLDsa65); + Assert.Same(MLDsaAlgorithm.MLDsa87, MLDsaAlgorithm.MLDsa87); + } + + [Theory] + [MemberData(nameof(MLDsaAlgorithms))] + public static void Algorithms_Equal(MLDsaAlgorithm algorithm) + { + AssertExtensions.TrueExpression(algorithm.Equals(algorithm)); + AssertExtensions.TrueExpression(algorithm.Equals((object)algorithm)); + AssertExtensions.FalseExpression(algorithm.Equals(null)); + } + + [Theory] + [MemberData(nameof(MLDsaAlgorithms))] + public static void Algorithms_GetHashCode(MLDsaAlgorithm algorithm) + { + Assert.Equal(algorithm.Name.GetHashCode(), algorithm.GetHashCode()); + } + + [Fact] + public static void Algorithms_Equality() + { + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa44 == MLDsaAlgorithm.MLDsa44); + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa65 == MLDsaAlgorithm.MLDsa65); + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa87 == MLDsaAlgorithm.MLDsa87); + + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa44 == MLDsaAlgorithm.MLDsa65); + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa65 == MLDsaAlgorithm.MLDsa87); + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa87 == MLDsaAlgorithm.MLDsa44); + } + + [Fact] + public static void Algorithms_Inequality() + { + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa44 != MLDsaAlgorithm.MLDsa44); + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa65 != MLDsaAlgorithm.MLDsa65); + AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa87 != MLDsaAlgorithm.MLDsa87); + + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa44 != MLDsaAlgorithm.MLDsa65); + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa65 != MLDsaAlgorithm.MLDsa87); + AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa87 != MLDsaAlgorithm.MLDsa44); + } + + [Theory] + [MemberData(nameof(MLDsaAlgorithms))] + public static void Algorithms_ToString(MLDsaAlgorithm algorithm) + { + Assert.Equal(algorithm.Name, algorithm.ToString()); + } + + public static IEnumerable MLDsaAlgorithms() + { + yield return new object[] { MLDsaAlgorithm.MLDsa44 }; + yield return new object[] { MLDsaAlgorithm.MLDsa65 }; + yield return new object[] { MLDsaAlgorithm.MLDsa87 }; + } + } +} diff --git a/src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj b/src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj index b4be58c63531ea..67eca5c3ff4fce 100644 --- a/src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj +++ b/src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj @@ -116,6 +116,8 @@ Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" /> + data, System.ReadOnlySpan context, System.ReadOnlySpan signature); } [System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")] - public sealed partial class MLDsaAlgorithm + public sealed partial class MLDsaAlgorithm : System.IEquatable { internal MLDsaAlgorithm() { } public static System.Security.Cryptography.MLDsaAlgorithm MLDsa44 { get { throw null; } } @@ -1869,6 +1869,12 @@ internal MLDsaAlgorithm() { } public int PublicKeySizeInBytes { get { throw null; } } public int SecretKeySizeInBytes { get { throw null; } } public int SignatureSizeInBytes { get { throw null; } } + public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; } + public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Security.Cryptography.MLDsaAlgorithm? other) { throw null; } + public override int GetHashCode() { throw null; } + public static bool operator ==(System.Security.Cryptography.MLDsaAlgorithm? left, System.Security.Cryptography.MLDsaAlgorithm? right) { throw null; } + public static bool operator !=(System.Security.Cryptography.MLDsaAlgorithm? left, System.Security.Cryptography.MLDsaAlgorithm? right) { throw null; } + public override string ToString() { throw null; } } [System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")] public sealed partial class MLDsaCng : System.Security.Cryptography.MLDsa diff --git a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj index 858b94a47985bb..c12732215efcef 100644 --- a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj +++ b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj @@ -223,6 +223,8 @@ Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" /> +