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