IncrementalMerkleTree

scalus.cardano.onchain.plutus.crypto.tree.IncrementalMerkleTree

Incremental Merkle tree for oracle-managed sets.

Fixed-depth D binary Merkle tree where leaves are appended sequentially. The tree is 1-indexed: tree(1) = root, tree(2^D + i) = leaf i.

Leaf i stores blake2b_256(key_i) or EmptyLeafHash if unused. EmptyLeafHash = blake2b_256(0x00..00) (hash of 32 zero bytes).

State: (root, size) where size = number of appended elements (next free slot).

Operations:

  • append: oracle adds a new member (2*D + 2 blake2b, single pass)
  • verifyMembership: user proves membership (D + 1 blake2b)

Membership proofs use interleaved format: D * (direction[1] + sibling[32]) = D*33 bytes. Append proofs are flat ByteStrings: D consecutive 32-byte sibling hashes.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def append(root: ByteString, size: BigInt, depth: BigInt, key: ByteString, siblings: ByteString): ByteString

Append a new key at position size, returning the new root hash.

Append a new key at position size, returning the new root hash.

Uses a combined single-pass algorithm: verifies the slot is empty AND computes the new root in one recursive walk. This saves D recursive calls, D sliceByteString, D modInteger, and D quotientInteger operations compared to two separate merkleUp calls.

Cost: 2*D + 2 blake2b calls.

Attributes

def combine(left: ByteString, right: ByteString): ByteString

Combine two 32-byte hashes into a parent hash.

Combine two 32-byte hashes into a parent hash.

Attributes

def merkleUp(proof: ByteString, hash: ByteString, offset: BigInt, endOffset: BigInt): ByteString

Walk from a leaf up to the root using an interleaved proof.

Walk from a leaf up to the root using an interleaved proof.

proof is a flat ByteString of D * 33 bytes: D repetitions of (direction[1] + sibling[32]). direction is 0 (left child) or 1 (right child).

Uses path-byte encoding (indexByteString) instead of BigInt modInteger/quotientInteger to determine left/right at each level, and offset tracking (offset increments by 33).

Attributes

def verifyMembership(root: ByteString, key: ByteString, depth: BigInt, proof: ByteString): Unit

Verify that key is a member of the tree.

Verify that key is a member of the tree.

proof is D * 33 bytes: D repetitions of (direction[1] + sibling[32]). Costs D + 1 blake2b calls.

Attributes

Concrete fields

Hash of 32 zero bytes — the hash stored in empty leaf slots.

Hash of 32 zero bytes — the hash stored in empty leaf slots.

Attributes

lazy val sirDeps: List[SIRModuleWithDeps]
lazy val sirModule: Module