Skip to content

Commit

Permalink
fix: workaround from_slice with nested slices (#5648)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #5633 

## Summary\*
This a workaround and it does not solve the 2 underlying problems:
1. the no_predicate defer the inlining, so the compiler misses some
constant values. We should handle no_predicates in a better way. I
removed the failing assert_constant to workaround this.
2. the from_slice somehow flatten the slice of curve points and loose
the composite type of a point (field, field and bool). I worked around
this by not using from_slice.

## Additional Context
I have activated no-predicate for Pedersen in Noir. If the protocol
circuits work well with this version, we will finally be able to remove
the Pedersen black box!


## 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.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
  • Loading branch information
guipublic and vezenovm authored Aug 2, 2024
1 parent c1ed9fb commit 6310a55
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn pedersen_commitment_with_separator<let N: u32>(input: [Field; N], separator:
}
}

#[no_predicates]
fn pedersen_commitment_with_separator_noir<let N: u32>(input: [Field; N], separator: u32) -> EmbeddedCurvePoint {
let mut points = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N];
for i in 0..N {
Expand All @@ -56,15 +57,19 @@ fn pedersen_commitment_with_separator_noir<let N: u32>(input: [Field; N], separa
multi_scalar_mul(generators, points)
}

#[no_predicates]
fn pedersen_hash_with_separator_noir<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 });
scalars.push(EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field });
let domain_generators :[EmbeddedCurvePoint; N]= derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);
let mut vec_generators = Vec::from_slice(domain_generators.as_slice());
let mut vec_generators = Vec::new();
for i in 0..N {
vec_generators.push(domain_generators[i]);
}
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]
Expand All @@ -86,7 +91,7 @@ fn __pedersen_commitment_with_separator<let N: u32>(input: [Field; N], separator
#[field(bn254)]
fn derive_generators<let N: u32, let M: u32>(domain_separator_bytes: [u8; M], starting_index: u32) -> [EmbeddedCurvePoint; N] {
crate::assert_constant(domain_separator_bytes);
crate::assert_constant(starting_index);
// TODO(https://github.com/noir-lang/noir/issues/5672): Add back assert_constant on starting_index
__derive_generators(domain_separator_bytes, starting_index)
}

Expand Down

0 comments on commit 6310a55

Please sign in to comment.