-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshamir.py
More file actions
191 lines (135 loc) · 3.84 KB
/
shamir.py
File metadata and controls
191 lines (135 loc) · 3.84 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from random import randint
MIN_SECRET = 1
MAX_SECRET = 100000
PRIME = 104729
class SecretPair():
def __init__(self,number,e):
self.x = int(number)
self.y = int(e)
def getX(self):
return self.x
def getY(self):
return self.y
def generate_random_number(min:int,max:int):
return int(randint(min,max))
def generate_coefficients(k:int,secret:int):
coefficients = []
coefficients.append(secret)
for i in range(1,k):
coefficients.append(generate_random_number(MIN_SECRET,MAX_SECRET))
return coefficients
def calculate_secret_pairs(n:int,coefficients:[]):
shareSecretPoints = []
k = len(coefficients)
for number in range(1,n+1):
accumulator = int(coefficients[0])
for exp in range(1,k):
current = 1
for i in range(1,exp+1):
current = (current * number) % PRIME
current = (current * int(coefficients[exp])) % PRIME
accumulator = (accumulator + current) % PRIME
shareSecretPoints.append(SecretPair(number,accumulator))
return shareSecretPoints
def gcdExtended(a:int,b:int,x,y):
if (b==0):
x = 1
y = 0
return (int(x),int(y))
else:
n = a / b
c = a % b
x1 = y1 = 0
x1,y1 = gcdExtended(b,c,x1,y1)
x = y1
y = x1 - y1 * n
return (int(x),int(y))
def reconstructSecret(secretPairs:[]):
secret = 0
k = len(secretPairs)
for i in range(0,k):
upper = 1
lower = 1
for j in range(0,k):
if (i == j):
continue
xi = secretPairs[i].getX()
xj = secretPairs[j].getX()
upper = (upper * xj * -1) % PRIME
lower = (lower * (xi - xj)) % PRIME
yi = secretPairs[i].getY()
x = y = 0
prime = PRIME
ok = 0
if (lower < 0):
ok = 1
lower *= -1
x,y = gcdExtended(prime, lower, x, y)
if (ok == 1):
y *= -1
lower = (y + PRIME) % PRIME
current = (upper * lower) % PRIME
current = (current * yi) % PRIME
secret = (PRIME + secret + current) % PRIME
return secret
def ekseptejszyn():
print("Niepoprawna wartość!!!")
from os import system
system("start nope.gif")
exit()
def split():
print("Wybierz sekret między: ",MIN_SECRET," , ",MAX_SECRET)
try:
secret = int(input())
except:
ekseptejszyn()
print("")
try:
n = int(input("n = "))
except:
ekseptejszyn()
try:
k = int(input("k = "))
except:
ekseptejszyn()
if (n < k):
print("Błąd!!! n nie może być mniejsze od k")
exit()
print("P(x) = (",end="")
coefficients = generate_coefficients(k,secret)
for i in range(0,k):
if (i == k - 1):
print(coefficients[i],"* x^",k-1,") % ",end="")
else:
print(coefficients[i],"* x^",i," + ",end="")
print(PRIME)
print("Wygenerowane pary (x , f(x)) to :")
shareSecretPoints = calculate_secret_pairs(n,coefficients)
for i in range(0,n):
print("(",shareSecretPoints[i].getX()," , ",shareSecretPoints[i].getY(),")")
def restore():
try:
k = int(input("Wprowadź k = "))
except:
ekseptejszyn()
print("Wprowadź pary: x,y : ")
secrets = []
for i in range(0,k):
xy = input("> ")
try:
xy_list = xy.split(",")
x = int(xy_list[0])
y = int(xy_list[1])
except:
ekseptejszyn()
secrets.append(SecretPair(x,y))
print("Twój sekret = ",reconstructSecret(secrets))
if __name__ == "__main__":
#zakres
MIN_SECRET = 1
MAX_SECRET = 1000000
#modulo
PRIME = 104729
#wywołanie funkcji
split()
restore()