2 *****************************************************************************
4 Implementation of functionality that runs the R1CS ppzkSNARK for
7 See run_r1cs_ppzksnark.hpp .
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 *****************************************************************************/
15 #ifndef RUN_R1CS_PPZKSNARK_TCC_
16 #define RUN_R1CS_PPZKSNARK_TCC_
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp>
21 #include <type_traits>
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)
33 libff::print_header("R1CS ppzkSNARK Affine Verifier");
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);
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)
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");
54 * The code below provides an example of all stages of running a R1CS ppzkSNARK.
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.
65 template<typename ppT>
66 bool run_r1cs_ppzksnark(
67 const r1cs_example<libff::Fr<ppT>> &example, const bool test_serialization)
69 libff::enter_block("Call to run_r1cs_ppzksnark");
71 libff::print_header("R1CS ppzkSNARK Generator");
72 r1cs_ppzksnark_keypair<ppT> keypair =
73 r1cs_ppzksnark_generator<ppT>(example.constraint_system);
75 libff::print_indent();
76 libff::print_mem("after generator");
78 libff::print_header("Preprocess verification key");
79 r1cs_ppzksnark_processed_verification_key<ppT> pvk =
80 r1cs_ppzksnark_verifier_process_vk<ppT>(keypair.vk);
82 if (test_serialization) {
83 libff::enter_block("Test serialization of keys");
85 libff::reserialize<r1cs_ppzksnark_proving_key<ppT>>(keypair.pk);
86 keypair.vk = libff::reserialize<r1cs_ppzksnark_verification_key<ppT>>(
89 libff::reserialize<r1cs_ppzksnark_processed_verification_key<ppT>>(
91 libff::leave_block("Test serialization of keys");
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);
98 libff::print_indent();
99 libff::print_mem("after prover");
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");
107 libff::print_header("R1CS ppzkSNARK Verifier");
108 const bool ans = r1cs_ppzksnark_verifier_strong_IC<ppT>(
109 keypair.vk, example.primary_input, proof);
111 libff::print_indent();
112 libff::print_mem("after verifier");
113 printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
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);
121 test_affine_verifier<ppT>(keypair.vk, example.primary_input, proof, ans);
123 libff::leave_block("Call to run_r1cs_ppzksnark");
128 } // namespace libsnark
130 #endif // RUN_R1CS_PPZKSNARK_TCC_