Skip to content

Commit

Permalink
Merge pull request #28 from pipermerriam/piper/cleanup-linting-recurs…
Browse files Browse the repository at this point in the history
…ion-and-mutability

Piper/cleanup linting recursion and mutability
  • Loading branch information
pipermerriam authored Dec 4, 2018
2 parents 8c2dba9 + 5edaeb9 commit bacb225
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 124 deletions.
16 changes: 10 additions & 6 deletions py_ecc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import absolute_import
import sys

from . import secp256k1 # noqa: F401
from . import bn128 # noqa: F401
from . import optimized_bn128 # noqa: F401
from . import bls12_381 # noqa: F401
from . import optimized_bls12_381 # noqa: F401

sys.setrecursionlimit(max(10000, sys.getrecursionlimit()))


from py_ecc import secp256k1 # noqa: F401
from py_ecc import bn128 # noqa: F401
from py_ecc import optimized_bn128 # noqa: F401
from py_ecc import bls12_381 # noqa: F401
from py_ecc import optimized_bls12_381 # noqa: F401
18 changes: 11 additions & 7 deletions py_ecc/bls12_381/bls12_381_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@
# Curve is y**2 = x**3 + 4
b = FQ(4)
# Twisted curve over FQ**2
b2 = FQ2([4, 4])
b2 = FQ2((4, 4))
# Extension curve over FQ**12; same b value as over FQ
b12 = FQ12([4] + [0] * 11)
b12 = FQ12((4,) + (0,) * 11)

# Generator for curve over FQ
G1 = (FQ(3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507), FQ(1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569))
G1 = (
FQ(3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507), # noqa: E501
FQ(1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569), # noqa: E501
)
# Generator for twisted curve over FQ2
G2 = (
FQ2([
352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160,
3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758,
352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160, # noqa: E501
3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758, # noqa: E501
]),
FQ2([
1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905,
927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905, # noqa: E501
927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582, # noqa: E501
]),
)
# Point at infinity over FQ
Z1 = None
# Point at infinity for twisted curve over FQ2
Z2 = None


# Check if a point is the point at infinity
def is_inf(pt):
return pt is None
Expand Down
56 changes: 22 additions & 34 deletions py_ecc/bls12_381/bls12_381_field_elements.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
from __future__ import absolute_import

import sys


sys.setrecursionlimit(100000)


# python3 compatibility
if sys.version_info.major == 2:
int_types = (int, long) # noqa: F821
else:
int_types = (int,)


# The prime modulus of the field
field_modulus = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787
field_modulus = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787 # noqa: E501
# See, it's prime!
assert pow(2, field_modulus, field_modulus) == 2

# The modulus of the polynomial in this representation of FQ12
FQ12_modulus_coeffs = [2, 0, 0, 0, 0, 0, -2, 0, 0, 0, 0, 0] # Implied + [1]
FQ12_modulus_coeffs = (2, 0, 0, 0, 0, 0, -2, 0, 0, 0, 0, 0) # Implied + [1]


# Extended euclidean algorithm to find modular inverses for
Expand All @@ -44,7 +32,7 @@ def __init__(self, n):
self.n = n.n
else:
self.n = n % field_modulus
assert isinstance(self.n, int_types)
assert isinstance(self.n, int)

def __add__(self, other):
on = other.n if isinstance(other, FQ) else other
Expand All @@ -70,15 +58,15 @@ def __sub__(self, other):

def __div__(self, other):
on = other.n if isinstance(other, FQ) else other
assert isinstance(on, int_types)
assert isinstance(on, int)
return FQ(self.n * inv(on, field_modulus) % field_modulus)

def __truediv__(self, other):
return self.__div__(other)

def __rdiv__(self, other):
on = other.n if isinstance(other, FQ) else other
assert isinstance(on, int_types), on
assert isinstance(on, int), on
return FQ(inv(self.n, field_modulus) * on % field_modulus)

def __rtruediv__(self, other):
Expand Down Expand Up @@ -135,33 +123,33 @@ def poly_rounded_div(a, b):
o[i] += temp[degb + i] / b[degb]
for c in range(degb + 1):
temp[c + i] -= o[c]
return o[:deg(o) + 1]
return tuple(o[:deg(o) + 1])


int_types_or_FQ = (FQ,) + int_types
int_types_or_FQ = (int, FQ)


# A class for elements in polynomial extension fields
class FQP(object):
def __init__(self, coeffs, modulus_coeffs):
assert len(coeffs) == len(modulus_coeffs)
self.coeffs = [FQ(c) for c in coeffs]
self.coeffs = tuple(FQ(c) for c in coeffs)
# The coefficients of the modulus, without the leading [1]
self.modulus_coeffs = modulus_coeffs
# The degree of the extension field
self.degree = len(self.modulus_coeffs)

