Groth16代码解析
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Groth16代码解析相关的知识,希望对你有一定的参考价值。
1. 引言
Groth 2016年论文《On the Size of Pairing-based Non-interactive Arguments》。
相关代码实现有:
- https://github.com/matter-labs/bellman
- https://github.com/zkcrypto/bellman
- https://github.com/arkworks-rs/groth16
本文主要关注代码:
- https://github.com/zkcrypto/bellman
该代码库中的主要依赖有:
- https://github.com/zkcrypto/ff
- https://github.com/zkcrypto/group
- https://github.com/zkcrypto/pairing
- https://github.com/zkcrypto/bls12_381
1.1 ff
https://github.com/zkcrypto/ff 为:
- 使用纯Rust语言编写的 finite field library,其中未使用任何
unsafe{}
代码。
其提供的traits有:
- BitView:Create a
BitSlice
view over some type that supports it. - Field:表示an element of a field.
- PrimeField:表示an element of a prime field。
其提供的函数接口有:
- adc:计算
a+b+carry
,返回the sum以及modify the carry value. - mac_with_carry:计算
a+(b*c)+carry
,返回the least significant digit以及setting carry to the most significant digit. - sbb:计算
a-b-borrow
,返回the result以及modify the borrow value.
其定义的类型有:
- FieldBits:为bit representation of a field element.
若想实现自己的prime field,按如下操作即可:
- 首先启用derive feature
[dependencies]
ff = { version = "0.9", features = ["derive"] }
- 然后按如下顺序设置宏即可:【至此,
Fp
即实现了Field和PrimeField。】
#[macro_use]
extern crate ff;
#[derive(PrimeField)]
#[PrimeFieldModulus = "52435875175126190479447740508185965837690552500527637822603658699938581184513"]
#[PrimeFieldGenerator = "7"]
#[PrimeFieldReprEndianness = "little"]
struct Fp([u64; 4]);
1.2 group
https://github.com/zkcrypto/group 为:
- a crate for working with groups over elliptic curves。
其提供的trait主要有:
- Add、Sub、AddAssign、SubAssign等group operation
GroupOps
- group scalar multiplication
ScalarMul
- cryptographic group中an element的表示
Group
- elliptic curve 上point的高效表示
Curve
(如to_affine坐标系表示,以及batch_normalize即批量将projective表示的元素转换为affine表示的元素。) GroupEncoding
以及UncompressedEncoding
PrimeGroup:Group+GroupEncoding
:表示an element of a prime-order cryptographic group.PrimeCurve
:表示an elliptic curve point guaranteed to be in the correct prime order subgroups.PrimeCurveAffine
:为an elliptic curve point guaranteed to be in the correct prime order subgroups的affIne表示。WnafGroup:Group
:为Extension trait on a [Group
] that provides helpers used by [Wnaf
]。
1.3 pairing
https://github.com/zkcrypto/pairing 为:
- a crate for using pairing-friendly elliptic curves.
其提供了构建pairing-friendly elliptic curve所需的基本traits。特定曲线的curve可参见特定的库。如BLS12-381 curve的实现见:https://github.com/zkcrypto/bls12_381。
其提供的trait主要有:
Engine
:为a collection of types (fields, elliptic curve groups, etc.) with well-defined relationships. In particular, the G1/G2 curve groups are of prime orderr
, and are equipped with a bilinear pairing function.PairingCurveAffine
:为 affine representation of an elliptic curve point that can be used to perform pairings.MultiMillerLoop
:为 an engine that can compute sums of pairings in an efficient way.MillerLoopResult
:为pairing运算中最昂贵的部分。Represents results of a Miller loop, one of the most expensive portions of the pairing function.MillerLoopResult
s cannot be compared with each other until [MillerLoopResult::final_exponentiation
] is called, which is also expensive.
1.4 bls12_381
https://github.com/zkcrypto/bls12_381 中构建了BLS12-381 pairing-friendly elliptic curve。该库:
- 未经过代码审计。
- 不需要Rust standard library。
- 除非明确标注,所有的操作都是constant time的。
其支持的主要特征有:
- bits:默认开启。允许通过API获取scalar的bit iterator。
- groups:默认开启。允许通过API进行group arithmetic with G1, G2和GT。
- pairings:默认开启。允许通过API进行pairing运算。
- alloc:默认开启。允许通过API获取allocator。包含了pairing优化。
- nightly:启用
subtle/nightly
,可阻止编译器优化影响constant time operations。需要nightly Rust compiler。 - endo:默认开启。允许借助curve endomorphism进行优化。已deprecated,在未来release将移除本功能。
以上是关于Groth16代码解析的主要内容,如果未能解决你的问题,请参考以下文章