-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSerialize.hpp
78 lines (58 loc) · 2.3 KB
/
Serialize.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _SNARKFRONT_SERIALIZE_HPP_
#define _SNARKFRONT_SERIALIZE_HPP_
#include <array>
#include <cstdint>
#include <istream>
#include <ostream>
#include <string>
#include <vector>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// read and write types: uint32_t
//
void writeStream(std::ostream& os, const std::uint32_t& a);
bool readStream(std::istream& is, std::uint32_t& a);
////////////////////////////////////////////////////////////////////////////////
// read and write types: uint64_t
//
void writeStream(std::ostream& os, const std::uint64_t& a);
bool readStream(std::istream& is, std::uint64_t& a);
////////////////////////////////////////////////////////////////////////////////
// read and write types: string
//
void writeStream(std::ostream& os, const std::string& a);
bool readStream(std::istream& is, std::string& a);
////////////////////////////////////////////////////////////////////////////////
// read and write types: vector<uint8_t> (byte vector)
//
void writeStream(std::ostream& os, const std::vector<std::uint8_t>& a);
bool readStream(std::istream& is, std::vector<std::uint8_t>& a);
////////////////////////////////////////////////////////////////////////////////
// read and write types: vector<uint32_t> (commitment vector)
//
void writeStream(std::ostream& os, const std::vector<std::uint32_t>& a);
bool readStream(std::istream& is, std::vector<std::uint32_t>& a);
////////////////////////////////////////////////////////////////////////////////
// read and write types: array<uint8_t, N> (byte array)
//
template <std::size_t N>
void writeStream(std::ostream& os, const std::array<std::uint8_t, N>& a) {
os.write(reinterpret_cast<const char*>(a.data()), N);
}
template <std::size_t N>
bool readStream(std::istream& is, std::array<std::uint8_t, N>& a) {
return is.read(reinterpret_cast<char*>(a.data()), N);
}
////////////////////////////////////////////////////////////////////////////////
// read and write types: array<uint32_t, N> (hash digest/trapdoor)
//
template <std::size_t N>
void writeStream(std::ostream& os, const std::array<std::uint32_t, N>& a) {
os << a;
}
template <std::size_t N>
bool readStream(std::istream& is, std::array<std::uint32_t, N>& a) {
return !!(is >> a);
}
} // namespace snarkfront
#endif