Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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
14 changes: 7 additions & 7 deletions src/Nethermind/Nethermind.Core.Test/AddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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 +129,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 +138,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 +147,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 +156,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 +165,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 +178,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
30 changes: 30 additions & 0 deletions src/Nethermind/Nethermind.Core/Precompiles/PrecompiledAddresses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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);
public static readonly AddressAsKey L1Sload = Address.FromNumber(0x10001);
}
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 @@ -496,6 +497,19 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec
/// </remarks>
public Array? EvmInstructionsTraced { get; set; }

/// <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; }

public ProofVersion BlobProofVersion => IsEip7594Enabled ? ProofVersion.V1 : ProofVersion.V0;

/// <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 @@ -150,6 +151,7 @@ public class ReleaseSpecDecorator(IReleaseSpec spec) : IReleaseSpec
public virtual bool ValidateReceipts => spec.ValidateReceipts;
Array? IReleaseSpec.EvmInstructionsNoTrace { get => spec.EvmInstructionsNoTrace; set => spec.EvmInstructionsNoTrace = value; }
Array? IReleaseSpec.EvmInstructionsTraced { get => spec.EvmInstructionsTraced; set => spec.EvmInstructionsTraced = value; }
HashSet<AddressAsKey> IReleaseSpec.Precompiles => spec.Precompiles;
public bool IsEip7939Enabled => spec.IsEip7939Enabled;
public bool IsEip7907Enabled => spec.IsEip7907Enabled;
public bool IsRip7728Enabled => spec.IsRip7728Enabled;
Expand Down
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
28 changes: 14 additions & 14 deletions src/Nethermind/Nethermind.Evm.Test/Eip2537Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,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 +55,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 +83,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 +111,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 +139,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 +167,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 +195,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
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm.Test/L1SloadPrecompileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public void Run_With_Provider_Returning_Null_Should_Fail()
[Test]
public void IsPrecompile_Active_With_Rip7728()
{
var enabledSpec = new ReleaseSpec { IsRip7728Enabled = true };
var disabledSpec = new ReleaseSpec { IsRip7728Enabled = false };
IReleaseSpec enabledSpec = new ReleaseSpec { IsRip7728Enabled = true };
IReleaseSpec disabledSpec = new ReleaseSpec { IsRip7728Enabled = false };

Address? precompileAddress = L1SloadPrecompile.Address;

Assert.That(precompileAddress.IsPrecompile(enabledSpec), Is.True,
Assert.That(enabledSpec.IsPrecompile(precompileAddress), Is.True,
"L1SloadPrecompile address should be identified as precompile when RIP-7728 is enabled");

Assert.That(precompileAddress.IsPrecompile(disabledSpec), Is.False,
Assert.That(disabledSpec.IsPrecompile(precompileAddress), Is.False,
"L1SloadPrecompile address should not be identified as precompile when RIP-7728 is disabled");
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected CodeInfoRepository(FrozenDictionary<AddressAsKey, PrecompileInfo> prec
}

public bool IsPrecompile(Address address, IReleaseSpec spec) =>
address.IsPrecompile(spec) && _localPrecompiles.ContainsKey(address);
spec.IsPrecompile(address) && _localPrecompiles.ContainsKey(address);

public ICodeInfo GetCachedCodeInfo(IWorldState worldState, Address codeSource, bool followDelegation, IReleaseSpec vmSpec, out Address? delegationAddress)
{
Expand Down
55 changes: 0 additions & 55 deletions src/Nethermind/Nethermind.Evm/Precompiles/AddressExtensions.cs

This file was deleted.

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.Core;
using Nethermind.Core.Specs;
using Nethermind.Int256;
Expand Down Expand Up @@ -198,5 +199,6 @@ public ulong Eip4844TransitionTimestamp
public bool IsEip7939Enabled => spec.IsEip7939Enabled;
public bool IsEip7907Enabled => spec.IsEip7907Enabled;
public bool IsRip7728Enabled => spec.IsRip7728Enabled;
HashSet<AddressAsKey> IReleaseSpec.Precompiles => spec.Precompiles;
}
}
Loading