From 3d06adce9f55b69252f0730618b176069c8ba837 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 3 Jan 2019 16:55:45 +0000 Subject: [PATCH] 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. --- test/eccref.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/eccref.py b/test/eccref.py index 93f6740b..44743044 100644 --- a/test/eccref.py +++ b/test/eccref.py @@ -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