#hashing #keyed-hashing

multimixer-128

基于整数乘法的通用密钥哈希

5 个版本

0.1.4 2023年11月25日
0.1.3 2023年11月21日
0.1.2 2023年11月9日
0.1.1 2023年11月8日
0.1.0 2023年11月7日

#786 in 加密学

MIT 许可证

2.5MB
185

multimixer-128

Multimixer-128:基于整数乘法的通用密钥哈希

概述

$Multimixer_{128}$ 是一个通用密钥哈希算法,仅基于整数算术(加法和乘法),在论文 https://ia.cr/2023/1357 中提出。

一个密钥哈希函数,接受一个密钥和可变长度的消息作为输入,将其压缩为固定长度的摘要。$Multimixer_{128}$ 论文中提出的密钥哈希方案,接受长度相等的密钥和消息(这意味着如果消息很长,则需要较长的密钥),使得它们是块大小的倍数(= 32 字节)并将它们压缩为 64 字节摘要。它已被证明是一个 ε-∆通用哈希函数,ε = $2^{−127}$。它可以用于消息认证码计算方案的压缩阶段。它也可以用于 Farfalle 构造的压缩阶段——这些构造用于构建牌函数。

由于 $Multimixer_{128}$ 仅基于整数算术,这在大多数处理器上都非常快,因此它在基准测试中显示出相当有希望的结果。请参阅 下面

您还可以在 下面 找到有关如何开始使用 $Multimixer_{128}$ API 的更多详细信息。

先决条件

Rust 稳定工具链;有关安装指南,请参阅 https://rustup.rs

# When developing this library, I was using
$ rustc --version
rustc 1.73.0 (cc66ad468 2023-10-03)

[!TIP] 我建议您使用 cargo-criterion 来运行基准可执行文件。有关更多信息,请参阅 https://crates.io/crates/cargo-criterion。您只需执行以下命令即可安装它。

cargo install cargo-criterion

测试

为了确保 $Multimixer_{128}$ 的功能正确性和符合规范,即 https://ia.cr/2023/1357(及其参考实现 https://github.com/Parisaa/Multimixer),我使用了由我根据 https://gist.github.com/itzmeanjan/a32eab0244af55eba2847c6472337535 中描述的说明生成的已知答案测试。

cargo test --lib

基准测试

在目标 CPU 上,使用以下命令对公共函数 $f_{128}$ 和 $Multimixer_{128}$ 进行基准测试,输入密钥和消息长度为 32 字节的非零倍数。

[!注意] 在对 x86x86_64aarch64loongarch64 目标进行基准测试时,会报告 CPU 周期和周期/字节指标,而对于其他目标,则使用 criterion.rs 的默认墙钟计时器来报告时间和吞吐量。我发现 https://github.com/pornin/crrl/blob/73b33c1efc73d637f3084d197353991a22c10366/benches/util.rs 在基准测试 Rust 函数时获取 CPU 周期非常有用。但我正在使用 criterion.rs 作为基准测试框架,因此我决定使用 https://crates.io/crates/criterion-cycles-per-byte 插件,它更容易集成。但我必须为我的用例对其进行修补,它们位于我 criterion-cycles-per-byte 分支的 add-memfence 中(参见我的提交 @ https://github.com/itzmeanjan/criterion-cycles-per-byte/commits/add-memfence )。

[!注意] 如果您正在 aarch64 目标上运行基准测试,请考虑阅读 https://github.com/itzmeanjan/criterion-cycles-per-byte/blob/d2f5bf8638640962a9b301966dbb3e65fbc6f283/src/lib.rs#L63-L70

[!警告] 在基准测试时,请确保您已禁用 CPU 频率调整,否则您看到的数字可能会非常误导。我发现 https://github.com/google/benchmark/blob/b40db869/docs/reducing_variance.md 非常有帮助。

# In case you didn't install `cargo-criterion`, you have to run benchmark with
# RUSTFLAGS="-C opt-level=3 -C target-cpu=native" cargo bench --features="internal"

RUSTFLAGS="-C opt-level=3 -C target-cpu=native" cargo criterion --features="internal"

12代英特尔(R)酷睿(TM) i7-1260P

