From 03370de6c2ae1e47748bc99b8a2fa5555e7f188d Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 11 Sep 2023 13:12:53 +0200 Subject: [PATCH 1/7] chore: Include bls12-381 vendored dependency --- Cargo.toml | 1 + src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index eae637a8..61025018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ serde_arrays = { version = "0.1.0", optional = true } hex = { version = "0.4", optional = true, default-features = false, features = ["alloc", "serde"] } blake2b_simd = "1" maybe-rayon = { version = "0.1.0", default-features = false } +bls12_381 = { git = "https://github.com/privacy-scaling-explorations/bls12_381", branch = "feat/pub_fq", features = ["groups", "basefield"] } [features] default = ["bits", "multicore"] diff --git a/src/lib.rs b/src/lib.rs index 82613ca9..19d33137 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod msm; pub mod multicore; pub mod serde; +pub mod bls12_381; pub mod bn256; pub mod grumpkin; pub mod pasta; From 0d4a224d955bcf7f1ccd78b94c916085206d65ab Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 11 Sep 2023 13:16:57 +0200 Subject: [PATCH 2/7] add: Compute endomorphism parameters for Bls12-381 --- src/bls12_381/mod.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/bls12_381/mod.rs diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs new file mode 100644 index 00000000..a3beec28 --- /dev/null +++ b/src/bls12_381/mod.rs @@ -0,0 +1,65 @@ +use crate::arithmetic::mul_512; +use crate::arithmetic::sbb; +use crate::{ + arithmetic::{CurveEndo, EndoParameters}, + endo, +}; +pub use bls12_381::{Fp, G1Projective, Scalar}; +use ff::PrimeField; +use ff::WithSmallOrderMulGroup; +use std::convert::TryInto; + +// Obtained from https://github.com/kilic/bls12-381/blob/master/glv.go and originally +// generated using 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 { + // round(b2/n) + gamma1: [0x63f6e522f6cfee30, 0x7c6becf1e01faadd, 0x01], + // round(-b1/n) + gamma2: [0x02, 0x0, 0x0, 0x0], + b1: [0x01, 0x0, 0x0, 0x0], + b2: [0x0000000100000000, 0xac45a4010001a402, 0x0, 0x0], +}; + +endo!(G1Projective, Fp, ENDO_PARAMS_EP); + +#[test] +fn test_endo() { + use ff::Field; + use rand_core::OsRng; + + for _ in 0..100000 { + let k = Fp::random(OsRng); + let (k1, k1_neg, k2, k2_neg) = Eq::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 { + assert_eq!(k, -Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + } else if k2_neg { + assert_eq!(k, Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + } else { + assert_eq!(k, Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + } + } + + for _ in 0..100000 { + let k = Fp::random(OsRng); + let (k1, k1_neg, k2, k2_neg) = Eq::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 { + assert_eq!(k, -Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + } else if k2_neg { + assert_eq!(k, Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + } else { + assert_eq!(k, Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + } + } +} + +#[test] +fn test_quadratic_residue() { + crate::tests::field::random_quadratic_residue_test::(); + crate::tests::field::random_quadratic_residue_test::(); +} From 405f2c6c6ba4d55c3d757acce4715b41abf441d6 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Thu, 14 Sep 2023 22:38:00 +0200 Subject: [PATCH 3/7] refactor endo impl with failing tests --- src/bls12_381/mod.rs | 67 +++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs index a3beec28..e405f861 100644 --- a/src/bls12_381/mod.rs +++ b/src/bls12_381/mod.rs @@ -9,20 +9,19 @@ use ff::PrimeField; use ff::WithSmallOrderMulGroup; use std::convert::TryInto; -// Obtained from https://github.com/kilic/bls12-381/blob/master/glv.go and originally -// generated using https://github.com/ConsenSys/gnark-crypto/blob/master/ecc/utils.go +// 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 { // round(b2/n) - gamma1: [0x63f6e522f6cfee30, 0x7c6becf1e01faadd, 0x01], + gamma1: [0x63f6e522f6cfee30u64, 0x7c6becf1e01faadd, 0x01, 0x0], // round(-b1/n) - gamma2: [0x02, 0x0, 0x0, 0x0], - b1: [0x01, 0x0, 0x0, 0x0], - b2: [0x0000000100000000, 0xac45a4010001a402, 0x0, 0x0], + gamma2: [0x02u64, 0x0, 0x0, 0x0], + b1: [0x01u64, 0x0, 0x0, 0x0], + b2: [0x0000000100000000u64, 0xac45a4010001a402, 0x0, 0x0], }; -endo!(G1Projective, Fp, ENDO_PARAMS_EP); +endo!(G1Projective, Scalar, ENDO_PARAMS_EP); #[test] fn test_endo() { @@ -30,36 +29,54 @@ fn test_endo() { use rand_core::OsRng; for _ in 0..100000 { - let k = Fp::random(OsRng); - let (k1, k1_neg, k2, k2_neg) = Eq::decompose_scalar(&k); + let k = Scalar::random(OsRng); + let (k1, k1_neg, k2, k2_neg) = G1Projective::decompose_scalar(&k); if k1_neg & k2_neg { - assert_eq!(k, -Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + -Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2) + ) } else if k1_neg { - assert_eq!(k, -Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + -Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2) + ) } else if k2_neg { - assert_eq!(k, Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2) + ) } else { - assert_eq!(k, Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2) + ) } } for _ in 0..100000 { - let k = Fp::random(OsRng); - let (k1, k1_neg, k2, k2_neg) = Eq::decompose_scalar(&k); + let k = Scalar::random(OsRng); + let (k1, k1_neg, k2, k2_neg) = G1Projective::decompose_scalar(&k); if k1_neg & k2_neg { - assert_eq!(k, -Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + -Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2) + ) } else if k1_neg { - assert_eq!(k, -Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + -Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2) + ) } else if k2_neg { - assert_eq!(k, Fp::from_u128(k1) + Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + Scalar::from_u128(k1) + Scalar::ZETA * Scalar::from_u128(k2) + ) } else { - assert_eq!(k, Fp::from_u128(k1) - Fp::ZETA * Fp::from_u128(k2)) + assert_eq!( + k, + Scalar::from_u128(k1) - Scalar::ZETA * Scalar::from_u128(k2) + ) } } } - -#[test] -fn test_quadratic_residue() { - crate::tests::field::random_quadratic_residue_test::(); - crate::tests::field::random_quadratic_residue_test::(); -} From 97b0de88f3226775f335ba72f75f7487984d5ee5 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Sat, 16 Sep 2023 09:40:34 +0200 Subject: [PATCH 4/7] fixup --- src/bls12_381/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs index e405f861..3669593d 100644 --- a/src/bls12_381/mod.rs +++ b/src/bls12_381/mod.rs @@ -17,8 +17,8 @@ const ENDO_PARAMS_EP: EndoParameters = EndoParameters { gamma1: [0x63f6e522f6cfee30u64, 0x7c6becf1e01faadd, 0x01, 0x0], // round(-b1/n) gamma2: [0x02u64, 0x0, 0x0, 0x0], - b1: [0x01u64, 0x0, 0x0, 0x0], - b2: [0x0000000100000000u64, 0xac45a4010001a402, 0x0, 0x0], + b2: [0x01u64, 0x0, 0x0, 0x0], + b1: [0x00000000ffffffffu64, 0xac45a4010001a402, 0x0, 0x0], }; endo!(G1Projective, Scalar, ENDO_PARAMS_EP); From 2fa3517207e1ac0cf02c4d08bcdd77877444d4c5 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 25 Sep 2023 15:51:44 +0200 Subject: [PATCH 5/7] change: Update params for Endo after missnaming --- src/bls12_381/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs index 3669593d..eef4cf77 100644 --- a/src/bls12_381/mod.rs +++ b/src/bls12_381/mod.rs @@ -12,16 +12,16 @@ 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!(G1Projective, Scalar, ENDO_PARAMS_BLS); #[test] fn test_endo() { From 9329af1e42fae4c3feb09c17a0a6ebcd7e2e6813 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 18 Dec 2023 10:40:21 +0100 Subject: [PATCH 6/7] update: Bls dep pulls now latest release tag --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 61025018..6d432017 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,8 +35,8 @@ serde = { version = "1.0", default-features = false, optional = true } serde_arrays = { version = "0.1.0", optional = true } hex = { version = "0.4", optional = true, default-features = false, features = ["alloc", "serde"] } blake2b_simd = "1" +bls12_381 = { git = "https://github.com/privacy-scaling-explorations/bls12_381", tag = "v2023_10_26", features = ["groups", "basefield"] } maybe-rayon = { version = "0.1.0", default-features = false } -bls12_381 = { git = "https://github.com/privacy-scaling-explorations/bls12_381", branch = "feat/pub_fq", features = ["groups", "basefield"] } [features] default = ["bits", "multicore"] From b02d296b9dbe9e515dc40203afdf487b67cbac1f Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 18 Dec 2023 10:40:46 +0100 Subject: [PATCH 7/7] chore: Re-export all bls12_381 lib artifacts --- src/bls12_381/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs index eef4cf77..d812da7a 100644 --- a/src/bls12_381/mod.rs +++ b/src/bls12_381/mod.rs @@ -9,6 +9,8 @@ use ff::PrimeField; use ff::WithSmallOrderMulGroup; use std::convert::TryInto; +pub use bls12_381::*; + // 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.