Skip to content

Commit 4dbbadf

Browse files
chrisethgeorgwiese
andauthored
Add sub test. (#2577)
Co-authored-by: Georg Wiese <[email protected]>
1 parent 1b49df5 commit 4dbbadf

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

executor/src/witgen/jit/interpreter.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ mod test {
785785
};
786786

787787
use powdr_ast::analyzed::Analyzed;
788-
use powdr_number::{FieldElement, GoldilocksField, LargeInt};
788+
use powdr_number::{BabyBearField, FieldElement, GoldilocksField, LargeInt};
789789

790790
use bit_vec::BitVec;
791791
use itertools::Itertools;
@@ -1061,4 +1061,36 @@ namespace arith(8);
10611061
&[GoldilocksField::from(7), GoldilocksField::from(0)]
10621062
);
10631063
}
1064+
1065+
#[test]
1066+
fn add_sub_bb() {
1067+
let pil = read_to_string("../test_data/pil/add_sub_bb.pil").unwrap();
1068+
let (analyzed, fixed_col_vals) = read_pil::<BabyBearField>(&pil);
1069+
let fixed_data = FixedData::new(&analyzed, &fixed_col_vals, &[], Default::default(), 0);
1070+
let fixed_data = global_constraints::set_global_constraints(fixed_data);
1071+
1072+
// First try to compute the "gt" flag.
1073+
let interpreter_gt =
1074+
TestInterpreter::new(&analyzed, &fixed_data, "main_add_sub", 4, 1, &|_| {
1075+
Err("Query not implemented".to_string())
1076+
});
1077+
interpreter_gt.test(&[1, 2, 3, 4, 0], &[1, 2, 3, 4, 1]);
1078+
interpreter_gt.test(&[3, 0, 2, 4, 0], &[3, 0, 2, 4, 0]);
1079+
interpreter_gt.test(&[5, 2, 0, 4, 0], &[5, 2, 0, 4, 0]);
1080+
1081+
// Then check that it also works if the result is already provided.
1082+
let interpreter_gt =
1083+
TestInterpreter::new(&analyzed, &fixed_data, "main_add_sub", 5, 0, &|_| {
1084+
Err("Query not implemented".to_string())
1085+
});
1086+
interpreter_gt.test(&[1, 2, 3, 4, 1], &[1, 2, 3, 4, 1]);
1087+
interpreter_gt.test(&[3, 0, 2, 4, 0], &[3, 0, 2, 4, 0]);
1088+
interpreter_gt.test(&[5, 2, 0, 4, 0], &[5, 2, 0, 4, 0]);
1089+
1090+
// This should actually panic, but it does not, because
1091+
// A_h is assigned a value outside of the two-byte range.
1092+
// We do not detect this because lookups that only result
1093+
// in range constraints are removed.
1094+
interpreter_gt.test(&[1, 2, 3, 4, 0], &[1, 2, 3, 4, 0]);
1095+
}
10641096
}

test_data/pil/add_sub_bb.pil

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// A compiled version of "add_sub.asm" for babybear.
2+
namespace main(32..4096);
3+
// Dummy connection constraint
4+
col witness i1, i2, i3, i4, i5;
5+
[i1, i2, i3, i4, i5] is [main_add_sub::C_h, main_add_sub::C_l, main_add_sub::B_h, main_add_sub::B_l, main_add_sub::carry32];
6+
namespace main_add_sub(32..4096);
7+
col witness carry16;
8+
main_add_sub::carry16 * (1 - main_add_sub::carry16) = 0;
9+
col witness carry32;
10+
main_add_sub::carry32 * (1 - main_add_sub::carry32) = 0;
11+
col witness A_h;
12+
col witness A_l;
13+
col witness B_h;
14+
col witness B_l;
15+
col witness C_h;
16+
col witness C_l;
17+
main_add_sub::A_l + main_add_sub::B_l = main_add_sub::C_l + main_add_sub::carry16 * 65536;
18+
main_add_sub::A_h + main_add_sub::B_h + main_add_sub::carry16 = main_add_sub::C_h + main_add_sub::carry32 * 65536;
19+
col witness sel;
20+
main_add_sub::sel * (1 - main_add_sub::sel) = 0;
21+
[main_add_sub::A_l] in [main_byte2::BYTE2];
22+
[main_add_sub::A_h] in [main_byte2::BYTE2];
23+
[main_add_sub::B_l] in [main_byte2::BYTE2];
24+
[main_add_sub::B_h] in [main_byte2::BYTE2];
25+
[main_add_sub::C_l] in [main_byte2::BYTE2];
26+
[main_add_sub::C_h] in [main_byte2::BYTE2];
27+
namespace main_byte2(65536);
28+
col fixed BYTE2(i) { i & 65535_int };

0 commit comments

Comments
 (0)