22 Dict ,
33 Iterable ,
44 Tuple ,
5+ Union ,
56)
67
78
1617 FQ ,
1718 FQ2 ,
1819 FQ12 ,
20+ FQP ,
1921 pairing ,
2022 normalize ,
2123 field_modulus ,
2628 final_exponentiate
2729)
2830from eth .utils .blake import blake
31+ from eth .utils .bn128 import (
32+ FQP_point_to_FQ2_point ,
33+ )
2934
3035
3136CACHE = {} # type: Dict[bytes, Tuple[FQ2, FQ2, FQ2]]
3843assert HEX_ROOT ** 16 == FQ2 ([1 , 0 ])
3944
4045
41- def compress_G1 (pt : Tuple [FQ2 , FQ2 , FQ2 ]) -> int :
46+ def compress_G1 (pt : Tuple [FQ , FQ , FQ ]) -> int :
4247 x , y = normalize (pt )
4348 return x .n + 2 ** 255 * (y .n % 2 )
4449
@@ -55,11 +60,11 @@ def decompress_G1(p: int) -> Tuple[FQ, FQ, FQ]:
5560 return (FQ (x ), FQ (y ), FQ (1 ))
5661
5762
58- def sqrt_fq2 (x : FQ2 ) -> FQ2 :
63+ def sqrt_fq2 (x : FQP ) -> FQ2 :
5964 y = x ** ((field_modulus ** 2 + 15 ) // 32 )
6065 while y ** 2 != x :
6166 y *= HEX_ROOT
62- return y
67+ return FQ2 ( y . coeffs )
6368
6469
6570def hash_to_G2 (m : bytes ) -> Tuple [FQ2 , FQ2 , FQ2 ]:
@@ -79,18 +84,22 @@ def hash_to_G2(m: bytes) -> Tuple[FQ2, FQ2, FQ2]:
7984 if xcb ** ((field_modulus ** 2 - 1 ) // 2 ) == FQ2 ([1 , 0 ]):
8085 break
8186 y = sqrt_fq2 (xcb )
82- o = multiply ((x , y , FQ2 ([1 , 0 ])), 2 * field_modulus - curve_order )
87+
88+ o = FQP_point_to_FQ2_point (multiply ((x , y , FQ2 ([1 , 0 ])), 2 * field_modulus - curve_order ))
8389 CACHE [m ] = o
8490 return o
8591
8692
87- def compress_G2 (pt : Tuple [FQ2 , FQ2 , FQ2 ]) -> Tuple [int , int ]:
93+ def compress_G2 (pt : Tuple [FQP , FQP , FQP ]) -> Tuple [int , int ]:
8894 assert is_on_curve (pt , b2 )
8995 x , y = normalize (pt )
90- return (x .coeffs [0 ] + 2 ** 255 * (y .coeffs [0 ] % 2 ), x .coeffs [1 ])
96+ return (
97+ int (x .coeffs [0 ] + 2 ** 255 * (y .coeffs [0 ] % 2 )),
98+ int (x .coeffs [1 ])
99+ )
91100
92101
93- def decompress_G2 (p : bytes ) -> Tuple [FQ2 , FQ2 , FQ2 ]:
102+ def decompress_G2 (p : bytes ) -> Tuple [FQP , FQP , FQP ]:
94103 x1 = p [0 ] % 2 ** 255
95104 y1_mod_2 = p [0 ] // 2 ** 255
96105 x2 = p [1 ]
@@ -99,7 +108,7 @@ def decompress_G2(p: bytes) -> Tuple[FQ2, FQ2, FQ2]:
99108 return FQ2 ([1 , 0 ]), FQ2 ([1 , 0 ]), FQ2 ([0 , 0 ])
100109 y = sqrt_fq2 (x ** 3 + b2 )
101110 if y .coeffs [0 ] % 2 != y1_mod_2 :
102- y = y * - 1
111+ y = FQ2 (( y * - 1 ). coeffs )
103112 assert is_on_curve ((x , y , FQ2 ([1 , 0 ])), b2 )
104113 return x , y , FQ2 ([1 , 0 ])
105114
@@ -114,16 +123,16 @@ def privtopub(k: int) -> int:
114123
115124def verify (m : bytes , pub : int , sig : bytes ) -> bool :
116125 final_exponentiation = final_exponentiate (
117- pairing (decompress_G2 (sig ), G1 , False ) *
118- pairing (hash_to_G2 (m ), neg (decompress_G1 (pub )), False )
126+ pairing (FQP_point_to_FQ2_point ( decompress_G2 (sig ) ), G1 , False ) *
127+ pairing (FQP_point_to_FQ2_point ( hash_to_G2 (m ) ), neg (decompress_G1 (pub )), False )
119128 )
120129 return final_exponentiation == FQ12 .one ()
121130
122131
123132def aggregate_sigs (sigs : Iterable [bytes ]) -> Tuple [int , int ]:
124133 o = Z2
125134 for s in sigs :
126- o = add (o , decompress_G2 (s ))
135+ o = FQP_point_to_FQ2_point ( add (o , decompress_G2 (s ) ))
127136 return compress_G2 (o )
128137
129138
0 commit comments