Browse Source

switch to bytes as it's smaller, raise error on 0 ** -x

main
John-Mark Gurney 1 year ago
parent
commit
52f6b9fc2a
1 changed files with 14 additions and 3 deletions
  1. +14
    -3
      shamirss.py

+ 14
- 3
shamirss.py View File

@@ -126,7 +126,7 @@ class GF2p8:
or x^8 + x^7 + x^2 + x + 1.
'''

_invcache = (None, 1, 195, 130, 162, 126, 65, 90, 81, 54, 63, 172, 227, 104, 45, 42, 235, 155, 27, 53, 220, 30, 86, 165, 178, 116, 52, 18, 213, 100, 21, 221, 182, 75, 142, 251, 206, 233, 217, 161, 110, 219, 15, 44, 43, 14, 145, 241, 89, 215, 58, 244, 26, 19, 9, 80, 169, 99, 50, 245, 201, 204, 173, 10, 91, 6, 230, 247, 71, 191, 190, 68, 103, 123, 183, 33, 175, 83, 147, 255, 55, 8, 174, 77, 196, 209, 22, 164, 214, 48, 7, 64, 139, 157, 187, 140, 239, 129, 168, 57, 29, 212, 122, 72, 13, 226, 202, 176, 199, 222, 40, 218, 151, 210, 242, 132, 25, 179, 185, 135, 167, 228, 102, 73, 149, 153, 5, 163, 238, 97, 3, 194, 115, 243, 184, 119, 224, 248, 156, 92, 95, 186, 34, 250, 240, 46, 254, 78, 152, 124, 211, 112, 148, 125, 234, 17, 138, 93, 188, 236, 216, 39, 4, 127, 87, 23, 229, 120, 98, 56, 171, 170, 11, 62, 82, 76, 107, 203, 24, 117, 192, 253, 32, 74, 134, 118, 141, 94, 158, 237, 70, 69, 180, 252, 131, 2, 84, 208, 223, 108, 205, 60, 106, 177, 61, 200, 36, 232, 197, 85, 113, 150, 101, 28, 88, 49, 160, 38, 111, 41, 20, 31, 109, 198, 136, 249, 105, 12, 121, 166, 66, 246, 207, 37, 154, 16, 159, 189, 128, 96, 144, 47, 114, 133, 51, 59, 231, 67, 137, 225, 143, 35, 193, 181, 146, 79)
_invcache = b'\x00\x01\xc3\x82\xa2~AZQ6?\xac\xe3h-*\xeb\x9b\x1b5\xdc\x1eV\xa5\xb2t4\x12\xd5d\x15\xdd\xb6K\x8e\xfb\xce\xe9\xd9\xa1n\xdb\x0f,+\x0e\x91\xf1Y\xd7:\xf4\x1a\x13\tP\xa9c2\xf5\xc9\xcc\xad\n[\x06\xe6\xf7G\xbf\xbeDg{\xb7!\xafS\x93\xff7\x08\xaeM\xc4\xd1\x16\xa4\xd60\x07@\x8b\x9d\xbb\x8c\xef\x81\xa89\x1d\xd4zH\r\xe2\xca\xb0\xc7\xde(\xda\x97\xd2\xf2\x84\x19\xb3\xb9\x87\xa7\xe4fI\x95\x99\x05\xa3\xeea\x03\xc2s\xf3\xb8w\xe0\xf8\x9c\\_\xba"\xfa\xf0.\xfeN\x98|\xd3p\x94}\xea\x11\x8a]\xbc\xec\xd8\'\x04\x7fW\x17\xe5xb8\xab\xaa\x0b>RLk\xcb\x18u\xc0\xfd J\x86v\x8d^\x9e\xedFE\xb4\xfc\x83\x02T\xd0\xdfl\xcd<j\xb1=\xc8$\xe8\xc5Uq\x96e\x1cX1\xa0&o)\x14\x1fm\xc6\x88\xf9i\x0cy\xa6B\xf6\xcf%\x9a\x10\x9f\xbd\x80`\x90/r\x853;\xe7C\x89\xe1\x8f#\xc1\xb5\x92O'

@staticmethod
def _primativemul(a, b):
@@ -224,6 +224,12 @@ class GF2p8:
return o * (self ** -1)

def __pow__(self, x):
if self._v == 0:
if x < 0:
raise ZeroDivisionError

return self

if x == -1 and self._invcache:
return self.__class__(self._invcache[self._v])

@@ -352,14 +358,19 @@ class TestShamirSS(unittest.TestCase):

self.assertEqual(a * ainv, 1)

invcache = (None, ) + \
tuple(int(GF2p8(x) ** -1) for x in range(1, 256))
invcache = bytes((0, ) + \
tuple(int(GF2p8(x) ** -1) for x in range(1, 256)))

if GF2p8._invcache != invcache: # pragma: no cover
print('inv cache:', repr(invcache))
self.assertEqual(GF2p8._invcache, invcache)

def test_gf2p8_power(self):
zero = GF2p8(0)
self.assertEqual(zero ** 5, zero)
with self.assertRaises(ZeroDivisionError):
zero ** -1

a = GF2p8(random.randint(0, 255))

v = GF2p8(1)


Loading…
Cancel
Save