Skip to content

Commit

Permalink
Merge pull request #2896 from o1-labs/dw/test-addi-overflow
Browse files Browse the repository at this point in the history
o1vm/riscv32im: test for addi with overflow
  • Loading branch information
dannywillems authored Dec 24, 2024
2 parents 00327cc + f89c47e commit 90dcb5e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Binary file added o1vm/resources/programs/riscv32im/bin/addi_overflow
Binary file not shown.
23 changes: 23 additions & 0 deletions o1vm/resources/programs/riscv32im/src/addi_overflow.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.section .text
.globl _start

_start:
li t0, 2147483647 # t0 = 2147483647 (Max 32-bit signed int)
addi t1, t0, 1 # t1 = t0 + 1 (Expected: overflow to -2147483648)

li t2, -2147483648 # t2 = -2147483648 (Min 32-bit signed int)
addi t3, t2, -1 # t3 = t2 + (-1) (Expected: overflow to 2147483647)

li t4, 123456789 # t4 = 123456789
addi t5, t4, 0 # t5 = t4 + 0 (Expected: t4 = 123456789)

# 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 @@ -214,3 +214,21 @@ fn test_add_sub_swap() {
assert_eq!(witness.registers[T1], 5);
assert_eq!(witness.registers[T2], 15);
}

#[test]
fn test_addi_overflow() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/addi_overflow",
));
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[T1], (-2147483648_i32) as u32);
assert_eq!(witness.registers[T3], 2147483647);
assert_eq!(witness.registers[T5], 123456789);
}

0 comments on commit 90dcb5e

Please sign in to comment.