You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # Spartan: High-speed zkSNARKs without trusted setup
  2. ![Rust](https://github.com/microsoft/Spartan/workflows/Rust/badge.svg)
  3. Spartan 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/).
  4. A 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.
  5. Note that this library has *not* received a security review or audit.
  6. ## Highlights
  7. We now highlight Spartan's distinctive features.
  8. * **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.
  9. * **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.
  10. * **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.
  11. * **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`.
  12. * **State-of-the-art performance:**
  13. Among 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.
  14. ### Implementation details
  15. `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.
  16. ## Examples
  17. The following example shows how to use `libspartan` to create and verify a SNARK proof.
  18. Some of our public APIs' style is inspired by the underlying crates we use.
  19. ```rust
  20. # extern crate libspartan;
  21. # extern crate merlin;
  22. # use libspartan::{Instance, SNARKGens, SNARK};
  23. # use merlin::Transcript;
  24. # fn main() {
  25. // specify the size of an R1CS instance
  26. let num_vars = 1024;
  27. let num_cons = 1024;
  28. let num_inputs = 10;
  29. let num_non_zero_entries = 1024;
  30. // produce public parameters
  31. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
  32. // ask the library to produce a synthentic R1CS instance
  33. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  34. // create a commitment to the R1CS instance
  35. let (comm, decomm) = SNARK::encode(&inst, &gens);
  36. // produce a proof of satisfiability
  37. let mut prover_transcript = Transcript::new(b"snark_example");
  38. let proof = SNARK::prove(&inst, &decomm, vars, &inputs, &gens, &mut prover_transcript);
  39. // verify the proof of satisfiability
  40. let mut verifier_transcript = Transcript::new(b"snark_example");
  41. assert!(proof
  42. .verify(&comm, &inputs, &mut verifier_transcript, &gens)
  43. .is_ok());
  44. # }
  45. ```
  46. Here is another example to use the NIZK variant of the Spartan proof system:
  47. ```rust
  48. # extern crate libspartan;
  49. # extern crate merlin;
  50. # use libspartan::{Instance, NIZKGens, NIZK};
  51. # use merlin::Transcript;
  52. # fn main() {
  53. // specify the size of an R1CS instance
  54. let num_vars = 1024;
  55. let num_cons = 1024;
  56. let num_inputs = 10;
  57. // produce public parameters
  58. let gens = NIZKGens::new(num_cons, num_vars);
  59. // ask the library to produce a synthentic R1CS instance
  60. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  61. // produce a proof of satisfiability
  62. let mut prover_transcript = Transcript::new(b"nizk_example");
  63. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  64. // verify the proof of satisfiability
  65. let mut verifier_transcript = Transcript::new(b"nizk_example");
  66. assert!(proof
  67. .verify(&inst, &inputs, &mut verifier_transcript, &gens)
  68. .is_ok());
  69. # }
  70. ```
  71. Finally, we provide an example that specifies a custom R1CS instance instead of using a synthetic instance
  72. ```rust
  73. # extern crate curve25519_dalek;
  74. # extern crate libspartan;
  75. # extern crate merlin;
  76. # use curve25519_dalek::scalar::Scalar;
  77. # use libspartan::{InputsAssignment, Instance, SNARKGens, VarsAssignment, SNARK};
  78. # use merlin::Transcript;
  79. # use rand::rngs::OsRng;
  80. # fn main() {
  81. // produce a tiny instance
  82. let (
  83. num_cons,
  84. num_vars,
  85. num_inputs,
  86. num_non_zero_entries,
  87. inst,
  88. assignment_vars,
  89. assignment_inputs,
  90. ) = produce_tiny_r1cs();
  91. // produce public parameters
  92. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
  93. // create a commitment to the R1CS instance
  94. let (comm, decomm) = SNARK::encode(&inst, &gens);
  95. // produce a proof of satisfiability
  96. let mut prover_transcript = Transcript::new(b"snark_example");
  97. let proof = SNARK::prove(
  98. &inst,
  99. &decomm,
  100. assignment_vars,
  101. &assignment_inputs,
  102. &gens,
  103. &mut prover_transcript,
  104. );
  105. // verify the proof of satisfiability
  106. let mut verifier_transcript = Transcript::new(b"snark_example");
  107. assert!(proof
  108. .verify(&comm, &assignment_inputs, &mut verifier_transcript, &gens)
  109. .is_ok());
  110. # }
  111. # fn produce_tiny_r1cs() -> (
  112. # usize,
  113. # usize,
  114. # usize,
  115. # usize,
  116. # Instance,
  117. # VarsAssignment,
  118. # InputsAssignment,
  119. # ) {
  120. // We will use the following example, but one could construct any R1CS instance.
  121. // Our R1CS instance is three constraints over five variables and two public inputs
  122. // (Z0 + Z1) * I0 - Z2 = 0
  123. // (Z0 + I1) * Z2 - Z3 = 0
  124. // Z4 * 1 - 0 = 0
  125. // parameters of the R1CS instance rounded to the nearest power of two
  126. let num_cons = 4;
  127. let num_vars = 8;
  128. let num_inputs = 2;
  129. let num_non_zero_entries = 8;
  130. // We will encode the above constraints into three matrices, where
  131. // the coefficients in the matrix are in the little-endian byte order
  132. let mut A: Vec<(usize, usize, [u8; 32])> = Vec::new();
  133. let mut B: Vec<(usize, usize, [u8; 32])> = Vec::new();
  134. let mut C: Vec<(usize, usize, [u8; 32])> = Vec::new();
  135. // The constraint system is defined over a finite field, which in our case is
  136. // the scalar field of ristreeto255/curve25519 i.e., p = 2^{252}+27742317777372353535851937790883648493
  137. // To construct these matrices, we will use `curve25519-dalek` but one can use any other method.
  138. // a variable that holds a byte representation of 1
  139. let one = Scalar::one().to_bytes();
  140. // R1CS is a set of three sparse matrices A B C, where is a row for every
  141. // constraint and a column for every entry in z = (vars, 1, inputs)
  142. // An R1CS instance is satisfiable iff:
  143. // Az \circ Bz = Cz, where z = (vars, 1, inputs)
  144. // constraint 0 entries in (A,B,C)
  145. // constraint 0 is (Z0 + Z1) * I0 - Z2 = 0.
  146. // We set 1 in matrix A for columns that correspond to Z0 and Z1
  147. // We set 1 in matrix B for column that corresponds to I0
  148. // We set 1 in matrix C for column that corresponds to Z2
  149. A.push((0, 0, one));
  150. A.push((0, 1, one));
  151. B.push((0, num_vars + 1, one));
  152. C.push((0, 2, one));
  153. // constraint 1 entries in (A,B,C)
  154. A.push((1, 0, one));
  155. A.push((1, num_vars + 2, one));
  156. B.push((1, 2, one));
  157. C.push((1, 3, one));
  158. // constraint 3 entries in (A,B,C)
  159. A.push((2, 4, one));
  160. B.push((2, num_vars, one));
  161. let inst = Instance::new(num_cons, num_vars, num_inputs, &A, &B, &C).unwrap();
  162. // compute a satisfying assignment
  163. let mut csprng: OsRng = OsRng;
  164. let i0 = Scalar::random(&mut csprng);
  165. let i1 = Scalar::random(&mut csprng);
  166. let z0 = Scalar::random(&mut csprng);
  167. let z1 = Scalar::random(&mut csprng);
  168. let z2 = (z0 + z1) * i0; // constraint 0
  169. let z3 = (z0 + i1) * z2; // constraint 1
  170. let z4 = Scalar::zero(); //constraint 2
  171. // create a VarsAssignment
  172. let mut vars = vec![Scalar::zero().to_bytes(); num_vars];
  173. vars[0] = z0.to_bytes();
  174. vars[1] = z1.to_bytes();
  175. vars[2] = z2.to_bytes();
  176. vars[3] = z3.to_bytes();
  177. vars[4] = z4.to_bytes();
  178. let assignment_vars = VarsAssignment::new(&vars).unwrap();
  179. // create an InputsAssignment
  180. let mut inputs = vec![Scalar::zero().to_bytes(); num_inputs];
  181. inputs[0] = i0.to_bytes();
  182. inputs[1] = i1.to_bytes();
  183. let assignment_inputs = InputsAssignment::new(&inputs).unwrap();
  184. // check if the instance we created is satisfiable
  185. let res = inst.is_sat(&assignment_vars, &assignment_inputs);
  186. assert_eq!(res.unwrap(), true);
  187. (
  188. num_cons,
  189. num_vars,
  190. num_inputs,
  191. num_non_zero_entries,
  192. inst,
  193. assignment_vars,
  194. assignment_inputs,
  195. )
  196. # }
  197. ```
  198. ## Building `libspartan`
  199. Install [`rustup`](https://rustup.rs/)
  200. Switch to nightly Rust using `rustup`:
  201. ```text
  202. rustup default nightly
  203. ```
  204. Clone the repository:
  205. ```text
  206. git clone https://github.com/Microsoft/Spartan
  207. cd Spartan
  208. ```
  209. To build docs for public APIs of `libspartan`:
  210. ```text
  211. cargo doc
  212. ```
  213. To run tests:
  214. ```text
  215. RUSTFLAGS="-C target_cpu=native" cargo test
  216. ```
  217. To build `libspartan`:
  218. ```text
  219. RUSTFLAGS="-C target_cpu=native" cargo build --release
  220. ```
  221. > 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`.
  222. ### Supported features
  223. * `profile`: enables fine-grained profiling information (see below for its use)
  224. ## Performance
  225. ### End-to-end benchmarks
  226. `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).
  227. To run end-to-end benchmarks:
  228. ```text
  229. RUSTFLAGS="-C target_cpu=native" cargo bench
  230. ```
  231. ### Fine-grained profiling
  232. Build `libspartan` with `profile` feature enabled. It creates two profilers: `./target/release/snark` and `./target/release/nizk`.
  233. These profilers report performance as depicted below (for varying R1CS instance sizes). The reported
  234. performance 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).
  235. See Section 9 in our [paper](https://eprint.iacr.org/2019/550) to see how this compares with other zkSNARKs in the literature.
  236. ```text
  237. $ ./target/release/snark
  238. Profiler:: SNARK
  239. * number_of_constraints 1048576
  240. * number_of_variables 1048576
  241. * number_of_inputs 10
  242. * number_non-zero_entries_A 1048576
  243. * number_non-zero_entries_B 1048576
  244. * number_non-zero_entries_C 1048576
  245. * SNARK::encode
  246. * SNARK::encode 14.2644201s
  247. * SNARK::prove
  248. * R1CSProof::prove
  249. * polycommit
  250. * polycommit 2.7175848s
  251. * prove_sc_phase_one
  252. * prove_sc_phase_one 683.7481ms
  253. * prove_sc_phase_two
  254. * prove_sc_phase_two 846.1056ms
  255. * polyeval
  256. * polyeval 193.4216ms
  257. * R1CSProof::prove 4.4416193s
  258. * len_r1cs_sat_proof 47024
  259. * eval_sparse_polys
  260. * eval_sparse_polys 377.357ms
  261. * R1CSEvalProof::prove
  262. * commit_nondet_witness
  263. * commit_nondet_witness 14.4507331s
  264. * build_layered_network
  265. * build_layered_network 3.4360521s
  266. * evalproof_layered_network
  267. * len_product_layer_proof 64712
  268. * evalproof_layered_network 15.5708066s
  269. * R1CSEvalProof::prove 34.2930559s
  270. * len_r1cs_eval_proof 133720
  271. * SNARK::prove 39.1297568s
  272. * SNARK::proof_compressed_len 141768
  273. * SNARK::verify
  274. * verify_sat_proof
  275. * verify_sat_proof 20.0828ms
  276. * verify_eval_proof
  277. * verify_polyeval_proof
  278. * verify_prod_proof
  279. * verify_prod_proof 1.1847ms
  280. * verify_hash_proof
  281. * verify_hash_proof 81.06ms
  282. * verify_polyeval_proof 82.3583ms
  283. * verify_eval_proof 82.8937ms
  284. * SNARK::verify 103.0536ms
  285. ```
  286. ```text
  287. $ ./target/release/nizk
  288. Profiler:: NIZK
  289. * number_of_constraints 1048576
  290. * number_of_variables 1048576
  291. * number_of_inputs 10
  292. * number_non-zero_entries_A 1048576
  293. * number_non-zero_entries_B 1048576
  294. * number_non-zero_entries_C 1048576
  295. * NIZK::prove
  296. * R1CSProof::prove
  297. * polycommit
  298. * polycommit 2.7220635s
  299. * prove_sc_phase_one
  300. * prove_sc_phase_one 722.5487ms
  301. * prove_sc_phase_two
  302. * prove_sc_phase_two 862.6796ms
  303. * polyeval
  304. * polyeval 190.2233ms
  305. * R1CSProof::prove 4.4982305s
  306. * len_r1cs_sat_proof 47024
  307. * NIZK::prove 4.5139888s
  308. * NIZK::proof_compressed_len 48134
  309. * NIZK::verify
  310. * eval_sparse_polys
  311. * eval_sparse_polys 395.0847ms
  312. * verify_sat_proof
  313. * verify_sat_proof 19.286ms
  314. * NIZK::verify 414.5102ms
  315. ```
  316. ## LICENSE
  317. See [LICENSE](./LICENSE)
  318. ## Contributing
  319. See [CONTRIBUTING](./CONTRIBUTING.md)