Changeset - db65075fe7e0
[Not reviewed]
default
0 2 0
Laman - 7 years ago 2017-09-17 22:36:32

reconstructing the secret
2 files changed with 21 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/gf256.py
Show inline comments
 
@@ -26,12 +26,25 @@ def ffmul(a, b):
 
	t=L[a]+L[b]
 
	if t>255: t-=255
 
	return E[t]
 

	
 

	
 
def evaluate(coefs,x):
 
	res=0
 
	xK=1
 
	for a in coefs:
 
		res^=ffmul(a,xK)
 
		xK=ffmul(xK,x)
 
	return res
 

	
 

	
 
def getConstantCoef(k,*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=ffmul(prod, (ffmul(xj,inv[xj^x])))
 
		res^=ffmul(y,prod)
 
	return res
src/shamira.py
Show inline comments
 
@@ -6,13 +6,20 @@ import gf256
 
def shareByte(secretB,k,n):
 
	assert n<255
 
	coefs=[int(secretB)]+[int(b) for b in os.urandom(k-1)]
 
	points=[gf256.evaluate(coefs,i) for i in range(1,n+1)]
 
	return points
 

	
 

	
 
def generate(secret,k,n):
 
	shares=[shareByte(b,k,n) for b in secret]
 
	return [(i+1, [s[i] for s in shares]) for i in range(n)]
 

	
 

	
 
print(generate(b"key",2,3))
 
def reconstruct(*shares):
 
	k=len(shares)
 
	secretLen=len(shares[0][1])
 
	res=[None]*secretLen
 
	for i in range(secretLen):
 
		bs=[(x,s[i]) for (x,s) in shares]
 
		res[i]=(gf256.getConstantCoef(k,*bs))
 
	return bytes(res)
0 comments (0 inline, 0 general)