1
1
# -*- coding: utf8 -*-
2
- import bitcoin
2
+ from py_ecc .secp256k1 import privtopub , ecdsa_raw_recover , N as secp256k1n
3
+ import hashlib
3
4
from rlp .utils import ascii_chr
4
5
5
6
from ethereum import utils , opcodes
@@ -26,7 +27,7 @@ def proc_ecrecover(ext, msg):
26
27
r = msg .data .extract32 (64 )
27
28
s = msg .data .extract32 (96 )
28
29
29
- if r >= bitcoin . N or s >= bitcoin . N or v < 27 or v > 28 :
30
+ if r >= secp256k1n or s >= secp256k1n or v < 27 or v > 28 :
30
31
return 1 , msg .gas - opcodes .GECRECOVER , []
31
32
try :
32
33
pub = utils .ecrecover_to_pub (message_hash , v , r , s )
@@ -44,7 +45,7 @@ def proc_sha256(ext, msg):
44
45
if msg .gas < gas_cost :
45
46
return 0 , 0 , []
46
47
d = msg .data .extract_all ()
47
- o = [safe_ord (x ) for x in bitcoin . bin_sha256 ( d )]
48
+ o = [safe_ord (x ) for x in hashlib . sha256 ( d ). digest ( )]
48
49
return 1 , msg .gas - gas_cost , o
49
50
50
51
@@ -56,7 +57,7 @@ def proc_ripemd160(ext, msg):
56
57
if msg .gas < gas_cost :
57
58
return 0 , 0 , []
58
59
d = msg .data .extract_all ()
59
- o = [0 ] * 12 + [safe_ord (x ) for x in bitcoin . ripemd . RIPEMD160 ( d ).digest ()]
60
+ o = [0 ] * 12 + [safe_ord (x ) for x in hashlib . new ( 'ripemd160' , d ).digest ()]
60
61
return 1 , msg .gas - gas_cost , o
61
62
62
63
@@ -100,13 +101,13 @@ def proc_modexp(ext, msg):
100
101
return 1 , msg .gas - gas_cost , [safe_ord (x ) for x in utils .zpad (utils .int_to_big_endian (o ), modlen )]
101
102
102
103
def validate_point (x , y ):
103
- import py_pairing
104
- FQ = py_pairing .FQ
105
- if x >= py_pairing .field_modulus or y >= py_pairing .field_modulus :
104
+ import py_ecc . optimized_bn128 as bn128
105
+ FQ = bn128 .FQ
106
+ if x >= bn128 .field_modulus or y >= bn128 .field_modulus :
106
107
return False
107
108
if (x , y ) != (0 , 0 ):
108
109
p1 = (FQ (x ), FQ (y ), FQ (1 ))
109
- if not py_pairing .is_on_curve (p1 , py_pairing .b ):
110
+ if not bn128 .is_on_curve (p1 , bn128 .b ):
110
111
return False
111
112
else :
112
113
p1 = (FQ (1 ), FQ (1 ), FQ (0 ))
@@ -115,8 +116,8 @@ def validate_point(x, y):
115
116
def proc_ecadd (ext , msg ):
116
117
if not ext .post_metropolis_hardfork ():
117
118
return 1 , msg .gas , []
118
- import py_pairing
119
- FQ = py_pairing .FQ
119
+ import py_ecc . optimized_bn128 as bn128
120
+ FQ = bn128 .FQ
120
121
print ('ecadd proc' , msg .gas )
121
122
if msg .gas < opcodes .GECADD :
122
123
return 0 , 0 , []
@@ -128,14 +129,14 @@ def proc_ecadd(ext, msg):
128
129
p2 = validate_point (x2 , y2 )
129
130
if p1 is False or p2 is False :
130
131
return 0 , 0 , []
131
- o = py_pairing .normalize (py_pairing .add (p1 , p2 ))
132
+ o = bn128 .normalize (bn128 .add (p1 , p2 ))
132
133
return 1 , msg .gas - opcodes .GECADD , [safe_ord (x ) for x in (encode_int32 (o [0 ].n ) + encode_int32 (o [1 ].n ))]
133
134
134
135
def proc_ecmul (ext , msg ):
135
136
if not ext .post_metropolis_hardfork ():
136
137
return 1 , msg .gas , []
137
- import py_pairing
138
- FQ = py_pairing .FQ
138
+ import py_ecc . optimized_bn128 as bn128
139
+ FQ = bn128 .FQ
139
140
print ('ecmul proc' , msg .gas )
140
141
if msg .gas < opcodes .GECMUL :
141
142
return 0 , 0 , []
@@ -145,24 +146,24 @@ def proc_ecmul(ext, msg):
145
146
p = validate_point (x , y )
146
147
if p is False :
147
148
return 0 , 0 , []
148
- o = py_pairing .normalize (py_pairing .multiply (p , m ))
149
+ o = bn128 .normalize (bn128 .multiply (p , m ))
149
150
return (1 , msg .gas - opcodes .GECMUL ,
150
151
[safe_ord (c ) for c in (encode_int32 (o [0 ].n ) + encode_int32 (o [1 ].n ))])
151
152
152
153
def proc_ecpairing (ext , msg ):
153
154
if not ext .post_metropolis_hardfork ():
154
155
return 1 , msg .gas , []
155
- import py_pairing
156
- FQ = py_pairing .FQ
156
+ import py_ecc . optimized_bn128 as bn128
157
+ FQ = bn128 .FQ
157
158
print ('pairing proc' , msg .gas )
158
159
# Data must be an exact multiple of 192 byte
159
160
if msg .data .size % 192 :
160
161
return 0 , 0 , []
161
162
gascost = opcodes .GPAIRINGBASE + msg .data .size // 192 * opcodes .GPAIRINGPERPOINT
162
163
if msg .gas < gascost :
163
164
return 0 , 0 , []
164
- zero = (py_pairing .FQ2 .one (), py_pairing .FQ2 .one (), py_pairing .FQ2 .zero ())
165
- exponent = py_pairing .FQ12 .one ()
165
+ zero = (bn128 .FQ2 .one (), bn128 .FQ2 .one (), bn128 .FQ2 .zero ())
166
+ exponent = bn128 .FQ12 .one ()
166
167
for i in range (0 , msg .data .size , 192 ):
167
168
x1 = msg .data .extract32 (i )
168
169
y1 = msg .data .extract32 (i + 32 )
@@ -174,20 +175,20 @@ def proc_ecpairing(ext, msg):
174
175
if p1 is False :
175
176
return 0 , 0 , []
176
177
for v in (x2_i , x2_r , y2_i , y2_r ):
177
- if v >= py_pairing .field_modulus :
178
+ if v >= bn128 .field_modulus :
178
179
return 0 , 0 , []
179
- fq2_x = py_pairing .FQ2 ([x2_r , x2_i ])
180
- fq2_y = py_pairing .FQ2 ([y2_r , y2_i ])
181
- if (fq2_x , fq2_y ) != (py_pairing .FQ2 .zero (), py_pairing .FQ2 .zero ()):
182
- p2 = (fq2_x , fq2_y , py_pairing .FQ2 .one ())
183
- if not py_pairing .is_on_curve (p2 , py_pairing .b2 ):
180
+ fq2_x = bn128 .FQ2 ([x2_r , x2_i ])
181
+ fq2_y = bn128 .FQ2 ([y2_r , y2_i ])
182
+ if (fq2_x , fq2_y ) != (bn128 .FQ2 .zero (), bn128 .FQ2 .zero ()):
183
+ p2 = (fq2_x , fq2_y , bn128 .FQ2 .one ())
184
+ if not bn128 .is_on_curve (p2 , bn128 .b2 ):
184
185
return 0 , 0 , []
185
186
else :
186
187
p2 = zero
187
- if py_pairing .multiply (p2 , py_pairing .curve_order )[- 1 ] != py_pairing .FQ2 .zero ():
188
+ if bn128 .multiply (p2 , bn128 .curve_order )[- 1 ] != bn128 .FQ2 .zero ():
188
189
return 0 , 0 , []
189
190
exponent *= py_pairing .pairing (p2 , p1 , final_exponentiate = False )
190
- result = py_pairing .final_exponentiate (exponent ) == py_pairing .FQ12 .one ()
191
+ result = bn128 .final_exponentiate (exponent ) == bn128 .FQ12 .one ()
191
192
return 1 , msg .gas - gascost , [0 ] * 31 + [1 if result else 0 ]
192
193
193
194
specials = {
0 commit comments