Electroneum
cryptonote_boost_serialization.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <boost/serialization/vector.hpp>
35 #include <boost/serialization/utility.hpp>
36 #include <boost/serialization/variant.hpp>
37 #include <boost/serialization/set.hpp>
38 #include <boost/serialization/map.hpp>
39 #include <boost/serialization/is_bitwise_serializable.hpp>
40 #include <boost/archive/binary_iarchive.hpp>
41 #include <boost/archive/portable_binary_iarchive.hpp>
42 #include <boost/archive/portable_binary_oarchive.hpp>
43 #include "cryptonote_basic.h"
45 #include "crypto/crypto.h"
46 #include "ringct/rctTypes.h"
47 #include "ringct/rctOps.h"
48 
49 //namespace cryptonote {
50 namespace boost
51 {
52  namespace serialization
53  {
54 
55  //---------------------------------------------------
56  template <class Archive>
57  inline void serialize(Archive &a, crypto::public_key &x, const boost::serialization::version_type ver)
58  {
59  a & reinterpret_cast<char (&)[sizeof(crypto::public_key)]>(x);
60  }
61  template <class Archive>
62  inline void serialize(Archive &a, crypto::secret_key &x, const boost::serialization::version_type ver)
63  {
64  a & reinterpret_cast<char (&)[sizeof(crypto::secret_key)]>(x);
65  }
66  template <class Archive>
67  inline void serialize(Archive &a, crypto::key_derivation &x, const boost::serialization::version_type ver)
68  {
69  a & reinterpret_cast<char (&)[sizeof(crypto::key_derivation)]>(x);
70  }
71  template <class Archive>
72  inline void serialize(Archive &a, crypto::key_image &x, const boost::serialization::version_type ver)
73  {
74  a & reinterpret_cast<char (&)[sizeof(crypto::key_image)]>(x);
75  }
76 
77  template <class Archive>
78  inline void serialize(Archive &a, crypto::signature &x, const boost::serialization::version_type ver)
79  {
80  a & reinterpret_cast<char (&)[sizeof(crypto::signature)]>(x);
81  }
82  template <class Archive>
83  inline void serialize(Archive &a, crypto::hash &x, const boost::serialization::version_type ver)
84  {
85  a & reinterpret_cast<char (&)[sizeof(crypto::hash)]>(x);
86  }
87 
88  template <class Archive>
89  inline void serialize(Archive &a, cryptonote::txout_to_script &x, const boost::serialization::version_type ver)
90  {
91  a & x.keys;
92  a & x.script;
93  }
94 
95 
96  template <class Archive>
97  inline void serialize(Archive &a, cryptonote::txout_to_key &x, const boost::serialization::version_type ver)
98  {
99  a & x.key;
100  }
101 
102  template <class Archive>
103  inline void serialize(Archive &a, cryptonote::txout_to_scripthash &x, const boost::serialization::version_type ver)
104  {
105  a & x.hash;
106  }
107 
108  template <class Archive>
109  inline void serialize(Archive &a, cryptonote::txin_gen &x, const boost::serialization::version_type ver)
110  {
111  a & x.height;
112  }
113 
114  template <class Archive>
115  inline void serialize(Archive &a, cryptonote::txin_to_script &x, const boost::serialization::version_type ver)
116  {
117  a & x.prev;
118  a & x.prevout;
119  a & x.sigset;
120  }
121 
122  template <class Archive>
123  inline void serialize(Archive &a, cryptonote::txin_to_scripthash &x, const boost::serialization::version_type ver)
124  {
125  a & x.prev;
126  a & x.prevout;
127  a & x.script;
128  a & x.sigset;
129  }
130 
131  template <class Archive>
132  inline void serialize(Archive &a, cryptonote::txin_to_key &x, const boost::serialization::version_type ver)
133  {
134  a & x.amount;
135  a & x.key_offsets;
136  a & x.k_image;
137  }
138 
139  template <class Archive>
140  inline void serialize(Archive &a, cryptonote::tx_out &x, const boost::serialization::version_type ver)
141  {
142  a & x.amount;
143  a & x.target;
144  }
145 
146 
147  template <class Archive>
148  inline void serialize(Archive &a, cryptonote::transaction_prefix &x, const boost::serialization::version_type ver)
149  {
150  a & x.version;
151  a & x.unlock_time;
152  a & x.vin;
153  a & x.vout;
154  a & x.extra;
155  }
156 
157  template <class Archive>
158  inline void serialize(Archive &a, cryptonote::transaction &x, const boost::serialization::version_type ver)
159  {
160  a & x.version;
161  a & x.unlock_time;
162  a & x.vin;
163  a & x.vout;
164  a & x.extra;
165  if (x.version == 1)
166  {
167  a & x.signatures;
168  }
169  else
170  {
173  a & x.rct_signatures.p;
174  }
175  }
176 
177  template <class Archive>
178  inline void serialize(Archive &a, cryptonote::block &b, const boost::serialization::version_type ver)
179  {
180  a & b.major_version;
181  a & b.minor_version;
182  a & b.timestamp;
183  a & b.prev_id;
184  a & b.nonce;
185  //------------------
186  a & b.miner_tx;
187  a & b.tx_hashes;
188  }
189 
190  template <class Archive>
191  inline void serialize(Archive &a, rct::key &x, const boost::serialization::version_type ver)
192  {
193  a & reinterpret_cast<char (&)[sizeof(rct::key)]>(x);
194  }
195 
196  template <class Archive>
197  inline void serialize(Archive &a, rct::ctkey &x, const boost::serialization::version_type ver)
198  {
199  a & x.dest;
200  a & x.mask;
201  }
202 
203  template <class Archive>
204  inline void serialize(Archive &a, rct::rangeSig &x, const boost::serialization::version_type ver)
205  {
206  a & x.asig;
207  a & x.Ci;
208  }
209 
210  template <class Archive>
211  inline void serialize(Archive &a, rct::boroSig &x, const boost::serialization::version_type ver)
212  {
213  a & x.s0;
214  a & x.s1;
215  a & x.ee;
216  }
217 
218  template <class Archive>
219  inline void serialize(Archive &a, rct::mgSig &x, const boost::serialization::version_type ver)
220  {
221  a & x.ss;
222  a & x.cc;
223  // a & x.II; // not serialized, we can recover it from the tx vin
224  }
225 
226  template <class Archive>
227  inline void serialize(Archive &a, rct::ecdhTuple &x, const boost::serialization::version_type ver)
228  {
229  a & x.mask;
230  a & x.amount;
231  // a & x.senderPk; // not serialized, as we do not use it in electroneum currently
232  }
233 
234  template <class Archive>
235  inline typename std::enable_if<Archive::is_loading::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
236  {
237  rct::keyV outPk;
238  a & outPk;
239  outPk_.resize(outPk.size());
240  for (size_t n = 0; n < outPk_.size(); ++n)
241  {
242  outPk_[n].dest = rct::identity();
243  outPk_[n].mask = outPk[n];
244  }
245  }
246 
247  template <class Archive>
248  inline typename std::enable_if<Archive::is_saving::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
249  {
250  rct::keyV outPk(outPk_.size());
251  for (size_t n = 0; n < outPk_.size(); ++n)
252  outPk[n] = outPk_[n].mask;
253  a & outPk;
254  }
255 
256  template <class Archive>
257  inline void serialize(Archive &a, rct::rctSigBase &x, const boost::serialization::version_type ver)
258  {
259  a & x.type;
260  if (x.type == rct::RCTTypeNull)
261  return;
262  if (x.type != rct::RCTTypeFull && x.type != rct::RCTTypeSimple)
263  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
264  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
265  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
266  if (x.type == rct::RCTTypeSimple)
267  a & x.pseudoOuts;
268  a & x.ecdhInfo;
269  serializeOutPk(a, x.outPk, ver);
270  a & x.txnFee;
271  }
272 
273  template <class Archive>
274  inline void serialize(Archive &a, rct::rctSigPrunable &x, const boost::serialization::version_type ver)
275  {
276  a & x.rangeSigs;
277  a & x.MGs;
278  }
279 
280  template <class Archive>
281  inline void serialize(Archive &a, rct::rctSig &x, const boost::serialization::version_type ver)
282  {
283  a & x.type;
284  if (x.type == rct::RCTTypeNull)
285  return;
286  if (x.type != rct::RCTTypeFull && x.type != rct::RCTTypeSimple)
287  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
288  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
289  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
290  if (x.type == rct::RCTTypeSimple)
291  a & x.pseudoOuts;
292  a & x.ecdhInfo;
293  serializeOutPk(a, x.outPk, ver);
294  a & x.txnFee;
295  //--------------
296  a & x.p.rangeSigs;
297  a & x.p.MGs;
298  }
299 }
300 }
301 
302 //}
Definition: binary_utils.h:37
key ee
Definition: rctTypes.h:133
xmr_amount txnFee
Definition: rctTypes.h:189
key64 Ci
Definition: rctTypes.h:162
Definition: cryptonote_basic.h:115
Definition: rctTypes.h:247
txout_to_script script
Definition: cryptonote_basic.h:119
Definition: unordered_containers_boost_serialization.h:38
vector< rangeSig > rangeSigs
Definition: rctTypes.h:248
POD_CLASS key_derivation
Definition: crypto.h:89
Definition: cryptonote_basic.h:382
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:127
Definition: rctTypes.h:100
key mask
Definition: rctTypes.h:114
key amount
Definition: rctTypes.h:115
vector< ctkey > ctkeyV
Definition: rctTypes.h:104
Definition: rctTypes.h:178
size_t prevout
Definition: cryptonote_basic.h:118
Definition: rctTypes.h:177
std::enable_if< Archive::is_loading::value, void >::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
Definition: cryptonote_boost_serialization.h:235
size_t prevout
Definition: cryptonote_basic.h:105
std::vector< uint64_t > key_offsets
Definition: cryptonote_basic.h:133
Definition: cryptonote_basic.h:83
Definition: cryptonote_basic.h:67
Definition: rctTypes.h:113
key dest
Definition: rctTypes.h:101
key64 s0
Definition: rctTypes.h:131
Definition: cryptonote_basic.h:93
std::vector< uint8_t > extra
Definition: cryptonote_basic.h:183
crypto::hash prev
Definition: cryptonote_basic.h:104
Definition: cryptonote_basic.h:172
std::vector< tx_out > vout
Definition: cryptonote_basic.h:181
Definition: cryptonote_basic.h:102
Definition: rctTypes.h:179
std::vector< txin_v > vin
Definition: cryptonote_basic.h:180
std::vector< uint8_t > script
Definition: cryptonote_basic.h:70
Definition: cryptonote_basic.h:78
uint64_t unlock_time
Definition: cryptonote_basic.h:178
rct::rctSig rct_signatures
Definition: cryptonote_basic.h:207
Definition: rctTypes.h:130
rctSigPrunable p
Definition: rctTypes.h:324
uint64_t amount
Definition: cryptonote_basic.h:132
Definition: rctTypes.h:82
int b
Definition: base.py:1
vector< ecdhTuple > ecdhInfo
Definition: rctTypes.h:187
Definition: cryptonote_basic.h:130
Definition: rctTypes.h:323
size_t version
Definition: cryptonote_basic.h:177
type
Definition: json.h:74
Definition: rctTypes.h:160
crypto::hash hash
Definition: cryptonote_basic.h:80
POD_CLASS public_key
Definition: crypto.h:65
Definition: rctTypes.h:143
std::vector< std::vector< crypto::signature > > signatures
Definition: cryptonote_basic.h:206
boroSig asig
Definition: rctTypes.h:161
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:106
POD_CLASS signature
Definition: crypto.h:99
key identity()
Definition: rctOps.h:75
crypto::hash prev
Definition: cryptonote_basic.h:117
crypto::public_key key
Definition: cryptonote_basic.h:87
POD_CLASS key_image
Definition: crypto.h:93
POD_CLASS secret_key
Definition: crypto.h:69
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:120
uint64_t amount
Definition: cryptonote_basic.h:151
uint8_t type
Definition: rctTypes.h:182
txout_target_v target
Definition: cryptonote_basic.h:152
key cc
Definition: rctTypes.h:145
string a
Definition: MakeCryptoOps.py:15
crypto::key_image k_image
Definition: cryptonote_basic.h:134
key mask
Definition: rctTypes.h:102
keyM ss
Definition: rctTypes.h:144
vector< key > keyV
Definition: rctTypes.h:92
std::vector< crypto::public_key > keys
Definition: cryptonote_basic.h:69
POD_CLASS hash
Definition: hash.h:46
Definition: cryptonote_basic.h:149
keyV pseudoOuts
Definition: rctTypes.h:186
size_t height
Definition: cryptonote_basic.h:95
Definition: rctTypes.h:181
Definition: cryptonote_basic.h:198
ctkeyV outPk
Definition: rctTypes.h:188
key64 s1
Definition: rctTypes.h:132
vector< mgSig > MGs
Definition: rctTypes.h:249