From d853070c6b7601eef95750963386ccf431435f10 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Fri, 15 Nov 2024 20:47:26 +0000 Subject: [PATCH] Add some tests with references --- .../hint_black_box/src/main.nr | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/test_programs/execution_success/hint_black_box/src/main.nr b/test_programs/execution_success/hint_black_box/src/main.nr index 635ac2b0b26..d944d4b75fb 100644 --- a/test_programs/execution_success/hint_black_box/src/main.nr +++ b/test_programs/execution_success/hint_black_box/src/main.nr @@ -27,6 +27,32 @@ fn main(a: u32, b: u32) { brillig_slice_sum(black_box(arr.as_slice())) }; assert_eq(s, b); + + let mut d = b; + // This gets completely eliminated: + let mut c = 0; + set_ref(&mut c, &mut d); + assert_eq(c, b); + + // This way the constraint is preserved: + let mut c = 0; + set_ref(&mut c, &mut black_box(d)); + assert_eq(c, b); + + // A reference over the output of black box is not the original variable: + let mut c = 0; + set_ref(&mut black_box(c), &mut d); + assert_eq(c, 0); + + // This would cause a causes a crash during SSA passes unless it's a Brillig runtime: + // > Could not resolve some references to the array. All references must be resolved at compile time + // The SSA cannot have Allocate by the time we start generating ACIR, but `black_box` prevents them + // from being optimised away during SSA passes. + // If we use `--force-brillig` then the it doesn't crash but the assertion fails because `mem2reg` + // eliminates the storing to the reference. + //let mut c = 0; + //set_ref(black_box(&mut c), black_box(&mut d)); + //assert_eq(c, b); } fn loop(n: u32, k: u32) -> u32 { @@ -47,7 +73,7 @@ fn array_sum(xs: [u32; N]) -> u32 { fn slice_sum(xs: [u32]) -> u32 { let mut sum = 0; - for x in xs{ + for x in xs { sum = sum + x; } sum @@ -55,8 +81,12 @@ fn slice_sum(xs: [u32]) -> u32 { unconstrained fn brillig_slice_sum(xs: [u32]) -> u32 { let mut sum = 0; - for x in xs{ + for x in xs { sum = sum + x; } sum -} \ No newline at end of file +} + +fn set_ref(c: &mut u32, b: &mut u32) { + *c = *b; +}