前序博客有:
- Groth16 学习笔记
- ZCash bellman版本 Groth16代码解析
以太坊matter-labs bellman代码仓库为:
- https://github.com/matter-labs/bellman
ZCash开发的https://github.com/zkcrypto/bellman 主要针对的是BLS12-381曲线,以太坊matter-labs在该基础上,对以太坊中所使用的BN256曲线进行了优化,并在crates.io
中发布为了bellman_ce
。
目前以太坊matter-labs bellman中提供了2个稳定可用于生产的features,且相应的API会保持不变。这两都是基于Groth16 proof system implementation实现的:
- multicore:默认启用。用于在PC上运行,且环境支持包含threading的full
std
。 - singlecore:主要用于WASM系统,其中non-compatible external crates已移除,以及所有的多线程实现也已移除。
为了实现对WASM的兼容,在实现GM17和SONIC方案时,也需要做相应的考虑。
- gm17(Groth和Maller 2017年发表于CRYPTO论文《Snarky Signatures: Minimal Signatures of Knowledge from Simulation-Extractable SNARKs》):目前未实现,计划在SONIC实现之后启动。
- sonic(Maller等人2019年论文《Sonic: Zero-Knowledge SNARKs from Linear-Size Universal and Updateable Structured Reference Strings》):目前完成90%。原来实现的
helped
协议实现了与Groth16类似的API接口。同时兼容现有的circuits。unhelped
版本目前未完成。但是所有的cryptographical primitives已完成并测试通过。当前优先级最高。
主要依赖有:
- pairing_ce:在Zcash https://github.com/zkcrypto/pairing 中仅支持BLS12-381曲线,matter-labs在此基础上,增加了BN256曲线支持。详细见https://github.com/matter-labs/pairing/tree/master/src/bn256 文件。
- cfg-if:宏定义,用于if-else chain大量 #[cfg] 配置来定义参数。
cfg_if::cfg_if! {
if #[cfg(unix)] {
fn foo() { /* unix specific functionality */ }
} else if #[cfg(target_pointer_width = "32")] {
fn foo() { /* non-unix, 32-bit functionality */ }
} else {
fn foo() { /* fallback implementation */ }
}
}
fn main() {
foo();
}
- byteorder:用于encode或decode number为big-endian或little-endian order。
- futures:为Rust异步编程库。
- num_cpus:用于获取机器CPU的数量。
- crossbeam:并发编程工具
- prefetch:提供了 a type-safe wrapper around LLVM’s prefetch intrinsic。
declare void @llvm.prefetch(i8* , i32 , i32 , i32 )
prefetch主要用来实现内存数据的预抓取处理。一般CPU内部都会提供几级高速缓存,在高速缓存中进行数据存取要比在内存中速度快。因此为了提升性能,可以预先将某个内存地址中的数据读取或写入到高速缓存中去,这样当真实需要对内存地址进行存取时实际上是在高速缓存中进行。而prefetch函数就是用来将某个内存中的数据预先加载或写入到高速缓存中去。
- web-sys:提供了raw
wasm-bindgen
imports for all of the Web’s APIs,包括:window.fetch、Node.prototype.appendChild、WebGL、WebAudio等等。 - tiny-keccak:实现了Keccak派生函数,如sha3等。
- blake2-rfc:为基于RFC7693 实现的BLAKE2 hash函数。
[1] Edcon2019_material [2] EDCON Workshop record (youtube): Intro to bellman: Practical zkSNARKs constructing for Ethereum [3] LLVM编译器中的内置(built-in)函数 [4] LLVM Language Reference Manual