Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_r1cs_se_ppzksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the R1CS SEppzkSNARK for
5  a given R1CS example.
6 
7  See run_r1cs_se_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_R1CS_SE_PPZKSNARK_TCC_
16 #define RUN_R1CS_SE_PPZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/r1cs_se_ppzksnark/r1cs_se_ppzksnark.hpp>
20 #include <sstream>
21 #include <type_traits>
22 
23 namespace libsnark
24 {
25 
26 /**
27  * The code below provides an example of all stages of running a R1CS
28  * SEppzkSNARK.
29  *
30  * Of course, in a real-life scenario, we would have three distinct entities,
31  * mangled into one in the demonstration below. The three entities are as
32  * follows. (1) The "generator", which runs the SEppzkSNARK generator on input a
33  * given constraint system CS to create a proving and a verification key for CS.
34  * (2) The "prover", which runs the SEppzkSNARK prover on input the proving key,
35  * a primary input for CS, and an auxiliary input for CS.
36  * (3) The "verifier", which runs the SEppzkSNARK verifier on input the
37  * verification key, a primary input for CS, and a proof.
38  */
39 template<typename ppT>
40 bool run_r1cs_se_ppzksnark(
41  const r1cs_example<libff::Fr<ppT>> &example, const bool test_serialization)
42 {
43  libff::enter_block("Call to run_r1cs_se_ppzksnark");
44 
45  libff::print_header("R1CS SEppzkSNARK Generator");
46  r1cs_se_ppzksnark_keypair<ppT> keypair =
47  r1cs_se_ppzksnark_generator<ppT>(example.constraint_system);
48  printf("\n");
49  libff::print_indent();
50  libff::print_mem("after generator");
51 
52  libff::print_header("Preprocess verification key");
53  r1cs_se_ppzksnark_processed_verification_key<ppT> pvk =
54  r1cs_se_ppzksnark_verifier_process_vk<ppT>(keypair.vk);
55 
56  if (test_serialization) {
57  libff::enter_block("Test serialization of keys");
58  keypair.pk =
59  libff::reserialize<r1cs_se_ppzksnark_proving_key<ppT>>(keypair.pk);
60  keypair.vk =
61  libff::reserialize<r1cs_se_ppzksnark_verification_key<ppT>>(
62  keypair.vk);
63  pvk = libff::reserialize<
64  r1cs_se_ppzksnark_processed_verification_key<ppT>>(pvk);
65  libff::leave_block("Test serialization of keys");
66  }
67 
68  libff::print_header("R1CS SEppzkSNARK Prover");
69  r1cs_se_ppzksnark_proof<ppT> proof = r1cs_se_ppzksnark_prover<ppT>(
70  keypair.pk, example.primary_input, example.auxiliary_input);
71  printf("\n");
72  libff::print_indent();
73  libff::print_mem("after prover");
74 
75  if (test_serialization) {
76  libff::enter_block("Test serialization of proof");
77  proof = libff::reserialize<r1cs_se_ppzksnark_proof<ppT>>(proof);
78  libff::leave_block("Test serialization of proof");
79  }
80 
81  libff::print_header("R1CS SEppzkSNARK Verifier");
82  const bool ans = r1cs_se_ppzksnark_verifier_strong_IC<ppT>(
83  keypair.vk, example.primary_input, proof);
84  printf("\n");
85  libff::print_indent();
86  libff::print_mem("after verifier");
87  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
88 
89  libff::print_header("R1CS SEppzkSNARK Online Verifier");
90  const bool ans2 = r1cs_se_ppzksnark_online_verifier_strong_IC<ppT>(
91  pvk, example.primary_input, proof);
92  assert(ans == ans2);
93  libff::UNUSED(ans2);
94 
95  libff::leave_block("Call to run_r1cs_se_ppzksnark");
96 
97  return ans;
98 }
99 
100 } // namespace libsnark
101 
102 #endif // RUN_R1CS_SE_PPZKSNARK_TCC_