Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_uscs_ppzksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the USCS ppzkSNARK for
5  a given USCS example.
6 
7  See run_uscs_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_USCS_PPZKSNARK_TCC_
16 #define RUN_USCS_PPZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/uscs_ppzksnark/uscs_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 USCS 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 constraint system CS to create a proving and a verification key for CS.
32  * (2) The "prover", which runs the ppzkSNARK prover on input the proving key,
33  * a primary input for CS, and an auxiliary input for CS.
34  * (3) The "verifier", which runs the ppzkSNARK verifier on input the
35  * verification key, a primary input for CS, and a proof.
36  */
37 template<typename ppT>
38 bool run_uscs_ppzksnark(
39  const uscs_example<libff::Fr<ppT>> &example, const bool test_serialization)
40 {
41  libff::enter_block("Call to run_uscs_ppzksnark");
42 
43  libff::print_header("USCS ppzkSNARK Generator");
44  uscs_ppzksnark_keypair<ppT> keypair =
45  uscs_ppzksnark_generator<ppT>(example.constraint_system);
46  printf("\n");
47  libff::print_indent();
48  libff::print_mem("after generator");
49 
50  libff::print_header("Preprocess verification key");
51  uscs_ppzksnark_processed_verification_key<ppT> pvk =
52  uscs_ppzksnark_verifier_process_vk<ppT>(keypair.vk);
53 
54  if (test_serialization) {
55  libff::enter_block("Test serialization of keys");
56  keypair.pk =
57  libff::reserialize<uscs_ppzksnark_proving_key<ppT>>(keypair.pk);
58  keypair.vk = libff::reserialize<uscs_ppzksnark_verification_key<ppT>>(
59  keypair.vk);
60  pvk =
61  libff::reserialize<uscs_ppzksnark_processed_verification_key<ppT>>(
62  pvk);
63  libff::leave_block("Test serialization of keys");
64  }
65 
66  libff::print_header("USCS ppzkSNARK Prover");
67  uscs_ppzksnark_proof<ppT> proof = uscs_ppzksnark_prover<ppT>(
68  keypair.pk, example.primary_input, example.auxiliary_input);
69  printf("\n");
70  libff::print_indent();
71  libff::print_mem("after prover");
72 
73  if (test_serialization) {
74  libff::enter_block("Test serialization of proof");
75  proof = libff::reserialize<uscs_ppzksnark_proof<ppT>>(proof);
76  libff::leave_block("Test serialization of proof");
77  }
78 
79  libff::print_header("USCS ppzkSNARK Verifier");
80  bool ans = uscs_ppzksnark_verifier_strong_IC<ppT>(
81  keypair.vk, example.primary_input, proof);
82  printf("\n");
83  libff::print_indent();
84  libff::print_mem("after verifier");
85  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
86 
87  libff::print_header("USCS ppzkSNARK Online Verifier");
88  bool ans2 = uscs_ppzksnark_online_verifier_strong_IC<ppT>(
89  pvk, example.primary_input, proof);
90  assert(ans == ans2);
91  libff::UNUSED(ans2);
92 
93  libff::leave_block("Call to run_uscs_ppzksnark");
94 
95  return ans;
96 }
97 
98 } // namespace libsnark
99 
100 #endif // RUN_USCS_PPZKSNARK_TCC_