Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 7 additions & 8 deletions src/Nethermind/Nethermind.Core.Test/AddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Nethermind.Core.Test.Builders;
using Nethermind.Int256;
using Nethermind.Evm;
using Nethermind.Evm.Precompiles;
using NUnit.Framework;

namespace Nethermind.Core.Test;
Expand Down Expand Up @@ -120,7 +119,7 @@ public void Is_precompiled_1()
byte[] addressBytes = new byte[20];
addressBytes[19] = 1;
Address address = new(addressBytes);
Assert.That(address.IsPrecompile(Frontier.Instance), Is.True);
Assert.That(Frontier.Instance.IsPrecompile(address), Is.True);
}

[Test]
Expand All @@ -129,7 +128,7 @@ public void Is_precompiled_4_regression()
byte[] addressBytes = new byte[20];
addressBytes[19] = 4;
Address address = new(addressBytes);
Assert.That(address.IsPrecompile(Frontier.Instance), Is.True);
Assert.That(Frontier.Instance.IsPrecompile(address), Is.True);
}

[Test]
Expand All @@ -138,7 +137,7 @@ public void Is_precompiled_5_frontier()
byte[] addressBytes = new byte[20];
addressBytes[19] = 5;
Address address = new(addressBytes);
Assert.That(address.IsPrecompile(Frontier.Instance), Is.False);
Assert.That(Frontier.Instance.IsPrecompile(address), Is.False);
}

[Test]
Expand All @@ -147,7 +146,7 @@ public void Is_precompiled_5_byzantium()
byte[] addressBytes = new byte[20];
addressBytes[19] = 5;
Address address = new(addressBytes);
Assert.That(address.IsPrecompile(Byzantium.Instance), Is.True);
Assert.That(Byzantium.Instance.IsPrecompile(address), Is.True);
}

[Test]
Expand All @@ -156,7 +155,7 @@ public void Is_precompiled_9_byzantium()
byte[] addressBytes = new byte[20];
addressBytes[19] = 9;
Address address = new(addressBytes);
Assert.That(address.IsPrecompile(Byzantium.Instance), Is.False);
Assert.That(Byzantium.Instance.IsPrecompile(address), Is.False);
}

[TestCase(0, false)]
Expand All @@ -165,7 +164,7 @@ public void Is_precompiled_9_byzantium()
public void From_number_for_precompile(int number, bool isPrecompile)
{
Address address = Address.FromNumber((UInt256)number);
Assert.That(address.IsPrecompile(Byzantium.Instance), Is.EqualTo(isPrecompile));
Assert.That(Byzantium.Instance.IsPrecompile(address), Is.EqualTo(isPrecompile));
}

[TestCase(0, "0x24cd2edba056b7c654a50e8201b619d4f624fdda")]
Expand All @@ -178,7 +177,7 @@ public void Of_contract(long nonce, string expectedAddress)

[TestCaseSource(nameof(PointEvaluationPrecompileTestCases))]
public bool Is_PointEvaluationPrecompile_properly_activated(IReleaseSpec spec) =>
Address.FromNumber(0x0a).IsPrecompile(spec);
spec.IsPrecompile(Address.FromNumber(0x0a));

[TestCase(Address.SystemUserHex, false)]
[TestCase("2" + Address.SystemUserHex, false)]
Expand Down
29 changes: 29 additions & 0 deletions src/Nethermind/Nethermind.Core/Precompiles/PrecompiledAddresses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

namespace Nethermind.Core.Precompiles;

/// <summary>
/// Contains static references to all known Ethereum precompile addresses.
/// </summary>
public static class PrecompiledAddresses
{
public static readonly AddressAsKey EcRecover = Address.FromNumber(0x01);
public static readonly AddressAsKey Sha256 = Address.FromNumber(0x02);
public static readonly AddressAsKey Ripemd160 = Address.FromNumber(0x03);
public static readonly AddressAsKey Identity = Address.FromNumber(0x04);
public static readonly AddressAsKey ModExp = Address.FromNumber(0x05);
public static readonly AddressAsKey Bn128Add = Address.FromNumber(0x06);
public static readonly AddressAsKey Bn128Mul = Address.FromNumber(0x07);
public static readonly AddressAsKey Bn128Pairing = Address.FromNumber(0x08);
public static readonly AddressAsKey Blake2F = Address.FromNumber(0x09);
public static readonly AddressAsKey PointEvaluation = Address.FromNumber(0x0a);
public static readonly AddressAsKey Bls12G1Add = Address.FromNumber(0x0b);
public static readonly AddressAsKey Bls12G1Mul = Address.FromNumber(0x0c);
public static readonly AddressAsKey Bls12G1MultiExp = Address.FromNumber(0x0d);
public static readonly AddressAsKey Bls12G2Add = Address.FromNumber(0x0e);
public static readonly AddressAsKey Bls12G2Mul = Address.FromNumber(0x0f);
public static readonly AddressAsKey Bls12G2MultiExp = Address.FromNumber(0x10);
public static readonly AddressAsKey Bls12Pairing = Address.FromNumber(0x11);
public static readonly AddressAsKey P256Verify = Address.FromNumber(0x0100);
}
14 changes: 14 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using Nethermind.Int256;

