set_commitment_component.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_ZK_BLUEPRINT_SET_COMMITMENT_COMPONENT_HPP
27 #define CRYPTO3_ZK_BLUEPRINT_SET_COMMITMENT_COMPONENT_HPP
28 
31 #include <nil/crypto3/zk/components/merkle_tree/merkle_tree_check_read_component.hpp>
32 #include <nil/crypto3/zk/snark/components/set_commitment/set_membership_proof_variable.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace zk {
37  namespace snark {
38  namespace components {
39 
40  template<typename FieldType, typename Hash>
41  using set_commitment_variable = digest_variable<FieldType>;
42 
43  template<typename FieldType, typename Hash>
44  class set_commitment_component : public component<FieldType> {
45  private:
46  std::shared_ptr<block_variable<FieldType>> element_block;
47  std::shared_ptr<digest_variable<FieldType>> element_digest;
48  std::shared_ptr<Hash> hash_element;
49  std::shared_ptr<merkle_tree_check_read_component<FieldType, Hash>> check_membership;
50 
51  public:
52  std::size_t tree_depth;
53  blueprint_variable_vector<FieldType> element_bits;
56  blueprint_linear_combination<FieldType> check_successful;
57 
58  set_commitment_component(blueprint<FieldType> &bp,
59  const std::size_t max_entries,
60  const blueprint_variable_vector<FieldType> &element_bits,
63  const blueprint_linear_combination<FieldType> &check_successful) :
64  component<FieldType>(bp),
65  tree_depth(static_cast<std::size_t>(std::ceil(std::log2(max_entries)))),
68  element_block.reset(new block_variable<FieldType>(bp, {element_bits}));
69 
70  if (tree_depth == 0) {
71  hash_element.reset(new Hash(bp, element_bits.size(), *element_block, root_digest));
72  } else {
73  element_digest.reset(new digest_variable<FieldType>(bp, Hash::get_digest_len()));
74  hash_element.reset(new Hash(bp, element_bits.size(), *element_block, *element_digest));
75  check_membership.reset(
76  new merkle_tree_check_read_component<FieldType, Hash>(bp,
77  tree_depth,
78  proof.address_bits,
79  *element_digest,
81  *proof.merkle_path,
83  }
84  }
85 
87  hash_element->generate_r1cs_constraints();
88 
89  if (tree_depth > 0) {
90  check_membership->generate_r1cs_constraints();
91  }
92  }
93 
95  hash_element->generate_r1cs_witness();
96 
97  if (tree_depth > 0) {
98  check_membership->generate_r1cs_witness();
99  }
100  }
101 
102  static std::size_t root_size_in_bits() {
103  return merkle_tree_check_read_component<FieldType, Hash>::root_size_in_bits();
104  }
105  };
106  } // namespace components
107  } // namespace snark
108  } // namespace zk
109  } // namespace crypto3
110 } // namespace nil
111 
112 #endif // CRYPTO3_ZK_BLUEPRINT_SET_COMMITMENT_COMPONENT_HPP
Definition: set_commitment_component.hpp:44
set_commitment_component(blueprint< FieldType > &bp, const std::size_t max_entries, const blueprint_variable_vector< FieldType > &element_bits, const set_commitment_variable< FieldType, Hash > &root_digest, const set_membership_proof_variable< FieldType, Hash > &proof, const blueprint_linear_combination< FieldType > &check_successful)
Definition: set_commitment_component.hpp:58
blueprint_linear_combination< FieldType > check_successful
Definition: set_commitment_component.hpp:56
void generate_r1cs_constraints()
Definition: set_commitment_component.hpp:86
set_membership_proof_variable< FieldType, Hash > proof
Definition: set_commitment_component.hpp:55
std::size_t tree_depth
Definition: set_commitment_component.hpp:52
set_commitment_variable< FieldType, Hash > root_digest
Definition: set_commitment_component.hpp:54
static std::size_t root_size_in_bits()
Definition: set_commitment_component.hpp:102
void generate_r1cs_witness()
Definition: set_commitment_component.hpp:94
blueprint_variable_vector< FieldType > element_bits
Definition: set_commitment_component.hpp:53
Definition: set_membership_proof_variable.hpp:44
Definition: snark/proof.hpp:37
digest_variable< FieldType > set_commitment_variable
Definition: set_commitment_component.hpp:41
Definition: pair.hpp:31