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: remove equality operation on boolean constraints against constants #5919

Merged
merged 2 commits into from
Sep 4, 2024
Merged
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
44 changes: 35 additions & 9 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,40 @@ impl<'block> BrilligBlock<'block> {
self.convert_ssa_binary(binary, dfg, result_var);
}
Instruction::Constrain(lhs, rhs, assert_message) => {
let condition = SingleAddrVariable {
address: self.brillig_context.allocate_register(),
bit_size: 1,
let (condition, deallocate) = match (
dfg.get_numeric_constant_with_type(*lhs),
dfg.get_numeric_constant_with_type(*rhs),
) {
// If the constraint is of the form `x == u1 1` then we can simply constrain `x` directly
(
Some((constant, Type::Numeric(NumericType::Unsigned { bit_size: 1 }))),
None,
) if constant == FieldElement::one() => {
(self.convert_ssa_single_addr_value(*rhs, dfg), false)
}
(
None,
Some((constant, Type::Numeric(NumericType::Unsigned { bit_size: 1 }))),
) if constant == FieldElement::one() => {
(self.convert_ssa_single_addr_value(*lhs, dfg), false)
}

// Otherwise we need to perform the equality explicitly.
_ => {
let condition = SingleAddrVariable {
address: self.brillig_context.allocate_register(),
bit_size: 1,
};
self.convert_ssa_binary(
&Binary { lhs: *lhs, rhs: *rhs, operator: BinaryOp::Eq },
dfg,
condition,
);

(condition, true)
}
};

self.convert_ssa_binary(
&Binary { lhs: *lhs, rhs: *rhs, operator: BinaryOp::Eq },
dfg,
condition,
);
match assert_message {
Some(ConstrainError::Dynamic(selector, values)) => {
let payload_values =
Expand All @@ -287,7 +311,9 @@ impl<'block> BrilligBlock<'block> {
self.brillig_context.codegen_constrain(condition, None);
}
}
self.brillig_context.deallocate_single_addr(condition);
if deallocate {
self.brillig_context.deallocate_single_addr(condition);
}
}
Instruction::Allocate => {
let result_value = dfg.instruction_results(instruction_id)[0];
Expand Down
Loading