Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
run_r1cs_ppzkadsnark.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of functionality that runs the R1CS ppzkADSNARK for
5  a given R1CS example.
6 
7  See run_r1cs_ppzkadsnark.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_PPZKADSNARK_TCC_
16 #define RUN_R1CS_PPZKADSNARK_TCC_
17 
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzkadsnark/r1cs_ppzkadsnark/examples/prf/aes_ctr_prf.tcc>
20 #include <libsnark/zk_proof_systems/ppzkadsnark/r1cs_ppzkadsnark/examples/signature/ed25519_signature.tcc>
21 #include <libsnark/zk_proof_systems/ppzkadsnark/r1cs_ppzkadsnark/r1cs_ppzkadsnark.hpp>
22 #include <sstream>
23 #include <type_traits>
24 
25 namespace libsnark
26 {
27 
28 /**
29  * The code below provides an example of all stages of running a R1CS
30  * ppzkADSNARK.
31  *
32  * Of course, in a real-life scenario, we would have three distinct entities,
33  * mangled into one in the demonstration below. The three entities are as
34  * follows. (1) The "generator", which runs the ppzkADSNARK generator on input a
35  * given constraint system CS to create a proving and a verification key for CS.
36  * (2) The "prover", which runs the ppzkADSNARK prover on input the proving key,
37  * a primary input for CS, and an auxiliary input for CS.
38  * (3) The "verifier", which runs the ppzkADSNARK verifier on input the
39  * verification key, a primary input for CS, and a proof.
40  */
41 template<typename ppT>
42 bool run_r1cs_ppzkadsnark(
43  const r1cs_example<libff::Fr<snark_pp<ppT>>> &example,
44  const bool test_serialization)
45 {
46  libff::enter_block("Call to run_r1cs_ppzkadsnark");
47 
48  r1cs_ppzkadsnark_auth_keys<ppT> auth_keys =
49  r1cs_ppzkadsnark_auth_generator<ppT>();
50 
51  libff::print_header("R1CS ppzkADSNARK Generator");
52  r1cs_ppzkadsnark_keypair<ppT> keypair = r1cs_ppzkadsnark_generator<ppT>(
53  example.constraint_system, auth_keys.pap);
54  printf("\n");
55  libff::print_indent();
56  libff::print_mem("after generator");
57 
58  libff::print_header("Preprocess verification key");
59  r1cs_ppzkadsnark_processed_verification_key<ppT> pvk =
60  r1cs_ppzkadsnark_verifier_process_vk<ppT>(keypair.vk);
61 
62  if (test_serialization) {
63  libff::enter_block("Test serialization of keys");
64  keypair.pk =
65  libff::reserialize<r1cs_ppzkadsnark_proving_key<ppT>>(keypair.pk);
66  keypair.vk = libff::reserialize<r1cs_ppzkadsnark_verification_key<ppT>>(
67  keypair.vk);
68  pvk = libff::reserialize<
69  r1cs_ppzkadsnark_processed_verification_key<ppT>>(pvk);
70  libff::leave_block("Test serialization of keys");
71  }
72 
73  libff::print_header("R1CS ppzkADSNARK Authenticate");
74  std::vector<libff::Fr<snark_pp<ppT>>> data;
75  data.reserve(example.constraint_system.num_inputs());
76  std::vector<labelT> labels;
77  labels.reserve(example.constraint_system.num_inputs());
78  for (size_t i = 0; i < example.constraint_system.num_inputs(); i++) {
79  labels.emplace_back(labelT());
80  data.emplace_back(example.primary_input[i]);
81  }
82  std::vector<r1cs_ppzkadsnark_auth_data<ppT>> auth_data =
83  r1cs_ppzkadsnark_auth_sign<ppT>(data, auth_keys.sak, labels);
84 
85  libff::print_header("R1CS ppzkADSNARK Verify Symmetric");
86  bool auth_res = r1cs_ppzkadsnark_auth_verify<ppT>(
87  data, auth_data, auth_keys.sak, labels);
88  printf("* The verification result is: %s\n", (auth_res ? "PASS" : "FAIL"));
89 
90  libff::print_header("R1CS ppzkADSNARK Verify Public");
91  bool auth_resp = r1cs_ppzkadsnark_auth_verify<ppT>(
92  data, auth_data, auth_keys.pak, labels);
93  assert(auth_res == auth_resp);
94 
95  libff::print_header("R1CS ppzkADSNARK Prover");
96  r1cs_ppzkadsnark_proof<ppT> proof = r1cs_ppzkadsnark_prover<ppT>(
97  keypair.pk, example.primary_input, example.auxiliary_input, auth_data);
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_ppzkadsnark_proof<ppT>>(proof);
105  libff::leave_block("Test serialization of proof");
106  }
107 
108  libff::print_header("R1CS ppzkADSNARK Symmetric Verifier");
109  bool ans = r1cs_ppzkadsnark_verifier<ppT>(
110  keypair.vk, proof, auth_keys.sak, labels);
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 ppzkADSNARK Symmetric Online Verifier");
117  bool ans2 = r1cs_ppzkadsnark_online_verifier<ppT>(
118  pvk, proof, auth_keys.sak, labels);
119  assert(ans == ans2);
120 
121  libff::print_header("R1CS ppzkADSNARK Public Verifier");
122  ans = r1cs_ppzkadsnark_verifier<ppT>(
123  keypair.vk, auth_data, proof, auth_keys.pak, labels);
124  printf("\n");
125  libff::print_indent();
126  libff::print_mem("after verifier");
127  printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
128 
129  libff::print_header("R1CS ppzkADSNARK Public Online Verifier");
130  ans2 = r1cs_ppzkadsnark_online_verifier<ppT>(
131  pvk, auth_data, proof, auth_keys.pak, labels);
132  assert(ans == ans2);
133 
134  libff::leave_block("Call to run_r1cs_ppzkadsnark");
135 
136  return ans;
137 }
138 
139 } // namespace libsnark
140 
141 #endif // RUN_R1CS_PPZKADSNARK_TCC_