Skip to content

Commit

Permalink
Add riscv-tests and fix divide extern (risc0#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaub authored Feb 1, 2023
1 parent d9df845 commit d6fc82a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
4 changes: 3 additions & 1 deletion risc0/zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[target.'cfg(not(target_os = "zkvm"))'.dev-dependencies]
env_logger = "0.10"
flate2 = "1.0"
lazy_static = "1.4"
risc0-zkvm-methods = { path = "methods" }
serial_test = "1.0"
Expand All @@ -65,13 +66,15 @@ tempdir = "0.3"
test-log = { version = "0.2", features = ["trace"] }

[features]
binfmt = ["dep:elf", "std"]
cuda = ["risc0-circuit-rv32im/cuda", "risc0-zkp/cuda"]
metal = ["risc0-circuit-rv32im/metal", "risc0-zkp/metal"]
default = ["prove"]
dual = []
insecure_skip_seal = []
profiler = ["dep:addr2line", "dep:gimli", "dep:prost", "dep:prost-build", "dep:protobuf-src"]
prove = [
"binfmt",
"dep:lazy-regex",
"dep:rand",
"dep:rayon",
Expand All @@ -81,4 +84,3 @@ prove = [
"std",
]
std = ["anyhow/std", "risc0-circuit-rv32im/std", "risc0-zeroio/std", "risc0-zkp/std", "serde/std"]
binfmt = ["dep:elf", "std"]
25 changes: 11 additions & 14 deletions risc0/zkvm/src/prove/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,18 @@ impl<'a, H: HostHandler> MachineContext<'a, H> {
(BabyBearElem, BabyBearElem, BabyBearElem, BabyBearElem),
(BabyBearElem, BabyBearElem, BabyBearElem, BabyBearElem),
) {
let mut numer = merge_word8(numer) as i64;
let mut denom = merge_word8(denom) as i64;
let mut numer = merge_word8(numer) as u32;
let mut denom = merge_word8(denom) as u32;
let sign: u32 = sign.into();
// debug!("divide: [{sign}] {numer} / {denom}");
let ones_comp = (sign == 2) as i64;
let neg_numer = sign != 0 && numer < 0;
let neg_denom = sign == 1 && denom < 0;
let ones_comp = (sign == 2) as u32;
let neg_numer = sign != 0 && (numer as i32) < 0;
let neg_denom = sign == 1 && (denom as i32) < 0;
if neg_numer {
numer = -numer - ones_comp;
numer = (!numer).overflowing_add(1 - ones_comp).0;
}
if neg_denom {
denom = -denom - ones_comp;
denom = (!denom).overflowing_add(1 - ones_comp).0;
}
let (mut quot, mut rem) = if denom == 0 {
(0xffffffff, numer)
Expand All @@ -559,16 +559,13 @@ impl<'a, H: HostHandler> MachineContext<'a, H> {
let quot_neg_out =
(neg_numer as u32 ^ neg_denom as u32) - ((denom == 0) as u32 * neg_numer as u32);
if quot_neg_out != 0 {
quot = -quot - ones_comp;
quot = (!quot).overflowing_add(1 - ones_comp).0;
}
if neg_numer {
rem = -rem - ones_comp;
rem = (!rem).overflowing_add(1 - ones_comp).0;
}
// debug!(" {quot}, {rem}");
(
split_word8(quot as i32 as u32),
split_word8(rem as i32 as u32),
)
// debug!(" quot: {quot}, rem: {rem}");
(split_word8(quot), split_word8(rem))
}

fn extract_trace(&mut self, message: &str, args: &[BabyBearElem]) -> Result<()> {
Expand Down
Binary file added risc0/zkvm/src/testdata/riscv-tests.tgz
Binary file not shown.
90 changes: 90 additions & 0 deletions risc0/zkvm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,93 @@ fn trace() {
value: 1337
}));
}

// These tests come from:
// https://github.com/riscv-software-src/riscv-tests
// They were built using the toolchain from:
// https://github.com/risc0/toolchain/releases/tag/2022.03.25
mod riscv_tests {
macro_rules! test_case {
($func_name:ident) => {
#[test_log::test]
fn $func_name() {
use std::io::Read;

use flate2::read::GzDecoder;
use risc0_zkvm_platform::{memory::MEM_SIZE, PAGE_SIZE};
use tar::Archive;
use $crate::{MemoryImage, Program, Prover};

let bytes = include_bytes!("testdata/riscv-tests.tgz");
let gz = GzDecoder::new(&bytes[..]);
let mut tar = Archive::new(gz);
for entry in tar.entries().unwrap() {
let mut entry = entry.unwrap();
if !entry.header().entry_type().is_file() {
continue;
}
let path = entry.path().unwrap();
let filename = path.file_name().unwrap().to_str().unwrap();
if filename != stringify!($func_name) {
continue;
}
let mut elf = Vec::new();
entry.read_to_end(&mut elf).unwrap();

let program = Program::load_elf(elf.as_slice(), MEM_SIZE as u32).unwrap();
let image = MemoryImage::new(&program, PAGE_SIZE as u32);

let mut prover = Prover::new(elf.as_slice(), image.root).unwrap();
prover.run().unwrap();
}
}
};
}

test_case!(add);
test_case!(addi);
test_case!(and);
test_case!(andi);
test_case!(auipc);
test_case!(beq);
test_case!(bge);
test_case!(bgeu);
test_case!(blt);
test_case!(bltu);
test_case!(bne);
test_case!(div);
test_case!(divu);
test_case!(jal);
test_case!(jalr);
test_case!(lb);
test_case!(lbu);
test_case!(lh);
test_case!(lhu);
test_case!(lui);
test_case!(lw);
test_case!(mul);
test_case!(mulh);
test_case!(mulhsu);
test_case!(mulhu);
test_case!(or);
test_case!(ori);
test_case!(rem);
test_case!(remu);
test_case!(sb);
test_case!(sh);
test_case!(simple);
test_case!(sll);
test_case!(slli);
test_case!(slt);
test_case!(slti);
test_case!(sltiu);
test_case!(sltu);
test_case!(sra);
test_case!(srai);
test_case!(srl);
test_case!(srli);
test_case!(sub);
test_case!(sw);
test_case!(xor);
test_case!(xori);
}

0 comments on commit d6fc82a

Please sign in to comment.