namespace Nethermind.Core.Specs
Expand Down Expand Up @@ -363,6 +364,19 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec
/// </summary>
bool IsEip7883Enabled { get; }

/// <summary>
/// Determines whether the specified address is a precompiled contract for this release specification.
/// </summary>
/// <param name="address">The address to check for precompile status.</param>
/// <returns>True if the address is a precompiled contract; otherwise, false.</returns>
bool IsPrecompile(Address address) => Precompiles.Contains(address);

/// <summary>
/// Gets a cached set of all precompiled contract addresses for this release specification.
/// Chain-specific implementations can override this to include their own precompiled contracts.
/// </summary>
HashSet<AddressAsKey> Precompiles { get; }

/// <summary>
/// Should transactions be validated against chainId.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/ReleaseSpecDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using Nethermind.Int256;

namespace Nethermind.Core.Specs;
Expand Down Expand Up @@ -147,4 +148,5 @@ public class ReleaseSpecDecorator(IReleaseSpec spec) : IReleaseSpec
Array? IReleaseSpec.EvmInstructionsNoTrace { get => spec.EvmInstructionsNoTrace; set => spec.EvmInstructionsNoTrace = value; }
Array? IReleaseSpec.EvmInstructionsTraced { get => spec.EvmInstructionsTraced; set => spec.EvmInstructionsTraced = value; }
public bool IsEip7939Enabled => spec.IsEip7939Enabled;
HashSet<AddressAsKey> IReleaseSpec.Precompiles => spec.Precompiles;
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm.Test/Eip1052Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void Non_existing_account_returns_0()
public void Non_existing_precompile_returns_0()
{
Address precompileAddress = Sha256Precompile.Address;
Assert.That(precompileAddress.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(precompileAddress), Is.True);

byte[] code = Prepare.EvmCode
.PushData(precompileAddress)
Expand All @@ -76,7 +76,7 @@ public void Non_existing_precompile_returns_0()
public void Existing_precompile_returns_empty_data_hash()
{
Address precompileAddress = Sha256Precompile.Address;
Assert.That(precompileAddress.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(precompileAddress), Is.True);

TestState.CreateAccount(precompileAddress, 1.Wei());

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm.Test/Eip152Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void before_istanbul()
{
_blockNumberAdjustment = -1;
Address precompileAddress = Blake2FPrecompile.Address;
Assert.That(precompileAddress.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(precompileAddress), Is.False);
}

[Test]
Expand Down
29 changes: 14 additions & 15 deletions src/Nethermind/Nethermind.Evm.Test/Eip2537Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using NUnit.Framework;
using Nethermind.Evm.Precompiles;
using Nethermind.Evm.Precompiles.Bls;
using Nethermind.Specs;

Expand All @@ -27,13 +26,13 @@ public override void TearDown()
public void Test_g1_add_before_prague()
{
_timestampAdjustment = -12;
Assert.That(G1AddPrecompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(G1AddPrecompile.Address), Is.False);
}

[Test]
public void Test_g1_add_after_prague()
{
Assert.That(G1AddPrecompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(G1AddPrecompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(G1AddPrecompile.Address, 1000L, new byte[256])
Expand All @@ -55,13 +54,13 @@ public void Test_g1_add_after_prague()
public void Test_g2_add_before_prague()
{
_timestampAdjustment = -12;
Assert.That(G2AddPrecompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(G2AddPrecompile.Address), Is.False);
}

[Test]
public void Test_g2_add_after_prague()
{
Assert.That(G2AddPrecompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(G2AddPrecompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(G2AddPrecompile.Address, 1000L, new byte[512])
Expand All @@ -83,13 +82,13 @@ public void Test_g2_add_after_prague()
public void Test_g1_msm_before_prague()
{
_timestampAdjustment = -12;
Assert.That(G1MSMPrecompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(G1MSMPrecompile.Address), Is.False);
}

[Test]
public void Test_g1_msm_after_prague()
{
Assert.That(G1MSMPrecompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(G1MSMPrecompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(G1MSMPrecompile.Address, 100000L, new byte[160])
Expand All @@ -111,13 +110,13 @@ public void Test_g1_msm_after_prague()
public void Test_g2_msm_before_prague()
{
_timestampAdjustment = -12;
Assert.That(G2MSMPrecompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(G2MSMPrecompile.Address), Is.False);
}

[Test]
public void Test_g2_msm_after_prague()
{
Assert.That(G2MSMPrecompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(G2MSMPrecompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(G2MSMPrecompile.Address, 100000L, new byte[288])
Expand All @@ -139,13 +138,13 @@ public void Test_g2_msm_after_prague()
public void Test_pairing_before_prague()
{
_timestampAdjustment = -12;
Assert.That(PairingCheckPrecompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(PairingCheckPrecompile.Address), Is.False);
}

[Test]
public void Test_pairing_check_after_prague()
{
Assert.That(PairingCheckPrecompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(PairingCheckPrecompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(PairingCheckPrecompile.Address, 100000L, new byte[384])
Expand All @@ -167,13 +166,13 @@ public void Test_pairing_check_after_prague()
public void Test_map_fp_to_g1_before_prague()
{
_timestampAdjustment = -12;
Assert.That(MapFpToG1Precompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(MapFpToG1Precompile.Address), Is.False);
}

[Test]
public void Test_map_fp_to_g1_after_prague()
{
Assert.That(MapFpToG1Precompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(MapFpToG1Precompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(MapFpToG1Precompile.Address, 10000L, new byte[64])
Expand All @@ -195,13 +194,13 @@ public void Test_map_fp_to_g1_after_prague()
public void Test_map_fp2_to_g2_before_prague()
{
_timestampAdjustment = -12;
Assert.That(MapFp2ToG2Precompile.Address.IsPrecompile(Spec), Is.False);
Assert.That(Spec.IsPrecompile(MapFp2ToG2Precompile.Address), Is.False);
}

[Test]
public void Test_map_fp2_to_g2_after_prague()
{
Assert.That(MapFp2ToG2Precompile.Address.IsPrecompile(Spec), Is.True);
Assert.That(Spec.IsPrecompile(MapFp2ToG2Precompile.Address), Is.True);

byte[] code = Prepare.EvmCode
.CallWithInput(MapFp2ToG2Precompile.Address, 100000L, new byte[128])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Trie.Pruning;
using NUnit.Framework;

namespace Nethermind.Evm.Test;
Expand Down
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public CodeInfoRepository(ConcurrentDictionary<PreBlockCaches.PrecompileCacheKey
public ICodeInfo GetCachedCodeInfo(IWorldState worldState, Address codeSource, bool followDelegation, IReleaseSpec vmSpec, out Address? delegationAddress)
{
delegationAddress = null;
if (codeSource.IsPrecompile(vmSpec))
if (vmSpec.IsPrecompile(codeSource))
{
return _localPrecompiles[codeSource];
}
Expand Down Expand Up @@ -271,4 +271,3 @@ public bool TryGet(in ValueHash256 codeHash, [NotNullWhen(true)] out ICodeInfo?
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Nethermind.Evm;
using unsafe OpCode = delegate*<VirtualMachineBase, ref EvmStack, ref long, ref int, EvmExceptionType>;
using Int256;
using Nethermind.Evm.Precompiles;

internal static unsafe partial class EvmInstructions
{
Expand Down Expand Up @@ -359,7 +358,7 @@ private static bool ChargeAccountAccessGas(ref long gasAvailable, VirtualMachine
}

// If the account is cold (and not a precompile), charge the cold access cost.
if (vmState.AccessTracker.IsCold(address) && !address.IsPrecompile(spec))
if (vmState.AccessTracker.IsCold(address) && !spec.IsPrecompile(address))
{
result = UpdateGas(GasCostOf.ColdAccountAccess, ref gasAvailable);
vmState.AccessTracker.WarmUp(address);
Expand Down
49 changes: 0 additions & 49 deletions src/Nethermind/Nethermind.Evm/Precompiles/AddressExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Nethermind.Core.Caching;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Evm.Precompiles;
using Nethermind.Logging;
#pragma warning disable CS0162 // Unreachable code detected

Expand Down Expand Up @@ -126,7 +125,7 @@ public Engine(IReleaseSpec spec)
/// <summary>
/// Checks if contract at given address is a precompile
/// </summary>
private bool IsPrecompiled(object address) => address.ToAddress().IsPrecompile(_spec);
private bool IsPrecompiled(object address) => _spec.IsPrecompile(address.ToAddress());

/// <summary>
/// Returns a slice of input
Expand Down
Loading