Skip to content

Commit

Permalink
chore: Replace explicit if-elses with FieldElement::from<bool>() fo…
Browse files Browse the repository at this point in the history
…r boolean fields (#1266)

* chore: replace if-elses with `FieldElement::from<bool>()`

* chore: replace explicit equality with usage of `is_zero()`

* chore: replace explicit usage of `from` with `.into()`
  • Loading branch information
TomAFrench authored May 2, 2023
1 parent 562c185 commit 62b7496
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
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

0 comments on commit 62b7496

Please sign in to comment.