Von Neumann's trick (EasyCrypt examples/vonNeumann.eca) #
EC: "In this theory, we illustrate some reasoning on distributions on Von Neumann's trick to
simulate a fair coin toss using only a biased coin (of unknown bias)." Two flips of the biased
coin are repeated until they differ, and the first is returned: Simulate.sample returns each
bit with the probability of a fair coin (Simulate_is_Fair).
EC's file, declaration by declaration and script line by line. All of EC's lemmas are proven.
Translation:
op p : { real | 0%r < p < 1%r } as in01_pis the classParams;clone import FixedBiased as Biased with op p <- pis a clone ofLib/EC/DBoolBiased.lean.duniform,duniform1E,duniformEareLib/EC/DUniform.lean;b2r (a <> b)isif a ≠ b then 1 else 0,b2i (b = b')isif §b = §b' then 1 else 0,pred1 x \o fstispred1 x ∘ Prod.fst.- pHL's bounds are reals, as EC's.
Gap list (what keeps the file from reading as EC's):
- GAP 1: EC's
equiv SamplePairisSamplePair_Fair(a module and a lemma cannot share a name). - GAP 2:
bypr (res{1}) (res{2})=> // &1 &2 b0introduces the arguments and the memories (intro a1 m1 a2 m2 b0 _); EC'shave ->: Pr[Fair.sample() @ &2 : res = b0] = 1%r/2%ris ahaveused by transitivity (the goal'sPris about the argumenta2, a variable, untilcases). - GAP 3:
byphoare (: True ==> _)for EC'sbyphoare (_: true ==> res = x)=> //(the postcondition is the event's);conseq (: _: = e)isphoare_conseq (: _ ==> _ : = e). - GAP 4:
vn_fst(mu dvn (pred1 x ∘ Prod.fst) = 1/2) is stated once; EC rewritesvnEand counts at its two uses. - GAP 5: after
rnd E; autothe goalsmu d E = … ∧ ∀ v ∈ d, …aresmt_close [mu_pred1, pred1E], withdbiased1Ewhere EC hasrewrite dbiasedE; three of them (rnd (pred1 (!x)); auto=> /#, a case analysis on booleans) and the conditions of threeconseq ih=> /#are by hand, as is the bounds' equation ofSimulate_is_Fair(dbiased1Eandring; EC:smt). - GAP 6: the rule for
while I V k εhas two goals more than EC's (bound_le_one,sat), andrate(0 < 2 p (1 - p)), which EC's=> />leaves in a conjunction;seq 1: b p (1%r - p) (1%r - p) pleaves0 ≤ pᵢ(nonneg) whenpositivitydoes not find it.
@[simp]: Fair.sample = Module.proc Fair.sample.procedure — true by definition, proc sample using no module parameter and so having no holes to fill.
The module declared by module Fair: the record of its procedures Fair.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 Fair.apply_simp to apply it with.
Instances For
proc sample of module Fair as a module: Module.proc Fair.sample.procedure, of type Module.Proc (procsig …). It uses no module parameter, so it is not a function — Fair.sample.apply_simp says exactly that.
Equations
Instances For
Instantiating the holes of Fair.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 [Fair.sample.procedure.apply_simp]).
The body of proc sample of module Fair, 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 Fair.sample.procedure.apply_simp) rather than run. Use Fair.sample for this procedure as a module.
Equations
- GaudisCrypt.Examples.VonNeumann.Fair.sample.procedure = proc () : Bool { var b : Bool; b <$ GaudisCrypt.EC.dbool; return §b }
Instances For
The module declared by module Simulate: the record of its procedures Simulate.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 Simulate.apply_simp to apply it with.
Instances For
@[simp]: Simulate.sample = Module.proc Simulate.sample.procedure — true by definition, proc sample using no module parameter and so having no holes to fill.
Instantiating the holes of Simulate.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 [Simulate.sample.procedure.apply_simp]).
The body of proc sample of module Simulate, 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 Simulate.sample.procedure.apply_simp) rather than run. Use Simulate.sample for this procedure as a module.
Equations
- One or more equations did not get rendered due to their size.
Instances For
proc sample of module Simulate as a module: Module.proc Simulate.sample.procedure, of type Module.Proc (procsig …). It uses no module parameter, so it is not a function — Simulate.sample.apply_simp says exactly that.
Equations
Instances For
Equations
Instances For
The module declared by module SamplePair: the record of its procedures SamplePair.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 SamplePair.apply_simp to apply it with.
Equations
Instances For
proc sample of module SamplePair as a module: Module.proc SamplePair.sample.procedure, of type Module.Proc (procsig …). It uses no module parameter, so it is not a function — SamplePair.sample.apply_simp says exactly that.
Equations
Instances For
The body of proc sample of module SamplePair, 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 SamplePair.sample.procedure.apply_simp) rather than run. Use SamplePair.sample for this procedure as a module.
Equations
- GaudisCrypt.Examples.VonNeumann.SamplePair.sample.procedure = proc () : Bool { var b : Bool, b' : Bool; b, b' <$ GaudisCrypt.Examples.VonNeumann.dvn; return §b }
Instances For
@[simp]: SamplePair.sample = Module.proc SamplePair.sample.procedure — true by definition, proc sample using no module parameter and so having no holes to fill.
Instantiating the holes of SamplePair.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 [SamplePair.sample.procedure.apply_simp]).
We can now prove that sampling a pair in the restricted distribution and flipping two coins independently until they are distinct, returning the first one, are equivalent.