diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs index 7b9ac3a720c..e6ec90cbbb0 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs @@ -347,11 +347,14 @@ 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 { - // Since `x` can only be 0 or 1, we can derive NOT as 1 - x - self.add_data(AcirVarData::Expr(&Expression::one() - self.vars[x].to_expression().as_ref())) + pub(crate) fn not_var(&mut self, x: AcirVar, typ: AcirType) -> Result { + if typ.is_signed() { + todo!("implement NOT for signed integers"); + } + let bit_size = typ.bit_size(); + // Subtracting from max flips the bits + let max = self.add_constant(FieldElement::from((1_u128 << bit_size) - 1)); + self.sub_var(max, x) } /// Returns an `AcirVar` that is constrained to be `lhs << rhs`. diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs index 9e61d579e48..7f87bce1f62 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs @@ -221,8 +221,14 @@ impl Context { } } Instruction::Not(value_id) => { - let boolean_var = self.convert_numeric_value(*value_id, dfg); - let result_acir_var = self.acir_context.not_var(boolean_var); + let (acir_var, typ) = match self.convert_value(*value_id, dfg) { + AcirValue::Var(acir_var, typ) => (acir_var, typ), + _ => unreachable!("NOT is only applied to numerics"), + }; + let result_acir_var = self + .acir_context + .not_var(acir_var, typ) + .expect("add Result types to all methods so errors bubble up"); self.define_result_var(dfg, instruction_id, result_acir_var); } Instruction::Truncate { value, bit_size, max_bit_size } => {