Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bls endo params. Do not merge. #92

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ paste = "1.0.11"
serde = { version = "1.0", default-features = false, optional = true }
serde_arrays = { version = "0.1.0", optional = true }
blake2b_simd = "1"
bls12_381 = { git = "https://github.com/privacy-scaling-explorations/bls12_381", branch = "feat/pub_fq", features = ["groups", "basefield"] }
bls12_381 = { path = "../bls12_381", features = ["groups", "basefield"] }

[features]
default = ["reexport", "bits"]
Expand Down
6 changes: 2 additions & 4 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
//! This module is temporary, and the extension traits defined here are expected to be
//! upstreamed into the `ff` and `group` crates after some refactoring.

use crate::CurveExt;

pub(crate) struct EndoParameters {
pub(crate) gamma1: [u64; 4],
pub(crate) gamma2: [u64; 4],
pub(crate) b1: [u64; 4],
pub(crate) b2: [u64; 4],
}

pub trait CurveEndo: CurveExt {
fn decompose_scalar(e: &Self::ScalarExt) -> (u128, bool, u128, bool);
pub trait CurveEndo: ff::WithSmallOrderMulGroup<3> {
fn decompose_scalar(e: &Self) -> (u128, bool, u128, bool);
}

/// Compute a + b + carry, returning the result and the new carry over.
Expand Down
45 changes: 11 additions & 34 deletions src/bls12_381/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,28 @@ use std::convert::TryInto;
// Obtained from https://github.com/ConsenSys/gnark-crypto/blob/master/ecc/utils.go
// See https://github.com/demining/Endomorphism-Secp256k1/blob/main/README.md
// to have more details about the endomorphism.
const ENDO_PARAMS_EP: EndoParameters = EndoParameters {
const ENDO_PARAMS_BLS: EndoParameters = EndoParameters {
// round(b2/n)
gamma1: [0x63f6e522f6cfee30u64, 0x7c6becf1e01faadd, 0x01, 0x0],
gamma2: [0x63f6e522f6cfee30u64, 0x7c6becf1e01faadd, 0x01, 0x0],
// round(-b1/n)
gamma2: [0x02u64, 0x0, 0x0, 0x0],
b2: [0x01u64, 0x0, 0x0, 0x0],
b1: [0x00000000ffffffffu64, 0xac45a4010001a402, 0x0, 0x0],
gamma1: [0x02u64, 0x0, 0x0, 0x0],
b1: [0x01u64, 0x0, 0x0, 0x0],
b2: [0x0000000100000000, 0xac45a4010001a402, 0x0, 0x0],
};

endo!(G1Projective, Scalar, ENDO_PARAMS_EP);
endo!(Scalar, ENDO_PARAMS_BLS);

#[test]
fn test_endo() {
use ff::Field;
use rand_core::OsRng;

for _ in 0..100000 {
assert_ne!(Scalar::ZETA * Scalar::ZETA, Scalar::ONE);
assert_eq!(Scalar::ZETA * Scalar::ZETA * Scalar::ZETA, Scalar::ONE);
for _ in 0..10000 {
let k = Scalar::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1Projective::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(
k,
-Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2)
)
} else if k1_neg {
assert_eq!(
k,
-Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2)
)
} else if k2_neg {
assert_eq!(
k,
Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2)
)
} else {
assert_eq!(
k,
Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2)
)
}
}

for _ in 0..100000 {
let k = Scalar::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1Projective::decompose_scalar(&k);
println!("k {:#?}", k);
let (k1, k1_neg, k2, k2_neg) = Scalar::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(
k,
Expand Down
4 changes: 2 additions & 2 deletions src/bn256/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const ENDO_PARAMS_BN: EndoParameters = EndoParameters {
b2: [0x0be4e1541221250b, 0x6f4d8248eeb859fd, 0, 0],
};

endo!(G1, Fr, ENDO_PARAMS_BN);
endo!(Fr, ENDO_PARAMS_BN);

impl group::cofactor::CofactorGroup for G1 {
type Subgroup = G1;
Expand Down Expand Up @@ -288,7 +288,7 @@ mod tests {
assert_eq!(g * Fr::ZETA, g.endo());
for _ in 0..100000 {
let k = Fr::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1::decompose_scalar(&k);
let (k1, k1_neg, k2, k2_neg) = Fr::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(k, -Fr::from_u128(k1) + Fr::ZETA * Fr::from_u128(k2))
} else if k1_neg {
Expand Down
4 changes: 2 additions & 2 deletions src/derive/curve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_export]
macro_rules! endo {
($name:ident, $field:ident, $params:expr) => {
impl CurveEndo for $name {
($field:ident, $params:expr) => {
impl CurveEndo for $field {
fn decompose_scalar(k: &$field) -> (u128, bool, u128, bool) {
let to_limbs = |e: &$field| {
let repr = e.to_repr();
Expand Down
4 changes: 2 additions & 2 deletions src/grumpkin/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ENDO_PARAMS_GRUMPKIN: EndoParameters = EndoParameters {
b2: [0x0be4e1541221250b, 0x6f4d8248eeb859fd, 0, 0],
};

endo!(G1, Fr, ENDO_PARAMS_GRUMPKIN);
endo!(Fr, ENDO_PARAMS_GRUMPKIN);

impl group::cofactor::CofactorGroup for G1 {
type Subgroup = G1;
Expand Down Expand Up @@ -125,7 +125,7 @@ mod tests {

for _ in 0..100000 {
let k = Fr::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1::decompose_scalar(&k);
let (k1, k1_neg, k2, k2_neg) = Fr::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(k, -Fr::from_u128(k1) + Fr::ZETA * Fr::from_u128(k2))
} else if k1_neg {
Expand Down
18 changes: 9 additions & 9 deletions src/pasta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const ENDO_PARAMS_EP: EndoParameters = EndoParameters {
b2: [0x0c7c095a00000001, 0x93cd3a2c8198e269, 0x0, 0x0],
};

endo!(Eq, Fp, ENDO_PARAMS_EQ);
endo!(Ep, Fq, ENDO_PARAMS_EP);
endo!(Fp, ENDO_PARAMS_EQ);
endo!(Fq, ENDO_PARAMS_EP);

prime_field_legendre!(Fp);
prime_field_legendre!(Fq);
Expand All @@ -48,7 +48,7 @@ fn test_endo() {

for _ in 0..100000 {
let k = Fp::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = Eq::decompose_scalar(&k);
let (k1, k1_neg, k2, k2_neg) = Fp::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(k, -Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2))
} else if k1_neg {
Expand All @@ -61,16 +61,16 @@ fn test_endo() {
}

for _ in 0..100000 {
let k = Fp::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = Eq::decompose_scalar(&k);
let k = Fq::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = Fq::decompose_scalar(&k);
if k1_neg & k2_neg {
assert_eq!(k, -Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2))
assert_eq!(k, -Fq::from_u128(k1) + Fq::ZETA * Fq::from_u128(k2))
} else if k1_neg {
assert_eq!(k, -Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2))
assert_eq!(k, -Fq::from_u128(k1) - Fq::ZETA * Fq::from_u128(k2))
} else if k2_neg {
assert_eq!(k, Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2))
assert_eq!(k, Fq::from_u128(k1) + Fq::ZETA * Fq::from_u128(k2))
} else {
assert_eq!(k, Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2))
assert_eq!(k, Fq::from_u128(k1) - Fq::ZETA * Fq::from_u128(k2))
}
}
}
Expand Down
Loading