f_128/f-128 (cached)    time:   [6.9927 cycles 7.0443 cycles 7.1069 cycles]                      
                        thrpt:  [0.2221 cpb 0.2201 cpb 0.2185 cpb]
f_128/f-128 (random)    time:   [21.1852 cycles 21.3297 cycles 21.4551 cycles]                   
                        thrpt:  [0.6705 cpb 0.6666 cpb 0.6620 cpb]

multimixer_128/32B key/ msg (cached)                                                                             
                        time:   [14.0913 cycles 14.1296 cycles 14.1664 cycles]
                        thrpt:  [0.2214 cpb 0.2208 cpb 0.2202 cpb]
multimixer_128/32B key/ msg (random)                                                                            
                        time:   [63.4513 cycles 63.8913 cycles 64.3942 cycles]
                        thrpt:  [1.0062 cpb 0.9983 cpb 0.9914 cpb]

multimixer_128/128B key/ msg (cached)                                                                             
                        time:   [42.2552 cycles 42.2975 cycles 42.3421 cycles]
                        thrpt:  [0.1654 cpb 0.1652 cpb 0.1651 cpb]
multimixer_128/128B key/ msg (random)                                                                            
                        time:   [107.6240 cycles 109.1370 cycles 111.0124 cycles]
                        thrpt:  [0.4336 cpb 0.4263 cpb 0.4204 cpb]

multimixer_128/512B key/ msg (cached)                                                                            
                        time:   [166.6061 cycles 166.8388 cycles 167.0822 cycles]
                        thrpt:  [0.1632 cpb 0.1629 cpb 0.1627 cpb]
multimixer_128/512B key/ msg (random)                                                                            
                        time:   [239.1786 cycles 240.4487 cycles 241.9385 cycles]
                        thrpt:  [0.2363 cpb 0.2348 cpb 0.2336 cpb]

multimixer_128/2048B key/ msg (cached)                                                                            
                        time:   [606.0156 cycles 606.3316 cycles 606.6701 cycles]
                        thrpt:  [0.1481 cpb 0.1480 cpb 0.1480 cpb]
multimixer_128/2048B key/ msg (random)                                                                             
                        time:   [835.5582 cycles 848.0583 cycles 859.5641 cycles]
                        thrpt:  [0.2099 cpb 0.2070 cpb 0.2040 cpb]

multimixer_128/8192B key/ msg (cached)                                                                             
                        time:   [2346.9930 cycles 2347.5554 cycles 2348.2118 cycles]
                        thrpt:  [0.1433 cpb 0.1433 cpb 0.1432 cpb]
multimixer_128/8192B key/ msg (random)                                                                             
                        time:   [2816.3210 cycles 2838.9367 cycles 2859.1557 cycles]
                        thrpt:  [0.1745 cpb 0.1733 cpb 0.1719 cpb]

ARM Cortex-A72 即树莓派 4B

f_128/f-128 (cached)    time:   [33.5869 cycles 33.6835 cycles 33.7842 cycles]                   
                        thrpt:  [1.0558 cpb 1.0526 cpb 1.0496 cpb]
f_128/f-128 (random)    time:   [118.9137 cycles 119.6657 cycles 120.5443 cycles]               
                        thrpt:  [3.7670 cpb 3.7396 cpb 3.7161 cpb]

multimixer_128/32B key/ msg (cached)                                                                            
                        time:   [189.0723 cycles 189.1691 cycles 189.2789 cycles]
                        thrpt:  [2.9575 cpb 2.9558 cpb 2.9543 cpb]
multimixer_128/32B key/ msg (random)                                                                             
                        time:   [433.4768 cycles 436.1863 cycles 438.5723 cycles]
                        thrpt:  [6.8527 cpb 6.8154 cpb 6.7731 cpb]

multimixer_128/128B key/ msg (cached)                                                                            
                        time:   [699.8622 cycles 699.9601 cycles 700.0751 cycles]
                        thrpt:  [2.7347 cpb 2.7342 cpb 2.7338 cpb]
multimixer_128/128B key/ msg (random)                                                                             
                        time:   [978.8853 cycles 979.8636 cycles 980.9012 cycles]
                        thrpt:  [3.8316 cpb 3.8276 cpb 3.8238 cpb]

