Skip to content

Commit

Permalink
fix: bump ff, group to 0.13 and pairing to 0.23 (#54)
Browse files Browse the repository at this point in the history
Update the dependencies: `ff`, `group` to 0.13 and `pairing` to 0.23

BREAKING CHANGE: the `ff` traits changed and are not compatible
with older versions.
  • Loading branch information
huitseeker committed Apr 14, 2023
1 parent 977e525 commit 285275a
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 213 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ rustdoc-args = [ "--html-in-header", "katex-header.html" ]
[dependencies]
blst = { version = "=0.3.10", default-features = true }
rand_core = "0.6"
ff = "0.12"
group = { version = "0.12", features = ["tests"] }
pairing_lib = { version = "0.22", package = "pairing" }
ff = "0.13"
group = { version = "0.13", features = ["tests"] }
pairing_lib = { version = "0.23", package = "pairing" }
subtle = "2.2.1"

serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
1 change: 0 additions & 1 deletion benches/blstrs_benches.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(test)]

extern crate test;

mod bls12_381;
37 changes: 20 additions & 17 deletions src/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl From<blst_fp> for Fp {

impl Default for Fp {
fn default() -> Self {
Fp::zero()
Fp::ZERO
}
}

Expand Down Expand Up @@ -469,6 +469,8 @@ impl_add_sub!(Fp);
impl_add_sub_assign!(Fp);
impl_mul!(Fp);
impl_mul_assign!(Fp);
impl_sum!(Fp);
impl_product!(Fp);

// Returns `true` if `le_bytes` is less than the modulus (both are in non-Montgomery form).
#[allow(clippy::comparison_chain)]
Expand Down Expand Up @@ -519,14 +521,10 @@ impl Field for Fp {
}
}

fn zero() -> Self {
ZERO
}
const ZERO: Self = ZERO;

// Returns `1 mod p` in Montgomery form `1 * R mod p`;
fn one() -> Self {
R
}
const ONE: Self = R;

