fastcrypto 0.1.11

Common cryptographic library used at Mysten Labs
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! A simple Merkle tree implementation.

extern crate alloc;

use alloc::{format, vec::Vec};
use core::{fmt::Debug, marker::PhantomData};

use crate::error::{FastCryptoError, FastCryptoResult};
use crate::hash::{Blake2b256, Digest, HashFunction};
use serde::{Deserialize, Serialize};

/// The length of the digests used in the merkle tree.
pub const DIGEST_LEN: usize = 32;

pub const LEAF_PREFIX: [u8; 1] = [0];
pub const INNER_PREFIX: [u8; 1] = [1];
pub const EMPTY_NODE: [u8; DIGEST_LEN] = [0; DIGEST_LEN];

/// A node in the Merkle tree.
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub enum Node {
    /// A node with an empty subtree.
    Empty,
    /// A node with children, hash value of length 32 bytes.
    Digest([u8; DIGEST_LEN]),
}

impl Node {
    /// Get the byte representation of [`self`].
    pub fn bytes(&self) -> [u8; DIGEST_LEN] {
        match self {
            Node::Empty => EMPTY_NODE,
            Node::Digest(val) => *val,
        }
    }
}

impl From<Digest<DIGEST_LEN>> for Node {
    fn from(value: Digest<DIGEST_LEN>) -> Self {
        Self::Digest(value.digest)
    }
}

impl From<[u8; DIGEST_LEN]> for Node {
    fn from(value: [u8; DIGEST_LEN]) -> Self {
        Self::Digest(value)
    }
}

impl AsRef<[u8]> for Node {
    fn as_ref(&self) -> &[u8] {
        match self {
            Node::Empty => EMPTY_NODE.as_ref(),
            Node::Digest(val) => val.as_ref(),
        }
    }
}

/// A proof that some data is at index `leaf_index` in a [`MerkleTree`].
#[derive(Serialize, Deserialize)]
pub struct MerkleProof<T = Blake2b256> {
    _hash_type: PhantomData<T>,
    /// The sibling hash values on the path from the leaf to the root.
    path: Vec<Node>,
}

// Cannot be derived as many hash functions don't implement `Clone` and the derive is not smart
// enough to see that it is not necessary.
impl<T> Clone for MerkleProof<T> {
    fn clone(&self) -> Self {
        Self {
            _hash_type: PhantomData,
            path: self.path.clone(),
        }
    }
}

// Cannot be derived as many hash functions don't implement `Debug` and the derive is not smart
// enough to see that it is not necessary.
impl<T> core::fmt::Debug for MerkleProof<T> {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.debug_struct(&format!("MerkleProof<{}>", core::any::type_name::<T>()))
            .field("path", &self.path)
            .finish()
    }
}

impl<T> PartialEq for MerkleProof<T> {
    fn eq(&self, other: &Self) -> bool {
        self.path.eq(&other.path)
    }
}

impl Eq for MerkleProof {}

impl<T> MerkleProof<T>
where
    T: HashFunction<DIGEST_LEN>,
{
    /// Construct Merkle proof from list of hashes and leaf index.
    pub fn new(path: &[Node]) -> Self {
        Self {
            _hash_type: PhantomData,
            path: path.into(),
        }
    }

    /// Verifies the proof given a Merkle root and the leaf data.
    pub fn verify_proof(
        &self,
        root: &Node,
        leaf: &[u8],
        leaf_index: usize,
    ) -> FastCryptoResult<()> {
        if self.compute_root(leaf, leaf_index).as_ref() != Some(root) {
            return Err(FastCryptoError::InvalidProof);
        }
        Ok(())
    }

    pub fn verify_proof_with_unserialized_leaf<L: Serialize>(
        &self,
        root: &Node,
        leaf: &L,
        leaf_index: usize,
    ) -> FastCryptoResult<()> {
        let bytes = bcs::to_bytes(leaf).map_err(|_| FastCryptoError::InvalidInput)?;
        self.verify_proof(root, &bytes, leaf_index)
    }

    /// Recomputes the Merkle root from the proof and the provided leaf data.
    ///
    /// Returns `None` if the provided index is too large.
    pub fn compute_root(&self, leaf: &[u8], leaf_index: usize) -> Option<Node> {
        if leaf_index
            .checked_shr(self.path.len() as u32)
            .is_none_or(|s| s != 0)
        {
            return None;
        }
        let mut current_hash = leaf_hash::<T>(leaf);
        let mut level_index = leaf_index;
        for sibling in self.path.iter() {
            // The sibling hash of the current node
            if level_index.is_multiple_of(2) {
                // The current node is a left child
                current_hash = inner_hash::<T>(&current_hash, sibling);
            } else {
                // The current node is a right child
                current_hash = inner_hash::<T>(sibling, &current_hash);
            };
            // Update to the level index one level up in the tree
            level_index /= 2;
        }
        Some(current_hash)
    }

    // Check if the proof is for the rightmost leaf in the tree
    pub fn is_right_most(&self, leaf_index: usize) -> bool {
        let mut level_index = leaf_index;
        for sibling in self.path.iter() {
            // The sibling hash of the current node
            if level_index.is_multiple_of(2) {
                // The current node is a left child
                if sibling.as_ref() != EMPTY_NODE.as_ref() {
                    return false;
                }
            }
            // Update to the level index one level up in the tree
            level_index /= 2;
        }
        true
    }

    /// The length of the proof, aka the number of sibling hashes on the path from the leaf to the root.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.path.len()
    }
}

