-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewCommutes.ec
426 lines (362 loc) · 14.3 KB
/
RewCommutes.ec
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
415
416
417
418
419
420
421
422
423
424
425
426
pragma Goals:printall.
require import AllCore.
require import Distr.
require import AllCore List Binomial.
require import Ring StdRing StdOrder StdBigop Discrete RealSeq RealSeries RealLub.
(*---*) import IterOp Bigint Bigreal Bigreal.BRA.
(*---*) import IntOrder RealOrder RField.
require import Finite.
require (*--*) FinType.
require import RandomFacts.
require ReflectionComp.
require RewCommutesSimple.
theory GenRewCommutes.
type at1, at2, rt1, rt2.
type sbits, irt, rrt,iat.
op pair_sbits : sbits * sbits -> sbits.
op unpair: sbits -> sbits * sbits.
axiom ips: injective pair_sbits.
axiom unpair_pair x : unpair (pair_sbits x) = x.
clone import RewBasics as RW with type sbits <- sbits,
type rrt <- rrt,
type irt <- irt,
type iat <- iat,
op pair_sbits <- pair_sbits,
op unpair <- unpair.
module type GenRewEx1Ex2 = {
proc getState() : sbits
proc setState(b : sbits) : unit (* EasyCrypt removed support for "proc *" *)
proc ex1(x1 : at1 * irt) : rt1
proc ex2(x2 : at2 * irt) : rt2
}.
module ModComm(A : GenRewEx1Ex2, B : Initializer) = {
proc ex2(i12 : at1 * at2,r0 : irt) = {
var r1, r2,s;
s <@ A.getState();
r1 <@ A.ex1(i12.`1,r0);
A.setState(s);
r2 <@ A.ex2(i12.`2,r0);
return (r1,r2);
}
proc ex1(i0 : iat) = {
var r0;
r0 <@ B.init(i0);
return r0;
}
}.
module ModComm'(A : GenRewEx1Ex2, B : Initializer) = {
proc ex2(i12 : at1 * at2,r0 : irt) = {
var r1, r2,s;
s <@ A.getState();
r2 <@ A.ex2(i12.`2,r0);
A.setState(s);
r1 <@ A.ex1(i12.`1,r0);
return (r1,r2);
}
proc ex1(i0 : iat) = {
var r0;
r0 <@ B.init(i0);
return r0;
}
}.
section.
declare module A <: GenRewEx1Ex2.
declare module B <: Initializer.
declare axiom Bsens : equiv[ B.init ~ B.init : ={arg, glob A, glob B} ==> ={glob A, glob B,res} ].
declare axiom RewProp :
exists (f : glob A -> sbits),
injective f /\
(forall &m, Pr[ A.getState() @ &m : (glob A) = ((glob A){m})
/\ res = f ((glob A){m} ) ] = 1%r) /\
(forall &m b (x: glob A), b = f x =>
Pr[A.setState(b) @ &m : glob A = x] = 1%r) /\
islossless A.setState.
clone import ReflectionComp.ReflComp as RC with type at1 <- iat,
type at2 <- at1 * at2,
type rt1 <- irt,
type rt2 <- rt1 * rt2.
clone import RewCommutesSimple.RewCommNI as RCNI with type at1 <- at1 * irt,
type at2 <- at2 * irt,
type rt1 <- rt1,
type rt2 <- rt2,
type sbits <- sbits,
type iat <- iat,
type irt <- irt,
op pair_sbits <- pair_sbits,
op unpair <- unpair.
local lemma reflectionComp1 :
exists (D : iat -> glob ModComm(A,B) -> (irt * glob ModComm(A,B)) distr),
exists (Q : at1 * at2 -> irt * glob ModComm(A,B) -> ((rt1 * rt2) * glob ModComm(A,B)) distr),
(forall M i1 &m, mu (D i1 (glob ModComm(A,B)){m}) M
= Pr[ ModComm(A,B).ex1(i1) @ &m : M (res, glob ModComm(A,B))]) /\
(forall M i2 r1 &m, mu (Q i2 (r1, glob ModComm(A,B)){m}) M
= Pr[ ModComm(A,B).ex2(i2, r1) @ &m : M (res, glob ModComm(A,B))]) /\
forall &m M i1 i2,
Pr[ RCR(ModComm(A,B)).main(i1,i2)
@ &m : M (res.`1, ((res.`2.`1, res.`2.`2), glob ModComm(A,B))) ]
= mu (dlet (D i1 (glob ModComm(A,B)){m}) (fun a => dmap (Q i2 a) (fun x => (a.`1, x)))) M.
proof.
elim (reflectionComp_dmap (ModComm(A,B))).
progress.
exists D.
exists Q.
progress. rewrite (H0 (fun r => M )). auto. rewrite - H1.
rewrite Pr[mu_eq]. smt(). auto.
qed.
local lemma reflectionComp2 :
exists (D : iat -> glob ModComm'(A,B) -> (irt * glob ModComm'(A,B)) distr),
exists (Q : at1 * at2 -> irt * glob ModComm'(A,B)
-> ((rt1 * rt2) * glob ModComm'(A,B)) distr),
(forall M i1 &m, mu (D i1 (glob ModComm'(A,B)){m}) M
= Pr[ ModComm'(A,B).ex1(i1) @ &m : M (res, glob ModComm'(A,B))]) /\
(forall M i2 r1 &m, mu (Q i2 (r1, glob ModComm'(A,B)){m}) M
= Pr[ ModComm'(A,B).ex2(i2, r1) @ &m : M (res, glob ModComm'(A,B))]) /\
forall &m M i1 i2, Pr[ RCR(ModComm'(A,B)).main(i1,i2)
@ &m : M (res.`1 , ((res.`2.`1, res.`2.`2), glob ModComm'(A,B))) ]
= mu (dlet (D i1 (glob ModComm'(A,B)){m}) (fun a => dmap (Q i2 a) (fun x => (a.`1, x)))) M.
proof.
elim (reflectionComp_dmap (ModComm'(A,B))).
progress.
exists D. exists Q.
progress. rewrite (H0 (fun r => M )). auto. rewrite - H1. rewrite Pr[mu_eq]. smt(). auto.
qed.
lemma reflectionComb :
forall &m M i1 i2, Pr[ RCR(ModComm'(A,B)).main(i1,i2) @ &m : M res ]
= Pr[ RCR(ModComm(A,B)).main(i1,i2) @ &m : M res ].
proof. move => &m M.
pose M' := (fun (x : irt * ((rt1 * rt2) * ((glob B) * (glob A)))) => M (x.`1, (x.`2.`1))).
elim reflectionComp1.
elim reflectionComp2.
progress.
have e1 : Pr[RCR(ModComm'(A, B)).main(i1, i2) @ &m : M res] =
Pr[RCR(ModComm'(A, B)).main(i1, i2) @ &m : M (res.`1, (res.`2.`1, res.`2.`2))].
rewrite Pr[mu_eq]. smt(). auto.
rewrite e1.
have e2 : Pr[RCR(ModComm(A, B)).main(i1, i2) @ &m : M res] =
Pr[RCR(ModComm(A, B)).main(i1, i2) @ &m : M (res.`1, (res.`2.`1, res.`2.`2))].
rewrite Pr[mu_eq]. smt(). auto.
rewrite e2.
rewrite (H1 &m M').
rewrite (H4 &m M' ).
rewrite dlet_mu_main.
rewrite dlet_mu_main.
have : (fun (a : irt * ((glob B) * (glob A))) =>
mu1 (D i1 ((glob B){m}, (glob A){m})) a *
mu
((fun (a0 : irt * ((glob B) * (glob A))) =>
dmap (Q i2 a0)
(fun (x : (rt1 * rt2) * ((glob B) * (glob A))) => (a0.`1, x)))
a) M')
= (fun (a : irt * ((glob B) * (glob A))) =>
mu1 (D0 i1 ((glob B){m}, (glob A){m})) a *
mu
((fun (a0 : irt * ((glob B) * (glob A))) =>
dmap (Q0 i2 a0)
(fun (x : (rt1 * rt2) * ((glob B) * (glob A))) => (a0.`1, x)))
a) M').
apply fun_ext. move => a.
simplify. rewrite H.
rewrite H2.
simplify pred1.
have jj : Pr[ModComm'(A,B).ex1(i1) @ &m : (res, ((glob B), (glob A))) = a]
= Pr[ModComm'(A,B).ex1(i1) @ &m : (res, ((glob B), (glob A))) = a /\ exists &n, (glob B, glob A){n} = a.`2].
rewrite Pr[mu_eq]. smt().
auto.
have yy : Pr[ModComm(A,B).ex1(i1) @ &m : (res, ((glob B), (glob A))) = a]
= Pr[ModComm(A,B).ex1(i1) @ &m : (res, ((glob B), (glob A))) = a /\ exists &n, (glob B, glob A){n} = a.`2].
rewrite Pr[mu_eq]. smt().
auto.
case (! exists &n, (glob B, glob A){n} = a.`2).
simplify.
rewrite jj.
rewrite yy.
move => ff.
have eq : Pr[ModComm'(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = a /\
exists &n, ((glob B){n}, (glob A){n}) = a.`2] = 0%r.
have eq1 : Pr[ModComm'(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = a /\
exists &n, ((glob B){n}, (glob A){n}) = a.`2] = Pr[ModComm'(A,B).ex1(i1) @ &m : false ].
rewrite Pr[mu_eq]. smt(). auto.
rewrite eq1. rewrite Pr[mu_false]. auto. rewrite eq. simplify. clear eq.
have eq : Pr[ModComm(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = a /\
exists &n, ((glob B){n}, (glob A){n}) = a.`2] = 0%r.
have eq1 : Pr[ModComm(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = a /\
exists &n, ((glob B){n}, (glob A){n}) = a.`2] = Pr[ModComm(A,B).ex1(i1) @ &m : false ].
rewrite Pr[mu_eq]. smt(). auto.
rewrite eq1. rewrite Pr[mu_false]. auto. rewrite eq. simplify. clear eq.
auto. simplify. move => zzz.
elim zzz. move => &n np .
have aa : a = (a.`1, a.`2). smt().
rewrite aa.
rewrite - np.
rewrite - dmeq. rewrite - dmeq.
rewrite (H0 (fun (x : (rt1 * rt2) * ((glob B) * (glob A))) =>
M' ((a.`1, ((glob B){n}, (glob A){n})).`1, x)) i2 a.`1).
rewrite (H3 (fun (x : (rt1 * rt2) * ((glob B) * (glob A))) =>
M' ((a.`1, ((glob B){n}, (glob A){n})).`1, x)) i2 a.`1).
have myeq1 : Pr[ModComm'(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = (a.`1, ((glob B){n}, (glob A){n}))]
= Pr[ModComm(A,B).ex1(i1) @ &m :
(res, ((glob B), (glob A))) = (a.`1, ((glob B){n}, (glob A){n}))].
byequiv (_: (={arg, glob A, glob B}) ==> ={glob A, glob B,res}).
proc. call Bsens. skip. progress. auto. smt().
rewrite myeq1.
simplify.
have q : Pr[ModComm'(A,B).ex2(i2, a.`1) @ &n : M' (a.`1, (res, ((glob B), (glob A))))]
= Pr[CommNoInit(A).ex2ex1((i2.`1 , a.`1), (i2.`2 , a.`1) ) @ &n : M (a.`1, res)].
have trv : Pr[ModComm'(A,B).ex2(i2, a.`1) @ &n : M' (a.`1, (res, ((glob B), (glob A))))]
= Pr[ModComm'(A,B).ex2(i2, a.`1) @ &n : M (a.`1, res)].
rewrite Pr[mu_eq]. auto. auto.
rewrite trv. clear trv.
byequiv. proc. call (_:true). call (_:true). call (_:true). call(_:true).
skip. progress. smt(). auto.
rewrite q. clear q.
have q : Pr[ModComm(A,B).ex2(i2, a.`1) @ &n : M' (a.`1, (res, ((glob B), (glob A))))]
= Pr[CommNoInit(A).ex1ex2((i2.`1, a.`1), (i2.`2 , a.`1)) @ &n : M (a.`1, res)].
have trv : Pr[ModComm(A,B).ex2(i2, a.`1) @ &n : M' (a.`1, (res, ((glob B), (glob A))))]
= Pr[ModComm(A,B).ex2(i2, a.`1) @ &n : M (a.`1, res) ].
rewrite Pr[mu_eq]. auto. auto.
rewrite trv. clear trv.
byequiv. proc. call (_:true). call (_:true). call (_:true). call(_:true).
skip. progress. smt(). auto.
rewrite q. clear q.
have q : Pr[CommNoInit(A).ex1ex2((i2.`1, a.`1), (i2.`2, a.`1) ) @ &n : M (a.`1, res)]
= Pr[CommNoInit(A).ex2ex1((i2.`1, a.`1), (i2.`2, a.`1)) @ &n : M (a.`1, res)].
have kk : i2 = (i2.`1, i2.`2). smt().
rewrite kk.
apply (rew_comm_law_simple A RewProp &n
(fun (x : rt1 * rt2) => M (a.`1, x))
((i2.`1, i2.`2).`1, a.`1)
((i2.`1, i2.`2).`2, a.`1)).
rewrite q. auto.
move => qqq. rewrite qqq. auto.
qed.
end section.
end GenRewCommutes.
(* once the Bsens bug will be fixed we could remove that and finish GenRewCommutes with i1 i2 as for-all-quantified arguments for ex1 and ex2; right now, if ex1 and ex2 need forall-quantified arguments they must be included into i0:iat and tunneled through B.init *)
theory SpecRewCommutes.
type rt1, rt2.
type sbits, irt, rrt,iat.
op pair_sbits : sbits * sbits -> sbits.
op unpair: sbits -> sbits * sbits.
axiom ips: injective pair_sbits.
axiom unpair_pair x : unpair (pair_sbits x) = x.
clone import RewBasics as RW with type sbits <- sbits,
type rrt <- rrt,
type irt <- irt,
type iat <- iat,
op pair_sbits <- pair_sbits,
op unpair <- unpair
proof*.
realize ips. apply ips. qed.
realize unpair_pair. apply unpair_pair. qed.
clone import GenRewCommutes as GRC with type at1 <- unit,
type at2 <- unit,
type rt1 <- rt1,
type rt2 <- rt2,
type iat <- iat,
type irt <- irt,
type sbits <- sbits,
op pair_sbits <- pair_sbits,
op unpair <- unpair.
module type SpecRewEx1Ex2 = {
proc getState() : sbits
proc setState(b : sbits) : unit (* EasyCrypt removed support for "proc *" *)
proc ex1(x1 : irt) : rt1
proc ex2(x2 : irt) : rt2
}.
module SpecRewComm(A : SpecRewEx1Ex2, B : Initializer) = {
proc iex1ex2(i0 : iat) = {
var r0, r1, r2, s;
r0 <@ B.init(i0);
s <@ A.getState();
r1 <@ A.ex1(r0);
A.setState(s);
r2 <@ A.ex2(r0);
return (r0, (r1, r2));
}
proc iex2ex1(i0 : iat) = {
var r0, r1, r2, s;
r0 <@ B.init(i0);
s <@ A.getState();
r2 <@ A.ex2(r0);
A.setState(s);
r1 <@ A.ex1(r0);
return (r0, (r1, r2));
}
}.
section.
declare module A <: SpecRewEx1Ex2.
declare module B <: Initializer.
local module WA = {
proc getState() : sbits = {
var r;
r <@ A.getState();
return r;
}
proc setState(b : sbits) : unit = {
A.setState(b);
}
proc ex1(x1 : unit * irt) : rt1 = {
var r;
r <@ A.ex1(x1.`2);
return r;
}
proc ex2(x2 : unit * irt) : rt2 = {
var r;
r <@ A.ex2(x2.`2);
return r;
}
}.
declare axiom Bsens : equiv[ B.init ~ B.init : ={arg, glob A, glob B} ==> ={glob A, glob B,res} ].
declare axiom RewProp :
exists (f : glob A -> sbits),
injective f /\
(forall &m, Pr[ A.getState() @ &m : (glob A) = ((glob A){m})
/\ res = f ((glob A){m} ) ] = 1%r) /\
(forall &m b (x: glob A), b = f x =>
Pr[A.setState(b) @ &m : glob A = x] = 1%r) /\
islossless A.setState.
lemma rew_comm_law :
forall &m M i0, Pr[ SpecRewComm(A,B).iex1ex2(i0) @ &m : M res ]
= Pr[ SpecRewComm(A,B).iex2ex1(i0) @ &m : M res ].
proof. move => &m M i0.
have eq : forall i2, Pr[SpecRewComm(A, B).iex1ex2(i0) @ &m : M res] =
Pr[RC.RCR(ModComm(WA, B)).main(i0, i2) @ &m : M res].
move => i2. byequiv (_: ={glob A, glob B} /\ arg.`1{2} = arg{1} ==> _).
proc. inline*. sp.
seq 0 0 : (={glob A, glob B, i0}).
skip. progress.
seq 1 1 : (={glob A, glob B, r0}). call Bsens. skip. progress.
wp. sp.
call (_:true). wp. call (_:true). wp. call (_:true). wp. call (_:true). skip. progress.
smt(). auto.
rewrite (eq (tt, tt)). clear eq.
have eq : forall i2, Pr[SpecRewComm(A, B).iex2ex1(i0) @ &m : M res] =
Pr[RC.RCR(ModComm'(WA, B)).main(i0, i2) @ &m : M res].
move => i2. byequiv (_: ={glob A, glob B} /\ arg.`1{2} = arg{1} ==> _).
proc. inline*. sp.
seq 0 0 : (={glob A, glob B, i0}).
skip. progress.
seq 1 1 : (={glob A, glob B, r0}). call Bsens. skip. progress.
wp. sp.
call (_:true). wp. call (_:true). wp. call (_:true). wp. call (_:true). skip. progress.
smt(). auto.
rewrite (eq (tt, tt)). clear eq.
rewrite (reflectionComb WA B). auto.
simplify. apply Bsens.
simplify.
elim (rewindable_A A RewProp). progress.
exists f. progress.
byphoare (_: (glob A) = (glob A){m0} ==> _).
proc. call (H0 (glob A){m0}). skip. auto. auto. auto.
byphoare (_: arg = f x /\ (glob A) = (glob A){m0} ==> _).
proc. call (H1 x). skip. auto. progress. auto.
proc. call H2. skip. auto. auto.
(* apply ips. apply unpair_pair. apply ips. apply unpair_pair. auto. *)
qed.
end section.
end SpecRewCommutes.