fn is_zero(&self) -> Choice {
self.ct_eq(&ZERO)
Expand All @@ -547,7 +545,7 @@ impl Field for Fp {
fn invert(&self) -> CtOption<Self> {
let mut inv = Self::default();
unsafe { blst_fp_eucl_inverse(&mut inv.0, &self.0) };
let is_invertible = !self.ct_eq(&Fp::zero());
let is_invertible = !self.ct_eq(&Fp::ZERO);
CtOption::new(inv, is_invertible)
}

Expand All @@ -556,6 +554,11 @@ impl Field for Fp {
let is_quad_res = unsafe { blst_fp_sqrt(&mut out.0, &self.0) };
CtOption::new(out, Choice::from(is_quad_res as u8))
}

fn sqrt_ratio(_num: &Self, _div: &Self) -> (Choice, Self) {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}
}

impl Fp {
Expand Down Expand Up @@ -689,7 +692,7 @@ mod tests {
#[test]
fn test_fp_neg_one() {
assert_eq!(
-Fp::one(),
-Fp::ONE,
Fp(blst::blst_fp {
l: [
0x43f5fffffffcaaae,
Expand Down Expand Up @@ -1125,14 +1128,14 @@ mod tests {

#[test]
fn test_fp_inverse() {
assert_eq!(Fp::zero().invert().is_none().unwrap_u8(), 1);
assert_eq!(Fp::ZERO.invert().is_none().unwrap_u8(), 1);

let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
]);

let one = Fp::one();
let one = Fp::ONE;

for _ in 0..1000 {
// Ensure that a * a^-1 = 1
Expand Down Expand Up @@ -1160,7 +1163,7 @@ mod tests {
#[test]
fn test_fp_negate() {
{
let a = Fp::zero();
let a = Fp::ZERO;
assert!(bool::from((-a).is_zero()));
}

Expand Down Expand Up @@ -1190,8 +1193,8 @@ mod tests {
// Exponentiate by various small numbers and ensure it consists with repeated
// multiplication.
let a = Fp::random(&mut rng);
let target = a.pow_vartime(&[i]);
let mut c = Fp::one();
let target = a.pow_vartime([i]);
let mut c = Fp::ONE;
for _ in 0..i {
c.mul_assign(&a);
}
Expand All @@ -1213,8 +1216,8 @@ mod tests {
0xbc, 0xe5,
]);

assert_eq!(Fp::zero().sqrt().unwrap(), Fp::zero());
assert_eq!(Fp::one().sqrt().unwrap(), Fp::one());
assert_eq!(Fp::ZERO.sqrt().unwrap(), Fp::ZERO);
assert_eq!(Fp::ONE.sqrt().unwrap(), Fp::ONE);

for _ in 0..1000 {
// Ensure sqrt(a^2) = a or -a
Expand Down Expand Up @@ -1401,6 +1404,6 @@ mod tests {
]);

assert_eq!(a.invert().unwrap(), b);
assert!(bool::from(Fp::zero().invert().is_none()));
assert!(bool::from(Fp::ZERO.invert().is_none()));
}
}
31 changes: 17 additions & 14 deletions src/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ impl fmt::Display for Fp12 {

impl From<Fp> for Fp12 {
fn from(f: Fp) -> Fp12 {
Fp12::new(Fp6::from(f), Fp6::zero())
Fp12::new(Fp6::from(f), Fp6::ZERO)
}
}

impl From<Fp2> for Fp12 {
fn from(f: Fp2) -> Fp12 {
Fp12::new(Fp6::from(f), Fp6::zero())
Fp12::new(Fp6::from(f), Fp6::ZERO)
}
}

impl From<Fp6> for Fp12 {
fn from(f: Fp6) -> Fp12 {
Fp12::new(f, Fp6::zero())
Fp12::new(f, Fp6::ZERO)
}
}

Expand Down Expand Up @@ -82,7 +82,7 @@ impl ConditionallySelectable for Fp12 {

impl Default for Fp12 {
fn default() -> Self {
Fp12::zero()
Fp12::ZERO
}
}

Expand Down Expand Up @@ -207,19 +207,17 @@ impl_add_sub!(Fp12);
impl_add_sub_assign!(Fp12);
impl_mul!(Fp12);
impl_mul_assign!(Fp12);
impl_sum!(Fp12);
impl_product!(Fp12);

impl Field for Fp12 {
fn random(mut rng: impl RngCore) -> Self {
Fp12::new(Fp6::random(&mut rng), Fp6::random(&mut rng))
}

fn zero() -> Self {
Fp12::new(Fp6::zero(), Fp6::zero())
}
const ZERO: Self = Fp12::new(Fp6::ZERO, Fp6::ZERO);

fn one() -> Self {
Fp12::new(Fp6::one(), Fp6::zero())
}
const ONE: Self = Fp12::new(Fp6::ONE, Fp6::ZERO);

fn is_zero(&self) -> Choice {
self.c0().is_zero() & self.c1().is_zero()
Expand All @@ -238,7 +236,7 @@ impl Field for Fp12 {
}

fn invert(&self) -> CtOption<Self> {
let is_zero = self.ct_eq(&Self::zero());
let is_zero = self.ct_eq(&Self::ZERO);
let mut inv = *self;
unsafe { blst_fp12_inverse(&mut inv.0, &self.0) }
CtOption::new(inv, !is_zero)
Expand All @@ -247,6 +245,11 @@ impl Field for Fp12 {
fn sqrt(&self) -> CtOption<Self> {
unimplemented!()
}

fn sqrt_ratio(_num: &Self, _div: &Self) -> (Choice, Self) {
// ff::helpers::sqrt_ratio_generic(num, div)
unimplemented!()
}
}

impl Fp12 {
Expand Down Expand Up @@ -581,9 +584,9 @@ mod tests {

#[test]
fn test_fp12_eq() {
assert_eq!(Fp12::one(), Fp12::one());
assert_eq!(Fp12::zero(), Fp12::zero());
assert_ne!(Fp12::zero(), Fp12::one());
assert_eq!(Fp12::ONE, Fp12::ONE);
assert_eq!(Fp12::ZERO, Fp12::ZERO);
assert_ne!(Fp12::ZERO, Fp12::ONE);
}

#[test]
Expand Down
Loading

0 comments on commit 285275a

Please sign in to comment.