/// A proof that some leaf is not in a Merkle tree.
/// To be used *only* when the tree is built over sorted leaves.
///
/// The proof contains:
/// - index: where the target leaf would be inserted in the sorted leaves.
/// - left_leaf, right_leaf: inclusion proofs for neighboring leaves at index - 1 & index.
///
/// The serde bound is needed because Rust is not smart enough to see that T does not need to be serialized
/// (see `test_serialization_with_blake2b256` which fails without the bound).
#[derive(Serialize, Deserialize)]
#[serde(bound(
    serialize = "L: Serialize",
    deserialize = "L: serde::de::DeserializeOwned"
))]
pub struct MerkleNonInclusionProof<L, T = Blake2b256>
where
    L: Ord + Serialize,
{
    pub index: usize,
    pub left_leaf: Option<(L, MerkleProof<T>)>,
    pub right_leaf: Option<(L, MerkleProof<T>)>,
}

impl<L, T> MerkleNonInclusionProof<L, T>
where
    T: HashFunction<DIGEST_LEN>,
    L: Ord + Serialize,
{
    pub fn new(
        left_leaf: Option<(L, MerkleProof<T>)>,
        right_leaf: Option<(L, MerkleProof<T>)>,
        index: usize,
    ) -> Self {
        Self {
            left_leaf,
            right_leaf,
            index,
        }
    }
}

impl<L, T> core::fmt::Debug for MerkleNonInclusionProof<L, T>
where
    L: Debug + Ord + Serialize,
    T: HashFunction<DIGEST_LEN>,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct(&format!(
            "MerkleNonInclusionProof<L={}, T={}>",
            std::any::type_name::<L>(),
            std::any::type_name::<T>()
        ))
        .field("left_leaf", &self.left_leaf)
        .field("right_leaf", &self.right_leaf)
        .field("index", &self.index)
        .finish()
    }
}

