@@ -22,36 +22,9 @@ function multComplexity(x: BN): BN {
22
22
}
23
23
}
24
24
25
- function calculateGasEIP2565 (
26
- baseLength : BN ,
27
- modulusLength : BN ,
28
- exponentLength : BN ,
29
- exponent : BN
30
- ) : BN {
31
- console . log (
32
- baseLength . toNumber ( ) ,
33
- modulusLength . toNumber ( ) ,
34
- exponentLength . toNumber ( ) ,
35
- exponent . toNumber ( )
36
- )
37
- const maxLength = BN . max ( baseLength , modulusLength )
38
- const words = maxLength . divn ( 8 )
39
- // Ceil operation
40
- if ( maxLength . mod ( new BN ( 8 ) ) . gtn ( 0 ) ) {
41
- words . iaddn ( 1 )
42
- }
43
- const multiplicationComplexity = words . pow ( new BN ( 2 ) )
44
- let iterationCount
45
- if ( exponentLength . lten ( 32 ) && exponent . eqn ( 0 ) ) {
46
- iterationCount = new BN ( 0 )
47
- } else if ( exponentLength . lten ( 32 ) ) {
48
- iterationCount = new BN ( exponent . bitLength ( ) - 1 )
49
- } else {
50
- const expMask = exponent . and ( new BN ( Buffer . alloc ( 32 , 0xff ) ) )
51
- iterationCount = new BN ( 8 ) . mul ( exponentLength . subn ( 32 ) ) . addn ( expMask . bitLength ( ) - 1 )
52
- }
53
- iterationCount = BN . max ( new BN ( 1 ) , iterationCount )
54
- return BN . max ( new BN ( 200 ) , multiplicationComplexity . mul ( iterationCount ) . divn ( 3 ) )
25
+ function multComplexityEIP2565 ( x : BN ) : BN {
26
+ const words = x . addn ( 7 ) . divn ( 8 )
27
+ return words . mul ( words )
55
28
}
56
29
57
30
function getAdjustedExponentLength ( data : Buffer ) : BN {
@@ -145,7 +118,10 @@ export default function (opts: PrecompileInput): ExecResult {
145
118
if ( ! opts . _common . eips ( ) . includes ( 2565 ) ) {
146
119
gasUsed = adjustedELen . mul ( multComplexity ( maxLen ) ) . divn ( Gquaddivisor )
147
120
} else {
148
- gasUsed = calculateGasEIP2565 ( bLen , eLen , mLen , E )
121
+ gasUsed = adjustedELen . mul ( multComplexityEIP2565 ( maxLen ) ) . divn ( Gquaddivisor )
122
+ if ( gasUsed . ltn ( 200 ) ) {
123
+ gasUsed = new BN ( 200 )
124
+ }
149
125
}
150
126
151
127
if ( opts . gasLimit . lt ( gasUsed ) ) {
@@ -155,7 +131,7 @@ export default function (opts: PrecompileInput): ExecResult {
155
131
if ( bLen . isZero ( ) ) {
156
132
return {
157
133
gasUsed,
158
- returnValue : new BN ( 0 ) . toArrayLike ( Buffer , 'be' , 1 ) ,
134
+ returnValue : new BN ( 0 ) . toArrayLike ( Buffer , 'be' , mLen . toNumber ( ) ) ,
159
135
}
160
136
}
161
137
0 commit comments