diff --git a/Cargo.toml b/Cargo.toml index 22dbc56..485800f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,11 +22,7 @@ rand = "0.4" byteorder = "1" #ff_ce = {version = "0.6", features = ["derive"] } #ff_ce = { git = 'https://github.com/matter-labs/ff', features = ["derive"], branch = "gpu"} -ff_ce = { path = '../ff', features = ["derive", "derive_serde"]} -serde = "1.0.80" -serde_derive = "1.0.80" -serde_json = "1.0.33" -hex = "0.3.2" +ff = { path = '../ff', package = "ff_ce", features = ["derive"]} [features] unstable-features = ["expose-arith"] diff --git a/src/bls12_381/ec.rs b/src/bls12_381/ec.rs index 3d505de..54c02c2 100644 --- a/src/bls12_381/ec.rs +++ b/src/bls12_381/ec.rs @@ -753,10 +753,12 @@ pub mod g1 { impl RawEncodable for G1Affine { fn into_raw_uncompressed_le(&self) -> Self::Uncompressed { let mut res = Self::Uncompressed::empty(); - let mut writer = &mut res.0[..]; + { + let mut writer = &mut res.0[..]; - self.x.into_raw_repr().write_le(&mut writer).unwrap(); - self.y.into_raw_repr().write_le(&mut writer).unwrap(); + self.x.into_raw_repr().write_le(&mut writer).unwrap(); + self.y.into_raw_repr().write_le(&mut writer).unwrap(); + } res } diff --git a/src/bn256/ec.rs b/src/bn256/ec.rs index 02fcbc0..cb3ab13 100644 --- a/src/bn256/ec.rs +++ b/src/bn256/ec.rs @@ -645,10 +645,12 @@ pub mod g1 { impl RawEncodable for G1Affine { fn into_raw_uncompressed_le(&self) -> Self::Uncompressed { let mut res = Self::Uncompressed::empty(); - let mut writer = &mut res.0[..]; + { + let mut writer = &mut res.0[..]; - self.x.into_raw_repr().write_le(&mut writer).unwrap(); - self.y.into_raw_repr().write_le(&mut writer).unwrap(); + self.x.into_raw_repr().write_le(&mut writer).unwrap(); + self.y.into_raw_repr().write_le(&mut writer).unwrap(); + } res } @@ -1078,25 +1080,6 @@ pub mod g2 { G1Affine ); - // impl Rand for G2 { - // fn rand(rng: &mut R) -> Self { - - // let mut r = G2::one(); - // let k = Fr::rand(rng); - // r.mul_assign(k); - // return r; - // } - // } - - // impl Rand for G2Affine { - // fn rand(rng: &mut R) -> Self { - // let mut r = G2::one(); - // let k = Fr::rand(rng); - // r.mul_assign(k); - // return r.into_affine(); - // } - // } - impl Rand for G2 { fn rand(rng: &mut R) -> Self { loop { @@ -1471,6 +1454,50 @@ pub mod g2 { } } + #[test] + fn test_generate_g2_in_subgroup() { + use SqrtField; + + let mut x = Fq2::zero(); + loop { + // y^2 = x^3 + b + let mut rhs = x; + rhs.square(); + rhs.mul_assign(&x); + rhs.add_assign(&G2Affine::get_coeff_b()); + + if let Some(y) = rhs.sqrt() { + let mut negy = y; + negy.negate(); + + let p = G2Affine { + x: x, + y: if y < negy { y } else { negy }, + infinity: false, + }; + + let g2 = p.into_projective(); + let mut minus_one = Fr::one(); + minus_one.negate(); + + let mut expected_zero = p.mul(minus_one); + expected_zero.add_assign(&g2); + + if !expected_zero.is_zero() { + let p = expected_zero.into_affine(); + let scaled_by_cofactor = p.scale_by_cofactor(); + if scaled_by_cofactor.is_zero() { + let g2 = G2Affine::from(expected_zero); + println!("Invalid subgroup point = {}", g2); + return; + } + } + } + + x.add_assign(&Fq2::one()); + } + } + #[cfg(test)] use rand::{SeedableRng, XorShiftRng}; diff --git a/src/lib.rs b/src/lib.rs index a22b5b7..8c1a255 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,21 +14,13 @@ extern crate byteorder; extern crate rand; -extern crate hex; -extern crate serde; -extern crate serde_derive; - #[cfg(test)] pub mod tests; -pub extern crate ff_ce as ff; +pub extern crate ff; pub use ff::*; -// pub mod ff { -// pub use ff::*; -// } - pub mod bls12_381; pub mod bn256;