Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Created November 14, 2022 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gonzih/8765bf2f565d02bedd5015f93f6b2b07 to your computer and use it in GitHub Desktop.
Save Gonzih/8765bf2f565d02bedd5015f93f6b2b07 to your computer and use it in GitHub Desktop.
zkdoc
extern crate libspartan;
extern crate merlin;
use libspartan::{Instance, SNARKGens, SNARK};
use merlin::Transcript;
use std::io::prelude::*;
// read file into Transcript
fn file_transcript(filename: &str) -> Transcript {
let mut transcript = Transcript::new(b"spartan");
let mut file = std::fs::File::open(filename).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
transcript.append_message(b"file", &buffer);
transcript
}
fn main() {
// specify the size of an R1CS instance
let num_vars = 1024;
let num_cons = 1024;
let num_inputs = 10;
let num_non_zero_entries = 1024;
// produce public parameters
let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
// ask the library to produce a synthentic R1CS instance
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
// create a commitment to the R1CS instance
let (comm, decomm) = SNARK::encode(&inst, &gens);
// produce a proof of satisfiability
let mut prover_transcript = file_transcript("src/main.rs");
let proof = SNARK::prove(
&inst,
&comm,
&decomm,
vars,
&inputs,
&gens,
&mut prover_transcript,
);
// verify the proof of satisfiability
let mut verifier_transcript = file_transcript("src/main.rs");
assert!(proof
.verify(&comm, &inputs, &mut verifier_transcript, &gens)
.is_ok());
println!("proof verification successful!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment