2 个版本

0.4.3 2024年6月17日
0.4.2 2024年6月6日

#459 in 密码学

Download history 134/week @ 2024-06-03 3/week @ 2024-06-10 914/week @ 2024-06-17 1446/week @ 2024-06-24 236/week @ 2024-07-01 1483/week @ 2024-07-08 1325/week @ 2024-07-15 1230/week @ 2024-07-22 1333/week @ 2024-07-29

5,392 每月下载量
用于 5 个 crate(4 个直接使用)

MIT/Apache

340KB
7.5K SLoC

ark-ff

此 crate 定义了有限域特性和遵循这些特性的有用抽象模型。可以在 arkworks-rs/curves 下的 arkworks-rs/curves/<你最喜欢的曲线>/src/fields/ 中找到某些流行椭圆曲线的具体有限域实现。

此 crate 包含两种类型的特性

  • Field 特性:这些定义了操作域元素的接口,例如加法、乘法、逆元、平方根等。
  • Config:指定定义域的参数。对于扩展域,它还提供用于构造域所需的其他功能,例如涉及用于构造域的(立方或二次)非剩余数(NONRESIDUE)的操作。

可用的域特性包括

  • AdditiveGroup - 与关联类型 Scalar 的“标量乘法”操作相关的加法群接口。这适用于素域、域扩展和用于密码学的椭圆曲线群。
  • Field - 泛型有限域的接口。
  • FftField - 提供方法,允许对域元素执行高效的 FFT。
  • PrimeField - 拥有素数 p 个元素的域,也称为 Fp

实现的模式有

上述两种模式作为直接构建扩展域 Fp^m(即 m 等于 2 或 3)或创建扩展塔以获得更高 m 的抽象。后者是通过迭代应用扩展来完成的,例如在二次扩展域上的三次扩展。

  • Fp2 - 直接在素数域上进行的二次扩展,即 BaseField == BasePrimeField
  • Fp3 - 直接在素数域上进行的立方扩展,即 BaseField == BasePrimeField
  • Fp6_2over3 - 扩展塔:在三次扩展域上的二次扩展,即 BaseField = Fp3,但 BasePrimeField = Fp
  • Fp6_3over2 - 扩展塔,与上述类似,但塔的顺序相反:它是在二次扩展域上的立方扩展,即 BaseField = Fp2,但 BasePrimeField = Fp。只有后者默认导出为 Fp6
  • Fp12_2over3over2 - 扩展塔:对 Fp6_3over2 的二次扩展,即 BaseField = Fp6

用法

当处理有限域时有两个重要的特质:FieldPrimeField。让我们通过示例来探讨这些。

加法群

AdditiveGroup 特质为具有相关标量乘法操作的加法群提供了一种通用接口。实现此特质的类型支持常见的群操作,如加法、减法、取反以及通过 Scalar 关联类型进行的标量乘法。

use ark_ff::AdditiveGroup;
// We'll use a field associated with the BLS12-381 pairing-friendly
// group for this example.
use ark_test_curves::bls12_381::Fq2 as F;
// `ark-std` is a utility crate that enables `arkworks` libraries
// to easily support `std` and `no_std` workloads, and also re-exports
// useful crates that should be common across the entire ecosystem, such as `rand`.
use ark_std::{One, UniformRand};

let mut rng = ark_std::test_rng();
// Let's sample uniformly random field elements:
let a = F::rand(&mut rng);
let b = F::rand(&mut rng);
let c = <F as AdditiveGroup>::Scalar::rand(&mut rng);

// We can add...
let c = a + b;
// ... subtract ...
let d = a - b;
// ... double elements ...
assert_eq!(c + d, a.double());
// ... negate them ...
assert_ne!(d, -d);

// ... and multiply them by scalars:
let e = d * c;

Field 特质为任何有限域提供了一种通用接口。实现 Field 的类型支持常见的域操作,如加法、减法、乘法和逆元,并且也必须是 AdditiveGroup

use ark_ff::{AdditiveGroup, Field};
// We'll use a field associated with the BLS12-381 pairing-friendly
// group for this example.
use ark_test_curves::bls12_381::Fq2 as F;
// `ark-std` is a utility crate that enables `arkworks` libraries
// to easily support `std` and `no_std` workloads, and also re-exports
// useful crates that should be common across the entire ecosystem, such as `rand`.
use ark_std::{One, UniformRand};

let mut rng = ark_std::test_rng();
// Let's sample uniformly random field elements:
let a = F::rand(&mut rng);
let b = F::rand(&mut rng);

// We can perform all the operations from the `AdditiveGroup` trait:
// We can add...
let c = a + b;
// ... subtract ...
let d = a - b;
// ... double elements ...
assert_eq!(c + d, a.double());

// ... multiply ...
let e = c * d;
// ... square elements ...
assert_eq!(e, a.square() - b.square());

// ... and compute inverses ...
assert_eq!(a.inverse().unwrap() * a, F::one()); // have to unwrap, as `a` could be zero.

在某些情况下,能够计算域元素的平方根非常有用(例如:用于椭圆曲线元素的点压缩)。为此,用户可以为他们的域类型实现与 sqrt 相关的方法。这种方法已经针对素数域(见下文)和二次扩展域实现了。

可以使用以下方式使用 sqrt 相关的方法

use ark_ff::Field;
// As before, we'll use a field associated with the BLS12-381 pairing-friendly
// group for this example.
use ark_test_curves::bls12_381::Fq2 as F;
use ark_std::{One, UniformRand};

let mut rng = ark_std::test_rng();
let a = F::rand(&mut rng);

// We can check if a field element is a square by computing its Legendre symbol...
if a.legendre().is_qr() {
    // ... and if it is, we can compute its square root.
    let b = a.sqrt().unwrap();
    assert_eq!(b.square(), a);
} else {
    // Otherwise, we can check that the square root is `None`.
    assert_eq!(a.sqrt(), None);
}

素数域

如果域的阶数为素数,则用户可以选择为它实现 PrimeField 特性。这提供了以下附加API的访问权限

use ark_ff::{Field, PrimeField, FpConfig, BigInteger, Zero};
// Now we'll use the prime field underlying the BLS12-381 G1 curve.
use ark_test_curves::bls12_381::Fq as F;
use ark_std::{One, UniformRand};

let mut rng = ark_std::test_rng();
let a = F::rand(&mut rng);
// We can access the prime modulus associated with `F`:
let modulus = <F as PrimeField>::MODULUS;
assert_eq!(a.pow(&modulus), a);

// We can convert field elements to integers in the range [0, MODULUS - 1]:
let one: num_bigint::BigUint = F::one().into();
assert_eq!(one, num_bigint::BigUint::one());

// We can construct field elements from an arbitrary sequence of bytes:
let n = F::from_le_bytes_mod_order(&modulus.to_bytes_le());
assert_eq!(n, F::zero());

依赖项

~2.5–3.5MB
~70K SLoC