merkletree_mintlayer/merkle/
mod.rs

1// Copyright (c) 2021-2024 RBB S.r.l
2// opensource@mintlayer.org
3// SPDX-License-Identifier: MIT
4// Licensed under the MIT License;
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://github.com/mintlayer/merkletree-mintlayer/blob/master/LICENSE
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16pub mod hasher;
17pub mod pos;
18pub mod proof;
19pub mod tree;
20
21#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
22pub enum MerkleTreeFormError {
23    #[error("Merkle tree input too small: {0}")]
24    TooSmall(usize),
25}
26
27#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
28pub enum MerkleTreeProofExtractionError {
29    #[error("No leaves were provided to create a proof")]
30    NoLeavesToCreateProof,
31    #[error("One or more indexes are larger than the number of leaves in the tree: {0:?} vs leaf-count {1}")]
32    IndexOutOfRange(Vec<u32>, u32),
33    #[error("Leaf index out of range: {0} vs leaves count {1}")]
34    LeafIndexOutOfRange(u32, u32),
35    #[error("Leaves indices must be sorted in ascending: {0:?}")]
36    UnsortedOrUniqueLeavesIndices(Vec<u32>),
37    #[error("Access error: {0}")]
38    AccessError(#[from] MerkleTreeAccessError),
39}
40
41#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
42pub enum MerkleTreeAccessError {
43    #[error("Invalid tree size provided is invalid: {0}")]
44    InvalidTreeSize(usize),
45    #[error("Invalid initial index for leaf in iterator. Provided {0} vs tree size {1}")]
46    AbsIndexOutOfRange(u32, u32),
47    #[error("Invalid initial index for leaf in iterator. Provided {0} vs size {1}")]
48    IterStartIndexOutOfRange(u32, u32),
49}
50
51#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
52pub enum MerkleProofVerificationError {
53    #[error("No leaves provided")]
54    LeavesContainerProvidedIsEmpty,
55    #[error("Invalid tree size")]
56    InvalidTreeLeavesCount(u32),
57    #[error("One or more leaves have indices out of range: {0:?} vs leaves count {1}")]
58    LeavesIndicesOutOfRange(Vec<u32>, u32),
59    #[error("One or more nodes have indices out of range: {0:?} vs tree size {1}")]
60    NodesIndicesOutOfRange(Vec<u32>, u32),
61    #[error("A required node is missing. Index of node: {0}")]
62    RequiredNodeMissing(u32),
63    #[error("Tree size arithmetic error: {0}")]
64    TreeSizeArithmeticError(u32),
65}