2个版本
0.2.5 |
|
---|---|
0.2.3 |
|
0.1.11 | 2023年4月28日 |
0.1.10 | 2023年1月17日 |
0.1.1 |
|
#2084 在 魔法豆
每月55次下载
在 7 crates 中使用
150KB
4K SLoC
加密
该crate提供了基本加密实现,如《Field》、《Curve》和《Pairing》,《Fft》、《Kzg》,还支持完全的《no_std》和《parity-scale-codec》。
用法
Field
以下《Fr》支持四种基本操作。
use zero_crypto::common::*;
use zero_crypto::dress::field::*;
use zero_crypto::arithmetic::bits_256::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Decode, Encode, Serialize, Deserialize)]
pub struct Fr(pub [u64; 4]);
const MODULUS: [u64; 4] = [
0xffffffff00000001,
0x53bda402fffe5bfe,
0x3339d80809a1d805,
0x73eda753299d7d48,
];
const GENERATOR: [u64; 4] = [
0x0000000efffffff1,
0x17e363d300189c0f,
0xff9c57876f8457b0,
0x351332208fc5a8c4,
];
/// R = 2^256 mod r
const R: [u64; 4] = [
0x00000001fffffffe,
0x5884b7fa00034802,
0x998c4fefecbc4ff5,
0x1824b159acc5056f,
];
/// R^2 = 2^512 mod r
const R2: [u64; 4] = [
0xc999e990f3f29c6d,
0x2b6cedcb87925c23,
0x05d314967254398f,
0x0748d9d99f59ff11,
];
/// R^3 = 2^768 mod r
const R3: [u64; 4] = [
0xc62c1807439b73af,
0x1b3e0d188cf06990,
0x73d13c71c7b5f418,
0x6e2a5bb9c8db33e9,
];
pub const INV: u64 = 0xfffffffeffffffff;
const S: usize = 32;
pub const ROOT_OF_UNITY: Fr = Fr([
0xb9b58d8c5f0e466a,
0x5b1b4c801819d7ec,
0x0af53ae352a31e64,
0x5bf3adda19e9b27b,
]);
impl Fr {
pub const fn to_mont_form(val: [u64; 4]) -> Self {
Self(to_mont_form(val, R2, MODULUS, INV))
}
pub(crate) const fn montgomery_reduce(self) -> [u64; 4] {
mont(
[self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0],
MODULUS,
INV,
)
}
}
fft_field_operation!(Fr, MODULUS, GENERATOR, INV, ROOT_OF_UNITY, R, R2, R3, S);
#[cfg(test)]
mod tests {
use super::*;
use paste::paste;
use rand_core::OsRng;
field_test!(bls12_381_scalar, Fr, 1000);
}
Curve
以下《G1Affine》和《G1Projective》支持点运算。
use crate::fq::Fq;
use crate::fr::Fr;
use zero_crypto::arithmetic::bits_384::*;
use zero_crypto::common::*;
use zero_crypto::dress::curve::*;
/// The projective form of coordinate
#[derive(Debug, Clone, Copy, Decode, Encode)]
pub struct G1Projective {
pub(crate) x: Fq,
pub(crate) y: Fq,
pub(crate) z: Fq,
}
/// The projective form of coordinate
#[derive(Debug, Clone, Copy, Decode, Encode)]
pub struct G1Affine {
pub(crate) x: Fq,
pub(crate) y: Fq,
is_infinity: bool,
}
curve_operation!(
Fr,
Fq,
G1_PARAM_A,
G1_PARAM_B,
G1Affine,
G1Projective,
G1_GENERATOR_X,
G1_GENERATOR_Y
);
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
curve_test!(bls12_381, Fr, G1Affine, G1Projective, 100);
}
依赖关系
~4MB
~84K SLoC