/// Non inclusion proof verification implemented using the Serialize trait.
impl<L, T> MerkleNonInclusionProof<L, T>
where
    T: HashFunction<DIGEST_LEN>,
    L: Ord + Serialize,
{
    fn is_valid_neighbor(
        &self,
        neighbor: &L,
        proof: &MerkleProof<T>,
        neighbor_index: usize,
        root: &Node,
    ) -> FastCryptoResult<()> {
        proof.verify_proof_with_unserialized_leaf(root, neighbor, neighbor_index)
    }

    // Proves non-inclusion of a leaf assuming sorted tree.
    // Edge case explanations
    // - Empty tree: no leaves, automatically valid
    // - Target leaf smaller than all: no left neighbor, right neighbor must be at position 0
    // - Target leaf larger than all: left neighbor must be rightmost leaf
    pub fn verify_proof(&self, root: &Node, target_leaf: &L) -> FastCryptoResult<()> {
        // Note: For empty trees, we don't need to check anything
        if root.as_ref() == EMPTY_NODE.as_ref() {
            return Ok(());
        }

        let right_leaf_index = self.index;

        // left_leaf_with_idx is None if either left_leaf is None or if index is zero
        let left_leaf_with_idx = self.left_leaf.as_ref().zip(self.index.checked_sub(1));

        if let Some(((left_leaf, left_proof), left_leaf_index)) = &left_leaf_with_idx {
            // Check that the left leaf is a valid neighbor
            self.is_valid_neighbor(left_leaf, left_proof, *left_leaf_index, root)?;
            // Check that the left leaf is less than the target leaf
            if left_leaf >= target_leaf {
                return Err(FastCryptoError::InvalidProof);
            }
            // Milestone: If left leaf is present, then left_leaf < target_leaf
        } else if right_leaf_index != 0 || self.right_leaf.is_none() {
            return Err(FastCryptoError::InvalidProof);
            // Milestone: If left leaf is not present, then right leaf must be present with index 0.
        }

        if let Some((right_leaf, right_proof)) = &self.right_leaf {
            // Check that the right leaf is a valid neighbor
            self.is_valid_neighbor(right_leaf, right_proof, right_leaf_index, root)?;
            // Check that the right leaf is greater than the target leaf
            if right_leaf <= target_leaf {
                return Err(FastCryptoError::InvalidProof);
            }

            // Milestone: If right leaf is present, then right_leaf > target_leaf
        } else if let Some(((_, left_proof), left_leaf_index)) = left_leaf_with_idx {
            if !left_proof.is_right_most(left_leaf_index) {
                return Err(FastCryptoError::InvalidProof);
            }
            // Milestone: If right leaf is not present, then left leaf must be present and be the rightmost leaf
        } else {
            return Err(FastCryptoError::InvalidProof);
        }

        Ok(())
    }
}

/// Merkle tree using a hash function `T` (default: [`Blake2b256`]) from the [`fastcrypto`] crate.
///
/// The data of the leaves is prefixed with `0x00` before hashing and hashes of inner nodes are
/// computed over the concatenation of their children prefixed with `0x01`. Hashes of empty
/// subtrees (i.e. subtrees without data at their leaves) are replaced with all zeros.
#[derive(Serialize, Deserialize)]
pub struct MerkleTree<T = Blake2b256> {
    _hash_type: PhantomData<T>,
    // The nodes of the Merkle tree are stored in a vector level by level starting with
    // the leaf hashes.
    nodes: Vec<Node>,
    n_leaves: usize,
}

impl<T> core::fmt::Debug for MerkleTree<T> {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.debug_struct(&format!("MerkleTree<{}>", core::any::type_name::<T>()))
            .field("nodes", &self.nodes)
            .field("n_leaves", &self.n_leaves)
            .finish()
    }
}

