#bulletproofs #ristretto #zero-knowledge #zero-knowledge-proofs #system-api #crypto

no-std sunscreen_bulletproofs

使用Ristretto实现的纯Rust Bulletproofs实现

2个版本

0.8.1 2023年9月11日
0.8.0 2023年8月23日

#1940 in 密码学

每月 21 次下载
用于4 个crate(2个直接使用)

MIT 许可证

1MB
4K SLoC

Bulletproofs

这是最快的Bulletproofs实现,包括单范围和聚合范围证明、强类型多方计算,以及用于证明任意陈述的可编程约束系统API(开发中)。

此库使用Ristretto实现Bulletproofs,使用curve25519-dalek中的ristretto255实现。当使用curve25519-dalek AVX2后端的并行公式时,它可以比基于原libsecp256k1的Bulletproofs实现快约两倍地验证64位rangeproofs。

此库提供了以下实现:

  • 使用聚合rangeproof构造的单个或多个范围的单方证明;

  • 使用会话类型静态强制正确协议流程的在线多方计算,用于多个方之间的rangeproof聚合;

  • 用于表示秩1约束系统和证明及验证任意陈述证明的可编程约束系统API(不稳定,与yoloproofs功能一起开发中);

  • 聚合约束系统证明的在线多方计算(计划中的未来工作)。

这些证明使用Merlin transcripts实现,允许它们与其他证明任意组合而无需实现更改。

开发路线图可以在里程碑部分找到,该部分位于Github仓库

约束系统API仅提供用于实验,必须通过指定yoloproofs功能来启用。它不受semver兼容性的覆盖,且将无通知的情况下进行更改。

目前,在crate的发布版本中已禁用yoloproofs功能,因此只能通过指定对develop分支的git依赖来使用。这意味着无法使用R1CS API发布crate,因为它仅适用于实验。

文档

此功能的用户文档可以在此处找到。此外,该库还包含关于Bulletproofs如何工作的详细笔记。这些笔记可以在库的内部文档中找到

  • Bulletproofs的工作原理
  • 范围证明协议的工作原理
  • 内积证明协议的工作原理
  • 聚合协议的工作原理
  • Bulletproof约束系统证明的工作原理(开发中)
  • 约束系统缩减的工作原理(开发中)
  • 聚合约束系统证明的工作原理(未来工作)

性能比较

以下表格给出了在Intel Skylake-X i7-7800X (@3.5GHz,关闭Turbo Boost)上对64位rangeproof进行证明和验证的比较时间。时间以微秒为单位(越低越好),与最快实现的相对速度。

实现 证明(μs) rel 验证(μs) rel
ours (avx2) ristretto255 7300 1.00x 1040 1.00x
ours (u64) ristretto255 11300 1.54x 1490 1.43x
libsecp+endo secp256k1 14300 1.96x 1900 1.83x
libsecp-endo secp256k1 16800 2.30x 2080 2.00x
Monero ed25519 (不安全) 53300 7.30x 4810 4.63x

使用curve25519-dalek IFMA后端在Cannonlake i3-8121U上提供1.5倍的额外速度提升,将验证速度提升至libsecp的3倍,Monero的7倍,但这些处理器尚未普遍可用。

此crate还包含其他基准测试;有关如何运行所有基准测试的详细信息,请参阅下方的测试和基准部分。

示例

以下示例展示了如何创建和验证32位rangeproof。

# // The #-commented lines are hidden in Rustdoc but not in raw
# // markdown rendering, and contain boilerplate code so that the
# // code in the README.md is actually run as part of the test suite.
#
# extern crate rand;
# use rand::thread_rng;
#
# extern crate curve25519_dalek;
# use curve25519_dalek::scalar::Scalar;
#
# extern crate merlin;
# use merlin::Transcript;
#
# extern crate bulletproofs;
# use bulletproofs::{BulletproofGens, PedersenGens, RangeProof};
#
# fn main() {
// Generators for Pedersen commitments.  These can be selected
// independently of the Bulletproofs generators.
let pc_gens = PedersenGens::default();

// Generators for Bulletproofs, valid for proofs up to bitsize 64
// and aggregation size up to 1.
let bp_gens = BulletproofGens::new(64, 1);

// A secret value we want to prove lies in the range [0, 2^32)
let secret_value = 1037578891u64;

// The API takes a blinding factor for the commitment.
let blinding = Scalar::random(&mut thread_rng());

// The proof can be chained to an existing transcript.
// Here we create a transcript with a doctest domain separator.
let mut prover_transcript = Transcript::new(b"doctest example");

// Create a 32-bit rangeproof.
let (proof, committed_value) = RangeProof::prove_single(
    &bp_gens,
    &pc_gens,
    &mut prover_transcript,
    secret_value,
    &blinding,
    32,
).expect("A real program could handle errors");

// Verification requires a transcript with identical initial state:
let mut verifier_transcript = Transcript::new(b"doctest example");
assert!(
    proof
        .verify_single(&bp_gens, &pc_gens, &mut verifier_transcript, &committed_value, 32)
        .is_ok()
);
# }

构建

要成功编译,您需要安装nightly Rust,而不是stable。

您可以使用rustup安装nightly Rust

rustup default nightly

测试和基准

使用cargo test运行测试。使用cargo bench运行基准测试。此crate使用criterion.rs进行基准测试。

功能

yoloproofs功能启用对排名1约束系统证明的支持。它是不稳定的且不适合部署仅提供用于测试

功能 avx2_backend 启用了 curve25519-dalek 的 AVX2 后端,该后端使用 并行公式 实现曲线算术。为了在 Bulletproofs 中使用它,target_cpu 必须支持 AVX2

RUSTFLAGS="-C target_cpu=skylake" cargo bench --features "avx2_backend"

Skylake-X CPU 的 AVX2 寄存器是两倍。要使用它们,尝试

RUSTFLAGS="-C target_cpu=skylake-avx512" cargo bench --features "avx2_backend"

这防止了 AVX2 并行字段乘法代码中的溢出,但导致其他地方的代码生成更差 ¯\_(ツ)_/¯

关于

这是一个由 Interstellar 赞助的研究项目,由 Henry de Valence、Cathie Yun 和 Oleg Andreev 开发。

依赖项

~2.9–4MB
~71K SLoC