Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_ram_zksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the RAM zkSNARK for
5  a given RAM example.
6 
7  See run_ram_zksnark.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_ZKSNARK_TCC_
16 #define RUN_RAM_ZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/zksnark/ram_zksnark/ram_zksnark.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 zkSNARK.
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 zkSNARK generator on input a
31  * given architecture. (2) The "prover", which runs the zkSNARK prover on input
32  * the proving key, a boot trace, and an auxiliary input. (3) The "verifier",
33  * which runs the zkSNARK verifier on input the verification key, a boot trace,
34  * a time bound, and a proof.
35  */
36 template<typename ram_zksnark_ppT>
37 bool run_ram_zksnark(
38  const ram_example<ram_zksnark_machine_pp<ram_zksnark_ppT>> &example,
39  const bool test_serialization)
40 {
41  libff::enter_block("Call to run_ram_zksnark");
42 
43  printf("This run uses an example with the following parameters:\n");
44  example.ap.print();
45  printf("* Time bound (T): %zu\n", example.time_bound);
46 
47  libff::print_header("RAM zkSNARK Generator");
48  ram_zksnark_keypair<ram_zksnark_ppT> keypair =
49  ram_zksnark_generator<ram_zksnark_ppT>(example.ap);
50  printf("\n");
51  libff::print_indent();
52  libff::print_mem("after generator");
53 
54  if (test_serialization) {
55  libff::enter_block("Test serialization of keys");
56  keypair.pk =
57  libff::reserialize<ram_zksnark_proving_key<ram_zksnark_ppT>>(
58  keypair.pk);
59  keypair.vk =
60  libff::reserialize<ram_zksnark_verification_key<ram_zksnark_ppT>>(
61  keypair.vk);
62  libff::leave_block("Test serialization of keys");
63  }
64 
65  libff::print_header("RAM zkSNARK Prover");
66  ram_zksnark_proof<ram_zksnark_ppT> proof =
67  ram_zksnark_prover<ram_zksnark_ppT>(
68  keypair.pk,
69  example.boot_trace,
70  example.time_bound,
71  example.auxiliary_input);
72  printf("\n");
73  libff::print_indent();
74  libff::print_mem("after prover");
75 
76  if (test_serialization) {
77  libff::enter_block("Test serialization of proof");
78  proof = libff::reserialize<ram_zksnark_proof<ram_zksnark_ppT>>(proof);
79  libff::leave_block("Test serialization of proof");
80  }
81 
82  libff::print_header("RAM zkSNARK Verifier");
83  bool ans = ram_zksnark_verifier<ram_zksnark_ppT>(
84  keypair.vk, example.boot_trace, example.time_bound, proof);
85  printf("\n");
86  libff::print_indent();
87  libff::print_mem("after verifier");
88  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
89 
90  libff::leave_block("Call to run_ram_zksnark");
91 
92  return ans;
93 }
94 
95 } // namespace libsnark
96 
97 #endif // RUN_RAM_ZKSNARK_TCC_