Skip to content
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
13 changes: 13 additions & 0 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ impl FunctionBuilder {
self.insert_instruction(Instruction::Constrain(lhs, rhs, assert_message), None);
}

/// Insert a [`Instruction::RangeCheck`] instruction at the end of the current block.
pub(crate) fn insert_range_check(
&mut self,
value: ValueId,
max_bit_size: u32,
assert_message: Option<String>,
) {
self.insert_instruction(
Instruction::RangeCheck { value, max_bit_size, assert_message },
None,
);
}

/// Insert a call instruction at the end of the current block and return
/// the results of the call.
pub(crate) fn insert_call(
Expand Down
22 changes: 10 additions & 12 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,11 @@ impl<'a> FunctionContext<'a> {
self.check_shift_overflow(result, rhs, bit_size, location, false)
} else {
let message = format!("attempt to {} with overflow", op_name);
let range_constraint = Instruction::RangeCheck {
value: result,
max_bit_size: bit_size,
assert_message: Some(message),
};
self.builder.set_location(location).insert_instruction(range_constraint, None);
self.builder.set_location(location).insert_range_check(
result,
bit_size,
Some(message),
);
result
}
}
Expand Down Expand Up @@ -464,12 +463,11 @@ impl<'a> FunctionContext<'a> {
let product_field = self.builder.insert_binary(lhs_abs, BinaryOp::Mul, rhs_abs);
// It must not already overflow the bit_size
let message = "attempt to multiply with overflow".to_string();
let size_overflow = Instruction::RangeCheck {
value: product_field,
max_bit_size: bit_size,
assert_message: Some(message.clone()),
};
self.builder.set_location(location).insert_instruction(size_overflow, None);
self.builder.set_location(location).insert_range_check(
product_field,
bit_size,
Some(message.clone()),
);
let product = self.builder.insert_cast(product_field, Type::unsigned(bit_size));

// Then we check the signed product fits in a signed integer of bit_size-bits
Expand Down