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