Skip to main content

kk_crypto/
error.rs

1// Copyright (c) 2026 John A Keeney, Entrouter. All rights reserved.
2// Licensed under the Apache License, Version 2.0 with Additional Terms.
3// NO COMMERCIAL USE without prior written authorization from Entrouter.
4// Unauthorized commercial use will be prosecuted to the fullest extent of the law.
5// See the LICENSE file in the project root for full license information.
6// NOTICE: Removal of this header is a violation of the license.
7
8#[cfg(not(feature = "std"))]
9use alloc::string::String;
10#[cfg(not(feature = "std"))]
11use core::fmt;
12
13#[cfg(feature = "std")]
14use thiserror::Error;
15
16/// Errors that can occur during KK operations.
17#[derive(Debug)]
18#[cfg_attr(feature = "std", derive(Error))]
19pub enum KkError {
20    #[cfg_attr(feature = "std", error("entropy collection failed: {0}"))]
21    EntropyFailure(String),
22
23    #[cfg_attr(
24        feature = "std",
25        error("temporal commitment verification failed, entropic moment mismatch")
26    )]
27    CommitmentMismatch,
28
29    #[cfg_attr(feature = "std", error("invalid packet: {0}"))]
30    InvalidPacket(String),
31
32    #[cfg_attr(feature = "std", error("empty input: nothing to encode"))]
33    EmptyInput,
34
35    #[cfg_attr(
36        feature = "std",
37        error(
38            "epoch drift too large: claimed {claimed_nanos} ns, \
39         drift {drift_nanos} ns exceeds max {max_nanos} ns"
40        )
41    )]
42    EpochDrift {
43        claimed_nanos: u128,
44        drift_nanos: u128,
45        max_nanos: u128,
46    },
47
48    #[cfg_attr(
49        feature = "std",
50        error("stale nonce: verifier nonce was already used or not recognized")
51    )]
52    StaleNonce,
53
54    #[cfg_attr(feature = "std", error("GPU error: {0}"))]
55    GpuError(String),
56}
57
58#[cfg(not(feature = "std"))]
59impl fmt::Display for KkError {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::EntropyFailure(s) => write!(f, "entropy collection failed: {s}"),
63            Self::CommitmentMismatch => write!(f, "temporal commitment verification failed"),
64            Self::InvalidPacket(s) => write!(f, "invalid packet: {s}"),
65            Self::EmptyInput => write!(f, "empty input: nothing to encode"),
66            Self::EpochDrift {
67                claimed_nanos,
68                drift_nanos,
69                max_nanos,
70            } => {
71                write!(f, "epoch drift too large: claimed {claimed_nanos} ns, drift {drift_nanos} ns exceeds max {max_nanos} ns")
72            }
73            Self::StaleNonce => write!(f, "stale nonce"),
74            Self::GpuError(s) => write!(f, "GPU error: {s}"),
75        }
76    }
77}
78
79pub type Result<T> = core::result::Result<T, KkError>;