28 #ifndef CRYPTO3_ZK_SPARSE_VECTOR_HPP
29 #define CRYPTO3_ZK_SPARSE_VECTOR_HPP
47 template<
typename Type>
53 using underlying_value_type =
54 typename group_type::value_type;
58 std::vector<underlying_value_type>
values;
73 underlying_value_type
operator[](
const std::size_t idx)
const {
76 underlying_value_type();
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]) {
92 }
else if (this->indices[this_pos] < other.
indices[other_pos]) {
93 if (!this->values[this_pos].
is_zero()) {
98 if (!other.
values[other_pos].is_zero()) {
106 while (this_pos < this->
indices.size()) {
107 if (!this->values[this_pos].
is_zero()) {
113 while (other_pos < other.
indices.size()) {
114 if (!other.
values[other_pos].is_zero()) {
123 bool operator==(
const std::vector<underlying_value_type> &other)
const {
124 if (this->domain_size_ < other.size()) {
129 for (std::size_t i = 0; i < other.size(); ++i) {
130 if (this->indices[j] == i) {
131 if (this->values[j] != other[j]) {
150 for (std::size_t i = 0; i + 1 <
indices.size(); ++i) {
176 return indices.size() * (
sizeof(std::size_t) * 8 + Type::value_bits);
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 {
185 const std::size_t chunks = omp_get_max_threads();
188 const std::size_t chunks = 1;
191 underlying_value_type accumulated_value = underlying_value_type::zero();
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,
201 for (std::size_t i = 0; i <
indices.size(); ++i) {
202 const bool matching_pos = (offset <=
indices[i] &&
indices[i] < offset + range_len);
208 if (matching_pos && last_pos == i - 1) {
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);
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,
252 return std::make_pair(accumulated_value, resulting_vector);
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: 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