merkrs 0.3.0

Merkle tree library for Rust, compatible with OpenZeppelin's JavaScript implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! [`StandardMerkleTree`] — Merkle tree over ABI-encoded Solidity values.

use serde::{Deserialize, Serialize};

use crate::abi::compute_leaf_hash;
use crate::bytes::{Bytes32, decode_hex, encode_hex};
use crate::error::{Error, Result};
use crate::hashes::standard_node_hash;
use crate::merkle::{LeafHasher, MerkleTree, TreeParts, build_sorted_tree};
use crate::tree;

const FORMAT: &str = "standard-v1";

/// Leaf hashing strategy for standard trees (ABI encode + double `keccak256`).
#[derive(Debug, Clone)]
pub struct StandardHasher {
    leaf_encoding: Box<[String]>,
}

impl StandardHasher {
    /// The Solidity types used to ABI-encode each leaf.
    #[must_use]
    pub fn leaf_encoding(&self) -> &[String] {
        &self.leaf_encoding
    }
}

impl LeafHasher for StandardHasher {
    type Value = Vec<serde_json::Value>;

    fn hash_leaf(&self, value: &Self::Value) -> Result<Bytes32> {
        compute_leaf_hash(&self.leaf_encoding, value)
    }
}

/// A Merkle tree over ABI-encoded Solidity values.
///
/// Leaves are double-hashed (`keccak256(keccak256(abi.encode(...)))`) to
/// prevent second-preimage attacks, matching the `OpenZeppelin` convention.
pub type StandardMerkleTree = MerkleTree<StandardHasher>;

/// Serialisable snapshot of a [`StandardMerkleTree`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StandardMerkleTreeData {
    /// Format identifier (always `"standard-v1"`).
    pub format: String,
    /// Solidity types used to ABI-encode each leaf.
    pub leaf_encoding: Vec<String>,
    /// Flat tree array as hex strings (index 0 = root).
    pub tree: Vec<String>,
    /// Original values with their tree positions.
    pub values: Vec<ValueEntry>,
}

/// One value stored in a [`StandardMerkleTree`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ValueEntry {
    /// The ABI-typed values for this leaf.
    pub value: Vec<serde_json::Value>,
    /// Index of this leaf's hash in the flat tree array.
    pub tree_index: usize,
}

/// Options for constructing a [`StandardMerkleTree`].
#[derive(Debug, Clone, Copy)]
pub struct Options {
    /// Whether to sort leaves by hash before building the tree.
    pub sort_leaves: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self { sort_leaves: true }
    }
}

impl Options {
    /// Set the `sort_leaves` option.
    #[must_use]
    pub const fn with_sort_leaves(mut self, sort: bool) -> Self {
        self.sort_leaves = sort;
        self
    }
}

impl StandardMerkleTree {
    /// Build a tree from ABI-typed values.
    ///
    /// # Errors
    ///
    /// Returns [`Error::EmptyLeaves`] when `values` is empty, or
    /// propagates ABI-encoding errors.
    pub fn new(
        values: Vec<Vec<serde_json::Value>>,
        leaf_encoding: Vec<String>,
        options: Options,
    ) -> Result<Self> {
        if values.is_empty() {
            return Err(Error::EmptyLeaves);
        }

        let (tree, tree_indices) = build_sorted_tree(
            &values,
            |v| compute_leaf_hash(&leaf_encoding, v),
            options.sort_leaves,
            standard_node_hash,
        )?;

        Ok(Self::from_parts(TreeParts {
            hasher: StandardHasher {
                leaf_encoding: leaf_encoding.into_boxed_slice(),
            },
            tree,
            values,
            tree_indices,
            node_hash: standard_node_hash,
            custom_node_hash: false,
        }))
    }

    /// Reconstruct from a serialised snapshot.
    ///
    /// # Errors
    ///
    /// Returns [`Error::UnknownFormat`] or [`Error::MissingLeafEncoding`] on
    /// invalid input, or propagates hex-decode / validation errors.
    pub fn from_data(data: StandardMerkleTreeData) -> Result<Self> {
        if data.format != FORMAT {
            return Err(Error::UnknownFormat(data.format));
        }
        if data.leaf_encoding.is_empty() {
            return Err(Error::MissingLeafEncoding);
        }

        let tree: Vec<Bytes32> = data
            .tree
            .iter()
            .map(|s| decode_hex(s))
            .collect::<Result<_>>()?;

        let mut values = Vec::with_capacity(data.values.len());
        let mut tree_indices = Vec::with_capacity(data.values.len());
        for entry in data.values {
            tree_indices.push(entry.tree_index);
            values.push(entry.value);
        }

        let me = Self::from_parts(TreeParts {
            hasher: StandardHasher {
                leaf_encoding: data.leaf_encoding.into_boxed_slice(),
            },
            tree,
            values,
            tree_indices,
            node_hash: standard_node_hash,
            custom_node_hash: false,
        });
        me.validate()?;
        Ok(me)
    }

