Skip to content

Commit

Permalink
secp256r1 precompile improvements (#8252)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexb5dh authored Feb 25, 2025
1 parent 0bbf49b commit a152d24
Show file tree
Hide file tree
Showing 8 changed files with 5,558 additions and 101 deletions.
1 change: 1 addition & 0 deletions src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<PackageVersion Include="Nethermind.Crypto.Bls" Version="1.0.4" />
<PackageVersion Include="Nethermind.Crypto.Pairings" Version="1.1.1" />
<PackageVersion Include="Nethermind.Crypto.SecP256k1" Version="1.4.0" />
<PackageVersion Include="Nethermind.Crypto.SecP256r1" Version="1.0.0-preview.1" />
<PackageVersion Include="Nethermind.DotNetty.Buffers" Version="1.0.1" />
<PackageVersion Include="Nethermind.DotNetty.Handlers" Version="1.0.1" />
<PackageVersion Include="Nethermind.DotNetty.Transport" Version="1.0.1" />
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/Nethermind.Evm.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@
<ProjectReference Include="..\Nethermind.JsonRpc\Nethermind.JsonRpc.csproj" />
<ProjectReference Include="..\Nethermind.Synchronization\Nethermind.Synchronization.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="TestFiles\p256Verify.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

This file was deleted.

66 changes: 66 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/Secp256r1PrecompileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Nethermind.Core.Extensions;
using Nethermind.Evm.Precompiles;
using Nethermind.Specs.Forks;
using NUnit.Framework;

namespace Nethermind.Evm.Test
{
[TestFixture]
public class Secp256r1PrecompileTests : VirtualMachineTestsBase
{
// ReSharper disable once ClassNeverInstantiated.Local
public record TestCase(string Input, string Expected, string Name);

private static IEnumerable<TestCase> TestSource()
{
// /~https://github.com/ethereum-optimism/op-geth/blob/7017b54770d480b5c8be63dc40eac9da166150f5/core/vm/testdata/precompiles/p256Verify.json
var data = File.ReadAllText("TestFiles/p256Verify.json");
return JsonSerializer.Deserialize<TestCase[]>(data);
}

[TestCaseSource(nameof(TestSource))]
public void Produces_Correct_Outputs(TestCase testCase)
{
var input = Bytes.FromHexString(testCase.Input);
var expected = Bytes.FromHexString(testCase.Expected);

(ReadOnlyMemory<byte> output, bool success) = Secp256r1Precompile.Instance.Run(input, Prague.Instance);

using (Assert.EnterMultipleScope())
{
Assert.That(success, Is.True);
Assert.That(output.ToArray(), Is.EquivalentTo(expected));
}
}

[Test]
[TestCase(
""
)]
[TestCase(
"4cee90eb86eaa050036147a12d49004b6a"
)]
[TestCase(
"4cee90eb86eaa050036147a12d49004b6a958b991cfd78f16537fe6d1f4afd10273384db08bdfc843562a22b0626766686f6aec8247599f40bfe01bec0e0ecf17b4319559022d4d9bf007fe929943004eb4866760dedf319"
)]
public void Produces_Empty_Output_On_Invalid_Input(string input)
{
var bytes = Bytes.FromHexString(input);

(ReadOnlyMemory<byte> output, bool success) = Secp256r1Precompile.Instance.Run(bytes, Prague.Instance);

using (Assert.EnterMultipleScope())
{
Assert.That(success, Is.True);
Assert.That(output.ToArray(), Is.EquivalentTo(Array.Empty<byte>()));
}
}
}
}
Loading

0 comments on commit a152d24

Please sign in to comment.