base64-ng 1.2.1

no_std-first Base64 encoding and decoding with strict APIs and a security-heavy release process
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use super::*;
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
use crate::{Alphabet, decode_alphabet_byte};
use crate::{Engine, Standard, UrlSafe};

#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
struct AnchorMatchingCustom;

#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
impl Alphabet for AnchorMatchingCustom {
    const ENCODE: [u8; 64] = *b"ACBDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    fn decode(byte: u8) -> Option<u8> {
        decode_alphabet_byte(byte, &Self::ENCODE)
    }
}

fn fill_pattern(output: &mut [u8], seed: usize) {
    for (index, byte) in output.iter_mut().enumerate() {
        let value = (index * 73 + seed * 19) % 256;
        *byte = u8::try_from(value).unwrap();
    }
}

#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
fn fill_indices_pattern(output: &mut [u8; 12], seed: u8) {
    let mut write = 0;
    for group in 0..4 {
        let i0 = seed.wrapping_add(group * 4) & 0x3f;
        let i1 = seed.wrapping_add(group * 4 + 1) & 0x3f;
        let i2 = seed.wrapping_add(group * 4 + 2) & 0x3f;
        let i3 = seed.wrapping_add(group * 4 + 3) & 0x3f;

        output[write] = (i0 << 2) | (i1 >> 4);
        output[write + 1] = (i1 << 4) | (i2 >> 2);
        output[write + 2] = (i2 << 6) | i3;
        write += 3;
    }
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn fill_indices_pattern_wide(output: &mut [u8; 24], seed: u8) {
    let mut write = 0;
    for group in 0..8 {
        let i0 = seed.wrapping_add(group * 4) & 0x3f;
        let i1 = seed.wrapping_add(group * 4 + 1) & 0x3f;
        let i2 = seed.wrapping_add(group * 4 + 2) & 0x3f;
        let i3 = seed.wrapping_add(group * 4 + 3) & 0x3f;

        output[write] = (i0 << 2) | (i1 >> 4);
        output[write + 1] = (i1 << 4) | (i2 >> 2);
        output[write + 2] = (i2 << 6) | i3;
        write += 3;
    }
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn fill_indices_pattern_zmm(output: &mut [u8; 48], seed: u8) {
    let mut write = 0;
    for group in 0..16 {
        let i0 = seed.wrapping_add(group * 4) & 0x3f;
        let i1 = seed.wrapping_add(group * 4 + 1) & 0x3f;
        let i2 = seed.wrapping_add(group * 4 + 2) & 0x3f;
        let i3 = seed.wrapping_add(group * 4 + 3) & 0x3f;

        output[write] = (i0 << 2) | (i1 >> 4);
        output[write + 1] = (i1 << 4) | (i2 >> 2);
        output[write + 2] = (i2 << 6) | i3;
        write += 3;
    }
}

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
#[test]
fn avx512_encode_block_matches_scalar_when_available() {
    if !avx512_vbmi_base64_available() {
        println!(
            "skipped: AVX-512 VBMI encode block test requires avx512f,avx512bw,avx512vl,avx512vbmi"
        );
        return;
    }

    let mut input = [0; 48];
    for seed in 0..64 {
        fill_pattern(&mut input, seed);

        let mut avx512_standard = [0x55; 64];
        let mut scalar_standard = [0xaa; 64];
        // SAFETY: The candidate check above uses runtime AVX-512 feature
        // bundle detection on std builds and compile-time target-feature
        // detection otherwise.
        unsafe {
            encode_48_bytes_avx512::<Standard>(&input, &mut avx512_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, avx512_standard.len());
        assert_eq!(avx512_standard, scalar_standard);

        let mut avx512_url_safe = [0x55; 64];
        let mut scalar_url_safe = [0xaa; 64];
        // SAFETY: The candidate check above proves the AVX-512 feature
        // bundle is available for this test invocation.
        unsafe {
            encode_48_bytes_avx512::<UrlSafe>(&input, &mut avx512_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, avx512_url_safe.len());
        assert_eq!(avx512_url_safe, scalar_url_safe);
    }

    for seed in 0..64 {
        fill_indices_pattern_zmm(&mut input, seed);

        let mut avx512_standard = [0x55; 64];
        let mut scalar_standard = [0xaa; 64];
        // SAFETY: The candidate check above proves the AVX-512 feature
        // bundle is available for this test invocation.
        unsafe {
            encode_48_bytes_avx512::<Standard>(&input, &mut avx512_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, avx512_standard.len());
        assert_eq!(avx512_standard, scalar_standard);

        let mut avx512_url_safe = [0x55; 64];
        let mut scalar_url_safe = [0xaa; 64];
        // SAFETY: The candidate check above proves the AVX-512 feature
        // bundle is available for this test invocation.
        unsafe {
            encode_48_bytes_avx512::<UrlSafe>(&input, &mut avx512_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, avx512_url_safe.len());
        assert_eq!(avx512_url_safe, scalar_url_safe);
    }

    fill_indices_pattern_zmm(&mut input, 0);
    let mut avx512_custom = [0x55; 64];
    let mut scalar_custom = [0xaa; 64];
    // SAFETY: The candidate check above proves the AVX-512 feature bundle is
    // available for this test invocation.
    unsafe {
        encode_48_bytes_avx512::<AnchorMatchingCustom>(&input, &mut avx512_custom);
    }
    let scalar_len = Engine::<AnchorMatchingCustom, true>::new()
        .encode_slice(&input, &mut scalar_custom)
        .unwrap();
    assert_eq!(scalar_len, avx512_custom.len());
    assert_eq!(avx512_custom, scalar_custom);
}

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
#[test]
fn avx2_encode_block_matches_scalar_when_available() {
    if !avx2_available() {
        println!("skipped: AVX2 encode block test requires avx2");
        return;
    }

    let mut input = [0; 24];
    for seed in 0..64 {
        fill_pattern(&mut input, seed);

        let mut avx2_standard = [0x55; 32];
        let mut scalar_standard = [0xaa; 32];
        // SAFETY: The feature check above uses runtime AVX2 detection on
        // std builds and compile-time target-feature detection otherwise.
        unsafe {
            encode_24_bytes_avx2::<Standard>(&input, &mut avx2_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, avx2_standard.len());
        assert_eq!(avx2_standard, scalar_standard);

        let mut avx2_url_safe = [0x55; 32];
        let mut scalar_url_safe = [0xaa; 32];
        // SAFETY: The feature check above proves AVX2 availability for
        // this test invocation.
        unsafe {
            encode_24_bytes_avx2::<UrlSafe>(&input, &mut avx2_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, avx2_url_safe.len());
        assert_eq!(avx2_url_safe, scalar_url_safe);
    }

    for seed in 0..64 {
        fill_indices_pattern_wide(&mut input, seed);

        let mut avx2_standard = [0x55; 32];
        let mut scalar_standard = [0xaa; 32];
        // SAFETY: The feature check above proves AVX2 availability for this
        // test invocation.
        unsafe {
            encode_24_bytes_avx2::<Standard>(&input, &mut avx2_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, avx2_standard.len());
        assert_eq!(avx2_standard, scalar_standard);

        let mut avx2_url_safe = [0x55; 32];
        let mut scalar_url_safe = [0xaa; 32];
        // SAFETY: The feature check above proves AVX2 availability for this
        // test invocation.
        unsafe {
            encode_24_bytes_avx2::<UrlSafe>(&input, &mut avx2_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, avx2_url_safe.len());
        assert_eq!(avx2_url_safe, scalar_url_safe);
    }

    fill_indices_pattern_wide(&mut input, 0);
    let mut avx2_custom = [0x55; 32];
    let mut scalar_custom = [0xaa; 32];
    // SAFETY: The feature check above proves AVX2 availability for this test
    // invocation.
    unsafe {
        encode_24_bytes_avx2::<AnchorMatchingCustom>(&input, &mut avx2_custom);
    }
    let scalar_len = Engine::<AnchorMatchingCustom, true>::new()
        .encode_slice(&input, &mut scalar_custom)
        .unwrap();
    assert_eq!(scalar_len, avx2_custom.len());
    assert_eq!(avx2_custom, scalar_custom);
}

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
#[test]
fn ssse3_sse41_encode_block_matches_scalar_when_available() {
    if !ssse3_sse41_available() {
        println!("skipped: SSSE3/SSE4.1 encode block test requires ssse3 and sse4.1");
        return;
    }

    let mut input = [0; 12];
    for seed in 0..64 {
        fill_pattern(&mut input, seed);

        let mut ssse3_standard = [0x55; 16];
        let mut scalar_standard = [0xaa; 16];
        // SAFETY: The feature check above uses runtime SSSE3/SSE4.1
        // detection on std builds and compile-time target-feature
        // detection otherwise.
        unsafe {
            encode_12_bytes_ssse3_sse41::<Standard>(&input, &mut ssse3_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, ssse3_standard.len());
        assert_eq!(ssse3_standard, scalar_standard);

        let mut ssse3_url_safe = [0x55; 16];
        let mut scalar_url_safe = [0xaa; 16];
        // SAFETY: The feature check above proves SSSE3/SSE4.1
        // availability for this test invocation.
        unsafe {
            encode_12_bytes_ssse3_sse41::<UrlSafe>(&input, &mut ssse3_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, ssse3_url_safe.len());
        assert_eq!(ssse3_url_safe, scalar_url_safe);
    }

    for seed in 0..64 {
        fill_indices_pattern(&mut input, seed);

        let mut ssse3_standard = [0x55; 16];
        let mut scalar_standard = [0xaa; 16];
        // SAFETY: The feature check above proves SSSE3/SSE4.1 availability
        // for this test invocation.
        unsafe {
            encode_12_bytes_ssse3_sse41::<Standard>(&input, &mut ssse3_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, ssse3_standard.len());
        assert_eq!(ssse3_standard, scalar_standard);

        let mut ssse3_url_safe = [0x55; 16];
        let mut scalar_url_safe = [0xaa; 16];
        // SAFETY: The feature check above proves SSSE3/SSE4.1 availability
        // for this test invocation.
        unsafe {
            encode_12_bytes_ssse3_sse41::<UrlSafe>(&input, &mut ssse3_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, ssse3_url_safe.len());
        assert_eq!(ssse3_url_safe, scalar_url_safe);
    }

    fill_indices_pattern(&mut input, 0);
    let mut ssse3_custom = [0x55; 16];
    let mut scalar_custom = [0xaa; 16];
    // SAFETY: The feature check above proves SSSE3/SSE4.1 availability for
    // this test invocation.
    unsafe {
        encode_12_bytes_ssse3_sse41::<AnchorMatchingCustom>(&input, &mut ssse3_custom);
    }
    let scalar_len = Engine::<AnchorMatchingCustom, true>::new()
        .encode_slice(&input, &mut scalar_custom)
        .unwrap();
    assert_eq!(scalar_len, ssse3_custom.len());
    assert_eq!(ssse3_custom, scalar_custom);
}

#[cfg(any(
    target_arch = "aarch64",
    all(target_arch = "arm", target_feature = "neon")
))]
#[test]
fn neon_encode_block_matches_scalar_when_available() {
    if !neon_available() {
        println!("skipped: NEON encode block test requires aarch64 or arm+neon");
        return;
    }

    let mut input = [0; 12];
    for seed in 0..64 {
        fill_pattern(&mut input, seed);

        let mut neon_standard = [0x55; 16];
        let mut scalar_standard = [0xaa; 16];
        // SAFETY: The candidate check above proves NEON availability for
        // this test invocation.
        unsafe {
            encode_12_bytes_neon::<Standard>(&input, &mut neon_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, neon_standard.len());
        assert_eq!(neon_standard, scalar_standard);

        let mut neon_url_safe = [0x55; 16];
        let mut scalar_url_safe = [0xaa; 16];
        // SAFETY: The candidate check above proves NEON availability for
        // this test invocation.
        unsafe {
            encode_12_bytes_neon::<UrlSafe>(&input, &mut neon_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, neon_url_safe.len());
        assert_eq!(neon_url_safe, scalar_url_safe);
    }

    for seed in 0..64 {
        fill_indices_pattern(&mut input, seed);

        let mut neon_standard = [0x55; 16];
        let mut scalar_standard = [0xaa; 16];
        // SAFETY: The candidate check above proves NEON availability for
        // this test invocation.
        unsafe {
            encode_12_bytes_neon::<Standard>(&input, &mut neon_standard);
        }
        let scalar_len = Engine::<Standard, true>::new()
            .encode_slice(&input, &mut scalar_standard)
            .unwrap();
        assert_eq!(scalar_len, neon_standard.len());
        assert_eq!(neon_standard, scalar_standard);

        let mut neon_url_safe = [0x55; 16];
        let mut scalar_url_safe = [0xaa; 16];
        // SAFETY: The candidate check above proves NEON availability for
        // this test invocation.
        unsafe {
            encode_12_bytes_neon::<UrlSafe>(&input, &mut neon_url_safe);
        }
        let scalar_len = Engine::<UrlSafe, true>::new()
            .encode_slice(&input, &mut scalar_url_safe)
            .unwrap();
        assert_eq!(scalar_len, neon_url_safe.len());
        assert_eq!(neon_url_safe, scalar_url_safe);
    }

    fill_indices_pattern(&mut input, 0);
    let mut neon_custom = [0x55; 16];
    let mut scalar_custom = [0xaa; 16];
    // SAFETY: The candidate check above proves NEON availability for this
    // test invocation.
    unsafe {
        encode_12_bytes_neon::<AnchorMatchingCustom>(&input, &mut neon_custom);
    }
    let scalar_len = Engine::<AnchorMatchingCustom, true>::new()
        .encode_slice(&input, &mut scalar_custom)
        .unwrap();
    assert_eq!(scalar_len, neon_custom.len());
    assert_eq!(neon_custom, scalar_custom);
}