From 244664b3afd567da63587e29fea2935d1c6a0c98 Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Wed, 28 Dec 2022 17:52:03 +0300 Subject: [PATCH] feat: add basic zerokit integration --- .gitignore | 3 ++- README.md | 2 +- src/args.rs | 5 +++++ src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 869df07..9d4d937 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -Cargo.lock \ No newline at end of file +Cargo.lock +temp \ No newline at end of file diff --git a/README.md b/README.md index 767872f..5fae318 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## How to use ```bash -cargo run -- help +cargo run --release -- help ``` ```bash diff --git a/src/args.rs b/src/args.rs index d6112bb..88001cf 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,4 +1,7 @@ #![allow(clippy::upper_case_acronyms)] + +use std::path::PathBuf; + use clap::{Parser, Subcommand}; #[derive(Parser)] @@ -11,4 +14,6 @@ pub struct CLI { #[derive(Subcommand)] pub enum Commands { GenerateContract, + GenerateProof { path: PathBuf }, + VerifyProof { path: PathBuf }, } diff --git a/src/main.rs b/src/main.rs index 48d9562..08c1337 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,15 @@ +use std::fs::File; +use std::io::{Cursor, Read, Write}; + use clap::Parser; +use rln::{ + protocol::{ + proof_values_from_witness, random_rln_witness, serialize_proof_values, serialize_witness, + }, + public::RLN, +}; + mod args; use args::*; @@ -7,6 +17,46 @@ fn main() { let cli = CLI::parse(); match cli.command { - Commands::GenerateContract {} => println!("ava"), + Commands::GenerateContract => println!("ava"), + Commands::GenerateProof { path } => { + let tree_height = 20; + let resources = Cursor::new("./temp/"); + + let mut rln = RLN::new(tree_height, resources); + + let rln_witness = random_rln_witness(tree_height); + + let mut input_buffer = Cursor::new(serialize_witness(&rln_witness)); + let mut output_buffer = Cursor::new(Vec::::new()); + + rln.prove(&mut input_buffer, &mut output_buffer).unwrap(); + let zk_proof = output_buffer.into_inner(); + + let proof_values = proof_values_from_witness(&rln_witness); + let serialized_proof_values = serialize_proof_values(&proof_values); + + let mut verify_data = Vec::::new(); + verify_data.extend(&zk_proof); + verify_data.extend(&serialized_proof_values); + + File::create(path).unwrap().write_all(&verify_data).unwrap(); + } + Commands::VerifyProof { path } => { + let tree_height = 20; + let resources = Cursor::new("./temp/"); + + let mut verify_data = Vec::::new(); + File::open(path) + .unwrap() + .read_to_end(&mut verify_data) + .unwrap(); + + let mut input_buffer = Cursor::new(verify_data); + + let rln = RLN::new(tree_height, resources); + + let verified = rln.verify(&mut input_buffer).unwrap(); + assert!(verified); + } }; }