Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
sparse_vector.hpp
Go to the documentation of this file.
1 
12 #ifndef SPARSE_VECTOR_HPP_
13 #define SPARSE_VECTOR_HPP_
14 
15 #include <iostream>
16 #include <vector>
17 
18 namespace libsnark
19 {
20 
21 template<typename T> struct sparse_vector;
22 
23 template<typename T>
24 std::ostream &operator<<(std::ostream &out, const sparse_vector<T> &v);
25 
26 template<typename T>
27 std::istream &operator>>(std::istream &in, sparse_vector<T> &v);
28 
33 template<typename T> struct sparse_vector {
34 
35  std::vector<size_t> indices;
36  std::vector<T> values;
37  size_t domain_size_;
38 
39  sparse_vector() = default;
40  sparse_vector(const sparse_vector<T> &other) = default;
41  sparse_vector(sparse_vector<T> &&other) = default;
42  sparse_vector(std::vector<T> &&v);
43 
44  sparse_vector<T> &operator=(const sparse_vector<T> &other) = default;
45  sparse_vector<T> &operator=(sparse_vector<T> &&other) = default;
46 
47  T operator[](const size_t idx) const;
48 
49  bool operator==(const sparse_vector<T> &other) const;
50  bool operator==(const std::vector<T> &other) const;
51 
52  bool is_valid() const;
53  bool empty() const;
54 
55  // return domain_size_
56  size_t domain_size() const;
57  // return the number of indices (representing the number of
58  // non-zero entries)
59  size_t size() const;
60  // return the number bits needed to store the sparse vector
61  size_t size_in_bits() const;
62 
63  // return a pair consisting of the accumulated value and the
64  // sparse vector of non-accumulated values
65  template<typename FieldT>
66  std::pair<T, sparse_vector<T>> accumulate(
67  const typename std::vector<FieldT>::const_iterator &it_begin,
68  const typename std::vector<FieldT>::const_iterator &it_end,
69  const size_t offset) const;
70 
71  friend std::ostream &operator<<<T>(
72  std::ostream &out, const sparse_vector<T> &v);
73  friend std::istream &operator>><T>(std::istream &in, sparse_vector<T> &v);
74 };
75 
76 template<typename T>
77 std::ostream &operator<<(std::ostream &out, const sparse_vector<T> &v);
78 
79 template<typename T>
80 std::istream &operator>>(std::istream &in, sparse_vector<T> &v);
81 
82 } // namespace libsnark
83 
85 
86 #endif // SPARSE_VECTOR_HPP_
libsnark::sparse_vector::operator[]
T operator[](const size_t idx) const
libsnark::sparse_vector::is_valid
bool is_valid() const
libsnark
Definition: accumulation_vector.hpp:18
libsnark::operator<<
std::ostream & operator<<(std::ostream &out, const accumulation_vector< T > &v)
libsnark::sparse_vector::sparse_vector
sparse_vector()=default
libsnark::sparse_vector::domain_size_
size_t domain_size_
Definition: sparse_vector.hpp:37
sparse_vector.tcc
libsnark::sparse_vector::size_in_bits
size_t size_in_bits() const
libsnark::sparse_vector::accumulate
std::pair< T, sparse_vector< T > > accumulate(const typename std::vector< FieldT >::const_iterator &it_begin, const typename std::vector< FieldT >::const_iterator &it_end, const size_t offset) const
libsnark::sparse_vector::operator=
sparse_vector< T > & operator=(const sparse_vector< T > &other)=default
libsnark::sparse_vector::size
size_t size() const
libsnark::sparse_vector::values
std::vector< T > values
Definition: sparse_vector.hpp:36
libsnark::operator>>
std::istream & operator>>(std::istream &in, accumulation_vector< T > &v)
libsnark::sparse_vector::empty
bool empty() const
libsnark::sparse_vector::operator==
bool operator==(const sparse_vector< T > &other) const
libsnark::sparse_vector
Definition: sparse_vector.hpp:21
libsnark::sparse_vector::indices
std::vector< size_t > indices
Definition: sparse_vector.hpp:35
libsnark::sparse_vector::domain_size
size_t domain_size() const