Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_ram_ppzksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the RAM ppzkSNARK for
5  a given RAM example.
6 
7  See run_ram_ppzksnark.hpp .
8 
9  *****************************************************************************
10  * @author This file is part of libsnark, developed by SCIPR Lab
11  * and contributors (see AUTHORS).
12  * @copyright MIT license (see LICENSE file)
13  *****************************************************************************/
14 
15 #ifndef RUN_RAM_PPZKSNARK_TCC_
16 #define RUN_RAM_PPZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/ram_ppzksnark.hpp>
20 #include <sstream>
21 
22 namespace libsnark
23 {
24 
25 /**
26  * The code below provides an example of all stages of running a RAM ppzkSNARK.
27  *
28  * Of course, in a real-life scenario, we would have three distinct entities,
29  * mangled into one in the demonstration below. The three entities are as
30  * follows. (1) The "generator", which runs the ppzkSNARK generator on input a
31  * given architecture and bounds on the computation. (2) The "prover", which
32  * runs the ppzkSNARK prover on input the proving key, a boot trace, and an
33  * auxiliary input. (3) The "verifier", which runs the ppzkSNARK verifier on
34  * input the verification key, a boot trace, and a proof.
35  */
36 template<typename ram_ppzksnark_ppT>
37 bool run_ram_ppzksnark(
38  const ram_example<ram_ppzksnark_machine_pp<ram_ppzksnark_ppT>> &example,
39  const bool test_serialization)
40 {
41  libff::enter_block("Call to run_ram_ppzksnark");
42 
43  printf("This run uses an example with the following parameters:\n");
44  example.ap.print();
45  printf(
46  "* Primary input size bound (L): %zu\n", example.boot_trace_size_bound);
47  printf("* Time bound (T): %zu\n", example.time_bound);
48  printf(
49  "Hence, libff::log2(L+2*T) equals %zu\n",
50  libff::log2(example.boot_trace_size_bound + 2 * example.time_bound));
51 
52  libff::print_header("RAM ppzkSNARK Generator");
53  ram_ppzksnark_keypair<ram_ppzksnark_ppT> keypair =
54  ram_ppzksnark_generator<ram_ppzksnark_ppT>(
55  example.ap, example.boot_trace_size_bound, example.time_bound);
56  printf("\n");
57  libff::print_indent();
58  libff::print_mem("after generator");
59 
60  if (test_serialization) {
61  libff::enter_block("Test serialization of keys");
62  keypair.pk =
63  libff::reserialize<ram_ppzksnark_proving_key<ram_ppzksnark_ppT>>(
64  keypair.pk);
65  keypair.vk = libff::reserialize<
66  ram_ppzksnark_verification_key<ram_ppzksnark_ppT>>(keypair.vk);
67  libff::leave_block("Test serialization of keys");
68  }
69 
70  libff::print_header("RAM ppzkSNARK Prover");
71  ram_ppzksnark_proof<ram_ppzksnark_ppT> proof =
72  ram_ppzksnark_prover<ram_ppzksnark_ppT>(
73  keypair.pk, example.boot_trace, example.auxiliary_input);
74  printf("\n");
75  libff::print_indent();
76  libff::print_mem("after prover");
77 
78  if (test_serialization) {
79  libff::enter_block("Test serialization of proof");
80  proof =
81  libff::reserialize<ram_ppzksnark_proof<ram_ppzksnark_ppT>>(proof);
82  libff::leave_block("Test serialization of proof");
83  }
84 
85  libff::print_header("RAM ppzkSNARK Verifier");
86  bool ans = ram_ppzksnark_verifier<ram_ppzksnark_ppT>(
87  keypair.vk, example.boot_trace, proof);
88  printf("\n");
89  libff::print_indent();
90  libff::print_mem("after verifier");
91  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
92 
93  libff::leave_block("Call to run_ram_ppzksnark");
94 
95  return ans;
96 }
97 
98 } // namespace libsnark
99 
100 #endif // RUN_RAM_PPZKSNARK_TCC_