From 1d3c778147884cc5760e8339a4615713401cf2fb Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 31 May 2023 23:40:44 -0700 Subject: [PATCH] implement / (division), use self.__class__ more.. --- shamirss.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/shamirss.py b/shamirss.py index 72ed64e..7d9f43f 100644 --- a/shamirss.py +++ b/shamirss.py @@ -164,8 +164,8 @@ class GF2p8: # basic operations def __add__(self, o): - if isinstance(o, int): - return self + self.__class__(o) + if not isinstance(o, self.__class__): + o = self.__class__(o) return self.__class__(self._v ^ o._v) @@ -180,7 +180,7 @@ class GF2p8: def __mul__(self, o): - if isinstance(o, int): + if not isinstance(o, self.__class__): o = self.__class__(o) m = o._v @@ -199,9 +199,21 @@ class GF2p8: def __rmul__(self, o): return self.__mul__(o) + def __truediv__(self, o): + if not isinstance(o, self.__class__): + o = self.__class__(o) + + return self * (o ** -1) + + def __rtruediv__(self, o): + if not isinstance(o, self.__class__): + o = self.__class__(o) + + return o * (self ** -1) + def __pow__(self, x): if x == -1 and self._invcache: - return GF2p8(self._invcache[self._v]) + return self.__class__(self._invcache[self._v]) if x < 0: x += 255 @@ -225,8 +237,8 @@ class GF2p8: return r def __eq__(self, o): - if isinstance(o, int): - return self._v == o + if not isinstance(o, self.__class__): + o = self.__class__(o) return self._v == o._v @@ -330,6 +342,10 @@ class TestShamirSS(unittest.TestCase): self.assertRaises(ValueError, GF2p8, 40.5) self.assertRaises(ValueError, GF2p8, -1) + def test_gf2p8_div(self): + self.assertEqual(GF2p8(10) / 11, GF2p8(11) ** -1 * GF2p8(10)) + self.assertEqual(10 / GF2p8(11), GF2p8(11) ** -1 * GF2p8(10)) + def test_gf2p8(self): self.assertEqual(int(GF2p8(5)), 5) self.assertEqual(repr(GF2p8(5)), 'GF2p8(5)')