Skip to content

Commit 1957f2b

Browse files
committed
Revert last change
1 parent 20e7540 commit 1957f2b

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

kryptools/poly.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,15 @@ def applyfunc(self, func) -> "Poly":
114114
def _check_type(self, other):
115115
return isinstance(other, int) or (isinstance(other, Number) and isinstance(self.coeff[0], Number)) or type(other) == type(self.coeff[0])
116116

117-
def _guess_zero(self):
117+
def _guess_ring(self):
118118
zero = 0 * self.coeff[0]
119-
one = zero**0
120-
return zero, one
119+
try:
120+
ring = type(zero)
121+
one = ring(1)
122+
except:
123+
ring = None
124+
one = zero**0
125+
return zero, one, ring
121126

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

226231
def divmod(self, other: "Poly") -> ("Poly", "Poly"):
227232
"Polynom division with remainder."
228-
zero, one = self._guess_zero()
233+
zero, one, ring = self._guess_ring()
229234
if isinstance(other, list):
230-
other = self.__class__(other)
235+
other = self.__class__(other, ring=ring)
231236
elif not isinstance(other, self.__class__):
232237
raise NotImplementedError(f"Cannot divide {self} and {other}.")
233238
if not other:
@@ -255,9 +260,9 @@ def divmod(self, other: "Poly") -> ("Poly", "Poly"):
255260

256261
def mod(self, other: "Poly") -> None:
257262
"Reduce with respect to a given polynomial."
258-
one = self._guess_zero()[1]
263+
one, ring = self._guess_ring()[1:]
259264
if isinstance(other, list):
260-
other = self.__class__(other)
265+
other = self.__class__(other, ring=ring)
261266
elif not isinstance(other, self.__class__):
262267
raise NotImplementedError(f"Cannot divide {self} and {other}.")
263268
if not other:
@@ -284,9 +289,9 @@ def inv(self, other: "Poly" = None) -> "Poly":
284289
"Inverse modulo a given polynomial."
285290
if not other:
286291
other = self.modulus
287-
zero, one = self._guess_zero()
292+
zero, one, ring = self._guess_ring()
288293
if isinstance(other, list):
289-
other = self.__class__(other)
294+
other = self.__class__(other, ring=ring)
290295
elif not isinstance(other, self.__class__):
291296
raise NotImplementedError(f"Cannot invert {self} modulo {other}.")
292297
if not other:

0 commit comments

Comments
 (0)