cryptography-rs 0.6.2

Block ciphers, hashes, public-key, and post-quantum primitives implemented directly from their specifications and original papers.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Magma (GOST R 34.12-2015) block cipher — RFC 8891.
//!
//! 64-bit block, 256-bit key, 32 Feistel rounds.
//! All tables and test vectors from RFC 8891.
//!
//! `Magma` keeps the original fast table lookups. `MagmaCt` is separate and
//! replaces only the eight 4-bit substitutions with fixed boolean circuits,
//! which is enough to remove secret-indexed lookups from the round function
//! while keeping the implementation compact.

// ── S-boxes (RFC 8891 §4.1) ────────────────────────────────────────────────
//
// Eight 4-bit bijections Pi'_0 .. Pi'_7.
// Pi'_i processes nibble i of the 32-bit word (nibble 0 = bits [3:0]).

const PI: [[u8; 16]; 8] = [
    [12, 4, 6, 2, 10, 5, 11, 9, 14, 8, 13, 7, 0, 3, 15, 1], // Pi'_0
    [6, 8, 2, 3, 9, 10, 5, 12, 1, 14, 4, 7, 11, 13, 0, 15], // Pi'_1
    [11, 3, 5, 8, 2, 15, 10, 13, 14, 1, 7, 4, 12, 9, 6, 0], // Pi'_2
    [12, 8, 2, 1, 13, 4, 15, 6, 7, 0, 10, 5, 3, 14, 9, 11], // Pi'_3
    [7, 15, 5, 10, 8, 1, 6, 13, 0, 9, 3, 14, 11, 4, 2, 12], // Pi'_4
    [5, 13, 15, 6, 9, 2, 12, 10, 11, 7, 8, 1, 4, 3, 14, 0], // Pi'_5
    [8, 14, 2, 5, 6, 9, 1, 12, 15, 4, 11, 0, 13, 10, 3, 7], // Pi'_6
    [1, 7, 14, 13, 0, 5, 8, 3, 4, 15, 10, 6, 9, 12, 11, 2], // Pi'_7
];

// ── Core transforms ────────────────────────────────────────────────────────

/// t(v): apply 8 independent 4-bit S-boxes to a 32-bit word.
/// Nibble i (bits [4i+3 : 4i]) is passed through Pi'_i.
#[inline]
fn t(v: u32) -> u32 {
    let mut r = 0u32;
    for (i, sbox) in PI.iter().enumerate() {
        let nibble = ((v >> (4 * i)) & 0xf) as usize;
        let sub = sbox[nibble];
        r |= u32::from(sub) << (4 * i);
    }
    r
}

/// Compute all monomials up to degree 3 of a 4-bit input.
///
/// Magma's S-boxes have Boolean degree ≤ 3, so these monomials span all ANF
/// terms that appear.  The `pi*_ct` functions XOR the terms with coefficient 1.
#[inline]
fn nibble_monomials(nibble: u8) -> ([u8; 4], [u8; 6], [u8; 4]) {
    let bits = [
        nibble & 1,
        (nibble >> 1) & 1,
        (nibble >> 2) & 1,
        (nibble >> 3) & 1,
    ];
    let pairs = [
        bits[0] & bits[1],
        bits[0] & bits[2],
        bits[0] & bits[3],
        bits[1] & bits[2],
        bits[1] & bits[3],
        bits[2] & bits[3],
    ];
    let triples = [
        pairs[0] & bits[2],
        pairs[0] & bits[3],
        pairs[1] & bits[3],
        pairs[3] & bits[3],
    ];
    (bits, pairs, triples)
}

