跳转到内容

std.crypto.aes.bitsliced

标准库Zig 0.17.0官方原文
源码std/crypto/aes/bitsliced.zig

第 1 行

用于不支持硬件 AES 的目标的位切片 AES 轮函数。 没有依赖数据的内存访问,因此是常量时间。

std.crypto.aes.bitsliced.Batch

类型函数

第 18 行

一批 8 * lanes 个块,每组八个块对应一个 32 位通道

pub fn Batch(comptime lanes: usize) type

参数

lanes: usize

std.crypto.aes.bitsliced.Batch.addRoundKey

函数

第 274 行

添加轮密钥,该密钥也必须采用位切片形式

pub fn addRoundKey(st: *Bitsliced, round_key: *const Bitsliced) void

参数

st: *Bitsliced
round_key: *const Bitsliced

std.crypto.aes.bitsliced.Batch.Bitsliced

类型

第 24 行

std.crypto.aes.bitsliced.Batch.broadcast_loc

常量

第 360 行

预计算轮密钥的第 i 位在打包后的落点。

std.crypto.aes.bitsliced.Batch.decrypt

函数

第 502 行

对位切片表示的整个批次执行完整的逆 AES 密码运算。

pub fn decrypt(comptime rounds: usize, keys: *const [rounds + 1]Bitsliced, data: *[nblocks * 16]u8) void

参数

rounds: usize
keys: *const [rounds + 1]Bitsliced
data: *[nblocks * 16]u8

std.crypto.aes.bitsliced.Batch.encrypt

函数

第 406 行

对整个批次原地运行完整 AES 密码运算。

pub fn encrypt(comptime rounds: usize, keys: *const [rounds + 1]Bitsliced, data: *[nblocks * 16]u8) void

参数

rounds: usize
keys: *const [rounds + 1]Bitsliced
data: *[nblocks * 16]u8

std.crypto.aes.bitsliced.Batch.invAffineGroup

函数

第 473 行

逆向 S 盒的线性映射

fn invAffineGroup(st: *Bitsliced, comptime g: usize) void

参数

st: *Bitsliced
g: usize

std.crypto.aes.bitsliced.Batch.invMixColumns

函数

第 461 行

