Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
pairing.py
Go to the documentation of this file.
1 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 #
3 # SPDX-License-Identifier: LGPL-3.0+
4 
5 """
6 Generic pairing types and operations
7 """
8 
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
13 import json
14 from math import log, floor
15 from typing import Dict, List, Union, Any
16 
17 
18 class G1Point:
19  """
20  G1 Group Points. A typed tuple of strings, stored as a JSON array.
21  """
22  def __init__(self, x_coord: str, y_coord: str):
23  self.x_coord = x_coord
24  self.y_coord = y_coord
25 
26  def __str__(self) -> str:
27  return str(self.to_json_list())
28 
29  def __repr__(self) -> str:
30  return self.__str__()
31 
32  def __eq__(self, other: object) -> bool:
33  if not isinstance(other, G1Point):
34  return False
35  return (self.x_coord == other.x_coord) and (self.y_coord == other.y_coord)
36 
37  def to_json_list(self) -> List[str]:
38  return [self.x_coord, self.y_coord]
39 
40  @staticmethod
41  def from_json_list(json_list: List[str]) -> G1Point:
42  return G1Point(json_list[0], json_list[1])
43 
44 
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)
52 
53 
55  g1: G1Point,
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)
59 
60 
61 def g1_point_to_contract_parameters(g1: G1Point) -> List[int]:
62  return \
63  list(hex_to_uint256_list(g1.x_coord)) + \
64  list(hex_to_uint256_list(g1.y_coord))
65 
66 
67 class G2Point:
68  """
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).
72  """
73  def __init__(
74  self,
75  x_coord: Union[str, List[str]],
76  y_coord: Union[str, List[str]]):
77  self.x_coord = x_coord
78  self.y_coord = y_coord
79 
80  def __str__(self) -> str:
81  return str(self.to_json_list())
82 
83  def __repr__(self) -> str:
84  return self.__str__()
85 
86  def __eq__(self, other: object) -> bool:
87  if not isinstance(other, G2Point):
88  return False
89  return (self.x_coord == other.x_coord) and (self.y_coord == other.y_coord)
90 
91  def to_json_list(self) -> List[Union[str, List[str]]]:
92  return [self.x_coord, self.y_coord]
93 
94  @staticmethod
95  def from_json_list(json_list: List[Union[str, List[str]]]) -> G2Point:
96  return G2Point(json_list[0], json_list[1])
97 
98 
100  point: ec_group_messages_pb2.Group2Point) -> G2Point:
101  return G2Point(
102  x_coord=json.loads(point.x_coord),
103  y_coord=json.loads(point.y_coord))
104 
105 
107  g2: G2Point,
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)
111 
112 
113 def g2_point_to_contract_parameters(g2: G2Point) -> List[int]:
114  if isinstance(g2.x_coord, str):
115  assert isinstance(g2.y_coord, str)
116  return \
117  list(hex_to_uint256_list(g2.x_coord)) + \
118  list(hex_to_uint256_list(g2.y_coord))
119  return \
120  hex_list_to_uint256_list(g2.x_coord) + \
121  hex_list_to_uint256_list(g2.y_coord)
122 
123 
125  """
126  The parameters for a specific pairing.
127  """
128  def __init__(
129  self,
130  name: str,
131  r: str,
132  q: str,
133  generator_g1: G1Point,
134  generator_g2: G2Point):
135  self.name = name
136  self.r = r
137  self.q = q
138  self.generator_g1 = generator_g1
139  self.generator_g2 = generator_g2
140  self.scalar_field_capacity: int = floor(log(int(self.r, 16), 2))
141 
142  def scalar_field_mod(self) -> int:
143  return int(self.r, 16)
144 
145  def base_field_mod(self) -> int:
146  return int(self.q, 16)
147 
148  def to_json_dict(self) -> Dict[str, Any]:
149  return {
150  "name": self.name,
151  "r": self.r,
152  "q": self.q,
153  "generator_g1": self.generator_g1.to_json_list(),
154  "generator_g2": self.generator_g2.to_json_list(),
155  }
156 
157  @staticmethod
158  def from_json_dict(json_dict: Dict[str, Any]) -> PairingParameters:
159  return PairingParameters(
160  name=json_dict["name"],
161  r=json_dict["r"],
162  q=json_dict["q"],
163  generator_g1=G1Point.from_json_list(json_dict["generator_g1"]),
164  generator_g2=G2Point.from_json_list(json_dict["generator_g2"]))
165 
166 
168  pairing_params_proto: ec_group_messages_pb2.PairingParameters
169 ) -> PairingParameters:
170  return PairingParameters(
171  name=pairing_params_proto.name,
172  r=pairing_params_proto.r,
173  q=pairing_params_proto.q,
174  generator_g1=g1_point_from_proto(pairing_params_proto.generator_g1),
175  generator_g2=g2_point_from_proto(pairing_params_proto.generator_g2))
176 
177 
178 def field_element_negate(value_hex: str, mod_hex: str) -> str:
179  mod, num_bytes = int_and_bytelen_from_hex(mod_hex)
180  value = int(value_hex, 16)
181  value = mod - (value % mod)
182  return int_to_hex(value, num_bytes)
183 
184 
186  g1: G1Point,
187  pairing_parameters: PairingParameters) -> G1Point:
188  return G1Point(
189  g1.x_coord, field_element_negate(g1.y_coord, pairing_parameters.q))
190 
191 
193  g2: G2Point,
194  pp: PairingParameters) -> G2Point:
195  if isinstance(g2.y_coord, str):
196  return G2Point(g2.x_coord, field_element_negate(g2.y_coord, pp.q))
197  return G2Point(
198  g2.x_coord, [field_element_negate(y, pp.q) for y in g2.y_coord])
zeth.core.pairing.G1Point
Definition: pairing.py:18
zeth.core.utils.hex_list_to_uint256_list
List[int] hex_list_to_uint256_list(Sequence[Union[str, List[str]]] elements)
Definition: utils.py:189
zeth.core.pairing.G1Point.to_json_list
List[str] to_json_list(self)
Definition: pairing.py:37
zeth.core.pairing.G2Point.__repr__
str __repr__(self)
Definition: pairing.py:83
test_commands.mock.str
str
Definition: mock.py:18
zeth.core.pairing.G1Point.__init__
def __init__(self, str x_coord, str y_coord)
Definition: pairing.py:22
zeth.core.pairing.G1Point.y_coord
y_coord
Definition: pairing.py:24
zeth.core.pairing.G2Point.to_json_list
List[Union[str, List[str]]] to_json_list(self)
Definition: pairing.py:91
zeth.cli.zeth_deploy.int
int
Definition: zeth_deploy.py:27
zeth.core.pairing.g1_point_to_contract_parameters
List[int] g1_point_to_contract_parameters(G1Point g1)
Definition: pairing.py:61
zeth.core.pairing.PairingParameters.base_field_mod
int base_field_mod(self)
Definition: pairing.py:145
zeth.core.pairing.PairingParameters.__init__
def __init__(self, str name, str r, str q, G1Point generator_g1, G2Point generator_g2)
Definition: pairing.py:128
zeth.core.pairing.g2_point_from_proto
G2Point g2_point_from_proto(ec_group_messages_pb2.Group2Point point)
Definition: pairing.py:99
zeth.core.pairing.PairingParameters.r
r
Definition: pairing.py:130
zeth.core.utils.int_to_hex
str int_to_hex(int value, int num_bytes)
Definition: utils.py:147
zeth.core.utils.int_and_bytelen_from_hex
Tuple[int, int] int_and_bytelen_from_hex(str value_hex)
Definition: utils.py:134
zeth.core.pairing.g1_point_negate
G1Point g1_point_negate(G1Point g1, PairingParameters pairing_parameters)
Definition: pairing.py:185
zeth.core.pairing.PairingParameters.from_json_dict
PairingParameters from_json_dict(Dict[str, Any] json_dict)
Definition: pairing.py:158
zeth.core.pairing.G2Point.__init__
def __init__(self, Union[str, List[str]] x_coord, Union[str, List[str]] y_coord)
Definition: pairing.py:73
zeth.core.pairing.G2Point.from_json_list
G2Point from_json_list(List[Union[str, List[str]]] json_list)
Definition: pairing.py:95
zeth.core.pairing.PairingParameters.generator_g1
generator_g1
Definition: pairing.py:132
zeth.core.pairing.field_element_negate
str field_element_negate(str value_hex, str mod_hex)
Definition: pairing.py:178
zeth.core.pairing.G2Point
Definition: pairing.py:67
zeth.core.pairing.PairingParameters.name
name
Definition: pairing.py:129
zeth.core.pairing.PairingParameters.scalar_field_mod
int scalar_field_mod(self)
Definition: pairing.py:142
zeth.core.pairing.PairingParameters.generator_g2
generator_g2
Definition: pairing.py:133
zeth.core.pairing.g2_point_negate
G2Point g2_point_negate(G2Point g2, PairingParameters pp)
Definition: pairing.py:192
zeth.core.pairing.G2Point.__eq__
bool __eq__(self, object other)
Definition: pairing.py:86
zeth.core.pairing.G2Point.y_coord
y_coord
Definition: pairing.py:75
zeth.core.pairing.G1Point.x_coord
x_coord
Definition: pairing.py:23
zeth.core.pairing.g1_point_from_proto
G1Point g1_point_from_proto(ec_group_messages_pb2.Group1Point point)
Definition: pairing.py:45
zeth.core.pairing.G1Point.__str__
str __str__(self)
Definition: pairing.py:26
zeth.core.pairing.G1Point.__repr__
str __repr__(self)
Definition: pairing.py:29
zeth.core.pairing.PairingParameters.to_json_dict
Dict[str, Any] to_json_dict(self)
Definition: pairing.py:148
zeth.core.pairing.PairingParameters
Definition: pairing.py:124
zeth.core.pairing.g2_point_to_proto
None g2_point_to_proto(G2Point g2, ec_group_messages_pb2.Group2Point g2_proto)
Definition: pairing.py:106
zeth.core.pairing.g2_point_to_contract_parameters
List[int] g2_point_to_contract_parameters(G2Point g2)
Definition: pairing.py:113
zeth.core.pairing.PairingParameters.q
q
Definition: pairing.py:131
zeth.core.pairing.G1Point.from_json_list
G1Point from_json_list(List[str] json_list)
Definition: pairing.py:41
zeth.core.pairing.G1Point.__eq__
bool __eq__(self, object other)
Definition: pairing.py:32
zeth.core.pairing.G2Point.x_coord
x_coord
Definition: pairing.py:74
zeth.core.pairing.pairing_parameters_from_proto
PairingParameters pairing_parameters_from_proto(ec_group_messages_pb2.PairingParameters pairing_params_proto)
Definition: pairing.py:167
zeth.core.pairing.g1_point_to_proto
None g1_point_to_proto(G1Point g1, ec_group_messages_pb2.Group1Point g1_proto)
Definition: pairing.py:54
zeth.core.utils.hex_to_uint256_list
Iterable[int] hex_to_uint256_list(str hex_str)
Definition: utils.py:172
zeth.core.pairing.G2Point.__str__
str __str__(self)
Definition: pairing.py:80