From 924a327f233015813a0989295d42db99c4e5e589 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 9 Oct 2023 16:09:51 +0100 Subject: [PATCH 1/2] fix: change non-constant radix/result_len errors from ICE to proper error --- noir_stdlib/src/field.nr | 49 +++++++++++++++---- .../radix_non_constant_length/Nargo.toml | 7 +++ .../radix_non_constant_length/Prover.toml | 2 + .../radix_non_constant_length/src/main.nr | 4 ++ 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Nargo.toml create mode 100644 tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Prover.toml create mode 100644 tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/src/main.nr diff --git a/noir_stdlib/src/field.nr b/noir_stdlib/src/field.nr index fe887aa89b0..912c34056f3 100644 --- a/noir_stdlib/src/field.nr +++ b/noir_stdlib/src/field.nr @@ -1,23 +1,52 @@ impl Field { + pub fn to_le_bits(self: Self, bit_size: u32) -> [u1] { + crate::assert_constant(bit_size); + self.to_le_bits(bit_size) + } + + pub fn to_be_bits(self: Self, bit_size: u32) -> [u1] { + crate::assert_constant(bit_size); + self.to_be_bits(bit_size) + } + #[builtin(to_le_bits)] - pub fn to_le_bits(_x : Field, _bit_size: u32) -> [u1] {} + fn __to_le_bits(_self: Self, _bit_size: u32) -> [u1] {} + #[builtin(to_be_bits)] - pub fn to_be_bits(_x : Field, _bit_size: u32) -> [u1] {} + fn __to_be_bits(_self: Self, _bit_size: u32) -> [u1] {} + + pub fn to_le_bytes(self: Self, byte_size: u32) -> [u8] { + self.to_le_radix(256, byte_size) + } + + pub fn to_be_bytes(self: Self, byte_size: u32) -> [u8] { + self.to_be_radix(256, byte_size) + } + - pub fn to_le_bytes(x : Field, byte_size: u32) -> [u8] { - x.to_le_radix(256, byte_size) + pub fn to_le_radix(self: Self, radix: u32, result_len: u32) -> [u8] { + crate::assert_constant(radix); + crate::assert_constant(result_len); + self.__to_le_radix(radix, result_len) } - pub fn to_be_bytes(x : Field, byte_size: u32) -> [u8] { - x.to_be_radix(256, byte_size) + + pub fn to_be_radix(self: Self, radix: u32, result_len: u32) -> [u8] { + crate::assert_constant(radix); + crate::assert_constant(result_len); + self.__to_be_radix(radix, result_len) } + + + // decompose `_self` into a `_result_len` vector over the `_radix` basis + // `_radix` must be less than 256 #[builtin(to_le_radix)] - //decompose _x into a _result_len vector over the _radix basis - //_radix must be less than 256 - pub fn to_le_radix(_x : Field, _radix: u32, _result_len: u32) -> [u8] {} + fn __to_le_radix(_self: Self, _radix: u32, _result_len: u32) -> [u8] {} + #[builtin(to_be_radix)] - pub fn to_be_radix(_x : Field, _radix: u32, _result_len: u32) -> [u8] {} + fn __to_be_radix(_self: Self, _radix: u32, _result_len: u32) -> [u8] {} + // Returns self to the power of the given exponent value. // Caution: we assume the exponent fits into 32 bits diff --git a/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Nargo.toml b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Nargo.toml new file mode 100644 index 00000000000..b8b1a2417dc --- /dev/null +++ b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "radix_non_constant_length" +type = "bin" +authors = [""] +compiler_version = "0.10.2" + +[dependencies] diff --git a/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Prover.toml b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Prover.toml new file mode 100644 index 00000000000..f28f2f8cc48 --- /dev/null +++ b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/Prover.toml @@ -0,0 +1,2 @@ +x = "5" +y = "10" diff --git a/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/src/main.nr b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/src/main.nr new file mode 100644 index 00000000000..adfbd265a1d --- /dev/null +++ b/tooling/nargo_cli/tests/compile_failure/radix_non_constant_length/src/main.nr @@ -0,0 +1,4 @@ +fn main(x : Field, y : pub u32) { + let bytes = x.to_be_bytes(y); + assert(bytes[0] == 0); +} From 48e5b30ee50b41a88992f033d8ac557e7a8dc392 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 9 Oct 2023 16:39:10 +0100 Subject: [PATCH 2/2] chore: fix recursion issue --- noir_stdlib/src/field.nr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/noir_stdlib/src/field.nr b/noir_stdlib/src/field.nr index 912c34056f3..3959f1ea175 100644 --- a/noir_stdlib/src/field.nr +++ b/noir_stdlib/src/field.nr @@ -2,12 +2,12 @@ impl Field { pub fn to_le_bits(self: Self, bit_size: u32) -> [u1] { crate::assert_constant(bit_size); - self.to_le_bits(bit_size) + self.__to_le_bits(bit_size) } pub fn to_be_bits(self: Self, bit_size: u32) -> [u1] { crate::assert_constant(bit_size); - self.to_be_bits(bit_size) + self.__to_be_bits(bit_size) } #[builtin(to_le_bits)]