eccref.py: add a couple more methods to ModP.

The __truediv__ pair makes the whole program work in Python 3 as well
as 2 (it was _so_ nearly there already!), and __int__ lets you easily
turn a ModP back into an ordinary Python integer representing its
least positive residue.
This commit is contained in:
Simon Tatham 2019-01-03 16:55:45 +00:00
Родитель c3ae739e6d
Коммит 3d06adce9f
1 изменённых файлов: 4 добавлений и 0 удалений

Просмотреть файл

@ -115,6 +115,8 @@ class ModP(object):
b, a = a, b
assert abs(a[0]) == 1
return a[1]*a[0]
def __int__(self):
return self.n
def __add__(self, rhs):
rhs = self.coerce_to(rhs)
return type(self)(self.p, (self.n + rhs.n) % self.p)
@ -141,6 +143,8 @@ class ModP(object):
def __rdiv__(self, rhs):
rhs = self.coerce_to(rhs)
return type(self)(self.p, (rhs.n * self.invert()) % self.p)
def __truediv__(self, rhs): return self.__div__(rhs)
def __rtruediv__(self, rhs): return self.__rdiv__(rhs)
def __pow__(self, exponent):
assert exponent >= 0
n, b_to_n = 1, self