authentication_path.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 Test program that exercises the SEppzkSNARK (first generator, then
26 // prover, then verifier) on a synthetic R1CS instance.
27 //---------------------------------------------------------------------------//
28 
29 #ifndef CRYPTO3_ZK_BLUEPRINT_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP
30 #define CRYPTO3_ZK_BLUEPRINT_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP
31 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace zk {
39  namespace components {
40 
41  template<typename FieldType, typename Hash>
42  struct merkle_authentication_path_variable : public component<FieldType> {
43 
44  const std::size_t tree_depth;
45  std::vector<digest_variable<FieldType>> left_digests;
46  std::vector<digest_variable<FieldType>> right_digests;
47 
49  component<FieldType>(bp), tree_depth(tree_depth) {
50  for (std::size_t i = 0; i < tree_depth; ++i) {
51  left_digests.emplace_back(digest_variable<FieldType>(bp, Hash::get_digest_len()));
52  right_digests.emplace_back(digest_variable<FieldType>(bp, Hash::get_digest_len()));
53  }
54  }
55 
57  for (std::size_t i = 0; i < tree_depth; ++i) {
58  left_digests[i].generate_r1cs_constraints();
59  right_digests[i].generate_r1cs_constraints();
60  }
61  }
62 
63  void generate_r1cs_witness(const std::size_t address,
65  assert(path.size() == tree_depth);
66 
67  for (std::size_t i = 0; i < tree_depth; ++i) {
68  if (address & (1ul << (tree_depth - 1 - i))) {
69  left_digests[i].generate_r1cs_witness(path[i]);
70  } else {
71  right_digests[i].generate_r1cs_witness(path[i]);
72  }
73  }
74  }
75 
78  for (std::size_t i = 0; i < tree_depth; ++i) {
79  if (address & (1ul << (tree_depth - 1 - i))) {
80  result.emplace_back(left_digests[i].get_digest());
81  } else {
82  result.emplace_back(right_digests[i].get_digest());
83  }
84  }
85 
86  return result;
87  }
88  };
89  } // namespace components
90  } // namespace zk
91  } // namespace crypto3
92 } // namespace nil
93 
94 #endif // CRYPTO3_ZK_BLUEPRINT_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP
Definition: blueprint.hpp:46
Definition: component.hpp:37
blueprint< FieldType > & bp
Definition: component.hpp:39
std::vector< merkle_authentication_node > merkle_authentication_path
Definition: blueprint/include/nil/crypto3/zk/merkle_tree.hpp:66
Definition: pair.hpp:31
void generate_r1cs_witness(const std::size_t address, const snark::merkle_authentication_path &path)
Definition: authentication_path.hpp:63
void generate_r1cs_constraints()
Definition: authentication_path.hpp:56
std::vector< digest_variable< FieldType > > right_digests
Definition: authentication_path.hpp:46
std::vector< digest_variable< FieldType > > left_digests
Definition: authentication_path.hpp:45
const std::size_t tree_depth
Definition: authentication_path.hpp:44
snark::merkle_authentication_path get_authentication_path(const std::size_t address) const
Definition: authentication_path.hpp:76
merkle_authentication_path_variable(blueprint< FieldType > &bp, const std::size_t tree_depth)
Definition: authentication_path.hpp:48