-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow numeric generics to non inlined ACIR functions (#4834)
# Description ## Problem\* Resolves #4826. ## Summary\* When running `nargo info` with the following code: ```rust use dep::std::hash::{pedersen_hash_with_separator, poseidon2::Poseidon2}; global NUM_HASHES = 2; global HASH_LENGTH = 10; #[fold] pub fn poseidon_hash<N>(inputs: [Field; N]) -> Field { Poseidon2::hash(inputs, inputs.len()) } fn main( to_hash: [[Field; HASH_LENGTH]; NUM_HASHES], enable: [bool; NUM_HASHES] ) -> pub [Field; NUM_HASHES] { let mut result = [0; NUM_HASHES]; for i in 0..NUM_HASHES { let enable = enable[i]; let to_hash = to_hash[i]; if enable { result[i] = poseidon_hash(to_hash); } } result } ``` <img width="739" alt="Screenshot 2024-04-17 at 3 46 54 PM" src="https://github.com/noir-lang/noir/assets/43554004/fa31977d-96e9-45f7-8001-43865bc4a83c"> When using `pedersen_hash` we also get 10 opcodes for `main`. Once we get more granularity in our inline types (fold vs. inline for proving) we can most likely fully resolve this issue (#4688). ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
Showing
8 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/fold_numeric_generic_poseidon/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "fold_numeric_generic_poseidon" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.27.0" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
test_programs/execution_success/fold_numeric_generic_poseidon/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
enable = [true, false] | ||
to_hash = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] |
33 changes: 33 additions & 0 deletions
33
test_programs/execution_success/fold_numeric_generic_poseidon/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use dep::std::hash::{pedersen_hash_with_separator, poseidon2::Poseidon2}; | ||
|
||
global NUM_HASHES = 2; | ||
global HASH_LENGTH = 10; | ||
|
||
#[fold] | ||
pub fn poseidon_hash<N>(inputs: [Field; N]) -> Field { | ||
Poseidon2::hash(inputs, inputs.len()) | ||
} | ||
|
||
fn main( | ||
to_hash: [[Field; HASH_LENGTH]; NUM_HASHES], | ||
enable: [bool; NUM_HASHES] | ||
) -> pub [Field; NUM_HASHES + 1] { | ||
let mut result = [0; NUM_HASHES + 1]; | ||
for i in 0..NUM_HASHES { | ||
let enable = enable[i]; | ||
let to_hash = to_hash[i]; | ||
if enable { | ||
result[i] = poseidon_hash(to_hash); | ||
} | ||
} | ||
|
||
// We want to make sure that the foldable function with a numeric generic | ||
// is monomorphized correctly. | ||
let mut double_preimage = [0; 20]; | ||
for i in 0..HASH_LENGTH * 2 { | ||
double_preimage[i] = to_hash[0][i % HASH_LENGTH]; | ||
} | ||
result[NUM_HASHES] = poseidon_hash(double_preimage); | ||
|
||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters