-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegate_new.m
415 lines (322 loc) · 11.7 KB
/
delegate_new.m
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
ms := 0; //increase for higher verifiablity - doesn't change delegator gain
degree_bound := 150;
function Tim()
//return Cputime();
return ClockCycles();
end function;
function csidh_private_keygen(n,B) //redefine keygen with parameters
return [Random([-B..B]) : i in [1..n]];
end function;
// ======================== //
// === Split and ShrVec === //
// ======================== //
function Split(s,k)
Cs := RandomSubset({1..#s},k);
ss := []; sp := [];
for i:=1 to #s do
if i in Cs then Append(~ss,s[i]); Append(~sp,0);
else Append(~ss,0); Append(~sp,s[i]);
end if;
end for;
return ss,sp;
end function;
function ShrVec(s,delta,B)
R0:=[]; R1:=[]; RS:=[];
for i:=1 to #s do
if delta gt 0 then //standard case
repeat
r0 := Random(-(delta+1)*B,(delta+1)*B);
r1 := s[i]-r0;
until ( (AbsoluteValue(r0) le delta*B) or (AbsoluteValue(r1) le delta*B) );
b := Random(1);
if (AbsoluteValue(r0) gt delta*B) then
if b eq 0 then r1:=Random(delta*B); end if;
r0 := -r1;
rs := s[i];
elif (AbsoluteValue(r1) gt delta*B) then
if b eq 0 then r0:=Random(delta*B); end if;
r1 := -r0;
rs := s[i];
else rs := 0;
end if;
else // delta = 0 corresponds to delta = infty or to Cl(O) known
repeat // these r0,r1 are not uniform! this case is just representative!
r0 := Random(-B,B);
r1 := s[i]-r0;
until ( (AbsoluteValue(r0) le B) and (AbsoluteValue(r1) le B) );
rs := 0;
end if;
Append(~R0,r0); Append(~R1,r1); Append(~RS,rs);
end for;
return R0,R1,RS;
end function;
function Split_and_Shroud(s,delta,B,k)
if k eq 0 then
SS := [0 : i in [1..#s]];
SP := s;
else SS,SP := Split(s,k);
end if;
R0,R1,RS := ShrVec(SP,delta,B);
return R0,R1,RS,SS;
end function;
// ============================== //
// === Delegation subroutines === //
// ============================== //
function ISO_2HBC(private_key,A,delta,B,k : server := false)
E := GF(p)!A;
// Shrouding step
t:=Tim();
r0,r1,rs,ss := Split_and_Shroud(private_key,delta,B,k);
TD:=Tim()-t;
// First Delegation
t:=Tim();
if server then E := csidh_action_new(r0,E,degree_bound); end if;
Ts1:=Tim()-t;
// Action of rstar
t:=Tim();
E := csidh_action_new(rs,E,degree_bound);
TD+:=Tim()-t;
// Second Delegation
t:=Tim();
if server then E := csidh_action_new(r1,E,degree_bound); end if;
Ts2:=Tim()-t;
// Action of sstar
if k ne 0 then // if k eq 0 --> CIso, else HIso
t:=Tim();
E := csidh_action_new(ss,E,degree_bound);
TD+:=Tim()-t;
end if;
return TD,Ts1,Ts2,E; // cost of delegator, server1, server2 ; codomain
end function;
function ISO_OMTUP(private_key,A,delta,B,k : server := false)
E := GF(p)!A;
n := #private_key;
mr := 3;
// Shrouding step
t:=Tim();
a1,a2,as,ss := Split_and_Shroud(private_key,delta,B,k);
sp := [a1[i]+a2[i]+as[i] : i in [1..n]];
b1,b2,bs,_ := ShrVec(sp,delta,B); // make sure to have the same sp and ss
e := [csidh_private_keygen(n,delta*B) : i in [1..mr]]; //R
c1:=[[]]; c2:=[[]]; d1:=[[]]; d2:=[[]];
for m:=1 to ms do
Append(~c1,[]); Append(~c2,[]); Append(~d1,[]); Append(~d2,[]);
for i:=1 to n do
repeat
_c1 := Random(-delta*B,delta*B);
_c2 := Random(-delta*B,delta*B);
_d1 := Random(-delta*B,delta*B);
_d2 := _c1+_c2-_d1;
until AbsoluteValue(_d2) le delta*B;
Append(~c1[m],_c1); Append(~c2[m],_c2); Append(~d1[m],_d1); Append(~d2[m],_d2); //S
end for;
end for;
TD:=Tim()-t;
//printf "Shrouding step time: %o", TD;
// First Delegation Step
t:=Tim();
if server then
Ea1 := csidh_action_new(a1,A,degree_bound);
Ec1 := [csidh_action_new(c1[i],A,degree_bound) : i in [1..ms]];
Ee1 := [csidh_action_new(e[i],A,degree_bound) : i in [1..mr]];
end if;
Ts1:=Tim()-t;
t:=Tim();
if server then
Eb1 := csidh_action_new(b1,A,degree_bound);
Ed1 := [csidh_action_new(d1[i],A,degree_bound) : i in [1..ms]];
Ee2 := [csidh_action_new(e[i],A,degree_bound) : i in [1..mr]];
end if;
Ts2:=Tim()-t;
if not server then Ea1:=E; Eb1:=E; end if;
//printf "First delegation step: %o", Ts1+Ts2+TD;
// Verification and action of astar,bstar
t:=Tim();
if server and (Ee1 ne Ee2) then print "Curves do not match after round 1"; return 1; end if;
Ea1 := csidh_action_new(as,Ea1,degree_bound);
Eb1 := csidh_action_new(bs,Eb1,degree_bound);
TD+:=Tim()-t;
//printf "Star action step: %o", Ts1+Ts2+TD;
// Second Delegation Step
t:=Tim();
if server then
Es1 := csidh_action_new(a2,Ea1,degree_bound);
Ed2 := [csidh_action_new(d2[i],Ed1[i],degree_bound) : i in [1..ms]];
end if;
Ts1+:=Tim()-t;
t:=Tim();
if server then
Es2 := csidh_action_new(b2,Eb1,degree_bound);
Ec2 := [csidh_action_new(c2[i],Ec1[i],degree_bound) : i in [1..ms]];
end if;
Ts2+:=Tim()-t;
//printf "Second delegation step: %o", Ts1+Ts2+TD;
// Verification
t:=Tim();
if server then
if Ed2 ne Ec2 then print "Curves do not match after round 2"; return 1; end if;
if Es1 ne Es2 then print "Curves do not match after round 2"; return 1; end if;
end if;
TD+:=Tim()-t;
//printf "Verification step: %o", Ts1+Ts2+TD;
if not server then Es1 := Ea1; end if;
// Action of sstar
if k ne 0 then
t:=Tim();
Es1 := csidh_action_new(ss,Es1,degree_bound);
TD+:=Tim()-t;
end if;
//printf "Sstar action step: %o", Ts1+Ts2+TD;
return TD,Ts1,Ts2,Es1;
end function;
// =============================== //
// === Cryptographic Protocols === //
// =============================== //
// a) Local Versions
function local_csidh(n,B)
a := csidh_private_keygen(n,B);
b := csidh_private_keygen(n,B);
t := Tim();
EA := csidh_action_new(a,0,degree_bound);
T := Tim()-t;
EB := csidh_action_new(b,0,degree_bound);
EAB := csidh_action_new(b,EA,degree_bound);
t := Tim();
EBA := csidh_action_new(a,EB,degree_bound);
T+:= Tim()-t;
if EBA ne EAB then print "Joint key does not match."; end if;
return T;
end function;
function local_ID(n,B) // binary ID protocol (underlying Seasign)
a := csidh_private_keygen(n,B);
t := Tim();
EA := csidh_action_new(a,0,degree_bound);
T := Tim()-t;
return T;
end function;
// b) Delegated Versions
function Delegated_CSIDH(n,B,delta,k,assumption : server := false)
a := csidh_private_keygen(n,B); //delegator (alice)
b := csidh_private_keygen(n,B); //bob
EB := csidh_action_new(b,0,degree_bound); // Bob
if assumption eq "2HBC" then
T1,Ts1_1,Ts2_1,EA := ISO_2HBC(a,0,delta,B,0 : server:=server);
T2,Ts1_2,Ts2_2,EBA := ISO_2HBC(a,EB,delta,B,k: server:=server);
elif assumption eq "OMTUP" then
T1,Ts1_1,Ts2_1,EA := ISO_OMTUP(a,0,delta,B,0 : server:=server);
T2,Ts1_2,Ts2_2,EBA := ISO_OMTUP(a,EB,delta,B,k: server:=server);
else print "WRONG ASSUMPTION INDICATOR";
end if;
if server then
EAB := csidh_action_new(b,EA,degree_bound); // Bob
if EBA ne EAB then print "Joint key does not match."; end if;
end if;
return T1+T2,Ts1_1+Ts1_2,Ts2_1+Ts2_2;
end function;
function Delegated_ID(n,B,delta,assumption : server:=false)
a := csidh_private_keygen(n,B);
t := Tim();
EA := csidh_action_new(a,0,degree_bound);
T := Tim()-t;
if assumption eq "2HBC" then
T1,Ts1,Ts2,EA := ISO_2HBC(a,0,delta,B,0 : server:=server);
elif assumption eq "OMTUP" then
T1,Ts1,Ts2,EA := ISO_OMTUP(a,0,delta,B,0 : server:=server);
else print "WRONG ASSUMPTION INDICATOR";
end if;
return T1,Ts1,Ts2,EA;
end function;
// c) benchmarks
procedure benchmark_CSIDH(delta,N : server:=false) // N = number of repetitions
server := false; //do not perform the server actions
//(this makes the benchmarks much quicker but returns incorrect results
// for correct results, set server := true; )
// LOCAL
T_local := 0;
for i:=1 to N do T_local +:= local_csidh(n,B); end for;
T_local := T_local/N;
printf " local cost : %o (%.5o)\n", T_local, T_local /T_local/1.0/1.0;
// HBC
T_2hbc:=0; Ts1_2hbc:=0; Ts2_2hbc:=0;
for i:=1 to N do
t_2hbc, ts1_2hbc, ts2_2hbc := Delegated_CSIDH(n,B,delta,k,"2HBC" : server:=server);
T_2hbc+:=t_2hbc; Ts1_2hbc+:=ts1_2hbc; Ts2_2hbc+:=ts2_2hbc;
end for;
T_2hbc:=T_2hbc/N/1.0; Ts1_2hbc:=Ts1_2hbc/N/1.0; Ts2_2hbc:=Ts2_2hbc/N/1.0;
printf " 2HBC : %.1o (%.5o)\n", T_2hbc, T_2hbc /T_local/1.0;
if server then
printf " 2HBC (server) : %.1o (%.5o)\n", Ts1_2hbc, Ts1_2hbc/T_local/1.0;
end if;
//printf " 2HBC (server) : %.1o (%.5o)\n", Ts2_2hbc, Ts2_2hbc/T_local/1.0;
// OMTUP
T_omtup:=0; Ts1_omtup:=0; Ts2_omtup:=0;
for i:=1 to N do
t_omtup, ts1_omtup, ts2_omtup := Delegated_CSIDH(n,B,delta,k,"OMTUP" : server:=server);
T_omtup+:=t_omtup; Ts1_omtup+:=ts1_omtup; Ts2_omtup+:=ts2_omtup;
end for;
T_omtup:=T_omtup/N/1.0; Ts1_omtup:=Ts1_omtup/N/1.0; Ts2_omtup:=Ts2_omtup/N/1.0;
printf " OMTUP : %.1o (%.5o)\n", T_omtup, T_omtup /T_local/1.0;
if server then
printf " OMTUP (server) : %.1o (%.5o)\n", Ts1_omtup,Ts1_omtup/T_local/1.0;
end if;
//printf " OMTUP (server) : %.1o (%.5o)\n", Ts2_omtup,Ts2_omtup/T_local/1.0;
end procedure;
procedure benchmark_ID(delta,N : server:=false) // N = number of repetitions
//server := false; //do not perform the server actions
//(this makes the benchmarks much quicker but returns incorrect results
// for correct results, set server := true; )
// LOCAL
T_local := 0;
for i:=1 to N do T_local +:= local_ID(n,B); end for;
T_local := T_local/N;
printf " local cost : %o (%.5o)\n", T_local, T_local /T_local/1.0/1.0;
// HBC
T_2hbc:=0; Ts1_2hbc:=0; Ts2_2hbc:=0;
for i:=1 to N do
t_2hbc, ts1_2hbc, ts2_2hbc := Delegated_ID(n,B,delta,"2HBC" : server:=server);
T_2hbc+:=t_2hbc; Ts1_2hbc+:=ts1_2hbc; Ts2_2hbc+:=ts2_2hbc;
end for;
T_2hbc:=T_2hbc/N/1.0; Ts1_2hbc:=Ts1_2hbc/N/1.0; Ts2_2hbc:=Ts2_2hbc/N/1.0;
printf " 2HBC : %.1o (%.5o)\n", T_2hbc, T_2hbc /T_local/1.0;
if server then
printf " 2HBC (server) : %.1o (%.5o)\n", Ts1_2hbc, Ts1_2hbc/T_local/1.0;
end if;
//printf " 2HBC (server) : %.1o (%.5o)\n", Ts2_2hbc, Ts2_2hbc/T_local/1.0;
// OMTUP
T_omtup:=0; Ts1_omtup:=0; Ts2_omtup:=0;
for i:=1 to N do
t_omtup, ts1_omtup, ts2_omtup := Delegated_ID(n,B,delta,"OMTUP" : server:=server);
T_omtup+:=t_omtup; Ts1_omtup+:=ts1_omtup; Ts2_omtup+:=ts2_omtup;
end for;
T_omtup:=T_omtup/N/1.0; Ts1_omtup:=Ts1_omtup/N/1.0; Ts2_omtup:=Ts2_omtup/N/1.0;
printf " OMTUP : %.1o (%.5o)\n", T_omtup, T_omtup /T_local/1.0;
if server then
printf " OMTUP (server) : %.1o (%.5o)\n", Ts1_omtup,Ts1_omtup/T_local/1.0;
end if;
//printf " OMTUP (server) : %.1o (%.5o)\n", Ts2_omtup,Ts2_omtup/T_local/1.0;
end procedure;
procedure benchmark_ShrVec(delta,B,n,N)
T:=0;
for i:=1 to N do
s := csidh_private_keygen(n,B);
t := Tim();
_,_,_,_ := ShrVec(s,delta,B,0);
T +:= Tim()-t;
end for;
print T/N/1.0;
end procedure;
// Verification procedures
function Exp(s)
E := 0; n:=#s/1.0;
for i:=1 to #s do E+:=AbsoluteValue(s[i])/n; end for;
return E;
end function;
function Verify_Exp(delta,B,n,N)
Er0 := 0.0; Er1 := 0.0; Ers := 0.0;
for i:=1 to N do
s := csidh_private_keygen(n,B);
r0,r1,rs,_ := ShrVec(s,delta,B,0);
Er0 +:= Exp(r0)/N; Er1 +:= Exp(r1)/N; Ers +:= Exp(rs)/N;
end for;
return Er0,Er1,Ers;
end function;