blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.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_SNARK_SET_COMMITMENT_HPP
27 #define CRYPTO3_ZK_BLUEPRINT_SNARK_SET_COMMITMENT_HPP
28 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace zk {
35  namespace snark {
36 
37  typedef std::vector<bool> set_commitment;
38 
40  std::size_t address;
42 
43  bool operator==(const set_membership_proof &other) const {
44  return (this->address == other.address && this->merkle_path == other.merkle_path);
45  }
46 
47  std::size_t size_in_bits() const {
48  if (merkle_path.empty()) {
49  return (8 * sizeof(address));
50  } else {
51  return (8 * sizeof(address) + merkle_path[0].size() * merkle_path.size());
52  }
53  }
54  };
55 
56  template<typename Hash>
58  private:
59  std::shared_ptr<merkle_tree<Hash>> tree;
60  std::map<std::vector<bool>, std::size_t> hash_to_pos;
61 
62  public:
63  std::size_t depth;
64  std::size_t digest_size;
65  std::size_t value_size;
66 
67  set_commitment_accumulator(const std::size_t max_entries, const std::size_t value_size = 0) :
69  depth = static_cast<std::size_t>(std::ceil(std::log2(max_entries)));
70  digest_size = Hash::digest_bits;
71 
72  tree.reset(new merkle_tree<Hash>(depth, digest_size));
73  }
74 
75  void add(const std::vector<bool> &value) {
76  assert(value_size == 0 || value.size() == value_size);
77  const std::vector<bool> hash = Hash::get_hash(value);
78  if (hash_to_pos.find(hash) == hash_to_pos.end()) {
79  const std::size_t pos = hash_to_pos.size();
80  tree->set_value(pos, hash);
81  hash_to_pos[hash] = pos;
82  }
83  }
84 
85  bool is_in_set(const std::vector<bool> &value) const {
86  assert(value_size == 0 || value.size() == value_size);
87  const std::vector<bool> hash = Hash::get_hash(value);
88  return (hash_to_pos.find(hash) != hash_to_pos.end());
89  }
90 
92  return tree->get_root();
93  }
94 
95  set_membership_proof get_membership_proof(const std::vector<bool> &value) const {
96  const std::vector<bool> hash = Hash::get_hash(value);
97  auto it = hash_to_pos.find(hash);
98  assert(it != hash_to_pos.end());
99 
101  proof.address = it->second;
102  proof.merkle_path = tree->get_path(it->second);
103 
104  return proof;
105  }
106  };
107 
108  } // namespace snark
109  } // namespace zk
110  } // namespace crypto3
111 } // namespace nil
112 
113 #endif // CRYPTO3_ZK_BLUEPRINT_SNARK_SET_COMMITMENT_HPP
Definition: snark/proof.hpp:37
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:57
bool is_in_set(const std::vector< bool > &value) const
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:85
void add(const std::vector< bool > &value)
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:75
std::size_t digest_size
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:64
set_membership_proof get_membership_proof(const std::vector< bool > &value) const
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:95
std::size_t value_size
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:65
std::size_t depth
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:63
set_commitment_accumulator(const std::size_t max_entries, const std::size_t value_size=0)
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:67
set_commitment get_commitment() const
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:91
std::enable_if<!boost::accumulators::detail::is_accumulator_set< OutputIterator >::value, OutputIterator >::type hash(InputIterator first, InputIterator last, OutputIterator out)
Definition: algorithm/hash.hpp:78
std::vector< merkle_authentication_node > merkle_authentication_path
Definition: blueprint/include/nil/crypto3/zk/merkle_tree.hpp:66
std::vector< bool > set_commitment
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:37
Definition: pair.hpp:31
Definition: blueprint/include/nil/crypto3/zk/merkle_tree.hpp:70
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:39
std::size_t address
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:40
bool operator==(const set_membership_proof &other) const
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:43
std::size_t size_in_bits() const
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:47
merkle_authentication_path merkle_path
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:41