Skip to content

eip-7883 implementation #8489

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec
/// </summary>
bool IsEip7623Enabled { get; }

/// <summary>
/// Increase ModExp Gas Cost
/// </summary>
bool IsEip7883Enabled { get; }

/// <summary>
/// Should transactions be validated against chainId.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class ReleaseSpecDecorator(IReleaseSpec spec) : IReleaseSpec
public virtual bool IsOpHoloceneEnabled => spec.IsOpHoloceneEnabled;
public virtual bool IsOntakeEnabled => spec.IsOntakeEnabled;
public virtual bool IsEip7623Enabled => spec.IsEip7623Enabled;
public bool IsEip7883Enabled => spec.IsEip7883Enabled;
public virtual ulong WithdrawalTimestamp => spec.WithdrawalTimestamp;
public virtual ulong Eip4844TransitionTimestamp => spec.Eip4844TransitionTimestamp;
public virtual bool IsEip158IgnoredAccount(Address address) => spec.IsEip158IgnoredAccount(address);
Expand Down
16 changes: 9 additions & 7 deletions src/Nethermind/Nethermind.Evm/Precompiles/ModExpPrecompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public long DataGasCost(ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec
UInt256 expLength = new(extendedInput.Slice(32, 32), true);
UInt256 modulusLength = new(extendedInput.Slice(64, 32), true);

UInt256 complexity = MultComplexity(baseLength, modulusLength);
UInt256 complexity = MultComplexity(baseLength, modulusLength, releaseSpec);

UInt256 expLengthUpTo32 = UInt256.Min(32, expLength);
UInt256 startIndex = 96 + baseLength; //+ expLength - expLengthUpTo32; // Geth takes head here, why?
UInt256 exp = new(inputData.Span.SliceWithZeroPaddingEmptyOnError((int)startIndex, (int)expLengthUpTo32), true);
UInt256 iterationCount = CalculateIterationCount(expLength, exp);
UInt256 iterationCount = CalculateIterationCount(expLength, exp, releaseSpec);
bool overflow = UInt256.MultiplyOverflow(complexity, iterationCount, out UInt256 result);
result /= 3;
return result > long.MaxValue || overflow ? long.MaxValue : Math.Max(200L, (long)result);
return result > long.MaxValue || overflow ? long.MaxValue : Math.Max(releaseSpec.IsEip7883Enabled ? 500L : 200L, (long)result);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we create constants in GasCostOf for these values?

}
catch (OverflowException)
{
Expand Down Expand Up @@ -173,12 +173,12 @@ public static (byte[], bool) OldRun(byte[] inputData)
/// return words**2
/// </summary>
/// <returns></returns>
private static UInt256 MultComplexity(in UInt256 baseLength, in UInt256 modulusLength)
private static UInt256 MultComplexity(in UInt256 baseLength, in UInt256 modulusLength, IReleaseSpec spec)
{
UInt256 maxLength = UInt256.Max(baseLength, modulusLength);
UInt256.Mod(maxLength, 8, out UInt256 mod8);
UInt256 words = (maxLength / 8) + ((mod8.IsZero) ? UInt256.Zero : UInt256.One);
return words * words;
return maxLength > 32 && spec.IsEip7883Enabled ? 2 * words * words : words * words;
}

/// <summary>
Expand All @@ -191,8 +191,9 @@ private static UInt256 MultComplexity(in UInt256 baseLength, in UInt256 modulusL
/// </summary>
/// <param name="exponentLength"></param>
/// <param name="exponent"></param>
/// <param name="spec"></param>
/// <returns></returns>
private static UInt256 CalculateIterationCount(UInt256 exponentLength, UInt256 exponent)
private static UInt256 CalculateIterationCount(UInt256 exponentLength, UInt256 exponent, IReleaseSpec spec)
{
try
{
Expand All @@ -209,7 +210,8 @@ private static UInt256 CalculateIterationCount(UInt256 exponentLength, UInt256 e
bitLength--;
}

bool overflow = UInt256.MultiplyOverflow((exponentLength - 32), 8, out UInt256 multiplicationResult);
var multiplier = (UInt256) (spec.IsEip7883Enabled ? 16 : 8);
bool overflow = UInt256.MultiplyOverflow(exponentLength - 32, multiplier, out UInt256 multiplicationResult);
overflow |= UInt256.AddOverflow(multiplicationResult, (UInt256)bitLength, out iterationCount);
if (overflow)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public bool IsOntakeEnabled
}
public bool IsEip7623Enabled => _spec.IsEip7623Enabled;

public bool IsEip7883Enabled => _spec.IsEip7883Enabled;

public bool IsEip3607Enabled { get; set; }

public bool IsEip158IgnoredAccount(Address address) => _spec.IsEip158IgnoredAccount(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public class ChainParameters
public ulong? OpGraniteTransitionTimestamp { get; set; }
public ulong? OpHoloceneTransitionTimestamp { get; set; }
public ulong? Eip7623TransitionTimestamp { get; set; }
public ulong? Eip7883TransitionTimestamp { get; set; }

public Dictionary<string, ChainSpecBlobCountJson> BlobSchedule { get; set; } = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private ReleaseSpec CreateReleaseSpec(ChainSpec chainSpec, long releaseStartBloc
releaseSpec.IsEip7251Enabled = (chainSpec.Parameters.Eip7251TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;
releaseSpec.Eip7251ContractAddress = chainSpec.Parameters.Eip7251ContractAddress;
releaseSpec.IsEip7623Enabled = (chainSpec.Parameters.Eip7623TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;
releaseSpec.IsEip7883Enabled = (chainSpec.Parameters.Eip7883TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;

releaseSpec.IsOntakeEnabled = (chainSpec.Parameters.OntakeTransition ?? long.MaxValue) <= releaseStartBlock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ bool GetForInnerPathExistence(KeyValuePair<string, JsonElement> o) =>
() => chainSpecJson.Params.ChainId == BlockchainIds.Mainnet ? Eip6110Constants.MainnetDepositContractAddress : null),
Eip7002TransitionTimestamp = chainSpecJson.Params.Eip7002TransitionTimestamp,
Eip7623TransitionTimestamp = chainSpecJson.Params.Eip7623TransitionTimestamp,
Eip7883TransitionTimestamp = chainSpecJson.Params.Eip7883TransitionTimestamp,
Eip7002ContractAddress = chainSpecJson.Params.Eip7002ContractAddress ?? Eip7002Constants.WithdrawalRequestPredeployAddress,
Eip7251TransitionTimestamp = chainSpecJson.Params.Eip7251TransitionTimestamp,
Eip7251ContractAddress = chainSpecJson.Params.Eip7251ContractAddress ?? Eip7251Constants.ConsolidationRequestPredeployAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ internal class ChainSpecParamsJson
public Address Eip7251ContractAddress { get; set; }
public ulong? Rip7212TransitionTimestamp { get; set; }
public ulong? Eip7702TransitionTimestamp { get; set; }
public ulong? Eip7883TransitionTimestamp { get; set; }
public ulong? OpGraniteTransitionTimestamp { get; set; }
public ulong? OpHoloceneTransitionTimestamp { get; set; }
public Dictionary<string, ChainSpecBlobCountJson> BlobSchedule { get; set; } = [];
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Specs/ReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public bool IsEip1559Enabled
public bool IsOpGraniteEnabled { get; set; }
public bool IsOpHoloceneEnabled { get; set; }
public bool IsEip7623Enabled { get; set; }
public bool IsEip7883Enabled { get; set; }
public bool IsEip5656Enabled { get; set; }
public bool IsEip6780Enabled { get; set; }
public bool IsEip4788Enabled { get; set; }
Expand Down
Loading