Skip to content

Commit

Permalink
Speed up quick tests (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth authored Jul 25, 2024
1 parent 58d405a commit 6774287
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 136 deletions.
46 changes: 27 additions & 19 deletions linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn process_link(link: Link) -> PilStatement {

#[cfg(test)]
mod test {
use std::fs;
use std::{fs, path::PathBuf};

use powdr_ast::object::PILGraph;
use powdr_number::{FieldElement, GoldilocksField};
Expand All @@ -242,6 +242,18 @@ mod test {

use crate::link;

fn parse_analyze_and_compile_file<T: FieldElement>(file: &str) -> PILGraph {
let contents = fs::read_to_string(file).unwrap();
let parsed = parse_asm(Some(file), &contents).unwrap_or_else(|e| {
e.output_to_stderr();
panic!();
});
let resolved =
powdr_importer::load_dependencies_and_resolve(Some(PathBuf::from(file)), parsed)
.unwrap();
powdr_airgen::compile(convert_asm_to_pil::<T>(resolved).unwrap())
}

fn parse_analyze_and_compile<T: FieldElement>(input: &str) -> PILGraph {
let parsed = parse_asm(None, input).unwrap_or_else(|e| {
e.output_to_stderr();
Expand Down Expand Up @@ -287,8 +299,7 @@ namespace main__rom(4 + 4);
"{}/../test_data/asm/empty_vm.asm",
env!("CARGO_MANIFEST_DIR")
);
let contents = fs::read_to_string(file_name).unwrap();
let graph = parse_analyze_and_compile::<GoldilocksField>(&contents);
let graph = parse_analyze_and_compile_file::<GoldilocksField>(&file_name);
let pil = link(graph).unwrap();
assert_eq!(extract_main(&format!("{pil}")), expectation);
}
Expand Down Expand Up @@ -416,8 +427,7 @@ namespace main_sub__rom(16);
"{}/../test_data/asm/different_signatures.asm",
env!("CARGO_MANIFEST_DIR")
);
let contents = fs::read_to_string(file_name).unwrap();
let graph = parse_analyze_and_compile::<GoldilocksField>(&contents);
let graph = parse_analyze_and_compile_file::<GoldilocksField>(&file_name);
let pil = link(graph).unwrap();
assert_eq!(extract_main(&format!("{pil}")), expectation);
}
Expand Down Expand Up @@ -500,8 +510,7 @@ namespace main__rom(1024);
"{}/../test_data/asm/simple_sum.asm",
env!("CARGO_MANIFEST_DIR")
);
let contents = fs::read_to_string(file_name).unwrap();
let graph = parse_analyze_and_compile::<GoldilocksField>(&contents);
let graph = parse_analyze_and_compile_file::<GoldilocksField>(&file_name);
let pil = link(graph).unwrap();
assert_eq!(extract_main(&format!("{pil}")), expectation);
}
Expand Down Expand Up @@ -671,7 +680,7 @@ namespace main_vm(1024);

#[test]
fn permutation_instructions() {
let expected = r#"namespace main(65536);
let expected = r#"namespace main(256);
pol commit _operation_id(i) query std::prover::Query::Hint(13);
pol constant _block_enforcer_last_step = [0]* + [1];
let _operation_id_no_change = (1 - _block_enforcer_last_step) * (1 - instr_return);
Expand Down Expand Up @@ -728,7 +737,7 @@ namespace main_vm(1024);
instr_or $ [0, X, Y, Z] is main_bin.latch * main_bin.sel[1] $ [main_bin.operation_id, main_bin.A, main_bin.B, main_bin.C];
pol constant _linker_first_step = [1] + [0]*;
_linker_first_step * (_operation_id - 2) = 0;
namespace main__rom(65536);
namespace main__rom(256);
pol constant p_line = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] + [13]*;
pol constant p_X_const = [0, 0, 2, 0, 1, 0, 3, 0, 2, 0, 1, 0, 0, 0] + [0]*;
pol constant p_X_read_free = [0]*;
Expand Down Expand Up @@ -760,15 +769,16 @@ namespace main__rom(65536);
pol constant p_reg_write_Z_B = [0]*;
pol constant operation_id = [0]*;
pol constant latch = [1]*;
namespace main_bin(65536);
namespace main_bin(256);
pol commit operation_id;
pol constant latch(i) { if i % 4 == 3 { 1 } else { 0 } };
pol constant FACTOR(i) { 1 << (i + 1) % 4 * 8 };
let a = (|i| i % 256);
pol constant latch(i) { if i % 8 == 7 { 1 } else { 0 } };
let sum_sel = std::array::sum(sel);
pol constant FACTOR(i) { 1 << (i + 1) % 8 * 4 };
let a = (|i| i % 16);
pol constant P_A(i) { a(i) };
let b = (|i| (i >> 8) % 256);
let b = (|i| (i >> 4) % 16);
pol constant P_B(i) { b(i) };
pol constant P_C(i) { (a(i) | b(i)) & 255 };
pol constant P_C(i) { (a(i) | b(i)) & 15 };
pol commit A_byte;
pol commit B_byte;
pol commit C_byte;
Expand All @@ -786,8 +796,7 @@ namespace main_bin(65536);
"{}/../test_data/asm/permutations/vm_to_block.asm",
env!("CARGO_MANIFEST_DIR")
);
let contents = fs::read_to_string(file_name).unwrap();
let graph = parse_analyze_and_compile::<GoldilocksField>(&contents);
let graph = parse_analyze_and_compile_file::<GoldilocksField>(&file_name);
let pil = link(graph).unwrap();
assert_eq!(extract_main(&format!("{pil}")), expected);
}
Expand Down Expand Up @@ -946,8 +955,7 @@ namespace main_submachine(1024);
"{}/../test_data/asm/permutations/link_merging.asm",
env!("CARGO_MANIFEST_DIR")
);
let contents = fs::read_to_string(file_name).unwrap();
let graph = parse_analyze_and_compile::<GoldilocksField>(&contents);
let graph = parse_analyze_and_compile_file::<GoldilocksField>(&file_name);
let pil = link(graph).unwrap();
assert_eq!(extract_main(&format!("{pil}")), expected);
}
Expand Down
37 changes: 37 additions & 0 deletions test_data/asm/permutations/binary4.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// Binary machine that works on chunks of 4 bits,
/// to be used with permutation lookups.
machine Binary4 with
latch: latch,
operation_id: operation_id,
// Allow this machine to be connected via a permutation
call_selectors: sel,
{
operation or<0> A, B -> C;
col witness operation_id;
col fixed latch(i) { if (i % 8) == 7 { 1 } else { 0 } };

// check that we can reference the call_selectors
let sum_sel = std::array::sum(sel);

col fixed FACTOR(i) { 1 << (((i + 1) % 8) * 4) };

let a = |i| i % 16;
col fixed P_A(i) { a(i) };
let b = |i| (i >> 4) % 16;
col fixed P_B(i) { b(i) };
col fixed P_C(i) { (a(i) | b(i)) & 0xf };

col witness A_byte;
col witness B_byte;
col witness C_byte;

col witness A;
col witness B;
col witness C;

A' = A * (1 - latch) + A_byte * FACTOR;
B' = B * (1 - latch) + B_byte * FACTOR;
C' = C * (1 - latch) + C_byte * FACTOR;

[A_byte, B_byte, C_byte] in [P_A, P_B, P_C];
}
44 changes: 7 additions & 37 deletions test_data/asm/permutations/block_to_block.asm
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
machine Binary with
latch: latch,
operation_id: operation_id,
call_selectors: sel,
{
col fixed FACTOR(i) { 1 << (((i + 1) % 4) * 8) };

operation or<0> A, B -> C;

col witness operation_id;
col fixed latch(i) { if (i % 4) == 3 { 1 } else { 0 } };

let a = |i| i % 256;
col fixed P_A(i) { a(i) };
let b = |i| (i >> 8) % 256;
col fixed P_B(i) { b(i) };
col fixed P_C(i) { (a(i) | b(i)) & 0xff };

col witness A_byte;
col witness B_byte;
col witness C_byte;

col witness A;
col witness B;
col witness C;

A' = A * (1 - latch) + A_byte * FACTOR;
B' = B * (1 - latch) + B_byte * FACTOR;
C' = C * (1 - latch) + C_byte * FACTOR;

[A_byte, B_byte, C_byte] in [P_A, P_B, P_C];
}
mod binary4;
use binary4::Binary4;

