Documentation

CatCryptCore.Bridge.SemPkg

The Package Category: Substitution, Not Sequencing #

Substitution vs Sequencing #

Cryptographic packages compose by oracle resolution (substitution): when package p₁ calls an oracle, the call is resolved by package p₂'s implementation. This is fundamentally different from Kleisli sequencing (the composition in Fam(KlSPComp)), where one computation's output feeds as input to the next.

Substitution (packages)Sequencing (Fam/Kleisli)
MorphismsHandler → Handler (higher-order)A →ₖ B (first-order)
CompositionFunction compositionMonadic bind
Crypto meaningOracle resolutionData pipeline
PL analogyVariable substitutionStatement sequencing

This distinction is well-studied in programming language semantics (Fiore–Plotkin–Turi), where substitution and sequencing are related by the continuation-passing transform but are categorically distinct. All major cryptographic frameworks (CatCrypt, constructive crypto, UC, EasyCrypt) use substitution-style composition for packages.

The Package Category #

We define a category on PkgInterface where:

This is the category induced by the functor TypedHandler : PkgInterface → Type that maps each interface to its handler type (product of Kleisli arrows). In the language of enriched category theory, it is the underlying ordinary category of Set(TypedHandler(I), TypedHandler(J)).

Monoidal Structure #

The symmetric monoidal structure comes from the product decomposition:

TypedHandler(I ⊗ J) ≅ TypedHandler(I) × TypedHandler(J)

Since PkgInterface.tensor uses Sum on index types and Sum.elim on families, this isomorphism is essentially definitional. The monoidal laws reduce to Sum index manipulation (funext + cases + rfl), in contrast to the complex fam_ext/HEq/erw proofs needed for Fam(C).

Relationship to Fam(KlSPComp) #

Fam(KlSPComp) provides the correct monoidal structure (parallel composition) and individual Kleisli arrows for each operation. But its composition (Kleisli bind) does NOT match cryptographic linking (oracle substitution). Packages share Fam's monoidal structure but have their own categorical composition.

There is no functor between these categories in either direction: Fam composition is first-order (data pipeline), while package composition is second-order (handler transformation). They are related by CPS / double-negation translation but categorically distinct.

Main definitions #

Implementation Note #

The MonoidalCategoryStruct is defined as a private helper and explicitly assigned to MonoidalCategory via toMonoidalCategoryStruct. This ensures:

  1. Within MonoidalCategory where, all struct fields are accessible for coherence proofs (pentagon, etc.) which are all rfl.
  2. There is exactly ONE path to tensorObj (no typeclass diamond), so I ⊗ J definitionally equals the struct's tensorObj I J.
  3. The braiding iso can be defined on PkgInterface.tensor (where Sum structure is visible) and coerced via definitional equality.

Typed Handler #

A typed oracle handler for interface I: for each operation indexed by I.ι, a Kleisli arrow from the domain type to the codomain type.

This is the product ∏_{i : I.ι} KlSPComp(I.doms i, I.codoms i), the "semantic content" of a package that exports interface I.

Equations
Instances For

    Extract the left component of a handler for a tensor interface.

    Equations
    Instances For

      Extract the right component of a handler for a tensor interface.

      Equations
      Instances For

        Combine handlers for the left and right components into a tensor handler.

        Equations
        Instances For
          @[simp]
          theorem CatCrypt.Category.TypedHandler.pair_fst {I J : PkgInterface} (h₁ : TypedHandler I) (h₂ : TypedHandler J) :
          (h₁.pair h₂).fst = h₁
          @[simp]
          theorem CatCrypt.Category.TypedHandler.pair_snd {I J : PkgInterface} (h₁ : TypedHandler I) (h₂ : TypedHandler J) :
          (h₁.pair h₂).snd = h₂

          Category Instance #

          @[implicit_reducible]

          The package category: objects are PkgInterface, morphisms are handler transformers. Composition is function composition, corresponding to oracle resolution (substitution).

          Equations
          • One or more equations did not get rendered due to their size.
          @[simp]
          theorem CatCrypt.Category.pkg_comp_apply {I J K : PkgInterface} (f : I J) (g : J K) (h : TypedHandler I) :

          Monoidal Category Instance #

          The MonoidalCategoryStruct is built as a private helper, then assigned to the MonoidalCategory instance via toMonoidalCategoryStruct. This avoids:

          @[implicit_reducible]
          Equations
          • One or more equations did not get rendered due to their size.
          @[implicit_reducible]
          Equations
          • One or more equations did not get rendered due to their size.

          Braiding and Symmetric Structure #

          The braiding iso is defined on PkgInterface.tensor where Sum structure is directly visible. It is then used in the SymmetricCategory instance via the definitional equality I.tensor J = I ⊗ J.

          @[implicit_reducible]
          Equations
          • One or more equations did not get rendered due to their size.

          Connection to Untyped Handlers #

          The untyped Handler from PkgEval.lean handles ALL operations (∀ op dom codom, dom → SPComp codom). TypedHandler I restricts to a specific interface. The relationship:

          The typed version is the correct mathematical object for the SMC structure, while the untyped version is convenient for the DeepPackage evaluation bridge.

          PkgImpl (σ = id) is exactly TypedHandler. This connects the categorical framework to the Fam-based representation. PkgImpl.ofImpls constructs a Fam morphism from componentwise implementations; TypedHandler IS those componentwise implementations.