Browse Source

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..
main
John-Mark Gurney 1 year ago
parent
commit
12aabd659d
1 changed files with 18 additions and 4 deletions
  1. +18
    -4
      shamirss.py

+ 18
- 4
shamirss.py View File

@@ -33,6 +33,12 @@ import unittest.mock


random = secrets.SystemRandom() random = secrets.SystemRandom()


__all__ = [
'create_shares',
'recover_data',
'GF2p8',
]

def _makered(x, y): def _makered(x, y):
'''Make reduction table entry. '''Make reduction table entry.


@@ -63,10 +69,12 @@ def create_shares(data, k, nshares):


data = bytes(data) data = bytes(data)


#print(repr(data), repr(k), repr(nshares))

powers = (None, ) + tuple(GF2p8(x).powerseries(k - 1) for x in powers = (None, ) + tuple(GF2p8(x).powerseries(k - 1) for x in
range(1, nshares + 1)) 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) ] range(k - 1) ] for idx, x in enumerate(data) ]


return [ (x, bytes([ int(evalpoly(coeffs[idx], return [ (x, bytes([ int(evalpoly(coeffs[idx],
@@ -166,9 +174,9 @@ class GF2p8:
def powerseries(self, cnt): def powerseries(self, cnt):
'''Generate [ self ** 0, self ** 1, ..., 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) r.append(r[-1] * self)


return r return r
@@ -191,6 +199,8 @@ class TestShamirSS(unittest.TestCase):


powers = a.powerseries(5) 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) ] vals = [ GF2p8(random.randint(0, 255)) for x in range(5) ]


r = evalpoly(vals, powers) r = evalpoly(vals, powers)
@@ -205,9 +215,13 @@ class TestShamirSS(unittest.TestCase):
self.assertRaises(TypeError, create_shares, '', 1, 1) self.assertRaises(TypeError, create_shares, '', 1, 1)


val = bytes([ random.randint(0, 255) for x in range(100) ]) 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) a = create_shares(val, 2, 3)


self.assertNotIn(val, set(x[1] for x in a))

# that it has the number of shares # that it has the number of shares
self.assertEqual(len(a), 3) self.assertEqual(len(a), 3)


@@ -248,7 +262,7 @@ class TestShamirSS(unittest.TestCase):
a = GF2p8(random.randint(0, 255)) a = GF2p8(random.randint(0, 255))


powers = a.powerseries(10) powers = a.powerseries(10)
for j in range(10):
for j in range(11):
self.assertEqual(powers[j], a ** j) self.assertEqual(powers[j], a ** j)


def test_gf2p8_errors(self): def test_gf2p8_errors(self):


Loading…
Cancel
Save