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
//! When something goes wrong

#[cfg(feature = "std")]
use std::error::Error;

use core::fmt;

/// Indicates that provided integer (usually encoded as bytes) can't be a valid coordinate of a point on curve `E`
#[derive(Debug, Clone, Copy)]
pub struct InvalidCoordinate;

impl fmt::Display for InvalidCoordinate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid coordinate")
    }
}

#[cfg(feature = "std")]
impl Error for InvalidCoordinate {}

/// Indicates that point is not valid
#[derive(Debug, Clone, Copy)]
pub struct InvalidPoint;

impl fmt::Display for InvalidPoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid point")
    }
}

#[cfg(feature = "std")]
impl Error for InvalidPoint {}

/// Indicates that scalar is not valid
#[derive(Debug, Clone, Copy)]
pub struct InvalidScalar;

impl fmt::Display for InvalidScalar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid scalar")
    }
}

#[cfg(feature = "std")]
impl Error for InvalidScalar {}

/// Indicates that [hash_to_curve](crate::hash_to_curve) primitive returned error
#[derive(Debug, Clone, Copy)]
pub struct HashError(pub(crate) HashErrorReason);

impl fmt::Display for HashError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            HashErrorReason::HashFailed => {
                f.write_str("couldn't perform hash to curve/scalar operation")
            }
            HashErrorReason::ProducedValueInvalid => {
                f.write_str("hash to curve/scalar produced invalid point/scalar")
            }
        }
    }
}

#[cfg(feature = "std")]
impl Error for HashError {}

#[derive(Debug, Clone, Copy)]
pub(crate) enum HashErrorReason {
    HashFailed,
    ProducedValueInvalid,
}

/// Appeared zero point is not expected/accepted
#[derive(Debug, Clone, Copy)]
pub struct ZeroPoint;

impl fmt::Display for ZeroPoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("zero point")
    }
}

#[cfg(feature = "std")]
impl Error for ZeroPoint {}

/// Appeared zero scalar is not expected/accepted
#[derive(Debug, Clone, Copy)]
pub struct ZeroScalar;

impl fmt::Display for ZeroScalar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("zero scalar")
    }
}

#[cfg(feature = "std")]
impl Error for ZeroScalar {}