fn invMixColumns(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.invShiftRows

函数

第 419 行

fn invShiftRows(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.invSubBytes

函数

第 495 行

紧凑逆 S 盒:先应用固定线性映射,再应用正向 S 盒,最后应用同一线性映射 此技巧来自 Thomas Pornin

fn invSubBytes(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.laneRotl

函数

第 32 行

fn laneRotl(x: Word, comptime n: u5) Word

参数

x: Word
n: u5

std.crypto.aes.bitsliced.Batch.mix_inv

常量

第 429 行

预计算 mixColumns 的逆矩阵

[32]u32

std.crypto.aes.bitsliced.Batch.mixColumns

函数

第 176 行

fn mixColumns(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.nblocks

常量

第 23 行

std.crypto.aes.bitsliced.Batch.pack

函数

第 330 行

将 nblocks 个小端序块转换为打包状态。 块 8*g .. 8*g+7 放入每个字的通道 g 中。

fn pack(bytes: *const [nblocks * 16]u8) Bitsliced

参数

bytes: *const [nblocks * 16]u8

std.crypto.aes.bitsliced.Batch.packKeys

函数

第 396 行

将完整密钥调度打包为位切片表示。

pub fn packKeys(round_keys: anytype) [round_keys.len]Bitsliced

std.crypto.aes.bitsliced.Batch.packRoundKey

函数

第 382 行

打包轮密钥并广播到全部数据块,以便直接与打包状态进行异或。

fn packRoundKey(round_key: *const [16]u8) Bitsliced

参数

round_key: *const [16]u8

std.crypto.aes.bitsliced.Batch.packState

函数

第 279 行

正交化(将字中的块转换为位切片)

pub fn packState(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.round

函数

第 267 行

对整个批次执行一轮无密钥 AES(SubBytes、ShiftRows、MixColumns)

pub fn round(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.sbox

函数

第 50 行

fn sbox(u: []Word) void

参数

u: []Word

std.crypto.aes.bitsliced.Batch.Shift

类型

第 21 行

std.crypto.aes.bitsliced.Batch.shiftRows

函数

第 167 行

fn shiftRows(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.splatWord

函数

第 27 行

将 32 位模式广播到一个字的所有通道。

pub fn splatWord(x: u32) Word

参数

x: u32

std.crypto.aes.bitsliced.Batch.subBytes

函数

第 160 行

fn subBytes(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.swapMove

函数

第 42 行

pub fn swapMove(a: *Word, b: *Word, comptime mask32: u32, comptime n: comptime_int) void

参数

a: *Word
b: *Word
mask32: u32
n: comptime_int

std.crypto.aes.bitsliced.Batch.unpack

函数

第 345 行

pack 的逆操作

fn unpack(st: *Bitsliced) [nblocks * 16]u8

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.unpackState

函数

第 304 行

packState 的逆操作

pub fn unpackState(st: *Bitsliced) void

参数

st: *Bitsliced

std.crypto.aes.bitsliced.Batch.Word

常量

第 20 行

std.crypto.aes.bitsliced.builtin

常量

第 5 行

std.crypto.aes.bitsliced.mem

别名

第 6 行

别名目标 std.mem

std.crypto.aes.bitsliced.State

常量

第 15 行

保存整批 width 个数据块的状态

std.crypto.aes.bitsliced.std

别名

第 4 行

别名目标 std

std.crypto.aes.bitsliced.testing

别名

第 520 行

别名目标 std.testing

std.crypto.aes.bitsliced.Wide

类型

第 9 行

目标支持的最大 AES 数据块批次宽度。

std.crypto.aes.bitsliced.width

常量

第 12 行

可同时处理的 AES 分组数

源码

std/crypto/aes/bitsliced.zig

//! 用于不支持硬件 AES 的目标的位切片 AES 轮函数。
//! 没有依赖数据的内存访问,因此是常量时间。

const std = @import("../../std.zig");
const builtin = @import("builtin");
const mem = std.mem;

/// 目标支持的最大 AES 数据块批次宽度。
pub const Wide = if (builtin.cpu.arch == .wasm32 or builtin.cpu.arch == .wasm64 or @bitSizeOf(usize) >= 64) Batch(2) else Batch(1);

/// 可同时处理的 AES 分组数
pub const width = Wide.nblocks;

/// 保存整批 `width` 个数据块的状态
pub const State = Wide.Bitsliced;

/// 一批 `8 * lanes` 个块,每组八个块对应一个 32 位通道
pub fn Batch(comptime lanes: usize) type {
    return struct {
        pub const Word = @Int(.unsigned, 32 * lanes);
        const Shift = std.math.Log2Int(Word);

        const nblocks = 8 * lanes;
        pub const Bitsliced = [32]Word;

        /// 将 32 位模式广播到一个字的所有通道。
        pub fn splatWord(x: u32) Word {
            const t: [lanes]u32 = @splat(x);
            return @bitCast(t);
        }

        fn laneRotl(x: Word, comptime n: u5) Word {
            if (lanes == 1) return std.math.rotl(u32, x, n);
            const lo_bits = (@as(u32, 1) << n) - 1;
            const mask_lo = comptime splatWord(lo_bits);
            const mask_hi = comptime splatWord(~lo_bits);
            const l: Shift = n;
            const r: Shift = @intCast(32 - @as(u32, n));
            return ((x << l) & mask_hi) | ((x >> r) & mask_lo);
        }

        pub fn swapMove(a: *Word, b: *Word, comptime mask32: u32, comptime n: comptime_int) void {
            const shift: Shift = n;
            const mask = comptime splatWord(mask32);
            const tmp = (b.* ^ (a.* >> shift)) & mask;
            b.* ^= tmp;
            a.* ^= tmp << shift;
        }

        fn sbox(u: []Word) void {
            const s0 = u[4] ^ u[16];
            const s1 = u[20] ^ u[28];
            const s2 = u[12] ^ s0;
            const s3 = u[0] ^ u[8];
            const q0 = s1 ^ s2;
            const s4 = u[0] ^ u[24];
            const s5 = u[8] ^ u[24];
            const s6 = u[12] ^ s1;
            const s7 = u[20] ^ s3;
            const q1 = s1 ^ s5;
            const q2 = u[8] ^ q0;
            const q3 = s4 ^ s2;
            const q4 = s3 ^ q0;
            const s8 = u[16] ^ s3;
            const q5 = s6 ^ s8;
            const q6 = u[8] ^ u[12];
            const q7 = u[24] ^ s2;
            const s9 = u[24] ^ s0;
            const q8 = s3 ^ s9;
            const q9 = s4 ^ s6;
            const q10 = s0 ^ s5;
            const q12 = u[28] ^ s2;
            const q13 = u[4] ^ s7;
            const q14 = u[28] ^ s3;
            const q15 = s2 ^ s7;
            const q16 = u[4] ^ s1;
            const q17 = u[4] ^ u[28];
            const q11 = u[20];

            const t20 = q6 & q12;
            const t21 = q3 & q14;
            const t22 = q1 & q16;
            const t23 = q2 & q17;
            const x0 = ((q3 | q14) ^ (q0 & q7)) ^ (t20 ^ t22);
            const x1 = ((q4 | q13) ^ (q10 & q11)) ^ (t21 ^ t20);
            const x2 = ((q2 | q17) ^ (q5 & q9)) ^ (t21 ^ t22);
            const x3 = ((q8 | q15) ^ t23) ^ (t21 ^ (q4 & q13));

            const a = x1 & ~x3;
            const b = x0 & ~x3;
            const c = x3 & ~x1;
            const d = x2 & ~x1;
            const e = x0 ^ a;
            const y0 = x3 ^ (x2 & ~e);
            const f = x1 ^ b;
            const y1 = c ^ (x2 & f);
            const g = x2 ^ c;
            const y2 = x1 ^ (x0 & ~g);
            const h = x3 ^ d;
            const y3 = a ^ (x0 & h);
            const y02 = y2 ^ y0;
            const y13 = y3 ^ y1;
            const y23 = y3 ^ y2;
            const y01 = y1 ^ y0;
            const y00 = y02 ^ y13;

            const a0 = y01 & q11;
            const a1 = y0 & q12;
            const a2 = y1 & q0;
            const a3 = y23 & q17;
            const a4 = y2 & q5;
            const a5 = y3 & q15;
            const a6 = y13 & q14;
            const a7 = y00 & q16;
            const a8 = y02 & q13;
            const a9 = y01 & q7;
            const a10 = y0 & q10;
            const a11 = y1 & q6;
            const a12 = y23 & q2;
            const a13 = y2 & q9;
            const a14 = y3 & q8;
            const a15 = y13 & q3;
            const a16 = y00 & q1;
            const a17 = y02 & q4;

            const r0 = a1 ^ a5;
            const r1 = a9 ^ a15;
            const r2 = a4 ^ r0;
            const r3 = a2 ^ a10;
            const r4 = a11 ^ a17;
            const r5 = a8 ^ r1;
            const r6 = a0 ^ a16;
            const r7 = a7 ^ a13;
            const r8 = a11 ^ a14;
            const r9 = r3 ^ r4;
            const r10 = r5 ^ r6;
            const r11 = r2 ^ r9;
            const r12 = a3 ^ r0;
            const r13 = r7 ^ r8;
            const r14 = r12 ^ r13;
            u[0] = r10 ^ r14;
            const r15 = a6 ^ a10;
            const r16 = r15 ^ r2;
            u[4] = ~(r10 ^ r16);
            u[8] = ~(a2 ^ r2);
            const r17 = a12 ^ a13;
            const r18 = a15 ^ r17;
            u[12] = r18 ^ r11;
            const r19 = a1 ^ a14;
            const r20 = a17 ^ r3;
            const r21 = r7 ^ r19;
            const r22 = r5 ^ r20;
            u[16] = r21 ^ r22;
            const r23 = a9 ^ a12;
            u[20] = r8 ^ r23;
            u[24] = ~(r1 ^ r4);
            u[28] = ~(a16 ^ r11);
        }

        fn subBytes(st: *Bitsliced) void {
            sbox(st[0..]);
            sbox(st[1..]);
            sbox(st[2..]);
            sbox(st[3..]);
        }

        fn shiftRows(st: *Bitsliced) void {
            var i: usize = 0;
            while (i < 32) : (i += 4) {
                st[i + 1] = laneRotl(st[i + 1], 24);
                st[i + 2] = laneRotl(st[i + 2], 16);
                st[i + 3] = laneRotl(st[i + 3], 8);
            }
        }

        fn mixColumns(st: *Bitsliced) void {
            const t2_0 = st[0] ^ st[1];
            const t2_1 = st[1] ^ st[2];
            const t2_2 = st[2] ^ st[3];
            const t2_3 = st[3] ^ st[0];
            var t0_0 = st[28] ^ st[29];
            var t0_1 = st[29] ^ st[30];
            var t0_2 = st[30] ^ st[31];
            var t0_3 = st[31] ^ st[28];
            var t = st[28];
            st[28] = t2_0 ^ t0_2 ^ st[29];
            st[29] = t2_1 ^ t0_2 ^ t;
            t = st[30];
            st[30] = t2_2 ^ t0_0 ^ st[31];
            st[31] = t2_3 ^ t0_0 ^ t;
            var t1_0 = st[24] ^ st[25];
            var t1_1 = st[25] ^ st[26];
            var t1_2 = st[26] ^ st[27];
            var t1_3 = st[27] ^ st[24];
            t = st[24];
            st[24] = t0_0 ^ t2_0 ^ st[25] ^ t1_2;
            var t_bis = st[25];
            st[25] = t0_1 ^ t2_1 ^ t1_2 ^ t;
            t = st[26];
            st[26] = t0_2 ^ t2_2 ^ t1_3 ^ t_bis;
            st[27] = t0_3 ^ t2_3 ^ t1_0 ^ t;
            t0_0 = st[20] ^ st[21];
            t0_1 = st[21] ^ st[22];
            t0_2 = st[22] ^ st[23];
            t0_3 = st[23] ^ st[20];
            t = st[20];
            st[20] = t1_0 ^ t0_1 ^ st[23];
            t_bis = st[21];
            st[21] = t1_1 ^ t0_2 ^ t;
            t = st[22];
            st[22] = t1_2 ^ t0_3 ^ t_bis;
            st[23] = t1_3 ^ t0_0 ^ t;
            t1_0 = st[16] ^ st[17];
            t1_1 = st[17] ^ st[18];
            t1_2 = st[18] ^ st[19];
            t1_3 = st[19] ^ st[16];
            t = st[16];
            st[16] = t0_0 ^ t2_0 ^ t1_1 ^ st[19];
            t_bis = st[17];
            st[17] = t0_1 ^ t2_1 ^ t1_2 ^ t;
            t = st[18];
            st[18] = t0_2 ^ t2_2 ^ t1_3 ^ t_bis;
            st[19] = t0_3 ^ t2_3 ^ t1_0 ^ t;
            t0_0 = st[12] ^ st[13];
            t0_1 = st[13] ^ st[14];
            t0_2 = st[14] ^ st[15];
            t0_3 = st[15] ^ st[12];
            t = st[12];
            st[12] = t1_0 ^ t2_0 ^ t0_1 ^ st[15];
            t_bis = st[13];
            st[13] = t1_1 ^ t2_1 ^ t0_2 ^ t;
            t = st[14];
            st[14] = t1_2 ^ t2_2 ^ t0_3 ^ t_bis;
            st[15] = t1_3 ^ t2_3 ^ t0_0 ^ t;
            t1_0 = st[8] ^ st[9];
            t1_1 = st[9] ^ st[10];
            t1_2 = st[10] ^ st[11];
            t1_3 = st[11] ^ st[8];
            t = st[8];
            st[8] = t0_0 ^ t1_1 ^ st[11];
            t_bis = st[9];
            st[9] = t0_1 ^ t1_2 ^ t;
            t = st[10];
            st[10] = t0_2 ^ t1_3 ^ t_bis;
            st[11] = t0_3 ^ t1_0 ^ t;
            t0_0 = st[4] ^ st[5];
            t0_1 = st[5] ^ st[6];
            t0_2 = st[6] ^ st[7];
            t0_3 = st[7] ^ st[4];
            t = st[4];
            st[4] = t1_0 ^ t0_1 ^ st[7];
            t_bis = st[5];
            st[5] = t1_1 ^ t0_2 ^ t;
            t = st[6];
            st[6] = t1_2 ^ t0_3 ^ t_bis;
            st[7] = t1_3 ^ t0_0 ^ t;
            t = st[0];
            st[0] = t0_0 ^ t2_1 ^ st[3];
            t_bis = st[1];
            st[1] = t0_1 ^ t2_2 ^ t;
            t = st[2];
            st[2] = t0_2 ^ t2_3 ^ t_bis;
            st[3] = t0_3 ^ t2_0 ^ t;
        }

        /// 对整个批次执行一轮无密钥 AES(SubBytes、ShiftRows、MixColumns)
        pub fn round(st: *Bitsliced) void {
            subBytes(st);
            shiftRows(st);
            mixColumns(st);
        }

        /// 添加轮密钥,该密钥也必须采用位切片形式
        pub fn addRoundKey(st: *Bitsliced, round_key: *const Bitsliced) void {
            for (0..32) |i| st[i] ^= round_key[i];
        }

        /// 正交化(将字中的块转换为位切片)
        pub fn packState(st: *Bitsliced) void {
            var i: usize = 0;
            while (i < 32) : (i += 4) {
                swapMove(&st[i], &st[i + 1], 0x00ff00ff, 8);
                swapMove(&st[i + 2], &st[i + 3], 0x00ff00ff, 8);
                swapMove(&st[i], &st[i + 2], 0x0000ffff, 16);
                swapMove(&st[i + 1], &st[i + 3], 0x0000ffff, 16);
            }
            for (0..4) |k| {
                swapMove(&st[k + 4], &st[k], 0x55555555, 1);
                swapMove(&st[k + 12], &st[k + 8], 0x55555555, 1);
                swapMove(&st[k + 20], &st[k + 16], 0x55555555, 1);
                swapMove(&st[k + 28], &st[k + 24], 0x55555555, 1);
                swapMove(&st[k + 8], &st[k], 0x33333333, 2);
                swapMove(&st[k + 12], &st[k + 4], 0x33333333, 2);
                swapMove(&st[k + 24], &st[k + 16], 0x33333333, 2);
                swapMove(&st[k + 28], &st[k + 20], 0x33333333, 2);
                swapMove(&st[k + 16], &st[k], 0x0f0f0f0f, 4);
                swapMove(&st[k + 20], &st[k + 4], 0x0f0f0f0f, 4);
                swapMove(&st[k + 24], &st[k + 8], 0x0f0f0f0f, 4);
                swapMove(&st[k + 28], &st[k + 12], 0x0f0f0f0f, 4);
            }
        }

        /// `packState` 的逆操作
        pub fn unpackState(st: *Bitsliced) void {
            for (0..4) |k| {
                swapMove(&st[k + 4], &st[k], 0x55555555, 1);
                swapMove(&st[k + 12], &st[k + 8], 0x55555555, 1);
                swapMove(&st[k + 20], &st[k + 16], 0x55555555, 1);
                swapMove(&st[k + 28], &st[k + 24], 0x55555555, 1);
                swapMove(&st[k + 8], &st[k], 0x33333333, 2);
                swapMove(&st[k + 12], &st[k + 4], 0x33333333, 2);
                swapMove(&st[k + 24], &st[k + 16], 0x33333333, 2);
                swapMove(&st[k + 28], &st[k + 20], 0x33333333, 2);
                swapMove(&st[k + 16], &st[k], 0x0f0f0f0f, 4);
                swapMove(&st[k + 20], &st[k + 4], 0x0f0f0f0f, 4);
                swapMove(&st[k + 24], &st[k + 8], 0x0f0f0f0f, 4);
                swapMove(&st[k + 28], &st[k + 12], 0x0f0f0f0f, 4);
            }
            var i: usize = 0;
            while (i < 32) : (i += 4) {
                swapMove(&st[i], &st[i + 2], 0x0000ffff, 16);
                swapMove(&st[i + 1], &st[i + 3], 0x0000ffff, 16);
                swapMove(&st[i], &st[i + 1], 0x00ff00ff, 8);
                swapMove(&st[i + 2], &st[i + 3], 0x00ff00ff, 8);
            }
        }

        /// 将 `nblocks` 个小端序块转换为打包状态。
        /// 块 `8*g .. 8*g+7` 放入每个字的通道 `g` 中。
        fn pack(bytes: *const [nblocks * 16]u8) Bitsliced {
            var st: Bitsliced = @splat(0);
            inline for (0..nblocks) |bl| {
                const g = bl / 8;
                const p = bl % 8;
                inline for (0..4) |wd| {
                    const v = mem.readInt(u32, bytes[bl * 16 + wd * 4 ..][0..4], .little);
                    st[p * 4 + wd] |= @as(Word, v) << (32 * g);
                }
            }
            packState(&st);
            return st;
        }

        /// `pack` 的逆操作
        fn unpack(st: *Bitsliced) [nblocks * 16]u8 {
            unpackState(st);
            var bytes: [nblocks * 16]u8 = undefined;
            inline for (0..nblocks) |bl| {
                const g = bl / 8;
                const p = bl % 8;
                inline for (0..4) |wd| {
                    const v: u32 = @truncate(st[p * 4 + wd] >> (32 * g));
                    mem.writeInt(u32, bytes[bl * 16 + wd * 4 ..][0..4], v, .little);
                }
            }
            return bytes;
        }

        /// 预计算轮密钥的第 `i` 位在打包后的落点。
        const broadcast_loc = blk: {
            @setEvalBranchQuota(200000);
            var loc: [128]struct { word: u8, shift: u5 } = undefined;
            for (0..128) |i| {
                var st: Bitsliced = @splat(0);
                const word = (i / 8) / 4;
                const bit: u5 = @intCast(((i / 8) % 4) * 8 + (i % 8));
                for (0..8) |b| st[b * 4 + word] |= @as(Word, 1) << bit;
                packState(&st);
                found: for (0..32) |w| {
                    for (0..4) |byte| {
                        if ((st[w] >> @intCast(byte * 8)) & 0xff == 0xff) {
                            loc[i] = .{ .word = @intCast(w), .shift = @intCast(byte * 8) };
                            break :found;
                        }
                    }
                }
            }
            break :blk loc;
        };

        /// 打包轮密钥并广播到全部数据块,以便直接与打包状态进行异或。
        fn packRoundKey(round_key: *const [16]u8) Bitsliced {
            var st: Bitsliced = @splat(0);
            inline for (0..16) |pos| {
                const byte = round_key[pos];
                inline for (0..8) |bit| {
                    const smear: Word = 0 -% @as(Word, (byte >> bit) & 1);
                    const loc = broadcast_loc[pos * 8 + bit];
                    st[loc.word] |= smear & (splatWord(0xff) << loc.shift);
                }
            }
            return st;
        }

        /// 将完整密钥调度打包为位切片表示。
        pub fn packKeys(round_keys: anytype) [round_keys.len]Bitsliced {
            var keys: [round_keys.len]Bitsliced = undefined;
            inline for (&keys, 0..) |*k, r| {
                const bytes = round_keys[r].toBytes();
                k.* = packRoundKey(&bytes);
            }
            return keys;
        }

        /// 对整个批次原地运行完整 AES 密码运算。
        pub fn encrypt(comptime rounds: usize, keys: *const [rounds + 1]Bitsliced, data: *[nblocks * 16]u8) void {
            var st = pack(data);
            addRoundKey(&st, &keys[0]);
            inline for (1..rounds) |r| {
                round(&st);
                addRoundKey(&st, &keys[r]);
            }
            subBytes(&st);
            shiftRows(&st);
            addRoundKey(&st, &keys[rounds]);
            data.* = unpack(&st);
        }

        fn invShiftRows(st: *Bitsliced) void {
            var i: usize = 0;
            while (i < 32) : (i += 4) {
                st[i + 1] = laneRotl(st[i + 1], 8);
                st[i + 2] = laneRotl(st[i + 2], 16);
                st[i + 3] = laneRotl(st[i + 3], 24);
            }
        }

        /// 预计算 `mixColumns` 的逆矩阵
        const mix_inv: [32]u32 = blk: {
            @setEvalBranchQuota(200000);
            var m: [32]u32 = @splat(0);
            for (0..32) |i| {
                var st: Bitsliced = @splat(0);
                st[i] = 1;
                mixColumns(&st);
                for (0..32) |o| {
                    if (st[o] & 1 == 1) m[o] |= @as(u32, 1) << @intCast(i);
                }
            }
            var inv: [32]u32 = undefined;
            for (0..32) |k| inv[k] = @as(u32, 1) << @intCast(k);
            for (0..32) |col| {
                var piv = col;
                while (m[piv] & (@as(u32, 1) << @intCast(col)) == 0) piv += 1;
                const tm = m[col];
                m[col] = m[piv];
                m[piv] = tm;
                const ti = inv[col];
                inv[col] = inv[piv];
                inv[piv] = ti;
                for (0..32) |r| {
                    if (r != col and (m[r] & (@as(u32, 1) << @intCast(col))) != 0) {
                        m[r] ^= m[col];
                        inv[r] ^= inv[col];
                    }
                }
            }
            break :blk inv;
        };

        fn invMixColumns(st: *Bitsliced) void {
            @setEvalBranchQuota(20000);
            var out: Bitsliced = @splat(0);
            inline for (0..32) |o| {
                inline for (0..32) |j| {
                    if (mix_inv[o] & (@as(u32, 1) << @intCast(j)) != 0) out[o] ^= st[j];
                }
            }
            st.* = out;
        }

        /// 逆向 S 盒的线性映射
        fn invAffineGroup(st: *Bitsliced, comptime g: usize) void {
            const p = [8]usize{ g + 28, g + 24, g + 20, g + 16, g + 12, g + 8, g + 4, g };
            const a0 = ~st[p[0]];
            const a1 = ~st[p[1]];
            const a2 = st[p[2]];
            const a3 = st[p[3]];
            const a4 = st[p[4]];
            const a5 = ~st[p[5]];
            const a6 = ~st[p[6]];
            const a7 = st[p[7]];
            st[p[7]] = a1 ^ a4 ^ a6;
            st[p[6]] = a0 ^ a3 ^ a5;
            st[p[5]] = a7 ^ a2 ^ a4;
            st[p[4]] = a6 ^ a1 ^ a3;
            st[p[3]] = a5 ^ a0 ^ a2;
            st[p[2]] = a4 ^ a7 ^ a1;
            st[p[1]] = a3 ^ a6 ^ a0;
            st[p[0]] = a2 ^ a5 ^ a7;
        }

        /// 紧凑逆 S 盒:先应用固定线性映射,再应用正向 S 盒,最后应用同一线性映射
        /// 此技巧来自 Thomas Pornin
        fn invSubBytes(st: *Bitsliced) void {
            inline for (0..4) |g| invAffineGroup(st, g);
            subBytes(st);
            inline for (0..4) |g| invAffineGroup(st, g);
        }

        /// 对位切片表示的整个批次执行完整的逆 AES 密码运算。
        pub fn decrypt(comptime rounds: usize, keys: *const [rounds + 1]Bitsliced, data: *[nblocks * 16]u8) void {
            var st = pack(data);
            addRoundKey(&st, &keys[rounds]);
            comptime var r: usize = rounds - 1;
            inline while (r > 0) : (r -= 1) {
                invShiftRows(&st);
                invSubBytes(&st);
                addRoundKey(&st, &keys[r]);
                invMixColumns(&st);
            }
            invShiftRows(&st);
            invSubBytes(&st);
            addRoundKey(&st, &keys[0]);
            data.* = unpack(&st);
        }
    };
}

const testing = std.testing;

test "bitslice cross-check against table AES" {
    const key: [16]u8 = @splat(0x37);
    const ref = std.crypto.core.aes.Aes128.initEnc(key);
    const keys = Wide.packKeys(ref.key_schedule.round_keys[0..]);

    var data: [width * 16]u8 = undefined;
    for (0..width * 16) |i| data[i] = @truncate(i *% 101 +% 7);

    var expected: [width * 16]u8 = undefined;
    for (0..width) |b| {
        var blk: [16]u8 = data[b * 16 ..][0..16].*;
        ref.encrypt(&blk, &blk);
        expected[b * 16 ..][0..16].* = blk;
    }

    Wide.encrypt(10, &keys, &data);
    try testing.expectEqualSlices(u8, &expected, &data);
}

test "bitslice decrypt cross-check against table AES" {
    inline for (.{ std.crypto.core.aes.Aes128, std.crypto.core.aes.Aes256 }) |Aes| {
        const key: [Aes.key_bits / 8]u8 = @splat(0x4d);
        const enc = Aes.initEnc(key);
        const dec = Aes.initDec(key);
        const keys = Wide.packKeys(enc.key_schedule.round_keys[0..]);

        var data: [width * 16]u8 = undefined;
        for (0..width * 16) |i| data[i] = @truncate(i *% 211 +% 13);

        var expected: [width * 16]u8 = undefined;
        for (0..width) |b| {
            var blk: [16]u8 = data[b * 16 ..][0..16].*;
            dec.decrypt(&blk, &blk);
            expected[b * 16 ..][0..16].* = blk;
        }

        Wide.decrypt(Aes.rounds, &keys, &data);
        try testing.expectEqualSlices(u8, &expected, &data);
    }
}