Debian Codegraph @

src/cryptonote_basic/cryptonote_basic.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/variant.hpp>
34 #include <boost/functional/hash/hash.hpp>
35 #include <vector>
36 #include <cstring> // memcmp
37 #include <sstream>
38 #include <atomic>
39 #include "serialization/variant.h"
40 #include "serialization/containers.h"
41 #include "serialization/binary_archive.h"
42 #include "serialization/json_archive.h"
43 #include "serialization/debug_archive.h"
44 #include "serialization/crypto.h"
45 #include "serialization/keyvalue_serialization.h" // eepe named serialization
46 #include "cryptonote_config.h"
47 #include "crypto/crypto.h"
48 #include "crypto/hash.h"
49 #include "misc_language.h"
50 #include "ringct/rctTypes.h"
51 #include "device/device.hpp"
52 #include "cryptonote_basic/fwd.h"
53
54 namespace cryptonote
55 {
56 typedef std::vector<crypto::signature> ring_signature;
57
58
59 /* outputs */
60
61 struct txout_to_script
62 {
63 std::vector<crypto::public_key> keys;
64 std::vector<uint8_t> script;
65
66 BEGIN_SERIALIZE_OBJECT()
67 FIELD(keys)
68 FIELD(script)
69 END_SERIALIZE()
70 };
71
72 struct txout_to_scripthash
73 {
74 crypto::hash hash;
75 };
76
77 // outputs <= HF_VERSION_VIEW_TAGS
78 struct txout_to_key
79 {
80 txout_to_key() { }
81 txout_to_key(const crypto::public_key &_key) : key(_key) { }
82 crypto::public_key key;
83 };
84
85 // outputs >= HF_VERSION_VIEW_TAGS
86 struct txout_to_tagged_key
87 {
88 txout_to_tagged_key() { }
89 txout_to_tagged_key(const crypto::public_key &_key, const crypto::view_tag &_view_tag) : key(_key), view_tag(_view_tag) { }
90 crypto::public_key key;
91 crypto::view_tag view_tag; // optimization to reduce scanning time
92
93 BEGIN_SERIALIZE_OBJECT()
94 FIELD(key)
95 FIELD(view_tag)
96 END_SERIALIZE()
97 };
98
99 /* inputs */
100
101 struct txin_gen
102 {
103 size_t height;
104
105 BEGIN_SERIALIZE_OBJECT()
106 VARINT_FIELD(height)
107 END_SERIALIZE()
108 };
109
110 struct txin_to_script
111 {
112 crypto::hash prev;
113 size_t prevout;
114 std::vector<uint8_t> sigset;
115
116 BEGIN_SERIALIZE_OBJECT()
117 FIELD(prev)
118 VARINT_FIELD(prevout)
119 FIELD(sigset)
120 END_SERIALIZE()
121 };
122
123 struct txin_to_scripthash
124 {
125 crypto::hash prev;
126 size_t prevout;
127 txout_to_script script;
128 std::vector<uint8_t> sigset;
129
130 BEGIN_SERIALIZE_OBJECT()
131 FIELD(prev)
132 VARINT_FIELD(prevout)
133 FIELD(script)
134 FIELD(sigset)
135 END_SERIALIZE()
136 };
137
138 struct txin_to_key
139 {
140 uint64_t amount;
141 std::vector<uint64_t> key_offsets;
142 crypto::key_image k_image; // double spending protection
143
144 BEGIN_SERIALIZE_OBJECT()
145 VARINT_FIELD(amount)
146 FIELD(key_offsets)
147 FIELD(k_image)
148 END_SERIALIZE()
149 };
150
151
152 typedef boost::variant<txin_gen, txin_to_script, txin_to_scripthash, txin_to_key> txin_v;
153
154 typedef boost::variant<txout_to_script, txout_to_scripthash, txout_to_key, txout_to_tagged_key> txout_target_v;
155
156 //typedef std::pair<uint64_t, txout> out_t;
157 struct tx_out
158 {
159 uint64_t amount;
160 txout_target_v target;
161
162 BEGIN_SERIALIZE_OBJECT()
163 VARINT_FIELD(amount)
164 FIELD(target)
165 END_SERIALIZE()
166
167
168 };
169
170 class transaction_prefix
171 {
172
173 public:
174 // tx information
175 size_t version;
176 uint64_t unlock_time; //number of block (or time), used as a limitation like: spend this tx not early then block/time
177
178 std::vector<txin_v> vin;
179 std::vector<tx_out> vout;
180 //extra
181 std::vector<uint8_t> extra;
182
183 BEGIN_SERIALIZE()
184 VARINT_FIELD(version)
185 if(version == 0 || CURRENT_TRANSACTION_VERSION < version) return false;
186 VARINT_FIELD(unlock_time)
187 FIELD(vin)
188 FIELD(vout)
189 FIELD(extra)
190 END_SERIALIZE()
191
192 public:
193 transaction_prefix(){ set_null(); }
194 void set_null()
195 {
196 version = 1;
197 unlock_time = 0;
198 vin.clear();
199 vout.clear();
200 extra.clear();
201 }
202 };
203
204 class transaction: public transaction_prefix
205 {
206 private:
207 // hash cash
208 mutable std::atomic<bool> hash_valid;
209 mutable std::atomic<bool> prunable_hash_valid;
210 mutable std::atomic<bool> blob_size_valid;
211
212 public:
213 std::vector<std::vector<crypto::signature> > signatures; //count signatures always the same as inputs count
214 rct::rctSig rct_signatures;
215
216 // hash cash
217 mutable crypto::hash hash;
218 mutable crypto::hash prunable_hash;
219 mutable size_t blob_size;
220
221 bool pruned;
222
223 std::atomic<unsigned int> unprunable_size;
224 std::atomic<unsigned int> prefix_size;
225
226 transaction();
227 transaction(const transaction &t);
228 transaction &operator=(const transaction &t);
229 virtual ~transaction();
230 void set_null();
231 void invalidate_hashes();
232 bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
233 void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
234 bool is_prunable_hash_valid() const { return prunable_hash_valid.load(std::memory_order_acquire); }
235 void set_prunable_hash_valid(bool v) const { prunable_hash_valid.store(v,std::memory_order_release); }
236 bool is_blob_size_valid() const { return blob_size_valid.load(std::memory_order_acquire); }
237 void set_blob_size_valid(bool v) const { blob_size_valid.store(v,std::memory_order_release); }
238 void set_hash(const crypto::hash &h) const { hash = h; set_hash_valid(true); }
239 void set_prunable_hash(const crypto::hash &h) const { prunable_hash = h; set_prunable_hash_valid(true); }
240 void set_blob_size(size_t sz) const { blob_size = sz; set_blob_size_valid(true); }
241
242 BEGIN_SERIALIZE_OBJECT()
243 if (!typename Archive<W>::is_saving())
244 {
245 set_hash_valid(false);
246 set_prunable_hash_valid(false);
247 set_blob_size_valid(false);
248 }
249
250 const auto start_pos = ar.getpos();
251
252 FIELDS(*static_cast<transaction_prefix *>(this))
253
254 if (std::is_same<Archive<W>, binary_archive<W>>())
255 prefix_size = ar.getpos() - start_pos;
256
257 if (version == 1)
258 {
259 if (std::is_same<Archive<W>, binary_archive<W>>())
260 unprunable_size = ar.getpos() - start_pos;
261
262 ar.tag("signatures");
263 ar.begin_array();
264 PREPARE_CUSTOM_VECTOR_SERIALIZATION(vin.size(), signatures);
265 bool signatures_not_expected = signatures.empty();
266 if (!signatures_not_expected && vin.size() != signatures.size())
267 return false;
268
269 if (!pruned) for (size_t i = 0; i < vin.size(); ++i)
270 {
271 size_t signature_size = get_signature_size(vin[i]);
272 if (signatures_not_expected)
273 {
274 if (0 == signature_size)
275 continue;
276 else
277 return false;
278 }
279
280 PREPARE_CUSTOM_VECTOR_SERIALIZATION(signature_size, signatures[i]);
281 if (signature_size != signatures[i].size())
282 return false;
283
284 FIELDS(signatures[i]);
285
286 if (vin.size() - i > 1)
287 ar.delimit_array();
288 }
289 ar.end_array();
290 }
291 else
292 {
293 ar.tag("rct_signatures");
294 if (!vin.empty())
295 {
296 ar.begin_object();
297 bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
298 if (!r || !ar.good()) return false;
299 ar.end_object();
300
301 if (std::is_same<Archive<W>, binary_archive<W>>())
302 unprunable_size = ar.getpos() - start_pos;
303
304 if (!pruned && rct_signatures.type != rct::RCTTypeNull)
305 {
306 ar.tag("rctsig_prunable");
307 ar.begin_object();
308 r = rct_signatures.p.serialize_rctsig_prunable(ar, rct_signatures.type, vin.size(), vout.size(),
309 vin.size() > 0 && vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(vin[0]).key_offsets.size() - 1 : 0);
310 if (!r || !ar.good()) return false;
311 ar.end_object();
312 }
313 }
314 }
315 if (!typename Archive<W>::is_saving())
316 pruned = false;
317 END_SERIALIZE()
318
319 template<bool W, template <bool> class Archive>
320 bool serialize_base(Archive<W> &ar)
321 {
322 FIELDS(*static_cast<transaction_prefix *>(this))
323
324 if (version == 1)
325 {
326 }
327 else
328 {
329 ar.tag("rct_signatures");
330 if (!vin.empty())
331 {
332 ar.begin_object();
333 bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
334 if (!r || !ar.good()) return false;
335 ar.end_object();
336 }
337 }
338 if (!typename Archive<W>::is_saving())
339 pruned = true;
340 return ar.good();
341 }
342
343 private:
344 static size_t get_signature_size(const txin_v& tx_in);
345 };
346
347 inline transaction::transaction(const transaction &t):
348 transaction_prefix(t),
349 hash_valid(false),
350 prunable_hash_valid(false),
351 blob_size_valid(false),
352 signatures(t.signatures),
353 rct_signatures(t.rct_signatures),
354 pruned(t.pruned),
355 unprunable_size(t.unprunable_size.load()),
356 prefix_size(t.prefix_size.load())
357 {
358 if (t.is_hash_valid())
359 {
360 hash = t.hash;
361 set_hash_valid(true);
362 }
363 if (t.is_blob_size_valid())
364 {
365 blob_size = t.blob_size;
366 set_blob_size_valid(true);
367 }
368 if (t.is_prunable_hash_valid())
369 {
370 prunable_hash = t.prunable_hash;
371 set_prunable_hash_valid(true);
372 }
373 }
374
375 inline transaction &transaction::operator=(const transaction &t)
376 {
377 transaction_prefix::operator=(t);
378
379 set_hash_valid(false);
380 set_prunable_hash_valid(false);
381 set_blob_size_valid(false);
382 signatures = t.signatures;
383 rct_signatures = t.rct_signatures;
384 if (t.is_hash_valid())
385 {
386 hash = t.hash;
387 set_hash_valid(true);
388 }
389 if (t.is_prunable_hash_valid())
390 {
391 prunable_hash = t.prunable_hash;
392 set_prunable_hash_valid(true);
393 }
394 if (t.is_blob_size_valid())
395 {
396 blob_size = t.blob_size;
397 set_blob_size_valid(true);
398 }
399 pruned = t.pruned;
400 unprunable_size = t.unprunable_size.load();
401 prefix_size = t.prefix_size.load();
402 return *this;
403 }
404
405 inline
406 transaction::transaction()
407 {
408 set_null();
409 }
410
411 inline
412 transaction::~transaction()
413 {
414 }
415
416 inline
417 void transaction::set_null()
418 {
419 transaction_prefix::set_null();
420 signatures.clear();
421 rct_signatures.type = rct::RCTTypeNull;
422 set_hash_valid(false);
423 set_prunable_hash_valid(false);
424 set_blob_size_valid(false);
425 pruned = false;
426 unprunable_size = 0;
427 prefix_size = 0;
428 }
429
430 inline
431 void transaction::invalidate_hashes()
432 {
433 set_hash_valid(false);
434 set_prunable_hash_valid(false);
435 set_blob_size_valid(false);
436 }
437
438 inline
439 size_t transaction::get_signature_size(const txin_v& tx_in)
440 {
441 struct txin_signature_size_visitor : public boost::static_visitor<size_t>
442 {
443 size_t operator()(const txin_gen& txin) const{return 0;}
444 size_t operator()(const txin_to_script& txin) const{return 0;}
445 size_t operator()(const txin_to_scripthash& txin) const{return 0;}
446 size_t operator()(const txin_to_key& txin) const {return txin.key_offsets.size();}
447 };
448
449 return boost::apply_visitor(txin_signature_size_visitor(), tx_in);
450 }
451
452
453
454 /************************************************************************/
455 /* */
456 /************************************************************************/
457 struct block_header
458 {
459 uint8_t major_version;
460 uint8_t minor_version; // now used as a voting mechanism, rather than how this particular block is built
461 uint64_t timestamp;
462 crypto::hash prev_id;
463 uint32_t nonce;
464
465 BEGIN_SERIALIZE()
466 VARINT_FIELD(major_version)
467 VARINT_FIELD(minor_version)
468 VARINT_FIELD(timestamp)
469 FIELD(prev_id)
470 FIELD(nonce)
471 END_SERIALIZE()
472 };
473
474 struct block: public block_header
475 {
476 private:
477 // hash cash
478 mutable std::atomic<bool> hash_valid;
479
480 public:
481 block(): block_header(), hash_valid(false) {}
482 block(const block &b): block_header(b), hash_valid(false), miner_tx(b.miner_tx), tx_hashes(b.tx_hashes) { if (b.is_hash_valid()) { hash = b.hash; set_hash_valid(true); } }
483 block &operator=(const block &b) { block_header::operator=(b); hash_valid = false; miner_tx = b.miner_tx; tx_hashes = b.tx_hashes; if (b.is_hash_valid()) { hash = b.hash; set_hash_valid(true); } return *this; }
484 void invalidate_hashes() { set_hash_valid(false); }
485 bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
486 void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
487 void set_hash(const crypto::hash &h) const { hash = h; set_hash_valid(true); }
488
489 transaction miner_tx;
490 std::vector<crypto::hash> tx_hashes;
491
492 // hash cash
493 mutable crypto::hash hash;
494
495 BEGIN_SERIALIZE_OBJECT()
496 if (!typename Archive<W>::is_saving())
497 set_hash_valid(false);
498
499 FIELDS(*static_cast<block_header *>(this))
500 FIELD(miner_tx)
501 FIELD(tx_hashes)
502 if (tx_hashes.size() > CRYPTONOTE_MAX_TX_PER_BLOCK)
503 return false;
504 END_SERIALIZE()
505 };
506
507
508 /************************************************************************/
509 /* */
510 /************************************************************************/
511 struct account_public_address
512 {
513 crypto::public_key m_spend_public_key;
514 crypto::public_key m_view_public_key;
515
516 BEGIN_SERIALIZE_OBJECT()
517 FIELD(m_spend_public_key)
518 FIELD(m_view_public_key)
519 END_SERIALIZE()
520
521 BEGIN_KV_SERIALIZE_MAP()
522 KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_spend_public_key)
523 KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_view_public_key)
524 END_KV_SERIALIZE_MAP()
525
526 bool operator==(const account_public_address& rhs) const
527 {
528 return m_spend_public_key == rhs.m_spend_public_key &&
529 m_view_public_key == rhs.m_view_public_key;
530 }
531
532 bool operator!=(const account_public_address& rhs) const
533 {
534 return !(*this == rhs);
535 }
536 };
537
538 struct keypair
539 {
540 crypto::public_key pub;
541 crypto::secret_key sec;
542
543 static inline keypair generate(hw::device &hwdev)
544 {
545 keypair k;
546 hwdev.generate_keys(k.pub, k.sec);
547 return k;
548 }
549 };
550 //---------------------------------------------------------------
551
552 }
553
554 namespace std {
555 template <>
556 struct hash<cryptonote::account_public_address>
557 {
558 std::size_t operator()(const cryptonote::account_public_address& addr) const
559 {
560 // https://stackoverflow.com/a/17017281
561 size_t res = 17;
562 res = res * 31 + hash<crypto::public_key>()(addr.m_spend_public_key);
563 res = res * 31 + hash<crypto::public_key>()(addr.m_view_public_key);
564 return res;
565 }
566 };
567 }
568
569 BLOB_SERIALIZER(cryptonote::txout_to_key);
570 BLOB_SERIALIZER(cryptonote::txout_to_scripthash);
571
572 VARIANT_TAG(binary_archive, cryptonote::txin_gen, 0xff);
573 VARIANT_TAG(binary_archive, cryptonote::txin_to_script, 0x0);
574 VARIANT_TAG(binary_archive, cryptonote::txin_to_scripthash, 0x1);
575 VARIANT_TAG(binary_archive, cryptonote::txin_to_key, 0x2);
576 VARIANT_TAG(binary_archive, cryptonote::txout_to_script, 0x0);
577 VARIANT_TAG(binary_archive, cryptonote::txout_to_scripthash, 0x1);
578 VARIANT_TAG(binary_archive, cryptonote::txout_to_key, 0x2);
579 VARIANT_TAG(binary_archive, cryptonote::txout_to_tagged_key, 0x3);
580 VARIANT_TAG(binary_archive, cryptonote::transaction, 0xcc);
581 VARIANT_TAG(binary_archive, cryptonote::block, 0xbb);
582
583 VARIANT_TAG(json_archive, cryptonote::txin_gen, "gen");
584 VARIANT_TAG(json_archive, cryptonote::txin_to_script, "script");
585 VARIANT_TAG(json_archive, cryptonote::txin_to_scripthash, "scripthash");
586 VARIANT_TAG(json_archive, cryptonote::txin_to_key, "key");
587 VARIANT_TAG(json_archive, cryptonote::txout_to_script, "script");
588 VARIANT_TAG(json_archive, cryptonote::txout_to_scripthash, "scripthash");
589 VARIANT_TAG(json_archive, cryptonote::txout_to_key, "key");
590 VARIANT_TAG(json_archive, cryptonote::txout_to_tagged_key, "tagged_key");
591 VARIANT_TAG(json_archive, cryptonote::transaction, "tx");
592 VARIANT_TAG(json_archive, cryptonote::block, "block");
593
594 VARIANT_TAG(debug_archive, cryptonote::txin_gen, "gen");
595 VARIANT_TAG(debug_archive, cryptonote::txin_to_script, "script");
596 VARIANT_TAG(debug_archive, cryptonote::txin_to_scripthash, "scripthash");
597 VARIANT_TAG(debug_archive, cryptonote::txin_to_key, "key");
598 VARIANT_TAG(debug_archive, cryptonote::txout_to_script, "script");
599 VARIANT_TAG(debug_archive, cryptonote::txout_to_scripthash, "scripthash");
600 VARIANT_TAG(debug_archive, cryptonote::txout_to_key, "key");
601 VARIANT_TAG(debug_archive, cryptonote::txout_to_tagged_key, "tagged_key");
602 VARIANT_TAG(debug_archive, cryptonote::transaction, "tx");
603 VARIANT_TAG(debug_archive, cryptonote::block, "block");