InputItem: Support extra traits (#55)

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
This commit is contained in:
Ignacio Hagopian
2025-07-18 21:05:25 +02:00
committed by GitHub
parent 3da61c14cb
commit f01a6c16db
4 changed files with 26 additions and 10 deletions

View File

@@ -9,14 +9,14 @@ license.workspace = true
zkvm-interface = { workspace = true }
anyhow = "1.0" #TODO: remove only needed in tests
toml = "0.8"
risc0-zkvm = { version = "^2.2.0", features = ["unstable"] }
risc0-zkvm = { version = "^2.3.0", features = ["unstable"] }
borsh = "1.5.7"
hex = "*"
tempfile = "3.3"
serde_json = "1.0"
thiserror = "2"
serde = { version = "1.0.219", features = ["derive"] }
serde = { version = "1.0.219", features = ["derive", "rc"] }
[build-dependencies]
build-utils = { workspace = true }

View File

@@ -1,15 +1,28 @@
use std::{fmt::Debug, sync::Arc};
use bincode::Options;
use erased_serde::Serialize as ErasedSerialize;
use serde::Serialize;
#[derive(Clone)]
pub enum InputItem {
/// A serializable object stored as a trait object
Object(Box<dyn ErasedSerialize>),
Object(Arc<Box<dyn ErasedSerialize + Send + Sync>>),
/// Pre-serialized bytes (e.g., from bincode)
Bytes(Vec<u8>),
}
impl Debug for InputItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InputItem::Object(_) => f.write_str("Object(<erased>)"),
InputItem::Bytes(bytes) => f.debug_tuple("Bytes").field(bytes).finish(),
}
}
}
/// Represents a builder for input data to be passed to a ZKVM guest program.
#[derive(Debug, Clone)]
pub struct Input {
items: Vec<InputItem>,
}
@@ -28,8 +41,9 @@ impl Input {
}
/// Write a serializable value as a trait object
pub fn write<T: Serialize + 'static>(&mut self, value: T) {
self.items.push(InputItem::Object(Box::new(value)));
pub fn write<T: Serialize + Send + Sync + 'static>(&mut self, value: T) {
self.items
.push(InputItem::Object(Arc::new(Box::new(value))));
}
/// Write pre-serialized bytes directly
@@ -81,7 +95,7 @@ impl InputItem {
erased_serde::serialize(obj.as_ref(), &mut serializer)?;
Ok(buf)
}
InputItem::Bytes(bytes) => Ok(bytes.clone()),
InputItem::Bytes(bytes) => Ok(bytes.to_vec()),
}
}
}
@@ -91,7 +105,7 @@ mod input_erased_tests {
use super::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Person {
name: String,
age: u32,
@@ -125,7 +139,7 @@ mod input_erased_tests {
assert_eq!(input.len(), 1);
match &input.items[0] {
InputItem::Bytes(stored_bytes) => assert_eq!(stored_bytes, &bytes),
InputItem::Bytes(stored_bytes) => assert_eq!(stored_bytes.to_vec(), bytes),
InputItem::Object(_) => panic!("Expected Bytes, got Object"),
}
}

View File

@@ -6,7 +6,7 @@ edition = "2021"
[workspace]
[build-dependencies]
risc0-build = { version = "^2.1.1" }
risc0-build = { version = "^2.3.0" }
[package.metadata.risc0]
methods = ["guest"]

View File

@@ -6,4 +6,6 @@ edition = "2021"
[workspace]
[dependencies]
risc0-zkvm = { version = "^2.0.2", default-features = false, features = ['std'] }
risc0-zkvm = { version = "^2.3.0", default-features = false, features = [
'std',
] }