Skip to content

Commit

Permalink
Support for no_std environment
Browse files Browse the repository at this point in the history
Also included minor refactoring
  • Loading branch information
JackPiri committed Jun 3, 2024
1 parent 3cddeac commit 7f0751e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub type ProofRawData = Vec<u8>;
pub type ProofRawData<'a> = &'a [u8];
14 changes: 7 additions & 7 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, [u32; 8]) {
#[derive(Deserialize)]
struct Data {
raw_proof_data: String,
Expand All @@ -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 = <ProofRawData>::try_from(hex::decode(raw_proof_data).unwrap()).unwrap();
let gino = <Vec<u8>>::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]
Expand All @@ -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 { .. })
));
}
Expand All @@ -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 { .. })
));
}

0 comments on commit 7f0751e

Please sign in to comment.