Skip to content

Commit 55abc37

Browse files
authored
Add equality to MLDsaAlgorithm and add tests
1 parent 0721dd8 commit 55abc37

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

src/libraries/Common/src/System/Security/Cryptography/MLDsaAlgorithm.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56

67
namespace System.Security.Cryptography
78
{
89
/// <summary>
910
/// Represents a specific algorithm within the ML-DSA family.
1011
/// </summary>
12+
[DebuggerDisplay("{Name,nq}")]
1113
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
12-
public sealed class MLDsaAlgorithm
14+
public sealed class MLDsaAlgorithm : IEquatable<MLDsaAlgorithm>
1315
{
1416
/// <summary>
1517
/// Gets the underlying string representation of the algorithm name.
@@ -117,5 +119,60 @@ private MLDsaAlgorithm(string name, int secretKeySizeInBytes, int publicKeySizeI
117119
_ => null,
118120
};
119121
}
122+
123+
/// <summary>
124+
/// Compares two <see cref="MLDsaAlgorithm" /> objects.
125+
/// </summary>
126+
/// <param name="other">
127+
/// An object to be compared to the current <see cref="MLDsaAlgorithm"/> object.
128+
/// </param>
129+
/// <returns>
130+
/// <see langword="true" /> if the objects are considered equal; otherwise, <see langword="false" />.
131+
/// </returns>
132+
// This is a closed type, so all we need to compare are the names.
133+
public bool Equals([NotNullWhen(true)] MLDsaAlgorithm? other) => other is not null && other.Name == Name;
134+
135+
/// <inheritdoc />
136+
public override bool Equals([NotNullWhen(true)] object? obj) => obj is MLDsaAlgorithm alg && alg.Name == Name;
137+
138+
/// <inheritdoc />
139+
public override int GetHashCode() => Name.GetHashCode();
140+
141+
/// <inheritdoc />
142+
public override string ToString() => Name;
143+
144+
/// <summary>
145+
/// Determines whether two <see cref="MLDsaAlgorithm" /> objects specify the same algorithm name.
146+
/// </summary>
147+
/// <param name="left">
148+
/// An object that specifies an algorithm name.
149+
/// </param>
150+
/// <param name="right">
151+
/// A second object, to be compared to the object that is identified by the <paramref name="left" /> parameter.
152+
/// </param>
153+
/// <returns>
154+
/// <see langword="true" /> if the objects are considered equal; otherwise, <see langword="false" />.
155+
/// </returns>
156+
public static bool operator ==(MLDsaAlgorithm? left, MLDsaAlgorithm? right)
157+
{
158+
return left is null ? right is null : left.Equals(right);
159+
}
160+
161+
/// <summary>
162+
/// Determines whether two <see cref="MLDsaAlgorithm" /> objects do not specify the same algorithm name.
163+
/// </summary>
164+
/// <param name="left">
165+
/// An object that specifies an algorithm name.
166+
/// </param>
167+
/// <param name="right">
168+
/// A second object, to be compared to the object that is identified by the <paramref name="left" /> parameter.
169+
/// </param>
170+
/// <returns>
171+
/// <see langword="true" /> if the objects are not considered equal; otherwise, <see langword="false" />.
172+
/// </returns>
173+
public static bool operator !=(MLDsaAlgorithm? left, MLDsaAlgorithm? right)
174+
{
175+
return !(left == right);
176+
}
120177
}
121178
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using Microsoft.DotNet.XUnitExtensions;
7+
using Xunit;
8+
9+
namespace System.Security.Cryptography.Tests
10+
{
11+
public static class MLDsaAlgorithmTests
12+
{
13+
[Fact]
14+
public static void Algorithms_AreSame()
15+
{
16+
Assert.Same(MLDsaAlgorithm.MLDsa44, MLDsaAlgorithm.MLDsa44);
17+
Assert.Same(MLDsaAlgorithm.MLDsa65, MLDsaAlgorithm.MLDsa65);
18+
Assert.Same(MLDsaAlgorithm.MLDsa87, MLDsaAlgorithm.MLDsa87);
19+
}
20+
21+
[Theory]
22+
[MemberData(nameof(MLDsaAlgorithms))]
23+
public static void Algorithms_Equal(MLDsaAlgorithm algorithm)
24+
{
25+
AssertExtensions.TrueExpression(algorithm.Equals(algorithm));
26+
AssertExtensions.TrueExpression(algorithm.Equals((object)algorithm));
27+
AssertExtensions.FalseExpression(algorithm.Equals(null));
28+
}
29+
30+
[Theory]
31+
[MemberData(nameof(MLDsaAlgorithms))]
32+
public static void Algorithms_GetHashCode(MLDsaAlgorithm algorithm)
33+
{
34+
Assert.Equal(algorithm.Name.GetHashCode(), algorithm.GetHashCode());
35+
}
36+
37+
[Fact]
38+
public static void Algorithms_Equality()
39+
{
40+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa44 == MLDsaAlgorithm.MLDsa44);
41+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa65 == MLDsaAlgorithm.MLDsa65);
42+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa87 == MLDsaAlgorithm.MLDsa87);
43+
44+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa44 == MLDsaAlgorithm.MLDsa65);
45+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa65 == MLDsaAlgorithm.MLDsa87);
46+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa87 == MLDsaAlgorithm.MLDsa44);
47+
}
48+
49+
[Fact]
50+
public static void Algorithms_Inequality()
51+
{
52+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa44 != MLDsaAlgorithm.MLDsa44);
53+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa65 != MLDsaAlgorithm.MLDsa65);
54+
AssertExtensions.FalseExpression(MLDsaAlgorithm.MLDsa87 != MLDsaAlgorithm.MLDsa87);
55+
56+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa44 != MLDsaAlgorithm.MLDsa65);
57+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa65 != MLDsaAlgorithm.MLDsa87);
58+
AssertExtensions.TrueExpression(MLDsaAlgorithm.MLDsa87 != MLDsaAlgorithm.MLDsa44);
59+
}
60+
61+
[Theory]
62+
[MemberData(nameof(MLDsaAlgorithms))]
63+
public static void Algorithms_ToString(MLDsaAlgorithm algorithm)
64+
{
65+
Assert.Equal(algorithm.Name, algorithm.ToString());
66+
}
67+
68+
public static IEnumerable<object[]> MLDsaAlgorithms()
69+
{
70+
yield return new object[] { MLDsaAlgorithm.MLDsa44 };
71+
yield return new object[] { MLDsaAlgorithm.MLDsa65 };
72+
yield return new object[] { MLDsaAlgorithm.MLDsa87 };
73+
}
74+
}
75+
}

