From 78e4b9b13cb1b0465a4110b74dd76960cdd9cbf8 Mon Sep 17 00:00:00 2001 From: Marc Harvey-Hill Date: Wed, 18 Dec 2024 23:31:47 +0800 Subject: [PATCH 1/2] remove mul precompiles --- .../BlsMulG1PrecompileTests.cs | 2 +- .../BlsMulG2PrecompileTests.cs | 2 +- .../Nethermind.Evm/CodeInfoRepository.cs | 2 - .../Precompiles/Bls/G1MSMPrecompile.cs | 2 +- .../Precompiles/Bls/G1MulPrecompile.cs | 59 ------------------ .../Precompiles/Bls/G2AddPrecompile.cs | 2 +- .../Precompiles/Bls/G2MSMPrecompile.cs | 2 +- .../Precompiles/Bls/G2MulPrecompile.cs | 61 ------------------- .../Precompiles/Bls/MapFp2ToG2Precompile.cs | 2 +- .../Precompiles/Bls/MapFpToG1Precompile.cs | 2 +- .../Precompiles/Bls/PairingCheckPrecompile.cs | 2 +- 11 files changed, 8 insertions(+), 130 deletions(-) delete mode 100644 src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MulPrecompile.cs delete mode 100644 src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MulPrecompile.cs diff --git a/src/Nethermind/Nethermind.Evm.Test/BlsMulG1PrecompileTests.cs b/src/Nethermind/Nethermind.Evm.Test/BlsMulG1PrecompileTests.cs index 26efd4d3724..c1089b16060 100644 --- a/src/Nethermind/Nethermind.Evm.Test/BlsMulG1PrecompileTests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/BlsMulG1PrecompileTests.cs @@ -17,7 +17,7 @@ public class BlsG1MulPrecompileTests [Test] public void Test() { - IPrecompile precompile = G1MulPrecompile.Instance; + IPrecompile precompile = G1MSMPrecompile.Instance; foreach ((byte[] input, ReadOnlyMemory expectedResult) in Inputs) { diff --git a/src/Nethermind/Nethermind.Evm.Test/BlsMulG2PrecompileTests.cs b/src/Nethermind/Nethermind.Evm.Test/BlsMulG2PrecompileTests.cs index 625dd89e7df..3ab9a85a5d8 100644 --- a/src/Nethermind/Nethermind.Evm.Test/BlsMulG2PrecompileTests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/BlsMulG2PrecompileTests.cs @@ -19,7 +19,7 @@ public void Test() { foreach ((byte[] input, ReadOnlyMemory expectedResult) in Inputs) { - IPrecompile precompile = G2MulPrecompile.Instance; + IPrecompile precompile = G2MSMPrecompile.Instance; (ReadOnlyMemory output, bool success) = precompile.Run(input, MuirGlacier.Instance); output.ToArray().Should().BeEquivalentTo(expectedResult.ToArray()); success.Should().BeTrue(); diff --git a/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs b/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs index 6103e2df810..499758f8c38 100644 --- a/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs +++ b/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs @@ -42,10 +42,8 @@ private static FrozenDictionary InitializePrecompiledCon [Blake2FPrecompile.Address] = new(Blake2FPrecompile.Instance), [G1AddPrecompile.Address] = new(G1AddPrecompile.Instance), - [G1MulPrecompile.Address] = new(G1MulPrecompile.Instance), [G1MSMPrecompile.Address] = new(G1MSMPrecompile.Instance), [G2AddPrecompile.Address] = new(G2AddPrecompile.Instance), - [G2MulPrecompile.Address] = new(G2MulPrecompile.Instance), [G2MSMPrecompile.Address] = new(G2MSMPrecompile.Instance), [PairingCheckPrecompile.Address] = new(PairingCheckPrecompile.Instance), [MapFpToG1Precompile.Address] = new(MapFpToG1Precompile.Instance), diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MSMPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MSMPrecompile.cs index 8f3f734f37f..c573bc83836 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MSMPrecompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MSMPrecompile.cs @@ -23,7 +23,7 @@ private G1MSMPrecompile() { } - public static Address Address { get; } = Address.FromNumber(0x0d); + public static Address Address { get; } = Address.FromNumber(0x0c); public long BaseGasCost(IReleaseSpec releaseSpec) => 0L; diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MulPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MulPrecompile.cs deleted file mode 100644 index 18eb5889913..00000000000 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G1MulPrecompile.cs +++ /dev/null @@ -1,59 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited -// SPDX-License-Identifier: LGPL-3.0-only - -using System; -using System.Runtime.CompilerServices; -using Nethermind.Core; -using Nethermind.Core.Specs; -using G1 = Nethermind.Crypto.Bls.P1; - -namespace Nethermind.Evm.Precompiles.Bls; - -/// -/// https://eips.ethereum.org/EIPS/eip-2537 -/// -public class G1MulPrecompile : IPrecompile -{ - public static readonly G1MulPrecompile Instance = new(); - - private G1MulPrecompile() - { - } - - public static Address Address { get; } = Address.FromNumber(0x0c); - - public long BaseGasCost(IReleaseSpec releaseSpec) => 12000L; - - public long DataGasCost(ReadOnlyMemory inputData, IReleaseSpec releaseSpec) => 0L; - - [SkipLocalsInit] - public (ReadOnlyMemory, bool) Run(ReadOnlyMemory inputData, IReleaseSpec releaseSpec) - { - Metrics.BlsG1MulPrecompile++; - - const int expectedInputLength = BlsConst.LenG1 + BlsConst.LenFr; - if (inputData.Length != expectedInputLength) - { - return IPrecompile.Failure; - } - - G1 x = new(stackalloc long[G1.Sz]); - if (!x.TryDecodeRaw(inputData[..BlsConst.LenG1].Span) || !(BlsConst.DisableSubgroupChecks || x.InGroup())) - { - return IPrecompile.Failure; - } - - bool scalarIsInfinity = !inputData.Span[BlsConst.LenG1..].ContainsAnyExcept((byte)0); - if (scalarIsInfinity || x.IsInf()) - { - return (BlsConst.G1Inf, true); - } - - Span scalar = stackalloc byte[32]; - inputData.Span[BlsConst.LenG1..].CopyTo(scalar); - scalar.Reverse(); - - G1 res = x.Mult(scalar); - return (res.EncodeRaw(), true); - } -} diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2AddPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2AddPrecompile.cs index 710954f3a3b..01b09be71c0 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2AddPrecompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2AddPrecompile.cs @@ -21,7 +21,7 @@ private G2AddPrecompile() { } - public static Address Address { get; } = Address.FromNumber(0x0e); + public static Address Address { get; } = Address.FromNumber(0x0d); public long BaseGasCost(IReleaseSpec releaseSpec) => 800L; diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MSMPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MSMPrecompile.cs index 495e5c91a59..068f9dca53b 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MSMPrecompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MSMPrecompile.cs @@ -23,7 +23,7 @@ private G2MSMPrecompile() { } - public static Address Address { get; } = Address.FromNumber(0x10); + public static Address Address { get; } = Address.FromNumber(0xe); public long BaseGasCost(IReleaseSpec releaseSpec) => 0L; diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MulPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MulPrecompile.cs deleted file mode 100644 index fbe99e32867..00000000000 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/G2MulPrecompile.cs +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited -// SPDX-License-Identifier: LGPL-3.0-only - -using System; -using System.Runtime.CompilerServices; -using Nethermind.Core; -using Nethermind.Core.Specs; - -using G2 = Nethermind.Crypto.Bls.P2; - -namespace Nethermind.Evm.Precompiles.Bls; - -/// -/// https://eips.ethereum.org/EIPS/eip-2537 -/// -public class G2MulPrecompile : IPrecompile -{ - public static readonly G2MulPrecompile Instance = new(); - - private G2MulPrecompile() - { - } - - public static Address Address { get; } = Address.FromNumber(0x0f); - - public long BaseGasCost(IReleaseSpec releaseSpec) => 45000L; - - public long DataGasCost(ReadOnlyMemory inputData, IReleaseSpec releaseSpec) => 0L; - - [SkipLocalsInit] - public (ReadOnlyMemory, bool) Run(ReadOnlyMemory inputData, IReleaseSpec releaseSpec) - { - Metrics.BlsG2MulPrecompile++; - - const int expectedInputLength = BlsConst.LenG2 + BlsConst.LenFr; - - if (inputData.Length != expectedInputLength) - { - return IPrecompile.Failure; - } - - G2 x = new(stackalloc long[G2.Sz]); - if (!x.TryDecodeRaw(inputData[..BlsConst.LenG2].Span) || !(BlsConst.DisableSubgroupChecks || x.InGroup())) - { - return IPrecompile.Failure; - } - - bool scalarIsInfinity = !inputData[BlsConst.LenG2..].Span.ContainsAnyExcept((byte)0); - if (scalarIsInfinity || x.IsInf()) - { - return (BlsConst.G2Inf, true); - } - - Span scalar = stackalloc byte[32]; - inputData.Span[BlsConst.LenG2..].CopyTo(scalar); - scalar.Reverse(); - - G2 res = x.Mult(scalar); - return (res.EncodeRaw(), true); - } -} diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFp2ToG2Precompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFp2ToG2Precompile.cs index 5190c58be57..1482dfe43de 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFp2ToG2Precompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFp2ToG2Precompile.cs @@ -21,7 +21,7 @@ private MapFp2ToG2Precompile() { } - public static Address Address { get; } = Address.FromNumber(0x13); + public static Address Address { get; } = Address.FromNumber(0x11); public long BaseGasCost(IReleaseSpec releaseSpec) => 75000; diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFpToG1Precompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFpToG1Precompile.cs index eb1032ea1f5..f0bbd633509 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFpToG1Precompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/MapFpToG1Precompile.cs @@ -20,7 +20,7 @@ private MapFpToG1Precompile() { } - public static Address Address { get; } = Address.FromNumber(0x12); + public static Address Address { get; } = Address.FromNumber(0x10); public long BaseGasCost(IReleaseSpec releaseSpec) => 5500L; diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/PairingCheckPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/PairingCheckPrecompile.cs index 75cd9167364..fd115731ca5 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Bls/PairingCheckPrecompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Bls/PairingCheckPrecompile.cs @@ -23,7 +23,7 @@ public class PairingCheckPrecompile : IPrecompile private PairingCheckPrecompile() { } - public static Address Address { get; } = Address.FromNumber(0x11); + public static Address Address { get; } = Address.FromNumber(0xf); public long BaseGasCost(IReleaseSpec releaseSpec) => 65000L; From c03442bc58125dcdf66166ee3a59cbefa2507cee Mon Sep 17 00:00:00 2001 From: Marc Harvey-Hill Date: Tue, 24 Dec 2024 13:34:00 +0000 Subject: [PATCH 2/2] use MSM in MUL benchmarks --- .../Nethermind.Precompiles.Benchmark/BlsG1MulBenchmark.cs | 2 +- .../Nethermind.Precompiles.Benchmark/BlsG2MulBenchmark.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG1MulBenchmark.cs b/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG1MulBenchmark.cs index 7d1adc95338..b32430d5b9d 100644 --- a/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG1MulBenchmark.cs +++ b/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG1MulBenchmark.cs @@ -11,7 +11,7 @@ public class BlsG1MulBenchmark : PrecompileBenchmarkBase { protected override IEnumerable Precompiles => new[] { - G1MulPrecompile.Instance + G1MSMPrecompile.Instance }; protected override string InputsDirectory => "blsg1mul"; diff --git a/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG2MulBenchmark.cs b/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG2MulBenchmark.cs index 6df853a9778..6eb529ed24f 100644 --- a/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG2MulBenchmark.cs +++ b/src/Nethermind/Nethermind.Precompiles.Benchmark/BlsG2MulBenchmark.cs @@ -11,7 +11,7 @@ public class BlsG2MulBenchmark : PrecompileBenchmarkBase { protected override IEnumerable Precompiles => new[] { - G2MulPrecompile.Instance + G2MSMPrecompile.Instance }; protected override string InputsDirectory => "blsg2mul";