A four-sided die from a six-sided one (EasyCrypt examples/Dice4_6.ec) #
Roll a six-sided die until it shows 1–4 (D6): each of 1–4 has probability 1/4 (prD6), as
for a four-sided die (D4, prD4). The two are related (D4_D6) through EC's theory of
rejection sampling, cloned as D4_6: D4 samples the restricted distribution (D4_Sample),
D6 is the rejection loop (D6_Sample), and the theory relates the two
(D4_6.sampleE_sampleWi, Lib/EC/WhileSamplingFixedTest.lean).
EC's file, declaration by declaration. All of EC's lemmas are proven.
Translation:
[1..6]isdinter 1 6(Lib/EC/DInterval.lean);var r <- 5is the declaration and the assignment;while (5 <= r) r <$ [1..6]has its body in braces.clone WhileSamplingFixedTest as D4_6 with …: the clone; the theory'sdt,testat the clone are writtenWhileSamplingFixedTest.dt,WhileSamplingFixedTest.test.
Gap list (what keeps the file from reading as EC's):
- GAP 1 (
D4_Sample): EC'srndasks that the two distributions give each value the same mass, which the script computes (dexcepted1E,weight_dinter,dinter1E,dinterE,iotaS, …);equiv_rndasks that they are equal, which is the library'sdinter_dexcepted(an interval conditioned on a sub-interval). - GAP 2 (
D4_D6): EC'sproc *andrewrite equiv[{1} 1 L args](the call on the left replaced along an equivalence, three times, thensim) are twotransitivitysteps on the procedures (equiv_trans (N.f) (P₁ ==> Q₁) (P₂ ==> Q₂)): throughD4_6.SampleE.sample(D4_Sample) andD4_6.SampleWi.sample(D4_6.sampleE_sampleWi, thenD6_Samplereversed). The argument((), 5 :@ r)and the side conditiondinter_llof EC's second rewrite are thehPgoal of the second step. - GAP 3:
rnd f finv; auto=> />isequiv_rnd f finv; equiv_skip(equiv_autoloops onb = f (finv b)); EC'srewrite supp_dinter /#on the conditions (andapply: dinter_uni) aresmt_close [supp_dinter],smt_close [supp_dinter, dinter1E]. - GAP 4:
byphoare (: True ==> _)forbyphoare=> //;rndon an exact bound isphoare_rnd (pred1 k).
proc sample of module D4 as a module: Module.proc D4.sample.procedure, of type Module.Proc (procsig …). It uses no module parameter, so it is not a function — D4.sample.apply_simp says exactly that.
Equations
Instances For
Instantiating the holes of D4.sample.procedure with a tuple holeArgs of procedures: the body of proc sample as written, with each hole call replaced by the matching holeArgs.lookup (HoleIndex.zero being the first hole the body declared). True by rfl.
Deliberately not @[simp], unlike the other apply_simp lemmas: its right-hand side is the whole body written out, so letting it fire on its own would turn any goal mentioning an instantiated procedure into that body. Inlining a procedure is a step to ask for by name (simp [D4.sample.procedure.apply_simp]).
@[simp]: D4.sample = Module.proc D4.sample.procedure — true by definition, proc sample using no module parameter and so having no holes to fill.
The module declared by module D4: the record of its procedures D4.sample.
The declaration has no parameter list, so no procedure of it can call one either and the module is not a function — it is the plain record itself, and there is no D4.apply_simp to apply it with.
Instances For
The body of proc sample of module D4, as a procedure of its own: it calls no module parameter, so it has no holes.
A hole stands for a call whose callee is not known here — a module parameter is supplied only when the module is applied — so the body is a ProcedureWithHoles, to be instantiated (see D4.sample.procedure.apply_simp) rather than run. Use D4.sample for this procedure as a module.
Equations
- GaudisCrypt.Examples.Dice4_6.D4.sample.procedure = proc () : ℤ { var r : ℤ; r <$ GaudisCrypt.EC.dinter 1 4; return §r }
Instances For
Instantiating the holes of D6.sample.procedure with a tuple holeArgs of procedures: the body of proc sample as written, with each hole call replaced by the matching holeArgs.lookup (HoleIndex.zero being the first hole the body declared). True by rfl.
Deliberately not @[simp], unlike the other apply_simp lemmas: its right-hand side is the whole body written out, so letting it fire on its own would turn any goal mentioning an instantiated procedure into that body. Inlining a procedure is a step to ask for by name (simp [D6.sample.procedure.apply_simp]).
The body of proc sample of module D6, as a procedure of its own: it calls no module parameter, so it has no holes.
A hole stands for a call whose callee is not known here — a module parameter is supplied only when the module is applied — so the body is a ProcedureWithHoles, to be instantiated (see D6.sample.procedure.apply_simp) rather than run. Use D6.sample for this procedure as a module.
Equations
- GaudisCrypt.Examples.Dice4_6.D6.sample.procedure = proc () : ℤ { var r : ℤ; r <- 5; while (decide (5 ≤ §r)) { r <$ GaudisCrypt.EC.dinter 1 6; } return §r }
Instances For
@[simp]: D6.sample = Module.proc D6.sample.procedure — true by definition, proc sample using no module parameter and so having no holes to fill.
The module declared by module D6: the record of its procedures D6.sample.
The declaration has no parameter list, so no procedure of it can call one either and the module is not a function — it is the plain record itself, and there is no D6.apply_simp to apply it with.
Instances For
proc sample of module D6 as a module: Module.proc D6.sample.procedure, of type Module.Proc (procsig …). It uses no module parameter, so it is not a function — D6.sample.apply_simp says exactly that.