Monero
cryptonote_boost_serialization.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2020, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 
33 #include <boost/serialization/vector.hpp>
34 #include <boost/serialization/utility.hpp>
35 #include <boost/serialization/variant.hpp>
36 #include <boost/serialization/set.hpp>
37 #include <boost/serialization/map.hpp>
38 #include <boost/serialization/is_bitwise_serializable.hpp>
39 #include <boost/archive/portable_binary_iarchive.hpp>
40 #include <boost/archive/portable_binary_oarchive.hpp>
41 #include "cryptonote_basic.h"
42 #include "difficulty.h"
44 #include "crypto/crypto.h"
45 #include "ringct/rctTypes.h"
46 #include "ringct/rctOps.h"
47 
48 namespace boost
49 {
50  namespace serialization
51  {
52 
53  //---------------------------------------------------
54  template <class Archive>
55  inline void serialize(Archive &a, crypto::public_key &x, const boost::serialization::version_type ver)
56  {
57  a & reinterpret_cast<char (&)[sizeof(crypto::public_key)]>(x);
58  }
59  template <class Archive>
60  inline void serialize(Archive &a, crypto::secret_key &x, const boost::serialization::version_type ver)
61  {
62  a & reinterpret_cast<char (&)[sizeof(crypto::secret_key)]>(x);
63  }
64  template <class Archive>
65  inline void serialize(Archive &a, crypto::key_derivation &x, const boost::serialization::version_type ver)
66  {
67  a & reinterpret_cast<char (&)[sizeof(crypto::key_derivation)]>(x);
68  }
69  template <class Archive>
70  inline void serialize(Archive &a, crypto::key_image &x, const boost::serialization::version_type ver)
71  {
72  a & reinterpret_cast<char (&)[sizeof(crypto::key_image)]>(x);
73  }
74 
75  template <class Archive>
76  inline void serialize(Archive &a, crypto::signature &x, const boost::serialization::version_type ver)
77  {
78  a & reinterpret_cast<char (&)[sizeof(crypto::signature)]>(x);
79  }
80  template <class Archive>
81  inline void serialize(Archive &a, crypto::hash &x, const boost::serialization::version_type ver)
82  {
83  a & reinterpret_cast<char (&)[sizeof(crypto::hash)]>(x);
84  }
85  template <class Archive>
86  inline void serialize(Archive &a, crypto::hash8 &x, const boost::serialization::version_type ver)
87  {
88  a & reinterpret_cast<char (&)[sizeof(crypto::hash8)]>(x);
89  }
90 
91  template <class Archive>
92  inline void serialize(Archive &a, cryptonote::txout_to_script &x, const boost::serialization::version_type ver)
93  {
94  a & x.keys;
95  a & x.script;
96  }
97 
98 
99  template <class Archive>
100  inline void serialize(Archive &a, cryptonote::txout_to_key &x, const boost::serialization::version_type ver)
101  {
102  a & x.key;
103  }
104 
105  template <class Archive>
106  inline void serialize(Archive &a, cryptonote::txout_to_scripthash &x, const boost::serialization::version_type ver)
107  {
108  a & x.hash;
109  }
110 
111  template <class Archive>
112  inline void serialize(Archive &a, cryptonote::txin_gen &x, const boost::serialization::version_type ver)
113  {
114  a & x.height;
115  }
116 
117  template <class Archive>
118  inline void serialize(Archive &a, cryptonote::txin_to_script &x, const boost::serialization::version_type ver)
119  {
120  a & x.prev;
121  a & x.prevout;
122  a & x.sigset;
123  }
124 
125  template <class Archive>
126  inline void serialize(Archive &a, cryptonote::txin_to_scripthash &x, const boost::serialization::version_type ver)
127  {
128  a & x.prev;
129  a & x.prevout;
130  a & x.script;
131  a & x.sigset;
132  }
133 
134  template <class Archive>
135  inline void serialize(Archive &a, cryptonote::txin_to_key &x, const boost::serialization::version_type ver)
136  {
137  a & x.amount;
138  a & x.key_offsets;
139  a & x.k_image;
140  }
141 
142  template <class Archive>
143  inline void serialize(Archive &a, cryptonote::tx_out &x, const boost::serialization::version_type ver)
144  {
145  a & x.amount;
146  a & x.target;
147  }
148 
149 
150  template <class Archive>
151  inline void serialize(Archive &a, cryptonote::transaction_prefix &x, const boost::serialization::version_type ver)
152  {
153  a & x.version;
154  a & x.unlock_time;
155  a & x.vin;
156  a & x.vout;
157  a & x.extra;
158  }
159 
160  template <class Archive>
161  inline void serialize(Archive &a, cryptonote::transaction &x, const boost::serialization::version_type ver)
162  {
163  a & x.version;
164  a & x.unlock_time;
165  a & x.vin;
166  a & x.vout;
167  a & x.extra;
168  if (x.version == 1)
169  {
170  a & x.signatures;
171  }
172  else
173  {
176  a & x.rct_signatures.p;
177  }
178  }
179 
180  template <class Archive>
181  inline void serialize(Archive &a, cryptonote::block &b, const boost::serialization::version_type ver)
182  {
183  a & b.major_version;
184  a & b.minor_version;
185  a & b.timestamp;
186  a & b.prev_id;
187  a & b.nonce;
188  //------------------
189  a & b.miner_tx;
190  a & b.tx_hashes;
191  }
192 
193  template <class Archive>
194  inline void serialize(Archive &a, rct::key &x, const boost::serialization::version_type ver)
195  {
196  a & reinterpret_cast<char (&)[sizeof(rct::key)]>(x);
197  }
198 
199  template <class Archive>
200  inline void serialize(Archive &a, rct::ctkey &x, const boost::serialization::version_type ver)
201  {
202  a & x.dest;
203  a & x.mask;
204  }
205 
206  template <class Archive>
207  inline void serialize(Archive &a, rct::rangeSig &x, const boost::serialization::version_type ver)
208  {
209  a & x.asig;
210  a & x.Ci;
211  }
212 
213  template <class Archive>
214  inline void serialize(Archive &a, rct::Bulletproof &x, const boost::serialization::version_type ver)
215  {
216  a & x.V;
217  a & x.A;
218  a & x.S;
219  a & x.T1;
220  a & x.T2;
221  a & x.taux;
222  a & x.mu;
223  a & x.L;
224  a & x.R;
225  a & x.a;
226  a & x.b;
227  a & x.t;
228  }
229 
230  template <class Archive>
231  inline void serialize(Archive &a, rct::boroSig &x, const boost::serialization::version_type ver)
232  {
233  a & x.s0;
234  a & x.s1;
235  a & x.ee;
236  }
237 
238  template <class Archive>
239  inline void serialize(Archive &a, rct::mgSig &x, const boost::serialization::version_type ver)
240  {
241  a & x.ss;
242  a & x.cc;
243  // a & x.II; // not serialized, we can recover it from the tx vin
244  }
245 
246  template <class Archive>
247  inline void serialize(Archive &a, rct::clsag &x, const boost::serialization::version_type ver)
248  {
249  a & x.s;
250  a & x.c1;
251  // a & x.I; // not serialized, we can recover it from the tx vin
252  a & x.D;
253  }
254 
255  template <class Archive>
256  inline void serialize(Archive &a, rct::ecdhTuple &x, const boost::serialization::version_type ver)
257  {
258  a & x.mask;
259  a & x.amount;
260  }
261 
262  template <class Archive>
263  inline void serialize(Archive &a, rct::multisig_kLRki &x, const boost::serialization::version_type ver)
264  {
265  a & x.k;
266  a & x.L;
267  a & x.R;
268  a & x.ki;
269  }
270 
271  template <class Archive>
272  inline void serialize(Archive &a, rct::multisig_out &x, const boost::serialization::version_type ver)
273  {
274  a & x.c;
275  if (ver < 1)
276  return;
277  a & x.mu_p;
278  }
279 
280  template <class Archive>
281  inline typename std::enable_if<Archive::is_loading::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
282  {
283  rct::keyV outPk;
284  a & outPk;
285  outPk_.resize(outPk.size());
286  for (size_t n = 0; n < outPk_.size(); ++n)
287  {
288  outPk_[n].dest = rct::identity();
289  outPk_[n].mask = outPk[n];
290  }
291  }
292 
293  template <class Archive>
294  inline typename std::enable_if<Archive::is_saving::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
295  {
296  rct::keyV outPk(outPk_.size());
297  for (size_t n = 0; n < outPk_.size(); ++n)
298  outPk[n] = outPk_[n].mask;
299  a & outPk;
300  }
301 
302  template <class Archive>
303  inline void serialize(Archive &a, rct::rctSigBase &x, const boost::serialization::version_type ver)
304  {
305  a & x.type;
306  if (x.type == rct::RCTTypeNull)
307  return;
309  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
310  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
311  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
312  if (x.type == rct::RCTTypeSimple) // moved to prunable with bulletproofs
313  a & x.pseudoOuts;
314  a & x.ecdhInfo;
315  serializeOutPk(a, x.outPk, ver);
316  a & x.txnFee;
317  }
318 
319  template <class Archive>
320  inline void serialize(Archive &a, rct::rctSigPrunable &x, const boost::serialization::version_type ver)
321  {
322  a & x.rangeSigs;
323  if (x.rangeSigs.empty())
324  a & x.bulletproofs;
325  a & x.MGs;
326  if (ver >= 1u)
327  a & x.CLSAGs;
328  if (x.rangeSigs.empty())
329  a & x.pseudoOuts;
330  }
331 
332  template <class Archive>
333  inline void serialize(Archive &a, rct::rctSig &x, const boost::serialization::version_type ver)
334  {
335  a & x.type;
336  if (x.type == rct::RCTTypeNull)
337  return;
339  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
340  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
341  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
342  if (x.type == rct::RCTTypeSimple)
343  a & x.pseudoOuts;
344  a & x.ecdhInfo;
345  serializeOutPk(a, x.outPk, ver);
346  a & x.txnFee;
347  //--------------
348  a & x.p.rangeSigs;
349  if (x.p.rangeSigs.empty())
350  a & x.p.bulletproofs;
351  a & x.p.MGs;
352  if (ver >= 1u)
353  a & x.p.CLSAGs;
355  a & x.p.pseudoOuts;
356  }
357 
358  template <class Archive>
359  inline void serialize(Archive &a, rct::RCTConfig &x, const boost::serialization::version_type ver)
360  {
361  a & x.range_proof_type;
362  a & x.bp_version;
363  }
364 
365  template <class Archive>
366  inline void serialize(Archive &a, cryptonote::difficulty_type &x, const boost::serialization::version_type ver)
367  {
368  if (Archive::is_loading::value)
369  {
370  // load high part
371  uint64_t v = 0;
372  a & v;
373  x = v;
374  // load low part
375  x = x << 64;
376  a & v;
377  x += v;
378  }
379  else
380  {
381  // store high part
382  cryptonote::difficulty_type x_ = (x >> 64) & 0xffffffffffffffff;
383  uint64_t v = x_.convert_to<uint64_t>();
384  a & v;
385  // store low part
386  x_ = x & 0xffffffffffffffff;
387  v = x_.convert_to<uint64_t>();
388  a & v;
389  }
390  }
391 
392 }
393 }
394 
396 BOOST_CLASS_VERSION(rct::rctSig, 1)
397 BOOST_CLASS_VERSION(rct::multisig_out, 1)
Definition: cryptonote_basic.h:160
std::vector< uint8_t > extra
Definition: cryptonote_basic.h:170
std::vector< txin_v > vin
Definition: cryptonote_basic.h:167
size_t version
Definition: cryptonote_basic.h:164
std::vector< tx_out > vout
Definition: cryptonote_basic.h:168
uint64_t unlock_time
Definition: cryptonote_basic.h:165
Definition: cryptonote_basic.h:194
rct::rctSig rct_signatures
Definition: cryptonote_basic.h:203
std::vector< std::vector< crypto::signature > > signatures
Definition: cryptonote_basic.h:202
string a
Definition: MakeCryptoOps.py:15
int b
Definition: base.py:1
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:126
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:281
Definition: unordered_containers_boost_serialization.h:38
POD_CLASS signature
Definition: crypto.h:93
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:67
POD_CLASS hash8
Definition: hash.h:51
POD_CLASS key_derivation
Definition: crypto.h:83
POD_CLASS public_key
Definition: crypto.h:61
POD_CLASS key_image
Definition: crypto.h:87
POD_CLASS hash
Definition: hash.h:48
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:41
Definition: bulletproofs.cc:64
std::vector< key > keyV
Definition: rctTypes.h:89
@ RCTTypeFull
Definition: rctTypes.h:255
@ RCTTypeSimple
Definition: rctTypes.h:256
@ RCTTypeCLSAG
Definition: rctTypes.h:259
@ RCTTypeBulletproof2
Definition: rctTypes.h:258
@ RCTTypeBulletproof
Definition: rctTypes.h:257
@ RCTTypeNull
Definition: rctTypes.h:254
std::vector< ctkey > ctkeyV
Definition: rctTypes.h:101
key identity()
Definition: rctOps.h:73
Definition: binary_utils.h:36
BOOST_CLASS_VERSION(nodetool::peerlist_types, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER)
Definition: cryptonote_basic.h:464
Definition: cryptonote_basic.h:143
txout_target_v target
Definition: cryptonote_basic.h:145
uint64_t amount
Definition: cryptonote_basic.h:144
Definition: cryptonote_basic.h:87
size_t height
Definition: cryptonote_basic.h:88
Definition: cryptonote_basic.h:124
crypto::key_image k_image
Definition: cryptonote_basic.h:127
std::vector< uint64_t > key_offsets
Definition: cryptonote_basic.h:126
uint64_t amount
Definition: cryptonote_basic.h:125
Definition: cryptonote_basic.h:96
crypto::hash prev
Definition: cryptonote_basic.h:97
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:99
size_t prevout
Definition: cryptonote_basic.h:98
Definition: cryptonote_basic.h:109
size_t prevout
Definition: cryptonote_basic.h:111
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:113
txout_to_script script
Definition: cryptonote_basic.h:112
crypto::hash prev
Definition: cryptonote_basic.h:110
Definition: cryptonote_basic.h:77
crypto::public_key key
Definition: cryptonote_basic.h:80
Definition: cryptonote_basic.h:61
std::vector< crypto::public_key > keys
Definition: cryptonote_basic.h:62
std::vector< uint8_t > script
Definition: cryptonote_basic.h:63
Definition: cryptonote_basic.h:72
crypto::hash hash
Definition: cryptonote_basic.h:73
Definition: rctTypes.h:205
rct::key T2
Definition: rctTypes.h:207
rct::key t
Definition: rctTypes.h:210
rct::key T1
Definition: rctTypes.h:207
rct::key S
Definition: rctTypes.h:207
rct::key a
Definition: rctTypes.h:210
rct::keyV L
Definition: rctTypes.h:209
rct::key taux
Definition: rctTypes.h:208
rct::keyV V
Definition: rctTypes.h:206
rct::keyV R
Definition: rctTypes.h:209
rct::key b
Definition: rctTypes.h:210
rct::key A
Definition: rctTypes.h:207
rct::key mu
Definition: rctTypes.h:208
Definition: rctTypes.h:262
int bp_version
Definition: rctTypes.h:264
RangeProofType range_proof_type
Definition: rctTypes.h:263
Definition: rctTypes.h:147
key64 s1
Definition: rctTypes.h:149
key ee
Definition: rctTypes.h:150
key64 s0
Definition: rctTypes.h:148
Definition: rctTypes.h:173
key D
Definition: rctTypes.h:178
key c1
Definition: rctTypes.h:175
keyV s
Definition: rctTypes.h:174
Definition: rctTypes.h:97
key dest
Definition: rctTypes.h:98
key mask
Definition: rctTypes.h:99
Definition: rctTypes.h:132
key amount
Definition: rctTypes.h:134
key mask
Definition: rctTypes.h:133
Definition: rctTypes.h:79
Definition: rctTypes.h:160
keyM ss
Definition: rctTypes.h:161
key cc
Definition: rctTypes.h:162
Definition: rctTypes.h:105
key ki
Definition: rctTypes.h:109
key L
Definition: rctTypes.h:107
key k
Definition: rctTypes.h:106
key R
Definition: rctTypes.h:108
Definition: rctTypes.h:114
std::vector< key > c
Definition: rctTypes.h:115
std::vector< key > mu_p
Definition: rctTypes.h:116
Definition: rctTypes.h:194
boroSig asig
Definition: rctTypes.h:195
key64 Ci
Definition: rctTypes.h:196
Definition: rctTypes.h:272
xmr_amount txnFee
Definition: rctTypes.h:280
std::vector< ecdhTuple > ecdhInfo
Definition: rctTypes.h:278
uint8_t type
Definition: rctTypes.h:273
keyV pseudoOuts
Definition: rctTypes.h:277
ctkeyV outPk
Definition: rctTypes.h:279
Definition: rctTypes.h:360
std::vector< clsag > CLSAGs
Definition: rctTypes.h:364
keyV pseudoOuts
Definition: rctTypes.h:365
std::vector< mgSig > MGs
Definition: rctTypes.h:363
std::vector< rangeSig > rangeSigs
Definition: rctTypes.h:361
std::vector< Bulletproof > bulletproofs
Definition: rctTypes.h:362
Definition: rctTypes.h:536
rctSigPrunable p
Definition: rctTypes.h:537