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

chore: Replace explicit if-elses with FieldElement::from<bool>() for boolean fields #1266

Merged
merged 3 commits into from
May 2, 2023
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
14 changes: 2 additions & 12 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ impl InputValue {

InputValue::Field(new_value)
}
TomlTypes::Bool(boolean) => {
let new_value = if boolean { FieldElement::one() } else { FieldElement::zero() };

InputValue::Field(new_value)
}
TomlTypes::Bool(boolean) => InputValue::Field(boolean.into()),
TomlTypes::ArrayNum(arr_num) => {
let array_elements =
vecmap(arr_num, |elem_num| FieldElement::from(i128::from(elem_num)));
Expand All @@ -132,13 +128,7 @@ impl InputValue {
InputValue::Vec(array_elements)
}
TomlTypes::ArrayBool(arr_bool) => {
let array_elements = vecmap(arr_bool, |elem_bool| {
if elem_bool {
FieldElement::one()
} else {
FieldElement::zero()
}
});
let array_elements = vecmap(arr_bool, FieldElement::from);

InputValue::Vec(array_elements)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub(super) fn simplify_bitwise(
let max = FieldElement::from((1_u128 << bit_size) - 1);

let (field, var) = match (lhs.to_const(), rhs.to_const()) {
(Some(l_c), None) => (l_c == FieldElement::zero() || l_c == max).then_some((l_c, rhs))?,
(None, Some(r_c)) => (r_c == FieldElement::zero() || r_c == max).then_some((r_c, lhs))?,
(Some(l_c), None) => (l_c.is_zero() || l_c == max).then_some((l_c, rhs))?,
(None, Some(r_c)) => (r_c.is_zero() || r_c == max).then_some((r_c, lhs))?,
_ => return None,
};

Expand Down
42 changes: 24 additions & 18 deletions crates/noirc_evaluator/src/ssa/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,10 @@ impl Binary {
!res_type.is_native_field(),
"ICE: comparisons are not implemented for field elements"
);
let res = if lhs < rhs { FieldElement::one() } else { FieldElement::zero() };
return Ok(NodeEval::Const(res, ObjectType::boolean()));
return Ok(NodeEval::Const(
FieldElement::from(lhs < rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::Ule => {
Expand All @@ -931,8 +933,10 @@ impl Binary {
!res_type.is_native_field(),
"ICE: comparisons are not implemented for field elements"
);
let res = if lhs <= rhs { FieldElement::one() } else { FieldElement::zero() };
return Ok(NodeEval::Const(res, ObjectType::boolean()));
return Ok(NodeEval::Const(
FieldElement::from(lhs <= rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::Slt => (),
Expand All @@ -942,39 +946,41 @@ impl Binary {
return Ok(NodeEval::Const(FieldElement::zero(), ObjectType::boolean()));
//n.b we assume the type of lhs and rhs is unsigned because of the opcode, we could also verify this
} else if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
let res = if lhs < rhs { FieldElement::one() } else { FieldElement::zero() };
return Ok(NodeEval::Const(res, ObjectType::boolean()));
return Ok(NodeEval::Const(
FieldElement::from(lhs < rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::Lte => {
if l_is_zero {
return Ok(NodeEval::Const(FieldElement::one(), ObjectType::boolean()));
//n.b we assume the type of lhs and rhs is unsigned because of the opcode, we could also verify this
} else if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
let res = if lhs <= rhs { FieldElement::one() } else { FieldElement::zero() };
return Ok(NodeEval::Const(res, ObjectType::boolean()));
return Ok(NodeEval::Const(
FieldElement::from(lhs <= rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::Eq => {
if self.lhs == self.rhs {
return Ok(NodeEval::Const(FieldElement::one(), ObjectType::boolean()));
} else if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
if lhs == rhs {
return Ok(NodeEval::Const(FieldElement::one(), ObjectType::boolean()));
} else {
return Ok(NodeEval::Const(FieldElement::zero(), ObjectType::boolean()));
}
return Ok(NodeEval::Const(
FieldElement::from(lhs == rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::Ne => {
if self.lhs == self.rhs {
return Ok(NodeEval::Const(FieldElement::zero(), ObjectType::boolean()));
} else if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
if lhs != rhs {
return Ok(NodeEval::Const(FieldElement::one(), ObjectType::boolean()));
} else {
return Ok(NodeEval::Const(FieldElement::zero(), ObjectType::boolean()));
}
return Ok(NodeEval::Const(
FieldElement::from(lhs != rhs),
ObjectType::boolean(),
));
}
}
BinaryOp::And => {
Expand Down