Constantine:用于证明系统和区块链协议的高性能密码学
"一个密码系统应该是安全的,即使除了密钥之外的所有系统信息都是公开的。"
— Auguste Kerckhoffs
这个库提供了加密原语的恒定时间实现,特别关注于区块链和零知识证明系统中使用的密码学。
<!-- TOC --> <!-- /TOC -->该库旨在成为一个快速、紧凑和强化的椭圆曲线密码学库,特别适用于区块链协议和零知识证明系统。
该库重点关注以下特性:
按此顺序排列。
协议是为特定目标或其组合而设计的一组例程:
图例
Constantine在其公共API中支持以下协议。
Nim | C | Rust | Go | |
---|---|---|---|---|
以太坊BLS签名 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
以太坊KZG承诺(用于EIP-4844) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
以太坊IPA承诺(用于Verkle树) | :building_construction: | :see_no_evil: | :see_no_evil: | :see_no_evil: |
以太坊虚拟机BN254预编译ECADD、ECMUL、ECPAIRING | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
EVM BLS12-381预编译(EIP-2537) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
EVM其他:SHA256、modexp | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Halo2证明系统的Zk加速层(实验性) | 不适用 | 不适用 | :white_check_mark: | 不适用 |
Constantine在其公共API中支持以下曲线。
Nim | C | Rust | Go | |
---|---|---|---|---|
BN254-Snarks | :white_check_mark: | :white_check_mark: | :white_check_mark: | :see_no_evil: |
BLS12-381 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :see_no_evil: |
Pasta曲线(Pallas和Vesta) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :see_no_evil: |
对于所有椭圆曲线,支持以下算术运算:
除非明确提到 vartime,否则所有操作都是恒定时间的。
对于支持配对的曲线,Fp2算术也被公开。
:building_construction: 配对和多重配对已实现但尚未公开。
Constantine在其公共API中支持以下哈希函数和CSPRNGs。
Nim | C | Rust | Go | |
---|---|---|---|---|
SHA256 | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
从操作系统获取加密安全的随机数生成器(sysrand) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Constantine 还为 Nim 提供了一个高性能线程池,其性能和 API 继承自:
任务并行 API RFC:https://github.com/nim-lang/RFCs/issues/347
spawn
和 sync
parallelFor
和 syncScope
parallelFor
支持任意复杂的归约。
Constantine 广泛使用它进行并行椭圆曲线求和归约。isSpawned
和 isReady
CPU 拓扑 - 查询操作系统/虚拟机级别可用于运行计算的线程数:
ctt_cpu_get_num_threads_os
getNumThreadsOS
constantine_core::hardware::get_num_threads_os
该线程池支持嵌套并行以充分利用高核心数,并且不受 OpenMP 嵌套并行循环限制的影响。对于批量 KZG 验证,Constantine 并行发出 3 个多标量乘法,每个使用至少 3 个嵌套并行循环。
有关线程池性能详情、设计和研究的更多信息,请参阅以下文档:
[!重要] Constantine 可以由 Nim v1.6.x、v2.0.2 和 v2.0.4 编译,但不能由 Nim v2.0.0 编译(由于编译时评估崩溃)
安装 clang
编译器,例如:
Debian/Ubuntu:sudo apt update && sudo apt install build-essential clang
Archlinux:pacman -S base-devel clang
[!提示] 我们需要 Clang,因为它在加密代码方面的性能明显优于 GCC,特别是对于 Constantine 没有汇编优化的 ARM 架构。Rust 和 Clang 都依赖于 LLVM。<br />通过删除这一行,可以更改为任何 C 编译器。
安装 nim,它在大多数 Linux 发行版的包管理器中可用,MacOS 可通过 Homebrew 安装 Windows 二进制文件可在官方网站获取:https://nim-lang.org/install_unix.html
sudo apt install nim
pacman -S nim
测试以下两项:
git clone https://github.com/mratsim/constantine
cd constantine
cargo test
cargo bench
在 Cargo.toml 中添加 Constantine 作为依赖
[dependencies] constantine-halo2-zal = { git = 'https://github.com/mratsim/constantine' }
[dependencies] constantine-ethereum-kzg = { git = 'https://github.com/mratsim/constantine' }
可选地,可以使用 Nim 和 Rust 之间的跨语言 LTO,参见 https://doc.rust-lang.org/rustc/linker-plugin-lto.html:
在项目中添加一个 .cargo/config.toml
文件,内容如下:
# .cargo/config.toml [build] rustflags="-Clinker-plugin-lto -Clinker=clang -Clink-arg=-fuse-ld=lld"
并修改 Constantine 的 build.rs
以传递 CTT_LTO=1
Command::new("nimble") .env("CC", "clang") .env("CTT_LTO", "1") // <-- .arg("make_lib_rust") .current_dir(root_dir) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .status() .expect("failed to execute process");
安装任意 C 编译器,推荐使用 clang
,例如:
sudo apt update && sudo apt install build-essential clang
pacman -S base-devel clang
安装 nim,它在大多数 Linux 发行版的包管理器中可用,MacOS 可通过 Homebrew 安装 Windows 二进制文件可在官方网站获取:https://nim-lang.org/install_unix.html
sudo apt install nim
pacman -S nim
在 ./include
中编译 Constantine 为静态(和共享)库
cd constantine
CC=clang nimble make_lib
测试 Go API。
cd constantine-go
go test -modfile=../go_test.mod
[!重要] Constantine 使用单独的 modfile 进行测试。<br />除测试外,它没有任何依赖(这对避免供应链攻击至关重要)。
安装 C 编译器,推荐使用 clang
,例如:
sudo apt update && sudo apt install build-essential clang
pacman -S base-devel clang
安装 nim,它在大多数 Linux 发行版的包管理器中可用,MacOS 可通过 Homebrew 安装 Windows 二进制文件可在官方网站获取:https://nim-lang.org/install_unix.html
sudo apt install nim
pacman -S nim
编译动态和静态库。
CC=clang nimble make_lib
CTT_ASM=0 nimble make_lib
nimble make_lib
确保库正常工作
nimble test_lib
库的位置
./lib/
文件夹中阅读examples-c中的示例:
您可以通过以下nimble命令安装库的开发版本
nimble install https://github.com/mratsim/constantine@#master
为了提高速度,建议使用Clang(参见编译器注意事项)。 特别是GCC生成的进位加法代码效率较低。
Constantine至少需要:
-masm=intel
的支持。 在Windows上,Constantine使用MinGW进行测试。Microsoft Visual C++编译器尚未配置。
除了编译器外,Constantine没有C、Nim、Rust、Go依赖,甚至Nim标准库也没有,除了:
这一部分内容太长,已单独成文。
请参见./README-PERFORMANCE.md
CTT_ASM=0
。汇编解决了两个问题:
针对所有现有和即将出现的攻击向量来强化实现是一项极其复杂的任务。 在以下情况之前,该库按原样提供,不提供任何保证:
针对常见攻击向量的防御是基于最大努力提供的。 请注意,Constantine没有外部包依赖,因此不容易受到 供应链攻击(除非它们影响编译器或操作系统)。
攻击者可能会 不遗余力地检索秘密数据,包括:
如果不提到硬件、操作系统和编译器 主动阻碍你的以下方面,这将是不完整的:
越来越多的攻击向量正在被收集,供你欣赏 在 https://github.com/mratsim/constantine/wiki/Constant-time-arithmetics
Constantine的作者尽最大努力实现一个安全的加密库, 特别是针对远程攻击向量,如定时攻击。
请注意,Constantine按原样提供,不提供任何保证。 使用风险自负。
在将数据置于风险之中之前,强烈建议彻底评估你的威胁模型、 你正在考虑的任何加密库的安全性以及你所冒险的机密。 作者希望提醒用户,最好的代码只能减轻 但不能防止人为失误,这是今天被利用的最薄弱环节和最大的 机密后门。
你可以通过"安全"选项卡私下报告安全漏洞。
安全 > 报告漏洞
Nim语言为加密提供以下优势:
{.compile: "myasm.S".}
根据以下任一许可证授权和分发
或者
由你选择。除非符合这些条款,否则不得复制、修改或分发本文件。
本库没有外部依赖。 特别是GMP仅用于测试和差分模糊测试, 并未链接到库中。