impl<T> MerkleTree<T>
where
    T: HashFunction<DIGEST_LEN>,
{
    /// Create the [`MerkleTree`] as a commitment to the provided data.
    pub fn build_from_serialized<I>(iter: I) -> Self
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator,
        I::Item: AsRef<[u8]>,
    {
        let leaf_hashes = iter
            .into_iter()
            .map(|leaf| leaf_hash::<T>(leaf.as_ref()))
            .collect::<Vec<_>>();
        Self::build_from_leaf_hashes(leaf_hashes)
    }

    /// Create the [`MerkleTree`] as a commitment to the provided data.
    /// The data is serialized using BCS and then hashed to produce the leaf hashes.
    /// Note: Sometimes implementing AsRef<[u8]> makes the calling code more complex.
    /// In those cases, prefer this method over build_from_serialized.
    /// On the other hand, we pay the cost of serializing the leaves multiple times.
    pub fn build_from_unserialized<I>(iter: I) -> FastCryptoResult<Self>
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator,
        I::Item: Serialize,
    {
        let leaf_hashes = iter
            .into_iter()
            .map(|leaf| {
                bcs::to_bytes(&leaf)
                    .map_err(|_| FastCryptoError::InvalidInput)
                    .map(|bytes| leaf_hash::<T>(&bytes))
            })
            .collect::<FastCryptoResult<Vec<_>>>()?;
        Ok(Self::build_from_leaf_hashes(leaf_hashes))
    }

    /// Create the [`MerkleTree`] as a commitment to the provided data hashes.
    fn build_from_leaf_hashes<I>(iter: I) -> Self
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator<Item = Node>,
    {
        let iter = iter.into_iter();

        // Create the capacity that we know will be needed, since the vec will be
        // reused by the call to from_leaf_nodes.
        let mut nodes = Vec::with_capacity(n_nodes(iter.len()));
        nodes.extend(iter);

        let n_leaves = nodes.len();
        let mut level_nodes = n_leaves;
        let mut prev_level_index = 0;

        // Fill all other nodes of the Merkle Tree
        while level_nodes > 1 {
            if level_nodes % 2 == 1 {
                // We need an empty sibling for the last node on the previous level
                nodes.push(Node::Empty);
                level_nodes += 1;
            }

            let new_level_index = prev_level_index + level_nodes;

            (prev_level_index..new_level_index)
                .step_by(2)
                .for_each(|index| nodes.push(inner_hash::<T>(&nodes[index], &nodes[index + 1])));

            prev_level_index = new_level_index;
            level_nodes /= 2;
        }

        Self {
            nodes,
            n_leaves,
            _hash_type: PhantomData,
        }
    }

    /// Verify that the root of `self` matches the provided root hash.
    pub fn verify_root(&self, root: &Node) -> bool {
        self.root() == *root
    }

    /// Get a copy of the root hash of `self`.
    pub fn root(&self) -> Node {
        self.nodes.last().map_or(Node::Empty, |val| val.clone())
    }

    /// Get the [`MerkleProof`] for the leaf at `leaf_index` consisting
    /// of all sibling hashes on the path from the leaf to the root.
    pub fn get_proof(&self, leaf_index: usize) -> FastCryptoResult<MerkleProof<T>> {
        if leaf_index >= self.n_leaves {
            return Err(FastCryptoError::GeneralError(format!(
                "Leaf index out of bounds: {}",
                leaf_index
            )));
        }
        let mut path = Vec::with_capacity(
            usize::try_from(self.n_leaves.ilog2()).expect("this is smaller than `n_leaves`") + 1,
        );
        let mut level_index = leaf_index;
        let mut n_level = self.n_leaves;
        let mut level_base_index = 0;
        while n_level > 1 {
            // All levels contain an even number of nodes
            n_level = n_level.next_multiple_of(2);
            let sibling_index = if level_index.is_multiple_of(2) {
                level_base_index + level_index + 1
            } else {
                level_base_index + level_index - 1
            };
            path.push(self.nodes[sibling_index].clone());
            // Index of the parent on the next level
            level_index /= 2;
            level_base_index += n_level;
            n_level /= 2;
        }
        Ok(MerkleProof {
            _hash_type: PhantomData,
            path,
        })
    }

    /// Compute the non-inclusion proof for the target leaf.
    /// Returns an error if the target leaf is already in the tree.
    pub fn compute_non_inclusion_proof<L: Ord + Serialize + Clone>(
        &self,
        leaves: &[L],
        target_leaf: &L,
    ) -> FastCryptoResult<MerkleNonInclusionProof<L, T>> {
        let position = leaves.partition_point(|x| x <= target_leaf);
        if position > 0 && leaves[position - 1] == *target_leaf {
            return Err(FastCryptoError::GeneralError(
                "Target leaf is already in the tree".to_string(),
            ));
        }

        let left_leaf_proof = if position > 0 {
            Some((leaves[position - 1].clone(), self.get_proof(position - 1)?))
        } else {
            None
        };

        let right_leaf_proof = if position < leaves.len() {
            Some((leaves[position].clone(), self.get_proof(position)?))
        } else {
            None
        };

        Ok(MerkleNonInclusionProof::new(
            left_leaf_proof,
            right_leaf_proof,
            position,
        ))
    }
}

/// Computes the hash of the provided input to be used as a leaf hash of a Merkle tree.
pub(crate) fn leaf_hash<T>(input: &[u8]) -> Node
where
    T: HashFunction<DIGEST_LEN>,
{
    let mut hash_fun = T::default();
    hash_fun.update(LEAF_PREFIX);
    hash_fun.update(input);
    hash_fun.finalize().into()
}

fn inner_hash<T>(left: &Node, right: &Node) -> Node
where
    T: HashFunction<DIGEST_LEN>,
{
    let mut hash_fun = T::default();
    hash_fun.update(INNER_PREFIX);
    hash_fun.update(left.bytes());
    hash_fun.update(right.bytes());
    hash_fun.finalize().into()
}

pub(crate) fn n_nodes(n_leaves: usize) -> usize {
    let mut lvl_nodes = n_leaves;
    let mut tot_nodes = 0;
    while lvl_nodes > 1 {
        lvl_nodes += lvl_nodes % 2;
        tot_nodes += lvl_nodes;
        lvl_nodes /= 2;
    }
    tot_nodes + lvl_nodes
}