Skip to content

Commit

Permalink
Merge pull request #2821 from o1-labs/o1vm/riscv32im/test-decoding-sll
Browse files Browse the repository at this point in the history
o1vm/riscv32im: test decoding sll
  • Loading branch information
dannywillems authored Dec 4, 2024
2 parents abc1bbb + e0b822f commit 388aaf0
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions o1vm/src/interpreters/riscv32im/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ use mina_curves::pasta::Fp;
use rand::{CryptoRng, Rng, RngCore};
use strum::EnumCount;

// Sanity check that we have as many selector as we have instructions
#[test]
fn test_regression_selectors_for_instructions() {
let mips_con_env = constraints::Env::<Fp>::default();
let constraints = mips_con_env.get_selector_constraints();
assert_eq!(
// We substract 1 as we have one boolean check per sel
// and 1 constraint to check that one and only one
// sel is activated
constraints.len() - 1,
// This should match the list in
// crate::interpreters::riscv32im::interpreter::Instruction
RInstruction::COUNT
+ IInstruction::COUNT
+ SInstruction::COUNT
+ SBInstruction::COUNT
+ UInstruction::COUNT
+ UJInstruction::COUNT
+ SyscallInstruction::COUNT
+ MInstruction::COUNT
);
// All instructions are degree 1 or 2.
constraints
.iter()
.for_each(|c| assert!(c.degree(1, 0) == 2 || c.degree(1, 0) == 1));
}

pub fn dummy_env() -> Env<Fp> {
Env {
instruction_counter: 0,
Expand Down Expand Up @@ -73,6 +100,29 @@ pub fn generate_random_sub_instruction<RNG: RngCore + CryptoRng>(rng: &mut RNG)
]
}

pub fn generate_random_sll_instruction<RNG: RngCore + CryptoRng>(rng: &mut RNG) -> [u8; 4] {
let opcode = 0b0110011;
let rd = rng.gen_range(0..32);
let funct3 = 0b001;
let rs1 = rng.gen_range(0..32);
let rs2 = rng.gen_range(0..32);
let funct2 = 0b00;
let funct5 = 0b00000;
let instruction = opcode
| (rd << 7)
| (funct3 << 12)
| (rs1 << 15)
| (rs2 << 20)
| (funct2 << 25)
| (funct5 << 27);
[
instruction as u8,
(instruction >> 8) as u8,
(instruction >> 16) as u8,
(instruction >> 24) as u8,
]
}

#[test]
pub fn test_instruction_decoding_add() {
let mut env: Env<Fp> = dummy_env();
Expand All @@ -99,29 +149,15 @@ pub fn test_instruction_decoding_sub() {
assert_eq!(opcode, Instruction::RType(RInstruction::Sub));
}

// Sanity check that we have as many selector as we have instructions
#[test]
fn test_regression_selectors_for_instructions() {
let mips_con_env = constraints::Env::<Fp>::default();
let constraints = mips_con_env.get_selector_constraints();
assert_eq!(
// We substract 1 as we have one boolean check per sel
// and 1 constraint to check that one and only one
// sel is activated
constraints.len() - 1,
// This should match the list in
// crate::interpreters::riscv32im::interpreter::Instruction
RInstruction::COUNT
+ IInstruction::COUNT
+ SInstruction::COUNT
+ SBInstruction::COUNT
+ UInstruction::COUNT
+ UJInstruction::COUNT
+ SyscallInstruction::COUNT
+ MInstruction::COUNT
);
// All instructions are degree 1 or 2.
constraints
.iter()
.for_each(|c| assert!(c.degree(1, 0) == 2 || c.degree(1, 0) == 1));
pub fn test_instruction_decoding_sll() {
let mut env: Env<Fp> = dummy_env();
let mut rng = o1_utils::tests::make_test_rng(None);
let instruction = generate_random_sll_instruction(&mut rng);
env.memory[0].1[0] = instruction[0];
env.memory[0].1[1] = instruction[1];
env.memory[0].1[2] = instruction[2];
env.memory[0].1[3] = instruction[3];
let (opcode, _instruction) = env.decode_instruction();
assert_eq!(opcode, Instruction::RType(RInstruction::ShiftLeftLogical));
}

0 comments on commit 388aaf0

Please sign in to comment.