Skip to content

Commit 88c7256

Browse files
Bhargavasomucburgdorf
authored andcommitted
Enable complete type hinting for eth.precompiles
1 parent 1dabf46 commit 88c7256

File tree

8 files changed

+64
-22
lines changed

8 files changed

+64
-22
lines changed

eth/precompiles/ecadd.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Tuple
2+
13
from py_ecc import (
24
optimized_bn128 as bn128,
35
)
@@ -21,8 +23,12 @@
2123
pad32r,
2224
)
2325

26+
from eth.vm.computation import (
27+
BaseComputation,
28+
)
29+
2430

25-
def ecadd(computation):
31+
def ecadd(computation: BaseComputation) -> BaseComputation:
2632
computation.consume_gas(constants.GAS_ECADD, reason='ECADD Precompile')
2733

2834
try:
@@ -39,7 +45,7 @@ def ecadd(computation):
3945
return computation
4046

4147

42-
def _ecadd(data):
48+
def _ecadd(data: bytes) -> Tuple[bn128.FQ, bn128.FQ]:
4349
x1_bytes = pad32r(data[:32])
4450
y1_bytes = pad32r(data[32:64])
4551
x2_bytes = pad32r(data[64:96])

eth/precompiles/ecmul.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Tuple
2+
13
from py_ecc import (
24
optimized_bn128 as bn128,
35
)
@@ -21,8 +23,12 @@
2123
pad32r,
2224
)
2325

26+
from eth.vm.computation import (
27+
BaseComputation,
28+
)
29+
2430

25-
def ecmul(computation):
31+
def ecmul(computation: BaseComputation) -> BaseComputation:
2632
computation.consume_gas(constants.GAS_ECMUL, reason='ECMUL Precompile')
2733

2834
try:
@@ -39,7 +45,7 @@ def ecmul(computation):
3945
return computation
4046

4147

42-
def _ecmull(data):
48+
def _ecmull(data: bytes) -> Tuple[bn128.FQ, bn128.FQ]:
4349
x_bytes = pad32r(data[:32])
4450
y_bytes = pad32r(data[32:64])
4551
m_bytes = pad32r(data[64:96])

