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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Static SIMD capability for deployments without runtime CPU probing.
use core::marker::PhantomData;
use crate::runtime::{Backend, OperationKind};
use crate::{Alphabet, DecodeError, EncodeError, Standard, UrlSafe};
/// Non-forgeable, thread-bound proof that a static SIMD backend passed its KAT.
///
/// The token bypasses runtime CPU probing only. It does not bypass bounds,
/// canonicality, backend health, quarantine, or reporting. Kernel operations
/// are added to this contract by the architecture admission commits that
/// follow Commit 24.
///
/// The raw-pointer marker deliberately makes this value neither `Send` nor
/// `Sync`; deployment evidence applies to the thread that constructed it.
///
/// ```compile_fail
/// let token = base64_ng::StaticBackendToken::for_compiled_target().unwrap();
/// std::thread::spawn(move || drop(token));
/// ```
#[derive(Debug)]
pub struct StaticBackendToken {
backend: Backend,
generation: usize,
_thread_bound: PhantomData<*mut ()>,
}
impl StaticBackendToken {
/// Selects the strongest backend proven by compile-time target features.
///
/// Returns `None` when the build lacks a complete feature bundle,
/// pointer-width atomics, or a passing known-answer test.
#[must_use]
pub fn for_compiled_target() -> Option<Self> {
let backend = compiled_backend()?;
Self::admit(backend, false)
}
/// Constructs a token from deployment-supplied backend evidence.
///
/// # Safety
///
/// Before calling, the deployment must prove that this thread's CPU and OS
/// vector state support every feature returned by
/// [`Backend::required_cpu_features`], that the ABI preserves the required
/// vector state, and that migration cannot move this thread to an
/// incompatible CPU. A false attestation can execute an unsupported
/// instruction during the mandatory KAT and terminate the process.
#[must_use]
pub unsafe fn assume_supported(backend: Backend) -> Option<Self> {
if backend_matches_target(backend) {
Self::admit(backend, true)
} else {
None
}
}
/// Returns the exact backend represented by this token.
#[must_use]
pub const fn backend(&self) -> Backend {
self.backend
}
/// Returns the health generation borrowed by this token.
#[must_use]
pub const fn health_generation(&self) -> usize {
self.generation
}
/// Returns whether quarantine and generation state still validate it.
///
/// This is an admission snapshot, not synchronous cancellation. An
/// invocation that already observed a healthy generation may finish while
/// another thread quarantines that backend.
#[must_use]
pub fn is_valid(&self) -> bool {
let encode = crate::v2::backend_health::snapshot(OperationKind::Encode, self.backend);
let decode = crate::v2::backend_health::snapshot(OperationKind::StrictDecode, self.backend);
encode.state == crate::BackendHealthState::Healthy
&& decode.state == crate::BackendHealthState::Healthy
&& encode.generation == self.generation
&& decode.generation == self.generation
}
/// Encodes with the statically admitted Standard-alphabet backend.
///
/// Commits 25, 26, and 29 enable direct SSSE3/SSE4.1, AVX2, AVX-512 VBMI,
/// and little-endian `AArch64` NEON execution. Other token backends, or a
/// token invalidated by quarantine, use the scalar encoder.
/// The `checked-backend` feature applies the same per-call redundant
/// scalar comparison and quarantine policy as automatic dispatch.
pub fn encode_standard<const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, EncodeError> {
self.encode::<Standard, PAD>(input, output)
}
/// Encodes with the statically admitted URL-safe-alphabet backend.
///
/// Commits 25, 26, and 29 enable direct SSSE3/SSE4.1, AVX2, AVX-512 VBMI,
/// and little-endian `AArch64` NEON execution. Other token backends, or a
/// token invalidated by quarantine, use the scalar encoder.
/// The `checked-backend` feature applies the same per-call redundant
/// scalar comparison and quarantine policy as automatic dispatch.
pub fn encode_url_safe<const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, EncodeError> {
self.encode::<UrlSafe, PAD>(input, output)
}
/// Decodes strict Standard Base64 with the statically admitted backend.
///
/// Commits 27, 28, and 29 enable direct SSSE3/SSE4.1, AVX2, AVX-512 VBMI,
/// and little-endian `AArch64` NEON execution. Invalid input retains the
/// ordinary strict decoder's exact diagnostics. Direct SIMD blocks are
/// whole-input prevalidated, but short scalar fallbacks retain the ordinary
/// decoder's partial-output-on-error behavior.
pub fn decode_standard<const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, DecodeError> {
self.decode::<Standard, PAD>(input, output)
}
/// Decodes strict URL-safe Base64 with the statically admitted backend.
///
/// Commits 27, 28, and 29 enable direct SSSE3/SSE4.1, AVX2, AVX-512 VBMI,
/// and little-endian `AArch64` NEON execution. Invalid input retains the
/// ordinary strict decoder's exact diagnostics. Direct SIMD blocks are
/// whole-input prevalidated, but short scalar fallbacks retain the ordinary
/// decoder's partial-output-on-error behavior.
pub fn decode_url_safe<const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, DecodeError> {
self.decode::<UrlSafe, PAD>(input, output)
}
fn encode<A: Alphabet, const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, EncodeError> {
if !self.is_valid() {
return crate::scalar::encode_slice::<A, PAD>(input, output);
}
#[cfg(all(
feature = "checked-backend",
any(
target_arch = "x86",
target_arch = "x86_64",
all(target_arch = "aarch64", target_endian = "little")
)
))]
match self.backend {
Backend::Avx512Vbmi | Backend::Avx2 | Backend::Ssse3Sse41 => {
return crate::encode_backend::encode_checked::<A, PAD>(
self.backend,
input,
output,
);
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
Backend::Neon => {
return crate::encode_backend::encode_checked::<A, PAD>(
self.backend,
input,
output,
);
}
_ => {}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
match self.backend {
Backend::Avx512Vbmi => {
return crate::simd::encode_slice_avx512::<A, PAD>(input, output);
}
Backend::Avx2 => return crate::simd::encode_slice_avx2::<A, PAD>(input, output),
Backend::Ssse3Sse41 => {
return crate::simd::encode_slice_ssse3_sse41::<A, PAD>(input, output);
}
_ => {}
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
if self.backend == Backend::Neon {
return crate::simd::encode_slice_neon::<A, PAD>(input, output);
}
crate::scalar::encode_slice::<A, PAD>(input, output)
}
fn decode<A: Alphabet, const PAD: bool>(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, DecodeError> {
if !self.is_valid() {
return crate::scalar::decode_slice::<A, PAD>(input, output);
}
#[cfg(all(
feature = "checked-backend",
any(
target_arch = "x86",
target_arch = "x86_64",
all(target_arch = "aarch64", target_endian = "little")
)
))]
match self.backend {
Backend::Avx512Vbmi | Backend::Avx2 | Backend::Ssse3Sse41 => {
return crate::decode_backend::decode_checked::<A, PAD>(
self.backend,
input,
output,
);
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
Backend::Neon => {
return crate::decode_backend::decode_checked::<A, PAD>(
self.backend,
input,
output,
);
}
_ => {}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
match self.backend {
Backend::Avx512Vbmi => {
return crate::simd::decode_slice_avx512::<A, PAD>(input, output);
}
Backend::Avx2 => return crate::simd::decode_slice_avx2::<A, PAD>(input, output),
Backend::Ssse3Sse41 => {
return crate::simd::decode_slice_ssse3_sse41::<A, PAD>(input, output);
}
_ => {}
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
if self.backend == Backend::Neon {
return crate::simd::decode_slice_neon::<A, PAD>(input, output);
}
crate::scalar::decode_slice::<A, PAD>(input, output)
}
fn admit(backend: Backend, deployment_attested: bool) -> Option<Self> {
let admit_operation = |operation| {
if deployment_attested {
crate::v2::backend_health::admit_deployment_attested(operation, backend)
} else {
crate::v2::backend_health::admit(operation, backend)
}
};
if !cfg!(target_has_atomic = "ptr")
|| !admit_operation(OperationKind::Encode)
|| !admit_operation(OperationKind::StrictDecode)
{
return None;
}
let encode = crate::v2::backend_health::snapshot(OperationKind::Encode, backend);
let decode = crate::v2::backend_health::snapshot(OperationKind::StrictDecode, backend);
if encode.generation != decode.generation {
return None;
}
Some(Self {
backend,
generation: encode.generation,
_thread_bound: PhantomData,
})
}
}
fn compiled_backend() -> Option<Backend> {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if cfg!(all(
target_feature = "avx512f",
target_feature = "avx512bw",
target_feature = "avx512vl",
target_feature = "avx512vbmi"
)) {
return Some(Backend::Avx512Vbmi);
}
if cfg!(target_feature = "avx2") {
return Some(Backend::Avx2);
}
if cfg!(all(target_feature = "ssse3", target_feature = "sse4.1")) {
return Some(Backend::Ssse3Sse41);
}
}
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
if cfg!(target_feature = "neon") {
return Some(Backend::Neon);
}
#[cfg(target_arch = "wasm32")]
if cfg!(target_feature = "simd128") {
return Some(Backend::WasmSimd128);
}
None
}
const fn backend_matches_target(backend: Backend) -> bool {
match backend {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Backend::Avx512Vbmi | Backend::Avx2 | Backend::Ssse3Sse41 => true,
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
Backend::Neon => true,
#[cfg(target_arch = "wasm32")]
Backend::WasmSimd128 => true,
_ => false,
}
}