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

EIP-2537 #6964

Merged
merged 28 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
07abc26
start implementing subgroup checks
Marchhill Apr 25, 2024
8e674bb
g1 subgroup check
Marchhill Apr 25, 2024
4071c3a
g2 subgroup checks
Marchhill Apr 26, 2024
09bf7c5
fix subgroup checks
Marchhill Apr 26, 2024
5bd7c3b
checks for multiexp and pairing
Marchhill Apr 26, 2024
bb47028
fix whitespace
Marchhill Apr 27, 2024
a5235a1
optimise sg checks
Marchhill Apr 27, 2024
08823f4
change constant endianness
Marchhill Apr 27, 2024
28ee4b4
eip2537 tests submodule
Marchhill Apr 30, 2024
4a4e239
load paris tests
Marchhill May 1, 2024
9eedb1d
timestamp enabled
Marchhill May 1, 2024
c5e660f
add Eip2537Tests
MarekM25 May 1, 2024
c3dac14
add some gas costs
Marchhill May 1, 2024
a2a117b
Merge branch 'feature/fast-sg-checks' into feature/bls-tests
Marchhill May 1, 2024
85dd47c
fix precompile addresses
MarekM25 May 2, 2024
f99119a
precompile address extensions
Marchhill May 2, 2024
9e8e520
update pairing and g2 multiexp costs
Marchhill May 2, 2024
dee0fb9
restore launch settings
Marchhill May 2, 2024
3c76afa
updated ethereum tests
Marchhill May 2, 2024
4094349
Merge remote-tracking branch 'upstream/master' into feature/bls-tests
Marchhill May 2, 2024
7cf4691
fix some tests
Marchhill May 2, 2024
ac6eba2
update tests submodue stCreate2
Marchhill May 3, 2024
655e615
try disabling eip2537 state tests
Marchhill May 3, 2024
a48a24f
restore eip2537 tests
Marchhill May 3, 2024
2e7436c
revert to old eth tests
Marchhill May 3, 2024
4ba8b31
fix whitespace
Marchhill May 3, 2024
86abf8f
revert metrics change
Marchhill May 7, 2024
998227e
remove old comments
Marchhill May 7, 2024
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
26 changes: 26 additions & 0 deletions src/Nethermind/Ethereum.Blockchain.Test/Eip2537Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

// using System.Collections.Generic;
// using Ethereum.Test.Base;
// using NUnit.Framework;

// namespace Ethereum.Blockchain.Test
// {
// [TestFixture]
// [Parallelizable(ParallelScope.All)]
// public class Eip2537Tests : GeneralStateTestBase
// {
// [TestCaseSource(nameof(LoadTests))]
// public void Test(GeneralStateTest test)
// {
// Assert.True(RunTest(test).Pass);
// }

// public static IEnumerable<GeneralStateTest> LoadTests()
// {
// var loader = new TestsSourceLoader(new LoadGeneralStateTestsStrategy(), "../EIPTests/StateTests/stEIP2537");
// return (IEnumerable<GeneralStateTest>)loader.LoadTests();
// }
// }
// }
2 changes: 2 additions & 0 deletions src/Nethermind/Ethereum.Test.Base/JsonToEthereumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private static IReleaseSpec ParseSpec(string network)
"GrayGlacier" => GrayGlacier.Instance,
"Shanghai" => Shanghai.Instance,
"Cancun" => Cancun.Instance,
"Paris" => Paris.Instance,
"Prague" => Prague.Instance,
_ => throw new NotSupportedException()
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Ethereum.Transition.Test/MetaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void All_categories_are_tested()
{
string[] directories = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tests"))
.Select(Path.GetFileName)
.Except(new[] { "bcArrowGlacierToMerge" }) // this one is missing
.Except(new[] { "bcArrowGlacierToMerge", "bcArrowGlacierToParis" }) // these ones are missing
.ToArray();
Type[] types = GetType().Assembly.GetTypes();
List<string> missingCategories = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static bool IsPrecompile(this Address address, IReleaseSpec releaseSpec)
0x08 => releaseSpec.Bn128Enabled,
0x09 => releaseSpec.BlakeEnabled,
0x0a => releaseSpec.IsEip4844Enabled,
0x0b => releaseSpec.Bls381Enabled,
0x0c => releaseSpec.Bls381Enabled,
0x0d => releaseSpec.Bls381Enabled,
0x0e => releaseSpec.Bls381Enabled,
Expand All @@ -35,7 +36,6 @@ public static bool IsPrecompile(this Address address, IReleaseSpec releaseSpec)
0x11 => releaseSpec.Bls381Enabled,
0x12 => releaseSpec.Bls381Enabled,
0x13 => releaseSpec.Bls381Enabled,
0x14 => releaseSpec.Bls381Enabled,
_ => false
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ private G1AddPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
return 600L;
return 500L;
}

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand All @@ -39,9 +39,6 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

// Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
// inputData.PrepareEthInput(inputDataSpan);

(byte[], bool) result;

Span<byte> output = stackalloc byte[2 * BlsParams.LenFp];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G1MulPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand All @@ -39,13 +39,12 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

// Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
// inputData.PrepareEthInput(inputDataSpan);

(byte[], bool) result;

Span<byte> output = stackalloc byte[2 * BlsParams.LenFp];
bool success = Pairings.BlsG1Mul(inputData.Span, output);
bool success = SubgroupChecks.G1IsInSubGroup(inputData.Span[..(2 * BlsParams.LenFp)])
&& Pairings.BlsG1Mul(inputData.Span, output);

if (success)
{
result = (output.ToArray(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G1MultiExpPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand All @@ -41,6 +41,15 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

for (int i = 0; i < (inputData.Length / ItemSize); i++)
{
int offset = i * ItemSize;
if (!SubgroupChecks.G1IsInSubGroup(inputData.Span[offset..(offset + (2 * BlsParams.LenFp))]))
{
return (Array.Empty<byte>(), false);
}
}

(byte[], bool) result;

Span<byte> output = stackalloc byte[2 * BlsParams.LenFp];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ private G2AddPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
return 4500L;
return 800L;
}

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand All @@ -39,9 +39,6 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

// Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
// inputData.PrepareEthInput(inputDataSpan);

(byte[], bool) result;

Span<byte> output = stackalloc byte[4 * BlsParams.LenFp];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ private G2MulPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
return 55000L;
return 45000L;
}

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand All @@ -45,7 +45,8 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
(byte[], bool) result;

Span<byte> output = stackalloc byte[4 * BlsParams.LenFp];
bool success = Pairings.BlsG2Mul(inputData.Span, output);
bool success = SubgroupChecks.G2IsInSubGroup(inputData.Span[..(4 * BlsParams.LenFp)])
&& Pairings.BlsG2Mul(inputData.Span, output);
if (success)
{
result = (output.ToArray(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private G2MultiExpPrecompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand All @@ -29,7 +29,7 @@ public long BaseGasCost(IReleaseSpec releaseSpec)
public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
{
int k = inputData.Length / ItemSize;
return 55000L * k * Discount.For(k) / 1000;
return 45000L * k * Discount.For(k) / 1000;
}

private const int ItemSize = 288;
Expand All @@ -41,6 +41,15 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

for (int i = 0; i < (inputData.Length / ItemSize); i++)
{
int offset = i * ItemSize;
if (!SubgroupChecks.G2IsInSubGroup(inputData.Span[offset..(offset + (4 * BlsParams.LenFp))]))
{
return (Array.Empty<byte>(), false);
}
}

(byte[], bool) result;

Span<byte> output = stackalloc byte[4 * BlsParams.LenFp];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private MapToG1Precompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
Expand All @@ -39,9 +39,6 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

// Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
// inputData.PrepareEthInput(inputDataSpan);

(byte[], bool) result;

Span<byte> output = stackalloc byte[128];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ private MapToG2Precompile()
{
}

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

public long BaseGasCost(IReleaseSpec releaseSpec)
{
return 110000;
return 75000;
}

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand All @@ -39,9 +39,6 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
return (Array.Empty<byte>(), false);
}

// Span<byte> inputDataSpan = stackalloc byte[2 * BlsParams.LenFp];
// inputData.PrepareEthInput(inputDataSpan);

(byte[], bool) result;

Span<byte> output = stackalloc byte[4 * BlsParams.LenFp];
Expand Down
23 changes: 20 additions & 3 deletions src/Nethermind/Nethermind.Evm/Precompiles/Bls/PairingPrecompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class PairingPrecompile : IPrecompile<PairingPrecompile>

private PairingPrecompile() { }

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

public static PairingPrecompile Instance = new PairingPrecompile();

public long BaseGasCost(IReleaseSpec releaseSpec) => 115000L;
public long BaseGasCost(IReleaseSpec releaseSpec) => 65000L;

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
{
return 23000L * (inputData.Length / PairSize);
return 43000L * (inputData.Length / PairSize);
}

public (ReadOnlyMemory<byte>, bool) Run(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand All @@ -38,6 +38,23 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS
(byte[], bool) result;

Span<byte> output = stackalloc byte[32];

for (int i = 0; i < (inputData.Length / PairSize); i++)
{
int offset = i * PairSize;
if (!SubgroupChecks.G1IsInSubGroup(inputData.Span[offset..(offset + (2 * BlsParams.LenFp))]))
{
return (Array.Empty<byte>(), false);
}

offset += 2 * BlsParams.LenFp;

if (!SubgroupChecks.G2IsInSubGroup(inputData.Span[offset..(offset + (4 * BlsParams.LenFp))]))
{
return (Array.Empty<byte>(), false);
}
}

bool success = Pairings.BlsPairing(inputData.Span, output);
if (success)
{
Expand Down
Loading