6 Generic pairing types and operations
9 from __future__
import annotations
10 from ..api
import ec_group_messages_pb2
11 from .utils
import hex_to_uint256_list, hex_list_to_uint256_list, \
12 int_and_bytelen_from_hex, int_to_hex
14 from math
import log, floor
15 from typing
import Dict, List, Union, Any
20 G1 Group Points. A typed tuple of strings, stored as a JSON array.
22 def __init__(self, x_coord: str, y_coord: str):
32 def __eq__(self, other: object) -> bool:
33 if not isinstance(other, G1Point):
35 return (self.
x_coord == other.x_coord)
and (self.
y_coord == other.y_coord)
42 return G1Point(json_list[0], json_list[1])
46 point: ec_group_messages_pb2.Group1Point) -> G1Point:
47 x_coord = json.loads(point.x_coord)
48 y_coord = json.loads(point.y_coord)
49 assert isinstance(x_coord, str)
50 assert isinstance(y_coord, str)
51 return G1Point(x_coord, y_coord)
56 g1_proto: ec_group_messages_pb2.Group1Point) ->
None:
57 g1_proto.x_coord = json.dumps(g1.x_coord)
58 g1_proto.y_coord = json.dumps(g1.y_coord)
69 G2 Group Points. Depending on the curve, coordinates may be in the base
70 (non-extension) field (i.e. simple json strings), or an extension field
71 (i.e. a list of strings).
75 x_coord: Union[str, List[str]],
76 y_coord: Union[str, List[str]]):
86 def __eq__(self, other: object) -> bool:
87 if not isinstance(other, G2Point):
89 return (self.
x_coord == other.x_coord)
and (self.
y_coord == other.y_coord)
96 return G2Point(json_list[0], json_list[1])
100 point: ec_group_messages_pb2.Group2Point) -> G2Point:
102 x_coord=json.loads(point.x_coord),
103 y_coord=json.loads(point.y_coord))
108 g2_proto: ec_group_messages_pb2.Group2Point) ->
None:
109 g2_proto.x_coord = json.dumps(g2.x_coord)
110 g2_proto.y_coord = json.dumps(g2.y_coord)
114 if isinstance(g2.x_coord, str):
115 assert isinstance(g2.y_coord, str)
126 The parameters for a specific pairing.
133 generator_g1: G1Point,
134 generator_g2: G2Point):
140 self.scalar_field_capacity: int = floor(log(
int(self.
r, 16), 2))
143 return int(self.
r, 16)
146 return int(self.
q, 16)
160 name=json_dict[
"name"],
163 generator_g1=G1Point.from_json_list(json_dict[
"generator_g1"]),
164 generator_g2=G2Point.from_json_list(json_dict[
"generator_g2"]))
168 pairing_params_proto: ec_group_messages_pb2.PairingParameters
169 ) -> PairingParameters:
171 name=pairing_params_proto.name,
172 r=pairing_params_proto.r,
173 q=pairing_params_proto.q,
180 value =
int(value_hex, 16)
181 value = mod - (value % mod)
187 pairing_parameters: PairingParameters) -> G1Point:
194 pp: PairingParameters) -> G2Point:
195 if isinstance(g2.y_coord, str):