From 8500ba0cb6e5af3860092fa85f0c97cd05f92fde Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Fri, 23 May 2025 16:03:20 -0700 Subject: [PATCH 1/2] cleanup and update --- README.md | 9 ++++----- profiler/nizk.rs | 2 +- profiler/snark.rs | 4 ++-- src/lib.rs | 22 +++++++++++----------- src/{r1csinstance.rs => r1cs.rs} | 10 +++++----- src/r1csproof.rs | 12 ++++++------ 6 files changed, 29 insertions(+), 30 deletions(-) rename src/{r1csinstance.rs => r1cs.rs} (98%) diff --git a/README.md b/README.md index 6a1cb60b..5e0cb693 100644 --- a/README.md +++ b/README.md @@ -285,21 +285,20 @@ cargo doc To run tests: ```text -RUSTFLAGS="-C target_cpu=native" cargo test +RUSTFLAGS='-C target_cpu=native --cfg curve25519_dalek_backend="BACKEND"' cargo test ``` To build `libspartan`: ```text -RUSTFLAGS="-C target_cpu=native" cargo build --release +RUSTFLAGS='-C target_cpu=native --cfg curve25519_dalek_backend="BACKEND"' cargo build --release ``` -> NOTE: We enable SIMD instructions in `curve25519-dalek` by default, so if it fails to build remove the "simd_backend" feature argument in `Cargo.toml`. +> NOTE: We enable SIMD instructions in `curve25519-dalek` by default, so if it fails to build remove the argument passed to curve25519_dalek in the above command. ### Supported features - `std`: enables std features (enabled by default) -- `simd_backend`: enables `curve25519-dalek`'s simd feature (enabled by default) - `profile`: enables fine-grained profiling information (see below for its use) ### WASM Support @@ -327,7 +326,7 @@ getrandom = { version = "0.1", features = ["wasm-bindgen"] } To run end-to-end benchmarks: ```text -RUSTFLAGS="-C target_cpu=native" cargo bench +RUSTFLAGS='-C target_cpu=native --cfg curve25519_dalek_backend="BACKEND"' cargo bench ``` ### Fine-grained profiling diff --git a/profiler/nizk.rs b/profiler/nizk.rs index 1499c86b..11be968d 100644 --- a/profiler/nizk.rs +++ b/profiler/nizk.rs @@ -25,7 +25,7 @@ pub fn main() { let num_cons = num_vars; let num_inputs = 10; - // produce a synthetic R1CSInstance + // produce a synthetic R1CSShape let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); // produce public generators diff --git a/profiler/snark.rs b/profiler/snark.rs index f30d7154..a7038dd0 100644 --- a/profiler/snark.rs +++ b/profiler/snark.rs @@ -24,13 +24,13 @@ pub fn main() { let num_cons = num_vars; let num_inputs = 10; - // produce a synthetic R1CSInstance + // produce a synthetic R1CSShape let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); // produce public generators let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons); - // create a commitment to R1CSInstance + // create a commitment to R1CSShape let (comm, decomm) = SNARK::encode(&inst, &gens); // produce a proof of satisfiability diff --git a/src/lib.rs b/src/lib.rs index 7425ecdf..d9d58cf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ mod group; mod math; mod nizk; mod product_tree; -mod r1csinstance; +mod r1cs; mod r1csproof; mod random; mod scalar; @@ -33,8 +33,8 @@ mod unipoly; use core::cmp::max; use errors::{ProofVerifyError, R1CSError}; use merlin::Transcript; -use r1csinstance::{ - R1CSCommitment, R1CSCommitmentGens, R1CSDecommitment, R1CSEvalProof, R1CSInstance, +use r1cs::{ + R1CSCommitment, R1CSCommitmentGens, R1CSDecommitment, R1CSEvalProof, R1CSShape, }; use r1csproof::{R1CSGens, R1CSProof}; use random::RandomTape; @@ -114,7 +114,7 @@ pub type InputsAssignment = Assignment; /// `Instance` holds the description of R1CS matrices and a hash of the matrices pub struct Instance { - inst: R1CSInstance, + inst: R1CSShape, digest: Vec, } @@ -214,7 +214,7 @@ impl Instance { return Err(C_scalar.err().unwrap()); } - let inst = R1CSInstance::new( + let inst = R1CSShape::new( num_cons_padded, num_vars_padded, num_inputs, @@ -228,7 +228,7 @@ impl Instance { Ok(Instance { inst, digest }) } - /// Checks if a given R1CSInstance is satisfiable with a given variables and inputs assignments + /// Checks if a given R1CSShape is satisfiable with a given variables and inputs assignments pub fn is_sat( &self, vars: &VarsAssignment, @@ -266,7 +266,7 @@ impl Instance { num_vars: usize, num_inputs: usize, ) -> (Instance, VarsAssignment, InputsAssignment) { - let (inst, vars, inputs) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); + let (inst, vars, inputs) = R1CSShape::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); let digest = inst.get_digest(); ( Instance { inst, digest }, @@ -513,7 +513,7 @@ impl NIZK { let mut random_tape = RandomTape::new(b"proof"); transcript.append_protocol_name(NIZK::protocol_name()); - transcript.append_message(b"R1CSInstanceDigest", &inst.digest); + transcript.append_message(b"R1CSShapeDigest", &inst.digest); let (r1cs_sat_proof, rx, ry) = { // we might need to pad variables @@ -558,7 +558,7 @@ impl NIZK { let timer_verify = Timer::new("NIZK::verify"); transcript.append_protocol_name(NIZK::protocol_name()); - transcript.append_message(b"R1CSInstanceDigest", &inst.digest); + transcript.append_message(b"R1CSShapeDigest", &inst.digest); // We send evaluations of A, B, C at r = (rx, ry) as claims // to enable the verifier complete the first sum-check @@ -601,10 +601,10 @@ mod tests { // produce public generators let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons); - // produce a synthetic R1CSInstance + // produce a synthetic R1CSShape let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); - // create a commitment to R1CSInstance + // create a commitment to R1CSShape let (comm, decomm) = SNARK::encode(&inst, &gens); // produce a proof diff --git a/src/r1csinstance.rs b/src/r1cs.rs similarity index 98% rename from src/r1csinstance.rs rename to src/r1cs.rs index 58706d93..db631ec3 100644 --- a/src/r1csinstance.rs +++ b/src/r1cs.rs @@ -16,7 +16,7 @@ use rand::rngs::OsRng; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -pub struct R1CSInstance { +pub struct R1CSShape { num_cons: usize, num_vars: usize, num_inputs: usize, @@ -83,7 +83,7 @@ impl R1CSCommitment { } } -impl R1CSInstance { +impl R1CSShape { pub fn new( num_cons: usize, num_vars: usize, @@ -91,7 +91,7 @@ impl R1CSInstance { A: &[(usize, usize, Scalar)], B: &[(usize, usize, Scalar)], C: &[(usize, usize, Scalar)], - ) -> R1CSInstance { + ) -> R1CSShape { Timer::print(&format!("number_of_constraints {num_cons}")); Timer::print(&format!("number_of_variables {num_vars}")); Timer::print(&format!("number_of_inputs {num_inputs}")); @@ -161,7 +161,7 @@ impl R1CSInstance { num_cons: usize, num_vars: usize, num_inputs: usize, - ) -> (R1CSInstance, Vec, Vec) { + ) -> (R1CSShape, Vec, Vec) { Timer::print(&format!("number_of_constraints {num_cons}")); Timer::print(&format!("number_of_variables {num_vars}")); Timer::print(&format!("number_of_inputs {num_inputs}")); @@ -223,7 +223,7 @@ impl R1CSInstance { let poly_B = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, B); let poly_C = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, C); - let inst = R1CSInstance { + let inst = R1CSShape { num_cons, num_vars, num_inputs, diff --git a/src/r1csproof.rs b/src/r1csproof.rs index 73c2abd2..711ecaa9 100644 --- a/src/r1csproof.rs +++ b/src/r1csproof.rs @@ -7,7 +7,7 @@ use super::errors::ProofVerifyError; use super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul}; use super::math::Math; use super::nizk::{EqualityProof, KnowledgeProof, ProductProof}; -use super::r1csinstance::R1CSInstance; +use super::r1cs::R1CSShape; use super::random::RandomTape; use super::scalar::Scalar; use super::sparse_mlpoly::{SparsePolyEntry, SparsePolynomial}; @@ -142,7 +142,7 @@ impl R1CSProof { } pub fn prove( - inst: &R1CSInstance, + inst: &R1CSShape, vars: Vec, input: &[Scalar], gens: &R1CSGens, @@ -495,7 +495,7 @@ mod tests { use super::*; use rand::rngs::OsRng; - fn produce_tiny_r1cs() -> (R1CSInstance, Vec, Vec) { + fn produce_tiny_r1cs() -> (R1CSShape, Vec, Vec) { // three constraints over five variables Z1, Z2, Z3, Z4, and Z5 // rounded to the nearest power of two let num_cons = 128; @@ -526,7 +526,7 @@ mod tests { A.push((2, 4, one)); B.push((2, num_vars, one)); - let inst = R1CSInstance::new(num_cons, num_vars, num_inputs, &A, &B, &C); + let inst = R1CSShape::new(num_cons, num_vars, num_inputs, &A, &B, &C); // compute a satisfying assignment let mut csprng: OsRng = OsRng; @@ -561,7 +561,7 @@ mod tests { #[test] fn test_synthetic_r1cs() { - let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(1024, 1024, 10); + let (inst, vars, input) = R1CSShape::produce_synthetic_r1cs(1024, 1024, 10); let is_sat = inst.is_sat(&vars, &input); assert!(is_sat); } @@ -571,7 +571,7 @@ mod tests { let num_vars = 1024; let num_cons = num_vars; let num_inputs = 10; - let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); + let (inst, vars, input) = R1CSShape::produce_synthetic_r1cs(num_cons, num_vars, num_inputs); let gens = R1CSGens::new(b"test-m", num_cons, num_vars); From 7a5e390c5898d9e5e5cc2149b8636f9d384bfa38 Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Fri, 23 May 2025 16:06:59 -0700 Subject: [PATCH 2/2] Cargo fmt --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d9d58cf7..64c99e96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,9 +33,7 @@ mod unipoly; use core::cmp::max; use errors::{ProofVerifyError, R1CSError}; use merlin::Transcript; -use r1cs::{ - R1CSCommitment, R1CSCommitmentGens, R1CSDecommitment, R1CSEvalProof, R1CSShape, -}; +use r1cs::{R1CSCommitment, R1CSCommitmentGens, R1CSDecommitment, R1CSEvalProof, R1CSShape}; use r1csproof::{R1CSGens, R1CSProof}; use random::RandomTape; use scalar::Scalar;