跳转到内容

std.crypto.ghash_polyval

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

第 1 行

std.crypto.ghash_polyval.assert

别名

第 3 行

别名目标 std.debug.assert

std.crypto.ghash_polyval.builtin

常量

第 2 行

std.crypto.ghash_polyval.Ghash

类型

第 15 行

GHASH 是通用哈希函数,在伽罗瓦域中与固定 参数相乘。

它不是一般用途的哈希函数;密钥必须保密、不可预测,且绝不能复用。

GHASH 通常用于计算 AES-GCM 构造中的认证标签。

std.crypto.ghash_polyval.Hash

类型函数

第 25 行

fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type

参数

endian: std.builtin.Endian
shift_key: bool

字段

hx: [pc_count]Precomp
acc: u128 = 0
leftover: usize = 0
buf: [block_length]u8 align(16) = undefined

std.crypto.ghash_polyval.Hash.agg_16_threshold

常量

第 36 行

std.crypto.ghash_polyval.Hash.agg_4_threshold

常量

第 34 行

std.crypto.ghash_polyval.Hash.agg_8_threshold

常量

第 35 行

std.crypto.ghash_polyval.Hash.block_length

常量

第 29 行

usize

std.crypto.ghash_polyval.Hash.blocks

函数

第 300 行

fn blocks(st: *Self, msg: []const u8) void

参数

st: *Self
msg: []const u8

std.crypto.ghash_polyval.Hash.clmul

常量

第 291 行

std.crypto.ghash_polyval.Hash.clmul128

函数

第 252 行

fn clmul128(x: u128, y: u128) I256

参数

x: u128
y: u128

std.crypto.ghash_polyval.Hash.clmulPclmul

函数

第 92 行

fn clmulPclmul(x: u128, y: u128, comptime half: Selector) u128

参数

x: u128
y: u128
half: Selector

std.crypto.ghash_polyval.Hash.clmulPmull

函数

第 125 行

fn clmulPmull(x: u128, y: u128, comptime half: Selector) u128

参数

x: u128
y: u128
half: Selector

std.crypto.ghash_polyval.Hash.clmulSoft

常量

第 158 行

在没有原生 128 位寄存器的平台上,clmulSoft128_64 更快。

std.crypto.ghash_polyval.Hash.clmulSoft128

函数

第 164 行

fn clmulSoft128(x_: u128, y_: u128, comptime half: Selector) u128

参数

x_: u128
y_: u128
half: Selector

std.crypto.ghash_polyval.Hash.clmulSoft128_64

函数

第 213 行

fn clmulSoft128_64(x_: u128, y_: u128, comptime half: Selector) u128

参数

x_: u128
y_: u128
half: Selector

std.crypto.ghash_polyval.Hash.clmulSoft32

函数

第 195 行

fn clmulSoft32(x: u32, y: u32) u64

参数

x: u32
y: u32

std.crypto.ghash_polyval.Hash.clsq128

函数

第 243 行

fn clsq128(x: u128) I256

参数

x: u128

std.crypto.ghash_polyval.Hash.create

函数

第 415 行

计算消息的 GHASH。

pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void

参数

out: *[mac_length]u8
msg: []const u8
key: *const [key_length]u8

std.crypto.ghash_polyval.Hash.final

函数

第 401 行

计算整个输入的 GHASH。

pub fn final(st: *Self, out: *[mac_length]u8) void

参数

st: *Self
out: *[mac_length]u8

std.crypto.ghash_polyval.Hash.finalResult

函数

第 408 行

pub fn finalResult(d: *Self) [mac_length]u8

参数

d: *Self

std.crypto.ghash_polyval.Hash.has_armaes

常量

第 289 行

std.crypto.ghash_polyval.Hash.has_avx

常量

第 288 行

std.crypto.ghash_polyval.Hash.has_pclmul

常量

第 287 行

std.crypto.ghash_polyval.Hash.I256

容器

第 228 行

字段

hi: u128
lo: u128
mid: u128