// Each `pi*_ct` is the ANF of one RFC 8891 S-box written over the four input
// bits.  Pairs/triples from `nibble_monomials` are the product terms; a bare
// `1` in a `b*` expression is the ANF constant term.
#[inline]
fn pi0_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = pairs[1] ^ pairs[3] ^ triples[0] ^ pairs[4] ^ triples[3];
    let b1 = bits[1] ^ bits[2] ^ pairs[1] ^ pairs[3] ^ bits[3] ^ pairs[2] ^ triples[2] ^ triples[3];
    let b2 = 1 ^ pairs[0] ^ bits[2] ^ pairs[1] ^ pairs[2] ^ triples[3];
    let b3 = 1 ^ bits[0] ^ bits[1] ^ pairs[0] ^ pairs[3] ^ pairs[2] ^ pairs[4] ^ pairs[5];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi1_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[1]
        ^ pairs[5];
    let b1 = 1 ^ bits[0] ^ pairs[0] ^ bits[2] ^ bits[3] ^ triples[1] ^ triples[3];
    let b2 = 1
        ^ bits[0]
        ^ bits[1]
        ^ pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[5]
        ^ triples[2]
        ^ triples[3];
    let b3 = bits[0] ^ pairs[0] ^ bits[2] ^ pairs[1] ^ pairs[3];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi2_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = 1
        ^ pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[1]
        ^ pairs[5]
        ^ triples[2]
        ^ triples[3];
    let b1 = 1 ^ bits[1] ^ pairs[3] ^ triples[0] ^ pairs[2] ^ pairs[4] ^ pairs[5] ^ triples[2];
    let b2 = bits[1]
        ^ pairs[0]
        ^ pairs[1]
        ^ pairs[3]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[2]
        ^ triples[3];
    let b3 = 1 ^ bits[0] ^ bits[1] ^ bits[2] ^ triples[0] ^ triples[1] ^ pairs[5] ^ triples[2];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi3_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[1]
        ^ pairs[5]
        ^ triples[2]
        ^ triples[3];
    let b1 = bits[1]
        ^ pairs[0]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[1]
        ^ triples[2]
        ^ triples[3];
    let b2 = 1
        ^ bits[0]
        ^ bits[1]
        ^ pairs[0]
        ^ pairs[1]
        ^ pairs[3]
        ^ triples[0]
        ^ triples[1]
        ^ pairs[5]
        ^ triples[2];
    let b3 = 1 ^ bits[1] ^ pairs[1] ^ pairs[3] ^ bits[3] ^ triples[1] ^ triples[3];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi4_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = 1
        ^ pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[1]
        ^ triples[2];
    let b1 = 1 ^ bits[1] ^ pairs[0] ^ bits[2] ^ bits[3] ^ triples[1] ^ triples[2] ^ triples[3];
    let b2 = 1
        ^ pairs[0]
        ^ bits[2]
        ^ pairs[3]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[5]
        ^ triples[2]
        ^ triples[3];
    let b3 = bits[0] ^ bits[2] ^ pairs[3];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi5_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = 1 ^ pairs[0] ^ pairs[1] ^ pairs[3] ^ pairs[4] ^ pairs[5];
    let b1 = bits[1] ^ pairs[1] ^ pairs[3] ^ bits[3] ^ pairs[5] ^ triples[3];
    let b2 = 1 ^ bits[2] ^ pairs[3] ^ triples[0] ^ bits[3] ^ pairs[2] ^ triples[1] ^ triples[3];
    let b3 = bits[0] ^ bits[1] ^ bits[2] ^ pairs[3] ^ triples[0] ^ bits[3] ^ pairs[4] ^ triples[2];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi6_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = pairs[0]
        ^ pairs[1]
        ^ pairs[3]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ triples[1]
        ^ triples[2]
        ^ triples[3];
    let b1 = bits[0] ^ bits[1] ^ bits[2] ^ triples[0] ^ bits[3] ^ pairs[4] ^ triples[3];
    let b2 = bits[0]
        ^ bits[2]
        ^ pairs[3]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ pairs[5]
        ^ triples[2]
        ^ triples[3];
    let b3 = 1 ^ bits[1] ^ bits[2] ^ pairs[1] ^ pairs[3] ^ pairs[2] ^ pairs[4] ^ pairs[5];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[inline]
