diff --git a/acvm-repo/bn254_blackbox_solver/src/generator/generators.rs b/acvm-repo/bn254_blackbox_solver/src/generator/generators.rs index 1413df69f0c..6f9c72f7ebb 100644 --- a/acvm-repo/bn254_blackbox_solver/src/generator/generators.rs +++ b/acvm-repo/bn254_blackbox_solver/src/generator/generators.rs @@ -9,7 +9,6 @@ use super::hash_to_curve::hash_to_curve; pub(crate) const DEFAULT_DOMAIN_SEPARATOR: &[u8] = "DEFAULT_DOMAIN_SEPARATOR".as_bytes(); - /// Derives generator points via [hash-to-curve][hash_to_curve]. /// /// # ALGORITHM DESCRIPTION @@ -51,15 +50,14 @@ pub(crate) fn derive_generators( #[cfg(test)] mod test { - use ark_ff::{BigInteger, PrimeField}; use ark_ec::AffineRepr; + use ark_ff::{BigInteger, PrimeField}; use super::*; #[test] fn test_derive_generators() { - let res = - derive_generators("test domain".as_bytes(), 128, 0); + let res = derive_generators("test domain".as_bytes(), 128, 0); let is_unique = |y: Affine, j: usize| -> bool { for (i, res) in res.iter().enumerate() { @@ -80,59 +78,75 @@ mod test { fn derive_length_generator() { let domain_separator = "pedersen_hash_length"; let length_generator = derive_generators(domain_separator.as_bytes(), 1, 0)[0]; - + let expected_generator = ( "2df8b940e5890e4e1377e05373fae69a1d754f6935e6a780b666947431f2cdcd", - "2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083" + "2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083", + ); + assert_eq!( + hex::encode(length_generator.x().unwrap().into_bigint().to_bytes_be()), + expected_generator.0, + "Failed on x component" + ); + assert_eq!( + hex::encode(length_generator.y().unwrap().into_bigint().to_bytes_be()), + expected_generator.1, + "Failed on y component" ); - assert_eq!(hex::encode(length_generator.x().unwrap().into_bigint().to_bytes_be()), expected_generator.0, "Failed on x component"); - assert_eq!(hex::encode(length_generator.y().unwrap().into_bigint().to_bytes_be()), expected_generator.1, "Failed on y component"); - } #[test] fn derives_default_generators() { - - const DEFAULT_GENERATORS: &[[&str; 2]] = &[[ - "083e7911d835097629f0067531fc15cafd79a89beecb39903f69572c636f4a5a", - "1a7f5efaad7f315c25a918f30cc8d7333fccab7ad7c90f14de81bcc528f9935d", - ], - [ - "054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402", - "209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126", - ], - [ - "1c44f2a5207c81c28a8321a5815ce8b1311024bbed131819bbdaf5a2ada84748", - "03aaee36e6422a1d0191632ac6599ae9eba5ac2c17a8c920aa3caf8b89c5f8a8", - ], - [ - "26d8b1160c6821a30c65f6cb47124afe01c29f4338f44d4a12c9fccf22fb6fb2", - "05c70c3b9c0d25a4c100e3a27bf3cc375f8af8cdd9498ec4089a823d7464caff", - ], - [ - "20ed9c6a1d27271c4498bfce0578d59db1adbeaa8734f7facc097b9b994fcf6e", - "29cd7d370938b358c62c4a00f73a0d10aba7e5aaa04704a0713f891ebeb92371", - ], - [ - "0224a8abc6c8b8d50373d64cd2a1ab1567bf372b3b1f7b861d7f01257052d383", - "2358629b90eafb299d6650a311e79914b0215eb0a790810b26da5a826726d711", - ], - [ - "0f106f6d46bc904a5290542490b2f238775ff3c445b2f8f704c466655f460a2a", - "29ab84d472f1d33f42fe09c47b8f7710f01920d6155250126731e486877bcf27", - ], - [ - "0298f2e42249f0519c8a8abd91567ebe016e480f219b8c19461d6a595cc33696", - "035bec4b8520a4ece27bd5aafabee3dfe1390d7439c419a8c55aceb207aac83b", - ], + const DEFAULT_GENERATORS: &[[&str; 2]] = &[ + [ + "083e7911d835097629f0067531fc15cafd79a89beecb39903f69572c636f4a5a", + "1a7f5efaad7f315c25a918f30cc8d7333fccab7ad7c90f14de81bcc528f9935d", + ], + [ + "054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402", + "209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126", + ], + [ + "1c44f2a5207c81c28a8321a5815ce8b1311024bbed131819bbdaf5a2ada84748", + "03aaee36e6422a1d0191632ac6599ae9eba5ac2c17a8c920aa3caf8b89c5f8a8", + ], + [ + "26d8b1160c6821a30c65f6cb47124afe01c29f4338f44d4a12c9fccf22fb6fb2", + "05c70c3b9c0d25a4c100e3a27bf3cc375f8af8cdd9498ec4089a823d7464caff", + ], + [ + "20ed9c6a1d27271c4498bfce0578d59db1adbeaa8734f7facc097b9b994fcf6e", + "29cd7d370938b358c62c4a00f73a0d10aba7e5aaa04704a0713f891ebeb92371", + ], + [ + "0224a8abc6c8b8d50373d64cd2a1ab1567bf372b3b1f7b861d7f01257052d383", + "2358629b90eafb299d6650a311e79914b0215eb0a790810b26da5a826726d711", + ], + [ + "0f106f6d46bc904a5290542490b2f238775ff3c445b2f8f704c466655f460a2a", + "29ab84d472f1d33f42fe09c47b8f7710f01920d6155250126731e486877bcf27", + ], + [ + "0298f2e42249f0519c8a8abd91567ebe016e480f219b8c19461d6a595cc33696", + "035bec4b8520a4ece27bd5aafabee3dfe1390d7439c419a8c55aceb207aac83b", + ], ]; - let generated_generators = derive_generators(DEFAULT_DOMAIN_SEPARATOR, DEFAULT_GENERATORS.len() as u32, 0); - for (i, (generator, expected_generator)) in generated_generators.iter().zip(DEFAULT_GENERATORS).enumerate() { - assert_eq!(hex::encode(generator.x().unwrap().into_bigint().to_bytes_be()), expected_generator[0], "Failed on x component of generator {i}"); - assert_eq!(hex::encode(generator.y().unwrap().into_bigint().to_bytes_be()), expected_generator[1], "Failed on y component of generator {i}"); + let generated_generators = + derive_generators(DEFAULT_DOMAIN_SEPARATOR, DEFAULT_GENERATORS.len() as u32, 0); + for (i, (generator, expected_generator)) in + generated_generators.iter().zip(DEFAULT_GENERATORS).enumerate() + { + assert_eq!( + hex::encode(generator.x().unwrap().into_bigint().to_bytes_be()), + expected_generator[0], + "Failed on x component of generator {i}" + ); + assert_eq!( + hex::encode(generator.y().unwrap().into_bigint().to_bytes_be()), + expected_generator[1], + "Failed on y component of generator {i}" + ); } - } - } diff --git a/acvm-repo/bn254_blackbox_solver/src/generator/hash_to_curve.rs b/acvm-repo/bn254_blackbox_solver/src/generator/hash_to_curve.rs index 65457d9d498..cfa25ec0b4d 100644 --- a/acvm-repo/bn254_blackbox_solver/src/generator/hash_to_curve.rs +++ b/acvm-repo/bn254_blackbox_solver/src/generator/hash_to_curve.rs @@ -2,9 +2,9 @@ use acvm_blackbox_solver::blake3; -use ark_ff::{BigInteger, PrimeField}; use ark_ec::{short_weierstrass::Affine, AffineRepr, CurveConfig}; use ark_ff::Field; +use ark_ff::{BigInteger, PrimeField}; use grumpkin::GrumpkinParameters; /// Hash a seed buffer into a point @@ -33,9 +33,9 @@ use grumpkin::GrumpkinParameters; /// c. If parity bit is set AND `y`'s most significant bit is not set, invert `y` /// /// d. If parity bit is not set AND `y`'s most significant bit is set, invert `y` -/// +/// /// e. return (x, y) -/// +/// /// N.B. steps c. and e. are because the `sqrt()` algorithm can return 2 values, /// we need to a way to canonically distinguish between these 2 values and select a "preferred" one pub(crate) fn hash_to_curve(seed: &[u8], attempt_count: u8) -> Affine { @@ -67,15 +67,15 @@ pub(crate) fn hash_to_curve(seed: &[u8], attempt_count: u8) -> Affine Result<(FieldElement, FieldElement), BlackBoxResolutionError> { let inputs: Vec = inputs.iter().map(|input| input.into_repr()).collect(); let result = pedersen::commitment::commit_native_with_index(&inputs, domain_separator); - let res_x = FieldElement::from_repr(*result.x().expect("should not commit to point at infinity")); - let res_y = FieldElement::from_repr(*result.y().expect("should not commit to point at infinity")); + let res_x = + FieldElement::from_repr(*result.x().expect("should not commit to point at infinity")); + let res_y = + FieldElement::from_repr(*result.y().expect("should not commit to point at infinity")); Ok((res_x, res_y)) } diff --git a/acvm-repo/bn254_blackbox_solver/src/pedersen/commitment.rs b/acvm-repo/bn254_blackbox_solver/src/pedersen/commitment.rs index 37c71c1accc..2cf349fe350 100644 --- a/acvm-repo/bn254_blackbox_solver/src/pedersen/commitment.rs +++ b/acvm-repo/bn254_blackbox_solver/src/pedersen/commitment.rs @@ -18,9 +18,10 @@ use crate::generator::generators::{derive_generators, DEFAULT_DOMAIN_SEPARATOR}; // NOTE: this could be generalized using SWCurveConfig but since we perform the operation over grumpkin its explicit pub(crate) fn commit_native_with_index( inputs: &[Fq], - starting_index: u32 + starting_index: u32, ) -> Affine { - let generators = derive_generators(DEFAULT_DOMAIN_SEPARATOR, inputs.len() as u32, starting_index); + let generators = + derive_generators(DEFAULT_DOMAIN_SEPARATOR, inputs.len() as u32, starting_index); inputs.iter().enumerate().fold(Affine::zero(), |mut acc, (i, input)| { //TODO: this is a sketch conversion do better @@ -32,7 +33,7 @@ pub(crate) fn commit_native_with_index( #[cfg(test)] mod test { - + use ark_ec::short_weierstrass::Affine; use ark_ff::MontFp; use ark_std::{One, Zero}; @@ -60,13 +61,13 @@ mod test { let res = commit_native_with_index(&[Fq::zero(), Fq::one()], 0); let expected = Affine::new( // 054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402 + MontFp!("2393473289045184898987089634332637236754766663897650125720167164137088869378"), + // 209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126 MontFp!( - "2393473289045184898987089634332637236754766663897650125720167164137088869378" + "14752839959415467457196082350231122454649853219840744672802853620609001898278" ), - // 209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126 - MontFp!("14752839959415467457196082350231122454649853219840744672802853620609001898278"), ); assert_eq!(res, expected); } -} \ No newline at end of file +} diff --git a/acvm-repo/bn254_blackbox_solver/src/pedersen/hash.rs b/acvm-repo/bn254_blackbox_solver/src/pedersen/hash.rs index 3a68b6f5600..7c049849743 100644 --- a/acvm-repo/bn254_blackbox_solver/src/pedersen/hash.rs +++ b/acvm-repo/bn254_blackbox_solver/src/pedersen/hash.rs @@ -1,9 +1,6 @@ // Taken from: https://github.com/laudiacay/barustenberg/blob/df6bc6f095fe7f288bf6a12e7317fd8eb33d68ae/barustenberg/src/crypto/pedersen/pederson_hash.rs -use ark_ec::{ - short_weierstrass::Affine, - CurveConfig, CurveGroup, -}; +use ark_ec::{short_weierstrass::Affine, CurveConfig, CurveGroup}; use grumpkin::GrumpkinParameters; use crate::generator::generators::derive_generators; @@ -24,7 +21,8 @@ pub(crate) fn hash_with_index( inputs: &[grumpkin::Fq], starting_index: u32, ) -> ::BaseField { - let length_as_scalar: ::ScalarField = (inputs.len() as u64).into(); + let length_as_scalar: ::ScalarField = + (inputs.len() as u64).into(); let length_prefix = length_generator(0) * length_as_scalar; let result = length_prefix + commit_native_with_index(inputs, starting_index); result.into_affine().x @@ -62,7 +60,9 @@ pub(crate) mod test { // 1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6 assert_eq!( res, - MontFp!("12785664284086914537273210116175139764153812914951498056047869066787449592486") + MontFp!( + "12785664284086914537273210116175139764153812914951498056047869066787449592486" + ) ); } -} \ No newline at end of file +} diff --git a/acvm-repo/bn254_blackbox_solver/src/pedersen/mod.rs b/acvm-repo/bn254_blackbox_solver/src/pedersen/mod.rs index 615690fddbf..c3c4ed56450 100644 --- a/acvm-repo/bn254_blackbox_solver/src/pedersen/mod.rs +++ b/acvm-repo/bn254_blackbox_solver/src/pedersen/mod.rs @@ -1,2 +1,2 @@ pub(crate) mod commitment; -pub(crate) mod hash; \ No newline at end of file +pub(crate) mod hash;