Browse Source

implement / (division), use self.__class__ more..

main
John-Mark Gurney 1 year ago
parent
commit
1d3c778147
1 changed files with 22 additions and 6 deletions
  1. +22
    -6
      shamirss.py

+ 22
- 6
shamirss.py View File

@@ -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)')


Loading…
Cancel
Save