Add crate test-utils (#82)

This commit is contained in:
Han
2025-08-14 22:00:23 +08:00
committed by GitHub
parent 360a59bd67
commit a0a29cbb7d
18 changed files with 233 additions and 237 deletions

View File

@@ -0,0 +1,19 @@
[package]
name = "test-utils"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
[dependencies]
serde = { workspace = true, features = ["derive"] }
# Local dependencies
zkvm-interface = { workspace = true, optional = true }
[lints]
workspace = true
[features]
default = []
host = ["dep:zkvm-interface"]

View File

@@ -0,0 +1,28 @@
use alloc::vec::Vec;
use core::iter;
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize)]
pub struct BasicStruct {
pub a: u8,
pub b: u16,
pub c: u32,
pub d: u64,
pub e: Vec<u8>,
}
impl BasicStruct {
/// Performs some computation (Xoring all fields as bytes into `[u8; 32]`).
pub fn output(&self) -> [u8; 32] {
let mut output = [0; 32];
iter::empty()
.chain(self.a.to_le_bytes())
.chain(self.b.to_le_bytes())
.chain(self.c.to_le_bytes())
.chain(self.d.to_le_bytes())
.chain(self.e.iter().copied())
.enumerate()
.for_each(|(idx, byte)| output[idx % output.len()] ^= byte);
output
}
}

View File

@@ -0,0 +1,73 @@
use crate::guest::BasicStruct;
use std::path::PathBuf;
use zkvm_interface::{Input, zkVM};
fn workspace() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.pop();
path.pop();
path
}
pub fn testing_guest_directory(zkvm_name: &str, program: &str) -> PathBuf {
workspace().join("tests").join(zkvm_name).join(program)
}
pub fn run_zkvm_execute(zkvm: &impl zkVM, inputs: &Input) {
let _report = zkvm
.execute(inputs)
.expect("execute should not fail with valid input");
// TODO: Check output are expected.
}
pub fn run_zkvm_prove(zkvm: &impl zkVM, inputs: &Input) {
let (proof, _report) = zkvm
.prove(inputs)
.expect("prove should not fail with valid input");
zkvm.verify(&proof)
.expect("verify should not fail with valid input");
// TODO: Check output are expected.
}
/// The basic program takes 2 inputs:
/// - `Vec<u8>` that supposed to be "Hello world"
/// - `BasicStruct`
///
/// Outputs `[u8; 32]` which computed by xoring fields of `BasicStruct`.
pub struct BasicProgramInputGen;
impl BasicProgramInputGen {
pub fn valid() -> Input {
let mut inputs = Input::new();
inputs.write_bytes("Hello world".as_bytes().to_vec());
inputs.write(BasicStruct {
a: 0xff,
b: 0x7777,
c: 0xffffffff,
d: 0x7777777777777777,
e: (0..u8::MAX).collect(),
});
inputs
}
pub fn invalid_string() -> Input {
let mut inputs = Input::new();
inputs.write_bytes("Unexpected string".as_bytes().to_vec());
inputs.write(BasicStruct::default());
inputs
}
pub fn invalid_type() -> Input {
let mut inputs = Input::new();
inputs.write(BasicStruct::default());
inputs.write_bytes("Hello world".as_bytes().to_vec());
inputs
}
pub fn empty() -> Input {
Input::new()
}
}

View File

@@ -0,0 +1,8 @@
#![cfg_attr(not(feature = "host"), no_std)]
extern crate alloc;
pub mod guest;
#[cfg(feature = "host")]
pub mod host;