fuel_merkle/binary/
verify.rs

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
use crate::{
    binary::{
        leaf_sum,
        node_sum,
    },
    common::{
        Bytes32,
        ProofSet,
    },
};

/// Returns None if:
/// - `num_leaves` is 0
/// - the result doens't fit in an usize
fn path_length_from_key(key: u64, num_leaves: u64) -> Option<usize> {
    if num_leaves == 0 {
        return None;
    }

    #[allow(clippy::arithmetic_side_effects)] // ilog2(..) < 64
    let path_length = if num_leaves.is_power_of_two() {
        num_leaves.ilog2()
    } else {
        num_leaves.ilog2() + 1
    };

    #[allow(clippy::arithmetic_side_effects)] // ilog2(..) > 0
    let num_leaves_left_subtree = 1 << (path_length - 1);

    let subtree_leaves = num_leaves.saturating_sub(num_leaves_left_subtree);

    let Some(subtree_key) = key.checked_sub(num_leaves_left_subtree) else {
        // If leaf is in left subtree, path length is full height of left subtree
        return path_length.try_into().ok();
    };

    // Otherwise, if left or right subtree has only one leaf, path has one additional step
    if num_leaves_left_subtree == 1 || subtree_leaves <= 1 {
        return Some(1);
    }

    // Otherwise, add 1 to height and recurse into right subtree
    path_length_from_key(subtree_key, subtree_leaves)?.checked_add(1)
}

pub fn verify<T: AsRef<[u8]>>(
    root: &Bytes32,
    data: &T,
    proof_set: &ProofSet,
    proof_index: u64,
    num_leaves: u64,
) -> bool {
    if num_leaves <= 1 {
        if !proof_set.is_empty() {
            return false;
        }
    } else if Some(proof_set.len()) != path_length_from_key(proof_index, num_leaves) {
        return false;
    }

    if proof_index >= num_leaves {
        return false;
    }

    let mut sum = leaf_sum(data.as_ref());
    if proof_set.is_empty() {
        return if num_leaves == 1 { *root == sum } else { false }
    }
    #[allow(clippy::arithmetic_side_effects)] // checked above
    let last_leaf = num_leaves - 1;

    let mut parent = 0usize;
    let mut stable_end = proof_index;

    loop {
        #[allow(clippy::arithmetic_side_effects)] // path_length_from_key checks
        let height = parent + 1;

        let subtree_size = 1u64 << height;
        #[allow(clippy::arithmetic_side_effects)] // floor(a / b) * b <= a
        let subtree_start_index = proof_index / subtree_size * subtree_size;
        #[allow(clippy::arithmetic_side_effects)]
        let subtree_end_index = subtree_start_index + subtree_size - 1;

        if subtree_end_index >= num_leaves {
            break
        }

        stable_end = subtree_end_index;

        if proof_set.len() < height {
            return false
        }

        let proof_data = proof_set[parent];
        #[allow(clippy::arithmetic_side_effects)] // proof_index > subtree_start_index
        if proof_index - subtree_start_index < (1 << parent) {
            sum = node_sum(&sum, &proof_data);
        } else {
            sum = node_sum(&proof_data, &sum);
        }

        #[allow(clippy::arithmetic_side_effects)] // path_length_from_key checks
        {
            parent += 1;
        }
    }

    if stable_end != last_leaf {
        if proof_set.len() <= parent {
            return false
        }
        let proof_data = proof_set[parent];
        sum = node_sum(&sum, &proof_data);
        #[allow(clippy::arithmetic_side_effects)] // path_length_from_key checks
        {
            parent += 1;
        }
    }

    while parent < proof_set.len() {
        let proof_data = proof_set[parent];
        sum = node_sum(&proof_data, &sum);
        #[allow(clippy::arithmetic_side_effects)] // path_length_from_key checks
        {
            parent += 1;
        }
    }

    sum == *root
}

