diff --git a/src/condensed.py b/src/condensed.py --- a/src/condensed.py +++ b/src/condensed.py @@ -7,43 +7,43 @@ """Arithmetic operations on Galois Field 2**8. See https://en.wikipedia.org/wiki/Finite_field_arithmetic""" -def gfmul(a,b): +def gfmul(a, b): """Basic multiplication. Russian peasant algorithm.""" - res=0 + res = 0 while a and b: - if b&1: res^=a - if a&0x80: a=0xff&(a<<1)^0x1b - else: a<<=1 - b>>=1 + if b&1: res ^= a + if a&0x80: a = 0xff&(a<<1)^0x1b + else: a <<= 1 + b >>= 1 return res -g=3 # generator -E=[None]*256 # exponentials -L=[None]*256 # logarithms -acc=1 +g = 3 # generator +E = [None]*256 # exponentials +L = [None]*256 # logarithms +acc = 1 for i in range(256): - E[i]=acc - L[acc]=i - acc=gfmul(acc, g) -L[1]=0 -inv=[E[255-L[i]] if i!=0 else None for i in range(256)] # multiplicative inverse + E[i] = acc + L[acc] = i + acc = gfmul(acc, g) +L[1] = 0 +inv = [E[255-L[i]] if i!=0 else None for i in range(256)] # multiplicative inverse def getConstantCoef(*points): """Compute constant polynomial coefficient given the points. See https://en.wikipedia.org/wiki/Shamir's_Secret_Sharing#Computationally_Efficient_Approach""" - k=len(points) - res=0 + k = len(points) + res = 0 for i in range(k): - (x,y)=points[i] - prod=1 + (x, y) = points[i] + prod = 1 for j in range(k): if i==j: continue - (xj,yj)=points[j] - prod=gfmul(prod, (gfmul(xj,inv[xj^x]))) - res^=gfmul(y,prod) + (xj, yj) = points[j] + prod = gfmul(prod, (gfmul(xj, inv[xj^x]))) + res ^= gfmul(y, prod) return res ### @@ -60,11 +60,11 @@ def reconstructRaw(*shares): :param shares: ((i, (bytes) share), ...) :return: (bytes) reconstructed secret. Too few shares returns garbage.""" - secretLen=len(shares[0][1]) - res=[None]*secretLen + secretLen = len(shares[0][1]) + res = [None]*secretLen for i in range(secretLen): - points=[(x,s[i]) for (x,s) in shares] - res[i]=(getConstantCoef(*points)) + points = [(x, s[i]) for (x, s) in shares] + res[i] = (getConstantCoef(*points)) return bytes(res) @@ -74,7 +74,7 @@ def reconstruct(*shares): :param shares: ((str) share, ...) :return: (str) reconstructed secret. Too few shares returns garbage.""" - bs=reconstructRaw(*(decode(s) for s in shares)) + bs = reconstructRaw(*(decode(s) for s in shares)) try: return bs.decode(encoding="utf-8") except UnicodeDecodeError: @@ -83,14 +83,14 @@ def reconstruct(*shares): def decode(share): try: - (i,_,shareStr)=share.partition(".") - i=int(i) + (i, _, shareStr) = share.partition(".") + i = int(i) if not 1<=i<=255: raise SException("Malformed share: Failed 1<=k<=255, k={0}".format(i)) - shareBytes=base64.b32decode(shareStr) - return (i,shareBytes) - except (ValueError,binascii.Error): + shareBytes = base64.b32decode(shareStr) + return (i, shareBytes) + except (ValueError, binascii.Error): raise SException('Malformed share: share="{0}"'.format(share)) ### @@ -99,16 +99,16 @@ from argparse import ArgumentParser def run(): - parser=ArgumentParser() - subparsers=parser.add_subparsers() + parser = ArgumentParser() + subparsers = parser.add_subparsers() - joiner=subparsers.add_parser("join") - joiner.add_argument("share",nargs="+",help="shares to be joined") + joiner = subparsers.add_parser("join") + joiner.add_argument("share", nargs="+", help="shares to be joined") joiner.set_defaults(func=_reconstruct) parser.set_defaults(func=lambda: parser.error("missing command")) - args=parser.parse_args() + args = parser.parse_args() args.func(args)