2 *****************************************************************************
4 Implementation of functionality that runs the R1CS GG-ppzkSNARK for
7 See run_r1cs_gg_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_GG_PPZKSNARK_TCC_
16 #define RUN_R1CS_GG_PPZKSNARK_TCC_
18 #include <libff/common/profiling.hpp>
19 #include <libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_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_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)
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);
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)
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");
54 * The code below provides an example of all stages of running a R1CS
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.
66 template<typename ppT>
67 bool run_r1cs_gg_ppzksnark(
68 const r1cs_example<libff::Fr<ppT>> &example, const bool test_serialization)
70 libff::enter_block("Call to run_r1cs_gg_ppzksnark");
72 libff::print_header("R1CS GG-ppzkSNARK Generator");
73 r1cs_gg_ppzksnark_keypair<ppT> keypair =
74 r1cs_gg_ppzksnark_generator<ppT>(example.constraint_system);
76 libff::print_indent();
77 libff::print_mem("after generator");
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);
83 if (test_serialization) {
84 libff::enter_block("Test serialization of keys");
86 libff::reserialize<r1cs_gg_ppzksnark_proving_key<ppT>>(keypair.pk);
88 libff::reserialize<r1cs_gg_ppzksnark_verification_key<ppT>>(
90 pvk = libff::reserialize<
91 r1cs_gg_ppzksnark_processed_verification_key<ppT>>(pvk);
92 libff::leave_block("Test serialization of keys");
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);
99 libff::print_indent();
100 libff::print_mem("after prover");
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");
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);
112 libff::print_indent();
113 libff::print_mem("after verifier");
114 printf("* The verification result is: %s\n", (ans ? "PASS" : "FAIL"));
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);
122 test_affine_verifier<ppT>(keypair.vk, example.primary_input, proof, ans);
124 libff::leave_block("Call to run_r1cs_gg_ppzksnark");
129 } // namespace libsnark
131 #endif // RUN_R1CS_GG_PPZKSNARK_TCC_