Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
serialization.hpp
Go to the documentation of this file.
1 
12 #ifndef SERIALIZATION_HPP_
13 #define SERIALIZATION_HPP_
14 
15 #include <istream>
16 #include <map>
17 #include <ostream>
18 #include <set>
19 #include <vector>
20 
21 namespace libff
22 {
23 
24 /*
25  * @todo
26  * The serialization is fragile. Shoud be rewritten using a standard,
27  * portable-format library like boost::serialize.
28  *
29  * However, for now the following conventions are used within the code.
30  *
31  * All algebraic objects support either binary or decimal output using
32  * the standard C++ stream operators (operator<<, operator>>).
33  *
34  * The binary mode is activated by defining a BINARY_OUTPUT
35  * preprocessor macro (e.g. g++ -DBINARY_OUTPUT ...).
36  *
37  * Binary output assumes that the stream is to be binary read at its
38  * current position so any white space should be consumed beforehand.
39  *
40  * Consecutive algebraic objects are separated by OUTPUT_NEWLINE and
41  * within themselves (e.g. X and Y coordinates for field elements) with
42  * OUTPUT_SEPARATOR (as defined below).
43  *
44  * Therefore to dump two integers, two Fp elements and another integer
45  * one would:
46  *
47  * out << 3 << "\n";
48  * out << 4 << "\n";
49  * out << FieldT(56) << OUTPUT_NEWLINE;
50  * out << FieldT(78) << OUTPUT_NEWLINE;
51  * out << 9 << "\n";
52  *
53  * Then reading back it its reader's responsibility (!) to consume "\n"
54  * after 4, but Fp::operator<< will correctly consume OUTPUT_NEWLINE.
55  *
56  * The reader should also consume "\n" after 9, so that another field
57  * element can be properly chained. This is especially important for
58  * binary output.
59  *
60  * The binary serialization of algebraic objects is currently *not*
61  * portable between machines of different word sizes.
62  */
63 
64 #ifdef BINARY_OUTPUT
65 #define OUTPUT_NEWLINE ""
66 #define OUTPUT_SEPARATOR ""
67 #else
68 #define OUTPUT_NEWLINE "\n"
69 #define OUTPUT_SEPARATOR " "
70 #endif
71 
72 inline void consume_newline(std::istream &in);
73 inline void consume_OUTPUT_NEWLINE(std::istream &in);
74 inline void consume_OUTPUT_SEPARATOR(std::istream &in);
75 
76 inline void output_bool(std::ostream &out, const bool b);
77 inline void input_bool(std::istream &in, bool &b);
78 
79 inline void output_bool_vector(std::ostream &out, const std::vector<bool> &v);
80 inline void input_bool_vector(std::istream &in, std::vector<bool> &v);
81 
82 template<typename T> T reserialize(const T &obj);
83 
84 template<typename T>
85 std::ostream &operator<<(std::ostream &out, const std::vector<T> &v);
86 
87 template<typename T>
88 std::istream &operator>>(std::ostream &out, std::vector<T> &v);
89 
90 template<typename T1, typename T2>
91 std::ostream &operator<<(std::ostream &out, const std::map<T1, T2> &m);
92 
93 template<typename T1, typename T2>
94 std::istream &operator>>(std::istream &in, std::map<T1, T2> &m);
95 
96 template<typename T>
97 std::ostream &operator<<(std::ostream &out, const std::set<T> &s);
98 
99 template<typename T> std::istream &operator>>(std::istream &in, std::set<T> &s);
100 
101 } // namespace libff
102 
104 
105 #endif // SERIALIZATION_HPP_
libff
Definition: ffi.cpp:8
libff::operator>>
std::istream & operator>>(std::istream &in, alt_bn128_G1 &g)
Definition: alt_bn128_g1.cpp:446
libff::output_bool_vector
void output_bool_vector(std::ostream &out, const std::vector< bool > &v)
libff::consume_OUTPUT_SEPARATOR
void consume_OUTPUT_SEPARATOR(std::istream &in)
libff::input_bool
void input_bool(std::istream &in, bool &b)
libff::operator<<
std::ostream & operator<<(std::ostream &out, const alt_bn128_G1 &g)
Definition: alt_bn128_g1.cpp:436
libff::consume_OUTPUT_NEWLINE
void consume_OUTPUT_NEWLINE(std::istream &in)
libff::output_bool
void output_bool(std::ostream &out, const bool b)
libff::consume_newline
void consume_newline(std::istream &in)
serialization.tcc
libff::input_bool_vector
void input_bool_vector(std::istream &in, std::vector< bool > &v)
libff::reserialize
T reserialize(const T &obj)