|
| 1 | +# Merkle Tree Implementations for Whitelist and Token Minting |
| 2 | + |
| 3 | +This repository demonstrates two approaches to managing whitelists in Ethereum-based smart contracts using **Cartesian Merkle Tree (CMT)** and **Binary Merkle Tree (BMT)**. These methods enable cryptographic validation of authorized users for ERC20 token minting. |
| 4 | + |
| 5 | +## Cartesian Merkle Tree Overview |
| 6 | + |
| 7 | +A **Cartesian Merkle Tree (CMT)** is a hybrid data structure combining features of: |
| 8 | +1. **Binary Search Trees (BST):** Ensures ordered insertion for fast lookup. |
| 9 | +2. **Heap Structures:** Maintains a heap priority, ensuring deterministic balancing. |
| 10 | +3. **Merkle Trees:** Cryptographically secure trees where each node stores a hash of its children, allowing efficient proof generation and validation. |
| 11 | + |
| 12 | +### Key Features of CMT |
| 13 | +* **Dynamic Management:** Supports adding and removing nodes dynamically. |
| 14 | +* **Advanced Proofs:** Handles inclusion and exclusion proofs. |
| 15 | +* **Deterministic:** The structure remains consistent regardless of the insertion order. |
| 16 | + |
| 17 | +### Benefits of CMT |
| 18 | +* **Ideal for dynamic whitelists:** Update permissions at runtime without redeploying the contract. |
| 19 | +* **Optimized for privacy:** Supports zero-knowledge proof (ZK-proof) applications. |
| 20 | +* **Secure:** Ensures robust cryptographic integrity for validation. |
| 21 | + |
| 22 | +### Use Cases |
| 23 | +* **Whitelist Management:** Dynamically add and remove users at runtime. |
| 24 | +* **Zero-Knowledge Applications:** Verify data without revealing sensitive information. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Binary Merkle Tree Overview |
| 29 | + |
| 30 | +A **Binary Merkle Tree (BMT)** is a simpler structure where each node stores a hash derived from its child nodes. It is widely used for efficient validation of inclusion proofs in pre-defined datasets. |
| 31 | + |
| 32 | +### Key Features of BMT |
| 33 | +* **Simple Structure:** Designed for static datasets. |
| 34 | +* **Inclusion Proofs:** Focused on validating the presence of data. |
| 35 | +* **Lightweight and Efficient:** Reduces computational complexity. |
| 36 | + |
| 37 | +### Benefits of BMT |
| 38 | +* **Straightforward Implementation:** Easy to integrate and understand. |
| 39 | +* **Ideal for static whitelists:** Perfect for use cases where the whitelist doesn't change. |
| 40 | +* **Seamless Integration:** Works with libraries like OpenZeppelin's `MerkleProof`. |
| 41 | + |
| 42 | +### Use Cases |
| 43 | +* **Static Whitelists:** Verify pre-computed whitelists for token minting. |
| 44 | +* **Efficient Validation:** Quickly validate authorized users. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Differences Between CMT and BMT |
| 49 | + |
| 50 | +| **Criteria** | **Cartesian Merkle Tree (CMT)** | **Binary Merkle Tree (BMT)** | |
| 51 | +|---------------------------|----------------------------------------------|--------------------------------------------| |
| 52 | +| **Structure** | Combination of BST, heap, and Merkle Tree | Simple binary Merkle Tree | |
| 53 | +| **Dynamic Management** | Supports dynamic addition and removal | Designed for static datasets | |
| 54 | +| **Proof Types** | Inclusion and exclusion | Inclusion only | |
| 55 | +| **Complexity** | Higher complexity, supports advanced cases | Lower complexity, easy to implement | |
| 56 | +| **Use Cases** | Dynamic whitelists, ZK-proof applications | Static whitelists, basic validation | |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Token Minting with Merkle Trees |
| 61 | + |
| 62 | +### Why Use Merkle Trees for Whitelist Management? |
| 63 | +* **Security:** Ensures only authorized users interact with the contract. |
| 64 | +* **Efficiency:** Reduces validation costs using cryptographic proofs. |
| 65 | +* **Flexibility:** CMT allows dynamic updates, while BMT is optimized for static setups. |
| 66 | + |
| 67 | +### Implementation Highlights |
| 68 | +1. **Whitelist Validation:** Both approaches use cryptographic proofs to validate user inclusion. |
| 69 | +2. **Token Minting:** Users can mint tokens only if they provide a valid proof. |
| 70 | +3. **Merkle Root Management:** Contracts validate proofs using the root hash of the Merkle Tree. |
| 71 | + |
| 72 | +### When to Use Each Approach |
| 73 | +* **Use CMT** for dynamic whitelists where permissions need to change frequently. |
| 74 | +* **Use BMT** for static whitelists that are pre-defined and unlikely to change. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## How to Run Tests |
| 79 | + |
| 80 | +### Prerequisites |
| 81 | +* **Foundry:** A testing framework for Solidity. |
| 82 | +* **Node.js:** To manage dependencies and scripts. |
| 83 | + |
| 84 | +### Steps |
| 85 | +1. **Clone the Repository:** |
| 86 | + ```bash |
| 87 | + git clone https://github.com/your-repo/merkle-trees.git |
| 88 | + cd merkle-trees |
| 89 | + ``` |
| 90 | + |
| 91 | +2. **Install Dependencies:** |
| 92 | + ```bash |
| 93 | + forge install |
| 94 | + ``` |
| 95 | + |
| 96 | +3. **Run Tests:** |
| 97 | + ```bash |
| 98 | + forge test |
| 99 | + ``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +This README provides a comprehensive overview of Cartesian and Binary Merkle Tree implementations for secure whitelist management and token minting. Choose the approach that best fits your project's needs! |
| 104 | + |
0 commit comments