2024-06-19 20:15:59 +02:00

79 lines
2.4 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 methods::{METHOD_ELF, METHOD_ID};
use risc0_zkvm::{default_prover, ExecutorEnv};
use std::collections::BTreeMap;
type Note = BTreeMap<u32, u32>;
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 in_note: BTreeMap<u32, u32> = [(0, 1000)].into_iter().collect();
let in_note_cm = calculate_note_hash(&in_note);
let out_note: BTreeMap<u32, u32> = [(0, 990), (1, 10)].into_iter().collect();
let out_note_cm = calculate_note_hash(&out_note);
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]];
println!("Before: {:?}", in_note);
let from = 0;
let to = 1;
let amount: u32 = 10;
let env = ExecutorEnv::builder()
.write(&ptx_root)
.unwrap()
.write(&in_ptx_path)
.unwrap()
.write(&out_ptx_path)
.unwrap()
.write(&in_note_cm)
.unwrap()
.write(&out_note_cm)
.unwrap()
.write(&from)
.unwrap()
.write(&to)
.unwrap()
.write(&amount)
.unwrap()
.write(&in_note)
.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::default().with_receipt_kind(risc0_zkvm::ReceiptKind::Groth16);
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.
// For example:
// let output: BTreeMap<u32, u32> = receipt.journal.decode().unwrap();
// println!("After: {:?}", output);
// 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_note_hash(n: &Note) -> [u8; 32] {
let mut out = [0u8; 32];
out[0] = n.len() as u8;
out
}