zkVK methods return public values (#106)

This commit is contained in:
Han
2025-08-27 14:27:56 +08:00
committed by GitHub
parent 63107268a1
commit 44044a1858
28 changed files with 655 additions and 337 deletions

View File

@@ -6,5 +6,6 @@ edition = "2021"
[workspace]
[dependencies]
bincode = "1.3.3"
openvm = { git = "https://github.com/openvm-org/openvm.git", features = ["std"], tag = "v1.2.0" }
test-utils = { path = "../../../crates/test-utils" }

View File

@@ -1,16 +1,30 @@
use core::array::from_fn;
use openvm::io::{read, read_vec, reveal_u32};
use test_utils::guest::BasicStruct;
use test_utils::guest::{BasicStruct, BASIC_PROGRAM_BYTES_LENGTH};
fn main() {
// Read `Hello world` bytes.
// Read `bytes`.
let bytes = read_vec();
assert_eq!(String::from_utf8_lossy(&bytes), "Hello world");
// Read `BasicStruct`.
// Read `basic_struct`.
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);
});
// Check `bytes` length is as expected.
assert_eq!(bytes.len(), BASIC_PROGRAM_BYTES_LENGTH);
// Do some computation on `basic_struct`.
let basic_struct_output = basic_struct.output();
// Write reversed `bytes` and `basic_struct_output`
let public_values = core::iter::empty()
.chain(bytes.into_iter().rev())
.chain(bincode::serialize(&basic_struct_output).unwrap())
.collect::<Vec<_>>();
public_values
.chunks(4)
.enumerate()
.for_each(|(idx, bytes)| {
let bytes = from_fn(|i| bytes.get(i).copied().unwrap_or_default());
reveal_u32(u32::from_le_bytes(bytes), idx);
});
}