Documentation

CatCryptCore.Core.GenHeap

Countable-Typed Heap Locations #

CatCrypt.Core.Location requires Fintype ty, so a heap cell can only hold a value of a finite type. GLocation requires only Countable ty and Inhabited ty, which admits cells holding Nat, List α, association lists, and other unbounded values.

Both flavours read and write the same carrier, CatCrypt.Core.Heap — a partial finite map from location ids to natural numbers. Only the value codec differs: Heap.get/Heap.set code through Fintype.equivFin, while Heap.gget/Heap.gset code through gencode, a chosen injection ty ↪ Nat. Since gencode uses Fintype.equivFin whenever the value type happens to be finite, the two codecs agree on finite types, and gget/gset restrict to get/set along GLocation.ofLocation (gget_ofLocation, gset_ofLocation).

SPComp α is Heap → SDistr (α × Heap), so it needs no adjustment: rHoare, DependsOn and PreservesOutside apply directly to computations built from SPComp.gget/SPComp.gset. This file supplies the footprint lemmas for those operations.

Main definitions #

Codec for a countable type #

noncomputable def CatCrypt.Core.gencode (α : Type) [Countable α] :
α → ℕ

Injection of a countable type into Nat. On a finite type it is Fintype.equivFin followed by Fin.val; otherwise it is a chosen injection.

Equations
Instances For

    On a finite type, gencode is Heap.encode.

    noncomputable def CatCrypt.Core.gdecode (α : Type) [Countable α] [Inhabited α] (n : ℕ) :
    α

    Partial inverse of gencode, returning default off its image.

    Equations
    Instances For
      theorem CatCrypt.Core.gdecode_gencode {α : Type} [Countable α] [Inhabited α] (a : α) :
      gdecode α (gencode α a) = a

      On a finite type, gdecode is Heap.decode.

      Locations with a countable value type #

      Heap location whose value type is countable and inhabited. The value type is fixed at Type (universe 0), matching Location.

      Instances For

        A Location is a GLocation: a finite type is countable.

        Equations
        Instances For

          Heap access at a GLocation #

          noncomputable def CatCrypt.Core.Heap.gget (h : Heap) (l : GLocation) :
          l.ty

          Read a countable-typed location. Returns default if never written.

          Equations
          Instances For
            noncomputable def CatCrypt.Core.Heap.gset (h : Heap) (l : GLocation) (v : l.ty) :

            Write a countable-typed location.

            Equations
            Instances For
              @[simp]
              theorem CatCrypt.Core.Heap.gget_gset_same (h : Heap) (l : GLocation) (v : l.ty) :
              (h.gset l v).gget l = v
              @[simp]
              theorem CatCrypt.Core.Heap.gget_gset_other (h : Heap) (l l' : GLocation) (v : l.ty) (hne : l.id ≠ l'.id) :
              (h.gset l v).gget l' = h.gget l'
              @[simp]
              theorem CatCrypt.Core.Heap.gset_gset_same (h : Heap) (l : GLocation) (v₁ v₂ : l.ty) :
              (h.gset l v₁).gset l v₂ = h.gset l v₂
              theorem CatCrypt.Core.Heap.gset_gset_comm (h : Heap) (l₁ l₂ : GLocation) (v₁ : l₁.ty) (v₂ : l₂.ty) (hne : l₁.id ≠ l₂.id) :
              (h.gset l₁ v₁).gset l₂ v₂ = (h.gset l₂ v₂).gset l₁ v₁
              @[simp]
              theorem CatCrypt.Core.Heap.gset_lookup_of_ne (h : Heap) (l : GLocation) (v : l.ty) {i : ℕ} (hne : i ≠ l.id) :
              theorem CatCrypt.Core.Heap.gget_congr_of_agreeOn {h₁ h₂ : Heap} {locs : LocSet} (hag : h₁.agreeOn h₂ locs) {l : GLocation} (hmem : l.id ∈ locs) :
              h₁.gget l = h₂.gget l

              Two heaps agreeing on a location's id agree on its contents.

              Compatibility of the two flavours #

              The countable codec extends the finite one, so a Location read or written through the generalized interface behaves exactly as it does through Heap.get/Heap.set.

              @[simp]
              theorem CatCrypt.Core.Heap.gget_set_other (h : Heap) (l : Location) (l' : GLocation) (v : l.ty) (hne : l.id ≠ l'.id) :
              (h.set l v).gget l' = h.gget l'

              A finite-typed write does not disturb a countable-typed read at a different id.

              theorem CatCrypt.Core.Heap.get_gset_other (h : Heap) (l : GLocation) (l' : Location) (v : l.ty) (hne : l.id ≠ l'.id) :
              (h.gset l v).get l' = h.get l'

              A countable-typed write does not disturb a finite-typed read at a different id.

              Stateful operations #

              noncomputable def CatCrypt.Core.SPComp.gget (l : GLocation) :

              Read a countable-typed location.

              Equations
              Instances For
                noncomputable def CatCrypt.Core.SPComp.gset (l : GLocation) (v : l.ty) :

                Write a countable-typed location.

                Equations
                Instances For

                  The generalized operations restrict to the finite-typed ones.

                  Framing #

                  The left heap stores v at a countable-typed location.

                  Equations
                  Instances For

                    The right heap stores v at a countable-typed location.

                    Equations
                    Instances For

                      Non-vacuity #

                      Nat is a legal GLocation value type and is not a legal Location value type.

                      @[reducible]

                      An unbounded counter cell.

                      Equations
                      Instances For

                        Nat carries no Fintype instance, so natCounter's value type is outside the range of Location.

                        noncomputable def CatCrypt.Core.incr (id : ℕ) :

                        Incrementing a counter held in the heap.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          incr run twice from the empty heap yields 2 and stores 2.