Browse Source

maps have rotted my brain.. drop if and adjust range to cover it..

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

+ 6
- 9
shamirss.py View File

@@ -59,7 +59,7 @@ def create_shares(data, k, nshares):


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


powers = { x: GF2p8(x).powerseries(k - 1) for x in range(1, nshares + 1) }
powers = (None, ) + tuple(GF2p8(x).powerseries(k - 1) for x in range(1, nshares + 1))


coeffs = [ [ x ] + [ random.randint(1, 255) for y in range(k - 1) ] for idx, x in enumerate(data) ] coeffs = [ [ x ] + [ random.randint(1, 255) for y in range(k - 1) ] for idx, x in enumerate(data) ]


@@ -79,7 +79,7 @@ def recover_data(shares, k):
0)) for idx in range(len(shares[0][1]))]) 0)) for idx in range(len(shares[0][1]))])


class GF2p8: class GF2p8:
_invcache = {1: 1, 2: 195, 3: 130, 4: 162, 5: 126, 6: 65, 7: 90, 8: 81, 9: 54, 10: 63, 11: 172, 12: 227, 13: 104, 14: 45, 15: 42, 16: 235, 17: 155, 18: 27, 19: 53, 20: 220, 21: 30, 22: 86, 23: 165, 24: 178, 25: 116, 26: 52, 27: 18, 28: 213, 29: 100, 30: 21, 31: 221, 32: 182, 33: 75, 34: 142, 35: 251, 36: 206, 37: 233, 38: 217, 39: 161, 40: 110, 41: 219, 42: 15, 43: 44, 44: 43, 45: 14, 46: 145, 47: 241, 48: 89, 49: 215, 50: 58, 51: 244, 52: 26, 53: 19, 54: 9, 55: 80, 56: 169, 57: 99, 58: 50, 59: 245, 60: 201, 61: 204, 62: 173, 63: 10, 64: 91, 65: 6, 66: 230, 67: 247, 68: 71, 69: 191, 70: 190, 71: 68, 72: 103, 73: 123, 74: 183, 75: 33, 76: 175, 77: 83, 78: 147, 79: 255, 80: 55, 81: 8, 82: 174, 83: 77, 84: 196, 85: 209, 86: 22, 87: 164, 88: 214, 89: 48, 90: 7, 91: 64, 92: 139, 93: 157, 94: 187, 95: 140, 96: 239, 97: 129, 98: 168, 99: 57, 100: 29, 101: 212, 102: 122, 103: 72, 104: 13, 105: 226, 106: 202, 107: 176, 108: 199, 109: 222, 110: 40, 111: 218, 112: 151, 113: 210, 114: 242, 115: 132, 116: 25, 117: 179, 118: 185, 119: 135, 120: 167, 121: 228, 122: 102, 123: 73, 124: 149, 125: 153, 126: 5, 127: 163, 128: 238, 129: 97, 130: 3, 131: 194, 132: 115, 133: 243, 134: 184, 135: 119, 136: 224, 137: 248, 138: 156, 139: 92, 140: 95, 141: 186, 142: 34, 143: 250, 144: 240, 145: 46, 146: 254, 147: 78, 148: 152, 149: 124, 150: 211, 151: 112, 152: 148, 153: 125, 154: 234, 155: 17, 156: 138, 157: 93, 158: 188, 159: 236, 160: 216, 161: 39, 162: 4, 163: 127, 164: 87, 165: 23, 166: 229, 167: 120, 168: 98, 169: 56, 170: 171, 171: 170, 172: 11, 173: 62, 174: 82, 175: 76, 176: 107, 177: 203, 178: 24, 179: 117, 180: 192, 181: 253, 182: 32, 183: 74, 184: 134, 185: 118, 186: 141, 187: 94, 188: 158, 189: 237, 190: 70, 191: 69, 192: 180, 193: 252, 194: 131, 195: 2, 196: 84, 197: 208, 198: 223, 199: 108, 200: 205, 201: 60, 202: 106, 203: 177, 204: 61, 205: 200, 206: 36, 207: 232, 208: 197, 209: 85, 210: 113, 211: 150, 212: 101, 213: 28, 214: 88, 215: 49, 216: 160, 217: 38, 218: 111, 219: 41, 220: 20, 221: 31, 222: 109, 223: 198, 224: 136, 225: 249, 226: 105, 227: 12, 228: 121, 229: 166, 230: 66, 231: 246, 232: 207, 233: 37, 234: 154, 235: 16, 236: 159, 237: 189, 238: 128, 239: 96, 240: 144, 241: 47, 242: 114, 243: 133, 244: 51, 245: 59, 246: 231, 247: 67, 248: 137, 249: 225, 250: 143, 251: 35, 252: 193, 253: 181, 254: 146, 255: 79}
_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)


@staticmethod @staticmethod
def _primativemul(a, b): def _primativemul(a, b):
@@ -95,7 +95,7 @@ class GF2p8:
return r return r


# polynomial 0x187 # polynomial 0x187
_reduce = { x: _makered(x, 0x87) for x in range(0, 16) }
_reduce = tuple(_makered(x, 0x87) for x in range(0, 16))


def __init__(self, v): def __init__(self, v):
if v >= 256: if v >= 256:
@@ -160,10 +160,7 @@ class GF2p8:


r = [ 1 ] r = [ 1 ]


if cnt > 0:
r.append(self)

for i in range(2, cnt):
for i in range(1, cnt):
r.append(r[-1] * self) r.append(r[-1] * self)


return r return r
@@ -216,12 +213,12 @@ class TestShamirSS(unittest.TestCase):


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


with unittest.mock.patch.object(GF2p8, '_invcache', {}) as pinvc:
with unittest.mock.patch.object(GF2p8, '_invcache', []) as pinvc:
ainv = a ** -1 ainv = a ** -1


self.assertEqual(a * ainv, 1) self.assertEqual(a * ainv, 1)


invcache = { x: int(GF2p8(x) ** -1) for x in range(1, 256) }
invcache = (None, ) + tuple(int(GF2p8(x) ** -1) for x in range(1, 256))


if GF2p8._invcache != invcache: if GF2p8._invcache != invcache:
print('inv cache:', repr(invcache)) print('inv cache:', repr(invcache))


Loading…
Cancel
Save