1use mpc_tls::MpcTlsError;
2use std::{error::Error, fmt};
3use tlsn_common::{encoding::EncodingError, zk_aes::ZkAesCtrError};
4
5#[derive(Debug, thiserror::Error)]
7pub struct ProverError {
8 kind: ErrorKind,
9 source: Option<Box<dyn Error + Send + Sync + 'static>>,
10}
11
12impl ProverError {
13 fn new<E>(kind: ErrorKind, source: E) -> Self
14 where
15 E: Into<Box<dyn Error + Send + Sync + 'static>>,
16 {
17 Self {
18 kind,
19 source: Some(source.into()),
20 }
21 }
22
23 pub(crate) fn config<E>(source: E) -> Self
24 where
25 E: Into<Box<dyn Error + Send + Sync + 'static>>,
26 {
27 Self::new(ErrorKind::Config, source)
28 }
29
30 pub(crate) fn mpc<E>(source: E) -> Self
31 where
32 E: Into<Box<dyn Error + Send + Sync + 'static>>,
33 {
34 Self::new(ErrorKind::Mpc, source)
35 }
36
37 pub(crate) fn zk<E>(source: E) -> Self
38 where
39 E: Into<Box<dyn Error + Send + Sync + 'static>>,
40 {
41 Self::new(ErrorKind::Zk, source)
42 }
43
44 pub(crate) fn commit<E>(source: E) -> Self
45 where
46 E: Into<Box<dyn Error + Send + Sync + 'static>>,
47 {
48 Self::new(ErrorKind::Commit, source)
49 }
50
51 pub(crate) fn attestation<E>(source: E) -> Self
52 where
53 E: Into<Box<dyn Error + Send + Sync + 'static>>,
54 {
55 Self::new(ErrorKind::Attestation, source)
56 }
57}
58
59#[derive(Debug)]
60enum ErrorKind {
61 Io,
62 Mpc,
63 Zk,
64 Config,
65 Commit,
66 Attestation,
67}
68
69impl fmt::Display for ProverError {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.write_str("prover error: ")?;
72
73 match self.kind {
74 ErrorKind::Io => f.write_str("io error")?,
75 ErrorKind::Mpc => f.write_str("mpc error")?,
76 ErrorKind::Zk => f.write_str("zk error")?,
77 ErrorKind::Config => f.write_str("config error")?,
78 ErrorKind::Commit => f.write_str("commit error")?,
79 ErrorKind::Attestation => f.write_str("attestation error")?,
80 }
81
82 if let Some(source) = &self.source {
83 write!(f, " caused by: {}", source)?;
84 }
85
86 Ok(())
87 }
88}
89
90impl From<std::io::Error> for ProverError {
91 fn from(e: std::io::Error) -> Self {
92 Self::new(ErrorKind::Io, e)
93 }
94}
95
96impl From<tls_client_async::ConnectionError> for ProverError {
97 fn from(e: tls_client_async::ConnectionError) -> Self {
98 Self::new(ErrorKind::Io, e)
99 }
100}
101
102impl From<uid_mux::yamux::ConnectionError> for ProverError {
103 fn from(e: uid_mux::yamux::ConnectionError) -> Self {
104 Self::new(ErrorKind::Io, e)
105 }
106}
107
108impl From<mpz_common::ContextError> for ProverError {
109 fn from(e: mpz_common::ContextError) -> Self {
110 Self::new(ErrorKind::Mpc, e)
111 }
112}
113
114impl From<MpcTlsError> for ProverError {
115 fn from(e: MpcTlsError) -> Self {
116 Self::new(ErrorKind::Mpc, e)
117 }
118}
119
120impl From<ZkAesCtrError> for ProverError {
121 fn from(e: ZkAesCtrError) -> Self {
122 Self::new(ErrorKind::Zk, e)
123 }
124}
125
126impl From<EncodingError> for ProverError {
127 fn from(e: EncodingError) -> Self {
128 Self::new(ErrorKind::Commit, e)
129 }
130}