Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def ec_mul(pt, coeff):

# Elliptic curve linear combination. A truly optimized implementation
# would replace this with a fast lin-comb algo, see https://ethresear.ch/t/7238
def ec_lincomb(pairs):
def ec_lincomb(pairs: list[tuple[G1Point, Scalar]]):
return lincomb(
[pt for (pt, _) in pairs],
[int(n) % b.curve_order for (_, n) in pairs],
Expand Down
31 changes: 23 additions & 8 deletions poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ def __add__(self, other):
self.basis,
)
else:
assert self.basis == Basis.LAGRANGE
assert isinstance(other, Scalar)
return Polynomial(
[x + other for x in self.values],
self.basis,
)

def __radd__(self, other):
return self + other

def __sub__(self, other):
if isinstance(other, Polynomial):
assert len(self.values) == len(other.values)
Expand Down Expand Up @@ -69,6 +73,9 @@ def __mul__(self, other):
self.basis,
)

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

def __truediv__(self, other):
if isinstance(other, Polynomial):
assert self.basis == Basis.LAGRANGE
Expand Down Expand Up @@ -134,6 +141,14 @@ def _fft(vals, modulus, roots_of_unity):
def ifft(self):
return self.fft(True)

def pad(self, length):
if self.basis == Basis.LAGRANGE:
p = self.ifft()
else:
p = Polynomial(self.values, Basis.MONOMIAL)
p.values += [Scalar(0)] * (length - len(self.values))
return p.fft()

# Converts a list of evaluations at [1, w, w**2... w**(n-1)] to
# a list of evaluations at
# [offset, offset * q, offset * q**2 ... offset * q**(4n-1)] where q = w**(1/4)
Expand All @@ -144,7 +159,7 @@ def to_coset_extended_lagrange(self, offset):
assert self.basis == Basis.LAGRANGE
group_order = len(self.values)
x_powers = self.ifft().values
x_powers = [(offset**i * x) for i, x in enumerate(x_powers)] + [Scalar(0)] * (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order+oparb=ETH6340 CPCOETST*01699

x_powers = [(offset ** i * x) for i, x in enumerate(x_powers)] + [Scalar(0)] * (
group_order * 3
)
return Polynomial(x_powers, Basis.MONOMIAL).fft()
Expand All @@ -159,8 +174,8 @@ def coset_extended_lagrange_to_coeffs(self, offset):
shifted_coeffs = self.ifft().values
inv_offset = 1 / offset
return Polynomial(
[v * inv_offset**i for (i, v) in enumerate(shifted_coeffs)],
Basis.LAGRANGE,
[v * inv_offset ** i for (i, v) in enumerate(shifted_coeffs)],
Basis.MONOMIAL,
)

# Given a polynomial expressed as a list of evaluations at roots of unity,
Expand All @@ -174,9 +189,9 @@ def barycentric_eval(self, x: Scalar):
(Scalar(x) ** order - 1)
/ order
* sum(
[
value * root / (x - root)
for value, root in zip(self.values, roots_of_unity)
]
)
[
value * root / (x - root)
for value, root in zip(self.values, roots_of_unity)
]
)
)
Loading