diff --git a/src/shamira/gf256.py b/src/shamira/gf256.py --- a/src/shamira/gf256.py +++ b/src/shamira/gf256.py @@ -2,6 +2,9 @@ """Arithmetic operations on Galois Field 2**8. See https://en.wikipedia.org/wiki/Finite_field_arithmetic""" +from functools import reduce +import operator + def _gfmul(a, b): """Basic multiplication. Russian peasant algorithm.""" @@ -47,18 +50,25 @@ def evaluate(coefs, x): return res -def get_constant_coef(*points): +def get_constant_coef(weights, y_coords): """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 - for i in range(k): - (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) + return reduce( + operator.xor, + map(lambda ab: gfmul(*ab), zip(weights, y_coords)) + ) + + +def compute_weights(x_coords): + assert x_coords + + res = [ + reduce( + gfmul, + (gfmul(xj, INV[xj^xi]) for xj in x_coords if xi!=xj), + 1 + ) for xi in x_coords + ] + return res