11from typing import (
2+ Callable ,
23 Tuple ,
34)
45
56from eth_utils import (
67 big_endian_to_int ,
78 int_to_big_endian ,
89)
10+ from eth_utils .toolz import (
11+ curry ,
12+ )
913
1014from eth import constants
1115
2327)
2428
2529
26- def _compute_adjusted_exponent_length (exponent_length : int ,
27- first_32_exponent_bytes : bytes ) -> int :
30+ def compute_adjusted_exponent_length (exponent_length : int ,
31+ first_32_exponent_bytes : bytes ) -> int :
2832 exponent = big_endian_to_int (first_32_exponent_bytes )
2933
3034 if exponent_length <= 32 and exponent == 0 :
@@ -50,7 +54,7 @@ def _compute_complexity(length: int) -> int:
5054 return length ** 2 // 16 + 480 * length - 199680
5155
5256
53- def _extract_lengths (data : bytes ) -> Tuple [int , int , int ]:
57+ def extract_lengths (data : bytes ) -> Tuple [int , int , int ]:
5458 # extract argument lengths
5559 base_length_bytes = pad32r (data [:32 ])
5660 base_length = big_endian_to_int (base_length_bytes )
@@ -64,14 +68,14 @@ def _extract_lengths(data: bytes) -> Tuple[int, int, int]:
6468 return base_length , exponent_length , modulus_length
6569
6670
67- def _compute_modexp_gas_fee (data : bytes ) -> int :
68- base_length , exponent_length , modulus_length = _extract_lengths (data )
71+ def _compute_modexp_gas_fee_eip_198 (data : bytes ) -> int :
72+ base_length , exponent_length , modulus_length = extract_lengths (data )
6973
7074 first_32_exponent_bytes = zpad_right (
7175 data [96 + base_length :96 + base_length + exponent_length ],
7276 to_size = min (exponent_length , 32 ),
7377 )[:32 ]
74- adjusted_exponent_length = _compute_adjusted_exponent_length (
78+ adjusted_exponent_length = compute_adjusted_exponent_length (
7579 exponent_length ,
7680 first_32_exponent_bytes ,
7781 )
@@ -86,7 +90,7 @@ def _compute_modexp_gas_fee(data: bytes) -> int:
8690
8791
8892def _modexp (data : bytes ) -> int :
89- base_length , exponent_length , modulus_length = _extract_lengths (data )
93+ base_length , exponent_length , modulus_length = extract_lengths (data )
9094
9195 if base_length == 0 :
9296 return 0
@@ -121,18 +125,22 @@ def _modexp(data: bytes) -> int:
121125 return result
122126
123127
124- def modexp (computation : BaseComputation ) -> BaseComputation :
128+ @curry
129+ def modexp (
130+ computation : BaseComputation ,
131+ gas_calculator : Callable [[bytes ], int ] = _compute_modexp_gas_fee_eip_198
132+ ) -> BaseComputation :
125133 """
126134 https://github.com/ethereum/EIPs/pull/198
127135 """
128136 data = computation .msg .data_as_bytes
129137
130- gas_fee = _compute_modexp_gas_fee (data )
138+ gas_fee = gas_calculator (data )
131139 computation .consume_gas (gas_fee , reason = 'MODEXP Precompile' )
132140
133141 result = _modexp (data )
134142
135- _ , _ , modulus_length = _extract_lengths (data )
143+ _ , _ , modulus_length = extract_lengths (data )
136144
137145 # Modulo 0 is undefined, return zero
138146 # https://math.stackexchange.com/questions/516251/why-is-n-mod-0-undefined
0 commit comments