Skip to content
Merged
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion profiler/nizk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions profiler/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod group;
mod math;
mod nizk;
mod product_tree;
mod r1csinstance;
mod r1cs;
mod r1csproof;
mod random;
mod scalar;
Expand All @@ -33,9 +33,7 @@ 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;
use scalar::Scalar;
Expand Down Expand Up @@ -114,7 +112,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<u8>,
}

Expand Down Expand Up @@ -214,7 +212,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,
Expand All @@ -228,7 +226,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,
Expand Down Expand Up @@ -266,7 +264,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 },
Expand Down Expand Up @@ -513,7 +511,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
Expand Down Expand Up @@ -558,7 +556,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
Expand Down Expand Up @@ -601,10 +599,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
Expand Down
10 changes: 5 additions & 5 deletions src/r1csinstance.rs → src/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -83,15 +83,15 @@ impl R1CSCommitment {
}
}

impl R1CSInstance {
impl R1CSShape {
pub fn new(
num_cons: usize,
num_vars: usize,
num_inputs: usize,
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}"));
Expand Down Expand Up @@ -161,7 +161,7 @@ impl R1CSInstance {
num_cons: usize,
num_vars: usize,
num_inputs: usize,
) -> (R1CSInstance, Vec<Scalar>, Vec<Scalar>) {
) -> (R1CSShape, Vec<Scalar>, Vec<Scalar>) {
Timer::print(&format!("number_of_constraints {num_cons}"));
Timer::print(&format!("number_of_variables {num_vars}"));
Timer::print(&format!("number_of_inputs {num_inputs}"));
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/r1csproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -142,7 +142,7 @@ impl R1CSProof {
}

pub fn prove(
inst: &R1CSInstance,
inst: &R1CSShape,
vars: Vec<Scalar>,
input: &[Scalar],
gens: &R1CSGens,
Expand Down Expand Up @@ -495,7 +495,7 @@ mod tests {
use super::*;
use rand::rngs::OsRng;

fn produce_tiny_r1cs() -> (R1CSInstance, Vec<Scalar>, Vec<Scalar>) {
fn produce_tiny_r1cs() -> (R1CSShape, Vec<Scalar>, Vec<Scalar>) {
// three constraints over five variables Z1, Z2, Z3, Z4, and Z5
// rounded to the nearest power of two
let num_cons = 128;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
Loading