Merkle root computation with one pending hash per level (slot model)
Definitionbsv_merkle_counterbitcoindata-structuresformal-verificationmerkle-tree
A model of the computation of a Merkle root that keeps one pending subtree hash per level, over an arbitrary combining function .
The pending hashes form a list of slots, lowest level first; each slot is empty or holds a hash.
insertadds a hash at the lowest level: it fills the first slot if that is empty; if it holds , it empties it and inserts one level up (a binary increment with carries).counterinserts the leaves of a list in order.carry, started on the slots above a hash : if no slot remains it returns ; at an occupied slot it replaces by and moves up; at an empty slot it stops with if all higher slots are empty, and otherwise replaces by and moves up.topreturns if all higher slots are empty, and otherwise carries .finishskips the empty lowest slots and appliestopat the first occupied one; it returnsnoneif there is none.rootCSisfinishapplied to the slots after all leaves have been inserted.
pairs combines the complete adjacent pairs of a list and drops an unpaired last element; odd returns that unpaired element if the length is odd.
Formalization Note This is a Lean model written for reasoning; nothing is claimed about any particular program.
Definition code
import Definitions.Def_bsv_merkle_tree
/-!
Authors: Gregory J. Ward, Bryan W. Daugherty, Shawn M. Ryan.
Source: https://github.com/codenlighten/bsv-lean-proofs (41c4bfd), Apache-2.0.
-/
namespace Bsv.Merkle
section
variable {α : Type} (H : α → α → α)
/-- Add one leaf (or a carried subtree hash) at the lowest level. -/
def insert (h : α) : List (Option α) → List (Option α)
| [] => [some h]
| none :: s => some h :: s
| some x :: s => none :: insert (H x h) s
/-- The slots after all leaves of `l` have been added to `s`. -/
def counter (l : List α) (s : List (Option α)) : List (Option α) :=
l.foldl (fun s h => insert H h s) s
/-- The inner loop of the sweep: `h` has just been combined with itself and is
carried up through occupied levels; at a free level it stops, and is combined
with itself again unless nothing is pending above. -/
def carry (h : α) : List (Option α) → α
| [] => h
| some x :: s => carry (H x h) s
| none :: s => if s.all Option.isNone then h else carry (H h h) s
/-- The outer loop of the sweep, from a hash `h` at an occupied level. -/
def top (h : α) (s : List (Option α)) : α :=
if s.all Option.isNone then h else carry H (H h h) s
/-- The sweep: start from the lowest occupied level. -/
def finish : List (Option α) → Option α
| [] => none
| none :: s => finish s
| some h :: s => some (top H h s)
/-- The root as the computation with one pending hash per level produces it. -/
def rootCS (l : List α) : Option α := finish H (counter H l [])
/-- The complete pairs of a level. -/
def pairs : List α → List α
| a :: b :: t => H a b :: pairs t
| _ => []
/-- The unpaired last node of a level, if its length is odd. -/
def odd : List α → Option α
| [] => none
| [a] => some a
| _ :: _ :: t => odd t
theorem length_pairs : ∀ l : List α, (pairs H l).length = l.length / 2
| [] => by simp [pairs]
| [_] => by simp [pairs]
| _ :: _ :: t => by simp only [pairs, List.length_cons, length_pairs t]; omega
end
end Bsv.Merkle
Source
Gregory J. Ward, Bryan W. Daugherty, Shawn M. Ryan, BSV Lean proofs (Apache-2.0). https://github.com/codenlighten/bsv-lean-proofs