77
88from eth_utils import (
99 big_endian_to_int ,
10+ ValidationError ,
1011)
1112
1213from py_ecc .optimized_bls12_381 import ( # NOQA
@@ -102,13 +103,17 @@ def compress_G1(pt: Tuple[FQ, FQ, FQ]) -> int:
102103 return x .n + 2 ** 383 * (y .n % 2 )
103104
104105
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 :
107108 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
110111 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+ )
112117 if y % 2 != y_mod_2 :
113118 y = q - y
114119 return (FQ (x ), FQ (y ), FQ (1 ))
@@ -118,7 +123,10 @@ def decompress_G1(p: int) -> Tuple[FQ, FQ, FQ]:
118123# G2
119124#
120125def 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+ )
122130 x , y = normalize (pt )
123131 return (
124132 int (x .coeffs [0 ] + 2 ** 383 * (y .coeffs [0 ] % 2 )),
@@ -136,7 +144,10 @@ def decompress_G2(p: bytes) -> Tuple[FQP, FQP, FQP]:
136144 y = modular_squareroot (x ** 3 + b2 )
137145 if y .coeffs [0 ] % 2 != y1_mod_2 :
138146 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+ )
140151 return x , y , FQ2 ([1 , 0 ])
141152
142153
@@ -189,7 +200,13 @@ def verify_multiple(pubkeys: Sequence[int],
189200 signature : bytes ,
190201 domain : int ) -> bool :
191202 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+ )
193210
194211 o = FQ12 ([1 ] + [0 ] * 11 )
195212 for m_pubs in set (messages ):
0 commit comments