machine Binary4 with
machine Binary4x with
latch: latch,
operation_id: operation_id,
call_selectors: sel,
{
Binary bin;
Binary4 bin;

operation or4<0> A, B, C, D -> E;

Expand All @@ -60,7 +30,7 @@ machine Binary4 with
col witness Y;
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
Expand All @@ -70,8 +40,8 @@ machine Main with degree: 65536 {
reg A;
reg B;

Binary bin;
Binary4 bin4;
Binary4 bin;
Binary4x bin4;

// two permutations to machine bin
instr or X, Y -> Z link ~> Z = bin.or(X, Y);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
machine Binary with
machine Binary4 with
latch: latch,
operation_id: operation_id,
// Allow this machine to be connected via a permutation
call_selectors: sel,
{
operation or<0> A, B -> C;
col witness operation_id;
col fixed latch(i) { if (i % 4) == 3 { 1 } else { 0 } };
col fixed latch(i) { if (i % 8) == 7 { 1 } else { 0 } };

// check that we can reference the call_selectors
let sum_sel = std::array::sum(sel);

col fixed FACTOR(i) { 1 << (((i + 1) % 4) * 8) };
col fixed FACTOR(i) { 1 << (((i + 1) % 8) * 4) };

let a = |i| i % 256;
let a = |i| i % 16;
col fixed P_A(i) { a(i) };
let b = |i| (i >> 8) % 256;
let b = |i| (i >> 4) % 16;
col fixed P_B(i) { b(i) };
col fixed P_C(i) { (a(i) | b(i)) & 0xff };
col fixed P_C(i) { (a(i) | b(i)) & 0xf };

col witness A_byte;
col witness B_byte;
Expand All @@ -34,14 +34,14 @@ machine Binary with
[A_byte, B_byte, C_byte] in [P_A, P_B, P_C];
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
reg Z[<=];
reg A;

Binary bin;
Binary4 bin;

// lookup to machine bin
instr or X, Y -> Z link => Z = bin.or(X, Y);
Expand Down
2 changes: 1 addition & 1 deletion test_data/asm/permutations/incoming_needs_selector.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ machine Binary with
C = A + B;
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
Expand Down
40 changes: 4 additions & 36 deletions test_data/asm/permutations/simple.asm
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
machine Binary with
latch: latch,
operation_id: operation_id,
call_selectors: sel,
{
operation or<0> A, B -> C;
col witness operation_id;
col fixed latch(i) { if (i % 4) == 3 { 1 } else { 0 } };
mod binary4;
use binary4::Binary4;

// check that we can reference the call_selectors
let sum_sel = std::array::sum(sel);

col fixed FACTOR(i) { 1 << (((i + 1) % 4) * 8) };

let a = |i| i % 256;
col fixed P_A(i) { a(i) };
let b = |i| (i >> 8) % 256;
col fixed P_B(i) { b(i) };
col fixed P_C(i) { (a(i) | b(i)) & 0xff };

col witness A_byte;
col witness B_byte;
col witness C_byte;

col witness A;
col witness B;
col witness C;

A' = A * (1 - latch) + A_byte * FACTOR;
B' = B * (1 - latch) + B_byte * FACTOR;
C' = C * (1 - latch) + C_byte * FACTOR;

[A_byte, B_byte, C_byte] in [P_A, P_B, P_C];
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
reg Z[<=];
reg A;

Binary bin;
Binary4 bin;

// permutation to machine bin
instr or X, Y -> Z link ~> Z = bin.or(X, Y);
Expand Down
38 changes: 4 additions & 34 deletions test_data/asm/permutations/vm_to_block.asm
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
machine Binary with
latch: latch,
operation_id: operation_id,
call_selectors: sel,
{
operation or<0> A, B -> C;
mod binary4;
use binary4::Binary4;

col witness operation_id;
col fixed latch(i) { if (i % 4) == 3 { 1 } else { 0 } };

col fixed FACTOR(i) { 1 << (((i + 1) % 4) * 8) };

let a = |i| i % 256;
col fixed P_A(i) { a(i) };
let b = |i| (i >> 8) % 256;
col fixed P_B(i) { b(i) };
col fixed P_C(i) { (a(i) | b(i)) & 0xff };

col witness A_byte;
col witness B_byte;
col witness C_byte;

col witness A;
col witness B;
col witness C;

A' = A * (1 - latch) + A_byte * FACTOR;
B' = B * (1 - latch) + B_byte * FACTOR;
C' = C * (1 - latch) + C_byte * FACTOR;

[A_byte, B_byte, C_byte] in [P_A, P_B, P_C];
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
reg Z[<=];
reg A;
reg B;

Binary bin;
Binary4 bin;

// two permutations to machine bin
instr or X, Y -> Z link ~> Z = bin.or(X, Y);
Expand Down
2 changes: 1 addition & 1 deletion test_data/asm/permutations/vm_to_vm.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ machine Binary {
}
}

machine Main with degree: 65536 {
machine Main with degree: 256 {
reg pc[@pc];
reg X[<=];
reg Y[<=];
Expand Down

0 comments on commit 6774287

Please sign in to comment.