Skip to content

Commit

Permalink
EIP-2537 (#6964)
Browse files Browse the repository at this point in the history
Co-authored-by: MarekM25 <marekm2504@gmail.com>
  • Loading branch information
Marchhill and MarekM25 authored May 7, 2024
1 parent 4e98f26 commit bf382fe
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 35 deletions.
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

0 comments on commit bf382fe

Please sign in to comment.