Skip to content

Commit

Permalink
keep sympy symbolic
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan committed Oct 31, 2024
1 parent d5f3147 commit 4c5b5e4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions qualtran/bloqs/factoring/ecc/ec_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

from qualtran.symbolics import is_symbolic, SymbolicInt

ec_times_x = sympy.Function("ec_times_x")
ec_times_y = sympy.Function("ec_times_y")
"""Support purely-symbolic ECPoint operations.
https://docs.sympy.org/latest/guides/custom-functions.html#easy-cases-fully-symbolic-or-fully-evaluated
"""


@frozen
class ECPoint:
Expand Down Expand Up @@ -50,9 +56,6 @@ def __add__(self, other):
if (other.mod != self.mod) or (other.curve_a != self.curve_a):
raise ValueError('Use consistent mod and curve')

if is_symbolic(self.x, self.y, other.x, other.y, self.mod, self.curve_a):
x, y, p = sympy.symbols('x y p')
return ECPoint(x=x, y=y, mod=p)
if self == -other:
return ECPoint.inf(mod=self.mod, curve_a=self.curve_a)
if self == ECPoint.inf(mod=self.mod, curve_a=self.curve_a):
Expand All @@ -72,11 +75,21 @@ def __add__(self, other):
yr = (lam * (self.x - xr) - self.y) % self.mod
return ECPoint(xr, yr, mod=self.mod, curve_a=self.curve_a)

def __mul__(self, other):
if other == 0:
def __mul__(self, times):
if times == 0:
return ECPoint.inf(mod=self.mod, curve_a=self.curve_a)
if is_symbolic(self.x, self.y):
# Symbolic case: use sympy.Function to opaquely represent the
# multiplication operation
return ECPoint(
ec_times_x(self.x, times),
ec_times_y(self.y, times),
mod=self.mod,
curve_a=self.curve_a,
)
# Otherwise, multiplication by an integer is repeated addition
x = self
for _ in range(other - 1):
for _ in range(times - 1):
x = x + self

return x
Expand Down

0 comments on commit 4c5b5e4

Please sign in to comment.