fn pi7_ct(nibble: u8) -> u8 {
    let (bits, pairs, triples) = nibble_monomials(nibble);

    let b0 = 1
        ^ bits[1]
        ^ pairs[0]
        ^ bits[2]
        ^ pairs[1]
        ^ pairs[3]
        ^ triples[0]
        ^ bits[3]
        ^ pairs[2]
        ^ pairs[4]
        ^ triples[2]
        ^ triples[3];
    let b1 = bits[0] ^ bits[1] ^ pairs[1] ^ pairs[3] ^ triples[0] ^ triples[1] ^ triples[3];
    let b2 = bits[0] ^ bits[1] ^ pairs[0] ^ pairs[3] ^ bits[3] ^ pairs[2] ^ pairs[5] ^ triples[2];
    let b3 = bits[1] ^ triples[0] ^ pairs[2] ^ pairs[5] ^ triples[2] ^ triples[3];

    b0 | (b1 << 1) | (b2 << 2) | (b3 << 3)
}

#[cfg(test)]
#[inline]
fn pi_ct(box_idx: usize, nibble: u8) -> u8 {
    match box_idx {
        0 => pi0_ct(nibble),
        1 => pi1_ct(nibble),
        2 => pi2_ct(nibble),
        3 => pi3_ct(nibble),
        4 => pi4_ct(nibble),
        5 => pi5_ct(nibble),
        6 => pi6_ct(nibble),
        7 => pi7_ct(nibble),
        _ => unreachable!(),
    }
}

/// Constant-time variant of `t(v)`.
///
/// This is the same eight-nibble substitution as `t()`, but each nibble is
/// routed through its explicit boolean circuit instead of indexing `PI`.
#[inline]
fn t_ct(v: u32) -> u32 {
    let n0 = (v & 0x0000_000f) as u8;
    let n1 = ((v >> 4) & 0x0000_000f) as u8;
    let n2 = ((v >> 8) & 0x0000_000f) as u8;
    let n3 = ((v >> 12) & 0x0000_000f) as u8;
    let n4 = ((v >> 16) & 0x0000_000f) as u8;
    let n5 = ((v >> 20) & 0x0000_000f) as u8;
    let n6 = ((v >> 24) & 0x0000_000f) as u8;
    let n7 = ((v >> 28) & 0x0000_000f) as u8;

    u32::from(pi0_ct(n0))
        | (u32::from(pi1_ct(n1)) << 4)
        | (u32::from(pi2_ct(n2)) << 8)
        | (u32::from(pi3_ct(n3)) << 12)
        | (u32::from(pi4_ct(n4)) << 16)
        | (u32::from(pi5_ct(n5)) << 20)
        | (u32::from(pi6_ct(n6)) << 24)
        | (u32::from(pi7_ct(n7)) << 28)
}

/// g[k](a): wrapping-add key, substitute, rotate left 11 bits.
#[inline]
fn g(k: u32, a: u32) -> u32 {
    t(a.wrapping_add(k)).rotate_left(11)
}

#[inline]
fn g_ct(k: u32, a: u32) -> u32 {
    // Same Feistel round function as `g()`: the only change is the Ct
    // substitution inside `t_ct`.
    t_ct(a.wrapping_add(k)).rotate_left(11)
}

// ── Key schedule (RFC 8891 §4.3) ───────────────────────────────────────────
//
// 256-bit key K split into 8 × 32-bit subkeys (big-endian bytes):
//   k[i] = K_{i+1} = k_{255-32i} .. k_{224-32i}
//
// Encryption round key sequence (32 keys):
//   k[0..8]  (×3 = rounds 1–24)  then  k[7..0]  (×1 = rounds 25–32)
//
// Decryption round key sequence = encryption sequence reversed:
//   k[0..8]  (×1)  then  k[7..0]  (×3)

fn build_round_keys(key: &[u8; 32]) -> ([u32; 32], [u32; 32]) {
    let mut k = [0u32; 8];
    for i in 0..8 {
        k[i] = u32::from_be_bytes(key[4 * i..4 * i + 4].try_into().unwrap());
    }

    let mut enc = [0u32; 32];
    for i in 0..24 {
        enc[i] = k[i % 8];
    } // rounds 1–24: forward three times
    for i in 0..8 {
        enc[24 + i] = k[7 - i];
    } // rounds 25–32: reversed once

    let mut dec = enc;
    dec.reverse();

    (enc, dec)
}