#[cfg(test)]
mod test {
    use super::verify;
    use crate::{
        binary::{
            MerkleTree,
            Primitive,
        },
        common::StorageMap,
    };
    use fuel_merkle_test_helpers::TEST_DATA;
    use fuel_storage::Mappable;

    #[derive(Debug)]
    struct TestTable;

    impl Mappable for TestTable {
        type Key = Self::OwnedKey;
        type OwnedKey = u64;
        type OwnedValue = Primitive;
        type Value = Self::OwnedValue;
    }

    #[test]
    fn verify_returns_true_when_the_given_proof_set_matches_the_given_merkle_root() {
        let mut storage_map = StorageMap::<TestTable>::new();
        let mut tree = MerkleTree::new(&mut storage_map);

        const PROOF_INDEX: usize = 2;
        const LEAVES_COUNT: usize = 5;

        let data = &TEST_DATA[0..LEAVES_COUNT]; // 5 leaves
        for datum in data.iter() {
            tree.push(datum).unwrap();
        }

        let (root, proof_set) = tree.prove(PROOF_INDEX as u64).unwrap();
        let verification = verify(
            &root,
            &TEST_DATA[PROOF_INDEX],
            &proof_set,
            PROOF_INDEX as u64,
            LEAVES_COUNT as u64,
        );
        assert!(verification);
    }

    #[test]
    fn verify_returns_false_when_the_given_proof_set_does_not_match_the_given_merkle_root(
    ) {
        // Check the Merkle root of one tree against the computed Merkle root of
        // another tree's proof set: because the two roots come from different
        // trees, the comparison should fail.

        // Generate the first Merkle tree and get its root
        let mut storage_map = StorageMap::<TestTable>::new();
        let mut tree = MerkleTree::new(&mut storage_map);

        const PROOF_INDEX: usize = 2;
        const LEAVES_COUNT: usize = 5;

        let data = &TEST_DATA[0..LEAVES_COUNT - 1];
        for datum in data.iter() {
            tree.push(datum).unwrap();
        }
        let proof = tree.prove(PROOF_INDEX as u64).unwrap();
        let root = proof.0;

        // Generate the second Merkle tree and get its proof set
        let mut storage_map = StorageMap::<TestTable>::new();
        let mut tree = MerkleTree::new(&mut storage_map);

        let data = &TEST_DATA[5..10];
        for datum in data.iter() {
            tree.push(datum).unwrap();
        }
        let proof = tree.prove(PROOF_INDEX as u64).unwrap();
        let set = proof.1;

        let verification = verify(
            &root,
            &TEST_DATA[PROOF_INDEX],
            &set,
            PROOF_INDEX as u64,
            LEAVES_COUNT as u64,
        );
        assert!(!verification);
    }

    #[test]
    fn verify_returns_false_when_the_proof_set_is_empty() {
        const PROOF_INDEX: usize = 0;
        const LEAVES_COUNT: usize = 0;

        let verification = verify(
            &Default::default(),
            &TEST_DATA[PROOF_INDEX],
            &vec![],
            PROOF_INDEX as u64,
            LEAVES_COUNT as u64,
        );
        assert!(!verification);
    }

    #[test]
    fn verify_returns_false_when_the_proof_index_is_invalid() {
        let mut storage_map = StorageMap::<TestTable>::new();
        let mut tree = MerkleTree::new(&mut storage_map);

        const PROOF_INDEX: usize = 0;
        const LEAVES_COUNT: usize = 5;

        let data = &TEST_DATA[0..LEAVES_COUNT - 1];
        for datum in data.iter() {
            tree.push(datum).unwrap();
        }

        let proof = tree.prove(PROOF_INDEX as u64).unwrap();
        let root = proof.0;
        let set = proof.1;

        let verification = verify(
            &root,
            &TEST_DATA[PROOF_INDEX],
            &set,
            PROOF_INDEX as u64 + 15,
            LEAVES_COUNT as u64,
        );
        assert!(!verification);
    }
}