Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zkInterface backend #279

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ rand = "0.6"
byteorder = "1"
serde = "1"
serde_derive = "1"
bincode = "1"
failure = "0.1"
merlin = "1.1"
clear_on_drop = "0.2"
zkinterface = { version = "1.3.3", optional = true }

[dev-dependencies]
hex = "0.3"
criterion = "0.2"
bincode = "1"
rand_chacha = "0.1"

[features]
default = ["yoloproofs"]
avx2_backend = ["curve25519-dalek/avx2_backend"]
# Disable the yoloproofs feature for the released crate, so that it's not possible for someone to publish a crate using R1CS proofs yet.
# yoloproofs = []
yoloproofs = ["zkinterface"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First thought: can we make zkinterface depend on yoloproofs, not the other way around? Then the user is able to use R1CS w/o zkinterface.


[[test]]
name = "range_proof"
Expand Down
73 changes: 73 additions & 0 deletions src/bin/zkif_bulletproofs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
extern crate bincode;
extern crate bulletproofs;
extern crate zkinterface;

use std::env;
use std::io::{stdin, Read, Write};
use std::fs::{File, create_dir_all};
use std::path::Path;
use self::zkinterface::{Result, Reader};
use bulletproofs::r1cs::{zkinterface_backend, R1CSProof};

const USAGE: &str = "Bulletproofs proving system.

zkif_bulletproofs prove [proof_path]
zkif_bulletproofs verify [proof_path]

";

const DEFAULT_PROOF_PATH: &str = "bulletproofs-proof";

pub fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
let args: Vec<&str> = args.iter().map(|a| &a[..]).collect();
if args.len() <= 1 {
eprintln!("{}", USAGE);
return Err("Missing command.".into());
}

let command = args[1];

let proof_path = if args.len() == 2 { DEFAULT_PROOF_PATH } else { args[2] };
let proof_path = Path::new(proof_path);
if let Some(parent) = proof_path.parent() {
create_dir_all(parent)?;
}

let read = || -> Result<Reader> {
let mut messages = Reader::new();
messages.read_from(&mut stdin())?;
Ok(messages)
};

match &command[..] {
"prove" => main_prove(read()?, proof_path),
"verify" => main_verify(read()?, proof_path),
_ => {
eprintln!("{}", USAGE);
Err(format!("Unknown command {}", command).into())
}
}
}

fn main_prove(messages: Reader, proof_path: &Path) -> Result<()> {
let proof = zkinterface_backend::prove(&messages)?;

// Save to file.
let proof_ser = bincode::serialize(&proof)?;
File::create(proof_path)?.write_all(&proof_ser)?;

eprintln!("Saved proof into {}", proof_path.display());
Ok(())
}

fn main_verify(messages: Reader, proof_path: &Path) -> Result<()> {
eprintln!("Verifying proof in {}", proof_path.display());

// Load from file.
let mut proof_ser = Vec::new();
File::open(&proof_path)?.read_to_end(&mut proof_ser)?;
let proof: R1CSProof = bincode::deserialize(&proof_ser)?;

zkinterface_backend::verify(&messages, &proof)
}
2 changes: 1 addition & 1 deletion src/inner_product_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use merlin::Transcript;
use errors::ProofError;
use transcript::TranscriptProtocol;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InnerProductProof {
pub(crate) L_vec: Vec<CompressedRistretto>,
pub(crate) R_vec: Vec<CompressedRistretto>,
Expand Down
5 changes: 3 additions & 2 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![doc(include = "../docs/r1cs-docs-example.md")]
#![doc(include = "../../docs/r1cs-docs-example.md")]

#[doc(include = "../docs/cs-proof.md")]
#[doc(include = "../../docs/cs-proof.md")]
mod notes {}

mod constraint_system;
mod linear_combination;
mod proof;
mod prover;
mod verifier;
pub mod zkinterface_backend;

pub use self::constraint_system::ConstraintSystem;
pub use self::linear_combination::{LinearCombination, Variable};
Expand Down
2 changes: 1 addition & 1 deletion src/r1cs/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use inner_product_proof::InnerProductProof;
/// the constraint system using
/// [`VerifierCS::verify`](::r1cs::VerifierCS::verify) to verify the
/// proof.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct R1CSProof {
/// Commitment to the values of input wires
Expand Down
Loading