Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow range checks to be performed within the comptime intepreter #6514

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
};

use self::builtin_helpers::{eq_item, get_array, get_ctstring, get_str, get_u8, hash_item, lex};
use super::Interpreter;
use super::{foreign, Interpreter};

pub(crate) mod builtin_helpers;

Expand All @@ -57,6 +57,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
let interner = &mut self.elaborator.interner;
let call_stack = &self.elaborator.interpreter_call_stack;
match name {
"apply_range_constraint" => foreign::apply_range_constraint(arguments, location),
"array_as_str_unchecked" => array_as_str_unchecked(interner, arguments, location),
"array_len" => array_len(interner, arguments, location),
"assert_constant" => Ok(Value::Bool(true)),
Expand Down
26 changes: 25 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use acvm::blackbox_solver::BlackBoxFunctionSolver;
use acvm::{
acir::BlackBoxFunc, blackbox_solver::BlackBoxFunctionSolver, AcirField, BlackBoxResolutionError,
};
use bn254_blackbox_solver::Bn254BlackBoxSolver;
use im::Vector;
use iter_extended::try_vecmap;
Expand Down Expand Up @@ -29,6 +31,28 @@ pub(super) fn call_foreign(
}
}

pub(super) fn apply_range_constraint(
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let (value, num_bits) = check_two_arguments(arguments, location)?;

let input = get_field(value)?;
let num_bits = get_u32(num_bits)?;

if input.num_bits() < num_bits {
Ok(Value::Unit)
} else {
Err(InterpreterError::BlackBoxError(
BlackBoxResolutionError::Failed(
BlackBoxFunc::RANGE,
"value exceeds range check bounds".to_owned(),
),
location,
))
}
}

// poseidon2_permutation<let N: u32>(_input: [Field; N], _state_length: u32) -> [Field; N]
fn poseidon2_permutation(
interner: &mut NodeInterner,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "comptime_apply_failing_range_constraint"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
comptime {
256.assert_max_bit_size::<8>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "comptime_apply_range_constraint"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
comptime {
2.assert_max_bit_size::<8>()
}
}
Loading