Skip to content

Commit

Permalink
Merge pull request #2899 from o1-labs/dw/sub-tests
Browse files Browse the repository at this point in the history
o1vm/riscv32im: add tests for sub
  • Loading branch information
dannywillems authored Dec 24, 2024
2 parents b7e8000 + b7e0aa9 commit 0b1438b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
Binary file added o1vm/resources/programs/riscv32im/bin/sub
Binary file not shown.
Binary file added o1vm/resources/programs/riscv32im/bin/sub_2
Binary file not shown.
Binary file added o1vm/resources/programs/riscv32im/bin/sub_3
Binary file not shown.
25 changes: 25 additions & 0 deletions o1vm/resources/programs/riscv32im/src/sub.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.section .text
.globl _start

_start:

# Test 1: Simple subtraction
li t0, 1000 # t0 = 1000
li t1, 500 # t1 = 500
sub t2, t0, t1 # t2 = t0 - t1 (Expected: t2 = 500)

# Test 2: Subtracting from zero
li t3, 0 # t3 = 0
li t4, 100 # t4 = 100
sub t5, t3, t4 # t5 = t3 - t4 (Expected: t5 = -100)

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

_start:

# Test 3: Subtracting a larger value (result negative)
li t0, 300 # t0 = 300
li t1, 500 # t1 = 500
sub t2, t0, t1 # t2 = t0 - t1 (Expected: t2 = -200)

# Test 4: Subtracting negative values (result positive)
li t3, 100 # t3 = 100
li t4, -50 # t4 = -50
sub t5, t3, t4 # t5 = t3 - t4 (Expected: t5 = 150)

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

_start:
# Test 5: Result of subtracting from a register (using same value)
li t0, 1234 # t0 = 1234
sub t1, t0, t0 # t1 = t0 - t0 (Expected: t1 = 0)

# Test 6: Handling overflow (large subtraction result)
li t2, 2147483647 # t2 = 2147483647 (max positive signed 32-bit)
li t3, -1 # t3 = -1
sub t4, t2, t3 # t4 = t2 - t3 (Expected: t0 = 2147483648, wraparound to -2147483648)
# 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
51 changes: 51 additions & 0 deletions o1vm/tests/test_riscv_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,54 @@ fn test_addi_boundary_immediate() {
assert_eq!(witness.registers[T1], 2147);
assert_eq!(witness.registers[T3], (-1048_i32) as u32);
}

#[test]
fn test_sub() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/sub",
));
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[T2], 500);
assert_eq!(witness.registers[T5], (-100_i32) as u32);
}

#[test]
fn test_sub_2() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/sub_2",
));
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[T2], -200_i32 as u32);
assert_eq!(witness.registers[T5], 150);
}

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

0 comments on commit 0b1438b

Please sign in to comment.