Skip to content

Commit

Permalink
chore: convert Intrinsic::ApplyRangeConstraint into `Instruction::R…
Browse files Browse the repository at this point in the history
…angeCheck`
  • Loading branch information
TomAFrench committed Jan 11, 2024
1 parent 1927658 commit e016972
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
13 changes: 1 addition & 12 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,18 +1713,7 @@ impl Context {
Ok(Self::convert_vars_to_values(vars, dfg, result_ids))
}
Intrinsic::ApplyRangeConstraint => {
let field = self.convert_value(arguments[0], dfg).into_var()?;

let bit_size = self.convert_value(arguments[1], dfg).into_var()?;
let bit_size = self.acir_context.constant(bit_size).to_u128() as u32;

self.acir_context.range_constrain_var(
field,
&NumericType::Unsigned { bit_size },
Some("call to apply_range_constraint".to_owned()),
)?;

Ok(Vec::new())
unreachable!("ICE: `Intrinsic::ApplyRangeConstraint` calls should be transformed into an `Instruction::RangeCheck`");
}
Intrinsic::ToRadix(endian) => {
let field = self.convert_value(arguments[0], dfg).into_var()?;
Expand Down
15 changes: 14 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,20 @@ pub(super) fn simplify_call(
SimplifyResult::None
}
}
Intrinsic::ApplyRangeConstraint => SimplifyResult::None,
Intrinsic::ApplyRangeConstraint => {
let value = arguments[0];
let max_bit_size = dfg.get_numeric_constant(arguments[1]);
if let Some(max_bit_size) = max_bit_size {
let max_bit_size = max_bit_size.to_u128() as u32;
SimplifyResult::SimplifiedToInstruction(Instruction::RangeCheck {
value,
max_bit_size,
assert_message: Some("call to apply_range_constraint".to_owned()),
})
} else {
SimplifyResult::None
}
}
Intrinsic::BlackBox(bb_func) => simplify_black_box_func(bb_func, arguments, dfg),
Intrinsic::Sort => simplify_sort(dfg, arguments),
Intrinsic::AsField => {
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Field {

pub fn assert_maximum_bit_size(self: Self, bit_size: u32) {
crate::assert_constant(bit_size);
assert(bit_size < modulus_num_bits());
assert(bit_size < modulus_num_bits() as u32);
self.__assert_maximum_bit_size(bit_size);
}

Expand Down

0 comments on commit e016972

Please sign in to comment.