7
7
8
8
from eth_utils import (
9
9
big_endian_to_int ,
10
+ ValidationError ,
10
11
)
11
12
12
13
from py_ecc .optimized_bls12_381 import ( # NOQA
@@ -102,13 +103,17 @@ def compress_G1(pt: Tuple[FQ, FQ, FQ]) -> int:
102
103
return x .n + 2 ** 383 * (y .n % 2 )
103
104
104
105
105
- def decompress_G1 (p : int ) -> Tuple [FQ , FQ , FQ ]:
106
- if p == 0 :
106
+ def decompress_G1 (pt : int ) -> Tuple [FQ , FQ , FQ ]:
107
+ if pt == 0 :
107
108
return (FQ (1 ), FQ (1 ), FQ (0 ))
108
- x = p % 2 ** 383
109
- y_mod_2 = p // 2 ** 383
109
+ x = pt % 2 ** 383
110
+ y_mod_2 = pt // 2 ** 383
110
111
y = pow ((x ** 3 + b .n ) % q , (q + 1 ) // 4 , q )
111
- assert pow (y , 2 , q ) == (x ** 3 + b .n ) % q
112
+
113
+ if pow (y , 2 , q ) != (x ** 3 + b .n ) % q :
114
+ raise ValueError (
115
+ "he given point is not on G1: y**2 = x**3 + b"
116
+ )
112
117
if y % 2 != y_mod_2 :
113
118
y = q - y
114
119
return (FQ (x ), FQ (y ), FQ (1 ))
@@ -118,7 +123,10 @@ def decompress_G1(p: int) -> Tuple[FQ, FQ, FQ]:
118
123
# G2
119
124
#
120
125
def compress_G2 (pt : Tuple [FQP , FQP , FQP ]) -> Tuple [int , int ]:
121
- assert is_on_curve (pt , b2 )
126
+ if not is_on_curve (pt , b2 ):
127
+ raise ValueError (
128
+ "The given point is not on the twisted curve over FQ**2"
129
+ )
122
130
x , y = normalize (pt )
123
131
return (
124
132
int (x .coeffs [0 ] + 2 ** 383 * (y .coeffs [0 ] % 2 )),
@@ -136,7 +144,10 @@ def decompress_G2(p: bytes) -> Tuple[FQP, FQP, FQP]:
136
144
y = modular_squareroot (x ** 3 + b2 )
137
145
if y .coeffs [0 ] % 2 != y1_mod_2 :
138
146
y = FQ2 ((y * - 1 ).coeffs )
139
- assert is_on_curve ((x , y , FQ2 ([1 , 0 ])), b2 )
147
+ if not is_on_curve ((x , y , FQ2 ([1 , 0 ])), b2 ):
148
+ raise ValueError (
149
+ "The given point is not on the twisted curve over FQ**2"
150
+ )
140
151
return x , y , FQ2 ([1 , 0 ])
141
152
142
153
@@ -189,7 +200,13 @@ def verify_multiple(pubkeys: Sequence[int],
189
200
signature : bytes ,
190
201
domain : int ) -> bool :
191
202
len_msgs = len (messages )
192
- assert len (pubkeys ) == len_msgs
203
+
204
+ if len (pubkeys ) != len_msgs :
205
+ raise ValidationError (
206
+ "len(pubkeys) (%s) should be equal to len(messages) (%s)" % (
207
+ len (pubkeys ), len_msgs
208
+ )
209
+ )
193
210
194
211
o = FQ12 ([1 ] + [0 ] * 11 )
195
212
for m_pubs in set (messages ):
0 commit comments