Giacomo Pasini 3c991173c5
fix zone PoC
2024-06-26 11:33:52 +02:00

88 lines
2.6 KiB
Rust

// These constants represent the RISC-V ELF and the image ID generated by risc0-build.
// The ELF is used for proving and the ID is used for verification.
use blake2::{Blake2s256, Digest};
use methods::{METHOD_ELF, METHOD_ID};
use risc0_zkvm::{default_prover, ExecutorEnv};
use common::*;
fn main() {
// Initialize tracing. In order to view logs, run `RUST_LOG=info cargo run`
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env())
.init();
let state: State = [(0, 1000)].into_iter().collect();
let journal = vec![];
let zone_input = Input::Transfer {
from: 0,
to: 1,
amount: 10,
};
let in_note = Note {
state_cm: calculate_state_hash(&state),
journal_cm: calculate_journal_hash(&journal),
zone_input,
};
let mut out_journal = journal.clone();
out_journal.push(zone_input);
let out_note = Note {
state_cm: calculate_state_hash(&stf(state.clone(), zone_input)),
journal_cm: calculate_journal_hash(&out_journal),
zone_input: Input::None,
};
let ptx_root = [0u8; 32];
let in_ptx_path: Vec<[u8; 32]> = vec![[0; 32]];
let out_ptx_path: Vec<[u8; 32]> = vec![[0; 32]];
let env = ExecutorEnv::builder()
.write(&ptx_root)
.unwrap()
.write(&in_ptx_path)
.unwrap()
.write(&out_ptx_path)
.unwrap()
.write(&in_note)
.unwrap()
.write(&out_note)
.unwrap()
.write(&state)
.unwrap()
.write(&journal)
.unwrap()
.build()
.unwrap();
// Obtain the default prover.
let prover = default_prover();
// Proof information by proving the specified ELF binary.
// This struct contains the receipt along with statistics about execution of the guest
let opts = risc0_zkvm::ProverOpts::succinct();
let prove_info = prover.prove_with_opts(env, METHOD_ELF, &opts).unwrap();
// extract the receipt.
let receipt = prove_info.receipt;
// TODO: Implement code for retrieving receipt journal here.
std::fs::write("proof.stark", bincode::serialize(&receipt).unwrap()).unwrap();
// The receipt was verified at the end of proving, but the below code is an
// example of how someone else could verify this receipt.
receipt.verify(METHOD_ID).unwrap();
}
fn calculate_state_hash(state: &State) -> [u8; 32] {
let bytes = bincode::serialize(state).unwrap();
Blake2s256::digest(&bytes).into()
}
fn calculate_journal_hash(journal: &Journal) -> [u8; 32] {
let bytes = bincode::serialize(journal).unwrap();
Blake2s256::digest(&bytes).into()
}