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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Centralized error types for the secure-gate crate.
//!
//! # Error types
//!
//! | Type | Produced by | Feature |
//! |------|------------|---------|
//! | [`FromSliceError`] | [`Fixed::try_from(&[u8])`](crate::Fixed) | Always |
//! | [`HexError`] | [`Fixed::try_from_hex`](crate::Fixed::try_from_hex), [`FromHexStr`](crate::FromHexStr) | `encoding-hex` |
//! | [`Base32Error`] | [`Fixed::try_from_base32`](crate::Fixed::try_from_base32), [`FromBase32Str`](crate::FromBase32Str) | `encoding-base32` |
//! | [`Base64Error`] | [`Fixed::try_from_base64url`](crate::Fixed::try_from_base64url), [`FromBase64UrlStr`](crate::FromBase64UrlStr) | `encoding-base64` |
//! | [`Bech32Error`] | `try_from_bech32*`, [`FromBech32Str`](crate::FromBech32Str), [`FromBech32mStr`](crate::FromBech32mStr) | `encoding-bech32` |
//!
//! Every type in this module is produced by a function in this crate. There is no
//! unified wrapper enum: a caller decoding several formats owns the union it needs,
//! and nothing here would help it build one.
//!
//! # Design: build-invariant, heap-free, forward-compatible
//!
//! - **Identical shapes in debug and release builds.** No variant is gated on
//! `cfg(debug_assertions)`, so code that matches on these enums compiles and
//! behaves the same under every profile.
//! - **No heap data.** Errors carry at most `usize` length metadata; they never
//! contain payload bytes, HRP strings, or other input-derived text. All error
//! types are `Copy` and work without `alloc`.
//! - **`#[non_exhaustive]`.** Variants (and fields of struct variants) may be
//! added in future releases without a semver-major bump; downstream matches
//! need a wildcard arm. This is also what lets the crate ship a variant only
//! once something produces it, rather than reserving one in advance.
//! - **No derive macros.** [`Display`](core::fmt::Display) and
//! [`Error`](core::error::Error) are written out by hand below. The messages are
//! fixed strings and two `usize` fields, which is not enough work to justify
//! putting a proc-macro (and `syn`, `quote`, `proc-macro2`) in the dependency
//! graph of every downstream build.
//!
//! # Security: what errors may reveal
//!
//! `InvalidLength` variants carry the expected and actual byte counts in all
//! builds. Expected lengths are compile-time protocol parameters (key sizes,
//! nonce sizes) and actual lengths derive from the caller's own input, so
//! neither is treated as secret — this matches the crate's threat model
//! (see SECURITY.md). Genuinely input-derived strings (received HRPs, encoding
//! hints) are **never** captured, in any build. If even coarse error categories
//! are sensitive in your deployment, redact errors at the logging boundary.
use fmt;
/// Error returned when a byte slice cannot be converted to a fixed-size array.
///
/// Carries the expected and actual lengths in all build profiles. Lengths are
/// public protocol parameters, not secret material.
///
/// Implements [`core::error::Error`] unconditionally — no `std` feature is
/// required, and the impl is present in `no_std` builds. (On the 0.8 line
/// this impl is `std`-gated, because `core::error::Error` needs Rust 1.81 and
/// that branch targets 1.70. Code that must build against both should not
/// assume the trait is available without `std`.)
/// Errors produced when decoding Bech32 (BIP-173) or Bech32m (BIP-350) strings.
///
/// *Requires feature `encoding-bech32`.*
///
/// Variant shapes are identical in debug and release builds. No input-derived
/// strings (such as the received HRP) are ever captured — the caller already
/// holds the input and the expected HRP.
///
/// Implements [`core::error::Error`] unconditionally — no `std` feature is
/// required, and the impl is present in `no_std` builds. (On the 0.8 line
/// this impl is `std`-gated, because `core::error::Error` needs Rust 1.81 and
/// that branch targets 1.70. Code that must build against both should not
/// assume the trait is available without `std`.)
/// Errors produced when decoding Base32 (RFC 4648 §6, uppercase, unpadded) strings.
///
/// *Requires feature `encoding-base32`.*
///
/// Variant shapes are identical in debug and release builds; only numeric
/// length metadata is carried.
///
/// Implements [`core::error::Error`] unconditionally — no `std` feature is
/// required, and the impl is present in `no_std` builds. (On the 0.8 line
/// this impl is `std`-gated, because `core::error::Error` needs Rust 1.81 and
/// that branch targets 1.70. Code that must build against both should not
/// assume the trait is available without `std`.)
/// Errors produced when decoding base64url strings.
///
/// *Requires feature `encoding-base64`.*
///
/// Variant shapes are identical in debug and release builds; only numeric
/// length metadata is carried.
///
/// Implements [`core::error::Error`] unconditionally — no `std` feature is
/// required, and the impl is present in `no_std` builds. (On the 0.8 line
/// this impl is `std`-gated, because `core::error::Error` needs Rust 1.81 and
/// that branch targets 1.70. Code that must build against both should not
/// assume the trait is available without `std`.)
/// Errors produced when decoding hexadecimal strings.
///
/// *Requires feature `encoding-hex`.*
///
/// Variant shapes are identical in debug and release builds; only numeric
/// length metadata is carried.
///
/// Implements [`core::error::Error`] unconditionally — no `std` feature is
/// required, and the impl is present in `no_std` builds. (On the 0.8 line
/// this impl is `std`-gated, because `core::error::Error` needs Rust 1.81 and
/// that branch targets 1.70. Code that must build against both should not
/// assume the trait is available without `std`.)