Skip to content

Commit

Permalink
chore: remove usage of slices in pedersen hash (#6295)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## 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.
  • Loading branch information
TomAFrench authored Oct 22, 2024
1 parent 2fae304 commit 8dec847
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
8 changes: 1 addition & 7 deletions noir_stdlib/src/embedded_curve_ops.nr
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,11 @@ pub fn multi_scalar_mul<let N: u32>(
}

#[foreign(multi_scalar_mul)]
fn multi_scalar_mul_array_return<let N: u32>(
pub(crate) fn multi_scalar_mul_array_return<let N: u32>(
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
Expand Down
23 changes: 11 additions & 12 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,22 +56,22 @@ pub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field

#[no_predicates]
pub fn pedersen_hash_with_separator<let N: u32>(input: [Field; N], separator: u32) -> Field {
let mut scalars: Vec<EmbeddedCurveScalar> =
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)]
Expand Down

0 comments on commit 8dec847

Please sign in to comment.