From 8dec84793d200dcb524aa5c397d0a84d38974e7e Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:37:18 +0100 Subject: [PATCH] chore: remove usage of slices in pedersen hash (#6295) # Description ## Problem\* Resolves ## Summary\* This PR reworks the pedersen hash implementation to work on arrays as this removes some brillig overhead. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- noir_stdlib/src/embedded_curve_ops.nr | 8 +------- noir_stdlib/src/hash/mod.nr | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index a77dd8d47d6..dd5e4285c00 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -112,17 +112,11 @@ pub fn multi_scalar_mul( } #[foreign(multi_scalar_mul)] -fn multi_scalar_mul_array_return( +pub(crate) fn multi_scalar_mul_array_return( points: [EmbeddedCurvePoint; N], scalars: [EmbeddedCurveScalar; N], ) -> [Field; 3] {} -#[foreign(multi_scalar_mul)] -pub(crate) fn multi_scalar_mul_slice( - points: [EmbeddedCurvePoint], - scalars: [EmbeddedCurveScalar], -) -> [Field; 3] {} - // docs:start:fixed_base_scalar_mul pub fn fixed_base_scalar_mul(scalar: EmbeddedCurveScalar) -> EmbeddedCurvePoint // docs:end:fixed_base_scalar_mul diff --git a/noir_stdlib/src/hash/mod.nr b/noir_stdlib/src/hash/mod.nr index 97e91f71601..609017d70aa 100644 --- a/noir_stdlib/src/hash/mod.nr +++ b/noir_stdlib/src/hash/mod.nr @@ -7,9 +7,8 @@ pub mod sha512; use crate::default::Default; use crate::uint128::U128; -use crate::collections::vec::Vec; use crate::embedded_curve_ops::{ - EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_slice, + EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return, }; use crate::meta::derive_via; @@ -57,22 +56,22 @@ pub fn pedersen_hash(input: [Field; N]) -> Field #[no_predicates] pub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field { - let mut scalars: Vec = - Vec::from_slice([EmbeddedCurveScalar { lo: 0, hi: 0 }; N].as_slice()); //Vec::new(); - for i in 0..N { - scalars.set(i, from_field_unsafe(input[i])); - } - scalars.push(EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field }); + let mut scalars: [EmbeddedCurveScalar; N + 1] = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N + 1]; + let mut generators: [EmbeddedCurvePoint; N + 1] = + [EmbeddedCurvePoint::point_at_infinity(); N + 1]; let domain_generators: [EmbeddedCurvePoint; N] = derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator); - let mut vec_generators = Vec::new(); + for i in 0..N { - vec_generators.push(domain_generators[i]); + scalars[i] = from_field_unsafe(input[i]); + generators[i] = domain_generators[i]; } + scalars[N] = EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field }; + let length_generator: [EmbeddedCurvePoint; 1] = derive_generators("pedersen_hash_length".as_bytes(), 0); - vec_generators.push(length_generator[0]); - multi_scalar_mul_slice(vec_generators.slice, scalars.slice)[0] + generators[N] = length_generator[0]; + multi_scalar_mul_array_return(generators, scalars)[0] } #[field(bn254)]