From 92ab4d83e89e4845b9883ab9bac8f77a82ca4068 Mon Sep 17 00:00:00 2001 From: Joss Date: Tue, 6 Jun 2023 18:01:03 +0100 Subject: [PATCH 01/11] chore(ssa gen): ssa gen truncate instruction --- .../src/ssa_refactor/ssa_builder/mod.rs | 12 +++++ .../src/ssa_refactor/ssa_gen/context.rs | 51 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs index ac022c9a9a7..520368a6a42 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs @@ -206,6 +206,18 @@ impl FunctionBuilder { self.insert_instruction(Instruction::Cast(value, typ), None).first() } + /// Insert a truncate instruction at the end of the current block. + /// Returns the result of the truncate instruction. + pub(crate) fn insert_truncate( + &mut self, + value: ValueId, + bit_size: u32, + max_bit_size: u32, + ) -> ValueId { + self.insert_instruction(Instruction::Truncate { value, bit_size, max_bit_size }, None) + .first() + } + /// Insert a constrain instruction at the end of the current block. pub(crate) fn insert_constrain(&mut self, boolean: ValueId) { self.insert_instruction(Instruction::Constrain(boolean), None); diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index 602c5a79030..daae6b68fba 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::sync::{Mutex, RwLock}; +use acvm::FieldElement; use iter_extended::vecmap; use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters}; use noirc_frontend::monomorphization::ast::{FuncId, Program}; @@ -10,7 +11,7 @@ use crate::ssa_refactor::ir::function::FunctionId as IrFunctionId; use crate::ssa_refactor::ir::function::{Function, RuntimeType}; use crate::ssa_refactor::ir::instruction::BinaryOp; use crate::ssa_refactor::ir::map::AtomicCounter; -use crate::ssa_refactor::ir::types::Type; +use crate::ssa_refactor::ir::types::{NumericType, Type}; use crate::ssa_refactor::ir::value::ValueId; use crate::ssa_refactor::ssa_builder::FunctionBuilder; @@ -226,6 +227,22 @@ impl<'a> FunctionContext<'a> { let mut result = self.builder.insert_binary(lhs, op, rhs); + let lhs_type = self.builder.current_function.dfg.type_of_value(lhs); + let rhs_type = self.builder.current_function.dfg.type_of_value(rhs); + if let Some(max_bit_size) = + operator_result_max_bit_size_to_truncate(operator, lhs_type, rhs_type) + { + let result_type = self.builder.current_function.dfg.type_of_value(result); + let bit_size = match result_type { + Type::Numeric(NumericType::Signed { bit_size }) + | Type::Numeric(NumericType::Unsigned { bit_size }) => bit_size, + _ => { + unreachable!("ICE: Truncation attempted on non-integer"); + } + }; + result = self.builder.insert_truncate(result, bit_size, max_bit_size); + } + if operator_requires_not(operator) { result = self.builder.insert_not(result); } @@ -334,6 +351,38 @@ fn operator_requires_swapped_operands(op: noirc_frontend::BinaryOpKind) -> bool matches!(op, Greater | LessEqual) } +/// If the operation requires its result to be truncated because it is an integer, the maximum +/// number of bits that result may occupy is returned. +fn operator_result_max_bit_size_to_truncate( + op: noirc_frontend::BinaryOpKind, + lhs_type: Type, + rhs_type: Type, +) -> Option { + let lhs_numeric = match lhs_type { + Type::Numeric(numeric) => numeric, + _ => return None, + }; + let rhs_numeric = match rhs_type { + Type::Numeric(numeric) => numeric, + _ => return None, + }; + let lhs_bit_size = match lhs_numeric { + NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size } => bit_size, + _ => return None, + }; + let rhs_bit_size = match rhs_numeric { + NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size } => bit_size, + _ => return None, + }; + use noirc_frontend::BinaryOpKind::*; + match op { + Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), + Multiply => Some(lhs_bit_size + rhs_bit_size), + ShiftRight => Some(FieldElement::max_num_bits()), + _ => None, + } +} + /// Converts the given operator to the appropriate BinaryOp. /// Take care when using this to insert a binary instruction: this requires /// checking operator_requires_not and operator_requires_swapped_operands From a006458e7c58242972cb1f6aa0dffee1b703c48a Mon Sep 17 00:00:00 2001 From: Joss Date: Wed, 7 Jun 2023 15:46:04 +0100 Subject: [PATCH 02/11] chore(ssa refactor): max bit size for subtract --- crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index daae6b68fba..ca8f146f2f2 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -377,6 +377,7 @@ fn operator_result_max_bit_size_to_truncate( use noirc_frontend::BinaryOpKind::*; match op { Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), + Subtract => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), Multiply => Some(lhs_bit_size + rhs_bit_size), ShiftRight => Some(FieldElement::max_num_bits()), _ => None, From 48b32a2c800dbc2f45a22a7c73b78a534490ad48 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 8 Jun 2023 10:19:29 +0100 Subject: [PATCH 03/11] Update crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs Co-authored-by: jfecher --- .../src/ssa_refactor/ssa_gen/context.rs | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index ca8f146f2f2..ea99df74356 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -358,22 +358,13 @@ fn operator_result_max_bit_size_to_truncate( lhs_type: Type, rhs_type: Type, ) -> Option { - let lhs_numeric = match lhs_type { - Type::Numeric(numeric) => numeric, - _ => return None, - }; - let rhs_numeric = match rhs_type { - Type::Numeric(numeric) => numeric, - _ => return None, - }; - let lhs_bit_size = match lhs_numeric { - NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size } => bit_size, - _ => return None, - }; - let rhs_bit_size = match rhs_numeric { - NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size } => bit_size, - _ => return None, + let get_bit_size = |typ| match typ { + Type::Numerc(NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size }) => Some(bit_size), + _ => None, }; + + let lhs_bit_size = get_bit_size(lhs_type)?; + let rhs_bit_size = get_bit_size(rhs_type)?; use noirc_frontend::BinaryOpKind::*; match op { Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), From 67982f33d86749d5eaa726390d9e16a3f7645856 Mon Sep 17 00:00:00 2001 From: Joss Date: Thu, 8 Jun 2023 11:27:11 +0100 Subject: [PATCH 04/11] chore(ssa refactor): truncate shift left --- .../noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index ea99df74356..4732fe30613 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -359,10 +359,12 @@ fn operator_result_max_bit_size_to_truncate( rhs_type: Type, ) -> Option { let get_bit_size = |typ| match typ { - Type::Numerc(NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size }) => Some(bit_size), + Type::Numeric(NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size }) => { + Some(bit_size) + } _ => None, }; - + let lhs_bit_size = get_bit_size(lhs_type)?; let rhs_bit_size = get_bit_size(rhs_type)?; use noirc_frontend::BinaryOpKind::*; @@ -370,7 +372,7 @@ fn operator_result_max_bit_size_to_truncate( Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), Subtract => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), Multiply => Some(lhs_bit_size + rhs_bit_size), - ShiftRight => Some(FieldElement::max_num_bits()), + ShiftLeft => Some(lhs_bit_size + 2_u32.pow(rhs_bit_size)), _ => None, } } From b7ccbf5ec591130046ae1d7092dd84865f4bc98c Mon Sep 17 00:00:00 2001 From: Joss Date: Thu, 8 Jun 2023 11:28:43 +0100 Subject: [PATCH 05/11] chore(ssa refactor): Add integer modulus when truncating subtraction --- .../src/ssa_refactor/acir_gen/mod.rs | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) 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 fdc67770789..3782a72b55e 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs @@ -22,6 +22,7 @@ use super::{ ssa_gen::Ssa, }; use crate::brillig::{artifact::BrilligArtifact, Brillig}; +use acvm::FieldElement; use noirc_abi::{AbiType, FunctionSignature, Sign}; pub(crate) use acir_ir::generated_acir::GeneratedAcir; @@ -271,14 +272,9 @@ impl Context { (vec![result_ids[0]], vec![result_acir_var]) } Instruction::Truncate { value, bit_size, max_bit_size } => { - let var = self.convert_ssa_value(*value, dfg); let result_ids = dfg.instruction_results(instruction_id); - - let result_acir_var = self - .acir_context - .truncate_var(var, *bit_size, *max_bit_size) - .expect("add Result types to all methods so errors bubble up"); - + let result_acir_var = + self.convert_ssa_truncate(*value, *bit_size, *max_bit_size, dfg); (vec![result_ids[0]], vec![result_acir_var]) } }; @@ -445,6 +441,33 @@ impl Context { } } + /// Returns an `AcirVar`that is constrained to be result of the truncation. + fn convert_ssa_truncate( + &mut self, + value_id: ValueId, + bit_size: u32, + max_bit_size: u32, + dfg: &DataFlowGraph, + ) -> AcirVar { + let mut var = self.convert_ssa_value(value_id, dfg); + let truncation_target = match &dfg[value_id] { + Value::Instruction { instruction, .. } => &dfg[*instruction], + _ => unreachable!("ICE: Truncates are only ever applied to the result of a binary op"), + }; + if matches!(truncation_target, Instruction::Binary(Binary { operator: BinaryOp::Sub, .. })) + { + // Subtractions must first have the integer modulus added before truncation can be + // applied. This is done in order to prevent underflow. + let integer_modulus = + self.acir_context.add_constant(FieldElement::from(2_u128.pow(bit_size))); + var = self.acir_context.add_var(var, integer_modulus); + } + + self.acir_context + .truncate_var(var, bit_size, max_bit_size) + .expect("add Result types to all methods so errors bubble up") + } + /// Returns a vector of `AcirVar`s constrained to be result of the function call. /// /// The function being called is required to be intrinsic. From c7748045370004dbfc39c5ec2f43d705271f0b57 Mon Sep 17 00:00:00 2001 From: Joss Date: Thu, 8 Jun 2023 12:56:45 +0100 Subject: [PATCH 06/11] chore(ssa refactor): clippy --- crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index 4732fe30613..d7baf5fb7ad 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::sync::{Mutex, RwLock}; -use acvm::FieldElement; use iter_extended::vecmap; use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters}; use noirc_frontend::monomorphization::ast::{FuncId, Program}; From e74481354658219507ddca7fe75825fd76d738ed Mon Sep 17 00:00:00 2001 From: Joss Date: Fri, 9 Jun 2023 12:31:26 +0100 Subject: [PATCH 07/11] chore(ssa refactor): fix left shift max bit size --- .../src/ssa_refactor/ssa_gen/context.rs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index 4a8e11d3166..eb043586d71 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -7,6 +7,7 @@ use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters}; use noirc_frontend::monomorphization::ast::{FuncId, Program}; use noirc_frontend::Signedness; +use crate::ssa_refactor::ir::dfg::DataFlowGraph; use crate::ssa_refactor::ir::function::FunctionId as IrFunctionId; use crate::ssa_refactor::ir::function::{Function, RuntimeType}; use crate::ssa_refactor::ir::instruction::BinaryOp; @@ -230,11 +231,12 @@ impl<'a> FunctionContext<'a> { let mut result = self.builder.insert_binary(lhs, op, rhs); - let lhs_type = self.builder.current_function.dfg.type_of_value(lhs); - let rhs_type = self.builder.current_function.dfg.type_of_value(rhs); - if let Some(max_bit_size) = - operator_result_max_bit_size_to_truncate(operator, lhs_type, rhs_type) - { + if let Some(max_bit_size) = operator_result_max_bit_size_to_truncate( + operator, + lhs, + rhs, + &self.builder.current_function.dfg, + ) { let result_type = self.builder.current_function.dfg.type_of_value(result); let bit_size = match result_type { Type::Numeric(NumericType::Signed { bit_size }) @@ -490,9 +492,13 @@ fn operator_requires_swapped_operands(op: noirc_frontend::BinaryOpKind) -> bool /// number of bits that result may occupy is returned. fn operator_result_max_bit_size_to_truncate( op: noirc_frontend::BinaryOpKind, - lhs_type: Type, - rhs_type: Type, + lhs: ValueId, + rhs: ValueId, + dfg: &DataFlowGraph, ) -> Option { + let lhs_type = dfg.type_of_value(lhs); + let rhs_type = dfg.type_of_value(rhs); + let get_bit_size = |typ| match typ { Type::Numeric(NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size }) => { Some(bit_size) @@ -507,7 +513,12 @@ fn operator_result_max_bit_size_to_truncate( Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), Subtract => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1), Multiply => Some(lhs_bit_size + rhs_bit_size), - ShiftLeft => Some(lhs_bit_size + 2_u32.pow(rhs_bit_size)), + ShiftLeft => { + let rhs_constant = + dfg.get_numeric_constant(rhs).expect("Left shift rhs must be a constant").to_u128() + as u32; + Some(lhs_bit_size + rhs_constant) + } _ => None, } } From 305df01380b1ac2dda11352f991346aea316ac4a Mon Sep 17 00:00:00 2001 From: Joss Date: Fri, 9 Jun 2023 11:15:31 +0100 Subject: [PATCH 08/11] chore(ssa refactor): cp xor test --- crates/nargo_cli/tests/test_data_ssa_refactor/xor/Nargo.toml | 5 +++++ .../nargo_cli/tests/test_data_ssa_refactor/xor/Prover.toml | 2 ++ .../nargo_cli/tests/test_data_ssa_refactor/xor/src/main.nr | 5 +++++ 3 files changed, 12 insertions(+) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/xor/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/xor/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/xor/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/xor/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/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/xor/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/Prover.toml new file mode 100644 index 00000000000..f28f2f8cc48 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/Prover.toml @@ -0,0 +1,2 @@ +x = "5" +y = "10" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/xor/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/src/main.nr new file mode 100644 index 00000000000..e893c938fc3 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/xor/src/main.nr @@ -0,0 +1,5 @@ +fn main(x : u32, y : pub u32) { + let m = x ^ y; + + assert(m != 10); +} \ No newline at end of file From 4d5c655899efc6f9720d90b4cb3d1839ce4cb96e Mon Sep 17 00:00:00 2001 From: Joss Date: Fri, 9 Jun 2023 14:14:18 +0100 Subject: [PATCH 09/11] chore(ssa refactor): cp working tests --- .../test_data_ssa_refactor/struct/Nargo.toml | 7 ++ .../test_data_ssa_refactor/struct/Prover.toml | 2 + .../test_data_ssa_refactor/struct/src/main.nr | 77 +++++++++++++++++++ .../struct_fields_ordering/Nargo.toml | 5 ++ .../struct_fields_ordering/Prover.toml | 3 + .../struct_fields_ordering/src/main.nr | 14 ++++ .../submodules/Nargo.toml | 7 ++ .../submodules/Prover.toml | 2 + .../submodules/src/main.nr | 17 ++++ 9 files changed, 134 insertions(+) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/submodules/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Prover.toml new file mode 100644 index 00000000000..7d59cc81807 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/Prover.toml @@ -0,0 +1,2 @@ +x = "0" +y = "1" \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/struct/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/src/main.nr new file mode 100644 index 00000000000..6d61393920d --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct/src/main.nr @@ -0,0 +1,77 @@ +use dep::std; + +struct Foo { + bar: Field, + array: [Field; 2], +} + +struct Pair { + first: Foo, + second: Field, +} + +impl Foo { + fn default(x: Field,y: Field) -> Self { + Self { bar: 0, array: [x,y] } + } +} + +impl Pair { + fn foo(p: Self) -> Foo { + p.first + } + + fn bar(self) -> Field { + self.foo().bar + } +} + +struct Nested { + a: Field, + b: Field +} +struct MyStruct { + my_bool: bool, + my_int: u32, + my_nest: Nested, +} +fn test_struct_in_tuple(a_bool : bool,x:Field, y:Field) -> (MyStruct, bool) { + let my_struct = MyStruct { + my_bool: a_bool, + my_int: 5, + my_nest: Nested{a:x,b:y}, + }; + (my_struct, a_bool) +} + +struct Animal { + legs: Field, + eyes: u8, +} + +fn get_dog() -> Animal { + let dog = Animal { legs: 4, eyes: 2 }; + dog +} + +fn main(x: Field, y: Field) { + let first = Foo::default(x,y); + let p = Pair { first, second: 1 }; + + assert(p.bar() == x); + assert(p.second == y); + assert(p.first.array[0] != p.first.array[1]); + + // Nested structs + let (struct_from_tuple, a_bool) = test_struct_in_tuple(true,x,y); + assert(struct_from_tuple.my_bool == true); + assert(a_bool == true); + assert(struct_from_tuple.my_int == 5); + assert(struct_from_tuple.my_nest.a == 0); + + // Regression test for issue #670 + let Animal { legs, eyes } = get_dog(); + let six = legs + eyes as Field; + + assert(six == 6); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/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/struct_fields_ordering/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Prover.toml new file mode 100644 index 00000000000..70640bba4cc --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/Prover.toml @@ -0,0 +1,3 @@ +[y] +foo = "5" +bar = "7" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/src/main.nr new file mode 100644 index 00000000000..0d6e411addf --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/struct_fields_ordering/src/main.nr @@ -0,0 +1,14 @@ +use dep::std; + +// Note that fields are not in alphabetical order. +// We want to check that this ordering is maintained +struct myStruct { + foo: u32, + bar: Field, +} + +fn main(y : pub myStruct) { + assert(y.foo == 5); + assert(y.bar == 7); +} + diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Prover.toml new file mode 100644 index 00000000000..b6626a67e19 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/Prover.toml @@ -0,0 +1,2 @@ +x = 1 +y = 0 diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/src/main.nr new file mode 100644 index 00000000000..9bfe382663f --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/submodules/src/main.nr @@ -0,0 +1,17 @@ +use mysubmodule::my_helper; + +fn main(x: u1, y: u1) { + my_helper(); + mysubmodule::my_bool_or(x, y); +} + +mod mysubmodule { + use dep::std; + + fn my_bool_or(x: u1, y: u1) { + assert(x | y == 1); + } + + fn my_helper() {} +} + From c12db287e24bc8ae959607f5611e75c8b4c50154 Mon Sep 17 00:00:00 2001 From: Joss Date: Fri, 9 Jun 2023 15:16:13 +0100 Subject: [PATCH 10/11] chore(ssa refactor): more working tests --- .../test_data_ssa_refactor/assert/Nargo.toml | 5 + .../test_data_ssa_refactor/assert/Prover.toml | 1 + .../test_data_ssa_refactor/assert/src/main.nr | 3 + .../test_data_ssa_refactor/bit_and/Nargo.toml | 5 + .../bit_and/Prover.toml | 2 + .../bit_and/src/main.nr | 18 ++ .../bool_not/Nargo.toml | 7 + .../bool_not/Prover.toml | 1 + .../bool_not/src/main.nr | 5 + .../test_data_ssa_refactor/bool_or/Nargo.toml | 7 + .../bool_or/Prover.toml | 2 + .../bool_or/src/main.nr | 7 + .../cast_bool/Nargo.toml | 7 + .../cast_bool/Prover.toml | 2 + .../cast_bool/src/main.nr | 6 + .../comptime_array_access/Nargo.toml | 5 + .../comptime_array_access/Prover.toml | 1 + .../comptime_array_access/src/main.nr | 17 + .../comptime_recursion_regression/Nargo.toml | 5 + .../comptime_recursion_regression/Prover.toml | 2 + .../comptime_recursion_regression/src/main.nr | 4 + .../contracts/Nargo.toml | 5 + .../contracts/Prover.toml | 2 + .../contracts/src/main.nr | 8 + .../hash_to_field/Nargo.toml | 5 + .../hash_to_field/Prover.toml | 1 + .../hash_to_field/src/main.nr | 5 + .../main_bool_arg/Nargo.toml | 6 + .../main_bool_arg/Prover.toml | 2 + .../main_bool_arg/src/main.nr | 8 + .../main_return/Nargo.toml | 6 + .../main_return/Prover.toml | 1 + .../main_return/src/main.nr | 3 + .../test_data_ssa_refactor/modules/Nargo.toml | 5 + .../modules/Prover.toml | 2 + .../test_data_ssa_refactor/modules/src/foo.nr | 3 + .../modules/src/main.nr | 14 + .../modules_more/Nargo.toml | 5 + .../modules_more/Prover.toml | 4 + .../modules_more/src/foo.nr | 5 + .../modules_more/src/foo/bar.nr | 3 + .../modules_more/src/main.nr | 6 + .../test_data_ssa_refactor/modulus/Nargo.toml | 5 + .../modulus/Prover.toml | 290 ++++++++++++++++++ .../modulus/src/main.nr | 27 ++ .../test_data_ssa_refactor/pred_eq/Nargo.toml | 7 + .../pred_eq/Prover.toml | 2 + .../pred_eq/src/main.nr | 6 + 48 files changed, 548 insertions(+) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/assert/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/assert/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/assert/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/contracts/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/main_return/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/foo.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo/bar.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/modulus/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/assert/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/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/assert/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/Prover.toml new file mode 100644 index 00000000000..4dd6b405159 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/Prover.toml @@ -0,0 +1 @@ +x = "1" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/assert/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/src/main.nr new file mode 100644 index 00000000000..00e94414c0b --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/assert/src/main.nr @@ -0,0 +1,3 @@ +fn main(x: Field) { + assert(x == 1); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/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/bit_and/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Prover.toml new file mode 100644 index 00000000000..40ce2b0bc27 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/Prover.toml @@ -0,0 +1,2 @@ +x = "0x00" +y = "0x10" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/src/main.nr new file mode 100644 index 00000000000..f4805960a33 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_and/src/main.nr @@ -0,0 +1,18 @@ +// You can only do bit operations with integers. +// (Kobi/Daira/Circom/#37) https://github.com/iden3/circom/issues/37 +fn main(x : Field, y : Field) { + let x_as_u8 = x as u8; + let y_as_u8 = y as u8; + + assert((x_as_u8 & y_as_u8) == x_as_u8); + + //bitwise and with 1 bit: + let flag = (x == 0) & (y == 16); + assert(flag); + + //bitwise and with odd bits: + let x_as_u11 = x as u11; + let y_as_u11 = y as u11; + assert((x_as_u11 & y_as_u11) == x_as_u11); +} + diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Prover.toml new file mode 100644 index 00000000000..4dd6b405159 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/Prover.toml @@ -0,0 +1 @@ +x = "1" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/src/main.nr new file mode 100644 index 00000000000..d6b4d7a9fad --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_not/src/main.nr @@ -0,0 +1,5 @@ +use dep::std; +fn main(x: u1) { + assert(!x == 0); +} + diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Prover.toml new file mode 100644 index 00000000000..a0397e89477 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/Prover.toml @@ -0,0 +1,2 @@ +x = "1" +y = "0" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/src/main.nr new file mode 100644 index 00000000000..4a74027e4aa --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bool_or/src/main.nr @@ -0,0 +1,7 @@ +use dep::std; +fn main(x: u1, y: u1) { + assert(x | y == 1); + + assert(x | y | x == 1); +} + diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Prover.toml new file mode 100644 index 00000000000..f489cbac003 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/Prover.toml @@ -0,0 +1,2 @@ +x = "10" +y = "10" \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/src/main.nr new file mode 100644 index 00000000000..57af8120b33 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/cast_bool/src/main.nr @@ -0,0 +1,6 @@ +fn main(x: Field, y: Field) { + let z = x == y; + let t = z as u8; + assert(t == 1); +} + diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/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/comptime_array_access/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Prover.toml new file mode 100644 index 00000000000..ec8d8e34856 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/Prover.toml @@ -0,0 +1 @@ +a = [1, 2, 3] diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/src/main.nr new file mode 100644 index 00000000000..04f08bb70c5 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_array_access/src/main.nr @@ -0,0 +1,17 @@ +fn main(a: [Field; 3]) { + let i = 1; + + // Using a comptime variable as a parameter should not make it non-comptime + let ii = foo(i); + let elem1 = a[i]; + + // Nor should using it in an expression with a non-comptime variable. + let two = i + ii; + assert(i == ii); + + let elem2 = a[i]; + assert(elem1 == elem2); + assert(two == 2); +} + +fn foo(x: Field) -> Field { x } diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/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/comptime_recursion_regression/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Prover.toml new file mode 100644 index 00000000000..745ce7c2361 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/Prover.toml @@ -0,0 +1,2 @@ +x = 5 +y = 6 diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/src/main.nr new file mode 100644 index 00000000000..0461fd9c4cb --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/comptime_recursion_regression/src/main.nr @@ -0,0 +1,4 @@ +fn main(x: Field, y: Field) { + let flag = (x == 1) | (y == 2); + assert(flag | false == flag); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/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/contracts/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Prover.toml new file mode 100644 index 00000000000..97d5b1e0eed --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/Prover.toml @@ -0,0 +1,2 @@ +x = 3 +y = 2 diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/src/main.nr new file mode 100644 index 00000000000..53e094eb4cc --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/contracts/src/main.nr @@ -0,0 +1,8 @@ +fn main(x : Field, y : pub Field) { + assert(x * 2 == y * 3); +} + +contract Foo { + fn double(x: Field) -> pub Field { x * 2 } + fn triple(x: Field) -> pub Field { x * 3 } +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/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/hash_to_field/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Prover.toml new file mode 100644 index 00000000000..f6597d3f78a --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/Prover.toml @@ -0,0 +1 @@ +input = "1" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/src/main.nr new file mode 100644 index 00000000000..ffc334179ee --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/hash_to_field/src/main.nr @@ -0,0 +1,5 @@ +use dep::std; + +fn main(input : Field) -> pub Field { + std::hash::hash_to_field([input]) +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Nargo.toml new file mode 100644 index 00000000000..fb93b9aa2a7 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Nargo.toml @@ -0,0 +1,6 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Prover.toml new file mode 100644 index 00000000000..f932e0b4817 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/Prover.toml @@ -0,0 +1,2 @@ +x = true +y = [true, false] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/src/main.nr new file mode 100644 index 00000000000..0615a7dbca4 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_bool_arg/src/main.nr @@ -0,0 +1,8 @@ +fn main(x : bool, y: [bool;2]) { + if x { + assert(1 != 2); + } + + assert(x); + assert(y[0] != y[1]); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Nargo.toml new file mode 100644 index 00000000000..fb93b9aa2a7 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Nargo.toml @@ -0,0 +1,6 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Prover.toml new file mode 100644 index 00000000000..63e9878811a --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/Prover.toml @@ -0,0 +1 @@ +x = "8" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/src/main.nr new file mode 100644 index 00000000000..06347eb0919 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/main_return/src/main.nr @@ -0,0 +1,3 @@ +fn main(x: pub Field) -> pub Field { + x +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/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/modules/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/Prover.toml new file mode 100644 index 00000000000..c0a0cdfbeb0 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/Prover.toml @@ -0,0 +1,2 @@ +x = "2" +y = "13" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/foo.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/foo.nr new file mode 100644 index 00000000000..1f771fa9425 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/foo.nr @@ -0,0 +1,3 @@ +fn hello(x : Field) -> Field { + x +} \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/main.nr new file mode 100644 index 00000000000..167f7e671a0 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules/src/main.nr @@ -0,0 +1,14 @@ +mod foo; +// This is a comment. +// +// `main` is the entry point to a binary +// +// You can have a `Binary` or a `Library` +// Release : 0.2 +// +// To run a proof on the command line, type `cargo run prove {proof_name}` +// +// To verify that proof, type `cargo run verify {proof_name}` +fn main(x: Field, y: pub Field) { + assert(x != foo::hello(y)); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/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/modules_more/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Prover.toml new file mode 100644 index 00000000000..39a4ddb9d15 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/Prover.toml @@ -0,0 +1,4 @@ + + x = "5" + y = "15" + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo.nr new file mode 100644 index 00000000000..ee0d20082f5 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo.nr @@ -0,0 +1,5 @@ +mod bar; + +fn hello(x : Field) -> Field { + x +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo/bar.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo/bar.nr new file mode 100644 index 00000000000..a92fb81dceb --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/foo/bar.nr @@ -0,0 +1,3 @@ +fn from_bar(x : Field) -> Field { + x +} \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/main.nr new file mode 100644 index 00000000000..8862e5a8650 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modules_more/src/main.nr @@ -0,0 +1,6 @@ +mod foo; + +// An example of the module system +fn main(x: Field, y: Field) { + assert(x != foo::bar::from_bar(y)); +} diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/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/modulus/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Prover.toml new file mode 100644 index 00000000000..d435609bb1a --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/Prover.toml @@ -0,0 +1,290 @@ +bn254_modulus_be_bytes = [ + 48, + 100, + 78, + 114, + 225, + 49, + 160, + 41, + 184, + 80, + 69, + 182, + 129, + 129, + 88, + 93, + 40, + 51, + 232, + 72, + 121, + 185, + 112, + 145, + 67, + 225, + 245, + 147, + 240, + 0, + 0, + 1, +] +bn254_modulus_be_bits = [ + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, +] diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/src/main.nr new file mode 100644 index 00000000000..4a13a6e06ba --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/modulus/src/main.nr @@ -0,0 +1,27 @@ +use dep::std; + +fn main(bn254_modulus_be_bytes : [u8; 32], bn254_modulus_be_bits : [u1; 254]) -> pub Field { + let modulus_size = std::field::modulus_num_bits(); + // NOTE: The constraints used in this circuit will only work when testing nargo with the plonk bn254 backend + assert(modulus_size == 254); + + let modulus_be_byte_array = std::field::modulus_be_bytes(); + for i in 0..32 { + assert(modulus_be_byte_array[i] == bn254_modulus_be_bytes[i]); + } + let modulus_le_byte_array = std::field::modulus_le_bytes(); + for i in 0..32 { + assert(modulus_le_byte_array[i] == bn254_modulus_be_bytes[31-i]); + } + + let modulus_be_bits = std::field::modulus_be_bits(); + for i in 0..254 { + assert(modulus_be_bits[i] == bn254_modulus_be_bits[i]); + } + let modulus_le_bits = std::field::modulus_le_bits(); + for i in 0..254 { + assert(modulus_le_bits[i] == bn254_modulus_be_bits[253-i]); + } + + modulus_size +} \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Nargo.toml new file mode 100644 index 00000000000..d9434868157 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Nargo.toml @@ -0,0 +1,7 @@ + + [package] + authors = [""] + compiler_version = "0.1" + + [dependencies] + \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Prover.toml new file mode 100644 index 00000000000..465ef562de4 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/Prover.toml @@ -0,0 +1,2 @@ +x = "1" +y = "1" diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/src/main.nr new file mode 100644 index 00000000000..c7986cb7af3 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/pred_eq/src/main.nr @@ -0,0 +1,6 @@ +use dep::std; + +fn main(x: Field, y: Field) { + let p = x == y; + assert(p == true); +} From ecd9c11cc48c3999f687d848ad366e161069b666 Mon Sep 17 00:00:00 2001 From: Joss Date: Mon, 12 Jun 2023 09:00:49 +0100 Subject: [PATCH 11/11] chore(ssa refactor): cp working test --- .../bit_shifts_comptime/Nargo.toml | 5 +++++ .../bit_shifts_comptime/Prover.toml | 1 + .../bit_shifts_comptime/src/main.nr | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/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/bit_shifts_comptime/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Prover.toml new file mode 100644 index 00000000000..cfd62c406cb --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/Prover.toml @@ -0,0 +1 @@ +x = 64 diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/src/main.nr new file mode 100644 index 00000000000..c1c6890febb --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/bit_shifts_comptime/src/main.nr @@ -0,0 +1,13 @@ +fn main(x: u64) { + let two: u64 = 2; + let three: u64 = 3; + + // comptime shifts on comptime values + assert(two << 2 == 8); + assert((two << 3) / 8 == two); + assert((three >> 1) == 1); + + // comptime shifts on runtime values + assert(x << 1 == 128); + assert(x >> 2 == 16); +}