Skip to main content

Crate dig_smt

Crate dig_smt 

Source
Expand description

§dig-smt — the DIG Network generic sparse-merkle-tree mechanism

A sha256 sparse merkle tree over 32-byte keys, with real membership AND non-membership proofs, extracted VERBATIM (byte-identical roots) from dig-identity’s profile tree. This crate is the generic MECHANISM only: it knows nothing about DIG slots, values, profiles, or DIDs. A caller supplies raw 32-byte keys and opaque value bytes; a higher-level crate builds a domain schema (slot-key derivation, value encoding) on top and CONSUMES this tree.

The tree wraps the audited Nervos sparse_merkle_tree crate — we never re-implement the merkle construction; we only supply the sha256 Sha256Hasher it hashes nodes with and the hash_leaf_value leaf scheme (sha256(0x01 ‖ key ‖ encoded_value), key bound in for self-contained leaf/branch domain separation — see hash).

§Byte-contract (§5.1 / NC-9)

For a given set of (key, encoded_value) leaves, the 32-byte SmtTree::root is byte-identical to dig-identity’s current profile root. A profile root is on-chain-anchored, so a drift would render published profiles unreadable. The invariant is pinned by a golden root vector (tests/golden_root.rs), computed from dig-identity’s current behaviour.

§What lives here

ConcernItem
sha256 + the leaf/node hashinghash
The mutable tree: set/get/root/proveSmtTree
Serializable proofs + root-only verificationSmtProof, verify_membership, verify_non_membership

§Proving a key against a root

use dig_smt::{SmtTree, verify_membership, verify_non_membership};

let mut tree = SmtTree::new();
let name_key = [1u8; 32];
tree.set(name_key, b"Ada".to_vec()).unwrap();
let root = tree.root();

// Prove the key holds "Ada" using only (root, key, value, proof).
let membership = tree.prove_membership(name_key).unwrap();
assert!(verify_membership(&root, name_key, b"Ada", &membership).unwrap());

// Prove a different key is absent.
let absent_key = [2u8; 32];
let absent = tree.prove_non_membership(absent_key).unwrap();
assert!(verify_non_membership(&root, absent_key, &absent).unwrap());

Re-exports§

pub use error::SmtError;
pub use error::SmtResult;
pub use hash::hash_leaf_value;
pub use hash::sha256;
pub use hash::Digest32;
pub use hash::Sha256Hasher;
pub use hash::LEAF_DOMAIN;
pub use proof::verify_membership;
pub use proof::verify_non_membership;
pub use proof::SmtProof;
pub use tree::SmtTree;

Modules§

error
The crate’s single error type.
hash
sha256 primitives: the leaf-value hash and the SMT node hasher.
proof
The serializable proof blob and the root-only verification functions.
tree
The mutable sparse merkle tree: set/get keys, compute the root, and produce proofs.