Package Behavioral Equivalence #
This file defines behavioral equivalence for deep packages and proves that package linking preserves it.
Main definitions #
DeepPackage.BehavEquiv- Two packages are behaviorally equivalent if they have the same exports and produce the sameSPCompfor every exported operation under every oracle handler.
Main results #
link_evalOp_eq- Linking correctness: evaluating a linked operation equals evaluating the original code with oracles resolved viaevalWith.BehavEquiv.link_congr_right- Ifp₂ ≡ p₃, thenp₁.link p₂ ≡ p₁.link p₃.BehavEquiv.advantage_link_congr- Ifp₂ ≡ p₃, the advantage of distinguishingp₁.link p₂fromp₁.link p₃is zero.
Design #
Behavioral equivalence is defined semantically using evalWith, which
interprets RawCode into SPComp given an oracle handler. Two packages are
equivalent when, for every shared exported operation, evaluating their
implementations with any oracle handler yields the same SPComp computation.
This is stronger than just comparing eval (which maps unresolved oracles to
fail), because it captures how the packages behave when composed with
arbitrary environments.
References #
Behavioral Equivalence #
Two deep packages are behaviorally equivalent if:
- They have the same export interface.
- For every exported operation and every oracle handler, their
implementations produce the same
SPCompcomputation.
This captures semantic equivalence: the packages are interchangeable
in any context (linking environment). Using evalWith rather than
eval ensures we account for how the code behaves when oracle calls
are resolved by an arbitrary environment.
The packages export the same operations
- impl_eq (op : ℕ) (dom codom : Type u) (h₁ : (op, dom, codom) ∈ p₁.exports.ops) (h₂ : (op, dom, codom) ∈ p₂.exports.ops) (x : dom) (oracle : ℕ → (dom' codom' : Type u) → dom' → Core.SPComp codom') : RawCode.evalWith oracle (p₁.impl op dom codom h₁ x).code = RawCode.evalWith oracle (p₂.impl op dom codom h₂ x).code
For every exported operation and oracle handler, the evaluations agree
Instances For
Behavioral equivalence is reflexive.
Behavioral equivalence is symmetric.
Behavioral equivalence is transitive.
Linking Correctness #
Linking correctness: Evaluating a linked operation is the same as evaluating the original code with an oracle handler that dispatches to the linked package's implementations.
Concretely, for an operation (op, dom, codom) exported by p₁:
((p₁.link p₂).impl op dom codom h x).code.eval
= (p₁.impl op dom codom h x).code.evalWith (fun op' dom' codom' x' =>
linkEnv p₂ op' dom' codom' x' |>.eval)
This follows directly from eval_substOracle.
Generalized linking correctness with an arbitrary outer oracle handler.
Evaluating a linked operation with handler oracle equals evaluating the
original code with a composed handler: oracle calls first go through p₂'s
link environment (via substOracle), then the result is evaluated with
oracle.
This follows from evalWith_substOracle.
Linking Preserves Behavioral Equivalence #
Right congruence for linking: If p₂ and p₃ are behaviorally
equivalent, then p₁.link p₂ is behaviorally equivalent to p₁.link p₃.
The proof unfolds link on both sides, applies evalWith_substOracle to
reduce to evaluating p₁'s code with composed handlers, then shows the
handlers agree because p₂ and p₃ agree (by behavioral equivalence).
Left congruence for linking: If p₁ and p₂ are behaviorally
equivalent and have the same imports, then p₁.link q is behaviorally
equivalent to p₂.link q.
The proof uses evalWith_congr in the other direction: now the outer
code differs (p₁ vs p₂) but the oracle environment is the same (q).
Advantage from Behavioral Equivalence #
If two packages are behaviorally equivalent, evaluating any exported
operation (with eval, i.e., unresolved oracles become fail) gives
the same SPComp.
Zero advantage from behavioral equivalence of linked packages.
If p₂ ≡ p₃, then for any distinguisher package p₁ and any
adversary A, the advantage of distinguishing p₁.link p₂ from
p₁.link p₃ is zero (for any exported operation used as the game).
Specialized to universe 0 because AdvantageA requires {α : Type}.
This follows from:
link_congr_rightgivesp₁.link p₂ ≡ p₁.link p₃- Behavioral equivalence implies
evalOpequality - Equal
SPComps have zero advantage
Useful Derived Lemmas #
Linking with behaviorally equivalent packages gives the same evalOp.
This is a convenience wrapper combining link_congr_right and evalOp_eq.
rHoare from behavioral equivalence: equivalent packages satisfy
relational Hoare logic with eqPre/eqPost.