2
2
Dict ,
3
3
Iterable ,
4
4
Tuple ,
5
+ Union ,
5
6
)
6
7
7
8
16
17
FQ ,
17
18
FQ2 ,
18
19
FQ12 ,
20
+ FQP ,
19
21
pairing ,
20
22
normalize ,
21
23
field_modulus ,
26
28
final_exponentiate
27
29
)
28
30
from eth .utils .blake import blake
31
+ from eth .utils .bn128 import (
32
+ FQP_point_to_FQ2_point ,
33
+ )
29
34
30
35
31
36
CACHE = {} # type: Dict[bytes, Tuple[FQ2, FQ2, FQ2]]
38
43
assert HEX_ROOT ** 16 == FQ2 ([1 , 0 ])
39
44
40
45
41
- def compress_G1 (pt : Tuple [FQ2 , FQ2 , FQ2 ]) -> int :
46
+ def compress_G1 (pt : Tuple [FQ , FQ , FQ ]) -> int :
42
47
x , y = normalize (pt )
43
48
return x .n + 2 ** 255 * (y .n % 2 )
44
49
@@ -55,11 +60,11 @@ def decompress_G1(p: int) -> Tuple[FQ, FQ, FQ]:
55
60
return (FQ (x ), FQ (y ), FQ (1 ))
56
61
57
62
58
- def sqrt_fq2 (x : FQ2 ) -> FQ2 :
63
+ def sqrt_fq2 (x : FQP ) -> FQ2 :
59
64
y = x ** ((field_modulus ** 2 + 15 ) // 32 )
60
65
while y ** 2 != x :
61
66
y *= HEX_ROOT
62
- return y
67
+ return FQ2 ( y . coeffs )
63
68
64
69
65
70
def hash_to_G2 (m : bytes ) -> Tuple [FQ2 , FQ2 , FQ2 ]:
@@ -79,18 +84,22 @@ def hash_to_G2(m: bytes) -> Tuple[FQ2, FQ2, FQ2]:
79
84
if xcb ** ((field_modulus ** 2 - 1 ) // 2 ) == FQ2 ([1 , 0 ]):
80
85
break
81
86
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 ))
83
89
CACHE [m ] = o
84
90
return o
85
91
86
92
87
- def compress_G2 (pt : Tuple [FQ2 , FQ2 , FQ2 ]) -> Tuple [int , int ]:
93
+ def compress_G2 (pt : Tuple [FQP , FQP , FQP ]) -> Tuple [int , int ]:
88
94
assert is_on_curve (pt , b2 )
89
95
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
+ )
91
100
92
101
93
- def decompress_G2 (p : bytes ) -> Tuple [FQ2 , FQ2 , FQ2 ]:
102
+ def decompress_G2 (p : bytes ) -> Tuple [FQP , FQP , FQP ]:
94
103
x1 = p [0 ] % 2 ** 255
95
104
y1_mod_2 = p [0 ] // 2 ** 255
96
105
x2 = p [1 ]
@@ -99,7 +108,7 @@ def decompress_G2(p: bytes) -> Tuple[FQ2, FQ2, FQ2]:
99
108
return FQ2 ([1 , 0 ]), FQ2 ([1 , 0 ]), FQ2 ([0 , 0 ])
100
109
y = sqrt_fq2 (x ** 3 + b2 )
101
110
if y .coeffs [0 ] % 2 != y1_mod_2 :
102
- y = y * - 1
111
+ y = FQ2 (( y * - 1 ). coeffs )
103
112
assert is_on_curve ((x , y , FQ2 ([1 , 0 ])), b2 )
104
113
return x , y , FQ2 ([1 , 0 ])
105
114
@@ -114,16 +123,16 @@ def privtopub(k: int) -> int:
114
123
115
124
def verify (m : bytes , pub : int , sig : bytes ) -> bool :
116
125
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 )
119
128
)
120
129
return final_exponentiation == FQ12 .one ()
121
130
122
131
123
132
def aggregate_sigs (sigs : Iterable [bytes ]) -> Tuple [int , int ]:
124
133
o = Z2
125
134
for s in sigs :
126
- o = add (o , decompress_G2 (s ))
135
+ o = FQP_point_to_FQ2_point ( add (o , decompress_G2 (s ) ))
127
136
return compress_G2 (o )
128
137
129
138
0 commit comments