Monero
cryptonote_boost_serialization.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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/binary_iarchive.hpp>
40 #include <boost/archive/portable_binary_iarchive.hpp>
41 #include <boost/archive/portable_binary_oarchive.hpp>
42 #include "cryptonote_basic.h"
44 #include "crypto/crypto.h"
45 #include "ringct/rctTypes.h"
46 #include "ringct/rctOps.h"
47 
48 //namespace cryptonote {
49 namespace boost
50 {
51  namespace serialization
52  {
53 
54  //---------------------------------------------------
55  template <class Archive>
56  inline void serialize(Archive &a, crypto::public_key &x, const boost::serialization::version_type ver)
57  {
58  a & reinterpret_cast<char (&)[sizeof(crypto::public_key)]>(x);
59  }
60  template <class Archive>
61  inline void serialize(Archive &a, crypto::secret_key &x, const boost::serialization::version_type ver)
62  {
63  a & reinterpret_cast<char (&)[sizeof(crypto::secret_key)]>(x);
64  }
65  template <class Archive>
66  inline void serialize(Archive &a, crypto::key_derivation &x, const boost::serialization::version_type ver)
67  {
68  a & reinterpret_cast<char (&)[sizeof(crypto::key_derivation)]>(x);
69  }
70  template <class Archive>
71  inline void serialize(Archive &a, crypto::key_image &x, const boost::serialization::version_type ver)
72  {
73  a & reinterpret_cast<char (&)[sizeof(crypto::key_image)]>(x);
74  }
75 
76  template <class Archive>
77  inline void serialize(Archive &a, crypto::signature &x, const boost::serialization::version_type ver)
78  {
79  a & reinterpret_cast<char (&)[sizeof(crypto::signature)]>(x);
80  }
81  template <class Archive>
82  inline void serialize(Archive &a, crypto::hash &x, const boost::serialization::version_type ver)
83  {
84  a & reinterpret_cast<char (&)[sizeof(crypto::hash)]>(x);
85  }
86  template <class Archive>
87  inline void serialize(Archive &a, crypto::hash8 &x, const boost::serialization::version_type ver)
88  {
89  a & reinterpret_cast<char (&)[sizeof(crypto::hash8)]>(x);
90  }
91 
92  template <class Archive>
93  inline void serialize(Archive &a, cryptonote::txout_to_script &x, const boost::serialization::version_type ver)
94  {
95  a & x.keys;
96  a & x.script;
97  }
98 
99 
100  template <class Archive>
101  inline void serialize(Archive &a, cryptonote::txout_to_key &x, const boost::serialization::version_type ver)
102  {
103  a & x.key;
104  }
105 
106  template <class Archive>
107  inline void serialize(Archive &a, cryptonote::txout_to_scripthash &x, const boost::serialization::version_type ver)
108  {
109  a & x.hash;
110  }
111 
112  template <class Archive>
113  inline void serialize(Archive &a, cryptonote::txin_gen &x, const boost::serialization::version_type ver)
114  {
115  a & x.height;
116  }
117 
118  template <class Archive>
119  inline void serialize(Archive &a, cryptonote::txin_to_script &x, const boost::serialization::version_type ver)
120  {
121  a & x.prev;
122  a & x.prevout;
123  a & x.sigset;
124  }
125 
126  template <class Archive>
127  inline void serialize(Archive &a, cryptonote::txin_to_scripthash &x, const boost::serialization::version_type ver)
128  {
129  a & x.prev;
130  a & x.prevout;
131  a & x.script;
132  a & x.sigset;
133  }
134 
135  template <class Archive>
136  inline void serialize(Archive &a, cryptonote::txin_to_key &x, const boost::serialization::version_type ver)
137  {
138  a & x.amount;
139  a & x.key_offsets;
140  a & x.k_image;
141  }
142 
143  template <class Archive>
144  inline void serialize(Archive &a, cryptonote::tx_out &x, const boost::serialization::version_type ver)
145  {
146  a & x.amount;
147  a & x.target;
148  }
149 
150 
151  template <class Archive>
152  inline void serialize(Archive &a, cryptonote::transaction_prefix &x, const boost::serialization::version_type ver)
153  {
154  a & x.version;
155  a & x.unlock_time;
156  a & x.vin;
157  a & x.vout;
158  a & x.extra;
159  }
160 
161  template <class Archive>
162  inline void serialize(Archive &a, cryptonote::transaction &x, const boost::serialization::version_type ver)
163  {
164  a & x.version;
165  a & x.unlock_time;
166  a & x.vin;
167  a & x.vout;
168  a & x.extra;
169  if (x.version == 1)
170  {
171  a & x.signatures;
172  }
173  else
174  {
177  a & x.rct_signatures.p;
178  }
179  }
180 
181  template <class Archive>
182  inline void serialize(Archive &a, cryptonote::block &b, const boost::serialization::version_type ver)
183  {
184  a & b.major_version;
185  a & b.minor_version;
186  a & b.timestamp;
187  a & b.prev_id;
188  a & b.nonce;
189  //------------------
190  a & b.miner_tx;
191  a & b.tx_hashes;
192  }
193 
194  template <class Archive>
195  inline void serialize(Archive &a, rct::key &x, const boost::serialization::version_type ver)
196  {
197  a & reinterpret_cast<char (&)[sizeof(rct::key)]>(x);
198  }
199 
200  template <class Archive>
201  inline void serialize(Archive &a, rct::ctkey &x, const boost::serialization::version_type ver)
202  {
203  a & x.dest;
204  a & x.mask;
205  }
206 
207  template <class Archive>
208  inline void serialize(Archive &a, rct::rangeSig &x, const boost::serialization::version_type ver)
209  {
210  a & x.asig;
211  a & x.Ci;
212  }
213 
214  template <class Archive>
215  inline void serialize(Archive &a, rct::Bulletproof &x, const boost::serialization::version_type ver)
216  {
217  a & x.V;
218  a & x.A;
219  a & x.S;
220  a & x.T1;
221  a & x.T2;
222  a & x.taux;
223  a & x.mu;
224  a & x.L;
225  a & x.R;
226  a & x.a;
227  a & x.b;
228  a & x.t;
229  }
230 
231  template <class Archive>
232  inline void serialize(Archive &a, rct::boroSig &x, const boost::serialization::version_type ver)
233  {
234  a & x.s0;
235  a & x.s1;
236  a & x.ee;
237  }
238 
239  template <class Archive>
240  inline void serialize(Archive &a, rct::mgSig &x, const boost::serialization::version_type ver)
241  {
242  a & x.ss;
243  a & x.cc;
244  // a & x.II; // not serialized, we can recover it from the tx vin
245  }
246 
247  template <class Archive>
248  inline void serialize(Archive &a, rct::ecdhTuple &x, const boost::serialization::version_type ver)
249  {
250  a & x.mask;
251  a & x.amount;
252  // a & x.senderPk; // not serialized, as we do not use it in monero currently
253  }
254 
255  template <class Archive>
256  inline void serialize(Archive &a, rct::multisig_kLRki &x, const boost::serialization::version_type ver)
257  {
258  a & x.k;
259  a & x.L;
260  a & x.R;
261  a & x.ki;
262  }
263 
264  template <class Archive>
265  inline void serialize(Archive &a, rct::multisig_out &x, const boost::serialization::version_type ver)
266  {
267  a & x.c;
268  }
269 
270  template <class Archive>
271  inline typename std::enable_if<Archive::is_loading::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
272  {
273  rct::keyV outPk;
274  a & outPk;
275  outPk_.resize(outPk.size());
276  for (size_t n = 0; n < outPk_.size(); ++n)
277  {
278  outPk_[n].dest = rct::identity();
279  outPk_[n].mask = outPk[n];
280  }
281  }
282 
283  template <class Archive>
284  inline typename std::enable_if<Archive::is_saving::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
285  {
286  rct::keyV outPk(outPk_.size());
287  for (size_t n = 0; n < outPk_.size(); ++n)
288  outPk[n] = outPk_[n].mask;
289  a & outPk;
290  }
291 
292  template <class Archive>
293  inline void serialize(Archive &a, rct::rctSigBase &x, const boost::serialization::version_type ver)
294  {
295  a & x.type;
296  if (x.type == rct::RCTTypeNull)
297  return;
299  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
300  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
301  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
302  if (x.type == rct::RCTTypeSimple) // moved to prunable with bulletproofs
303  a & x.pseudoOuts;
304  a & x.ecdhInfo;
305  serializeOutPk(a, x.outPk, ver);
306  a & x.txnFee;
307  }
308 
309  template <class Archive>
310  inline void serialize(Archive &a, rct::rctSigPrunable &x, const boost::serialization::version_type ver)
311  {
312  a & x.rangeSigs;
313  if (x.rangeSigs.empty())
314  a & x.bulletproofs;
315  a & x.MGs;
316  if (x.rangeSigs.empty())
317  a & x.pseudoOuts;
318  }
319 
320  template <class Archive>
321  inline void serialize(Archive &a, rct::rctSig &x, const boost::serialization::version_type ver)
322  {
323  a & x.type;
324  if (x.type == rct::RCTTypeNull)
325  return;
327  throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
328  // a & x.message; message is not serialized, as it can be reconstructed from the tx data
329  // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
330  if (x.type == rct::RCTTypeSimple)
331  a & x.pseudoOuts;
332  a & x.ecdhInfo;
333  serializeOutPk(a, x.outPk, ver);
334  a & x.txnFee;
335  //--------------
336  a & x.p.rangeSigs;
337  if (x.p.rangeSigs.empty())
338  a & x.p.bulletproofs;
339  a & x.p.MGs;
340  if (x.type == rct::RCTTypeBulletproof)
341  a & x.p.pseudoOuts;
342  }
343 }
344 }
345 
346 //}
Definition: binary_utils.h:36
key ee
Definition: rctTypes.h:145
std::vector< rangeSig > rangeSigs
Definition: rctTypes.h:302
xmr_amount txnFee
Definition: rctTypes.h:243
rct::key b
Definition: rctTypes.h:188
key64 Ci
Definition: rctTypes.h:174
Definition: cryptonote_basic.h:109
Definition: rctTypes.h:111
Definition: rctTypes.h:301
key k
Definition: rctTypes.h:105
txout_to_script script
Definition: cryptonote_basic.h:113
Definition: unordered_containers_boost_serialization.h:37
Definition: rctTypes.h:232
std::vector< ecdhTuple > ecdhInfo
Definition: rctTypes.h:241
std::vector< mgSig > MGs
Definition: rctTypes.h:304
key ki
Definition: rctTypes.h:108
POD_CLASS key_derivation
Definition: crypto.h:85
Definition: cryptonote_basic.h:366
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:126
rct::key S
Definition: rctTypes.h:185
Definition: rctTypes.h:96
key mask
Definition: rctTypes.h:126
key amount
Definition: rctTypes.h:127
Definition: rctTypes.h:230
rct::keyV L
Definition: rctTypes.h:187
size_t prevout
Definition: cryptonote_basic.h:112
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:271
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:69
size_t prevout
Definition: cryptonote_basic.h:99
std::vector< uint64_t > key_offsets
Definition: cryptonote_basic.h:127
Definition: cryptonote_basic.h:77
Definition: cryptonote_basic.h:61
rct::key T1
Definition: rctTypes.h:185
Definition: rctTypes.h:125
key dest
Definition: rctTypes.h:97
key64 s0
Definition: rctTypes.h:143
rct::key mu
Definition: rctTypes.h:186
std::vector< Bulletproof > bulletproofs
Definition: rctTypes.h:303
Definition: cryptonote_basic.h:87
Definition: rctTypes.h:104
std::vector< uint8_t > extra
Definition: cryptonote_basic.h:167
crypto::hash prev
Definition: cryptonote_basic.h:98
std::vector< key > keyV
Definition: rctTypes.h:88
Definition: cryptonote_basic.h:156
std::vector< tx_out > vout
Definition: cryptonote_basic.h:165
Definition: cryptonote_basic.h:96
std::vector< txin_v > vin
Definition: cryptonote_basic.h:164
rct::keyV R
Definition: rctTypes.h:187
std::vector< uint8_t > script
Definition: cryptonote_basic.h:64
Definition: cryptonote_basic.h:72
uint64_t unlock_time
Definition: cryptonote_basic.h:162
rct::rctSig rct_signatures
Definition: cryptonote_basic.h:191
Definition: rctTypes.h:142
rctSigPrunable p
Definition: rctTypes.h:417
std::vector< ctkey > ctkeyV
Definition: rctTypes.h:100
uint64_t amount
Definition: cryptonote_basic.h:126
key L
Definition: rctTypes.h:106
Definition: rctTypes.h:78
int b
Definition: base.py:1
Definition: cryptonote_basic.h:124
Definition: rctTypes.h:416
key R
Definition: rctTypes.h:107
size_t version
Definition: cryptonote_basic.h:161
keyV pseudoOuts
Definition: rctTypes.h:305
Definition: rctTypes.h:172
crypto::hash hash
Definition: cryptonote_basic.h:74
POD_CLASS public_key
Definition: crypto.h:63
Definition: rctTypes.h:155
std::vector< std::vector< crypto::signature > > signatures
Definition: cryptonote_basic.h:190
rct::key T2
Definition: rctTypes.h:185
Definition: rctTypes.h:182
std::vector< key > c
Definition: rctTypes.h:112
boroSig asig
Definition: rctTypes.h:173
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:100
Definition: rctTypes.h:231
POD_CLASS signature
Definition: crypto.h:95
key identity()
Definition: rctOps.h:73
crypto::hash prev
Definition: cryptonote_basic.h:111
POD_CLASS hash8
Definition: hash.h:52
crypto::public_key key
Definition: cryptonote_basic.h:81
POD_CLASS key_image
Definition: crypto.h:89
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:114
uint64_t amount
Definition: cryptonote_basic.h:145
uint8_t type
Definition: rctTypes.h:236
txout_target_v target
Definition: cryptonote_basic.h:146
key cc
Definition: rctTypes.h:157
string a
Definition: MakeCryptoOps.py:15
crypto::key_image k_image
Definition: cryptonote_basic.h:128
key mask
Definition: rctTypes.h:98
keyM ss
Definition: rctTypes.h:156
std::vector< crypto::public_key > keys
Definition: cryptonote_basic.h:63
rct::key A
Definition: rctTypes.h:185
POD_CLASS hash
Definition: hash.h:49
Definition: rctTypes.h:229
rct::key a
Definition: rctTypes.h:188
Definition: cryptonote_basic.h:143
rct::key taux
Definition: rctTypes.h:186
rct::keyV V
Definition: rctTypes.h:184
keyV pseudoOuts
Definition: rctTypes.h:240
size_t height
Definition: cryptonote_basic.h:89
Definition: rctTypes.h:235
Definition: cryptonote_basic.h:182
ctkeyV outPk
Definition: rctTypes.h:242
rct::key t
Definition: rctTypes.h:188
key64 s1
Definition: rctTypes.h:144