Debian Codegraph @

src/cryptonote_core/tx_verification_utils.cpp

raw symbols SCIP
1 // Copyright (c) 2023, 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 #include <boost/iterator/transform_iterator.hpp>
30
31 #include "cryptonote_core/blockchain.h"
32 #include "cryptonote_core/cryptonote_core.h"
33 #include "cryptonote_core/tx_verification_utils.h"
34 #include "hardforks/hardforks.h"
35 #include "ringct/rctSigs.h"
36
37 #undef MONERO_DEFAULT_LOG_CATEGORY
38 #define MONERO_DEFAULT_LOG_CATEGORY "blockchain"
39
40 #define VER_ASSERT(cond, msgexpr) CHECK_AND_ASSERT_MES(cond, false, msgexpr)
41
42 using namespace cryptonote;
43
44 // Do RCT expansion, then do post-expansion sanity checks, then do full non-semantics verification.
45 static bool expand_tx_and_ver_rct_non_sem(transaction& tx, const rct::ctkeyM& mix_ring)
46 {
47 // Pruned transactions can not be expanded and verified because they are missing RCT data
48 VER_ASSERT(!tx.pruned, "Pruned transaction will not pass verRctNonSemanticsSimple");
49
50 // Calculate prefix hash
51 const crypto::hash tx_prefix_hash = get_transaction_prefix_hash(tx);
52
53 // Expand mixring, tx inputs, tx key images, prefix hash message, etc into the RCT sig
54 const bool exp_res = Blockchain::expand_transaction_2(tx, tx_prefix_hash, mix_ring);
55 VER_ASSERT(exp_res, "Failed to expand rct signatures!");
56
57 const rct::rctSig& rv = tx.rct_signatures;
58
59 // Check that expanded RCT mixring == input mixring
60 VER_ASSERT(rv.mixRing == mix_ring, "Failed to check ringct signatures: mismatched pubkeys/mixRing");
61
62 // Check CLSAG/MLSAG size against transaction input
63 const size_t n_sigs = rct::is_rct_clsag(rv.type) ? rv.p.CLSAGs.size() : rv.p.MGs.size();
64 VER_ASSERT(n_sigs == tx.vin.size(), "Failed to check ringct signatures: mismatched input sigs/vin sizes");
65
66 // For each input, check that the key images were copied into the expanded RCT sig correctly
67 for (size_t n = 0; n < n_sigs; ++n)
68 {
69 const crypto::key_image& nth_vin_image = boost::get<txin_to_key>(tx.vin[n]).k_image;
70
71 if (rct::is_rct_clsag(rv.type))
72 {
73 const bool ki_match = 0 == memcmp(&nth_vin_image, &rv.p.CLSAGs[n].I, 32);
74 VER_ASSERT(ki_match, "Failed to check ringct signatures: mismatched CLSAG key image");
75 }
76 else
77 {
78 const bool mg_nonempty = !rv.p.MGs[n].II.empty();
79 VER_ASSERT(mg_nonempty, "Failed to check ringct signatures: missing MLSAG key image");
80 const bool ki_match = 0 == memcmp(&nth_vin_image, &rv.p.MGs[n].II[0], 32);
81 VER_ASSERT(ki_match, "Failed to check ringct signatures: mismatched MLSAG key image");
82 }
83 }
84
85 // Mix ring data is now known to be correctly incorporated into the RCT sig inside tx.
86 return rct::verRctNonSemanticsSimple(rv);
87 }
88
89 // Create a unique identifier for pair of tx blob + mix ring
90 static crypto::hash calc_tx_mixring_hash(const transaction& tx, const rct::ctkeyM& mix_ring)
91 {
92 std::stringstream ss;
93
94 // Start with domain seperation
95 ss << config::HASH_KEY_TXHASH_AND_MIXRING;
96
97 // Then add TX hash
98 const crypto::hash tx_hash = get_transaction_hash(tx);
99 ss.write(tx_hash.data, sizeof(crypto::hash));
100
101 // Then serialize mix ring
102 binary_archive<true> ar(ss);
103 ::do_serialize(ar, const_cast<rct::ctkeyM&>(mix_ring));
104
105 // Calculate hash of TX hash and mix ring blob
106 crypto::hash tx_and_mixring_hash;
107 get_blob_hash(ss.str(), tx_and_mixring_hash);
108
109 return tx_and_mixring_hash;
110 }
111
112 static bool is_canonical_bulletproof_layout(const std::vector<rct::Bulletproof> &proofs)
113 {
114 if (proofs.size() != 1)
115 return false;
116 const size_t sz = proofs[0].V.size();
117 if (sz == 0 || sz > BULLETPROOF_MAX_OUTPUTS)
118 return false;
119 return true;
120 }
121
122 static bool is_canonical_bulletproof_plus_layout(const std::vector<rct::BulletproofPlus> &proofs)
123 {
124 if (proofs.size() != 1)
125 return false;
126 const size_t sz = proofs[0].V.size();
127 if (sz == 0 || sz > BULLETPROOF_PLUS_MAX_OUTPUTS)
128 return false;
129 return true;
130 }
131
132 template <class TxForwardIt>
133 static bool ver_non_input_consensus_templated(TxForwardIt tx_begin, TxForwardIt tx_end,
134 tx_verification_context& tvc, std::uint8_t hf_version)
135 {
136 std::vector<const rct::rctSig*> rvv;
137 rvv.reserve(static_cast<size_t>(std::distance(tx_begin, tx_end)));
138
139 const size_t max_tx_version = hf_version < HF_VERSION_DYNAMIC_FEE ? 1 : 2;
140
141 const size_t tx_weight_limit = get_transaction_weight_limit(hf_version);
142
143 for (; tx_begin != tx_end; ++tx_begin)
144 {
145 const transaction& tx = *tx_begin;
146 const uint64_t blob_size = get_transaction_blob_size(tx);
147
148 // Rule 1
149 if (blob_size > get_max_tx_size())
150 {
151 tvc.m_verifivation_failed = true;
152 tvc.m_too_big = true;
153 return false;
154 }
155
156 // Rule 2 & 3
157 if (tx.version == 0 || tx.version > max_tx_version)
158 {
159 tvc.m_verifivation_failed = true;
160 return false;
161 }
162
163 // Rule 4
164 const size_t tx_weight = get_transaction_weight(tx, blob_size);
165 if (hf_version >= HF_VERSION_PER_BYTE_FEE && tx_weight > tx_weight_limit)
166 {
167 tvc.m_verifivation_failed = true;
168 tvc.m_too_big = true;
169 return false;
170 }
171
172 // Rule 5
173 if (!core::check_tx_semantic(tx, tvc, hf_version))
174 return false;
175
176 // Rule 6
177 if (!Blockchain::check_tx_outputs(tx, tvc, hf_version) || tvc.m_verifivation_failed)
178 return false;
179
180 // We only want to check RingCT semantics if this is actually a RingCT transaction
181 if (tx.version >= 2)
182 rvv.push_back(&tx.rct_signatures);
183 }
184
185 // Rule 7
186 if (!ver_mixed_rct_semantics(std::move(rvv)))
187 {
188 tvc.m_verifivation_failed = true;
189 tvc.m_invalid_input = true;
190 return false;
191 }
192
193 return true;
194 }
195
196 ////////////////////////////////////////////////////////////////////////////////////////////////////
197
198 namespace cryptonote
199 {
200
201 uint64_t get_transaction_weight_limit(const uint8_t hf_version)
202 {
203 // from v8, limit a tx to 50% of the minimum block weight
204 if (hf_version >= HF_VERSION_PER_BYTE_FEE)
205 return get_min_block_weight(hf_version) / 2 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
206 else
207 return get_min_block_weight(hf_version) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
208 }
209
210 bool ver_rct_non_semantics_simple_cached
211 (
212 transaction& tx,
213 const rct::ctkeyM& mix_ring,
214 rct_ver_cache_t& cache,
215 const std::uint8_t rct_type_to_cache
216 )
217 {
218 // Hello future Monero dev! If you got this assert, read the following carefully:
219 //
220 // For this version of RCT, the way we guaranteed that verification caches do not generate false
221 // positives (and thus possibly enabling double spends) is we take a hash of two things. One,
222 // we use get_transaction_hash() which gives us a (cryptographically secure) unique
223 // representation of all "knobs" controlled by the possibly malicious constructor of the
224 // transaction. Two, we take a hash of all *previously validated* blockchain data referenced by
225 // this transaction which is required to validate the ring signature. In our case, this is the
226 // mixring. Future versions of the protocol may differ in this regard, but if this assumptions
227 // holds true in the future, enable the verification hash by modifying the `untested_tx`
228 // condition below.
229 const bool untested_tx = tx.version > 2 || tx.rct_signatures.type > rct::RCTTypeBulletproofPlus;
230 VER_ASSERT(!untested_tx, "Unknown TX type. Make sure RCT cache works correctly with this type and then enable it in the code here.");
231
232 // Don't cache older (or newer) rctSig types
233 // This cache only makes sense when it caches data from mempool first,
234 // so only "current fork version-enabled" RCT types need to be cached
235 if (tx.rct_signatures.type != rct_type_to_cache)
236 {
237 MDEBUG("RCT cache: tx " << get_transaction_hash(tx) << " skipped");
238 return expand_tx_and_ver_rct_non_sem(tx, mix_ring);
239 }
240
241 // Generate unique hash for tx+mix_ring pair
242 const crypto::hash tx_mixring_hash = calc_tx_mixring_hash(tx, mix_ring);
243
244 // Search cache for successful verification of same TX + mix ring combination
245 if (cache.has(tx_mixring_hash))
246 {
247 MDEBUG("RCT cache: tx " << get_transaction_hash(tx) << " hit");
248 return true;
249 }
250
251 // We had a cache miss, so now we must expand the mix ring and do full verification
252 MDEBUG("RCT cache: tx " << get_transaction_hash(tx) << " missed");
253 if (!expand_tx_and_ver_rct_non_sem(tx, mix_ring))
254 {
255 return false;
256 }
257
258 // At this point, the TX RCT verified successfully, so add it to the cache and return true
259 cache.add(tx_mixring_hash);
260
261 return true;
262 }
263
264 bool ver_mixed_rct_semantics(std::vector<const rct::rctSig*> rvv)
265 {
266 size_t batch_rv_size = 0; // this acts as an "end" iterator to the last simple batchable sig ptr
267 for (size_t i = 0; i < rvv.size(); ++i)
268 {
269 const rct::rctSig& rv = *rvv[i];
270
271 bool is_batchable_rv = false;
272
273 switch (rv.type)
274 {
275 case rct::RCTTypeNull:
276 // coinbase should not come here, so we reject for all other types
277 MERROR("Unexpected Null rctSig type");
278 return false;
279 break;
280 case rct::RCTTypeSimple:
281 if (!rct::verRctSemanticsSimple(rv))
282 {
283 MERROR("rct signature semantics check failed: type simple");
284 return false;
285 }
286 break;
287 case rct::RCTTypeFull:
288 if (!rct::verRct(rv, /*semantics=*/true))
289 {
290 MERROR("rct signature semantics check failed: type full");
291 return false;
292 }
293 break;
294 case rct::RCTTypeBulletproof:
295 case rct::RCTTypeBulletproof2:
296 case rct::RCTTypeCLSAG:
297 if (!is_canonical_bulletproof_layout(rv.p.bulletproofs))
298 {
299 MERROR("Bulletproof does not have canonical form");
300 return false;
301 }
302 is_batchable_rv = true;
303 break;
304 case rct::RCTTypeBulletproofPlus:
305 if (!is_canonical_bulletproof_plus_layout(rv.p.bulletproofs_plus))
306 {
307 MERROR("Bulletproof_plus does not have canonical form");
308 return false;
309 }
310 is_batchable_rv = true;
311 break;
312 default:
313 MERROR("Unknown rct type: " << rv.type);
314 return false;
315 break;
316 }
317
318 // Save this ring sig for later, as we will attempt simple RCT semantics batch verification
319 if (is_batchable_rv)
320 rvv[batch_rv_size++] = rvv[i];
321 }
322
323 if (batch_rv_size) // if any simple, batchable ring sigs...
324 {
325 rvv.resize(batch_rv_size);
326 if (!rct::verRctSemanticsSimple(rvv))
327 {
328 MERROR("rct signature semantics check failed: simple-style batch verification failed");
329 return false;
330 }
331 }
332
333 return true;
334 }
335
336 bool ver_non_input_consensus(const transaction& tx, tx_verification_context& tvc,
337 std::uint8_t hf_version)
338 {
339 return ver_non_input_consensus_templated(&tx, &tx + 1, tvc, hf_version);
340 }
341
342 bool ver_non_input_consensus(const pool_supplement& ps, tx_verification_context& tvc,
343 const std::uint8_t hf_version)
344 {
345 // We already verified the pool supplement for this hard fork version! Yippee!
346 if (ps.nic_verified_hf_version == hf_version)
347 return true;
348
349 const auto it_transform = [] (const decltype(ps.txs_by_txid)::value_type& in)
350 -> const transaction& { return in.second.first; };
351 const auto tx_begin = boost::make_transform_iterator(ps.txs_by_txid.cbegin(), it_transform);
352 const auto tx_end = boost::make_transform_iterator(ps.txs_by_txid.cend(), it_transform);
353
354 // Perform the checks...
355 const bool verified = ver_non_input_consensus_templated(tx_begin, tx_end, tvc, hf_version);
356
357 // Cache the hard fork version on success
358 if (verified)
359 ps.nic_verified_hf_version = hf_version;
360
361 return verified;
362 }
363
364 } // namespace cryptonote