Skip to content

Commit

Permalink
fix: make toBits accept arrays and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant committed Nov 28, 2023
1 parent 9e11de9 commit 89068f5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
21 changes: 12 additions & 9 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,18 @@ impl<'block> BrilligBlock<'block> {
);
let target_len = target_len_variable.extract_register();

let target_vector = self
.variables
.define_variable(
self.function_context,
self.brillig_context,
results[1],
dfg,
)
.extract_vector();
let target_vector = match self.variables.define_variable(
self.function_context,
self.brillig_context,
results[1],
dfg,
) {
BrilligVariable::BrilligArray(array) => {
self.brillig_context.array_to_vector(&array)
}
BrilligVariable::BrilligVector(vector) => vector,
BrilligVariable::Simple(..) => unreachable!("ICE: ToBits on non-array"),
};

let radix = self.brillig_context.make_constant(2_usize.into());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "brillig_cow"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
original = [0, 1, 2, 3, 4]
index = 2

[expected_result]
original = [0, 1, 2, 3, 4]
modified_once = [0, 1, 27, 3, 4]
modified_twice = [0, 1, 27, 27, 4]
54 changes: 54 additions & 0 deletions tooling/nargo_cli/tests/execution_success/brillig_cow/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Tests the copy on write optimization for arrays. We look for cases where we are modifying an array in place when we shouldn't.

global ARRAY_SIZE = 5;

struct ExecutionResult {
original: [Field; ARRAY_SIZE],
modified_once: [Field; ARRAY_SIZE],
modified_twice: [Field; ARRAY_SIZE],
}

impl ExecutionResult {
fn is_equal(self, other: ExecutionResult) -> bool {
(self.original == other.original) &
(self.modified_once == other.modified_once) &
(self.modified_twice == other.modified_twice)
}
}

fn modify_in_inlined_constrained(original: [Field; ARRAY_SIZE], index: u64) -> ExecutionResult {
let mut modified = original;

modified[index] = 27;

let modified_once = modified;

modified[index+1] = 27;

ExecutionResult {
original,
modified_once,
modified_twice: modified
}
}

unconstrained fn modify_in_unconstrained(original: [Field; ARRAY_SIZE], index: u64) -> ExecutionResult {
let mut modified = original;

modified[index] = 27;

let modified_once = modified;

modified[index+1] = 27;

ExecutionResult {
original,
modified_once,
modified_twice: modified
}
}

unconstrained fn main(original: [Field; ARRAY_SIZE], index: u64, expected_result: ExecutionResult) {
assert(expected_result.is_equal(modify_in_unconstrained(original, index)));
assert(expected_result.is_equal(modify_in_inlined_constrained(original, index)));
}

0 comments on commit 89068f5

Please sign in to comment.