@@ -24,8 +24,9 @@ import type { PrecompileInput } from './types.ts'
2424
2525const BIGINT_4 = BigInt ( 4 )
2626const BIGINT_16 = BigInt ( 16 )
27- const BIGINT_200 = BigInt ( 200 )
2827const BIGINT_480 = BigInt ( 480 )
28+ const BIGINT_200 = BigInt ( 200 )
29+ const BIGINT_500 = BigInt ( 500 )
2930const BIGINT_1024 = BigInt ( 1024 )
3031const BIGINT_3072 = BigInt ( 3072 )
3132const BIGINT_199680 = BigInt ( 199680 )
@@ -56,7 +57,7 @@ function multiplicationComplexityEIP2565(x: bigint): bigint {
5657 return words * words
5758}
5859
59- function getAdjustedExponentLength ( data : Uint8Array ) : bigint {
60+ function getAdjustedExponentLength ( data : Uint8Array , opts : PrecompileInput ) : bigint {
6061 let expBytesStart
6162 try {
6263 const baseLen = bytesToBigInt ( data . subarray ( 0 , 32 ) )
@@ -83,8 +84,8 @@ function getAdjustedExponentLength(data: Uint8Array): bigint {
8384 if ( expLenMinus32OrZero < BIGINT_0 ) {
8485 expLenMinus32OrZero = BIGINT_0
8586 }
86- const eightTimesExpLenMinus32OrZero = expLenMinus32OrZero * BIGINT_8
87- let adjustedExpLen = eightTimesExpLenMinus32OrZero
87+ let adjustedExpLen =
88+ expLenMinus32OrZero * ( opts . common . isActivatedEIP ( 7883 ) === true ? BIGINT_16 : BIGINT_8 )
8889 if ( bitLen > 0 ) {
8990 adjustedExpLen += BigInt ( bitLen )
9091 }
@@ -108,7 +109,7 @@ export function precompile05(opts: PrecompileInput): ExecResult {
108109 const pName = getPrecompileName ( '05' )
109110 const data = opts . data . length < 96 ? setLengthRight ( opts . data , 96 ) : opts . data
110111
111- let adjustedELen = getAdjustedExponentLength ( data )
112+ let adjustedELen = getAdjustedExponentLength ( data , opts )
112113 if ( adjustedELen < BIGINT_1 ) {
113114 adjustedELen = BIGINT_1
114115 }
@@ -131,13 +132,19 @@ export function precompile05(opts: PrecompileInput): ExecResult {
131132 const mStart = eEnd
132133 const mEnd = mStart + mLen
133134
134- if ( ! opts . common . isActivatedEIP ( 2565 ) ) {
135- gasUsed = ( adjustedELen * multiplicationComplexity ( maxLen ) ) / Gquaddivisor
136- } else {
135+ if ( opts . common . isActivatedEIP ( 7883 ) ) {
136+ const wordsSquared = multiplicationComplexityEIP2565 ( maxLen )
137+ gasUsed = ( adjustedELen * ( maxLen > 32 ? 2n * wordsSquared : wordsSquared ) ) / Gquaddivisor
138+ if ( gasUsed < BIGINT_500 ) {
139+ gasUsed = BIGINT_500
140+ }
141+ } else if ( opts . common . isActivatedEIP ( 2565 ) ) {
137142 gasUsed = ( adjustedELen * multiplicationComplexityEIP2565 ( maxLen ) ) / Gquaddivisor
138143 if ( gasUsed < BIGINT_200 ) {
139144 gasUsed = BIGINT_200
140145 }
146+ } else {
147+ gasUsed = ( adjustedELen * multiplicationComplexity ( maxLen ) ) / Gquaddivisor
141148 }
142149 if ( ! gasLimitCheck ( opts , gasUsed , pName ) ) {
143150 return OOGResult ( opts . gasLimit )
0 commit comments