Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_r1cs_ppzksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the R1CS ppzkSNARK for
5  a given R1CS example.
6 
7  See run_r1cs_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_PPZKSNARK_TCC_
16 #define RUN_R1CS_PPZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp>
20 #include <sstream>
21 #include <type_traits>
22 
23 namespace libsnark
24 {
25 
26 template<typename ppT>
27 typename std::enable_if<ppT::has_affine_pairing, void>::type test_affine_verifier(
28  const r1cs_ppzksnark_verification_key<ppT> &vk,
29  const r1cs_ppzksnark_primary_input<ppT> &primary_input,
30  const r1cs_ppzksnark_proof<ppT> &proof,
31  const bool expected_answer)
32 {
33  libff::print_header("R1CS ppzkSNARK Affine Verifier");
34  const bool answer =
35  r1cs_ppzksnark_affine_verifier_weak_IC<ppT>(vk, primary_input, proof);
36  assert(answer == expected_answer);
37  libff::UNUSED(answer);
38  libff::UNUSED(expected_answer);
39 }
40 
41 template<typename ppT>
42 typename std::enable_if<!ppT::has_affine_pairing, void>::type test_affine_verifier(
43  const r1cs_ppzksnark_verification_key<ppT> &vk,
44  const r1cs_ppzksnark_primary_input<ppT> &primary_input,
45  const r1cs_ppzksnark_proof<ppT> &proof,
46  const bool expected_answer)
47 {
48  libff::UNUSED(vk, primary_input, proof, expected_answer);
49  libff::print_header("R1CS ppzkSNARK Affine Verifier");
50  printf("Affine verifier is not supported; not testing anything.\n");
51 }
52 
53 /**
54  * The code below provides an example of all stages of running a R1CS ppzkSNARK.
55  *
56  * Of course, in a real-life scenario, we would have three distinct entities,
57  * mangled into one in the demonstration below. The three entities are as
58  * follows. (1) The "generator", which runs the ppzkSNARK generator on input a
59  * given constraint system CS to create a proving and a verification key for CS.
60  * (2) The "prover", which runs the ppzkSNARK prover on input the proving key,
61  * a primary input for CS, and an auxiliary input for CS.
62  * (3) The "verifier", which runs the ppzkSNARK verifier on input the
63  * verification key, a primary input for CS, and a proof.
64  */
65 template<typename ppT>
66 bool run_r1cs_ppzksnark(
67  const r1cs_example<libff::Fr<ppT>> &example, const bool test_serialization)
68 {
69  libff::enter_block("Call to run_r1cs_ppzksnark");
70 
71  libff::print_header("R1CS ppzkSNARK Generator");
72  r1cs_ppzksnark_keypair<ppT> keypair =
73  r1cs_ppzksnark_generator<ppT>(example.constraint_system);
74  printf("\n");
75  libff::print_indent();
76  libff::print_mem("after generator");
77 
78  libff::print_header("Preprocess verification key");
79  r1cs_ppzksnark_processed_verification_key<ppT> pvk =
80  r1cs_ppzksnark_verifier_process_vk<ppT>(keypair.vk);
81 
82  if (test_serialization) {
83  libff::enter_block("Test serialization of keys");
84  keypair.pk =
85  libff::reserialize<r1cs_ppzksnark_proving_key<ppT>>(keypair.pk);
86  keypair.vk = libff::reserialize<r1cs_ppzksnark_verification_key<ppT>>(
87  keypair.vk);
88  pvk =
89  libff::reserialize<r1cs_ppzksnark_processed_verification_key<ppT>>(
90  pvk);
91  libff::leave_block("Test serialization of keys");
92  }
93 
94  libff::print_header("R1CS ppzkSNARK Prover");
95  r1cs_ppzksnark_proof<ppT> proof = r1cs_ppzksnark_prover<ppT>(
96  keypair.pk, example.primary_input, example.auxiliary_input);
97  printf("\n");
98  libff::print_indent();
99  libff::print_mem("after prover");
100 
101  if (test_serialization) {
102  libff::enter_block("Test serialization of proof");
103  proof = libff::reserialize<r1cs_ppzksnark_proof<ppT>>(proof);
104  libff::leave_block("Test serialization of proof");
105  }
106 
107  libff::print_header("R1CS ppzkSNARK Verifier");
108  const bool ans = r1cs_ppzksnark_verifier_strong_IC<ppT>(
109  keypair.vk, example.primary_input, proof);
110  printf("\n");
111  libff::print_indent();
112  libff::print_mem("after verifier");
113  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
114 
115  libff::print_header("R1CS ppzkSNARK Online Verifier");
116  const bool ans2 = r1cs_ppzksnark_online_verifier_strong_IC<ppT>(
117  pvk, example.primary_input, proof);
118  assert(ans == ans2);
119  libff::UNUSED(ans2);
120 
121  test_affine_verifier<ppT>(keypair.vk, example.primary_input, proof, ans);
122 
123  libff::leave_block("Call to run_r1cs_ppzksnark");
124 
125  return ans;
126 }
127 
128 } // namespace libsnark
129 
130 #endif // RUN_R1CS_PPZKSNARK_TCC_