-
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: Implement
poseidon2_permutation
in comptime interpreter (#5590)
# Description ## Problem\* Resolves another of the many builtin/foreign functions that still need to be implemented in the interpreter ## Summary\* ## 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
Showing
6 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
48 changes: 48 additions & 0 deletions
48
compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs
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,48 @@ | ||
use acvm::BlackBoxFunctionSolver; | ||
use bn254_blackbox_solver::Bn254BlackBoxSolver; | ||
use iter_extended::try_vecmap; | ||
use noirc_errors::Location; | ||
|
||
use crate::{ | ||
hir::comptime::{errors::IResult, interpreter::builtin::get_field, InterpreterError, Value}, | ||
macros_api::NodeInterner, | ||
}; | ||
|
||
use super::builtin::{check_argument_count, get_array, get_u32}; | ||
|
||
pub(super) fn call_foreign( | ||
interner: &mut NodeInterner, | ||
name: &str, | ||
arguments: Vec<(Value, Location)>, | ||
location: Location, | ||
) -> IResult<Value> { | ||
match name { | ||
"poseidon2_permutation" => poseidon2_permutation(interner, arguments, location), | ||
_ => { | ||
let item = format!("Comptime evaluation for builtin function {name}"); | ||
Err(InterpreterError::Unimplemented { item, location }) | ||
} | ||
} | ||
} | ||
|
||
// poseidon2_permutation<let N: u32>(_input: [Field; N], _state_length: u32) -> [Field; N] | ||
fn poseidon2_permutation( | ||
interner: &mut NodeInterner, | ||
mut arguments: Vec<(Value, Location)>, | ||
location: Location, | ||
) -> IResult<Value> { | ||
check_argument_count(2, &arguments, location)?; | ||
|
||
let state_length = get_u32(arguments.pop().unwrap().0, location)?; | ||
let (input, typ) = get_array(interner, arguments.pop().unwrap().0, location)?; | ||
|
||
let input = try_vecmap(input, |integer| get_field(integer, location))?; | ||
|
||
// Currently locked to only bn254! | ||
let fields = Bn254BlackBoxSolver | ||
.poseidon2_permutation(&input, state_length) | ||
.map_err(|error| InterpreterError::BlackBoxError(error, location))?; | ||
|
||
let array = fields.into_iter().map(Value::Field).collect(); | ||
Ok(Value::Array(array, typ)) | ||
} |