From 7f0751efb030adfebaa8edb90fc37c9d4ea01ce9 Mon Sep 17 00:00:00 2001 From: JackPiri Date: Mon, 3 Jun 2024 14:41:41 +0200 Subject: [PATCH] Support for no_std environment Also included minor refactoring --- src/deserializer.rs | 4 ++-- src/lib.rs | 9 +++++---- src/proof.rs | 2 +- tests/integration.rs | 14 +++++++------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/deserializer.rs b/src/deserializer.rs index 1d6cb66..ef2bc7c 100644 --- a/src/deserializer.rs +++ b/src/deserializer.rs @@ -13,8 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use risc0_zkvm::*; -use snafu::*; +use risc0_zkvm::Receipt; +use snafu::Snafu; /// Deserialization error #[derive(Debug, Snafu)] diff --git a/src/lib.rs b/src/lib.rs index 92aab6f..857452f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,13 +13,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +#![cfg_attr(not(feature = "std"), no_std)] + mod deserializer; mod proof; -use deserializer::*; +use deserializer::{deserialize, DeserializeError}; pub use proof::ProofRawData; -use risc0_zkvm::*; -use snafu::*; +use snafu::Snafu; /// Deserialization error. #[derive(Debug, Snafu)] @@ -56,6 +57,6 @@ impl From<[u32; 8]> for Vk { } pub fn verify(proof: ProofRawData, image_id: Vk) -> Result<(), VerifyError> { - let receipt = deserialize(&proof)?; + let receipt = deserialize(proof)?; receipt.verify(image_id.0).map_err(Into::into) } diff --git a/src/proof.rs b/src/proof.rs index 4a4fb89..09038f8 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -13,4 +13,4 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub type ProofRawData = Vec; +pub type ProofRawData<'a> = &'a [u8]; diff --git a/tests/integration.rs b/tests/integration.rs index c410510..e3d6354 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -13,12 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use risc0_verifier::{verify, ProofRawData, VerifyError}; +use risc0_verifier::{verify, VerifyError}; use rstest::rstest; use serde::Deserialize; use std::path::{Path, PathBuf}; -fn load_data(path: &Path) -> (ProofRawData, [u32; 8]) { +fn load_data(path: &Path) -> (Vec, [u32; 8]) { #[derive(Deserialize)] struct Data { raw_proof_data: String, @@ -30,16 +30,16 @@ fn load_data(path: &Path) -> (ProofRawData, [u32; 8]) { image_id_data, } = serde_json::from_reader(std::fs::File::open(path).unwrap()).unwrap(); - let proof_raw_data = ::try_from(hex::decode(raw_proof_data).unwrap()).unwrap(); + let gino = >::try_from(hex::decode(raw_proof_data).unwrap()).unwrap(); - (proof_raw_data, image_id_data) + (gino, image_id_data) } #[rstest] fn should_verify_valid_proof(#[files("./resources/*.json")] path: PathBuf) { let (proof_raw_data, image_id_data) = load_data(&path); - assert!(verify(proof_raw_data, image_id_data.into()).is_ok()); + assert!(verify(&proof_raw_data, image_id_data.into()).is_ok()); } #[test] @@ -50,7 +50,7 @@ fn should_not_verify_invalid_proof() { proof_raw_data[0] = (proof_raw_data.first().unwrap() + 1) % 255; assert!(matches!( - verify(proof_raw_data, image_id_data.into()), + verify(&proof_raw_data, image_id_data.into()), Err(VerifyError::InvalidData { .. }) )); } @@ -64,7 +64,7 @@ fn should_not_verify_false_proof() { proof_raw_data[len - 1] = (proof_raw_data.last().unwrap() + 1) % 255; assert!(matches!( - verify(proof_raw_data, image_id_data.into()), + verify(&proof_raw_data, image_id_data.into()), Err(VerifyError::Failure { .. }) )); }