Skip to content

Commit

Permalink
fix: allow range checks to be performed within the comptime intepreter (
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Nov 14, 2024
1 parent 35408ab commit 852c87a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
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>()
}
}

0 comments on commit 852c87a

Please sign in to comment.