    /// Verify a single leaf against a known root without a tree instance.
    ///
    /// # Errors
    ///
    /// Propagates ABI-encoding errors.
    pub fn verify(
        root: &Bytes32,
        leaf_encoding: &[String],
        leaf: &[serde_json::Value],
        proof: &[Bytes32],
    ) -> Result<bool> {
        let hash = compute_leaf_hash(leaf_encoding, leaf)?;
        Ok(tree::process_proof(&hash, proof, standard_node_hash) == *root)
    }

    /// Verify a multi-proof against a known root without a tree instance.
    ///
    /// # Errors
    ///
    /// Propagates any multi-proof processing error.
    pub fn verify_multi_proof(root: &Bytes32, mp: &crate::MultiProof) -> Result<bool> {
        let computed = tree::process_multi_proof(mp, standard_node_hash)?;
        Ok(computed == *root)
    }

    /// Serialise to a JSON-friendly snapshot.
    #[must_use]
    pub fn to_data(&self) -> StandardMerkleTreeData {
        let tree_hex: Vec<String> = self.tree.iter().map(encode_hex).collect();
        let values: Vec<ValueEntry> = self
            .values
            .iter()
            .zip(&self.tree_indices)
            .map(|(v, &ti)| ValueEntry {
                value: v.clone(),
                tree_index: ti,
            })
            .collect();

        StandardMerkleTreeData {
            format: FORMAT.into(),
            leaf_encoding: self.hasher.leaf_encoding().to_vec(),
            tree: tree_hex,
            values,
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn airdrop_data(count: usize) -> Vec<Vec<serde_json::Value>> {
        (0..count)
            .map(|i| vec![json!(format!("0x{:040x}", i + 1)), json!((i + 1) * 100)])
            .collect()
    }

    fn encoding() -> Vec<String> {
        vec!["address".into(), "uint256".into()]
    }

    #[test]
    fn basic_construction() {
        let tree =
            StandardMerkleTree::new(airdrop_data(4), encoding(), Options::default()).unwrap();
        assert_eq!(tree.len(), 4);
        tree.validate().unwrap();
    }

    #[test]
    fn single_leaf() {
        let tree =
            StandardMerkleTree::new(airdrop_data(1), encoding(), Options::default()).unwrap();
        assert_eq!(tree.len(), 1);
        tree.validate().unwrap();
    }

    #[test]
    fn proof_roundtrip() {
        let values = airdrop_data(8);
        let tree = StandardMerkleTree::new(values.clone(), encoding(), Options::default()).unwrap();
        for (i, v) in values.iter().enumerate() {
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
            assert!(tree.verify_proof_by_index(i, &p).unwrap());
        }
    }

    #[test]
    fn static_verify() {
        let values = airdrop_data(4);
        let enc = encoding();
        let tree =
            StandardMerkleTree::new(values.clone(), enc.clone(), Options::default()).unwrap();
        for v in &values {
            let p = tree.proof(v).unwrap();
            assert!(StandardMerkleTree::verify(tree.root(), &enc, v, &p).unwrap());
        }
    }

    #[test]
    fn reject_invalid_proof() {
        let v1 = airdrop_data(4);
        let t1 = StandardMerkleTree::new(v1.clone(), encoding(), Options::default()).unwrap();
        let v2: Vec<_> = airdrop_data(4)
            .into_iter()
            .map(|mut v| {
                if let Some(slot) = v.get_mut(1) {
                    *slot = json!(9999);
                }
                v
            })
            .collect();
        let t2 = StandardMerkleTree::new(v2, encoding(), Options::default()).unwrap();
        let proof = t1.proof(v1.first().unwrap()).unwrap();
        assert!(!t2.verify_proof(v1.first().unwrap(), &proof).unwrap());
    }

    #[test]
    fn multiproof() {
        let tree =
            StandardMerkleTree::new(airdrop_data(8), encoding(), Options::default()).unwrap();
        let mp = tree.multi_proof_by_indices(&[0, 2, 5]).unwrap();
        assert_eq!(mp.leaves.len(), 3);
    }

    #[test]
    fn dump_and_load() {
        let tree =
            StandardMerkleTree::new(airdrop_data(4), encoding(), Options::default()).unwrap();
        let data = tree.to_data();
        assert_eq!(data.format, FORMAT);
        let json_str = serde_json::to_string(&data).unwrap();
        let loaded_data: StandardMerkleTreeData = serde_json::from_str(&json_str).unwrap();
        let loaded = StandardMerkleTree::from_data(loaded_data).unwrap();
        assert_eq!(tree.root(), loaded.root());
        assert_eq!(tree.len(), loaded.len());
    }

    #[test]
    fn entries_match_get() {
        let tree =
            StandardMerkleTree::new(airdrop_data(4), encoding(), Options::default()).unwrap();
        for (i, v) in tree.entries() {
            assert_eq!(Some(v), tree.get(i));
        }
        assert!(tree.get(tree.len()).is_none());
    }

    #[test]
    fn unsorted_leaves() {
        let values = airdrop_data(4);
        let tree = StandardMerkleTree::new(
            values.clone(),
            encoding(),
            Options::default().with_sort_leaves(false),
        )
        .unwrap();
        tree.validate().unwrap();
        for v in &values {
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
        }
    }

    #[test]
    fn various_types() {
        let values = vec![
            vec![
                json!("0x1111111111111111111111111111111111111111"),
                json!(100u64),
            ],
            vec![
                json!("0x2222222222222222222222222222222222222222"),
                json!(200u64),
            ],
        ];
        let tree = StandardMerkleTree::new(values.clone(), encoding(), Options::default()).unwrap();
        for v in &values {
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
        }
    }

    #[test]
    fn bytes32_type() {
        let values = vec![
            vec![
                json!("0x1111111111111111111111111111111111111111111111111111111111111111"),
                json!(100u64),
            ],
            vec![
                json!("0x2222222222222222222222222222222222222222222222222222222222222222"),
                json!(200u64),
            ],
        ];
        let tree = StandardMerkleTree::new(
            values.clone(),
            vec!["bytes32".into(), "uint256".into()],
            Options::default(),
        )
        .unwrap();
        for v in &values {
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
        }
    }

    #[test]
    fn uint_types() {
        let values = vec![
            vec![json!(100u64), json!(200u64), json!(50u64)],
            vec![json!(300u64), json!(400u64), json!(60u64)],
        ];
        let tree = StandardMerkleTree::new(
            values.clone(),
            vec!["uint256".into(), "uint128".into(), "uint64".into()],
            Options::default(),
        )
        .unwrap();
        for v in &values {
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
        }
    }

    #[test]
    fn unknown_format_rejected() {
        let data = StandardMerkleTreeData {
            format: "bad".into(),
            leaf_encoding: vec!["uint256".into()],
            tree: vec![],
            values: vec![],
        };
        assert!(matches!(
            StandardMerkleTree::from_data(data),
            Err(Error::UnknownFormat(_))
        ));
    }

    #[test]
    fn out_of_bounds() {
        let tree =
            StandardMerkleTree::new(airdrop_data(4), encoding(), Options::default()).unwrap();
        assert!(matches!(
            tree.proof_by_index(100),
            Err(Error::IndexOutOfBounds { .. })
        ));
    }

    #[test]
    fn leaf_not_found() {
        let tree =
            StandardMerkleTree::new(airdrop_data(4), encoding(), Options::default()).unwrap();
        let bad = vec![
            json!("0x9999999999999999999999999999999999999999"),
            json!(9999),
        ];
        assert!(matches!(tree.proof(&bad), Err(Error::LeafNotFound)));
    }

    #[test]
    fn large_tree() {
        let values = airdrop_data(100);
        let tree = StandardMerkleTree::new(values.clone(), encoding(), Options::default()).unwrap();
        assert_eq!(tree.len(), 100);
        tree.validate().unwrap();
        for i in [0, 25, 50, 75, 99] {
            let v = values.get(i).unwrap();
            let p = tree.proof(v).unwrap();
            assert!(tree.verify_proof(v, &p).unwrap());
        }
    }
}