mirror of
https://github.com/eth-act/ere.git
synced 2026-04-03 03:00:17 -04:00
Add crate test-utils (#82)
This commit is contained in:
19
crates/test-utils/Cargo.toml
Normal file
19
crates/test-utils/Cargo.toml
Normal 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"]
|
||||
28
crates/test-utils/src/guest.rs
Normal file
28
crates/test-utils/src/guest.rs
Normal 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
|
||||
}
|
||||
}
|
||||
73
crates/test-utils/src/host.rs
Normal file
73
crates/test-utils/src/host.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
8
crates/test-utils/src/lib.rs
Normal file
8
crates/test-utils/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#![cfg_attr(not(feature = "host"), no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
pub mod guest;
|
||||
|
||||
#[cfg(feature = "host")]
|
||||
pub mod host;
|
||||
Reference in New Issue
Block a user