Duplicate-subtree flag of the pending-hash Merkle computation
Definitionbsv_merkle_mutationbitcoindata-structuresformal-verificationmerkle-tree
The flag raised when, while adding a leaf, the computation combines a pending hash with an equal carried hash (the pattern of CVE-2012-2459; nothing is proved about that attack beyond naming the pattern), and the tree property it is compared with.
insertMutrecords whether, while a hash is carried up through the occupied slots, some pending hash equals the hash being carried.mutatedCSis the disjunction of these comparisons over the insertion of all leaves. The final sweep compares nothing.eqPairsays that some complete adjacent pair (positions ; ; and so on) of a list has equal entries.mutatedTreeiseqPairon the list, or recursively on the list of its combined complete pairs. Thus only pairs of complete subtrees are examined.
Formalization Note This is a Lean model written for reasoning; nothing is claimed about any particular program.
Definition code
import Definitions.Def_bsv_merkle_counter
/-!
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} [DecidableEq α] (H : α → α → α)
/-- The comparisons made while one leaf is carried up: `inner[level] == hash`
at each occupied level it passes. -/
def insertMut (h : α) : List (Option α) → Bool
| [] => false
| none :: _ => false
| some x :: s => x == h || insertMut (H x h) s
/-- The flag after adding the leaves of `l` to slots `s`. -/
def mutCounter : List α → List (Option α) → Bool
| [], _ => false
| a :: t, s => insertMut H a s || mutCounter t (insert H a s)
/-- The flag after all leaves have been added to empty slots. -/
def mutatedCS (l : List α) : Bool := mutCounter H l []
/-- Some complete pair of this level has two equal nodes. -/
def eqPair : List α → Bool
| a :: b :: t => a == b || eqPair t
| _ => false
/-- The tree-level meaning. `pairs` keeps exactly the nodes of the next level
that are roots of complete subtrees, so the recursion visits, at every level,
the pairs of complete subtrees and no others. -/
def mutatedTree : List α → Bool
| a :: b :: t => eqPair (a :: b :: t) || mutatedTree (pairs H (a :: b :: t))
| _ => false
termination_by l => l.length
decreasing_by simp only [length_pairs, List.length_cons]; 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