cartesian_merkle_tree 0.2.1
Cairo implementation of Cartesian Merkle Trees - efficient cryptographic data structure combining BST, heap, and Merkle tree properties
A Cairo library implementation of Cartesian Merkle Trees (CMTs) - an efficient data structure combining binary search tree properties with heap properties and Merkle tree cryptographic verification capabilities.
Cartesian Merkle Trees represent a significant advancement over traditional Merkle tree structures by storing useful data in every node rather than just leaves. This implementation provides:
CMTs combine three fundamental data structures:
Each element in the tree corresponds to a point on a 2D plane:
This dual ordering ensures both efficient operations and cryptographic integrity.
CMTs are particularly well-suited for blockchain applications:
CMTs are ideal for various onchain applications requiring dynamic, verifiable data structures:
Add to your Scarb.toml
:
[dependencies]
cartesian_merkle_tree = "0.2.0"
Or install from GitHub:
[dependencies]
cartesian_merkle_tree = { git = "https://github.com/ametel01/cartesian-merkle-tree.git" }
use cartesian_merkle_tree::{CMTree, CMTreeTrait};
// Create a new tree
let mut tree = CMTreeTrait::new();
// Insert elements
tree.insert(50);
tree.insert(30);
tree.insert(70);
// Search for elements
assert!(tree.search(50));
assert!(!tree.search(100));
// Remove elements
assert!(tree.remove(30));
assert!(!tree.search(30));
// Get root hash for verification
let root_hash = tree.get_root_hash();
use cartesian_merkle_tree::{CMTree, CMTreeTrait, CMTreeProofTrait};
let mut tree = CMTreeTrait::new();
tree.insert(50);
tree.insert(30);
tree.insert(70);
// Generate existence proof
let proof = tree.generate_proof(30);
let root_hash = tree.get_root_hash();
// Verify the proof
assert!(proof.verify(root_hash, 30));
// Generate non-existence proof
let non_existence_proof = tree.generate_proof(40);
assert!(non_existence_proof.verify(root_hash, 40));
#[starknet::contract]
mod MyContract {
use cartesian_merkle_tree::cmtree_component::cmtree_component;
component!(path: cmtree_component, storage: cmtree, event: CMTreeEvent);
// Embed the CMTree component's external functions
#[abi(embed_v0)]
impl CMTreeImpl = cmtree_component::CMTree<ContractState>;
// Access to internal functions
impl CMTreeInternalImpl = cmtree_component::InternalFunctions<ContractState>;
#[storage]
struct Storage {
#[substorage(v0)]
cmtree: cmtree_component::Storage,
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
CMTreeEvent: cmtree_component::Event,
}
#[constructor]
fn constructor(ref self: ContractState) {
// Initialize the CMTree component
self.cmtree.initializer();
}
}
use cartesian_merkle_tree::components::cmtree_component::{
ICMTreeDispatcher, ICMTreeDispatcherTrait
};
fn use_cmt_contract(contract_address: ContractAddress) {
let dispatcher = ICMTreeDispatcher { contract_address };
// Insert elements
dispatcher.insert(50);
dispatcher.insert(30);
dispatcher.insert(70);
// Search for elements
assert!(dispatcher.search(50));
// Generate and verify proofs
let proof = dispatcher.generate_proof(30);
let root_hash = dispatcher.get_root_hash();
assert!(dispatcher.verify_proof(proof, root_hash, 30));
// Remove elements
assert!(dispatcher.remove(30));
}
This Cairo implementation features:
This implementation is based on the research paper "Cartesian Merkle Trees" which provides the theoretical foundation and formal analysis of the data structure.
For complete API documentation and implementation details, run:
scarb doc --build
Run the test suite with:
snforge test
The implementation includes comprehensive tests covering:
This implementation follows the research outlined in the linked paper and provides a practical Cairo implementation for blockchain applications.
Version 0.2.1
Uploaded 2 days ago
License MIT
Size 1017.6 KB
Run the following command in your project dir
scarb add cartesian_merkle_tree@0.2.1
Or add the following line to your Scarb.toml
cartesian_merkle_tree = "0.2.1"