Skip to main content

pakery_core/
error.rs

1//! Common error types for PAKE protocols.
2
3use core::fmt;
4
5/// Errors that can occur during PAKE protocol execution.
6#[derive(Debug)]
7pub enum PakeError {
8    /// A received point could not be decoded as a valid group element.
9    InvalidPoint,
10    /// A computed or received point is the group identity element.
11    IdentityPoint,
12    /// Invalid input was provided.
13    InvalidInput(&'static str),
14    /// A protocol-level error occurred.
15    ProtocolError(&'static str),
16}
17
18impl fmt::Display for PakeError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            PakeError::InvalidPoint => write!(f, "invalid point encoding"),
22            PakeError::IdentityPoint => write!(f, "identity point encountered"),
23            PakeError::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
24            PakeError::ProtocolError(msg) => write!(f, "protocol error: {msg}"),
25        }
26    }
27}
28
29#[cfg(feature = "std")]
30impl std::error::Error for PakeError {}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use alloc::string::ToString;
36
37    #[test]
38    fn display_output_per_variant() {
39        assert_eq!(
40            PakeError::InvalidPoint.to_string(),
41            "invalid point encoding"
42        );
43        assert_eq!(
44            PakeError::IdentityPoint.to_string(),
45            "identity point encountered"
46        );
47        assert_eq!(
48            PakeError::InvalidInput("bad length").to_string(),
49            "invalid input: bad length"
50        );
51        assert_eq!(
52            PakeError::ProtocolError("mac mismatch").to_string(),
53            "protocol error: mac mismatch"
54        );
55    }
56}