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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
use core::fmt;
// MERKLE TREE ERROR
// ================================================================================================
/// Defines errors which can occur when using Merkle trees.
#[derive(Debug, PartialEq, Eq)]
pub enum MerkleTreeError {
/// Fewer than two leaves were used to construct a Merkle tree.
TooFewLeaves(usize, usize),
/// Number of leaves for a Merkle tree was not a power of two.
NumberOfLeavesNotPowerOfTwo(usize),
/// A leaf index was greater than or equal to the number of leaves in the tree.
LeafIndexOutOfBounds(usize, usize),
/// A leaf index was included more than once in the list of indexes for a batch proof.
DuplicateLeafIndex,
/// No leaf indexes were provided for a batch Merkle proof.
TooFewLeafIndexes,
/// Too many leaf index were provided for a batch Merkle proof.
TooManyLeafIndexes(usize, usize),
/// Merkle proof is not valid for the specified position(s).
InvalidProof,
}
impl fmt::Display for MerkleTreeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooFewLeaves(expected, actual) => {
write!(
f,
"a Merkle tree must contain at least {expected} leaves, but {actual} were provided"
)
}
Self::NumberOfLeavesNotPowerOfTwo(num_leaves) => {
write!(
f,
"number of leaves must be a power of two, but {num_leaves} were provided"
)
}
Self::LeafIndexOutOfBounds(expected, actual) => {
write!(f, "a leaf index cannot exceed {expected}, but was {actual}")
}
Self::DuplicateLeafIndex => {
write!(f, "repeating indexes detected")
}
Self::TooFewLeafIndexes => {
write!(f, "at least one leaf index must be provided")
}
Self::TooManyLeafIndexes(max_indexes, num_indexes) => {
write!(
f,
"number of leaf indexes cannot exceed {max_indexes}, but {num_indexes} was provided"
)
}
Self::InvalidProof => {
write!(f, "Merkle proof is invalid")
}
}
}
}
// RANDOM COIN ERROR
// ================================================================================================
/// Defines errors which can occur when drawing values from a random coin.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RandomCoinError {
/// A valid element could not be drawn from the field after the specified number of tries.
FailedToDrawFieldElement(usize),
/// The required number of integer values could not be drawn from the specified domain after
/// the specified number of tries.
FailedToDrawIntegers(usize, usize, usize),
}
impl fmt::Display for RandomCoinError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FailedToDrawFieldElement(num_tries) => {
write!(
f,
"failed to generate a valid field element after {num_tries} tries"
)
}
Self::FailedToDrawIntegers(num_expected, num_actual, num_tries) => {
write!(
f,
"needed to draw {num_expected} integers from a domain, but drew only {num_actual} after {num_tries} tries"
)
}
}
}
}