Skip to content

Commit

Permalink
Merge pull request #2904 from o1-labs/dw/rem-by-zero
Browse files Browse the repository at this point in the history
o1vm/riscv32im: rem and remu by zero
  • Loading branch information
dannywillems authored Dec 24, 2024
2 parents 639b0cb + 603969c commit 12a1b74
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
Binary file added o1vm/resources/programs/riscv32im/bin/rem_by_zero
Binary file not shown.
Binary file added o1vm/resources/programs/riscv32im/bin/remu_by_zero
Binary file not shown.
19 changes: 19 additions & 0 deletions o1vm/resources/programs/riscv32im/src/rem_by_zero.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section .text
.globl _start

_start:
# Signed remainder by zero (rem)
li t0, 42 # t0 = 42
li t1, 0 # t1 = 0
rem t2, t0, t1 # t2 = t0 % t1 (Expected: remainder by zero)

# 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
19 changes: 19 additions & 0 deletions o1vm/resources/programs/riscv32im/src/remu_by_zero.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section .text
.globl _start

_start:
# Unsigned remainder by zero (remu)
li t0, 42 # t0 = 42
li t1, 0 # t1 = 0
remu t2, t0, t1 # t2 = t0 % t1 (Expected: remainder by zero)

# 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
30 changes: 30 additions & 0 deletions o1vm/tests/test_riscv_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,33 @@ fn test_divu_by_zero() {
witness.step();
}
}

#[test]
#[should_panic]
fn test_rem_by_zero() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/rem_by_zero",
));
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();
}
}

#[test]
#[should_panic]
fn test_remu_by_zero() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/remu_by_zero",
));
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();
}
}

0 comments on commit 12a1b74

Please sign in to comment.