Skip to main content

hekate_core/
errors.rs

1// SPDX-License-Identifier: Apache-2.0
2// This file is part of the hekate project.
3// Copyright (C) 2026 Andrei Kochergin <andrei@oumuamua.dev>
4// Copyright (C) 2026 Oumuamua Labs <info@oumuamua.dev>. All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10//     http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::poly::variant;
19use crate::{config, trace};
20use core::fmt;
21use hekate_crypto::{merkle, transcript};
22
23pub type Result<T> = core::result::Result<T, Error>;
24
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub enum Error {
27    Config(config::Error),
28    Trace(trace::Error),
29    Merkle(merkle::Error),
30    Transcript(transcript::Error),
31    VirtualPoly(variant::Error),
32
33    /// Cross-crate protocol failures that
34    /// do not belong to a specific sub-error enum.
35    Protocol {
36        protocol: &'static str,
37        message: &'static str,
38    },
39
40    /// Internal invariant breach; a bug,
41    /// not a verifier-observable soundness failure.
42    InvariantViolation {
43        message: &'static str,
44    },
45}
46
47impl fmt::Display for Error {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            Self::Config(e) => e.fmt(f),
51            Self::Trace(e) => e.fmt(f),
52            Self::Merkle(e) => e.fmt(f),
53            Self::Transcript(e) => e.fmt(f),
54            Self::VirtualPoly(e) => e.fmt(f),
55            Self::Protocol { protocol, message } => {
56                write!(f, "Protocol error ({protocol}): {message}")
57            }
58            Self::InvariantViolation { message } => {
59                write!(f, "Invariant violation: {message}")
60            }
61        }
62    }
63}
64
65impl From<config::Error> for Error {
66    fn from(value: config::Error) -> Self {
67        Self::Config(value)
68    }
69}
70
71impl From<trace::Error> for Error {
72    fn from(value: trace::Error) -> Self {
73        Self::Trace(value)
74    }
75}
76
77impl From<merkle::Error> for Error {
78    fn from(value: merkle::Error) -> Self {
79        Self::Merkle(value)
80    }
81}
82
83impl From<transcript::Error> for Error {
84    fn from(value: transcript::Error) -> Self {
85        Self::Transcript(value)
86    }
87}
88
89impl From<variant::Error> for Error {
90    fn from(value: variant::Error) -> Self {
91        Self::VirtualPoly(value)
92    }
93}