Skip to content

Commit

Permalink
Merge pull request #2894 from o1-labs/dw/test-addi-negative-values
Browse files Browse the repository at this point in the history
o1vm/riscv32im: add tests for addi with negative values
  • Loading branch information
dannywillems authored Dec 23, 2024
2 parents 9ccd1c9 + 911df44 commit 598d2bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Binary file added o1vm/resources/programs/riscv32im/bin/addi_negative
Binary file not shown.
22 changes: 22 additions & 0 deletions o1vm/resources/programs/riscv32im/src/addi_negative.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.section .text
.globl _start

_start:
li t0, 100 # t0 = 100

# Subtract 50 using addi (addi with negative immediate)
addi t1, t0, -50 # t1 = t0 + (-50) (Expected: t1 = 50)

# Subtract 100 using addi (addi with negative immediate)
addi t2, t1, -100 # t2 = t1 + (-100) (Expected: t2 = -50)

# Custom exit syscall
li a0, 0
li a1, 0
li a2, 0
li a3, 0
li a4, 0
li a5, 0
li a6, 0
li a7, 42
ecall
18 changes: 18 additions & 0 deletions o1vm/tests/test_riscv_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,21 @@ fn test_add_overflow() {
assert_eq!(witness.registers[T3], 0b11111111111111111111111111111111);
assert_eq!(witness.registers[T4], 0b00000000000000000000000000000000);
}

#[test]
fn test_addi_negative() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/addi_negative",
));
let state = o1vm::elf_loader::parse_riscv32(&path).unwrap();
let mut witness = Env::<Fp>::create(PAGE_SIZE.try_into().unwrap(), state);

while !witness.halt {
witness.step();
}

assert_eq!(witness.registers[T0], 100);
assert_eq!(witness.registers[T1], 50);
assert_eq!(witness.registers[T2], (-50_i32) as u32);
}

0 comments on commit 598d2bc

Please sign in to comment.