From 12aabd659da9f80f71025b9edde90fcdeff59293 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 29 May 2023 19:15:18 -0700 Subject: [PATCH] major fix, was generating shares for k-1 instead of k... also use 0 as a coeff, the original paper uses 0. for powerseries, my docs were correct, but the implementation wrong... partly because I used zip which works on mismatched lengths.. --- shamirss.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/shamirss.py b/shamirss.py index 71ac70a..1e41872 100644 --- a/shamirss.py +++ b/shamirss.py @@ -33,6 +33,12 @@ import unittest.mock random = secrets.SystemRandom() +__all__ = [ + 'create_shares', + 'recover_data', + 'GF2p8', +] + def _makered(x, y): '''Make reduction table entry. @@ -63,10 +69,12 @@ def create_shares(data, k, nshares): data = bytes(data) + #print(repr(data), repr(k), repr(nshares)) + powers = (None, ) + tuple(GF2p8(x).powerseries(k - 1) for x in range(1, nshares + 1)) - coeffs = [ [ x ] + [ random.randint(1, 255) for y in + coeffs = [ [ x ] + [ random.randint(0, 255) for y in range(k - 1) ] for idx, x in enumerate(data) ] return [ (x, bytes([ int(evalpoly(coeffs[idx], @@ -166,9 +174,9 @@ class GF2p8: def powerseries(self, cnt): '''Generate [ self ** 0, self ** 1, ..., self ** cnt ].''' - r = [ 1 ] + r = [ self.__class__(1) ] - for i in range(1, cnt): + for i in range(1, cnt + 1): r.append(r[-1] * self) return r @@ -191,6 +199,8 @@ class TestShamirSS(unittest.TestCase): powers = a.powerseries(5) + self.assertTrue(all(isinstance(x, GF2p8) for x in powers)) + vals = [ GF2p8(random.randint(0, 255)) for x in range(5) ] r = evalpoly(vals, powers) @@ -205,9 +215,13 @@ class TestShamirSS(unittest.TestCase): self.assertRaises(TypeError, create_shares, '', 1, 1) val = bytes([ random.randint(0, 255) for x in range(100) ]) + #val = b'this is a test of english text.' + #val = b'1234' a = create_shares(val, 2, 3) + self.assertNotIn(val, set(x[1] for x in a)) + # that it has the number of shares self.assertEqual(len(a), 3) @@ -248,7 +262,7 @@ class TestShamirSS(unittest.TestCase): a = GF2p8(random.randint(0, 255)) powers = a.powerseries(10) - for j in range(10): + for j in range(11): self.assertEqual(powers[j], a ** j) def test_gf2p8_errors(self):