testudo/README.md
README.mdRaw
1# Spartan: High-speed zkSNARKs without trusted setup
2
3
4
5
6Spartan is a high-speed zero-knowledge proof system, a cryptographic primitive that enables a prover to prove a mathematical statement to a verifier without revealing anything besides the validity of the statement. This repository provides `libspartan,` a Rust library that implements a zero-knowledge succinct non-interactive argument of knowledge (zkSNARK), which is a type of zero-knowledge proof system with short proofs and fast verification times. The details of the Spartan proof system are described in our [paper](https://eprint.iacr.org/2019/550) published at [CRYPTO 2020](https://crypto.iacr.org/2020/). The security of the Spartan variant implemented in this library is based on the discrete logarithm problem in the random oracle model.
7
8A simple example application is proving the knowledge of a secret s such that H(s) == d for a public d, where H is a cryptographic hash function (e.g., SHA-256, Keccak). A more complex application is a database-backed cloud service that produces proofs of correct state machine transitions for auditability. See this [paper](https://eprint.iacr.org/2020/758.pdf) for an overview and this [paper](https://eprint.iacr.org/2018/907.pdf) for details.
9
10Note that this library has *not* received a security review or audit.
11
12## Highlights
13We now highlight Spartan's distinctive features.
14
15* **No "toxic" waste:** Spartan is a *transparent* zkSNARK and does not require a trusted setup. So, it does not involve any trapdoors that must be kept secret or require a multi-party ceremony to produce public parameters.
16
17* **General-purpose:** Spartan produces proofs for arbitrary NP statements. `libspartan` supports NP statements expressed as rank-1 constraint satisfiability (R1CS) instances, a popular language for which there exists efficient transformations and compiler toolchains from high-level programs of interest.
18
19* **Sub-linear verification costs and linear-time proving costs:** Spartan is the first transparent proof system with sub-linear verification costs for arbitrary NP statements (e.g., R1CS). Spartan also features a linear-time prover, a property that has remained elusive for nearly all zkSNARKs in the literature.
20
21* **Standardized security:** Spartan's security relies on the hardness of computing discrete logarithms (a standard cryptographic assumption) in the random oracle model. `libspartan` uses `ristretto255`, a prime-order group abstraction atop `curve25519` (a high-speed elliptic curve). We use [`curve25519-dalek`](https://docs.rs/curve25519-dalek) for arithmetic over `ristretto255`.
22
23* **State-of-the-art performance:**
24Among transparent SNARKs, Spartan offers the fastest prover with speedups of 36–152× depending on the baseline, produces proofs that are shorter by 1.2–416×, and incurs the lowest verification times with speedups of 3.6–1326×. The only exception is proof sizes under Bulletproofs, but Bulletproofs incurs slower verification both asymptotically and concretely. When compared to the state-of-the-art zkSNARK with trusted setup, Spartan’s prover is 2× faster for arbitrary R1CS instances and 16× faster for data-parallel workloads.
25
26### Implementation details
27`libspartan` uses [`merlin`](https://docs.rs/merlin/) to automate the Fiat-Shamir transform. We also introduce a new type called `RandomTape` that extends a `Transcript` in `merlin` to allow the prover's internal methods to produce private randomness using its private transcript without having to create `OsRng` objects throughout the code. An object of type `RandomTape` is initialized with a new random seed from `OsRng` for each proof produced by the library.
28
29## Examples
30The following example shows how to use `libspartan` to create and verify a SNARK proof.
31Some of our public APIs' style is inspired by the underlying crates we use.
32
33```rust
34# extern crate libspartan;
35# extern crate merlin;
36# use libspartan::{Instance, SNARKGens, SNARK};
37# use merlin::Transcript;
38# fn main() {
39 // specify the size of an R1CS instance
40 let num_vars = 1024;
41 let num_cons = 1024;
42 let num_inputs = 10;
43 let num_non_zero_entries = 1024;
44
45 // produce public parameters
46 let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
47
48 // ask the library to produce a synthentic R1CS instance
49 let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
50
51 // create a commitment to the R1CS instance
52 let (comm, decomm) = SNARK::encode(&inst, &gens);
53
54 // produce a proof of satisfiability
55 let mut prover_transcript = Transcript::new(b"snark_example");
56 let proof = SNARK::prove(&inst, &decomm, vars, &inputs, &gens, &mut prover_transcript);
57
58 // verify the proof of satisfiability
59 let mut verifier_transcript = Transcript::new(b"snark_example");
60 assert!(proof
61 .verify(&comm, &inputs, &mut verifier_transcript, &gens)
62 .is_ok());
63# }
64```
65
66Here is another example to use the NIZK variant of the Spartan proof system:
67```rust
68# extern crate libspartan;
69# extern crate merlin;
70# use libspartan::{Instance, NIZKGens, NIZK};
71# use merlin::Transcript;
72# fn main() {
73 // specify the size of an R1CS instance
74 let num_vars = 1024;
75 let num_cons = 1024;
76 let num_inputs = 10;
77
78 // produce public parameters
79 let gens = NIZKGens::new(num_cons, num_vars);
80
81 // ask the library to produce a synthentic R1CS instance
82 let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
83
84 // produce a proof of satisfiability
85 let mut prover_transcript = Transcript::new(b"nizk_example");
86 let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
87
88 // verify the proof of satisfiability
89 let mut verifier_transcript = Transcript::new(b"nizk_example");
90 assert!(proof
91 .verify(&inst, &inputs, &mut verifier_transcript, &gens)
92 .is_ok());
93# }
94```
95
96Finally, we provide an example that specifies a custom R1CS instance instead of using a synthetic instance
97```rust
98# extern crate curve25519_dalek;
99# extern crate libspartan;
100# extern crate merlin;
101# use curve25519_dalek::scalar::Scalar;
102# use libspartan::{InputsAssignment, Instance, SNARKGens, VarsAssignment, SNARK};
103# use merlin::Transcript;
104# use rand::rngs::OsRng;
105# fn main() {
106 // produce a tiny instance
107 let (
108 num_cons,
109 num_vars,
110 num_inputs,
111 num_non_zero_entries,
112 inst,
113 assignment_vars,
114 assignment_inputs,
115 ) = produce_tiny_r1cs();
116
117 // produce public parameters
118 let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
119
120 // create a commitment to the R1CS instance
121 let (comm, decomm) = SNARK::encode(&inst, &gens);
122
123 // produce a proof of satisfiability
124 let mut prover_transcript = Transcript::new(b"snark_example");
125 let proof = SNARK::prove(
126 &inst,
127 &decomm,
128 assignment_vars,
129 &assignment_inputs,
130 &gens,
131 &mut prover_transcript,
132 );
133
134 // verify the proof of satisfiability
135 let mut verifier_transcript = Transcript::new(b"snark_example");
136 assert!(proof
137 .verify(&comm, &assignment_inputs, &mut verifier_transcript, &gens)
138 .is_ok());
139# }
140
141# fn produce_tiny_r1cs() -> (
142# usize,
143# usize,
144# usize,
145# usize,
146# Instance,
147# VarsAssignment,
148# InputsAssignment,
149# ) {
150 // We will use the following example, but one could construct any R1CS instance.
151 // Our R1CS instance is three constraints over five variables and two public inputs
152 // (Z0 + Z1) * I0 - Z2 = 0
153 // (Z0 + I1) * Z2 - Z3 = 0
154 // Z4 * 1 - 0 = 0
155
156 // parameters of the R1CS instance rounded to the nearest power of two
157 let num_cons = 4;
158 let num_vars = 8;
159 let num_inputs = 2;
160 let num_non_zero_entries = 8;
161
162 // We will encode the above constraints into three matrices, where
163 // the coefficients in the matrix are in the little-endian byte order
164 let mut A: Vec<(usize, usize, [u8; 32])> = Vec::new();
165 let mut B: Vec<(usize, usize, [u8; 32])> = Vec::new();
166 let mut C: Vec<(usize, usize, [u8; 32])> = Vec::new();
167
168 // The constraint system is defined over a finite field, which in our case is
169 // the scalar field of ristreeto255/curve25519 i.e., p = 2^{252}+27742317777372353535851937790883648493
170 // To construct these matrices, we will use `curve25519-dalek` but one can use any other method.
171
172 // a variable that holds a byte representation of 1
173 let one = Scalar::one().to_bytes();
174
175 // R1CS is a set of three sparse matrices A B C, where is a row for every
176 // constraint and a column for every entry in z = (vars, 1, inputs)
177 // An R1CS instance is satisfiable iff:
178 // Az \circ Bz = Cz, where z = (vars, 1, inputs)
179
180 // constraint 0 entries in (A,B,C)
181 // constraint 0 is (Z0 + Z1) * I0 - Z2 = 0.
182 // We set 1 in matrix A for columns that correspond to Z0 and Z1
183 // We set 1 in matrix B for column that corresponds to I0
184 // We set 1 in matrix C for column that corresponds to Z2
185 A.push((0, 0, one));
186 A.push((0, 1, one));
187 B.push((0, num_vars + 1, one));
188 C.push((0, 2, one));
189
190 // constraint 1 entries in (A,B,C)
191 A.push((1, 0, one));
192 A.push((1, num_vars + 2, one));
193 B.push((1, 2, one));
194 C.push((1, 3, one));
195
196 // constraint 3 entries in (A,B,C)
197 A.push((2, 4, one));
198 B.push((2, num_vars, one));
199
200 let inst = Instance::new(num_cons, num_vars, num_inputs, &A, &B, &C).unwrap();
201
202 // compute a satisfying assignment
203 let mut csprng: OsRng = OsRng;
204 let i0 = Scalar::random(&mut csprng);
205 let i1 = Scalar::random(&mut csprng);
206 let z0 = Scalar::random(&mut csprng);
207 let z1 = Scalar::random(&mut csprng);
208 let z2 = (z0 + z1) * i0; // constraint 0
209 let z3 = (z0 + i1) * z2; // constraint 1
210 let z4 = Scalar::zero(); //constraint 2
211
212 // create a VarsAssignment
213 let mut vars = vec![Scalar::zero().to_bytes(); num_vars];
214 vars[0] = z0.to_bytes();
215 vars[1] = z1.to_bytes();
216 vars[2] = z2.to_bytes();
217 vars[3] = z3.to_bytes();
218 vars[4] = z4.to_bytes();
219 let assignment_vars = VarsAssignment::new(&vars).unwrap();
220
221 // create an InputsAssignment
222 let mut inputs = vec![Scalar::zero().to_bytes(); num_inputs];
223 inputs[0] = i0.to_bytes();
224 inputs[1] = i1.to_bytes();
225 let assignment_inputs = InputsAssignment::new(&inputs).unwrap();
226
227 // check if the instance we created is satisfiable
228 let res = inst.is_sat(&assignment_vars, &assignment_inputs);
229 assert_eq!(res.unwrap(), true);
230
231 (
232 num_cons,
233 num_vars,
234 num_inputs,
235 num_non_zero_entries,
236 inst,
237 assignment_vars,
238 assignment_inputs,
239 )
240# }
241```
242
243## Building `libspartan`
244Install [`rustup`](https://rustup.rs/)
245
246Switch to nightly Rust using `rustup`:
247```text
248rustup default nightly
249```
250
251Clone the repository:
252```text
253git clone https://github.com/Microsoft/Spartan
254cd Spartan
255```
256
257To build docs for public APIs of `libspartan`:
258```text
259cargo doc
260```
261
262To run tests:
263```text
264RUSTFLAGS="-C target_cpu=native" cargo test
265```
266
267To build `libspartan`:
268```text
269RUSTFLAGS="-C target_cpu=native" cargo build --release
270```
271
272> 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`.
273
274### Supported features
275* `profile`: enables fine-grained profiling information (see below for its use)
276
277## Performance
278
279### End-to-end benchmarks
280`libspartan` includes two benches: `benches/nizk.rs` and `benches/snark.rs`. If you report the performance of Spartan in a research paper, we recommend using these benches for higher accuracy instead of fine-grained profiling (listed below).
281
282To run end-to-end benchmarks:
283```text
284RUSTFLAGS="-C target_cpu=native" cargo bench
285```
286
287### Fine-grained profiling
288Build `libspartan` with `profile` feature enabled. It creates two profilers: `./target/release/snark` and `./target/release/nizk`.
289
290These profilers report performance as depicted below (for varying R1CS instance sizes). The reported
291performance is from running the profilers on a Microsoft Surface Laptop 3 on a single CPU core of Intel Core i7-1065G7 running Ubuntu 20.04 (atop WSL2 on Windows 10).
292See Section 9 in our [paper](https://eprint.iacr.org/2019/550) to see how this compares with other zkSNARKs in the literature.
293
294```text
295$ ./target/release/snark
296Profiler:: SNARK
297 * number_of_constraints 1048576
298 * number_of_variables 1048576
299 * number_of_inputs 10
300 * number_non-zero_entries_A 1048576
301 * number_non-zero_entries_B 1048576
302 * number_non-zero_entries_C 1048576
303 * SNARK::encode
304 * SNARK::encode 14.2644201s
305 * SNARK::prove
306 * R1CSProof::prove
307 * polycommit
308 * polycommit 2.7175848s
309 * prove_sc_phase_one
310 * prove_sc_phase_one 683.7481ms
311 * prove_sc_phase_two
312 * prove_sc_phase_two 846.1056ms
313 * polyeval
314 * polyeval 193.4216ms
315 * R1CSProof::prove 4.4416193s
316 * len_r1cs_sat_proof 47024
317 * eval_sparse_polys
318 * eval_sparse_polys 377.357ms
319 * R1CSEvalProof::prove
320 * commit_nondet_witness
321 * commit_nondet_witness 14.4507331s
322 * build_layered_network
323 * build_layered_network 3.4360521s
324 * evalproof_layered_network
325 * len_product_layer_proof 64712
326 * evalproof_layered_network 15.5708066s
327 * R1CSEvalProof::prove 34.2930559s
328 * len_r1cs_eval_proof 133720
329 * SNARK::prove 39.1297568s
330 * SNARK::proof_compressed_len 141768
331 * SNARK::verify
332 * verify_sat_proof
333 * verify_sat_proof 20.0828ms
334 * verify_eval_proof
335 * verify_polyeval_proof
336 * verify_prod_proof
337 * verify_prod_proof 1.1847ms
338 * verify_hash_proof
339 * verify_hash_proof 81.06ms
340 * verify_polyeval_proof 82.3583ms
341 * verify_eval_proof 82.8937ms
342 * SNARK::verify 103.0536ms
343```
344
345```text
346$ ./target/release/nizk
347Profiler:: NIZK
348 * number_of_constraints 1048576
349 * number_of_variables 1048576
350 * number_of_inputs 10
351 * number_non-zero_entries_A 1048576
352 * number_non-zero_entries_B 1048576
353 * number_non-zero_entries_C 1048576
354 * NIZK::prove
355 * R1CSProof::prove
356 * polycommit
357 * polycommit 2.7220635s
358 * prove_sc_phase_one
359 * prove_sc_phase_one 722.5487ms
360 * prove_sc_phase_two
361 * prove_sc_phase_two 862.6796ms
362 * polyeval
363 * polyeval 190.2233ms
364 * R1CSProof::prove 4.4982305s
365 * len_r1cs_sat_proof 47024
366 * NIZK::prove 4.5139888s
367 * NIZK::proof_compressed_len 48134
368 * NIZK::verify
369 * eval_sparse_polys
370 * eval_sparse_polys 395.0847ms
371 * verify_sat_proof
372 * verify_sat_proof 19.286ms
373 * NIZK::verify 414.5102ms
374```
375
376## LICENSE
377
378See [LICENSE](./LICENSE)
379
380## Contributing
381
382See [CONTRIBUTING](./CONTRIBUTING.md)