From f01a6c16dbcc896d4a0e80b6978125aec021d579 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 18 Jul 2025 21:05:25 +0200 Subject: [PATCH] InputItem: Support extra traits (#55) Signed-off-by: Ignacio Hagopian --- crates/ere-risczero/Cargo.toml | 4 +-- crates/zkvm-interface/src/input.rs | 26 ++++++++++++++----- .../project_structure_build/Cargo.toml | 2 +- .../project_structure_build/guest/Cargo.toml | 4 ++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/ere-risczero/Cargo.toml b/crates/ere-risczero/Cargo.toml index 93a2699..d5af27b 100644 --- a/crates/ere-risczero/Cargo.toml +++ b/crates/ere-risczero/Cargo.toml @@ -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 } diff --git a/crates/zkvm-interface/src/input.rs b/crates/zkvm-interface/src/input.rs index e8020bb..8c03b82 100644 --- a/crates/zkvm-interface/src/input.rs +++ b/crates/zkvm-interface/src/input.rs @@ -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), + Object(Arc>), /// Pre-serialized bytes (e.g., from bincode) Bytes(Vec), } +impl Debug for InputItem { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + InputItem::Object(_) => f.write_str("Object()"), + 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, } @@ -28,8 +41,9 @@ impl Input { } /// Write a serializable value as a trait object - pub fn write(&mut self, value: T) { - self.items.push(InputItem::Object(Box::new(value))); + pub fn write(&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"), } } diff --git a/tests/risczero/compile/project_structure_build/Cargo.toml b/tests/risczero/compile/project_structure_build/Cargo.toml index 10eea34..f7824df 100644 --- a/tests/risczero/compile/project_structure_build/Cargo.toml +++ b/tests/risczero/compile/project_structure_build/Cargo.toml @@ -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"] diff --git a/tests/risczero/compile/project_structure_build/guest/Cargo.toml b/tests/risczero/compile/project_structure_build/guest/Cargo.toml index b4e33cc..b6d189f 100644 --- a/tests/risczero/compile/project_structure_build/guest/Cargo.toml +++ b/tests/risczero/compile/project_structure_build/guest/Cargo.toml @@ -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', +] }