diff --git a/crates/nargo_cli/tests/test_data/to_bits/Nargo.toml b/crates/nargo_cli/tests/test_data/to_bits/Nargo.toml new file mode 100644 index 00000000000..5a02ffe4729 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/to_bits/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.7.0" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/to_bits/src/main.nr b/crates/nargo_cli/tests/test_data/to_bits/src/main.nr new file mode 100644 index 00000000000..feeb5089d13 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/to_bits/src/main.nr @@ -0,0 +1,24 @@ +use dep::std; + +fn main() { + let field = 1000; + let be_bits = field.to_be_bits(16); + let le_bits = field.to_le_bits(16); + + for i in 0..16 { + let x = be_bits[i]; + let y = le_bits[15-i]; + assert(x == y); + } + + let x = 3; + let be_bits_x = x.to_be_bits(4); + let le_bits_x = x.to_le_bits(4); + + for i in 0..4 { + let be_bit = be_bits_x[i]; + let le_bit = le_bits_x[3-i]; + assert(be_bit == le_bit); + } + +} \ No newline at end of file diff --git a/crates/noirc_evaluator/src/ssa/optimizations.rs b/crates/noirc_evaluator/src/ssa/optimizations.rs index d238ae7b0fe..0ca56211455 100644 --- a/crates/noirc_evaluator/src/ssa/optimizations.rs +++ b/crates/noirc_evaluator/src/ssa/optimizations.rs @@ -79,11 +79,15 @@ fn evaluate_intrinsic( block_id: BlockId, ) -> Result, RuntimeErrorKind> { match op { - builtin::Opcode::ToBits(_) => { + builtin::Opcode::ToBits(endian) => { let bit_count = args[1].to_u128() as u32; let mut result = Vec::new(); let mut bits = args[0].bits(); bits.reverse(); + bits.resize(bit_count as usize, false); + if endian == builtin::Endian::Big { + bits.reverse(); + } if let ObjectType::ArrayPointer(a) = res_type { for i in 0..bit_count {