Skip to content

Commit

Permalink
Revert last change
Browse files Browse the repository at this point in the history
  • Loading branch information
teschlg committed Nov 28, 2024
1 parent 20e7540 commit 1957f2b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions kryptools/poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ def applyfunc(self, func) -> "Poly":
def _check_type(self, other):
return isinstance(other, int) or (isinstance(other, Number) and isinstance(self.coeff[0], Number)) or type(other) == type(self.coeff[0])

def _guess_zero(self):
def _guess_ring(self):
zero = 0 * self.coeff[0]
one = zero**0
return zero, one
try:
ring = type(zero)
one = ring(1)
except:
ring = None
one = zero**0
return zero, one, ring

def __add__(self, other: "Poly") -> "Poly":
zero = 0 * self.coeff[0]
Expand Down Expand Up @@ -225,9 +230,9 @@ def __mod__(self, other: "Poly") -> "Poly":

def divmod(self, other: "Poly") -> ("Poly", "Poly"):
"Polynom division with remainder."
zero, one = self._guess_zero()
zero, one, ring = self._guess_ring()
if isinstance(other, list):
other = self.__class__(other)
other = self.__class__(other, ring=ring)
elif not isinstance(other, self.__class__):
raise NotImplementedError(f"Cannot divide {self} and {other}.")
if not other:
Expand Down Expand Up @@ -255,9 +260,9 @@ def divmod(self, other: "Poly") -> ("Poly", "Poly"):

def mod(self, other: "Poly") -> None:
"Reduce with respect to a given polynomial."
one = self._guess_zero()[1]
one, ring = self._guess_ring()[1:]
if isinstance(other, list):
other = self.__class__(other)
other = self.__class__(other, ring=ring)
elif not isinstance(other, self.__class__):
raise NotImplementedError(f"Cannot divide {self} and {other}.")
if not other:
Expand All @@ -284,9 +289,9 @@ def inv(self, other: "Poly" = None) -> "Poly":
"Inverse modulo a given polynomial."
if not other:
other = self.modulus
zero, one = self._guess_zero()
zero, one, ring = self._guess_ring()
if isinstance(other, list):
other = self.__class__(other)
other = self.__class__(other, ring=ring)
elif not isinstance(other, self.__class__):
raise NotImplementedError(f"Cannot invert {self} modulo {other}.")
if not other:
Expand Down

0 comments on commit 1957f2b

Please sign in to comment.