r1cs_ppzksnark/prover.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_R1CS_PPZKSNARK_BASIC_PROVER_HPP
27 #define CRYPTO3_R1CS_PPZKSNARK_BASIC_PROVER_HPP
28 
29 #ifdef MULTICORE
30 #include <omp.h>
31 #endif
32 
36 
40 #include <nil/crypto3/zk/snark/schemes/ppzksnark/r1cs_ppzksnark/detail/basic_policy.hpp>
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace zk {
45  namespace snark {
46 
55  template<typename CurveType>
58 
59  using g1_type = typename CurveType::template g1_type<>;
60  using g2_type = typename CurveType::template g2_type<>;
61  using g1_value_type = typename g1_type::value_type;
62  using g2_value_type = typename g2_type::value_type;
63  using scalar_field_type = typename CurveType::scalar_field_type;
64 
65  public:
68 
70 
72 
74  const primary_input_type &primary_input,
75  const auxiliary_input_type &auxiliary_input) {
76 
77  const typename scalar_field_type::value_type d1 = algebra::random_element<scalar_field_type>(),
78  d2 = algebra::random_element<scalar_field_type>(),
79  d3 = algebra::random_element<scalar_field_type>();
80 
81  const qap_witness<scalar_field_type> qap_wit =
83  auxiliary_input, d1, d2, d3);
84 
86  proving_key.A_query[0] + qap_wit.d1 * proving_key.A_query[qap_wit.num_variables + 1];
88  proving_key.B_query[0] + qap_wit.d2 * proving_key.B_query[qap_wit.num_variables + 1];
90  proving_key.C_query[0] + qap_wit.d3 * proving_key.C_query[qap_wit.num_variables + 1];
91 
92  g1_value_type g_H = g1_value_type::zero();
93  g1_value_type g_K =
94  (proving_key.K_query[0] + qap_wit.d1 * proving_key.K_query[qap_wit.num_variables + 1] +
95  qap_wit.d2 * proving_key.K_query[qap_wit.num_variables + 2] +
96  qap_wit.d3 * proving_key.K_query[qap_wit.num_variables + 3]);
97 #ifdef MULTICORE
98  const std::size_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env
99  // var or call omp_set_num_threads()
100 #else
101  const std::size_t chunks = 1;
102 #endif
103 
104  g_A = g_A + kc_multiexp_with_mixed_addition<algebra::policies::multiexp_method_BDLO12>(
105  proving_key.A_query, 1, 1 + qap_wit.num_variables,
106  qap_wit.coefficients_for_ABCs.begin(),
107  qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks);
108 
109  g_B = g_B + kc_multiexp_with_mixed_addition<algebra::policies::multiexp_method_BDLO12>(
110  proving_key.B_query, 1, 1 + qap_wit.num_variables,
111  qap_wit.coefficients_for_ABCs.begin(),
112  qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks);
113 
114  g_C = g_C + kc_multiexp_with_mixed_addition<algebra::policies::multiexp_method_BDLO12>(
115  proving_key.C_query, 1, 1 + qap_wit.num_variables,
116  qap_wit.coefficients_for_ABCs.begin(),
117  qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks);
118 
119  g_H = g_H + algebra::multiexp<algebra::policies::multiexp_method_BDLO12>(
120  proving_key.H_query.begin(), proving_key.H_query.begin() + qap_wit.degree + 1,
121  qap_wit.coefficients_for_H.begin(),
122  qap_wit.coefficients_for_H.begin() + qap_wit.degree + 1, chunks);
123 
124  g_K =
125  g_K + algebra::multiexp_with_mixed_addition<algebra::policies::multiexp_method_BDLO12>(
126  proving_key.K_query.begin() + 1,
127  proving_key.K_query.begin() + 1 + qap_wit.num_variables,
128  qap_wit.coefficients_for_ABCs.begin(),
129  qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables, chunks);
130 
131  return proof_type(std::move(g_A), std::move(g_B), std::move(g_C), std::move(g_H),
132  std::move(g_K));
133  }
134  };
135  } // namespace snark
136  } // namespace zk
137  } // namespace crypto3
138 } // namespace nil
139 
140 #endif // CRYPTO3_R1CS_PPZKSNARK_BASIC_PROVER_HPP
Definition: proving_key.hpp:37
Definition: snark/systems/ppzksnark/r1cs_ppzksnark/proof.hpp:43
Definition: r1cs_ppzksnark/prover.hpp:56
policy_type::proving_key_type proving_key_type
Definition: r1cs_ppzksnark/prover.hpp:69
static proof_type process(const proving_key_type &proving_key, const primary_input_type &primary_input, const auxiliary_input_type &auxiliary_input)
Definition: r1cs_ppzksnark/prover.hpp:73
policy_type::proof_type proof_type
Definition: r1cs_ppzksnark/prover.hpp:71
policy_type::auxiliary_input_type auxiliary_input_type
Definition: r1cs_ppzksnark/prover.hpp:67
policy_type::primary_input_type primary_input_type
Definition: r1cs_ppzksnark/prover.hpp:66
Definition: systems/ppzksnark/r1cs_ppzksnark/proving_key.hpp:47
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/detail/basic_policy.hpp:78
r1cs_primary_input< typename CurveType::scalar_field_type > primary_input_type
Definition: zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/detail/basic_policy.hpp:89
r1cs_auxiliary_input< typename CurveType::scalar_field_type > auxiliary_input_type
Definition: zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/detail/basic_policy.hpp:91
Definition: knowledge_commitment.hpp:49
field_value_type d3
Definition: qap.hpp:319
std::size_t num_variables
Definition: qap.hpp:315
std::vector< field_value_type > coefficients_for_H
Definition: qap.hpp:322
std::vector< field_value_type > coefficients_for_ABCs
Definition: qap.hpp:321
field_value_type d1
Definition: qap.hpp:319
field_value_type d2
Definition: qap.hpp:319
std::size_t degree
Definition: qap.hpp:316
static qap_witness< FieldType > witness_map(const r1cs_constraint_system< FieldType > &cs, const r1cs_primary_input< FieldType > &primary_input, const r1cs_auxiliary_input< FieldType > &auxiliary_input, const typename FieldType::value_type &d1, const typename FieldType::value_type &d2, const typename FieldType::value_type &d3)
Definition: r1cs_to_qap.hpp:221