-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret_sharing_suite.py
144 lines (104 loc) · 4.56 KB
/
secret_sharing_suite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from random import randint
# https://lorenzogentile404.github.io/posts/2020/06/2020-06-14-queen-treasure/
# Common
def compute_shares(s, n):
print('s_1,..., s_{n-1} <-$- Z_q')
print('s_n = (s - (s_1 + ... + s_{n-1})) % q')
S = [0 for i in range(0,n)]
for i in range(0, n):
if i == n - 1:
S[n-1] = (s - sum(S)) % q # s_n = s - (s_1 + ... + s_{n-1}) % q
else:
S[i] = randint(0,q-1) # s_1,...,s_{n-1}
print('s_' + str(i + 1) + ' = ' + str(S[i]))
return S
def reconstruct(S):
print('\ns_r = (s_1 + ... + s_n) % q')
s_r = sum(S) % q
print('s_r = ' + str(s_r) + ' => ' + int_to_string(s_r,len(msg)))
def compute_GAMMA(P, PI):
GAMMA = [] # Access structure (minimal qualified sets)
[None if p in t else GAMMA.append(t + [p]) for t in PI for p in P if set(t + [p]) not in [set(hatt)for hatt in GAMMA]]
return GAMMA
string_to_int = lambda msg : int.from_bytes(bytes(msg, 'utf-8'), byteorder = 'big')
int_to_string = lambda n,l : n.to_bytes(l, byteorder='big').decode("utf-8")
selection = int(input('Select type of secret sharing:' +
'\n(1) n-out-of-n secret sharing' +
'\n(2) t-out-of-n secret sharing' +
'\n(3) general access structure secret sharing\n'))
################################################################################
if selection == 1:
print('\nn-out-of-n secret sharing\n')
msg = input('Insert secret message: ')
n = int(input('Insert number of parties n: ')) # Number of parties/shares
q = 2**(len(msg)*8)
print('Group Z_q with q = ' + str(q))
# Convert msg to int
s = string_to_int(msg)
print('\ns = ' + str(s) + '\n')
# Parties
P = [i for i in range(1,n+1)]
print('Parties = ', P)
# Compute shares
S = compute_shares(s, n)
# Distribute shares
for i in range(0,len(S)):
print('Send s_' + str(i + 1) + ' to party ' + str(i+1))
# Reconstruct
reconstruct(S)
################################################################################
elif selection == 2:
print('\nt-out-of-n secret sharing\n')
msg = input('Insert secret message: ')
n = int(input('Insert number of parties n: ')) # Number of parties
t = int(input('Insert number of parties required to reconstruct the secret t: ')) # Number of parties required to reconstruct the secret
q = 2**(len(msg)*8)
print('Group Z_q with q = ' + str(q))
# Convert msg to int
s = string_to_int(msg)
print('\ns = ' + str(s) + '\n')
# Parties
P = [i for i in range(1,n+1)]
print('Parties = ', P)
import itertools
def findsubsets(s, n):
return list(itertools.combinations(s, n))
PI = findsubsets(P,t-1) # Secrecy structure PI = {T_1 = {}, ...}
hatT = [list(set(P).difference(t)) for t in PI] # hatT = P \ T_i with T_i \in PI
print('Secrecy structure PI (maximal unqualified sets) = ', str([list(t) for t in PI]))
print('Access structure GAMMA (minimal qualified sets) = ', str(compute_GAMMA(P, [list(t) for t in PI])))
print('hatT = P \ T_i with T_i \in PI = ', str(hatT))
# Compute shares
S = compute_shares(s, len(hatT))
# Distribute shares
for i, hatT in zip(range(0,len(S)), hatT):
print('Send s_' + str(i + 1) + ' to parties in hatT_' + str(i + 1) + ' = ' + str(hatT))
# Reconstruct
reconstruct(S)
################################################################################
elif selection == 3:
print('\ngeneral access structure secret sharing\n')
msg = input('Insert secret message: ')
n = int(input('Insert number of parties n: ')) # Number of parties
q = 2**(len(msg)*8)
print('Group Z_q with q = ' + str(q))
# Convert msg to int
s = string_to_int(msg)
print('\ns = ' + str(s) + '\n')
# Parties
P = [i for i in range(1,n+1)]
print('Parties = ', P)
PI = input('Insert secrecy structure PI (maximal unqualified sets): ')
import json
PI = [tuple(t) for t in json.loads(PI)]
hatT = [list(set(P).difference(t)) for t in PI] # hatT = P \ T_i with T_i \in PI
print('Secrecy structure PI (maximal unqualified sets) = ', str([list(t) for t in PI]))
print('Access structure GAMMA (minimal qualified sets) = ', str(compute_GAMMA(P, [list(t) for t in PI])))
print('hatT = P \ T_i with T_i \in PI = ', str(hatT))
# Compute shares
S = compute_shares(s, len(hatT))
# Distribute shares
for i, hatT in zip(range(0,len(S)), hatT):
print('Send s_' + str(i + 1) + ' to parties in hatT_' + str(i + 1) + ' = ' + str(hatT))
# Reconstruct
reconstruct(S)