src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
117117
<Compile Include="$(CommonTestPath)System\Security\Cryptography\CngKeyWrapper.cs"
118118
Link="TestCommon\System\Security\Cryptography\CngKeyWrapper.cs" />
119+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLDsaAlgorithmTests.cs"
120+
Link="CommonTest\System\Security\Cryptography\MLDsaAlgorithmTests.cs" />
119121
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemAlgorithmTests.cs"
120122
Link="CommonTest\System\Security\Cryptography\MLKemAlgorithmTests.cs" />
121123
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemContractTests.cs"

src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ protected virtual void Dispose(bool disposing) { }
18581858
protected abstract bool VerifyDataCore(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<byte> context, System.ReadOnlySpan<byte> signature);
18591859
}
18601860
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
1861-
public sealed partial class MLDsaAlgorithm
1861+
public sealed partial class MLDsaAlgorithm : System.IEquatable<System.Security.Cryptography.MLDsaAlgorithm>
18621862
{
18631863
internal MLDsaAlgorithm() { }
18641864
public static System.Security.Cryptography.MLDsaAlgorithm MLDsa44 { get { throw null; } }
@@ -1869,6 +1869,12 @@ internal MLDsaAlgorithm() { }
18691869
public int PublicKeySizeInBytes { get { throw null; } }
18701870
public int SecretKeySizeInBytes { get { throw null; } }
18711871
public int SignatureSizeInBytes { get { throw null; } }
1872+
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
1873+
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Security.Cryptography.MLDsaAlgorithm? other) { throw null; }
1874+
public override int GetHashCode() { throw null; }
1875+
public static bool operator ==(System.Security.Cryptography.MLDsaAlgorithm? left, System.Security.Cryptography.MLDsaAlgorithm? right) { throw null; }
1876+
public static bool operator !=(System.Security.Cryptography.MLDsaAlgorithm? left, System.Security.Cryptography.MLDsaAlgorithm? right) { throw null; }
1877+
public override string ToString() { throw null; }
18721878
}
18731879
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
18741880
public sealed partial class MLDsaCng : System.Security.Cryptography.MLDsa

src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@
223223
Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
224224
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs"
225225
Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
226+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLDsaAlgorithmTests.cs"
227+
Link="CommonTest\System\Security\Cryptography\MLDsaAlgorithmTests.cs" />
226228
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemAlgorithmTests.cs"
227229
Link="CommonTest\System\Security\Cryptography\MLKemAlgorithmTests.cs" />
228230
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemBaseTests.cs"

0 commit comments

Comments
 (0)