Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// 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
{
/// <summary>
/// Represents a specific algorithm within the ML-DSA family.
/// </summary>
[DebuggerDisplay("{Name,nq}")]
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
public sealed class MLDsaAlgorithm
public sealed class MLDsaAlgorithm : IEquatable<MLDsaAlgorithm>
{
/// <summary>
/// Gets the underlying string representation of the algorithm name.
Expand Down Expand Up @@ -117,5 +119,60 @@ private MLDsaAlgorithm(string name, int secretKeySizeInBytes, int publicKeySizeI
_ => null,
};
}

/// <summary>
/// Compares two <see cref="MLDsaAlgorithm" /> objects.
/// </summary>
/// <param name="other">
/// An object to be compared to the current <see cref="MLDsaAlgorithm"/> object.
/// </param>
/// <returns>
/// <see langword="true" /> if the objects are considered equal; otherwise, <see langword="false" />.
/// </returns>
// 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;

/// <inheritdoc />
public override bool Equals([NotNullWhen(true)] object? obj) => obj is MLDsaAlgorithm alg && alg.Name == Name;

/// <inheritdoc />
public override int GetHashCode() => Name.GetHashCode();

/// <inheritdoc />
public override string ToString() => Name;

/// <summary>
/// Determines whether two <see cref="MLDsaAlgorithm" /> objects specify the same algorithm name.
/// </summary>
/// <param name="left">
/// An object that specifies an algorithm name.
/// </param>
/// <param name="right">
/// A second object, to be compared to the object that is identified by the <paramref name="left" /> parameter.
/// </param>
/// <returns>
/// <see langword="true" /> if the objects are considered equal; otherwise, <see langword="false" />.
/// </returns>
public static bool operator ==(MLDsaAlgorithm? left, MLDsaAlgorithm? right)
{
return left is null ? right is null : left.Equals(right);
}

/// <summary>
/// Determines whether two <see cref="MLDsaAlgorithm" /> objects do not specify the same algorithm name.
/// </summary>
/// <param name="left">
/// An object that specifies an algorithm name.
/// </param>
/// <param name="right">
/// A second object, to be compared to the object that is identified by the <paramref name="left" /> parameter.
/// </param>
/// <returns>
/// <see langword="true" /> if the objects are not considered equal; otherwise, <see langword="false" />.
/// </returns>
public static bool operator !=(MLDsaAlgorithm? left, MLDsaAlgorithm? right)
{
return !(left == right);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<object[]> MLDsaAlgorithms()
{
yield return new object[] { MLDsaAlgorithm.MLDsa44 };
yield return new object[] { MLDsaAlgorithm.MLDsa65 };
yield return new object[] { MLDsaAlgorithm.MLDsa87 };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\CngKeyWrapper.cs"
Link="TestCommon\System\Security\Cryptography\CngKeyWrapper.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLDsaAlgorithmTests.cs"
Link="CommonTest\System\Security\Cryptography\MLDsaAlgorithmTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemAlgorithmTests.cs"
Link="CommonTest\System\Security\Cryptography\MLKemAlgorithmTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemContractTests.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@
Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs"
Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLDsaAlgorithmTests.cs"
Link="CommonTest\System\Security\Cryptography\MLDsaAlgorithmTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemAlgorithmTests.cs"
Link="CommonTest\System\Security\Cryptography\MLKemAlgorithmTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemBaseTests.cs"
Expand Down
Loading