accumulation_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 // Copyright (c) 2021 Ilias Khairullin <ilias@nil.foundation>
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_SNARK_ACCUMULATION_VECTOR_HPP
28 #define CRYPTO3_ZK_SNARK_ACCUMULATION_VECTOR_HPP
29 
30 #include <iostream>
31 #include <iterator>
32 
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace zk {
38  namespace snark {
39 
45  template<typename Type>
47  using underlying_value_type = typename Type::value_type;
48 
49  public:
50  using group_type = Type;
51 
52  underlying_value_type first;
54 
55  accumulation_vector() = default;
58  accumulation_vector(const underlying_value_type &first, sparse_vector<Type> &&rest) :
59  first(first), rest(std::move(rest)) {};
60  accumulation_vector(underlying_value_type &&first, sparse_vector<Type> &&rest) :
61  first(std::move(first)), rest(std::move(rest)) {};
62  accumulation_vector(underlying_value_type &&first, std::vector<underlying_value_type> &&v) :
63  first(std::move(first)), rest(std::move(v)) {
64  }
65  accumulation_vector(std::vector<underlying_value_type> &&v) :
66  first(underlying_value_type::zero()), rest(std::move(v)) {};
67 
70 
71  bool operator==(const accumulation_vector<Type> &other) const {
72  return (this->first == other.first && this->rest == other.rest);
73  }
74 
75  bool is_fully_accumulated() const {
76  return rest.empty();
77  }
78 
79  std::size_t domain_size() const {
80  return rest.domain_size();
81  }
82 
83  std::size_t size() const {
84  return rest.domain_size();
85  }
86 
87  std::size_t size_in_bits() const {
88  const std::size_t first_size_in_bits = Type::value_bits;
89  const std::size_t rest_size_in_bits = rest.size_in_bits();
90  return first_size_in_bits + rest_size_in_bits;
91  }
92 
93  template<typename InputIterator>
94  accumulation_vector<Type> accumulate_chunk(InputIterator begin, InputIterator end,
95  std::size_t offset) const {
96  std::pair<underlying_value_type, sparse_vector<Type>> acc_result =
97  rest.accumulate(begin, end, offset);
98  underlying_value_type new_first = first + acc_result.first;
99  return accumulation_vector<Type>(std::move(new_first), std::move(acc_result.second));
100  }
101  };
102  } // namespace snark
103  } // namespace zk
104  } // namespace crypto3
105 } // namespace nil
106 
107 #endif // CRYPTO3_ZK_SNARK_ACCUMULATION_VECTOR_HPP
Definition: accumulation_vector.hpp:46
Type group_type
Definition: accumulation_vector.hpp:50
accumulation_vector< Type > & operator=(accumulation_vector< Type > &&other)=default
bool is_fully_accumulated() const
Definition: accumulation_vector.hpp:75
std::size_t size() const
Definition: accumulation_vector.hpp:83
accumulation_vector(underlying_value_type &&first, std::vector< underlying_value_type > &&v)
Definition: accumulation_vector.hpp:62
std::size_t domain_size() const
Definition: accumulation_vector.hpp:79
accumulation_vector(std::vector< underlying_value_type > &&v)
Definition: accumulation_vector.hpp:65
accumulation_vector< Type > & operator=(const accumulation_vector< Type > &other)=default
accumulation_vector(const accumulation_vector< Type > &other)=default
sparse_vector< Type > rest
Definition: accumulation_vector.hpp:53
accumulation_vector(underlying_value_type &&first, sparse_vector< Type > &&rest)
Definition: accumulation_vector.hpp:60
bool operator==(const accumulation_vector< Type > &other) const
Definition: accumulation_vector.hpp:71
accumulation_vector(const underlying_value_type &first, sparse_vector< Type > &&rest)
Definition: accumulation_vector.hpp:58
accumulation_vector< Type > accumulate_chunk(InputIterator begin, InputIterator end, std::size_t offset) const
Definition: accumulation_vector.hpp:94
accumulation_vector(accumulation_vector< Type > &&other)=default
std::size_t size_in_bits() const
Definition: accumulation_vector.hpp:87
underlying_value_type first
Definition: accumulation_vector.hpp:52
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: sparse_vector.hpp:48