multimixer_128/512B key/ msg (cached)                                                                             
                        time:   [2742.5257 cycles 2743.5495 cycles 2744.8465 cycles]
                        thrpt:  [2.6805 cpb 2.6792 cpb 2.6782 cpb]
multimixer_128/512B key/ msg (random)                                                                             
                        time:   [3223.0299 cycles 3228.1490 cycles 3233.4994 cycles]
                        thrpt:  [3.1577 cpb 3.1525 cpb 3.1475 cpb]

multimixer_128/2048B key/ msg (cached)                                                                             
                        time:   [10932.5461 cycles 10935.6646 cycles 10939.9686 cycles]
                        thrpt:  [2.6709 cpb 2.6698 cpb 2.6691 cpb]
multimixer_128/2048B key/ msg (random)                                                                             
                        time:   [12448.8476 cycles 12496.1792 cycles 12541.6384 cycles]
                        thrpt:  [3.0619 cpb 3.0508 cpb 3.0393 cpb]

multimixer_128/8192B key/ msg (cached)                                                                             
                        time:   [44770.5750 cycles 44779.3846 cycles 44790.3585 cycles]
                        thrpt:  [2.7338 cpb 2.7331 cpb 2.7326 cpb]
multimixer_128/8192B key/ msg (random)                                                                             
                        time:   [45914.0882 cycles 45978.0062 cycles 46044.7573 cycles]
                        thrpt:  [2.8103 cpb 2.8063 cpb 2.8024 cpb]

使用方法

使用 $Multimixer_{128}$ 哈希器 API 非常简单。

  1. multimixer-128 添加到您的项目 Cargo.toml 文件的 [dependencies] 部分。
[dependencies]
multimixer-128 = { git = "https://github.com/itzmeanjan/multimixer-128" }
# or
multimixer-128 = "0.1.4"

# In case you're also interested in using f_128, the public function of multimixer-128,
# enable `internal` feature-gate of this crate.
#
# multimixer-128 = { version = "0.1.4", features = "internal" }
  1. 获取非零、等长密钥和消息,其字节长度是块大小的倍数(= 32 字节),作为输入。
use multimixer_128;

fn main() {
    const BLOCKS: usize = 1;
    const MLEN: usize = multimixer_128::BLOCK_SIZE * BLOCKS;

    // Beware, this is just an example key !
    let key = [0x0fu8; MLEN];
    let msg = [0xf0u8; MLEN];
    let mut dig = [0u8; multimixer_128::DIGEST_SIZE];

    // ...
}
  1. 给定等长(非零)密钥和消息,计算 64 字节消息摘要。
fn main() {
    // ...

    multimixer_128::multimixer_128(&key, &msg, &mut dig);
    assert_eq!(dig, [1, 0, 0, 0, 254, 255, 255, 255, 1, 0, 0, 0, 254, 255, 255, 255, 1, 0, 0, 0, 254, 255, 255, 255, 1, 0, 0, 0, 254, 255, 255, 255, 9, 0, 0, 0, 250, 255, 255, 255, 9, 0, 0, 0, 250, 255, 255, 255, 9, 0, 0, 0, 250, 255, 255, 255, 9, 0, 0, 0, 250, 255, 255, 255]);
}

我维护了一个示例程序,展示了 $Multimixer_{128}$ API 的使用方法,位于 examples 目录中。

cargo run --example multimixer_128

Key     = e02f28d290bd51df3b103396debe7f2ffc90b837588e37b13a7cc8cccce9fa11
Message = 1511ad54daae3df6706e33d9953f5dff4eabb2da85ad1added1aba2e0b571397
Digest  = d2324595d842cb028205c1f00328ba760d292a690452726d95728070ee7ff21e8e6af14b1e4c6d15404e709fd1fec952da3e3b7b3fe7d038fca793f3b4d7661a

[!注意] 此库的大部分内部函数都实现为 const fn 即编译时可以评估的函数。这就是为什么我还维护另一个示例 f_128.rs,它使用静态断言展示了此库的特性。通过执行以下命令尝试运行示例程序: $ cargo run --example f_128 --features="internal",您将看到它。

依赖项