// ── Feistel core ───────────────────────────────────────────────────────────
//
// Block is split as a₁ || a₀  (a₁ = upper 32 bits, a₀ = lower 32 bits).
//
// Rounds 1–31: G[k](a₁, a₀) = (a₀,  g[k](a₀) ⊕ a₁)  — apply then swap
// Round 32:   G*[k](a₁, a₀) = (g[k](a₀) ⊕ a₁) || a₀  — apply, no swap

fn magma_core(block: [u8; 8], rk: &[u32; 32]) -> [u8; 8] {
    let mut a1 = u32::from_be_bytes(block[0..4].try_into().unwrap()); // upper
    let mut a0 = u32::from_be_bytes(block[4..8].try_into().unwrap()); // lower

    for &round_key in rk.iter().take(31) {
        let tmp = g(round_key, a0) ^ a1;
        a1 = a0;
        a0 = tmp;
    }

    // G* (final round — no swap)
    let c1 = g(rk[31], a0) ^ a1; // upper half of output
    let c0 = a0; // lower half of output

    let mut out = [0u8; 8];
    out[0..4].copy_from_slice(&c1.to_be_bytes());
    out[4..8].copy_from_slice(&c0.to_be_bytes());
    out
}

fn magma_core_ct(block: [u8; 8], rk: &[u32; 32]) -> [u8; 8] {
    let mut a1 = u32::from_be_bytes(block[0..4].try_into().unwrap());
    let mut a0 = u32::from_be_bytes(block[4..8].try_into().unwrap());

    for &round_key in rk.iter().take(31) {
        let tmp = g_ct(round_key, a0) ^ a1;
        a1 = a0;
        a0 = tmp;
    }

    let c1 = g_ct(rk[31], a0) ^ a1;
    let c0 = a0;

    let mut out = [0u8; 8];
    out[0..4].copy_from_slice(&c1.to_be_bytes());
    out[4..8].copy_from_slice(&c0.to_be_bytes());
    out
}

// ── Public interface ───────────────────────────────────────────────────────

/// Magma block cipher — RFC 8891 / GOST R 34.12-2015.
///
/// 64-bit block, 256-bit key.  Pure Rust, no unsafe, no heap allocation.
pub struct Magma {
    enc_rk: [u32; 32],
    dec_rk: [u32; 32],
}

impl Magma {
    /// Construct from a 32-byte (256-bit) key.
    #[must_use]
    pub fn new(key: &[u8; 32]) -> Self {
        let (enc_rk, dec_rk) = build_round_keys(key);
        Magma { enc_rk, dec_rk }
    }

    /// Construct from a 32-byte key and wipe the provided key buffer.
    pub fn new_wiping(key: &mut [u8; 32]) -> Self {
        let out = Self::new(key);
        crate::ct::zeroize_slice(key.as_mut_slice());
        out
    }

    /// Encrypt a 64-bit block (ECB mode).
    #[must_use]
    pub fn encrypt_block(&self, block: &[u8; 8]) -> [u8; 8] {
        magma_core(*block, &self.enc_rk)
    }

    /// Decrypt a 64-bit block (ECB mode).
    #[must_use]
    pub fn decrypt_block(&self, block: &[u8; 8]) -> [u8; 8] {
        magma_core(*block, &self.dec_rk)
    }
}

/// A software-only constant-time Magma path.
///
/// `MagmaCt` keeps Magma's original structure and key schedule, but replaces
/// the nibble S-box lookups with the fixed boolean circuits above so the round
/// function no longer indexes memory with secret-derived values.
pub struct MagmaCt {
    enc_rk: [u32; 32],
    dec_rk: [u32; 32],
}

impl MagmaCt {
    /// Construct from a 32-byte (256-bit) key.
    #[must_use]
    pub fn new(key: &[u8; 32]) -> Self {
        let (enc_rk, dec_rk) = build_round_keys(key);
        MagmaCt { enc_rk, dec_rk }
    }

    /// Construct from a 32-byte key and wipe the provided key buffer.
    pub fn new_wiping(key: &mut [u8; 32]) -> Self {
        let out = Self::new(key);
        crate::ct::zeroize_slice(key.as_mut_slice());
        out
    }

