Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_r1cs_gg_ppzksnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the R1CS GG-ppzkSNARK for
5  a given R1CS example.
6 
7  See run_r1cs_gg_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_GG_PPZKSNARK_TCC_
16 #define RUN_R1CS_GG_PPZKSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_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_gg_ppzksnark_verification_key<ppT> &vk,
29  const r1cs_gg_ppzksnark_primary_input<ppT> &primary_input,
30  const r1cs_gg_ppzksnark_proof<ppT> &proof,
31  const bool expected_answer)
32 {
33  libff::print_header("R1CS GG-ppzkSNARK Affine Verifier");
34  const bool answer = r1cs_gg_ppzksnark_affine_verifier_weak_IC<ppT>(
35  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_gg_ppzksnark_verification_key<ppT> &vk,
44  const r1cs_gg_ppzksnark_primary_input<ppT> &primary_input,
45  const r1cs_gg_ppzksnark_proof<ppT> &proof,
46  const bool expected_answer)
47 {
48  libff::print_header("R1CS GG-ppzkSNARK Affine Verifier");
49  libff::UNUSED(vk, primary_input, proof, expected_answer);
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
55  * GG-ppzkSNARK.
56  *
57  * Of course, in a real-life scenario, we would have three distinct entities,
58  * mangled into one in the demonstration below. The three entities are as
59  * follows. (1) The "generator", which runs the ppzkSNARK generator on input a
60  * given constraint system CS to create a proving and a verification key for CS.
61  * (2) The "prover", which runs the ppzkSNARK prover on input the proving key,
62  * a primary input for CS, and an auxiliary input for CS.
63  * (3) The "verifier", which runs the ppzkSNARK verifier on input the
64  * verification key, a primary input for CS, and a proof.
65  */
66 template<typename ppT>
67 bool run_r1cs_gg_ppzksnark(
68  const r1cs_example<libff::Fr<ppT>> &example, const bool test_serialization)
69 {
70  libff::enter_block("Call to run_r1cs_gg_ppzksnark");
71 
72  libff::print_header("R1CS GG-ppzkSNARK Generator");
73  r1cs_gg_ppzksnark_keypair<ppT> keypair =
74  r1cs_gg_ppzksnark_generator<ppT>(example.constraint_system);
75  printf("\n");
76  libff::print_indent();
77  libff::print_mem("after generator");
78 
79  libff::print_header("Preprocess verification key");
80  r1cs_gg_ppzksnark_processed_verification_key<ppT> pvk =
81  r1cs_gg_ppzksnark_verifier_process_vk<ppT>(keypair.vk);
82 
83  if (test_serialization) {
84  libff::enter_block("Test serialization of keys");
85  keypair.pk =
86  libff::reserialize<r1cs_gg_ppzksnark_proving_key<ppT>>(keypair.pk);
87  keypair.vk =
88  libff::reserialize<r1cs_gg_ppzksnark_verification_key<ppT>>(
89  keypair.vk);
90  pvk = libff::reserialize<
91  r1cs_gg_ppzksnark_processed_verification_key<ppT>>(pvk);
92  libff::leave_block("Test serialization of keys");
93  }
94 
95  libff::print_header("R1CS GG-ppzkSNARK Prover");
96  r1cs_gg_ppzksnark_proof<ppT> proof = r1cs_gg_ppzksnark_prover<ppT>(
97  keypair.pk, example.primary_input, example.auxiliary_input);
98  printf("\n");
99  libff::print_indent();
100  libff::print_mem("after prover");
101 
102  if (test_serialization) {
103  libff::enter_block("Test serialization of proof");
104  proof = libff::reserialize<r1cs_gg_ppzksnark_proof<ppT>>(proof);
105  libff::leave_block("Test serialization of proof");
106  }
107 
108  libff::print_header("R1CS GG-ppzkSNARK Verifier");
109  const bool ans = r1cs_gg_ppzksnark_verifier_strong_IC<ppT>(
110  keypair.vk, example.primary_input, proof);
111  printf("\n");
112  libff::print_indent();
113  libff::print_mem("after verifier");
114  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
115 
116  libff::print_header("R1CS GG-ppzkSNARK Online Verifier");
117  const bool ans2 = r1cs_gg_ppzksnark_online_verifier_strong_IC<ppT>(
118  pvk, example.primary_input, proof);
119  assert(ans == ans2);
120  libff::UNUSED(ans2);
121 
122  test_affine_verifier<ppT>(keypair.vk, example.primary_input, proof, ans);
123 
124  libff::leave_block("Call to run_r1cs_gg_ppzksnark");
125 
126  return ans;
127 }
128 
129 } // namespace libsnark
130 
131 #endif // RUN_R1CS_GG_PPZKSNARK_TCC_