bacs_to_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 BACS-to-R1CS reduction, that is, constructing
26 // a R1CS ("Rank-1 Constraint System") from a BACS ("Bilinear Arithmetic Circuit Satisfiability").
27 //
28 // The reduction is straightforward: each bilinear gate gives rises to a
29 // corresponding R1CS constraint that enforces correct computation of the gate;
30 // also, each output gives rise to a corresponding R1CS constraint that enforces
31 // that the output is zero.
32 //---------------------------------------------------------------------------//
33 
34 #ifndef CRYPTO3_ZK_BACS_TO_R1CS_BASIC_POLICY_HPP
35 #define CRYPTO3_ZK_BACS_TO_R1CS_BASIC_POLICY_HPP
36 
39 
40 namespace nil {
41  namespace crypto3 {
42  namespace zk {
43  namespace snark {
44  namespace reductions {
45 
46  template<typename FieldType>
47  struct bacs_to_r1cs {
48  typedef FieldType field_type;
49 
54  assert(circuit.is_valid());
56 
57  result.primary_input_size = circuit.primary_input_size;
58  result.auxiliary_input_size = circuit.auxiliary_input_size + circuit.gates.size();
59 
60  for (auto &g : circuit.gates) {
61  result.constraints.emplace_back(r1cs_constraint<FieldType>(g.lhs, g.rhs, g.output));
62  }
63 
64  for (auto &g : circuit.gates) {
65  if (g.is_circuit_output) {
66  result.constraints.emplace_back(r1cs_constraint<FieldType>(1, g.output, 0));
67  }
68  }
69 
70  return result;
71  }
72 
78  const bacs_primary_input<FieldType> &primary_input,
79  const bacs_auxiliary_input<FieldType> &auxiliary_input) {
81  circuit.get_all_wires(primary_input, auxiliary_input);
82  return result;
83  }
84  };
85  } // namespace reductions
86  } // namespace snark
87  } // namespace zk
88  } // namespace crypto3
89 } // namespace nil
90 
91 #endif // CRYPTO3_ZK_BACS_TO_R1CS_BASIC_POLICY_HPP
bacs_variable_assignment< FieldType > bacs_auxiliary_input
Definition: bacs.hpp:95
bacs_variable_assignment< FieldType > bacs_primary_input
Definition: bacs.hpp:89
std::vector< typename FieldType::value_type > r1cs_variable_assignment
Definition: r1cs.hpp:107
Definition: pair.hpp:31
bacs_variable_assignment< FieldType > get_all_wires(const bacs_primary_input< FieldType > &primary_input, const bacs_auxiliary_input< FieldType > &auxiliary_input) const
Definition: bacs.hpp:212
bool is_valid() const
Definition: bacs.hpp:158
std::size_t primary_input_size
Definition: bacs.hpp:113
std::size_t auxiliary_input_size
Definition: bacs.hpp:114
std::vector< bacs_gate< FieldType > > gates
Definition: bacs.hpp:115
std::vector< r1cs_constraint< FieldType > > constraints
Definition: r1cs.hpp:130
std::size_t primary_input_size
Definition: r1cs.hpp:127
std::size_t auxiliary_input_size
Definition: r1cs.hpp:128
FieldType field_type
Definition: bacs_to_r1cs.hpp:48
static r1cs_constraint_system< FieldType > instance_map(const bacs_circuit< FieldType > &circuit)
Definition: bacs_to_r1cs.hpp:53
static r1cs_variable_assignment< FieldType > witness_map(const bacs_circuit< FieldType > &circuit, const bacs_primary_input< FieldType > &primary_input, const bacs_auxiliary_input< FieldType > &auxiliary_input)
Definition: bacs_to_r1cs.hpp:77