From b07bb4e2b07d355710dfa394163df62e1738cee4 Mon Sep 17 00:00:00 2001 From: parazyd Date: Tue, 8 Feb 2022 14:27:46 +0100 Subject: [PATCH] proof: Add example for arithmetic operations. --- Cargo.toml | 7 +++++ Makefile | 3 ++- proof/arithmetic.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++ proof/arithmetic.zk | 14 ++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 proof/arithmetic.rs create mode 100644 proof/arithmetic.zk diff --git a/Cargo.toml b/Cargo.toml index 90b5a675e..881e0997c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -241,6 +241,13 @@ name = "tree" path = "example/tree.rs" required-features = ["crypto"] +# ZK VM Proof examples + +[[example]] +name = "arithmetic" +path = "proof/arithmetic.rs" +required-features = ["cli", "crypto", "zkas"] + [[example]] name = "mint" path = "proof/mint.rs" diff --git a/Makefile b/Makefile index c8817c7ca..7f8dc9b61 100644 --- a/Makefile +++ b/Makefile @@ -39,13 +39,14 @@ test: test-vm test-tx test-tx: $(CARGO) run --release --features=node,zkas --example tx -VM_SRC = proof/mint.zk proof/burn.zk +VM_SRC = proof/arithmetic.zk proof/mint.zk proof/burn.zk VM_BIN = $(VM_SRC:=.bin) $(VM_BIN): zkas $(VM_SRC) ./zkas $(basename $@) -o $@ test-vm: $(VM_BIN) + $(CARGO) run --release --features=cli,crypto,zkas --example arithmetic $(CARGO) run --release --features=cli,crypto,zkas --example mint $(CARGO) run --release --features=cli,crypto,zkas --example burn diff --git a/proof/arithmetic.rs b/proof/arithmetic.rs new file mode 100644 index 000000000..a2619ddda --- /dev/null +++ b/proof/arithmetic.rs @@ -0,0 +1,66 @@ +use darkfi::{ + crypto::{ + proof::{ProvingKey, VerifyingKey}, + Proof, + }, + zk::vm::{Witness, ZkCircuit}, + zkas::decoder::ZkBinary, + Result, +}; +use log::info; +use pasta_curves::pallas; +use rand::rngs::OsRng; +use simplelog::{ColorChoice::Auto, Config, LevelFilter, TermLogger, TerminalMode::Mixed}; + +fn main() -> Result<()> { + let loglevel = match option_env!("RUST_LOG") { + Some("debug") => LevelFilter::Debug, + Some("trace") => LevelFilter::Trace, + Some(_) | None => LevelFilter::Info, + }; + TermLogger::init(loglevel, Config::default(), Mixed, Auto)?; + + /* ANCHOR: main */ + let bincode = include_bytes!("arithmetic.zk.bin"); + let zkbin = ZkBinary::decode(bincode)?; + + // ====== + // Prover + // ====== + + // Witness values + let a = pallas::Base::from(42); + let b = pallas::Base::from(69); + + let prover_witnesses = vec![Witness::Base(Some(a)), Witness::Base(Some(b))]; + + // Create the public inputs + let sum = a + b; + let product = a * b; + + let public_inputs = vec![sum, product]; + + // Create the circuit + let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); + + info!(target: "PROVER", "Building proving key and creating the zero-knowledge proof"); + let proving_key = ProvingKey::build(11, &circuit); + let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?; + + // ======== + // Verifier + // ======== + + // Construct empty witnesses + let verifier_witnesses = vec![Witness::Base(None), Witness::Base(None)]; + + // Create the circuit + let circuit = ZkCircuit::new(verifier_witnesses, zkbin); + + info!(target: "VERIFIER", "Building verifying key and verifying the zero-knowledge proof"); + let verifying_key = VerifyingKey::build(11, &circuit); + proof.verify(&verifying_key, &public_inputs)?; + /* ANCHOR_END: main */ + + Ok(()) +} diff --git a/proof/arithmetic.zk b/proof/arithmetic.zk new file mode 100644 index 000000000..f7e614b6c --- /dev/null +++ b/proof/arithmetic.zk @@ -0,0 +1,14 @@ +constant "Arith" {} + +contract "Arith" { + Base a, + Base b, +} + +circuit "Arith" { + sum = base_add(a, b); + constrain_instance(sum); + + product = base_mul(a, b); + constrain_instance(product); +}