def __add__(self, other):
assert isinstance(other, self.__class__)
return self.__class__([x + y for x, y in zip(self.coeffs, other.coeffs)])
return self.__class__(tuple(x + y for x, y in zip(self.coeffs, other.coeffs)))

def __sub__(self, other):
assert isinstance(other, self.__class__)
return self.__class__([x - y for x, y in zip(self.coeffs, other.coeffs)])
return self.__class__(tuple(x - y for x, y in zip(self.coeffs, other.coeffs)))

def __mul__(self, other):
if isinstance(other, int_types_or_FQ):
return self.__class__([c * other for c in self.coeffs])
return self.__class__(tuple(c * other for c in self.coeffs))
else:
assert isinstance(other, self.__class__)
b = [FQ(0) for i in range(self.degree * 2 - 1)]
Expand All @@ -172,14 +160,14 @@ def __mul__(self, other):
exp, top = len(b) - self.degree - 1, b.pop()
for i in range(self.degree):
b[exp + i] -= top * FQ(self.modulus_coeffs[i])
return self.__class__(b)
return self.__class__(tuple(b))

def __rmul__(self, other):
return self * other

def __div__(self, other):
if isinstance(other, int_types_or_FQ):
return self.__class__([c / other for c in self.coeffs])
return self.__class__(tuple(c / other for c in self.coeffs))
else:
assert isinstance(other, self.__class__)
return self * other.inv()
Expand All @@ -189,7 +177,7 @@ def __truediv__(self, other):

def __pow__(self, other):
if other == 0:
return self.__class__([1] + [0] * (self.degree - 1))
return self.__class__((1,) + (0,) * (self.degree - 1))
elif other == 1:
return self.__class__(self.coeffs)
elif other % 2 == 0:
Expand All @@ -200,9 +188,9 @@ def __pow__(self, other):
# Extended euclidean algorithm used to find the modular inverse
def inv(self):
lm, hm = [1] + [0] * self.degree, [0] * (self.degree + 1)
low, high = self.coeffs + [0], self.modulus_coeffs + [1]
low, high = self.coeffs + (0,), self.modulus_coeffs + (1,)
while deg(low):
r = poly_rounded_div(high, low)
r = list(poly_rounded_div(high, low))
r += [0] * (self.degree + 1 - len(r))
nm = [x for x in hm]
new = [x for x in high]
Expand All @@ -214,7 +202,7 @@ def inv(self):
nm[i + j] -= lm[i] * r[j]
new[i + j] -= low[i] * r[j]
lm, low, hm, high = nm, new, lm, low
return self.__class__(lm[:self.degree]) / low[0]
return self.__class__(tuple(lm[:self.degree])) / low[0]

def __repr__(self):
return repr(self.coeffs)
Expand All @@ -234,26 +222,26 @@ def __neg__(self):

@classmethod
def one(cls):
return cls([1] + [0] * (cls.degree - 1))
return cls((1,) + (0,) * (cls.degree - 1))

@classmethod
def zero(cls):
return cls([0] * cls.degree)
return cls((0,) * cls.degree)


# The quadratic extension field
class FQ2(FQP):
def __init__(self, coeffs):
self.coeffs = [FQ(c) for c in coeffs]
self.modulus_coeffs = [1, 0]
self.coeffs = tuple(FQ(c) for c in coeffs)
self.modulus_coeffs = (1, 0)
self.degree = 2
self.__class__.degree = 2


# The 12th-degree extension field
class FQ12(FQP):
def __init__(self, coeffs):
self.coeffs = [FQ(c) for c in coeffs]
self.coeffs = tuple(FQ(c) for c in coeffs)
self.modulus_coeffs = FQ12_modulus_coeffs
self.degree = 12
self.__class__.degree = 12
2 changes: 1 addition & 1 deletion py_ecc/bls12_381/bls12_381_pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def miller_loop(Q, P):
if ate_loop_count & (2**i):
f = f * linefunc(R, Q, P)
R = add(R, Q)
#assert R == multiply(Q, ate_loop_count)
# assert R == multiply(Q, ate_loop_count)
# Q1 = (Q[0] ** field_modulus, Q[1] ** field_modulus)
# assert is_on_curve(Q1, b12)
# nQ2 = (Q1[0] ** field_modulus, -Q1[1] ** field_modulus)
Expand Down
Loading

0 comments on commit bacb225

Please sign in to comment.