From c459f5ed4a3670a854582e1b0c0bc63b1bd4d1c4 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 8 Jun 2023 13:44:32 +0000 Subject: [PATCH 1/3] add code for not operation --- .../brillig_not/Nargo.toml | 5 ++ .../brillig_not/Prover.toml | 2 + .../brillig_not/src/main.nr | 11 ++++ .../src/brillig/brillig_gen.rs | 59 +++++++++++++++++-- 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Prover.toml new file mode 100644 index 00000000000..a0397e89477 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Prover.toml @@ -0,0 +1,2 @@ +x = "1" +y = "0" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr new file mode 100644 index 00000000000..91ff8a20852 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr @@ -0,0 +1,11 @@ +// Tests a very simple program. +// +// The features being tested is not instruction on brillig +fn main(x: Field, y : Field) { + assert(false == not_operator(x as bool)); + assert(true == not_operator(y as bool)); +} + +unconstrained fn not_operator(x : bool) -> bool { + !x +} \ No newline at end of file diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen.rs b/crates/noirc_evaluator/src/brillig/brillig_gen.rs index f57503dd5e5..339e9c69431 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen.rs @@ -8,8 +8,11 @@ use crate::ssa_refactor::ir::{ types::{NumericType, Type}, value::{Value, ValueId}, }; -use acvm::acir::brillig_vm::{ - BinaryFieldOp, BinaryIntOp, Opcode as BrilligOpcode, RegisterIndex, Value as BrilligValue, +use acvm::{ + acir::brillig_vm::{ + BinaryFieldOp, BinaryIntOp, Opcode as BrilligOpcode, RegisterIndex, Value as BrilligValue, + }, + FieldElement, }; use std::collections::HashMap; @@ -37,11 +40,23 @@ impl BrilligGen { return *register_index; } - let register = RegisterIndex::from(self.latest_register); + let register = self.create_register(); + + // Cache the `ValueId` so that if we call it again, it will + // return the register that has just been created. + // + // WARNING: This assumes that a register has not been + // modified. If a MOV instruction has overwritten the value + // at a register, then this cache will be invalid. self.ssa_value_to_register.insert(value, register); - self.latest_register += 1; + register + } + /// Creates a new register. + fn create_register(&mut self) -> RegisterIndex { + let register = RegisterIndex::from(self.latest_register); + self.latest_register += 1; register } @@ -146,10 +161,44 @@ impl BrilligGen { let result_register = self.get_or_create_register(result_ids[0]); self.convert_ssa_binary(binary, dfg, result_register); } - _ => todo!("ICE: Instruction not supported"), + Instruction::Not(value) => { + let result_ids = dfg.instruction_results(instruction_id); + let result_register = self.get_or_create_register(result_ids[0]); + + assert_eq!( + dfg.type_of_value(*value), + Type::bool(), + "not operator can only be applied to boolean values" + ); + + let one = self.make_constant(FieldElement::one()); + let condition = self.convert_ssa_value(*value, dfg); + + // Compile !x as (1 - x) + let opcode = BrilligOpcode::BinaryIntOp { + destination: result_register, + op: BinaryIntOp::Sub, + bit_size: 1, + lhs: one, + rhs: condition, + }; + self.push_code(opcode) + } + _ => todo!("ICE: Instruction not supported {instruction:?}"), }; } + /// Returns a register which holds the value of a constant + fn make_constant(&mut self, constant: FieldElement) -> RegisterIndex { + let register = self.create_register(); + + let const_opcode = + BrilligOpcode::Const { destination: register, value: BrilligValue::from(constant) }; + self.push_code(const_opcode); + + register + } + /// Converts the Binary instruction into a sequence of Brillig opcodes. fn convert_ssa_binary( &mut self, From 4fa7aa6269a10d1bed29b72e96645f935146a76a Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 8 Jun 2023 13:49:30 +0000 Subject: [PATCH 2/3] fix clippy --- crates/noirc_evaluator/src/brillig/brillig_gen.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen.rs b/crates/noirc_evaluator/src/brillig/brillig_gen.rs index 339e9c69431..aae46952813 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen.rs @@ -182,7 +182,7 @@ impl BrilligGen { lhs: one, rhs: condition, }; - self.push_code(opcode) + self.push_code(opcode); } _ => todo!("ICE: Instruction not supported {instruction:?}"), }; From 8148867cded0647772bc6c42d1ea3f66cb99b9f9 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 8 Jun 2023 15:27:48 +0100 Subject: [PATCH 3/3] Update crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr --- .../tests/test_data_ssa_refactor/brillig_not/src/main.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr index 91ff8a20852..bc94810efb9 100644 --- a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr @@ -1,4 +1,4 @@ -// Tests a very simple program. +// Tests a very simple Brillig function. // // The features being tested is not instruction on brillig fn main(x: Field, y : Field) {