Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BLS12-381 and EIP-4844 precompiles #5857

Merged
merged 6 commits into from
Jun 23, 2023
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 @@ -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
Comment on lines +15 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading to a Vector256 wouldn't be even faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything but 4B integers does seem to work well with 20B address. May be we could cast it additionally to 8B int / Vector128(?) and have less comparisons, but it'd become too complicated

&& (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