This crate provides basic cryptographic implementation as in Field
, Curve
and Pairing
, Fft
, Kzg
, and also supports fully no_std
and parity-scale-codec
.
The following Fr
support four basic operation.
```rust use zerocrypto::common::*; use zerocrypto::dress::field::; use zero_crypto::arithmetic::bits_256::; use serde::{Deserialize, Serialize};
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 ROOTOFUNITY: Fr = Fr([ 0xb9b58d8c5f0e466a, 0x5b1b4c801819d7ec, 0x0af53ae352a31e64, 0x5bf3adda19e9b27b, ]);
impl Fr { pub const fn tomontform(val: [u64; 4]) -> Self { Self(tomontform(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,
)
}
}
fftfieldoperation!(Fr, MODULUS, GENERATOR, INV, ROOTOFUNITY, R, R2, R3, S);
mod tests { use super::*; use paste::paste; use rand_core::OsRng;
field_test!(bls12_381_scalar, Fr, 1000);
} ```
The following G1Affine
and G1Projective
supports point arithmetic.
```norun use crate::fq::Fq; use crate::fr::Fr; use zerocrypto::arithmetic::bits384::*; use zerocrypto::common::; use zero_crypto::dress::curve::;
/// The projective form of coordinate
pub struct G1Projective { pub(crate) x: Fq, pub(crate) y: Fq, pub(crate) z: Fq, }
/// The projective form of coordinate
pub struct G1Affine { pub(crate) x: Fq, pub(crate) y: Fq, is_infinity: bool, }
curveoperation!( Fr, Fq, G1PARAMA, G1PARAMB, G1Affine, G1Projective, G1GENERATORX, G1GENERATOR_Y );
mod tests { #[allow(unused_imports)] use super::*;
curve_test!(bls12_381, Fr, G1Affine, G1Projective, 100);
} ```