set_membership_proof_variable.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_SET_MEMBERSHIP_PROOF_VARIABLE_HPP
30 #define CRYPTO3_ZK_BLUEPRINT_SET_MEMBERSHIP_PROOF_VARIABLE_HPP
31 
35 #include <nil/crypto3/zk/components/merkle_tree/merkle_authentication_path_variable.hpp>
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace zk {
40  namespace snark {
41  namespace components {
42 
43  template<typename FieldType, typename Hash>
44  class set_membership_proof_variable : public component<FieldType> {
45  public:
46  blueprint_variable_vector<FieldType> address_bits;
47  std::shared_ptr<merkle_authentication_path_variable<FieldType, Hash>> merkle_path;
48 
49  const std::size_t max_entries;
50  const std::size_t tree_depth;
51 
52  set_membership_proof_variable(blueprint<FieldType> &bp, const std::size_t max_entries) :
53  component<FieldType>(bp), max_entries(max_entries),
54  tree_depth(static_cast<std::size_t>(std::ceil(std::log2(max_entries)))) {
55  if (tree_depth > 0) {
56  address_bits.allocate(bp, tree_depth);
57  merkle_path.reset(
58  new merkle_authentication_path_variable<FieldType, Hash>(bp, tree_depth));
59  }
60  }
61 
63  if (tree_depth > 0) {
64  for (std::size_t i = 0; i < tree_depth; ++i) {
65  generate_boolean_r1cs_constraint<FieldType>(this->bp, address_bits[i]);
66  }
67  merkle_path->generate_r1cs_constraints();
68  }
69  }
71  if (tree_depth > 0) {
72  address_bits.fill_with_bits_of_field_element(
73  this->bp, typename FieldType::value_type(proof.address));
74  merkle_path->generate_r1cs_witness(proof.address, proof.merkle_path);
75  }
76  }
77 
79  set_membership_proof result;
80 
81  if (tree_depth == 0) {
82  result.address = 0;
83  } else {
84  result.address = address_bits.get_field_element_from_bits(this->bp).as_ulong();
85  result.merkle_path = merkle_path->get_authentication_path(result.address);
86  }
87 
88  return result;
89  }
90 
93 
94  blueprint<FieldType> bp;
95  const std::size_t max_entries = (1ul << (proof.merkle_path.size()));
97  "proof_variable");
98  proof_variable.generate_r1cs_witness(proof);
99 
100  return bp.full_variable_assignment();
101  }
102  };
103  } // namespace components
104  } // namespace snark
105  } // namespace zk
106  } // namespace crypto3
107 } // namespace nil
108 
109 #endif // CRYPTO3_ZK_BLUEPRINT_SET_MEMBERSHIP_PROOF_VARIABLE_HPP
Definition: set_membership_proof_variable.hpp:44
set_membership_proof get_membership_proof() const
Definition: set_membership_proof_variable.hpp:78
static snark::r1cs_variable_assignment< FieldType > as_r1cs_variable_assignment(const set_membership_proof &proof)
Definition: set_membership_proof_variable.hpp:92
void generate_r1cs_witness(const set_membership_proof &proof)
Definition: set_membership_proof_variable.hpp:70
const std::size_t tree_depth
Definition: set_membership_proof_variable.hpp:50
void generate_r1cs_constraints()
Definition: set_membership_proof_variable.hpp:62
set_membership_proof_variable(blueprint< FieldType > &bp, const std::size_t max_entries)
Definition: set_membership_proof_variable.hpp:52
std::shared_ptr< merkle_authentication_path_variable< FieldType, Hash > > merkle_path
Definition: set_membership_proof_variable.hpp:47
const std::size_t max_entries
Definition: set_membership_proof_variable.hpp:49
blueprint_variable_vector< FieldType > address_bits
Definition: set_membership_proof_variable.hpp:46
Definition: snark/proof.hpp:37
std::vector< typename FieldType::value_type > r1cs_variable_assignment
Definition: r1cs.hpp:107
Definition: pair.hpp:31
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
merkle_authentication_path merkle_path
Definition: blueprint/include/nil/crypto3/zk/components/schemes/snark/set_commitment.hpp:41