blueprint_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 // Copyright (c) 2021 Noam Yemini <@NoamDev at GitHub>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ZK_BLUEPRINT_VARIABLE_HPP
28 #define CRYPTO3_ZK_BLUEPRINT_VARIABLE_HPP
29 
30 #include <vector>
31 
32 #include <nil/crypto3/multiprecision/integer.hpp>
33 #include <nil/crypto3/multiprecision/number.hpp>
34 
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace zk {
40  namespace components {
41 
42  template<typename FieldType>
43  class blueprint;
44 
45  template<typename FieldType>
46  class blueprint_variable : public snark::variable<FieldType> {
47  public:
49  snark::variable<FieldType>(index) {};
50 
52  this->index = bp.allocate_var_index();
53  }
54  };
55 
56  template<typename FieldType>
57  class blueprint_variable_vector : private std::vector<blueprint_variable<FieldType>> {
58  typedef FieldType field_type;
59  typedef typename field_type::value_type field_value_type;
60  typedef std::vector<blueprint_variable<field_type>> contents;
61 
62  public:
63  using typename contents::const_iterator;
64  using typename contents::const_reverse_iterator;
65  using typename contents::iterator;
66  using typename contents::reverse_iterator;
67 
68  using contents::begin;
69  using contents::emplace_back;
70  using contents::empty;
71  using contents::end;
72  using contents::insert;
73  using contents::rbegin;
74  using contents::rend;
75  using contents::reserve;
76  using contents::size;
77  using contents::operator[];
78  using contents::resize;
79 
80  blueprint_variable_vector() : contents() {};
81  blueprint_variable_vector(std::size_t count, const blueprint_variable<field_type> &value) :
82  contents(count, value) {};
83  blueprint_variable_vector(typename contents::const_iterator first,
84  typename contents::const_iterator last) :
85  contents(first, last) {};
86  blueprint_variable_vector(typename contents::const_reverse_iterator first,
87  typename contents::const_reverse_iterator last) :
88  contents(first, last) {};
89 
90  /* allocates blueprint_variable<field_type> array in MSB->LSB order */
91  void allocate(blueprint<field_type> &bp, const std::size_t n) {
92  (*this).resize(n);
93 
94  for (std::size_t i = 0; i < n; ++i) {
95  (*this)[i].allocate(bp);
96  }
97  }
98 
100  const std::vector<field_value_type> &vals) const {
101  assert(this->size() == vals.size());
102  for (std::size_t i = 0; i < vals.size(); ++i) {
103  bp.val((*this)[i]) = vals[i];
104  }
105  }
106 
107  void fill_with_bits(blueprint<field_type> &bp, const std::vector<bool> &bits) const {
108  assert(this->size() == bits.size());
109  for (std::size_t i = 0; i < bits.size(); ++i) {
110  bp.val((*this)[i]) = (bits[i] ? field_value_type::one() : field_value_type::zero());
111  }
112  }
113 
114  void fill_with_bits_of_ulong(blueprint<field_type> &bp, const unsigned long i) const {
115  this->fill_with_bits_of_field_element(bp, field_value_type(i));
116  }
117 
118  void fill_with_bits_of_field_element(blueprint<field_type> &bp, const field_value_type &r) const {
119  for (std::size_t i = 0; i < this->size(); ++i) {
120  bp.val((*this)[i]) = nil::crypto3::multiprecision::bit_test(r.data, i) ?
121  field_value_type::one() :
122  field_value_type::zero();
123  }
124  }
125 
126  std::vector<field_value_type> get_vals(const blueprint<field_type> &bp) const {
127  std::vector<field_value_type> result(this->size());
128  for (std::size_t i = 0; i < this->size(); ++i) {
129  result[i] = bp.val((*this)[i]);
130  }
131  return result;
132  }
133 
134  std::vector<bool> get_bits(const blueprint<field_type> &bp) const {
135  std::vector<bool> result;
136  for (std::size_t i = 0; i < this->size(); ++i) {
137  const field_value_type v = bp.val((*this)[i]);
138  assert(v.is_zero() || v.is_one());
139  result.push_back(v.is_one());
140  }
141  return result;
142  }
143 
144  field_value_type get_field_element_from_bits(const blueprint<field_type> &bp) const {
145  field_value_type result = field_value_type::zero();
146 
147  for (std::size_t i = 0; i < this->size(); ++i) {
148  /* push in the new bit */
149  const field_value_type v = bp.val((*this)[this->size() - 1 - i]);
150  assert(v.is_zero() || v.is_one());
151  result += result + v;
152  }
153 
154  return result;
155  }
156  };
157  } // namespace components
158  } // namespace zk
159  } // namespace crypto3
160 } // namespace nil
161 
162 #endif // CRYPTO3_ZK_BLUEPRINT_VARIABLE_HPP
Definition: blueprint_variable.hpp:57
blueprint_variable_vector()
Definition: blueprint_variable.hpp:80
std::vector< field_value_type > get_vals(const blueprint< field_type > &bp) const
Definition: blueprint_variable.hpp:126
void fill_with_field_elements(blueprint< field_type > &bp, const std::vector< field_value_type > &vals) const
Definition: blueprint_variable.hpp:99
blueprint_variable_vector(std::size_t count, const blueprint_variable< field_type > &value)
Definition: blueprint_variable.hpp:81
field_value_type get_field_element_from_bits(const blueprint< field_type > &bp) const
Definition: blueprint_variable.hpp:144
void fill_with_bits(blueprint< field_type > &bp, const std::vector< bool > &bits) const
Definition: blueprint_variable.hpp:107
void allocate(blueprint< field_type > &bp, const std::size_t n)
Definition: blueprint_variable.hpp:91
blueprint_variable_vector(typename contents::const_iterator first, typename contents::const_iterator last)
Definition: blueprint_variable.hpp:83
void fill_with_bits_of_field_element(blueprint< field_type > &bp, const field_value_type &r) const
Definition: blueprint_variable.hpp:118
std::vector< bool > get_bits(const blueprint< field_type > &bp) const
Definition: blueprint_variable.hpp:134
blueprint_variable_vector(typename contents::const_reverse_iterator first, typename contents::const_reverse_iterator last)
Definition: blueprint_variable.hpp:86
void fill_with_bits_of_ulong(blueprint< field_type > &bp, const unsigned long i) const
Definition: blueprint_variable.hpp:114
Definition: blueprint_variable.hpp:46
void allocate(blueprint< FieldType > &bp)
Definition: blueprint_variable.hpp:51
blueprint_variable(const typename snark::variable< FieldType >::index_type index=0)
Definition: blueprint_variable.hpp:48
Definition: blueprint.hpp:46
FieldType::value_type & val(const blueprint_variable< FieldType > &var)
Definition: blueprint.hpp:70
vector(T, U...) -> vector< std::enable_if_t<(std::is_same_v< T, U > &&...), T >, 1+sizeof...(U)>
deduction guide for uniform initialization
digest< DigestBits > resize(const digest< DigestBits > &od, std::size_t new_size)
Definition: block/include/nil/crypto3/detail/digest.hpp:131
digest< NewBits > reserve(const digest< OldBits > &od)
Definition: block/include/nil/crypto3/detail/digest.hpp:115
Definition: pair.hpp:31
Definition: variable.hpp:66
index_type index
Definition: variable.hpp:69
variable(const index_type index=0)
Definition: variable.hpp:71
std::size_t index_type
Definition: variable.hpp:68