eth/precompiles/ecpairing.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Tuple
2+
13
from cytoolz import (
24
curry,
35
pipe,
@@ -16,19 +18,24 @@
1618
from eth.exceptions import (
1719
VMError,
1820
)
21+
1922
from eth.utils.bn128 import (
2023
validate_point,
2124
)
2225
from eth.utils.padding import (
2326
pad32,
2427
)
2528

29+
from eth.vm.computation import (
30+
BaseComputation,
31+
)
32+
2633

2734
ZERO = (bn128.FQ2.one(), bn128.FQ2.one(), bn128.FQ2.zero())
2835
EXPONENT = bn128.FQ12.one()
2936

3037

31-
def ecpairing(computation):
38+
def ecpairing(computation: BaseComputation) -> BaseComputation:
3239
if len(computation.msg.data) % 192:
3340
# data length must be an exact multiple of 192
3441
raise VMError("Invalid ECPAIRING parameters")
@@ -52,7 +59,7 @@ def ecpairing(computation):
5259
return computation
5360

5461

55-
def _ecpairing(data):
62+
def _ecpairing(data: bytes) -> bool:
5663
exponent = bn128.FQ12.one()
5764

5865
processing_pipeline = (
@@ -67,7 +74,7 @@ def _ecpairing(data):
6774

6875

6976
@curry
70-
def _process_point(data_buffer, exponent):
77+
def _process_point(data_buffer: bytes, exponent: int) -> int:
7178
x1, y1, x2_i, x2_r, y2_i, y2_r = _extract_point(data_buffer)
7279
p1 = validate_point(x1, y1)
7380

@@ -91,7 +98,7 @@ def _process_point(data_buffer, exponent):
9198
return exponent * bn128.pairing(p2, p1, final_exponentiate=False)
9299

93100

94-
def _extract_point(data_slice):
101+
def _extract_point(data_slice: bytes) -> Tuple[int, int, int, int, int, int]:
95102
x1_bytes = data_slice[:32]
96103
y1_bytes = data_slice[32:64]
97104
x2_i_bytes = data_slice[64:96]

eth/precompiles/ecrecover.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010

1111
from eth import constants
1212

13+
from eth.utils.padding import (
14+
pad32,
15+
pad32r,
16+
)
17+
1318
from eth.validation import (
1419
validate_lt_secpk1n,
1520
validate_gte,
1621
validate_lte,
1722
)
18-
19-
from eth.utils.padding import (
20-
pad32,
21-
pad32r,
23+
from eth.vm.computation import (
24+
BaseComputation,
2225
)
2326

2427

25-
def ecrecover(computation):
28+
def ecrecover(computation: BaseComputation) -> BaseComputation:
2629
computation.consume_gas(constants.GAS_ECRECOVER, reason="ECRecover Precompile")
2730
raw_message_hash = computation.msg.data[:32]
2831
message_hash = pad32r(raw_message_hash)

eth/precompiles/identity.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
ceil32,
44
)
55

6+
from eth.vm.computation import (
7+
BaseComputation,
8+
)
9+
610

7-
def identity(computation):
11+
def identity(computation: BaseComputation) -> BaseComputation:
812
word_count = ceil32(len(computation.msg.data)) // 32
913
gas_fee = constants.GAS_IDENTITY + word_count * constants.GAS_IDENTITYWORD
1014

eth/precompiles/modexp.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import (
2+
Tuple,
3+
)
4+
15
from eth_utils import (
26
big_endian_to_int,
37
int_to_big_endian,
@@ -14,8 +18,13 @@
1418
zpad_left,
1519
)
1620

21+
from eth.vm.computation import (
22+
BaseComputation,
23+
)
24+
1725

18-
def _compute_adjusted_exponent_length(exponent_length, first_32_exponent_bytes):
26+
def _compute_adjusted_exponent_length(exponent_length: int,
27+
first_32_exponent_bytes: bytes) -> int:
1928
exponent = big_endian_to_int(first_32_exponent_bytes)
2029

2130
if exponent_length <= 32 and exponent == 0:
@@ -30,7 +39,7 @@ def _compute_adjusted_exponent_length(exponent_length, first_32_exponent_bytes):
3039
)
3140

3241

33-
def _compute_complexity(length):
42+
def _compute_complexity(length: int) -> int:
3443
if length <= 64:
3544
return length ** 2
3645
elif length <= 1024:
@@ -41,7 +50,7 @@ def _compute_complexity(length):
4150
return length ** 2 // 16 + 480 * length - 199680
4251

4352

44-
def _extract_lengths(data):
53+
def _extract_lengths(data: bytes) -> Tuple[int, int, int]:
4554
# extract argument lengths
4655
base_length_bytes = pad32r(data[:32])
4756
base_length = big_endian_to_int(base_length_bytes)
@@ -55,7 +64,7 @@ def _extract_lengths(data):
5564
return base_length, exponent_length, modulus_length
5665

5766

58-
def _compute_modexp_gas_fee(data):
67+
def _compute_modexp_gas_fee(data: bytes) -> int:
5968
base_length, exponent_length, modulus_length = _extract_lengths(data)
6069

6170
first_32_exponent_bytes = zpad_right(
@@ -76,7 +85,7 @@ def _compute_modexp_gas_fee(data):
7685
return gas_fee
7786

7887

79-
def _modexp(data):
88+
def _modexp(data: bytes) -> int:
8089
base_length, exponent_length, modulus_length = _extract_lengths(data)
8190

8291
if base_length == 0:
@@ -112,7 +121,7 @@ def _modexp(data):
112121
return result
113122

114123

115-
def modexp(computation):
124+
def modexp(computation: BaseComputation) -> BaseComputation:
116125
"""
117126
https://github.com/ethereum/EIPs/pull/198
118127
"""

eth/precompiles/ripemd160.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
from eth.utils.padding import (
99
pad32,
1010
)
11+
from eth.vm.computation import (
12+
BaseComputation,
13+
)
1114

1215

13-
def ripemd160(computation):
16+
def ripemd160(computation: BaseComputation) -> BaseComputation:
1417
word_count = ceil32(len(computation.msg.data)) // 32
1518
gas_fee = constants.GAS_RIPEMD160 + word_count * constants.GAS_RIPEMD160WORD
1619

eth/precompiles/sha256.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
ceil32,
77
)
88

9+
from eth.vm.computation import (
10+
BaseComputation,
11+
)
12+
913

10-
def sha256(computation):
14+
def sha256(computation: BaseComputation) -> BaseComputation:
1115
word_count = ceil32(len(computation.msg.data)) // 32
1216
gas_fee = constants.GAS_SHA256 + word_count * constants.GAS_SHA256WORD
1317

0 commit comments

Comments
 (0)