Use test-utils for all zkvms (#88)

This commit is contained in:
Han
2025-08-15 09:34:24 +08:00
committed by GitHub
parent a0a29cbb7d
commit bf6d94f32a
48 changed files with 374 additions and 706 deletions

View File

@@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
openvm = { git = "https://github.com/openvm-org/openvm.git", features = ["std"], tag = "v1.2.0" }
test-utils = { path = "../../../crates/test-utils" }

View File

@@ -0,0 +1,16 @@
use openvm::io::{read, read_vec, reveal_u32};
use test_utils::guest::BasicStruct;
fn main() {
// Read `Hello world` bytes.
let bytes = read_vec();
assert_eq!(String::from_utf8_lossy(&bytes), "Hello world");
// Read `BasicStruct`.
let basic_struct = read::<BasicStruct>();
let output = basic_struct.output();
output.chunks(4).enumerate().for_each(|(idx, bytes)| {
reveal_u32(u32::from_le_bytes(bytes.try_into().unwrap()), idx);
});
}

View File

@@ -1,14 +0,0 @@
use openvm::io::{read, reveal_u32};
fn main() {
let n: u64 = read();
let mut a: u64 = 0;
let mut b: u64 = 1;
for _ in 0..n {
let c: u64 = a.wrapping_add(b);
a = b;
b = c;
}
reveal_u32(a as u32, 0);
reveal_u32((a >> 32) as u32, 1);
}

View File

@@ -0,0 +1,10 @@
[package]
name = "app"
version = "0.1.0"
edition = "2024"
[workspace]
[dependencies]
pico-sdk = { git = "https://github.com/brevis-network/pico", tag = "v1.1.4" }
test-utils = { path = "../../../crates/test-utils" }

View File

@@ -0,0 +1,19 @@
#![no_main]
use pico_sdk::io::{commit, read_as, read_vec};
use test_utils::guest::BasicStruct;
pico_sdk::entrypoint!(main);
pub fn main() {
// Read `Hello world` bytes.
let bytes = read_vec();
assert_eq!(String::from_utf8_lossy(&bytes), "Hello world");
// Read `BasicStruct`.
let basic_struct = read_as::<BasicStruct>();
let output = basic_struct.output();
// Write `output`
commit(&output);
}

View File

@@ -1,2 +0,0 @@
**/target
**/elf

View File

@@ -1,7 +0,0 @@
[workspace]
members = ["lib", "app", "prover"]
resolver = "2"
[workspace.dependencies]
pico-sdk = { git = "https://github.com/brevis-network/pico", tag = "v1.1.4" }
serde = { version = "1.0.205", features = ["derive"] }

View File

@@ -1,8 +0,0 @@
[package]
name = "app"
version = "0.1.0"
edition = "2024"
[dependencies]
pico-sdk = { workspace = true }
fibonacci-lib ={ path = "../lib"}

View File

@@ -1,26 +0,0 @@
#![no_main]
pico_sdk::entrypoint!(main);
use fibonacci_lib::{FibonacciData, fibonacci};
use pico_sdk::io::{commit, read_as};
pub fn main() {
// Read inputs `n` from the environment
let n: u32 = read_as();
let a: u32 = 0;
let b: u32 = 1;
// Compute Fibonacci values starting from `a` and `b`
let (a_result, b_result) = fibonacci(a, b, n);
// Commit the assembled Fibonacci data as the public values in the Pico proof.
// This allows the values to be verified by others.
let result = FibonacciData {
n,
a: a_result,
b: b_result,
};
commit(&result);
}

View File

@@ -1,7 +0,0 @@
[package]
name = "fibonacci-lib"
version = "0.1.0"
edition = "2024"
[dependencies]
serde = { workspace = true }

View File

@@ -1,27 +0,0 @@
use serde::{Deserialize, Serialize};
use std::fs;
#[derive(Serialize, Deserialize)]
pub struct FibonacciData {
pub a: u32,
pub b: u32,
pub n: u32,
}
/// Computes the Fibonacci sequence starting from `a` and `b` up to the `n`-th iteration.
/// Returns the last two values in the sequence: (a, b).
pub fn fibonacci(mut a: u32, mut b: u32, n: u32) -> (u32, u32) {
for _ in 0..n {
let next = a.wrapping_add(b);
a = b;
b = next;
}
(a, b)
}
/// Loads an ELF file from the specified path.
pub fn load_elf(path: &str) -> Vec<u8> {
fs::read(path).unwrap_or_else(|err| {
panic!("Failed to load ELF file from {}: {}", path, err);
})
}

View File

@@ -1,10 +0,0 @@
[package]
name = "prover"
version = "0.1.0"
edition = "2024"
[dependencies]
pico-sdk = { workspace = true }
bincode = "1.3.3"
fibonacci-lib ={ path = "../lib"}
serde = { workspace = true }

View File

@@ -1,49 +0,0 @@
use fibonacci_lib::{FibonacciData, fibonacci, load_elf};
use pico_sdk::{client::DefaultProverClient, init_logger};
fn main() {
// Initialize logger
init_logger();
// Load the ELF file
let elf = load_elf("../elf/riscv32im-pico-zkvm-elf");
// Initialize the prover client
let client = DefaultProverClient::new(&elf);
// Initialize new stdin
let mut stdin_builder = client.new_stdin_builder();
// Set up input
let n = 100u32;
stdin_builder.write(&n);
// Generate proof
let proof = client
.prove_fast(stdin_builder)
.expect("Failed to generate proof");
// Decodes public values from the proof's public value stream.
let public_buffer = proof.pv_stream.unwrap();
// Deserialize public_buffer into FibonacciData
let public_values: FibonacciData =
bincode::deserialize(&public_buffer).expect("Failed to deserialize");
// Verify the public values
verify_public_values(n, &public_values);
}
/// Verifies that the computed Fibonacci values match the public values.
fn verify_public_values(n: u32, public_values: &FibonacciData) {
println!(
"Public value n: {:?}, a: {:?}, b: {:?}",
public_values.n, public_values.a, public_values.b
);
// Compute Fibonacci values locally
let (result_a, result_b) = fibonacci(0, 1, n);
// Assert that the computed values match the public values
assert_eq!(result_a, public_values.a, "Mismatch in value 'a'");
assert_eq!(result_b, public_values.b, "Mismatch in value 'b'");
}

View File

@@ -6,4 +6,5 @@ edition = "2021"
[workspace]
[dependencies]
risc0-zkvm = { version = "2.3.1", default-features = false, features = ['std'] }
risc0-zkvm = { version = "2.3.1", default-features = false, features = ['std', 'unstable'] }
test-utils = { path = "../../../crates/test-utils" }

View File

@@ -0,0 +1,15 @@
use risc0_zkvm::guest::env;
use test_utils::guest::BasicStruct;
fn main() {
// Read `Hello world` bytes.
let bytes = env::read_frame();
assert_eq!(String::from_utf8_lossy(&bytes), "Hello world");
// Read `BasicStruct`.
let basic_struct = env::read::<BasicStruct>();
let output = basic_struct.output();
// Write `output`
env::commit(&output);
}

View File

@@ -1,13 +0,0 @@
use risc0_zkvm::guest::env;
fn main() {
// TODO: Implement your guest code here
// read the input
let input: u32 = env::read();
// TODO: do something with the input
// write public output to the journal
env::commit(&input);
}

View File

@@ -6,4 +6,6 @@ edition = "2021"
[workspace]
[dependencies]
bincode = "1.3.3"
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.9.0" }
test-utils = { path = "../../../crates/test-utils" }

View File

@@ -0,0 +1,22 @@
#![no_main]
use test_utils::guest::BasicStruct;
ziskos::entrypoint!(main);
fn main() {
let input = ziskos::read_input();
let mut input = input.as_slice();
// Read `Hello world` bytes.
let bytes: Vec<u8> = bincode::deserialize_from(&mut input).unwrap();
assert_eq!(String::from_utf8_lossy(&bytes), "Hello world");
// Read `BasicStruct`.
let basic_struct: BasicStruct = bincode::deserialize_from(&mut input).unwrap();
let output = basic_struct.output();
output.chunks(4).enumerate().for_each(|(idx, bytes)| {
ziskos::set_output(idx, u32::from_le_bytes(bytes.try_into().unwrap()));
});
}

View File

@@ -1,9 +0,0 @@
[package]
name = "ere-test-zisk-guest"
version = "0.1.0"
edition = "2021"
[workspace]
[dependencies]
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.9.0" }

View File

@@ -1,14 +0,0 @@
#![no_main]
ziskos::entrypoint!(main);
fn main() {
// Read an input
let n = u32::from_le_bytes(
ziskos::read_input()
.try_into()
.expect("input to be 4 bytes"),
);
// Write n*2 to output
ziskos::set_output(0, n * 2);
}

View File

@@ -1,9 +0,0 @@
[package]
name = "ere-test-zisk-guest"
version = "0.1.0"
edition = "2021"
[workspace]
[dependencies]
ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git", tag = "v0.9.0" }

View File

@@ -1,16 +0,0 @@
#![no_main]
ziskos::entrypoint!(main);
fn main() {
let input = ziskos::read_input();
if input.len() != 6 {
std::process::exit(1);
}
// Read an input
let n = u32::from_le_bytes(input[..4].try_into().unwrap());
let a = u16::from_le_bytes(input[4..6].try_into().unwrap()) as u32;
ziskos::set_output(0, (n + a) * 2);
}

View File

@@ -1,16 +0,0 @@
#![no_main]
ziskos::entrypoint!(main);
fn main() {
let input = ziskos::read_input();
if input.len() != 6 {
std::process::exit(1);
}
// Read an input
let n = u32::from_le_bytes(input[..4].try_into().unwrap());
let a = u16::from_le_bytes(input[4..6].try_into().unwrap()) as u32;
ziskos::set_output(0, (n + a) * 2);
}