sparse_vector.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 Declaration of interfaces for a sparse vector.
26 //---------------------------------------------------------------------------//
27 
28 #ifndef CRYPTO3_ZK_SPARSE_VECTOR_HPP
29 #define CRYPTO3_ZK_SPARSE_VECTOR_HPP
30 
31 #include <iostream>
32 #include <vector>
33 #include <numeric>
34 
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace zk {
41  namespace snark {
42 
47  template<typename Type>
48  struct sparse_vector {
49 
50  using group_type = Type;
51 
52  private:
53  using underlying_value_type =
54  typename group_type::value_type;
55  public:
56 
57  std::vector<std::size_t> indices;
58  std::vector<underlying_value_type> values;
59  std::size_t domain_size_;
60 
61  sparse_vector() = default;
62  sparse_vector(const sparse_vector<Type> &other) = default;
63  sparse_vector(sparse_vector<Type> &&other) = default;
64  sparse_vector(std::vector<underlying_value_type> &&v) :
65  values(std::move(v)), domain_size_(values.size()) {
66  indices.resize(domain_size_);
67  std::iota(indices.begin(), indices.end(), 0);
68  }
69 
72 
73  underlying_value_type operator[](const std::size_t idx) const {
74  auto it = std::lower_bound(indices.begin(), indices.end(), idx);
75  return (it != indices.end() && *it == idx) ? values[it - indices.begin()] :
76  underlying_value_type();
77  }
78 
79  bool operator==(const sparse_vector<Type> &other) const {
80  if (this->domain_size_ != other.domain_size_) {
81  return false;
82  }
83 
84  std::size_t this_pos = 0, other_pos = 0;
85  while (this_pos < this->indices.size() && other_pos < other.indices.size()) {
86  if (this->indices[this_pos] == other.indices[other_pos]) {
87  if (this->values[this_pos] != other.values[other_pos]) {
88  return false;
89  }
90  ++this_pos;
91  ++other_pos;
92  } else if (this->indices[this_pos] < other.indices[other_pos]) {
93  if (!this->values[this_pos].is_zero()) {
94  return false;
95  }
96  ++this_pos;
97  } else {
98  if (!other.values[other_pos].is_zero()) {
99  return false;
100  }
101  ++other_pos;
102  }
103  }
104 
105  /* at least one of the vectors has been exhausted, so other must be empty */
106  while (this_pos < this->indices.size()) {
107  if (!this->values[this_pos].is_zero()) {
108  return false;
109  }
110  ++this_pos;
111  }
112 
113  while (other_pos < other.indices.size()) {
114  if (!other.values[other_pos].is_zero()) {
115  return false;
116  }
117  ++other_pos;
118  }
119 
120  return true;
121  }
122 
123  bool operator==(const std::vector<underlying_value_type> &other) const {
124  if (this->domain_size_ < other.size()) {
125  return false;
126  }
127 
128  std::size_t j = 0;
129  for (std::size_t i = 0; i < other.size(); ++i) {
130  if (this->indices[j] == i) {
131  if (this->values[j] != other[j]) {
132  return false;
133  }
134  ++j;
135  } else {
136  if (!other[j].is_zero()) {
137  return false;
138  }
139  }
140  }
141 
142  return true;
143  }
144 
145  bool is_valid() const {
146  if (values.size() == indices.size() && values.size() <= domain_size_) {
147  return false;
148  }
149 
150  for (std::size_t i = 0; i + 1 < indices.size(); ++i) {
151  if (indices[i] >= indices[i + 1]) {
152  return false;
153  }
154  }
155 
156  if (!indices.empty() && indices[indices.size() - 1] >= domain_size_) {
157  return false;
158  }
159 
160  return true;
161  }
162 
163  bool empty() const {
164  return indices.empty();
165  }
166 
167  std::size_t domain_size() const {
168  return domain_size_;
169  }
170 
171  std::size_t size() const {
172  return indices.size();
173  }
174 
175  std::size_t size_in_bits() const {
176  return indices.size() * (sizeof(std::size_t) * 8 + Type::value_bits);
177  }
178 
179  /* return a pair consisting of the accumulated value and the sparse vector of non-accumulated values
180  */
181  template<typename InputBaseIterator>
182  std::pair<underlying_value_type, sparse_vector<Type>>
183  accumulate(InputBaseIterator it_begin, InputBaseIterator it_end, std::size_t offset) const {
184 #ifdef MULTICORE
185  const std::size_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var
186  // or call omp_set_num_threads()
187 #else
188  const std::size_t chunks = 1;
189 #endif
190 
191  underlying_value_type accumulated_value = underlying_value_type::zero();
192  sparse_vector<Type> resulting_vector;
193  resulting_vector.domain_size_ = domain_size_;
194 
195  const std::size_t range_len = std::distance(it_begin, it_end);
196  bool in_block = false;
197  std::size_t first_pos = -1,
198  last_pos = -1; // g++ -flto emits unitialized warning, even though in_block
199  // guards for such cases.
200 
201  for (std::size_t i = 0; i < indices.size(); ++i) {
202  const bool matching_pos = (offset <= indices[i] && indices[i] < offset + range_len);
203  // printf("i = %zu, pos[i] = %zu, offset = %zu, w_size = %zu\n", i, indices[i], offset,
204  // w_size);
205  bool copy_over;
206 
207  if (in_block) {
208  if (matching_pos && last_pos == i - 1) {
209  // block can be extended, do it
210  last_pos = i;
211  copy_over = false;
212  } else {
213  // block has ended here
214  in_block = false;
215  copy_over = true;
216 
217  accumulated_value =
218  accumulated_value +
219  algebra::multiexp<algebra::policies::multiexp_method_bos_coster>(
220  values.begin() + first_pos, values.begin() + last_pos + 1,
221  it_begin + (indices[first_pos] - offset),
222  it_begin + (indices[last_pos] - offset) + 1, chunks);
223  }
224  } else {
225  if (matching_pos) {
226  // block can be started
227  first_pos = i;
228  last_pos = i;
229  in_block = true;
230  copy_over = false;
231  } else {
232  copy_over = true;
233  }
234  }
235 
236  if (copy_over) {
237  resulting_vector.indices.emplace_back(indices[i]);
238  resulting_vector.values.emplace_back(values[i]);
239  }
240  }
241 
242  if (in_block) {
243  accumulated_value =
244  accumulated_value + algebra::multiexp<algebra::policies::multiexp_method_bos_coster>(
245  values.begin() + first_pos,
246  values.begin() + last_pos + 1,
247  it_begin + (indices[first_pos] - offset),
248  it_begin + (indices[last_pos] - offset) + 1,
249  chunks);
250  }
251 
252  return std::make_pair(accumulated_value, resulting_vector);
253  }
254  };
255  } // namespace snark
256  } // namespace zk
257  } // namespace crypto3
258 } // namespace nil
259 
260 #endif // CRYPTO3_ZK_SPARSE_VECTOR_HPP
constexpr vector< T, N > iota(T value=T())
generates a vector containing consecutive elements
Definition: vector/utility.hpp:95
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
bool is_zero(const Range &a)
Definition: basic_operations.hpp:43
Definition: pair.hpp:31
Definition: sparse_vector.hpp:48
std::size_t size() const
Definition: sparse_vector.hpp:171
bool is_valid() const
Definition: sparse_vector.hpp:145
Type group_type
Definition: sparse_vector.hpp:50
sparse_vector(sparse_vector< Type > &&other)=default
bool operator==(const std::vector< underlying_value_type > &other) const
Definition: sparse_vector.hpp:123
bool operator==(const sparse_vector< Type > &other) const
Definition: sparse_vector.hpp:79
underlying_value_type operator[](const std::size_t idx) const
Definition: sparse_vector.hpp:73
sparse_vector(std::vector< underlying_value_type > &&v)
Definition: sparse_vector.hpp:64
std::pair< underlying_value_type, sparse_vector< Type > > accumulate(InputBaseIterator it_begin, InputBaseIterator it_end, std::size_t offset) const
Definition: sparse_vector.hpp:183
sparse_vector< Type > & operator=(const sparse_vector< Type > &other)=default
sparse_vector< Type > & operator=(sparse_vector< Type > &&other)=default
std::vector< underlying_value_type > values
Definition: sparse_vector.hpp:58
std::size_t domain_size() const
Definition: sparse_vector.hpp:167
bool empty() const
Definition: sparse_vector.hpp:163
sparse_vector(const sparse_vector< Type > &other)=default
std::size_t size_in_bits() const
Definition: sparse_vector.hpp:175
std::vector< std::size_t > indices
Definition: sparse_vector.hpp:57
std::size_t domain_size_
Definition: sparse_vector.hpp:59