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

feat: Add assert_max_bit_size method to Field #4016

Merged
merged 12 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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]);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
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()),
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
})
} 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);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading