Skip to content

Commit e82f3e5

Browse files
committed
babybear test for addi/logici
1 parent 155987f commit e82f3e5

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

ceno_zkvm/src/instructions/riscv/arith_imm.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,40 @@ mod test {
2626
structs::ProgramParams,
2727
};
2828
use ceno_emul::{Change, InsnKind, PC_STEP_SIZE, StepRecord, encode_rv32};
29-
use ff_ext::GoldilocksExt2;
29+
use ff_ext::{BabyBearExt4, ExtensionField, GoldilocksExt2};
3030
use gkr_iop::circuit_builder::DebugIndex;
3131

3232
#[test]
33-
fn test_opcode_addi_v1() {
34-
test_opcode_addi(1000, 1003, 3);
35-
test_opcode_addi(1000, 997, -3);
33+
fn test_opcode_addi() {
34+
let cases = vec![
35+
(1000, 1003, 3), // positive immediate
36+
(1000, 997, -3), // negative immediate
37+
];
38+
39+
for &(rs1, expected, imm) in &cases {
40+
test_opcode_addi_internal::<GoldilocksExt2>(rs1, expected, imm);
41+
#[cfg(feature = "u16limb_circuit")]
42+
test_opcode_addi_internal::<BabyBearExt4>(rs1, expected, imm);
43+
}
3644
}
3745

38-
fn test_opcode_addi(rs1: u32, rd: u32, imm: i32) {
39-
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
46+
fn test_opcode_addi_internal<E: ExtensionField>(rs1: u32, rd: u32, imm: i32) {
47+
let mut cs = ConstraintSystem::<E>::new(|| "riscv");
4048
let mut cb = CircuitBuilder::new(&mut cs);
4149
let config = cb
4250
.namespace(
4351
|| "addi",
4452
|cb| {
45-
let config = AddiInstruction::<GoldilocksExt2>::construct_circuit(
46-
cb,
47-
&ProgramParams::default(),
48-
);
53+
let config =
54+
AddiInstruction::<E>::construct_circuit(cb, &ProgramParams::default());
4955
Ok(config)
5056
},
5157
)
5258
.unwrap()
5359
.unwrap();
5460

5561
let insn_code = encode_rv32(InsnKind::ADDI, 2, 0, 4, imm);
56-
let (raw_witin, lkm) = AddiInstruction::<GoldilocksExt2>::assign_instances(
62+
let (raw_witin, lkm) = AddiInstruction::<E>::assign_instances(
5763
&config,
5864
cb.cs.num_witin as usize,
5965
cb.cs.num_structural_witin as usize,

ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<E: ExtensionField> LogicConfig<E> {
124124
#[cfg(test)]
125125
mod test {
126126
use ceno_emul::{Change, InsnKind, PC_STEP_SIZE, StepRecord, encode_rv32u};
127-
use ff_ext::GoldilocksExt2;
127+
use ff_ext::{BabyBearExt4, ExtensionField, GoldilocksExt2};
128128
use gkr_iop::circuit_builder::DebugIndex;
129129

130130
use crate::{
@@ -150,26 +150,55 @@ mod test {
150150

151151
#[test]
152152
fn test_opcode_andi() {
153-
verify::<AndiOp>("basic", 0x0000_0011, 3, 0x0000_0011 & 3);
154-
verify::<AndiOp>("zero result", 0x0000_0100, 3, 0x0000_0100 & 3);
155-
verify::<AndiOp>("negative imm", TEST, NEG, TEST & NEG);
153+
let cases = vec![
154+
("basic", 0x0000_0011, 3, 0x0000_0011 & 3),
155+
("zero result", 0x0000_0100, 3, 0x0000_0100 & 3),
156+
("negative imm", TEST, NEG, TEST & NEG),
157+
];
158+
159+
for &(name, rs1, imm, expected) in &cases {
160+
verify::<AndiOp, GoldilocksExt2>(name, rs1, imm, expected);
161+
#[cfg(feature = "u16limb_circuit")]
162+
verify::<AndiOp, BabyBearExt4>(name, rs1, imm, expected);
163+
}
156164
}
157165

158166
#[test]
159167
fn test_opcode_ori() {
160-
verify::<OriOp>("basic", 0x0000_0011, 3, 0x0000_0011 | 3);
161-
verify::<OriOp>("basic2", 0x0000_0100, 3, 0x0000_0100 | 3);
162-
verify::<OriOp>("negative imm", TEST, NEG, TEST | NEG);
168+
let cases = vec![
169+
("basic", 0x0000_0011, 3, 0x0000_0011 | 3),
170+
("basic2", 0x0000_0100, 3, 0x0000_0100 | 3),
171+
("negative imm", TEST, NEG, TEST | NEG),
172+
];
173+
174+
for &(name, rs1, imm, expected) in &cases {
175+
verify::<OriOp, GoldilocksExt2>(name, rs1, imm, expected);
176+
#[cfg(feature = "u16limb_circuit")]
177+
verify::<OriOp, BabyBearExt4>(name, rs1, imm, expected);
178+
}
163179
}
164180

165181
#[test]
166182
fn test_opcode_xori() {
167-
verify::<XoriOp>("basic", 0x0000_0011, 3, 0x0000_0011 ^ 3);
168-
verify::<XoriOp>("non-overlap", 0x0000_0100, 3, 0x0000_0100 ^ 3);
169-
verify::<XoriOp>("negative imm", TEST, NEG, TEST ^ NEG);
183+
let cases = vec![
184+
("basic", 0x0000_0011, 3, 0x0000_0011 ^ 3),
185+
("non-overlap", 0x0000_0100, 3, 0x0000_0100 ^ 3),
186+
("negative imm", TEST, NEG, TEST ^ NEG),
187+
];
188+
189+
for &(name, rs1, imm, expected) in &cases {
190+
verify::<XoriOp, GoldilocksExt2>(name, rs1, imm, expected);
191+
#[cfg(feature = "u16limb_circuit")]
192+
verify::<XoriOp, BabyBearExt4>(name, rs1, imm, expected);
193+
}
170194
}
171195

172-
fn verify<I: LogicOp>(name: &'static str, rs1_read: u32, imm: u32, expected_rd_written: u32) {
196+
fn verify<I: LogicOp, E: ExtensionField>(
197+
name: &'static str,
198+
rs1_read: u32,
199+
imm: u32,
200+
expected_rd_written: u32,
201+
) {
173202
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
174203
let mut cb = CircuitBuilder::new(&mut cs);
175204

0 commit comments

Comments
 (0)