Debian Codegraph @

src/cryptonote_basic/cryptonote_boost_serialization.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, 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"
43 #include "common/unordered_containers_boost_serialization.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 template <class Archive>
75 inline void serialize(Archive &a, crypto::view_tag &x, const boost::serialization::version_type ver)
76 {
77 a & reinterpret_cast<char (&)[sizeof(crypto::view_tag)]>(x);
78 }
79 template <class Archive>
80 inline void serialize(Archive &a, crypto::signature &x, const boost::serialization::version_type ver)
81 {
82 a & reinterpret_cast<char (&)[sizeof(crypto::signature)]>(x);
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_tagged_key &x, const boost::serialization::version_type ver)
111 {
112 a & x.key;
113 a & x.view_tag;
114 }
115
116 template <class Archive>
117 inline void serialize(Archive &a, cryptonote::txout_to_scripthash &x, const boost::serialization::version_type ver)
118 {
119 a & x.hash;
120 }
121
122 template <class Archive>
123 inline void serialize(Archive &a, cryptonote::txin_gen &x, const boost::serialization::version_type ver)
124 {
125 a & x.height;
126 }
127
128 template <class Archive>
129 inline void serialize(Archive &a, cryptonote::txin_to_script &x, const boost::serialization::version_type ver)
130 {
131 a & x.prev;
132 a & x.prevout;
133 a & x.sigset;
134 }
135
136 template <class Archive>
137 inline void serialize(Archive &a, cryptonote::txin_to_scripthash &x, const boost::serialization::version_type ver)
138 {
139 a & x.prev;
140 a & x.prevout;
141 a & x.script;
142 a & x.sigset;
143 }
144
145 template <class Archive>
146 inline void serialize(Archive &a, cryptonote::txin_to_key &x, const boost::serialization::version_type ver)
147 {
148 a & x.amount;
149 a & x.key_offsets;
150 a & x.k_image;
151 }
152
153 template <class Archive>
154 inline void serialize(Archive &a, cryptonote::tx_out &x, const boost::serialization::version_type ver)
155 {
156 a & x.amount;
157 a & x.target;
158 }
159
160
161 template <class Archive>
162 inline void serialize(Archive &a, cryptonote::transaction_prefix &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 }
170
171 template <class Archive>
172 inline void serialize(Archive &a, cryptonote::transaction &x, const boost::serialization::version_type ver)
173 {
174 a & x.version;
175 a & x.unlock_time;
176 a & x.vin;
177 a & x.vout;
178 a & x.extra;
179 if (x.version == 1)
180 {
181 a & x.signatures;
182 }
183 else
184 {
185 a & (rct::rctSigBase&)x.rct_signatures;
186 if (x.rct_signatures.type != rct::RCTTypeNull)
187 a & x.rct_signatures.p;
188 }
189 }
190
191 template <class Archive>
192 inline void serialize(Archive &a, cryptonote::block &b, const boost::serialization::version_type ver)
193 {
194 a & b.major_version;
195 a & b.minor_version;
196 a & b.timestamp;
197 a & b.prev_id;
198 a & b.nonce;
199 //------------------
200 a & b.miner_tx;
201 a & b.tx_hashes;
202 }
203
204 template <class Archive>
205 inline void serialize(Archive &a, rct::key &x, const boost::serialization::version_type ver)
206 {
207 a & reinterpret_cast<char (&)[sizeof(rct::key)]>(x);
208 }
209
210 template <class Archive>
211 inline void serialize(Archive &a, rct::ctkey &x, const boost::serialization::version_type ver)
212 {
213 a & x.dest;
214 a & x.mask;
215 }
216
217 template <class Archive>
218 inline void serialize(Archive &a, rct::rangeSig &x, const boost::serialization::version_type ver)
219 {
220 a & x.asig;
221 a & x.Ci;
222 }
223
224 template <class Archive>
225 inline void serialize(Archive &a, rct::Bulletproof &x, const boost::serialization::version_type ver)
226 {
227 a & x.V;
228 a & x.A;
229 a & x.S;
230 a & x.T1;
231 a & x.T2;
232 a & x.taux;
233 a & x.mu;
234 a & x.L;
235 a & x.R;
236 a & x.a;
237 a & x.b;
238 a & x.t;
239 }
240
241 template <class Archive>
242 inline void serialize(Archive &a, rct::BulletproofPlus &x, const boost::serialization::version_type ver)
243 {
244 a & x.V;
245 a & x.A;
246 a & x.A1;
247 a & x.B;
248 a & x.r1;
249 a & x.s1;
250 a & x.d1;
251 a & x.L;
252 a & x.R;
253 }
254
255 template <class Archive>
256 inline void serialize(Archive &a, rct::boroSig &x, const boost::serialization::version_type ver)
257 {
258 a & x.s0;
259 a & x.s1;
260 a & x.ee;
261 }
262
263 template <class Archive>
264 inline void serialize(Archive &a, rct::mgSig &x, const boost::serialization::version_type ver)
265 {
266 a & x.ss;
267 a & x.cc;
268 // a & x.II; // not serialized, we can recover it from the tx vin
269 }
270
271 template <class Archive>
272 inline void serialize(Archive &a, rct::clsag &x, const boost::serialization::version_type ver)
273 {
274 a & x.s;
275 a & x.c1;
276 // a & x.I; // not serialized, we can recover it from the tx vin
277 a & x.D;
278 }
279
280 template <class Archive>
281 inline void serialize(Archive &a, rct::ecdhTuple &x, const boost::serialization::version_type ver)
282 {
283 a & x.mask;
284 a & x.amount;
285 }
286
287 template <class Archive>
288 inline void serialize(Archive &a, rct::multisig_kLRki &x, const boost::serialization::version_type ver)
289 {
290 a & x.k;
291 a & x.L;
292 a & x.R;
293 a & x.ki;
294 }
295
296 template <class Archive>
297 inline void serialize(Archive &a, rct::multisig_out &x, const boost::serialization::version_type ver)
298 {
299 a & x.c;
300 if (ver < 1)
301 return;
302 a & x.mu_p;
303 }
304
305 template <class Archive>
306 inline typename std::enable_if<Archive::is_loading::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
307 {
308 rct::keyV outPk;
309 a & outPk;
310 outPk_.resize(outPk.size());
311 for (size_t n = 0; n < outPk_.size(); ++n)
312 {
313 outPk_[n].dest = rct::identity();
314 outPk_[n].mask = outPk[n];
315 }
316 }
317
318 template <class Archive>
319 inline typename std::enable_if<Archive::is_saving::value, void>::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver)
320 {
321 rct::keyV outPk(outPk_.size());
322 for (size_t n = 0; n < outPk_.size(); ++n)
323 outPk[n] = outPk_[n].mask;
324 a & outPk;
325 }
326
327 template <class Archive>
328 inline void serialize(Archive &a, rct::rctSigBase &x, const boost::serialization::version_type ver)
329 {
330 a & x.type;
331 if (x.type == rct::RCTTypeNull)
332 return;
333 if (x.type != rct::RCTTypeFull && x.type != rct::RCTTypeSimple && x.type != rct::RCTTypeBulletproof && x.type != rct::RCTTypeBulletproof2 && x.type != rct::RCTTypeCLSAG && x.type != rct::RCTTypeBulletproofPlus)
334 throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
335 // a & x.message; message is not serialized, as it can be reconstructed from the tx data
336 // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
337 if (x.type == rct::RCTTypeSimple) // moved to prunable with bulletproofs
338 a & x.pseudoOuts;
339 a & x.ecdhInfo;
340 serializeOutPk(a, x.outPk, ver);
341 a & x.txnFee;
342 }
343
344 template <class Archive>
345 inline void serialize(Archive &a, rct::rctSigPrunable &x, const boost::serialization::version_type ver)
346 {
347 a & x.rangeSigs;
348 if (x.rangeSigs.empty())
349 {
350 a & x.bulletproofs;
351 if (ver >= 2u)
352 a & x.bulletproofs_plus;
353 }
354 a & x.MGs;
355 if (ver >= 1u)
356 a & x.CLSAGs;
357 if (x.rangeSigs.empty())
358 a & x.pseudoOuts;
359 }
360
361 template <class Archive>
362 inline void serialize(Archive &a, rct::rctSig &x, const boost::serialization::version_type ver)
363 {
364 a & x.type;
365 if (x.type == rct::RCTTypeNull)
366 return;
367 if (x.type != rct::RCTTypeFull && x.type != rct::RCTTypeSimple && x.type != rct::RCTTypeBulletproof && x.type != rct::RCTTypeBulletproof2 && x.type != rct::RCTTypeCLSAG && x.type != rct::RCTTypeBulletproofPlus)
368 throw boost::archive::archive_exception(boost::archive::archive_exception::other_exception, "Unsupported rct type");
369 // a & x.message; message is not serialized, as it can be reconstructed from the tx data
370 // a & x.mixRing; mixRing is not serialized, as it can be reconstructed from the offsets
371 if (x.type == rct::RCTTypeSimple)
372 a & x.pseudoOuts;
373 a & x.ecdhInfo;
374 serializeOutPk(a, x.outPk, ver);
375 a & x.txnFee;
376 //--------------
377 a & x.p.rangeSigs;
378 if (x.p.rangeSigs.empty())
379 {
380 a & x.p.bulletproofs;
381 if (ver >= 2u)
382 a & x.p.bulletproofs_plus;
383 }
384 a & x.p.MGs;
385 if (ver >= 1u)
386 a & x.p.CLSAGs;
387 if (x.type == rct::RCTTypeBulletproof || x.type == rct::RCTTypeBulletproof2 || x.type == rct::RCTTypeCLSAG || x.type == rct::RCTTypeBulletproofPlus)
388 a & x.p.pseudoOuts;
389 }
390
391 template <class Archive>
392 inline void serialize(Archive &a, rct::RCTConfig &x, const boost::serialization::version_type ver)
393 {
394 a & x.range_proof_type;
395 a & x.bp_version;
396 }
397
398 template <class Archive>
399 inline void serialize(Archive &a, cryptonote::difficulty_type &x, const boost::serialization::version_type ver)
400 {
401 if (Archive::is_loading::value)
402 {
403 // load high part
404 uint64_t v = 0;
405 a & v;
406 x = v;
407 // load low part
408 x = x << 64;
409 a & v;
410 x += v;
411 }
412 else
413 {
414 // store high part
415 cryptonote::difficulty_type x_ = (x >> 64) & 0xffffffffffffffff;
416 uint64_t v = x_.convert_to<uint64_t>();
417 a & v;
418 // store low part
419 x_ = x & 0xffffffffffffffff;
420 v = x_.convert_to<uint64_t>();
421 a & v;
422 }
423 }
424
425 }
426 }
427
428 BOOST_CLASS_VERSION(rct::rctSigPrunable, 2)
429 BOOST_CLASS_VERSION(rct::rctSig, 2)
430 BOOST_CLASS_VERSION(rct::multisig_out, 1)