component_from_r1cs.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 // @file Declaration of interfaces for a component that can be created from an R1CS constraint system.
26 //---------------------------------------------------------------------------//
27 
28 #ifndef CRYPTO3_ZK_BLUEPRINT_COMPONENT_FROM_R1CS_HPP
29 #define CRYPTO3_ZK_BLUEPRINT_COMPONENT_FROM_R1CS_HPP
30 
31 #include <map>
32 
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace zk {
38  namespace components {
39 
40  template<typename FieldType>
41  class component_from_r1cs : public component<FieldType> {
42 
43  const std::vector<blueprint_variable_vector<FieldType>> vars;
45  std::map<std::size_t, std::size_t> cs_to_vars;
46 
47  public:
51  component<FieldType>(bp),
52  vars(vars), cs(cs) {
53  cs_to_vars[0] = 0; /* constant term maps to constant term */
54 
55  std::size_t cs_var_idx = 1;
56  for (auto va : vars) {
57  for (auto v : va) {
58  cs_to_vars[cs_var_idx] = v.index;
59  ++cs_var_idx;
60  }
61  }
62 
63  assert(cs_var_idx - 1 == cs.num_variables());
64  }
65 
67  for (std::size_t i = 0; i < cs.num_constraints(); ++i) {
68  const snark::r1cs_constraint<FieldType> &constr = cs.constraints[i];
69  snark::r1cs_constraint<FieldType> translated_constr;
70 
71  for (const linear_term<FieldType> &t : constr.a.terms) {
72  translated_constr.a.terms.emplace_back(
73  linear_term<FieldType>(variable<FieldType>(cs_to_vars[t.index]), t.coeff));
74  }
75 
76  for (const linear_term<FieldType> &t : constr.b.terms) {
77  translated_constr.b.terms.emplace_back(
78  linear_term<FieldType>(variable<FieldType>(cs_to_vars[t.index]), t.coeff));
79  }
80 
81  for (const linear_term<FieldType> &t : constr.c.terms) {
82  translated_constr.c.terms.emplace_back(
83  linear_term<FieldType>(variable<FieldType>(cs_to_vars[t.index]), t.coeff));
84  }
85 
86  this->bp.add_r1cs_constraint(translated_constr);
87  }
88  }
90  const snark::r1cs_auxiliary_input<FieldType> &auxiliary_input) {
91  assert(cs.num_inputs() == primary_input.size());
92  assert(cs.num_variables() == primary_input.size() + auxiliary_input.size());
93 
94  for (std::size_t i = 0; i < primary_input.size(); ++i) {
95  this->bp.val(variable<FieldType>(cs_to_vars[i + 1])) = primary_input[i];
96  }
97 
98  for (std::size_t i = 0; i < auxiliary_input.size(); ++i) {
99  this->bp.val(variable<FieldType>(cs_to_vars[primary_input.size() + i + 1])) =
100  auxiliary_input[i];
101  }
102  }
103  };
104  } // namespace components
105  } // namespace zk
106  } // namespace crypto3
107 } // namespace nil
108 
109 #endif // CRYPTO3_ZK_BLUEPRINT_COMPONENT_FROM_R1CS_HPP
Definition: blueprint_variable.hpp:57
Definition: blueprint.hpp:46
Definition: component_from_r1cs.hpp:41
void generate_r1cs_constraints()
Definition: component_from_r1cs.hpp:66
void generate_r1cs_witness(const snark::r1cs_primary_input< FieldType > &primary_input, const snark::r1cs_auxiliary_input< FieldType > &auxiliary_input)
Definition: component_from_r1cs.hpp:89
component_from_r1cs(blueprint< FieldType > &bp, const std::vector< blueprint_variable_vector< FieldType >> &vars, const snark::r1cs_constraint_system< FieldType > &cs)
Definition: component_from_r1cs.hpp:48
Definition: component.hpp:37
blueprint< FieldType > & bp
Definition: component.hpp:39
vector(T, U...) -> vector< std::enable_if_t<(std::is_same_v< T, U > &&...), T >, 1+sizeof...(U)>
deduction guide for uniform initialization
std::vector< typename FieldType::value_type > r1cs_auxiliary_input
Definition: r1cs.hpp:104
std::vector< typename FieldType::value_type > r1cs_primary_input
Definition: r1cs.hpp:101
Definition: pair.hpp:31
std::vector< r1cs_constraint< FieldType > > constraints
Definition: r1cs.hpp:130
std::size_t num_constraints() const
Definition: r1cs.hpp:143
std::size_t num_inputs() const
Definition: r1cs.hpp:135
std::size_t num_variables() const
Definition: r1cs.hpp:139
linear_combination< FieldType > c
Definition: r1cs.hpp:63
linear_combination< FieldType > a
Definition: r1cs.hpp:63
linear_combination< FieldType > b
Definition: r1cs.hpp:63