From 8d894100ce991d1964036d8eab9fb1f666d2c82b Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 31 May 2023 15:33:08 +0200 Subject: [PATCH] Test for macros in assembly. --- compiler/tests/pil.rs | 14 +++++++++ test_data/pil/simple_sum_asm_macro.pil | 42 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test_data/pil/simple_sum_asm_macro.pil diff --git a/compiler/tests/pil.rs b/compiler/tests/pil.rs index 3a1039e4d..7cdbf6829 100644 --- a/compiler/tests/pil.rs +++ b/compiler/tests/pil.rs @@ -84,3 +84,17 @@ fn test_simple_sum_asm_pil() { }), ) } + +#[test] +fn test_simple_sum_asm_macro_pil() { + verify_pil( + "simple_sum_asm_macro.pil", + Some(|q| match q { + "\"input\", 0" => Some(13.into()), + "\"input\", 1" => Some(2.into()), + "\"input\", 2" => Some(11.into()), + "\"input\", 3" => Some(2.into()), + _ => Some(7.into()), + }), + ) +} diff --git a/test_data/pil/simple_sum_asm_macro.pil b/test_data/pil/simple_sum_asm_macro.pil new file mode 100644 index 000000000..43454380e --- /dev/null +++ b/test_data/pil/simple_sum_asm_macro.pil @@ -0,0 +1,42 @@ +namespace Main(2**10); + col witness XInv; + col witness XIsZero; + XIsZero * (1 - XIsZero) = 0; + + macro if_then_else(condition, true_value, false_value) { condition * true_value + (1 - condition) * false_value }; + macro jump_to(target) { pc' = target; }; + macro jump_to_if(condition, target) { jump_to(if_then_else(condition, target, pc + 1)); }; + +assembly { + reg pc[@pc]; + reg X[<=]; + reg A; + reg CNT; + + pil { + // Just to test if pil-inside-assembly-inside-pil works. + XIsZero = 1 - X * XInv; + XIsZero * X = 0; + } + + instr jmpz X, l: label { jump_to_if(XIsZero, l) } + instr jmp l: label { jump_to(l) } + instr dec_CNT { CNT' = CNT - 1 } + instr assert_zero X { XIsZero = 1 } + + CNT <=X= ${ ("input", 1) }; + + start:: + jmpz CNT, check; + A <=X= A + ${ ("input", CNT + 1) }; + // Could use "CNT <=X= CNT - 1", but that would need X. + dec_CNT; + jmp start; + + check:: + A <=X= A - ${ ("input", 0) }; + assert_zero A; + + end:: + jmp end; +}; \ No newline at end of file