From 2e72e2ee88650757b09490c561288b4a2cd8a752 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 2 May 2023 10:04:47 +0100 Subject: [PATCH 1/4] chore: add test for bitshift operators --- .../tests/test_data/bit_shifts/Nargo.toml | 5 ++++ .../tests/test_data/bit_shifts/Prover.toml | 2 ++ .../tests/test_data/bit_shifts/src/main.nr | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr diff --git a/crates/nargo_cli/tests/test_data/bit_shifts/Nargo.toml b/crates/nargo_cli/tests/test_data/bit_shifts/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts/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/bit_shifts/Prover.toml b/crates/nargo_cli/tests/test_data/bit_shifts/Prover.toml new file mode 100644 index 00000000000..67bf6a6a234 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts/Prover.toml @@ -0,0 +1,2 @@ +x = 64 +y = 1 diff --git a/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr new file mode 100644 index 00000000000..385bef2c5b0 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr @@ -0,0 +1,26 @@ +fn main(x: u64, y: u64) { + let two: u64 = 2; + let three: u64 = 3; + + // comptime shifts on comptime values + constrain two << 2 == 8; + constrain (two << 3) / 8 == two; + constrain (three >> 1) == 1; + + // comptime shifts on runtime values + constrain x << 1 == 128; + constrain x >> 2 == 16; + + // runtime shifts on comptime values + // These are currently unimplemented and panic with "ICE: ShiftLeft and ShiftRight are replaced by multiplications and divisions in optimization pass." + // See: https://github.com/noir-lang/noir/issues/1265 + // constrain 64 << y == 128; + // constrain 64 >> y == 32; + + // runtime shifts on runtime values + // These are currently unimplemented and panic with "ICE: ShiftLeft and ShiftRight are replaced by multiplications and divisions in optimization pass." + // See: https://github.com/noir-lang/noir/issues/1265 + // constrain x << y == 128; + // constrain x >> y == 32; + constrain y == 1; +} From 01be5d58c70c2161c7615e352611d376300afe13 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 2 May 2023 11:07:00 +0100 Subject: [PATCH 2/4] chore: update error message to flag up that runtime shifts are not implemented yet --- crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr | 4 ++-- crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr index 385bef2c5b0..ee9d81bf286 100644 --- a/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr +++ b/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr @@ -12,13 +12,13 @@ fn main(x: u64, y: u64) { constrain x >> 2 == 16; // runtime shifts on comptime values - // These are currently unimplemented and panic with "ICE: ShiftLeft and ShiftRight are replaced by multiplications and divisions in optimization pass." + // These are currently unimplemented and panic with "ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented." // See: https://github.com/noir-lang/noir/issues/1265 // constrain 64 << y == 128; // constrain 64 >> y == 32; // runtime shifts on runtime values - // These are currently unimplemented and panic with "ICE: ShiftLeft and ShiftRight are replaced by multiplications and divisions in optimization pass." + // These are currently unimplemented and panic with "ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented." // See: https://github.com/noir-lang/noir/issues/1265 // constrain x << y == 128; // constrain x >> y == 32; diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs b/crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs index 87280eb1fde..166a55b0d52 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs @@ -238,7 +238,7 @@ pub(crate) fn evaluate( }; InternalVar::from(bitwise_result) } - BinaryOp::Shl | BinaryOp::Shr(_) => unreachable!("ICE: ShiftLeft and ShiftRight are replaced by multiplications and divisions in optimization pass."), + BinaryOp::Shl | BinaryOp::Shr(_) => todo!("ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented."), i @ BinaryOp::Assign => unreachable!("Invalid Instruction: {:?}", i), }; Some(binary_output) From f5e5dd1ff311b04864b888b50e63dbaa7d3b8fa5 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 2 May 2023 15:40:03 +0100 Subject: [PATCH 3/4] chore: split runtime and comptime bitshift tests --- .../tests/test_data/bit_shifts/src/main.nr | 26 ------------------- .../Nargo.toml | 0 .../test_data/bit_shifts_comptime/Prover.toml | 1 + .../test_data/bit_shifts_comptime/src/main.nr | 13 ++++++++++ .../test_data/bit_shifts_runtime/Nargo.toml | 5 ++++ .../Prover.toml | 0 .../test_data/bit_shifts_runtime/src/main.nr | 12 +++++++++ crates/nargo_cli/tests/test_data/config.toml | 2 +- 8 files changed, 32 insertions(+), 27 deletions(-) delete mode 100644 crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr rename crates/nargo_cli/tests/test_data/{bit_shifts => bit_shifts_comptime}/Nargo.toml (100%) create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_comptime/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml rename crates/nargo_cli/tests/test_data/{bit_shifts => bit_shifts_runtime}/Prover.toml (100%) create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr diff --git a/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr deleted file mode 100644 index ee9d81bf286..00000000000 --- a/crates/nargo_cli/tests/test_data/bit_shifts/src/main.nr +++ /dev/null @@ -1,26 +0,0 @@ -fn main(x: u64, y: u64) { - let two: u64 = 2; - let three: u64 = 3; - - // comptime shifts on comptime values - constrain two << 2 == 8; - constrain (two << 3) / 8 == two; - constrain (three >> 1) == 1; - - // comptime shifts on runtime values - constrain x << 1 == 128; - constrain x >> 2 == 16; - - // runtime shifts on comptime values - // These are currently unimplemented and panic with "ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented." - // See: https://github.com/noir-lang/noir/issues/1265 - // constrain 64 << y == 128; - // constrain 64 >> y == 32; - - // runtime shifts on runtime values - // These are currently unimplemented and panic with "ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented." - // See: https://github.com/noir-lang/noir/issues/1265 - // constrain x << y == 128; - // constrain x >> y == 32; - constrain y == 1; -} diff --git a/crates/nargo_cli/tests/test_data/bit_shifts/Nargo.toml b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/Nargo.toml similarity index 100% rename from crates/nargo_cli/tests/test_data/bit_shifts/Nargo.toml rename to crates/nargo_cli/tests/test_data/bit_shifts_comptime/Nargo.toml diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_comptime/Prover.toml b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/Prover.toml new file mode 100644 index 00000000000..cfd62c406cb --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/Prover.toml @@ -0,0 +1 @@ +x = 64 diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr new file mode 100644 index 00000000000..cd137c41d1f --- /dev/null +++ b/crates/nargo_cli/tests/test_data/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 + constrain two << 2 == 8; + constrain (two << 3) / 8 == two; + constrain (three >> 1) == 1; + + // comptime shifts on runtime values + constrain x << 1 == 128; + constrain x >> 2 == 16; +} diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/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/bit_shifts/Prover.toml b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml similarity index 100% rename from crates/nargo_cli/tests/test_data/bit_shifts/Prover.toml rename to crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr new file mode 100644 index 00000000000..0e21ddf3c4c --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr @@ -0,0 +1,12 @@ +fn main(x: u64, y: u64) { + // These are currently unimplemented and panic with "ShiftLeft and ShiftRight operations with shifts which are only known at runtime are not yet implemented." + // See: https://github.com/noir-lang/noir/issues/1265 + + // runtime shifts on comptime values + constrain 64 << y == 128; + constrain 64 >> y == 32; + + // runtime shifts on runtime values + constrain x << y == 128; + constrain x >> y == 32; +} diff --git a/crates/nargo_cli/tests/test_data/config.toml b/crates/nargo_cli/tests/test_data/config.toml index 1c7536af5a2..80822d22375 100644 --- a/crates/nargo_cli/tests/test_data/config.toml +++ b/crates/nargo_cli/tests/test_data/config.toml @@ -2,7 +2,7 @@ # "1_mul", "2_div","3_add","4_sub","5_over", "6","6_array", "7_function","7","8_integration", "9_conditional", "10_slices", "assign_ex", "bool_not", "bool_or", "pedersen_check", "poseidonperm_x5_254", "poseidonsponge_x5_254", "pred_eq", "schnorr", "sha256", "tuples", # "array_len", "array_neq", "bit_and", "cast_bool", "comptime_array_access", "generics", "global_comptime", "main_bool_arg", "main_return", "merkle_insert", "modules", "modules_more", "scalar_mul", "simple_shield", "struct", "submodules", # Exclude "poseidonsponge_x5_254" and "sha2_byte" due to relatively long computation time and "sha2_blocks" due to very long computation time. -exclude = ["comptime_fail", "poseidonsponge_x5_254", "sha2_blocks", "sha2_byte"] +exclude = ["bit_shifts_runtime", "comptime_fail", "poseidonsponge_x5_254", "sha2_blocks", "sha2_byte"] # List of tests (as their directory name in test_data) expecting to fail: if the test pass, we report an error. From c96aa042b40ed1b4b12d835fa2c816e91b57318e Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 2 May 2023 15:59:11 +0100 Subject: [PATCH 4/4] chore: replace `constrain` with `assert()` --- .../tests/test_data/bit_shifts_comptime/src/main.nr | 10 +++++----- .../tests/test_data/bit_shifts_runtime/src/main.nr | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr index cd137c41d1f..c1c6890febb 100644 --- a/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr +++ b/crates/nargo_cli/tests/test_data/bit_shifts_comptime/src/main.nr @@ -3,11 +3,11 @@ fn main(x: u64) { let three: u64 = 3; // comptime shifts on comptime values - constrain two << 2 == 8; - constrain (two << 3) / 8 == two; - constrain (three >> 1) == 1; + assert(two << 2 == 8); + assert((two << 3) / 8 == two); + assert((three >> 1) == 1); // comptime shifts on runtime values - constrain x << 1 == 128; - constrain x >> 2 == 16; + assert(x << 1 == 128); + assert(x >> 2 == 16); } diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr index 0e21ddf3c4c..903a5f35463 100644 --- a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr @@ -3,10 +3,10 @@ fn main(x: u64, y: u64) { // See: https://github.com/noir-lang/noir/issues/1265 // runtime shifts on comptime values - constrain 64 << y == 128; - constrain 64 >> y == 32; + assert(64 << y == 128); + assert(64 >> y == 32); // runtime shifts on runtime values - constrain x << y == 128; - constrain x >> y == 32; + assert(x << y == 128); + assert(x >> y == 32); }