Skip to content

Commit

Permalink
Update BLS12-381 and EIP-4844 precompiles (#5857)
Browse files Browse the repository at this point in the history
* Update precompile addresses
* Remove mentions of System.Transaction(small unrelated refactoring)
* Optimize
  • Loading branch information
flcl42 authored Jun 23, 2023
1 parent 1bfc733 commit dcc2d20
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Transactions;

namespace Nethermind.Blockchain.Find;

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core.Test/AddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void Of_contract(long nonce, string expectedAddress)

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

public static IEnumerable PointEvaluationPrecompileTestCases
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core.Test/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void EqualToTransaction(this Transaction subject, Transaction expe
subject.Should().BeEquivalentTo(
expectation,
o => o
.ComparingByMembers<System.Transactions.Transaction>()
.ComparingByMembers<Transaction>()
.Using<Memory<byte>>(ctx => ctx.Subject.AsArray().Should().BeEquivalentTo(ctx.Expectation.AsArray()))
.WhenTypeIs<Memory<byte>>()
);
Expand Down
62 changes: 28 additions & 34 deletions src/Nethermind/Nethermind.Evm/Precompiles/AddressExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,41 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.InteropServices;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;

namespace Nethermind.Evm.Precompiles
namespace Nethermind.Evm.Precompiles;

public static class AddressExtensions
{
public static class AddressExtensions
public static bool IsPrecompile(this Address address, IReleaseSpec releaseSpec)
{
private static byte[] _nineteenZeros = new byte[19];

public static bool IsPrecompile(this Address address, IReleaseSpec releaseSpec)
{
if (!Bytes.AreEqual(address.Bytes.AsSpan(0, 19), _nineteenZeros))
{
return false;
}

int precompileCode = address[19];
return precompileCode switch
Span<uint> data = MemoryMarshal.Cast<byte, uint>(address.Bytes.AsSpan());
return (data[4] & 0x00ffffff) == 0
&& data[3] == 0 && data[2] == 0 && data[1] == 0 && data[0] == 0
&& (data[4] >>> 24) switch
{
1 => true,
2 => true,
3 => true,
4 => true,
5 => releaseSpec.ModExpEnabled,
6 => releaseSpec.Bn128Enabled,
7 => releaseSpec.Bn128Enabled,
8 => releaseSpec.Bn128Enabled,
9 => releaseSpec.BlakeEnabled,
10 => releaseSpec.Bls381Enabled,
11 => releaseSpec.Bls381Enabled,
12 => releaseSpec.Bls381Enabled,
13 => releaseSpec.Bls381Enabled,
14 => releaseSpec.Bls381Enabled,
15 => releaseSpec.Bls381Enabled,
16 => releaseSpec.Bls381Enabled,
17 => releaseSpec.Bls381Enabled,
18 => releaseSpec.Bls381Enabled,
20 => releaseSpec.IsEip4844Enabled,
0x01 => true,
0x02 => true,
0x03 => true,
0x04 => true,
0x05 => releaseSpec.ModExpEnabled,
0x06 => releaseSpec.Bn128Enabled,
0x07 => releaseSpec.Bn128Enabled,
0x08 => releaseSpec.Bn128Enabled,
0x09 => releaseSpec.BlakeEnabled,
0x0a => releaseSpec.IsEip4844Enabled,
0x0c => releaseSpec.Bls381Enabled,
0x0d => releaseSpec.Bls381Enabled,
0x0e => releaseSpec.Bls381Enabled,
0x0f => releaseSpec.Bls381Enabled,
0x10 => releaseSpec.Bls381Enabled,
0x11 => releaseSpec.Bls381Enabled,
0x12 => releaseSpec.Bls381Enabled,
0x13 => releaseSpec.Bls381Enabled,
0x14 => releaseSpec.Bls381Enabled,
_ => false
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G1AddPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(10);
public Address Address { get; } = Address.FromNumber(0x0c);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G1MulPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(11);
public Address Address { get; } = Address.FromNumber(0x0d);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G1MultiExpPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(12);
public Address Address { get; } = Address.FromNumber(0x0e);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G2AddPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(13);
public Address Address { get; } = Address.FromNumber(0x0f);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G2MulPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(14);
public Address Address { get; } = Address.FromNumber(0x10);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G2MultiExpPrecompile()
{
}

public Address Address { get; } = Address.FromNumber(15);
public Address Address { get; } = Address.FromNumber(0x11);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private MapToG1Precompile()
{
}

public Address Address { get; } = Address.FromNumber(17);
public Address Address { get; } = Address.FromNumber(0x13);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private MapToG2Precompile()
{
}

public Address Address { get; } = Address.FromNumber(18);
public Address Address { get; } = Address.FromNumber(0x14);

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PairingPrecompile : IPrecompile

private PairingPrecompile() { }

public Address Address { get; } = Address.FromNumber(16);
public Address Address { get; } = Address.FromNumber(0x12);

public static IPrecompile Instance = new PairingPrecompile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PointEvaluationPrecompile : IPrecompile
.Concat(KzgPolynomialCommitments.BlsModulus.ToBigEndian())
.ToArray();

public Address Address { get; } = Address.FromNumber(0x14);
public Address Address { get; } = Address.FromNumber(0x0a);

public long BaseGasCost(IReleaseSpec releaseSpec) => 50000L;

Expand Down

0 comments on commit dcc2d20

Please sign in to comment.