winter_crypto/
errors.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree.
5
6use core::fmt;
7
8// MERKLE TREE ERROR
9// ================================================================================================
10
11/// Defines errors which can occur when using Merkle trees.
12#[derive(Debug, PartialEq, Eq)]
13pub enum MerkleTreeError {
14    /// Fewer than two leaves were used to construct a Merkle tree.
15    TooFewLeaves(usize, usize),
16    /// Number of leaves for a Merkle tree was not a power of two.
17    NumberOfLeavesNotPowerOfTwo(usize),
18    /// A leaf index was greater than or equal to the number of leaves in the tree.
19    LeafIndexOutOfBounds(usize, usize),
20    /// A leaf index was included more than once in the list of indexes for a batch proof.
21    DuplicateLeafIndex,
22    /// No leaf indexes were provided for a batch Merkle proof.
23    TooFewLeafIndexes,
24    /// Too many leaf index were provided for a batch Merkle proof.
25    TooManyLeafIndexes(usize, usize),
26    /// Merkle proof is not valid for the specified position(s).
27    InvalidProof,
28}
29
30impl fmt::Display for MerkleTreeError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::TooFewLeaves(expected, actual) => {
34                write!(
35                    f,
36                    "a Merkle tree must contain at least {expected} leaves, but {actual} were provided"
37                )
38            },
39            Self::NumberOfLeavesNotPowerOfTwo(num_leaves) => {
40                write!(f, "number of leaves must be a power of two, but {num_leaves} were provided")
41            },
42            Self::LeafIndexOutOfBounds(expected, actual) => {
43                write!(f, "a leaf index cannot exceed {expected}, but was {actual}")
44            },
45            Self::DuplicateLeafIndex => {
46                write!(f, "repeating indexes detected")
47            },
48            Self::TooFewLeafIndexes => {
49                write!(f, "at least one leaf index must be provided")
50            },
51            Self::TooManyLeafIndexes(max_indexes, num_indexes) => {
52                write!(
53                    f,
54                    "number of leaf indexes cannot exceed {max_indexes}, but {num_indexes} was provided"
55                )
56            },
57            Self::InvalidProof => {
58                write!(f, "Merkle proof is invalid")
59            },
60        }
61    }
62}
63
64impl core::error::Error for MerkleTreeError {}
65
66// RANDOM COIN ERROR
67// ================================================================================================
68
69/// Defines errors which can occur when drawing values from a random coin.
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub enum RandomCoinError {
72    /// A valid element could not be drawn from the field after the specified number of tries.
73    FailedToDrawFieldElement(usize),
74    /// The required number of integer values could not be drawn from the specified domain after
75    /// the specified number of tries.
76    FailedToDrawIntegers(usize, usize, usize),
77}
78
79impl fmt::Display for RandomCoinError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::FailedToDrawFieldElement(num_tries) => {
83                write!(f, "failed to generate a valid field element after {num_tries} tries")
84            },
85            Self::FailedToDrawIntegers(num_expected, num_actual, num_tries) => {
86                write!(
87                    f,
88                    "needed to draw {num_expected} integers from a domain, but drew only {num_actual} after {num_tries} tries"
89                )
90            },
91        }
92    }
93}
94
95impl core::error::Error for RandomCoinError {}