Skip to content

Commit 12dfa2f

Browse files
committed
Further replace all the assertions with exceptions
1 parent 10e89d8 commit 12dfa2f

File tree

5 files changed

+219
-59
lines changed

5 files changed

+219
-59
lines changed

py_ecc/bls12_381_curve.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ class BLS12_381_Curve(BaseCurve):
4444
log_ate_loop_count = curve_properties[curve_name]["log_ate_loop_count"]
4545
pseudo_binary_encoding = curve_properties[curve_name]["pseudo_binary_encoding"]
4646

47-
def __init__(self) -> None:
48-
super().__init__()
49-
5047
@staticmethod
5148
def twist(pt: Point2D[FQP]) -> Point2D[FQP]:
5249
if pt is None:
@@ -102,9 +99,6 @@ class Optimized_BLS12_381_Curve(BaseOptimizedCurve):
10299
log_ate_loop_count = optimized_curve_properties[curve_name]["log_ate_loop_count"]
103100
pseudo_binary_encoding = optimized_curve_properties[curve_name]["pseudo_binary_encoding"]
104101

105-
def __init__(self) -> None:
106-
super().__init__()
107-
108102
@staticmethod
109103
def twist(pt: Optimized_Point3D[optimized_FQP]) -> Optimized_Point3D[optimized_FQP]:
110104
if pt is None:

py_ecc/bn128_curve.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ class BN128_Curve(BaseCurve):
4848
log_ate_loop_count = curve_properties[curve_name]["log_ate_loop_count"]
4949
pseudo_binary_encoding = curve_properties[curve_name]["pseudo_binary_encoding"]
5050

51-
def __init__(self) -> None:
52-
super().__init__()
53-
5451
@staticmethod
5552
def twist(pt: Point2D[FQP]) -> Point2D[FQP]:
5653
if pt is None:
@@ -107,9 +104,6 @@ class Optimized_BN128_Curve(BaseOptimizedCurve):
107104
log_ate_loop_count = optimized_curve_properties[curve_name]["log_ate_loop_count"]
108105
pseudo_binary_encoding = optimized_curve_properties[curve_name]["pseudo_binary_encoding"]
109106

110-
def __init__(self) -> None:
111-
super().__init__()
112-
113107
@staticmethod
114108
def twist(pt: Optimized_Point3D[optimized_FQP]) -> Optimized_Point3D[optimized_FQP]:
115109
if pt is None:

py_ecc/field_elements.py

Lines changed: 110 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,38 @@ def __init__(self, val: IntOrFQ, curve_name: str) -> None:
3737

3838
if isinstance(val, FQ):
3939
self.n = val.n
40-
else:
40+
elif isinstance(val, int):
4141
self.n = val % self.field_modulus
42-
assert isinstance(self.n, int)
42+
else:
43+
raise ValueError(
44+
"Expected an int or FQ object, but got object of type {}"
45+
.format(type(val))
46+
)
4347

4448
def __add__(self, other: IntOrFQ) -> "FQ":
45-
on = other.n if isinstance(other, FQ) else other
49+
if isinstance(other, FQ):
50+
on = other.n
51+
elif isinstance(other, int):
52+
on = other
53+
else:
54+
raise ValueError(
55+
"Expected an int or FQ object, but got object of type {}"
56+
.format(type(other))
57+
)
58+
4659
return FQ((self.n + on) % self.field_modulus, self.curve_name)
4760

4861
def __mul__(self, other: IntOrFQ) -> "FQ":
49-
on = other.n if isinstance(other, FQ) else other
62+
if isinstance(other, FQ):
63+
on = other.n
64+
elif isinstance(other, int):
65+
on = other
66+
else:
67+
raise ValueError(
68+
"Expected an int or FQ object, but got object of type {}"
69+
.format(type(other))
70+
)
71+
5072
return FQ((self.n * on) % self.field_modulus, self.curve_name)
5173

5274
def __rmul__(self, other: IntOrFQ) -> "FQ":
@@ -56,16 +78,42 @@ def __radd__(self, other: IntOrFQ) -> "FQ":
5678
return self + other
5779