    /// Encrypt a 64-bit block (ECB mode).
    #[must_use]
    pub fn encrypt_block(&self, block: &[u8; 8]) -> [u8; 8] {
        magma_core_ct(*block, &self.enc_rk)
    }

    /// Decrypt a 64-bit block (ECB mode).
    #[must_use]
    pub fn decrypt_block(&self, block: &[u8; 8]) -> [u8; 8] {
        magma_core_ct(*block, &self.dec_rk)
    }
}

impl crate::BlockCipher for Magma {
    const BLOCK_LEN: usize = 8;
    fn encrypt(&self, block: &mut [u8]) {
        let arr: &[u8; 8] = (&*block).try_into().expect("wrong block length");
        block.copy_from_slice(&self.encrypt_block(arr));
    }
    fn decrypt(&self, block: &mut [u8]) {
        let arr: &[u8; 8] = (&*block).try_into().expect("wrong block length");
        block.copy_from_slice(&self.decrypt_block(arr));
    }
}

impl crate::BlockCipher for MagmaCt {
    const BLOCK_LEN: usize = 8;
    fn encrypt(&self, block: &mut [u8]) {
        let arr: &[u8; 8] = (&*block).try_into().expect("wrong block length");
        block.copy_from_slice(&self.encrypt_block(arr));
    }
    fn decrypt(&self, block: &mut [u8]) {
        let arr: &[u8; 8] = (&*block).try_into().expect("wrong block length");
        block.copy_from_slice(&self.decrypt_block(arr));
    }
}

impl Drop for Magma {
    fn drop(&mut self) {
        // Magma caches both round-key orders; wipe them when the instance dies.
        crate::ct::zeroize_slice(self.enc_rk.as_mut_slice());
        crate::ct::zeroize_slice(self.dec_rk.as_mut_slice());
    }
}

impl Drop for MagmaCt {
    fn drop(&mut self) {
        crate::ct::zeroize_slice(self.enc_rk.as_mut_slice());
        crate::ct::zeroize_slice(self.dec_rk.as_mut_slice());
    }
}

// ── Tests (vectors from RFC 8891) ─────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn h8(s: &str) -> [u8; 8] {
        let b: Vec<u8> = (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
            .collect();
        b.try_into().unwrap()
    }

    fn h32(s: &str) -> [u8; 32] {
        let b: Vec<u8> = (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
            .collect();
        b.try_into().unwrap()
    }

    // ── Encrypt / Decrypt (RFC 8891 §A.3) ────────────────────────────────

    #[test]
    fn encrypt_decrypt_rfc() {
        let key = h32("ffeeddccbbaa99887766554433221100f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
        let pt = h8("fedcba9876543210");
        let ct = h8("4ee901e5c2d8ca3d");
        let m = Magma::new(&key);
        assert_eq!(m.encrypt_block(&pt), ct, "encrypt");
        assert_eq!(m.decrypt_block(&ct), pt, "decrypt");
    }

    // ── Roundtrip ─────────────────────────────────────────────────────────

    #[test]
    fn roundtrip() {
        let key = [0x42u8; 32];
        let pt = [0xABu8; 8];
        let m = Magma::new(&key);
        assert_eq!(m.decrypt_block(&m.encrypt_block(&pt)), pt);
    }

    #[test]
    fn ct_sboxes_match_tables() {
        for (box_idx, table) in PI.iter().enumerate() {
            for nibble in 0u8..16 {
                assert_eq!(pi_ct(box_idx, nibble), table[nibble as usize]);
            }
        }
    }

    #[test]
    fn encrypt_decrypt_rfc_ct() {
        let key = h32("ffeeddccbbaa99887766554433221100f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
        let pt = h8("fedcba9876543210");
        let ct = h8("4ee901e5c2d8ca3d");
        let fast = Magma::new(&key);
        let slow = MagmaCt::new(&key);
        assert_eq!(slow.encrypt_block(&pt), ct, "encrypt");
        assert_eq!(slow.decrypt_block(&ct), pt, "decrypt");
        assert_eq!(
            slow.encrypt_block(&pt),
            fast.encrypt_block(&pt),
            "match fast"
        );
    }
}