-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter3.py
372 lines (286 loc) · 10.7 KB
/
Chapter3.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from dumb25519 import Scalar, Point
import dumb25519
import random
q=dumb25519.q
d=dumb25519.d
G=dumb25519.G
l=Scalar(dumb25519.l)
'''
# 3.1 Prove k in kG, kR, and that k is the same for both (without revealing k)
# Non-interactive proof
k=dumb25519.random_scalar()
count = 10 # base key count
J= []
J= [dumb25519.random_point() for i in range(count)]
K= [k * value for value in J]
# 1. Generate random numbers alpha and compute alpha * J
alpha= dumb25519.random_scalar()
alphaJ = [alpha * value for value in J]
# 2. Calculate the challenge
c=dumb25519.hash_to_scalar(J,K,alphaJ)
# 3. Define the response
r=alpha - c * k
# 4. Publish signature
signature=(c,r)
# Verification
# 1. Calculate the challenge
rJcK=[signature[1] * J[i] + signature[0] * K[i] for i in range(count)]
cprime= dumb25519.hash_to_scalar(J,K,rJcK)
# 2. Check c == cprime
print('c = ', c)
print('cprime = ',cprime)
'''
'''
# 3.2
count = 10 # key count
# Non-interactive proof
k=[dumb25519.random_scalar() for i in range(count)]
J= []
J= [dumb25519.random_point() for i in range(count)]
K= [k[i] * J[i] for i in range(count)]
# 1. Generate random numbers alpha and compute alpha * J
alpha= [dumb25519.random_scalar() for i in range(count)]
alphaJ = [alpha[i] * J[i] for i in range(count)]
# 2. Calculate the challenge
c=dumb25519.hash_to_scalar(J,K,alphaJ)
# 3. Define each response
r=[alpha[i] - c * k[i] for i in range(count)]
# 4. Publish the signature
signature = (c,r)
# Verification -- same as before, with individualized responses
# 1. Calculate the challenge
rJcK=[signature[1][i] * J[i] + signature[0] * K[i] for i in range(count)]
cprime= dumb25519.hash_to_scalar(J,K,rJcK)
# 2. Check c == cprime
print('c = ', c)
print('cprime = ',cprime)
'''
'''
# 3.3 SAG
m="Someone among us said this."
count = 10 # key count
pi= count-1 #Set pi as final index to simplify example code. Same effect as a random selection with the elements rearranged.
k_pi=dumb25519.random_scalar()
K_pi= k_pi * G
R= [dumb25519.random_point() for i in range(count)]
R[pi]=K_pi
# Signature
# 1. Generate random number alpha, and fake responses for all but pi
alpha= dumb25519.random_scalar()
alphaG= alpha * G
c=[Scalar(0)]*count
r=[dumb25519.random_scalar() for i in range(count)]
r[pi]=Scalar(0) # exclude i = pi
# 2. Calculate challenge for pi+1
c[0]=dumb25519.hash_to_scalar(R,m,alphaG)
# 3. Starting after pi, calculate challenges
for index in range(pi):
c[index+1]=dumb25519.hash_to_scalar(R,m, r[index] * G + c[index] * R[index])
# 4. Define the real response
r[pi] = alpha - c[pi] * k_pi
#Verification
cprime=[0]*(count+1)
# 1. Compute set of cprime values
cprime[0] = c[0] #start with signature value
for index in range(1,count+1):
cprime[index]=dumb25519.hash_to_scalar(R,m, r[index-1] * G + cprime[index-1] * R[index-1])
# 2. Check c_1 == cprime_1, with cprime_1 being the last term calculated.
if c[0] == cprime[count]:
print('Valid')
'''
'''
# 3.4 bLSAG
m="Someone among us said this."
count = 10 # key count
pi= count-1 #Set pi as final index to simplify example code. Same effect as a random selection with the elements rearranged.
k_pi=dumb25519.random_scalar()
K_pi= k_pi * G
R= [dumb25519.random_point() for i in range(count)]
R[pi]=K_pi
# Signature
# 1. Calculate key image K_tilde
K_tilde = k_pi * dumb25519.hash_to_point(K_pi)
# 2. Generate random number alpha, and fake responses for all but pi
alpha= dumb25519.random_scalar()
alphaG= alpha * G
r=[dumb25519.random_scalar() for i in range(count)]
r[pi]=Scalar(0) # exclude i = pi
c=[0]*count
# 3. Calculate challenge for pi+1
c[0]=dumb25519.hash_to_scalar(m, alphaG, alpha * dumb25519.hash_to_point(K_pi))
# 4. Compute challenge set
for index in range(pi):
c[index+1]=dumb25519.hash_to_scalar(m, r[index] * G + c[index] * R[index], r[index] * dumb25519.hash_to_point(R[index]) + c[index] * K_tilde)
# 5. Define real response
r[pi] = alpha - c[pi] * k_pi
#Verification
cprime=[0]*(count+1)
# 1. Check l * K_tilde returns zero/point-at-infinity element (0,1)
zero_check = l * K_tilde
if zero_check.x == 0:
print('zero check passed')
# 2. Compute cprime set
cprime[0] = c[0] #start with signature value
for index in range(1,count+1):
cprime[index]=dumb25519.hash_to_scalar(m, r[index-1] * G + cprime[index-1] * R[index-1], r[index-1] * dumb25519.hash_to_point(R[index-1]) + c[index-1] * K_tilde)
# 3. Check c_1 == cprime_1, with cprime_1 being the last term calculated.
if c[0] == cprime[count]:
print('Valid')
'''
'''
# 3.5 MLSAG
# 2d set of public keys K_i_j, with knowledge of m private key k_pi_j for index i = pi
m="Someone among us said this."
count = 5 # key count (size of each dimension of R, for a total of count^2 keys)
pi= count-1 #Set pi as final index to simplify example code. Same effect as a random selection with the elements rearranged.
k_pi_j=[dumb25519.random_scalar() for i in range(count)]
K_pi_j= [k_pi_j[i] * G for i in range(count)]
R= [[dumb25519.random_point() for i in range(count)] for j in range(count)]
R[pi]=K_pi_j
# Signature
# 1. Calculate key images K_tilde_j
K_tilde_j= [k_pi_j[j] * dumb25519.hash_to_point(K_pi_j[j]) for j in range(count)]
# 2. Generate random numbers alpha_j and r_i_j except for the pi set of j
alpha_j=[dumb25519.random_scalar() for i in range(count)]
r_i_j=[[dumb25519.random_scalar() for j in range(count)] for i in range(count)]
r_i_j[pi]=[0]*count # clear pi set of j
c=[0]*count
# 3. Compute challenge for pi+1 (note that pi+1 is index 0)
#prepare ordered list of calculated items to hash
ordered=[0]*(2*count)
for index in range(count):
alphaG=alpha_j[index] * G
alphaHash=alpha_j[index] * dumb25519.hash_to_point(K_pi_j[index])
ordered[index*2]=alphaG
ordered[index*2+1]=alphaHash
c[0]=dumb25519.hash_to_scalar(m, ordered)
# 4. Compute challenge set
for number in range(0,pi):
ordered=[0]*(2*count)
for index in range(count):
rGcK=r_i_j[number][index] * G + c[number] * R[number][index]
rHashcK_tilde=r_i_j[number][index] * dumb25519.hash_to_point(R[number][index]) + c[number] * K_tilde_j[index]
ordered[index*2]=rGcK
ordered[index*2+1]=rHashcK_tilde
c[number+1]=dumb25519.hash_to_scalar(m, ordered)
# 5. Define all r_pi_j = alpha_j - c_pi * k_pi_j
for index in range(count):
r_i_j[pi][index]=alpha_j[index] - c[pi] * k_pi_j[index]
# Verification
cprime=[0]*(count+1)
# 1. zero check all l * K_tilde_j
for index in range(count):
zero_check= l * K_tilde_j[index]
if zero_check.x != 0:
print('Zero Check Fail')
break
if index == count-1:
print('Zero Check Passed')
# 2. Compute cprime set
cprime[0] = c[0] #start with signature value
# Exact same calculation performed in Signature step 4, but expanded to calculate signature challenge value twice
for number in range(0,count):
ordered=[0]*(2*count)
for index in range(count):
rGcK=r_i_j[number][index] * G + c[number] * R[number][index]
rHashcK_tilde=r_i_j[number][index] * dumb25519.hash_to_point(R[number][index]) + c[number] * K_tilde_j[index]
ordered[index*2]=rGcK
ordered[index*2+1]=rHashcK_tilde
cprime[number+1]=dumb25519.hash_to_scalar(m, ordered)
# 3. Check c_1 == cprime_1
if c[0] == cprime[count]:
print('Valid')
'''
'''
# 3.6 CLSAG
# 2d set of public keys K_i_j, with knowledge of m private key k_pi_j for index i = pi
m="Someone among us said this."
count = 5 # key count (size of each dimension of R, for a total of count^2 keys)
pi= count-1 #Set pi as final index to simplify example code. Same effect as a random selection with the elements rearranged.
k_pi_j=[dumb25519.random_scalar() for i in range(count)]
K_pi_j= [k_pi_j[i] * G for i in range(count)]
R= [[dumb25519.random_point() for i in range(count)] for j in range(count)]
R[pi]=K_pi_j
# Signature
# 1. Calculate key images K_tilde_j
K_tilde_j= [k_pi_j[j] * dumb25519.hash_to_point(K_pi_j[0]) for j in range(count)]
# 2. Generate random numbers alpha and r_i_j except for the pi set of j
alpha=dumb25519.random_scalar()
r_i=[dumb25519.random_scalar() for j in range(count)]
r_i[pi]=0
c=[0]*count
# 3. Calculate aggregate public keys W_i for all i, and aggregate key image W_tilde
W_i=[0]*count
j_terms=[0]*count
T_j_raw=['CLSAG_']*count
T_j=[f'{elm}{index+1}' for index, elm in enumerate(T_j_raw)]
#T_j=['CLSAG_1', 'CLSAG_2', 'CLSAG_3', 'CLSAG_4', 'CLSAG_5']
T_c=('CLSAG_c')
# Aggregate public keys W_i for all i
for i in range(count):
#get sum of j terms
for j in range(count):
j_terms[j]=dumb25519.hash_to_scalar(T_j[j], R, K_tilde_j) * R[i][j]
if j == 0:
j_sum = j_terms[j]
j_sum = j_sum + j_terms[j]
W_i[i]=j_sum
# Aggregate key image W_tilde
for j in range(count):
j_terms[j]=dumb25519.hash_to_scalar(T_j[j], R, K_tilde_j) * K_tilde_j[j]
if j == 0:
j_sum = j_terms[j]
j_sum = j_sum + j_terms[j]
W_tilde=j_sum
# Aggregate private key w_pi
for j in range(count):
j_terms[j]=dumb25519.hash_to_scalar(T_j[j], R, K_tilde_j) * k_pi_j[j]
if j == 0:
j_sum = j_terms[j]
j_sum = j_sum + j_terms[j]
w_pi=j_sum
# 4. Compute challenge for pi+1 (note that pi+1 is index 0)
c[0]=dumb25519.hash_to_scalar(T_c, R, m, alpha * G, alpha * dumb25519.hash_to_point(K_pi_j[0]))
# 5. Compute challenge set
for i in range(pi):
c[i+1]=dumb25519.hash_to_scalar(T_c, R, m, r_i[i] * G + c[i] * W_i[i], r_i[i] * dumb25519.hash_to_point(R[i][0]) + c[i] * W_tilde)
# 6. Define r_pi
r_i[pi]=alpha - c[pi] * w_pi
# Verification
cprime=[0]*(count+1)
# 1. zero check all l * K_tilde_j
for index in range(count):
zero_check= l * K_tilde_j[index]
if zero_check.x != 0:
print('Zero Check Fail')
break
if index == count-1:
print('Zero Check Passed')
# 2. Calculate aggregate public keys W_i for all i, and aggregate key image W_tilde (same as Signature step 3, without knowledge of private keys)
W_i=[0]*count
j_terms=[0]*count
# Aggregate public keys W_i for all i
for i in range(count):
#get sum of j terms
for j in range(count):
j_terms[j]=dumb25519.hash_to_scalar(T_j[j], R, K_tilde_j) * R[i][j]
if j == 0:
j_sum = j_terms[j]
j_sum = j_sum + j_terms[j]
W_i[i]=j_sum
# Aggregate key image W_tilde
for j in range(count):
j_terms[j]=dumb25519.hash_to_scalar(T_j[j], R, K_tilde_j) * K_tilde_j[j]
if j == 0:
j_sum = j_terms[j]
j_sum = j_sum + j_terms[j]
W_tilde=j_sum
# 3. Compute cprime set
cprime[0] = c[0] #start with signature value
# Exact same calculation performed in Signature step 5, but expanded to calculate signature challenge value twice
for i in range(count):
cprime[i+1]=dumb25519.hash_to_scalar(T_c, R, m, r_i[i] * G + cprime[i] * W_i[i], r_i[i] * dumb25519.hash_to_point(R[i][0]) + cprime[i] * W_tilde)
# 4. Check c_1 == cprime_1
if c[0] == cprime[count]:
print('Valid')
'''