Skip to content

Commit

Permalink
chore(ssa refactor): acir-gen for NOT instruction (#1450)
Browse files Browse the repository at this point in the history
* add field mul and div

* add code to process field mul and div

* add assert example

* add `is_equal` constraint

* add `eq_var` method for AcirVar

* process `Constrain` instruction and BinaryOp::Eq

* add TODO for more than the maximum number of bits

* add numeric_cast_var method which constrains a variable to be equal to a NumericType

* implement casting for numeric types

* add simple range constraint example

* add constraints for `more_than_eq`

* - add more_than_eq method
- This method needs to know the bit size, so we cache this information whenever we do a range constraint.
We should ideally also cache it for constants too since we can figure out their bit-sizes easily

* add method to process less than binary operation

* add example

* assign result of cast operation

* add `y` as an input value

* return optimized circuit

* Addressed in Address GtEq extra opcodes #1444

* chore(ssa refactor): acir-gen for ! instruction

* inline function

* Update crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs

* Update crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs

* chore(ssa refactor): return acir var/result pair for binding

---------

Co-authored-by: Kevaundray Wedderburn <[email protected]>
  • Loading branch information
joss-aztec and kevaundray authored May 31, 2023
1 parent eae624b commit 9c05a24
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.6.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// A simple program for testing the NOT op
fn main(x : bool) -> pub bool {
!x
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ impl AcirContext {
}
}

/// Adds a new variable that is constrained to be the logical NOT of `x`.
///
/// `x` must be a 1-bit integer (i.e. a boolean)
pub(crate) fn not_var(&mut self, x: AcirVar) -> AcirVar {
assert_eq!(
self.variables_to_bit_sizes.get(&x),
Some(&1),
"ICE: NOT op applied to non-bool"
);
let data = &self.data[&x];
// Since `x` can only be 0 or 1, we can derive NOT as 1 - x
match data {
AcirVarData::Const(constant) => {
self.add_data(AcirVarData::Expr(&Expression::one() - &Expression::from(*constant)))
}
AcirVarData::Expr(expr) => self.add_data(AcirVarData::Expr(&Expression::one() - expr)),
AcirVarData::Witness(witness) => {
self.add_data(AcirVarData::Expr(&Expression::one() - *witness))
}
}
}

/// Converts the `AcirVar` to a `Witness` if it hasn't been already, and appends it to the
/// `GeneratedAcir`'s return witnesses.
pub(crate) fn return_var(&mut self, acir_var: AcirVar) {
Expand Down
8 changes: 8 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ impl Context {
assert_eq!(result_ids.len(), 1, "Cast ops have a single result");
(vec![result_ids[0]], vec![result_acir_var])
}
Instruction::Not(value_id) => {
let boolean_var = self.convert_ssa_value(*value_id, dfg);
let result_acir_var = self.acir_context.not_var(boolean_var);

let result_ids = dfg.instruction_results(instruction_id);
assert_eq!(result_ids.len(), 1, "Not ops have a single result");
(vec![result_ids[0]], vec![result_acir_var])
}
_ => todo!("{instruction:?}"),
};

Expand Down

0 comments on commit 9c05a24

Please sign in to comment.