integer_permutation.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_SNARK_INTEGER_PERMUTATION_HPP
27 #define CRYPTO3_ZK_SNARK_INTEGER_PERMUTATION_HPP
28 
29 #include <algorithm>
30 #include <cstddef>
31 #include <vector>
32 #include <unordered_set>
33 #include <numeric>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace zk {
38  namespace snark {
40  private:
41  std::vector<std::size_t> contents; /* offset by min_element */
42 
43  public:
44  std::size_t min_element;
45  std::size_t max_element;
46 
47  integer_permutation(const std::size_t size = 0) : min_element(0), max_element(size - 1) {
48  contents.resize(size);
49  std::iota(contents.begin(), contents.end(), 0);
50  }
51  integer_permutation(const std::size_t min_element, const std::size_t max_element) :
53  assert(min_element <= max_element);
54  const std::size_t size = max_element - min_element + 1;
55  contents.resize(size);
56  std::iota(contents.begin(), contents.end(), min_element);
57  }
58 
60 
61  std::size_t size() const {
62  return max_element - min_element + 1;
63  }
64 
65  std::vector<std::size_t> &data() {
66  return contents;
67  }
68 
69  const std::vector<std::size_t> &data() const {
70  return contents;
71  }
72 
73  bool operator==(const integer_permutation &other) const {
74  return (this->min_element == other.min_element && this->max_element == other.max_element &&
75  this->contents == other.contents);
76  }
77 
78  void set(const std::size_t position, const std::size_t value) {
79  assert(min_element <= position && position <= max_element);
80  contents[position - min_element] = value;
81  }
82  std::size_t get(const std::size_t position) const {
83  assert(min_element <= position && position <= max_element);
84  return contents[position - min_element];
85  }
86 
87  bool is_valid() const {
88  std::unordered_set<std::size_t> elems;
89 
90  for (auto &el : contents) {
91  if (el < min_element || el > max_element || elems.find(el) != elems.end()) {
92  return false;
93  }
94 
95  elems.insert(el);
96  }
97 
98  return true;
99  }
100 
103 
104  for (std::size_t position = min_element; position <= max_element; ++position) {
105  result.contents[this->contents[position - min_element] - min_element] = position;
106  }
107 
108 #ifdef DEBUG
109  assert(result.is_valid());
110 #endif
111 
112  return result;
113  }
114 
115  integer_permutation slice(const std::size_t slice_min_element,
116  const std::size_t slice_max_element) const {
117  assert(min_element <= slice_min_element && slice_min_element <= slice_max_element &&
118  slice_max_element <= max_element);
119  integer_permutation result(slice_min_element, slice_max_element);
120  std::copy(this->contents.begin() + (slice_min_element - min_element),
121  this->contents.begin() + (slice_max_element - min_element) + 1,
122  result.contents.begin());
123 #ifdef DEBUG
124  assert(result.is_valid());
125 #endif
126 
127  return result;
128  }
129 
130  /* Similarly to std::next_permutation this transforms the current
131  integer permutation into the next lexicographically ordered
132  permutation; returns false if the last permutation was reached and
133  this is now the identity permutation on [min_element .. max_element] */
135  return std::next_permutation(contents.begin(), contents.end());
136  }
137 
138  void random_shuffle() {
139  return std::random_shuffle(contents.begin(), contents.end());
140  }
141  };
142  } // namespace snark
143  } // namespace zk
144  } // namespace crypto3
145 } // namespace nil
146 
147 #endif // CRYPTO3_ZK_SNARK_INTEGER_PERMUTATION_HPP
Definition: integer_permutation.hpp:39
integer_permutation(const std::size_t min_element, const std::size_t max_element)
Definition: integer_permutation.hpp:51
const std::vector< std::size_t > & data() const
Definition: integer_permutation.hpp:69
std::size_t get(const std::size_t position) const
Definition: integer_permutation.hpp:82
std::vector< std::size_t > & data()
Definition: integer_permutation.hpp:65
void random_shuffle()
Definition: integer_permutation.hpp:138
std::size_t min_element
Definition: integer_permutation.hpp:44
bool next_permutation()
Definition: integer_permutation.hpp:134
std::size_t size() const
Definition: integer_permutation.hpp:61
void set(const std::size_t position, const std::size_t value)
Definition: integer_permutation.hpp:78
integer_permutation inverse() const
Definition: integer_permutation.hpp:101
bool is_valid() const
Definition: integer_permutation.hpp:87
integer_permutation(const std::size_t size=0)
Definition: integer_permutation.hpp:47
bool operator==(const integer_permutation &other) const
Definition: integer_permutation.hpp:73
integer_permutation slice(const std::size_t slice_min_element, const std::size_t slice_max_element) const
Definition: integer_permutation.hpp:115
integer_permutation & operator=(const integer_permutation &other)=default
std::size_t max_element
Definition: integer_permutation.hpp:45
constexpr vector< T, N > iota(T value=T())
generates a vector containing consecutive elements
Definition: vector/utility.hpp:95
Definition: pair.hpp:31