5880
def __rsub__(self, other: IntOrFQ) -> "FQ":
59-
on = other.n if isinstance(other, FQ) else other
81+
if isinstance(other, FQ):
82+
on = other.n
83+
elif isinstance(other, int):
84+
on = other
85+
else:
86+
raise ValueError(
87+
"Expected an int or FQ object, but got object of type {}"
88+
.format(type(other))
89+
)
90+
6091
return FQ((on - self.n) % self.field_modulus, self.curve_name)
6192

6293
def __sub__(self, other: IntOrFQ) -> "FQ":
63-
on = other.n if isinstance(other, FQ) else other
94+
if isinstance(other, FQ):
95+
on = other.n
96+
elif isinstance(other, int):
97+
on = other
98+
else:
99+
raise ValueError(
100+
"Expected an int or FQ object, but got object of type {}"
101+
.format(type(other))
102+
)
103+
64104
return FQ((self.n - on) % self.field_modulus, self.curve_name)
65105

66106
def __div__(self, other: IntOrFQ) -> "FQ":
67-
on = other.n if isinstance(other, FQ) else other
68-
assert isinstance(on, int)
107+
if isinstance(other, FQ):
108+
on = other.n
109+
elif isinstance(other, int):
110+
on = other
111+
else:
112+
raise ValueError(
113+
"Expected an int or FQ object, but got object of type {}"
114+
.format(type(other))
115+
)
116+
69117
return FQ(
70118
self.n * prime_field_inv(on, self.field_modulus) % self.field_modulus,
71119
self.curve_name
@@ -75,8 +123,16 @@ def __truediv__(self, other: IntOrFQ) -> "FQ":
75123
return self.__div__(other)
76124

77125
def __rdiv__(self, other: IntOrFQ) -> "FQ":
78-
on = other.n if isinstance(other, FQ) else other
79-
assert isinstance(on, int), on
126+
if isinstance(other, FQ):
127+
on = other.n
128+
elif isinstance(other, int):
129+
on = other
130+
else:
131+
raise ValueError(
132+
"Expected an int or FQ object, but got object of type {}"
133+
.format(type(other))
134+
)
135+
80136
return FQ(
81137
prime_field_inv(self.n, self.field_modulus) * on % self.field_modulus,
82138
self.curve_name
@@ -98,8 +154,13 @@ def __pow__(self, other: int) -> "FQ":
98154
def __eq__(self, other: IntOrFQ) -> bool: # type:ignore # https://github.com/python/mypy/issues/2783 # noqa: E501
99155
if isinstance(other, FQ):
100156
return self.n == other.n
101-
else:
157+
elif isinstance(other, int):
102158
return self.n == other
159+
else:
160+
raise ValueError(
161+
"Expected an int or FQ object, but got object of type {}"
162+
.format(type(other))
163+
)
103164

104165
def __ne__(self, other: IntOrFQ) -> bool: # type:ignore # https://github.com/python/mypy/issues/2783 # noqa: E501
105166
return not self == other
@@ -134,7 +195,10 @@ def __init__(self,
134195
coeffs: Sequence[IntOrFQ],
135196
curve_name: str,
136197
modulus_coeffs: Sequence[IntOrFQ]=None) -> None:
137-
assert len(coeffs) == len(modulus_coeffs)
198+
if len(coeffs) != len(modulus_coeffs):
199+
raise Exception(
200+
"coeffs and modulus_coeffs aren't of the same length"
201+
)
138202
self.coeffs = tuple(FQ(c, curve_name) for c in coeffs)
139203
self.curve_name = curve_name
140204
# The coefficients of the modulus, without the leading [1]
@@ -143,18 +207,27 @@ def __init__(self,
143207
self.degree = len(self.modulus_coeffs)
144208

145209
def __add__(self, other: "FQP") -> "FQP":
146-
assert isinstance(other, type(self))
210+
if not isinstance(other, type(self)):
211+
raise ValueError(
212+
"Expected an FQP object, but got object of type {}"
213+
.format(type(other))
214+
)
215+
147216
return type(self)([x + y for x, y in zip(self.coeffs, other.coeffs)], self.curve_name)
148217

149218
def __sub__(self, other: "FQP") -> "FQP":
150-
assert isinstance(other, type(self))
219+
if not isinstance(other, type(self)):
220+
raise ValueError(
221+
"Expected an FQP object, but got object of type {}"
222+
.format(type(other))
223+
)
224+
151225
return type(self)([x - y for x, y in zip(self.coeffs, other.coeffs)], self.curve_name)
152226

153227
def __mul__(self, other: Union[int, "FQ", "FQP"]) -> "FQP":
154228
if isinstance(other, int) or isinstance(other, FQ):
155229
return type(self)([c * other for c in self.coeffs], self.curve_name)
156-
else:
157-
assert isinstance(other, FQP)
230+
elif isinstance(other, FQP):
158231
b = [FQ(0, self.curve_name) for i in range(self.degree * 2 - 1)]
159232
for i in range(self.degree):
160233
for j in range(self.degree):
@@ -164,16 +237,25 @@ def __mul__(self, other: Union[int, "FQ", "FQP"]) -> "FQP":
164237
for i in range(self.degree):
165238
b[exp + i] -= top * FQ(self.modulus_coeffs[i], self.curve_name)
166239
return type(self)(b, self.curve_name)
240+
else:
241+
raise ValueError(
242+
"Expected an int or FQ object or FQP object, but got object of type {}"
243+
.format(type(other))
244+
)
167245

168246
def __rmul__(self, other: Union[int, "FQ", "FQP"]) -> "FQP":
169247
return self * other
170248

171249
def __div__(self, other: Union[int, "FQ", "FQP"]) -> "FQP":
172250
if isinstance(other, int_types_or_FQ):
173251
return type(self)([c / other for c in self.coeffs], self.curve_name)
174-
else:
175-
assert isinstance(other, FQP)
252+
elif isinstance(other, FQP):
176253
return self * other.inv()
254+
else:
255+
raise ValueError(
256+
"Expected an int or FQ object or FQP object, but got object of type {}"
257+
.format(type(other))
258+
)
177259

178260
def __truediv__(self, other: Union[int, "FQ", "FQP"]) -> "FQP":
179261
return self.__div__(other)
@@ -204,9 +286,11 @@ def inv(self) -> "FQP":
204286
r += [0] * (self.degree + 1 - len(r))
205287
nm = [x for x in hm]
206288
new = [x for x in high]
207-
assert len(set(
289+
if len(set(
208290
[len(lm), len(hm), len(low), len(high), len(nm), len(new), self.degree + 1]
209-
)) == 1
291+
)) != 1:
292+
raise Exception("Mismatch between the lengths of lm, hm, low, high, nm, new")
293+
210294
for i in range(self.degree + 1):
211295
for j in range(self.degree + 1 - i):
212296
nm[i + j] -= lm[i] * int(r[j])
@@ -218,7 +302,12 @@ def __repr__(self) -> str:
218302
return repr(self.coeffs)
219303

220304
def __eq__(self, other: "FQP") -> bool: # type: ignore # https://github.com/python/mypy/issues/2783 # noqa: E501
221-
assert isinstance(other, type(self))
305+
if not isinstance(other, type(self)):
306+
raise ValueError(
307+
"Expected an FQP object, but got object of type {}"
308+
.format(type(other))
309+
)
310+
222311
for c1, c2 in zip(self.coeffs, other.coeffs):
223312
if c1 != c2:
224313
return False
@@ -246,7 +335,6 @@ class FQ2(FQP):
246335
def __init__(self, coeffs: Sequence[IntOrFQ], curve_name: str) -> None:
247336
FQ2_MODULUS_COEFFS = field_properties[curve_name]["fq2_modulus_coeffs"]
248337
super().__init__(coeffs, curve_name, FQ2_MODULUS_COEFFS)
249-
assert self.degree == 2
250338

251339

252340
# The 12th-degree extension field
@@ -256,4 +344,3 @@ class FQ12(FQP):
256344
def __init__(self, coeffs: Sequence[IntOrFQ], curve_name: str) -> None:
257345
FQ12_MODULUS_COEFFS = field_properties[curve_name]["fq12_modulus_coeffs"]
258346
super().__init__(coeffs, curve_name, FQ12_MODULUS_COEFFS)
259-
assert self.degree == 12

0 commit comments

Comments
 (0)