From 1550278f1e96392967b477b9b12be3bb0eea8fd6 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:32:00 +0100 Subject: [PATCH] feat: implement `to_be_radix` in the comptime interpreter (#6043) # Description ## Problem\* Resolves #6042 ## Summary\* This PR implements `to_be_radix` in the comptime interpreter by reusing our impl for `to_le_radix`. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../src/hir/comptime/interpreter/builtin.rs | 16 ++++++++++++++++ .../comptime_to_radix/Nargo.toml | 6 ++++++ .../comptime_to_radix/src/main.nr | 10 ++++++++++ 3 files changed, 32 insertions(+) create mode 100644 test_programs/compile_success_empty/comptime_to_radix/Nargo.toml create mode 100644 test_programs/compile_success_empty/comptime_to_radix/src/main.nr diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 51837debb24..407cdc216db 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -174,6 +174,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "struct_def_module" => struct_def_module(self, arguments, location), "struct_def_name" => struct_def_name(interner, arguments, location), "struct_def_set_fields" => struct_def_set_fields(interner, arguments, location), + "to_be_radix" => to_be_radix(arguments, return_type, location), "to_le_radix" => to_le_radix(arguments, return_type, location), "trait_constraint_eq" => trait_constraint_eq(arguments, location), "trait_constraint_hash" => trait_constraint_hash(arguments, location), @@ -756,6 +757,21 @@ fn quoted_tokens(arguments: Vec<(Value, Location)>, location: Location) -> IResu )) } +fn to_be_radix( + arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + let le_radix_limbs = to_le_radix(arguments, return_type, location)?; + + let Value::Array(limbs, typ) = le_radix_limbs else { + unreachable!("`to_le_radix` should always return an array"); + }; + let be_radix_limbs = limbs.into_iter().rev().collect(); + + Ok(Value::Array(be_radix_limbs, typ)) +} + fn to_le_radix( arguments: Vec<(Value, Location)>, return_type: Type, diff --git a/test_programs/compile_success_empty/comptime_to_radix/Nargo.toml b/test_programs/compile_success_empty/comptime_to_radix/Nargo.toml new file mode 100644 index 00000000000..35713aff805 --- /dev/null +++ b/test_programs/compile_success_empty/comptime_to_radix/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "comptime_to_radix" +type = "bin" +authors = [""] + +[dependencies] diff --git a/test_programs/compile_success_empty/comptime_to_radix/src/main.nr b/test_programs/compile_success_empty/comptime_to_radix/src/main.nr new file mode 100644 index 00000000000..959a7c12007 --- /dev/null +++ b/test_programs/compile_success_empty/comptime_to_radix/src/main.nr @@ -0,0 +1,10 @@ +fn main() { + comptime + { + let le_bytes: [u8; 3] = 257.to_le_bytes(); + assert_eq(le_bytes, [1, 1, 0]); + + let be_bytes: [u8; 3] = 257.to_be_bytes(); + assert_eq(be_bytes, [0, 1, 1]); + } +}