Skip to content

Commit

Permalink
chore: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed May 18, 2024
1 parent 0d804a5 commit 1888892
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 76 deletions.
110 changes: 62 additions & 48 deletions acvm-repo/bn254_blackbox_solver/src/generator/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<grumpkin::GrumpkinParameters>, j: usize| -> bool {
for (i, res) in res.iter().enumerate() {
Expand All @@ -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}"
);
}

}

}
24 changes: 16 additions & 8 deletions acvm-repo/bn254_blackbox_solver/src/generator/hash_to_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<GrumpkinParameters> {
Expand Down Expand Up @@ -67,15 +67,15 @@ pub(crate) fn hash_to_curve(seed: &[u8], attempt_count: u8) -> Affine<GrumpkinPa
}
} else {
hash_to_curve(seed, attempt_count + 1)
}
}
}

#[cfg(test)]
mod test {

use ark_ff::{BigInteger, PrimeField};
use ark_ec::AffineRepr;

use ark_ff::{BigInteger, PrimeField};

use super::hash_to_curve;

#[test]
Expand Down Expand Up @@ -118,8 +118,16 @@ mod test {
for (seed, attempt_count, expected_point) in test_cases {
let point = hash_to_curve(seed, attempt_count);
assert!(point.is_on_curve());
assert_eq!(hex::encode(point.x().unwrap().into_bigint().to_bytes_be()), expected_point.0, "Failed on x component with seed {seed:?}, attempt_count {attempt_count}");
assert_eq!(hex::encode(point.y().unwrap().into_bigint().to_bytes_be()), expected_point.1, "Failed on y component with seed {seed:?}, attempt_count {attempt_count}");
assert_eq!(
hex::encode(point.x().unwrap().into_bigint().to_bytes_be()),
expected_point.0,
"Failed on x component with seed {seed:?}, attempt_count {attempt_count}"
);
assert_eq!(
hex::encode(point.y().unwrap().into_bigint().to_bytes_be()),
expected_point.1,
"Failed on y component with seed {seed:?}, attempt_count {attempt_count}"
);
}
}
}
3 changes: 1 addition & 2 deletions acvm-repo/bn254_blackbox_solver/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
//!
//! [barustenberg]: https://github.com/laudiacay/barustenberg/blob/df6bc6f095fe7f288bf6a12e7317fd8eb33d68ae/


pub(crate) mod generators;
mod hash_to_curve;
mod hash_to_curve;
8 changes: 5 additions & 3 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use acvm_blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError};

mod embedded_curve_ops;
mod generator;
mod pedersen;
mod poseidon2;
mod wasm;
mod pedersen;

use ark_ec::AffineRepr;
pub use embedded_curve_ops::{embedded_curve_add, multi_scalar_mul};
Expand Down Expand Up @@ -77,8 +77,10 @@ impl BlackBoxFunctionSolver for Bn254BlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
let inputs: Vec<grumpkin::Fq> = 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))
}

Expand Down
15 changes: 8 additions & 7 deletions acvm-repo/bn254_blackbox_solver/src/pedersen/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrumpkinParameters> {
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
Expand All @@ -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};
Expand Down Expand Up @@ -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);
}
}
}
14 changes: 7 additions & 7 deletions acvm-repo/bn254_blackbox_solver/src/pedersen/hash.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,7 +21,8 @@ pub(crate) fn hash_with_index(
inputs: &[grumpkin::Fq],
starting_index: u32,
) -> <GrumpkinParameters as CurveConfig>::BaseField {
let length_as_scalar: <GrumpkinParameters as CurveConfig>::ScalarField = (inputs.len() as u64).into();
let length_as_scalar: <GrumpkinParameters as CurveConfig>::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
Expand Down Expand Up @@ -62,7 +60,9 @@ pub(crate) mod test {
// 1c446df60816b897cda124524e6b03f36df0cec333fad87617aab70d7861daa6
assert_eq!(
res,
MontFp!("12785664284086914537273210116175139764153812914951498056047869066787449592486")
MontFp!(
"12785664284086914537273210116175139764153812914951498056047869066787449592486"
)
);
}
}
}
2 changes: 1 addition & 1 deletion acvm-repo/bn254_blackbox_solver/src/pedersen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub(crate) mod commitment;
pub(crate) mod hash;
pub(crate) mod hash;

0 comments on commit 1888892

Please sign in to comment.