std.crypto.ghash_polyval.Hash.init

函数

第 85 行

用密钥初始化 GHASH 状态。

pub fn init(key: *const [key_length]u8) Self

参数

key: *const [key_length]u8

std.crypto.ghash_polyval.Hash.initForBlockCount

函数

第 53 行

使用密钥及最小数据块数量初始化 GHASH 状态。

pub fn initForBlockCount(key: *const [key_length]u8, block_count: usize) Self

参数

key: *const [key_length]u8
block_count: usize

std.crypto.ghash_polyval.Hash.key_length

常量

第 31 行

std.crypto.ghash_polyval.Hash.mac_length

常量

第 30 行

std.crypto.ghash_polyval.Hash.mul_algorithm

常量

第 44 行

std.crypto.ghash_polyval.Hash.pad

函数

第 388 行

补零,使下一个输入与块的首字节对齐

pub fn pad(st: *Self) void

参数

st: *Self

std.crypto.ghash_polyval.Hash.pc_count

常量

第 33 行

std.crypto.ghash_polyval.Hash.reduce

函数

第 276 行

fn reduce(x: I256) u128

参数

x: I256

std.crypto.ghash_polyval.Hash.Selector

容器

第 89 行

字段

lo
hi
hi_lo

std.crypto.ghash_polyval.Hash.Self

别名

第 27 行

别名目标 std.crypto.ghash_polyval.Hash

std.crypto.ghash_polyval.Hash.update

函数

第 357 行

将消息吸收到 GHASH 状态中。

pub fn update(st: *Self, m: []const u8) void

参数

st: *Self
m: []const u8

std.crypto.ghash_polyval.Hash.xor256

函数

第 234 行

fn xor256(x: *I256, y: I256) void

参数

x: *I256
y: I256

std.crypto.ghash_polyval.htest

常量

第 423 行

std.crypto.ghash_polyval.math

别名

第 4 行

别名目标 std.math

std.crypto.ghash_polyval.mem

别名

第 5 行

别名目标 std.mem

std.crypto.ghash_polyval.Polyval

类型

第 23 行

POLYVAL 是一种通用哈希函数,在伽罗瓦域内使用固定 参数进行乘法。

它并非一般用途的哈希函数:密钥必须保密、不可预测,且绝不重复使用。

POLYVAL 通常用于计算 AES-GCM-SIV 构造中的认证标签。

std.crypto.ghash_polyval.Precomp

类型

第 7 行

std.crypto.ghash_polyval.std

别名

第 1 行

别名目标 std

源码

std/crypto/ghash_polyval.zig

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

const Precomp = u128;

/// GHASH 是通用哈希函数,在伽罗瓦域中与固定
/// 参数相乘。
///
/// 它不是一般用途的哈希函数;密钥必须保密、不可预测,且绝不能复用。
///
/// GHASH 通常用于计算 AES-GCM 构造中的认证标签。
pub const Ghash = Hash(.big, true);

/// POLYVAL 是一种通用哈希函数,在伽罗瓦域内使用固定
/// 参数进行乘法。
///
/// 它并非一般用途的哈希函数:密钥必须保密、不可预测,且绝不重复使用。
///
/// POLYVAL 通常用于计算 AES-GCM-SIV 构造中的认证标签。
pub const Polyval = Hash(.little, false);

fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type {
    return struct {
        const Self = @This();

        pub const block_length: usize = 16;
        pub const mac_length = 16;
        pub const key_length = 16;

        const pc_count = if (builtin.mode != .small) 16 else 2;
        const agg_4_threshold = 22;
        const agg_8_threshold = 84;
        const agg_16_threshold = 328;

        // Haswell 架构之前,无进位乘法指令
        // 非常慢。因此即使操作数为 128 位,Karatsuba 乘法
        // 也比朴素乘法更快。
        // 现在已非如此:现代 CPU,包括基于 ARM 的 CPU,均有快速
        // 无进位乘法指令;执行 4 次乘法如今比
        // 执行 3 次乘法再加额外移位和加法更快。
        const mul_algorithm = if (builtin.cpu.arch == .x86) .karatsuba else .schoolbook;

        hx: [pc_count]Precomp,
        acc: u128 = 0,

        leftover: usize = 0,
        buf: [block_length]u8 align(16) = undefined,

        /// 使用密钥及最小数据块数量初始化 GHASH 状态。
        pub fn initForBlockCount(key: *const [key_length]u8, block_count: usize) Self {
            var h = mem.readInt(u128, key[0..16], endian);
            if (shift_key) {
                // 将密钥左移 1 位,并按 GCM 规则约减。
                const carry = ((@as(u128, 0xc2) << 120) | 1) & (@as(u128, 0) -% (h >> 127));
                h = (h << 1) ^ carry;
            }
            var hx: [pc_count]Precomp = undefined;
            hx[0] = h;
            hx[1] = reduce(clsq128(hx[0])); // h^2

            if (builtin.mode != .small) {
                hx[2] = reduce(clmul128(hx[1], h)); // h^3
                hx[3] = reduce(clsq128(hx[1])); // h^4 = h^2^2
                if (block_count >= agg_8_threshold) {
                    hx[4] = reduce(clmul128(hx[3], h)); // h^5
                    hx[5] = reduce(clsq128(hx[2])); // h^6 = h^3^2
                    hx[6] = reduce(clmul128(hx[5], h)); // h^7
                    hx[7] = reduce(clsq128(hx[3])); // h^8 = h^4^2
                }
                if (block_count >= agg_16_threshold) {
                    var i: usize = 8;
                    while (i < 16) : (i += 2) {
                        hx[i] = reduce(clmul128(hx[i - 1], h));
                        hx[i + 1] = reduce(clsq128(hx[i / 2]));
                    }
                }
            }
            return Self{ .hx = hx };
        }

        /// 用密钥初始化 GHASH 状态。
        pub fn init(key: *const [key_length]u8) Self {
            return Self.initForBlockCount(key, math.maxInt(usize));
        }

        const Selector = enum { lo, hi, hi_lo };

        // 适用于 x86_64 的两个 64 位整数的无进位乘法。
        fn clmulPclmul(x: u128, y: u128, comptime half: Selector) u128 {
            switch (half) {
                .hi => {
                    const product = asm (
                        \\ vpclmulqdq $0x11, %[x], %[y], %[out]
                        : [out] "=x" (-> @Vector(2, u64)),
                        : [x] "x" (@as(@Vector(2, u64), @bitCast(x))),
                          [y] "x" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
                .lo => {
                    const product = asm (
                        \\ vpclmulqdq $0x00, %[x], %[y], %[out]
                        : [out] "=x" (-> @Vector(2, u64)),
                        : [x] "x" (@as(@Vector(2, u64), @bitCast(x))),
                          [y] "x" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
                .hi_lo => {
                    const product = asm (
                        \\ vpclmulqdq $0x10, %[x], %[y], %[out]
                        : [out] "=x" (-> @Vector(2, u64)),
                        : [x] "x" (@as(@Vector(2, u64), @bitCast(x))),
                          [y] "x" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
            }
        }

        // 用于 ARM 加密的两个 64 位整数的无进位乘法。
        fn clmulPmull(x: u128, y: u128, comptime half: Selector) u128 {
            switch (half) {
                .hi => {
                    const product = asm (
                        \\ pmull2 %[out].1q, %[x].2d, %[y].2d
                        : [out] "=w" (-> @Vector(2, u64)),
                        : [x] "w" (@as(@Vector(2, u64), @bitCast(x))),
                          [y] "w" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
                .lo => {
                    const product = asm (
                        \\ pmull %[out].1q, %[x].1d, %[y].1d
                        : [out] "=w" (-> @Vector(2, u64)),
                        : [x] "w" (@as(@Vector(2, u64), @bitCast(x))),
                          [y] "w" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
                .hi_lo => {
                    const product = asm (
                        \\ pmull %[out].1q, %[x].1d, %[y].1d
                        : [out] "=w" (-> @Vector(2, u64)),
                        : [x] "w" (@as(@Vector(2, u64), @bitCast(x >> 64))),
                          [y] "w" (@as(@Vector(2, u64), @bitCast(y))),
                    );
                    return @as(u128, @bitCast(product));
                },
            }
        }

        /// 在没有原生 128 位寄存器的平台上,clmulSoft128_64 更快。
        const clmulSoft = switch (builtin.cpu.arch) {
            .wasm32, .wasm64 => clmulSoft128_64,
            else => if (std.simd.suggestVectorLength(u128) != null) clmulSoft128 else clmulSoft128_64,
        };

        // 使用原生 128 位寄存器,以软件实现两个 64 位整数的无进位乘法。
        fn clmulSoft128(x_: u128, y_: u128, comptime half: Selector) u128 {
            const x = @as(u64, @truncate(if (half == .hi or half == .hi_lo) x_ >> 64 else x_));
            const y = @as(u64, @truncate(if (half == .hi) y_ >> 64 else y_));

            const x0 = x & 0x1111111111111110;
            const x1 = x & 0x2222222222222220;
            const x2 = x & 0x4444444444444440;
            const x3 = x & 0x8888888888888880;
            const y0 = y & 0x1111111111111111;
            const y1 = y & 0x2222222222222222;
            const y2 = y & 0x4444444444444444;
            const y3 = y & 0x8888888888888888;
            const z0 = (x0 * @as(u128, y0)) ^ (x1 * @as(u128, y3)) ^ (x2 * @as(u128, y2)) ^ (x3 * @as(u128, y1));
            const z1 = (x0 * @as(u128, y1)) ^ (x1 * @as(u128, y0)) ^ (x2 * @as(u128, y3)) ^ (x3 * @as(u128, y2));
            const z2 = (x0 * @as(u128, y2)) ^ (x1 * @as(u128, y1)) ^ (x2 * @as(u128, y0)) ^ (x3 * @as(u128, y3));
            const z3 = (x0 * @as(u128, y3)) ^ (x1 * @as(u128, y2)) ^ (x2 * @as(u128, y1)) ^ (x3 * @as(u128, y0));

            const x0_mask = @as(u64, 0) -% (x & 1);
            const x1_mask = @as(u64, 0) -% ((x >> 1) & 1);
            const x2_mask = @as(u64, 0) -% ((x >> 2) & 1);
            const x3_mask = @as(u64, 0) -% ((x >> 3) & 1);
            const extra = (x0_mask & y) ^ (@as(u128, x1_mask & y) << 1) ^
                (@as(u128, x2_mask & y) << 2) ^ (@as(u128, x3_mask & y) << 3);

            return (z0 & 0x11111111111111111111111111111111) ^
                (z1 & 0x22222222222222222222222222222222) ^
                (z2 & 0x44444444444444444444444444444444) ^
                (z3 & 0x88888888888888888888888888888888) ^ extra;
        }

        // 两个 32 位整数的无进位乘法的软件实现。
        fn clmulSoft32(x: u32, y: u32) u64 {
            const mulWide = math.mulWide;
            const a0 = x & 0x11111111;
            const a1 = x & 0x22222222;
            const a2 = x & 0x44444444;
            const a3 = x & 0x88888888;
            const b0 = y & 0x11111111;
            const b1 = y & 0x22222222;
            const b2 = y & 0x44444444;
            const b3 = y & 0x88888888;
            const c0 = mulWide(u32, a0, b0) ^ mulWide(u32, a1, b3) ^ mulWide(u32, a2, b2) ^ mulWide(u32, a3, b1);
            const c1 = mulWide(u32, a0, b1) ^ mulWide(u32, a1, b0) ^ mulWide(u32, a2, b3) ^ mulWide(u32, a3, b2);
            const c2 = mulWide(u32, a0, b2) ^ mulWide(u32, a1, b1) ^ mulWide(u32, a2, b0) ^ mulWide(u32, a3, b3);
            const c3 = mulWide(u32, a0, b3) ^ mulWide(u32, a1, b2) ^ mulWide(u32, a2, b1) ^ mulWide(u32, a3, b0);
            return (c0 & 0x1111111111111111) | (c1 & 0x2222222222222222) | (c2 & 0x4444444444444444) | (c3 & 0x8888888888888888);
        }

        // 使用 64 位寄存器,以软件实现两个 128 位整数的无进位乘法。
        fn clmulSoft128_64(x_: u128, y_: u128, comptime half: Selector) u128 {
            const a = @as(u64, @truncate(if (half == .hi or half == .hi_lo) x_ >> 64 else x_));
            const b = @as(u64, @truncate(if (half == .hi) y_ >> 64 else y_));
            const a0 = @as(u32, @truncate(a));
            const a1 = @as(u32, @truncate(a >> 32));
            const b0 = @as(u32, @truncate(b));
            const b1 = @as(u32, @truncate(b >> 32));
            const lo = clmulSoft32(a0, b0);
            const hi = clmulSoft32(a1, b1);
            const mid = clmulSoft32(a0 ^ a1, b0 ^ b1) ^ lo ^ hi;
            const res_lo = lo ^ (mid << 32);
            const res_hi = hi ^ (mid >> 32);
            return @as(u128, res_lo) | (@as(u128, res_hi) << 64);
        }

        const I256 = struct {
            hi: u128,
            lo: u128,
            mid: u128,
        };

        fn xor256(x: *I256, y: I256) void {
            x.* = I256{
                .hi = x.hi ^ y.hi,
                .lo = x.lo ^ y.lo,
                .mid = x.mid ^ y.mid,
            };
        }

        // 在 GF(2^128) 中计算 128 位整数的平方。
        fn clsq128(x: u128) I256 {
            return .{
                .hi = clmul(x, x, .hi),
                .lo = clmul(x, x, .lo),
                .mid = 0,
            };
        }

        // 在 GF(2^128) 中乘以两个 128 位整数。
        fn clmul128(x: u128, y: u128) I256 {
            if (mul_algorithm == .karatsuba) {
                const x_hi = @as(u64, @truncate(x >> 64));
                const y_hi = @as(u64, @truncate(y >> 64));
                const r_lo = clmul(x, y, .lo);
                const r_hi = clmul(x, y, .hi);
                const r_mid = clmul(x ^ x_hi, y ^ y_hi, .lo) ^ r_lo ^ r_hi;
                return .{
                    .hi = r_hi,
                    .lo = r_lo,
                    .mid = r_mid,
                };
            } else {
                return .{
                    .hi = clmul(x, y, .hi),
                    .lo = clmul(x, y, .lo),
                    .mid = clmul(x, y, .hi_lo) ^ clmul(y, x, .hi_lo),
                };
            }
        }

        // 将多项式的 256 位表示模不可约多项式 x^128 + x^127 + x^126 + x^121 + 1 约简。
        // 采用 Shay Gueron 的技巧,其原理解释见:
        // https://blog.quarkslab.com/reversing-a-finite-field-multiplication-optimization.html
        fn reduce(x: I256) u128 {
            const hi = x.hi ^ (x.mid >> 64);
            const lo = x.lo ^ (x.mid << 64);
            const p64 = (((1 << 121) | (1 << 126) | (1 << 127)) >> 64);
            const a = clmul(lo, p64, .lo);
            const b = ((lo << 64) | (lo >> 64)) ^ a;
            const c = clmul(b, p64, .lo);
            const d = ((b << 64) | (b >> 64)) ^ c;
            return d ^ hi;
        }

        const has_pclmul = builtin.cpu.has(.x86, .pclmul);
        const has_avx = builtin.cpu.has(.x86, .avx);
        const has_armaes = builtin.cpu.has(.aarch64, .aes);
        // C 后端目前不支持将向量传递给内联汇编。
        const clmul = if (builtin.cpu.arch == .x86_64 and builtin.zig_backend != .stage2_c and has_pclmul and has_avx) impl: {
            break :impl clmulPclmul;
        } else if (builtin.cpu.arch == .aarch64 and builtin.zig_backend != .stage2_c and has_armaes) impl: {
            break :impl clmulPmull;
        } else impl: {
            break :impl clmulSoft;
        };

        // 处理 16 字节数据块。
        fn blocks(st: *Self, msg: []const u8) void {
            assert(msg.len % 16 == 0); // GHASH 的 blocks() 要求完整数据块
            var acc = st.acc;

            var i: usize = 0;

            if (builtin.mode != .small and msg.len >= agg_16_threshold * block_length) {
                // 16 块聚合约减
                while (i + 256 <= msg.len) : (i += 256) {
                    var u = clmul128(acc ^ mem.readInt(u128, msg[i..][0..16], endian), st.hx[15 - 0]);
                    comptime var j = 1;
                    inline while (j < 16) : (j += 1) {
                        xor256(&u, clmul128(mem.readInt(u128, msg[i..][j * 16 ..][0..16], endian), st.hx[15 - j]));
                    }
                    acc = reduce(u);
                }
            } else if (builtin.mode != .small and msg.len >= agg_8_threshold * block_length) {
                // 8 个数据块的聚合约减
                while (i + 128 <= msg.len) : (i += 128) {
                    var u = clmul128(acc ^ mem.readInt(u128, msg[i..][0..16], endian), st.hx[7 - 0]);
                    comptime var j = 1;
                    inline while (j < 8) : (j += 1) {
                        xor256(&u, clmul128(mem.readInt(u128, msg[i..][j * 16 ..][0..16], endian), st.hx[7 - j]));
                    }
                    acc = reduce(u);
                }
            } else if (builtin.mode != .small and msg.len >= agg_4_threshold * block_length) {
                // 4 块聚合约减
                while (i + 64 <= msg.len) : (i += 64) {
                    var u = clmul128(acc ^ mem.readInt(u128, msg[i..][0..16], endian), st.hx[3 - 0]);
                    comptime var j = 1;
                    inline while (j < 4) : (j += 1) {
                        xor256(&u, clmul128(mem.readInt(u128, msg[i..][j * 16 ..][0..16], endian), st.hx[3 - j]));
                    }
                    acc = reduce(u);
                }
            }
            // 聚合两个分组的约减
            while (i + 32 <= msg.len) : (i += 32) {
                var u = clmul128(acc ^ mem.readInt(u128, msg[i..][0..16], endian), st.hx[1 - 0]);
                comptime var j = 1;
                inline while (j < 2) : (j += 1) {
                    xor256(&u, clmul128(mem.readInt(u128, msg[i..][j * 16 ..][0..16], endian), st.hx[1 - j]));
                }
                acc = reduce(u);
            }
            // 剩余数据块
            if (i < msg.len) {
                const u = clmul128(acc ^ mem.readInt(u128, msg[i..][0..16], endian), st.hx[0]);
                acc = reduce(u);
                i += 16;
            }
            assert(i == msg.len);
            st.acc = acc;
        }

        /// 将消息吸收到 GHASH 状态中。
        pub fn update(st: *Self, m: []const u8) void {
            var mb = m;

            if (st.leftover > 0) {
                const want = @min(block_length - st.leftover, mb.len);
                const mc = mb[0..want];
                for (mc, 0..) |x, i| {
                    st.buf[st.leftover + i] = x;
                }
                mb = mb[want..];
                st.leftover += want;
                if (st.leftover < block_length) {
                    return;
                }
                st.blocks(&st.buf);
                st.leftover = 0;
            }
            if (mb.len >= block_length) {
                const want = mb.len & ~(block_length - 1);
                st.blocks(mb[0..want]);
                mb = mb[want..];
            }
            if (mb.len > 0) {
                for (mb, 0..) |x, i| {
                    st.buf[st.leftover + i] = x;
                }
                st.leftover += mb.len;
            }
        }

        /// 补零,使下一个输入与块的首字节对齐
        pub fn pad(st: *Self) void {
            if (st.leftover == 0) {
                return;
            }
            var i = st.leftover;
            while (i < block_length) : (i += 1) {
                st.buf[i] = 0;
            }
            st.blocks(&st.buf);
            st.leftover = 0;
        }

        /// 计算整个输入的 GHASH。
        pub fn final(st: *Self, out: *[mac_length]u8) void {
            st.pad();
            mem.writeInt(u128, out[0..16], st.acc, endian);

            std.crypto.secureZero(Self, st[0..1]);
        }

        pub fn finalResult(d: *Self) [mac_length]u8 {
            var result: [mac_length]u8 = undefined;
            d.final(&result);
            return result;
        }

        /// 计算消息的 GHASH。
        pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
            var st = Self.init(key);
            st.update(msg);
            st.final(out);
        }
    };
}

const htest = @import("test.zig");

test "ghash" {
    const key: [16]u8 = @splat(0x42);
    const m: [256]u8 = @splat(0x69);

    var st = Ghash.init(&key);
    st.update(&m);
    var out: [16]u8 = undefined;
    st.final(&out);
    try htest.assertEqual("889295fa746e8b174bf4ec80a65dea41", &out);

    st = Ghash.init(&key);
    st.update(m[0..100]);
    st.update(m[100..]);
    st.final(&out);
    try htest.assertEqual("889295fa746e8b174bf4ec80a65dea41", &out);
}

test "ghash2" {
    var key: [16]u8 = undefined;
    var i: usize = 0;
    while (i < key.len) : (i += 1) {
        key[i] = @as(u8, @intCast(i * 15 + 1));
    }
    const tvs = [_]struct { len: usize, hash: [:0]const u8 }{
        .{ .len = 5263, .hash = "b9395f37c131cd403a327ccf82ec016a" },
        .{ .len = 1361, .hash = "8c24cb3664e9a36e32ddef0c8178ab33" },
        .{ .len = 1344, .hash = "015d7243b52d62eee8be33a66a9658cc" },
        .{ .len = 1000, .hash = "56e148799944193f351f2014ef9dec9d" },
        .{ .len = 512, .hash = "ca4882ce40d37546185c57709d17d1ca" },
        .{ .len = 128, .hash = "d36dc3aac16cfe21a75cd5562d598c1c" },
        .{ .len = 111, .hash = "6e2bea99700fd19cf1694e7b56543320" },
        .{ .len = 80, .hash = "aa28f4092a7cca155f3de279cf21aa17" },
        .{ .len = 16, .hash = "9d7eb5ed121a52a4b0996e4ec9b98911" },
        .{ .len = 1, .hash = "968a203e5c7a98b6d4f3112f4d6b89a7" },
        .{ .len = 0, .hash = "00000000000000000000000000000000" },
    };
    inline for (tvs) |tv| {
        var m: [tv.len]u8 = undefined;
        i = 0;
        while (i < m.len) : (i += 1) {
            m[i] = @as(u8, @truncate(i % 254 + 1));
        }
        var st = Ghash.init(&key);
        st.update(&m);
        var out: [16]u8 = undefined;
        st.final(&out);
        try htest.assertEqual(tv.hash, &out);
    }
}

test "polyval" {
    const key: [16]u8 = @splat(0x42);
    const m: [256]u8 = @splat(0x69);

    var st = Polyval.init(&key);
    st.update(&m);
    var out: [16]u8 = undefined;
    st.final(&out);
    try htest.assertEqual("0713c82b170eef25c8955ddf72c85ccb", &out);

    st = Polyval.init(&key);
    st.update(m[0..100]);
    st.update(m[100..]);
    st.final(&out);
    try htest.assertEqual("0713c82b170eef25c8955ddf72c85ccb", &out);
}