| 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 <sstream> |
| 30 | |
| 31 | #define IN_UNIT_TESTS // To access Blockchain::{expand_transaction_2, verRctNonSemanticsSimpleCached} |
| 32 | |
| 33 | #include "gtest/gtest.h" |
| 34 | #include "unit_tests_utils.h" |
| 35 | |
| 36 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 37 | #include "cryptonote_core/blockchain.h" |
| 38 | #include "file_io_utils.h" |
| 39 | #include "misc_log_ex.h" |
| 40 | #include "ringct/rctSigs.h" |
| 41 | |
| 42 | namespace cryptonote |
| 43 | { |
| 44 | // declaration not provided in cryptonote_format_utils.h, but definition is not static ;) |
| 45 | bool expand_transaction_1(transaction &tx, bool base_only); |
| 46 | } |
| 47 | |
| 48 | namespace |
| 49 | { |
| 50 | /** |
| 51 | * @brief Make rct::ctkey from hex string representation of destionation and mask |
| 52 | * |
| 53 | * @param dest_hex |
| 54 | * @param mask_hex |
| 55 | * @return rct::ctkey |
| 56 | */ |
| 57 | static rct::ctkey make_ctkey(const char* dest_hex, const char* mask_hex) |
| 58 | { |
| 59 | rct::key dest; |
| 60 | rct::key mask; |
| 61 | CHECK_AND_ASSERT_THROW_MES(epee::from_hex::to_buffer(epee::as_mut_byte_span(dest), dest_hex), "dest bad hex: " << dest_hex); |
| 62 | CHECK_AND_ASSERT_THROW_MES(epee::from_hex::to_buffer(epee::as_mut_byte_span(mask), mask_hex), "mask bad hex: " << mask_hex); |
| 63 | return {dest, mask}; |
| 64 | } |
| 65 | |
| 66 | template <typename T> |
| 67 | static std::string stringify_with_do_serialize(const T& t) |
| 68 | { |
| 69 | std::stringstream ss; |
| 70 | binary_archive<true> ar(ss); |
| 71 | CHECK_AND_ASSERT_THROW_MES(ar.good(), "Archiver is not in a good state. This shouldn't happen!"); |
| 72 | ::do_serialize(ar, const_cast<T&>(t)); |
| 73 | return ss.str(); |
| 74 | } |
| 75 | |
| 76 | static bool check_tx_is_expanded(const cryptonote::transaction& tx, const rct::ctkeyM& pubkeys) |
| 77 | { |
| 78 | // Ripped from cryptonote_core/blockchain.cpp |
| 79 | |
| 80 | const rct::rctSig& rv = tx.rct_signatures; |
| 81 | |
| 82 | if (pubkeys.size() != rv.mixRing.size()) |
| 83 | { |
| 84 | MERROR("Failed to check ringct signatures: mismatched pubkeys/mixRing size"); |
| 85 | return false; |
| 86 | } |
| 87 | for (size_t i = 0; i < pubkeys.size(); ++i) |
| 88 | { |
| 89 | if (pubkeys[i].size() != rv.mixRing[i].size()) |
| 90 | { |
| 91 | MERROR("Failed to check ringct signatures: mismatched pubkeys/mixRing size"); |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | for (size_t n = 0; n < pubkeys.size(); ++n) |
| 97 | { |
| 98 | for (size_t m = 0; m < pubkeys[n].size(); ++m) |
| 99 | { |
| 100 | if (pubkeys[n][m].dest != rct::rct2pk(rv.mixRing[n][m].dest)) |
| 101 | { |
| 102 | MERROR("Failed to check ringct signatures: mismatched pubkey at vin " << n << ", index " << m); |
| 103 | return false; |
| 104 | } |
| 105 | if (pubkeys[n][m].mask != rct::rct2pk(rv.mixRing[n][m].mask)) |
| 106 | { |
| 107 | MERROR("Failed to check ringct signatures: mismatched commitment at vin " << n << ", index " << m); |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | const size_t n_sigs = rct::is_rct_clsag(rv.type) ? rv.p.CLSAGs.size() : rv.p.MGs.size(); |
| 114 | if (n_sigs != tx.vin.size()) |
| 115 | { |
| 116 | MERROR("Failed to check ringct signatures: mismatched MGs/vin sizes"); |
| 117 | return false; |
| 118 | } |
| 119 | for (size_t n = 0; n < tx.vin.size(); ++n) |
| 120 | { |
| 121 | bool error; |
| 122 | if (rct::is_rct_clsag(rv.type)) |
| 123 | error = memcmp(&boost::get<cryptonote::txin_to_key>(tx.vin[n]).k_image, &rv.p.CLSAGs[n].I, 32); |
| 124 | else |
| 125 | error = rv.p.MGs[n].II.empty() || memcmp(&boost::get<cryptonote::txin_to_key>(tx.vin[n]).k_image, &rv.p.MGs[n].II[0], 32); |
| 126 | if (error) |
| 127 | { |
| 128 | MERROR("Failed to check ringct signatures: mismatched key image"); |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @brief Perform expand_transaction_1 and Blockchain::expand_transaction_2 on a certain transaction |
| 138 | */ |
| 139 | static void expand_transaction_fully(cryptonote::transaction& tx, const rct::ctkeyM& input_pubkeys) |
| 140 | { |
| 141 | const crypto::hash tx_prefix_hash = cryptonote::get_transaction_prefix_hash(tx); |
| 142 | CHECK_AND_ASSERT_THROW_MES(cryptonote::expand_transaction_1(tx, false), "expand 1 failed"); |
| 143 | CHECK_AND_ASSERT_THROW_MES |
| 144 | ( |
| 145 | cryptonote::Blockchain::expand_transaction_2(tx, tx_prefix_hash, input_pubkeys), |
| 146 | "expand 2 failed" |
| 147 | ); |
| 148 | CHECK_AND_ASSERT_THROW_MES(!memcmp(&tx_prefix_hash, &tx.rct_signatures.message, 32), "message check failed"); |
| 149 | CHECK_AND_ASSERT_THROW_MES(input_pubkeys == tx.rct_signatures.mixRing, "mixring check failed"); |
| 150 | CHECK_AND_ASSERT_THROW_MES(check_tx_is_expanded(tx, input_pubkeys), "tx expansion check 2 failed"); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @brief Mostly construct transaction from binary file and provided mix ring pubkeys |
| 155 | * |
| 156 | * Most important to us, this should populate the .rct_signatures.message and |
| 157 | * .rct_signatures.mixRings fields of the transaction. |
| 158 | * |
| 159 | * @param file_name relative file path in unit test data directory |
| 160 | * @param input_pubkeys manually retrived input pubkey destination / masks for each ring |
| 161 | * @return cryptonote::transaction the expanded transaction |
| 162 | */ |
| 163 | static cryptonote::transaction expand_transaction_from_bin_file_and_pubkeys |
| 164 | ( |
| 165 | const char* file_name, |
| 166 | const rct::ctkeyM& input_pubkeys |
| 167 | ) |
| 168 | { |
| 169 | cryptonote::transaction transaction; |
| 170 | |
| 171 | const boost::filesystem::path tx_json_path = unit_test::data_dir / file_name; |
| 172 | std::string tx_blob; |
| 173 | CHECK_AND_ASSERT_THROW_MES |
| 174 | ( |
| 175 | epee::file_io_utils::load_file_to_string(tx_json_path.string(), tx_blob), |
| 176 | "loading file to string failed" |
| 177 | ); |
| 178 | |
| 179 | CHECK_AND_ASSERT_THROW_MES |
| 180 | ( |
| 181 | cryptonote::parse_and_validate_tx_from_blob(tx_blob, transaction), |
| 182 | "TX blob could not be parsed" |
| 183 | ); |
| 184 | |
| 185 | expand_transaction_fully(transaction, input_pubkeys); |
| 186 | |
| 187 | return transaction; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @brief Return whether a modification changes blob resulting from do_serialize() |
| 192 | */ |
| 193 | template <typename T, class TModifier> |
| 194 | static bool modification_changes_do_serialize |
| 195 | ( |
| 196 | const T& og_obj, |
| 197 | TModifier& obj_modifier_func, |
| 198 | bool expected_change |
| 199 | ) |
| 200 | { |
| 201 | T modded_obj = og_obj; |
| 202 | obj_modifier_func(modded_obj); |
| 203 | const std::string og_blob = stringify_with_do_serialize(og_obj); |
| 204 | const std::string modded_blob = stringify_with_do_serialize(modded_obj); |
| 205 | const bool did_change = modded_blob != og_blob; |
| 206 | if (did_change != expected_change) |
| 207 | { |
| 208 | const std::string og_hex = epee::to_hex::string(epee::strspan<uint8_t>(og_blob)); |
| 209 | const std::string modded_hex = epee::to_hex::string(epee::strspan<uint8_t>(modded_blob)); |
| 210 | MERROR("unexpected: modded_blob '" << modded_hex << "' vs og_blob ' << " << og_hex << "'"); |
| 211 | } |
| 212 | return did_change; |
| 213 | } |
| 214 | |
| 215 | // Contains binary representation of mainnet transaction (height 2777777): |
| 216 | // e89415b95564aa7e3587c91422756ba5303e727996e19c677630309a0d52a7ca |
| 217 | static constexpr const char* tx1_file_name = "txs/bpp_tx_e89415.bin"; |
| 218 | |
| 219 | // This contains destination key / mask pairs for each output in the input ring of the above tx |
| 220 | static const rct::ctkeyM tx1_input_pubkeys = |
| 221 | {{ |
| 222 | make_ctkey("e50f476129d40af31e0938743f7f2d60e867aab31294f7acaf6e38f0976f0228", "51e788ddf5c95c124a7314d45a91b52d60db25a0572de9c2b4ec515aca3d4481"), |
| 223 | make_ctkey("804245d067fcfe6cd66376db0571869989bc68b3e22a0f902109c7530df47a59", "c3cc65d3b3a05defaa05213dc3b0496f9b86dbeeefbff28db34b134b6ee3230b"), |
| 224 | make_ctkey("527563a03b498e47732b815f5f0c5875a70e0fb71a37c88123f0f8686349fae4", "04417c03b397cd11e403275ec89cb0ab5b8476bb88470e9ae7208ea63dacf073"), |
| 225 | make_ctkey("bffca8b5c7fe4235ba7136d6b5325f63df343dc147940b677f50217f8953bca6", "5cd8c5e54e07275422c9c5a9f4a7268d26c494ffba419e878b7e873a02ae2e76"), |
| 226 | make_ctkey("1f73385ea74308aa78b5abf585faac14a5e78a6e23f0f68c9c14681108b28ef0", "5c02b3156daaa8ec476d3244439d90efa266f3e51cb9c8eb384d8b9a8efaa024"), |
| 227 | make_ctkey("a2421eae8bb256644b34feeab48c6086c2c9feb40d2643436dc45e303eee8ab2", "787823abffa988b56d4a7b4a834630f71520220fd82fad035955e616ec095788"), |
| 228 | make_ctkey("17d8d8dc1e1c25b7295f2eab44c4ccc08a629b8e8d781bbb6f9a51a9561bcd4c", "db1ea24be6947e03176a297160dba16d65f37751bb0ef2ba71a4590d12b61dfc"), |
| 229 | make_ctkey("2c39348a9ab04dbabe3b5249819b7845ed8aaebd0d8eddd98bda0bf40753a398", "4e6cd25fbd10e2e040be84e3bf8043c612daeef625e66a5e5bcff88c9c46e82c"), |
| 230 | make_ctkey("c4c97157f23b45c7084526aaa9958fe858bebe446a7efa22c491c439b74271b1", "e251db2c86193a11a5bffefffe48c20e3d92a8dc98cb3a2f41704e565bcd860a"), |
| 231 | make_ctkey("d342045525139a8551bcdfa7aa0117d2ac2327cb6cf449ca59420c300e4471a5", "789c11f72060ad80f4cda5d89b24d49f9435bf765598dea7a91776e99f05f87c"), |
| 232 | make_ctkey("9a972ccf2c74f648070b0be839749c98eca87166de401a6c1f59e64b938a46c1", "5444cbed5cec31fb6ed1612f815d292f2bf3d2ff584bbcd8e5201ec59670d414"), |
| 233 | make_ctkey("49ccb806ccf5cbd74bae8d9fb2da8918ab61d0774ee6a6c3a6ccd237db22a088", "0c5db942fb44f29f6ef956e24db91f98a6de6e7288b0b04d01b8f260453d1431"), |
| 234 | make_ctkey("74417e8d1483df2df6fe68c88fc9a72639c35d765b38351b838521addf45dadc", "a1a606d6c4762ef51c1759bcb8b5c88be1d323025400c41fe6885431064b64dc"), |
| 235 | make_ctkey("48c4c349adaf7b3be27656ea70d1c83b93e1511bb0aac987861a4da9689b0e95", "ad14ffd5edac199ea7c5437d558089b0f2f03aa74bde43611322d769968b5a1c"), |
| 236 | make_ctkey("2d2ffade0f85ddd83a036469e49542e93cad94f9bea535f0ea2eb2f56304517e", "bcc48d00bd06dc5439200e749d0caf8a062b072d0c0eb1f78f6a4d8f2373e5f4"), |
| 237 | make_ctkey("4ee857d0ce17f66eca9c81eb326e404ceb50c8198248f2f827c440ee7aa0c0d7", "a8a9d61d4abbfb123630ffd214c834cc45113eaa51dd2f904cc6ae0c3c5d70e3") |
| 238 | }}; |
| 239 | } // anonymous namespace |
| 240 | |
| 241 | TEST(verRctNonSemanticsSimple, tx1_preconditions) |
| 242 | { |
| 243 | // If this unit test fails, something changed about transaction deserialization / expansion or |
| 244 | // something changed about RingCT signature verification. |
| 245 | |
| 246 | cryptonote::rct_ver_cache_t rct_ver_cache; |
| 247 | |
| 248 | cryptonote::transaction tx = expand_transaction_from_bin_file_and_pubkeys |
| 249 | (tx1_file_name, tx1_input_pubkeys); |
| 250 | const rct::rctSig& rs = tx.rct_signatures; |
| 251 | |
| 252 | const crypto::hash tx_prefix_hash = cryptonote::get_transaction_prefix_hash(tx); |
| 253 | |
| 254 | EXPECT_EQ(1, tx.vin.size()); |
| 255 | EXPECT_EQ(2, tx.vout.size()); |
| 256 | const rct::key expected_sig_msg = rct::hash2rct(tx_prefix_hash); |
| 257 | EXPECT_EQ(expected_sig_msg, rs.message); |
| 258 | EXPECT_EQ(1, rs.mixRing.size()); |
| 259 | EXPECT_EQ(16, rs.mixRing[0].size()); |
| 260 | EXPECT_EQ(0, rs.pseudoOuts.size()); |
| 261 | EXPECT_EQ(0, rs.p.rangeSigs.size()); |
| 262 | EXPECT_EQ(0, rs.p.bulletproofs.size()); |
| 263 | EXPECT_EQ(1, rs.p.bulletproofs_plus.size()); |
| 264 | EXPECT_EQ(2, rs.p.bulletproofs_plus[0].V.size()); |
| 265 | EXPECT_EQ(7, rs.p.bulletproofs_plus[0].L.size()); |
| 266 | EXPECT_EQ(7, rs.p.bulletproofs_plus[0].R.size()); |
| 267 | EXPECT_EQ(0, rs.p.MGs.size()); |
| 268 | EXPECT_EQ(1, rs.p.CLSAGs.size()); |
| 269 | EXPECT_EQ(16, rs.p.CLSAGs[0].s.size()); |
| 270 | EXPECT_EQ(1, rs.p.pseudoOuts.size()); |
| 271 | EXPECT_EQ(tx1_input_pubkeys, rs.mixRing); |
| 272 | EXPECT_EQ(2, rs.outPk.size()); |
| 273 | |
| 274 | EXPECT_TRUE(rct::verRctSemanticsSimple(rs)); |
| 275 | EXPECT_TRUE(rct::verRctNonSemanticsSimple(rs)); |
| 276 | EXPECT_TRUE(rct::verRctSimple(rs)); |
| 277 | EXPECT_TRUE(cryptonote::ver_rct_non_semantics_simple_cached(tx, tx1_input_pubkeys, rct_ver_cache, rct::RCTTypeBulletproofPlus)); |
| 278 | EXPECT_TRUE(cryptonote::ver_rct_non_semantics_simple_cached(tx, tx1_input_pubkeys, rct_ver_cache, rct::RCTTypeBulletproofPlus)); |
| 279 | } |
| 280 | |
| 281 | #define SERIALIZABLE_SIG_CHANGES_SUBTEST(fieldmodifyclause) \ |
| 282 | do { \ |
| 283 | const auto sig_modifier_func = [](rct::rctSig& rs) { rs.fieldmodifyclause; }; \ |
| 284 | EXPECT_TRUE(modification_changes_do_serialize(original_sig, sig_modifier_func, true)); \ |
| 285 | } while (0); \ |
| 286 | |
| 287 | TEST(verRctNonSemanticsSimple, serializable_sig_changes) |
| 288 | { |
| 289 | // Hello, future visitors. If this unit test fails, then fields of rctSig have been dropped from |
| 290 | // serialization. |
| 291 | |
| 292 | const cryptonote::transaction tx = expand_transaction_from_bin_file_and_pubkeys |
| 293 | (tx1_file_name, tx1_input_pubkeys); |
| 294 | const rct::rctSig& original_sig = tx.rct_signatures; |
| 295 | |
| 296 | // These are the subtests most likely to fail. Fields 'message' and 'mixRing' are not serialized |
| 297 | // when sent over the wire, since they can be reconstructed from transaction data. However, they |
| 298 | // are serialized by ::do_serialize(rctSig). |
| 299 | // How signatures are serialized for the blockchain can be found in the methods |
| 300 | // rct::rctSigBase::serialize_rctsig_base and rct::rctSigPrunable::serialize_rctsig_prunable. |
| 301 | SERIALIZABLE_SIG_CHANGES_SUBTEST(message.bytes[31]++) |
| 302 | SERIALIZABLE_SIG_CHANGES_SUBTEST(mixRing.push_back({})) |
| 303 | SERIALIZABLE_SIG_CHANGES_SUBTEST(mixRing[0].push_back({})) |
| 304 | SERIALIZABLE_SIG_CHANGES_SUBTEST(mixRing[0][8].dest[10]--) |
| 305 | SERIALIZABLE_SIG_CHANGES_SUBTEST(mixRing[0][15].mask[3]--) |
| 306 | |
| 307 | // rctSigBase changes. These subtests are less likely to break |
| 308 | SERIALIZABLE_SIG_CHANGES_SUBTEST(type ^= 23) |
| 309 | SERIALIZABLE_SIG_CHANGES_SUBTEST(pseudoOuts.push_back({})) |
| 310 | SERIALIZABLE_SIG_CHANGES_SUBTEST(ecdhInfo.push_back({})) |
| 311 | SERIALIZABLE_SIG_CHANGES_SUBTEST(outPk.push_back({})) |
| 312 | SERIALIZABLE_SIG_CHANGES_SUBTEST(outPk[0].dest[14]--) |
| 313 | SERIALIZABLE_SIG_CHANGES_SUBTEST(outPk[1].dest[14]--) |
| 314 | SERIALIZABLE_SIG_CHANGES_SUBTEST(outPk[0].mask[14]--) |
| 315 | SERIALIZABLE_SIG_CHANGES_SUBTEST(outPk[1].mask[14]--) |
| 316 | SERIALIZABLE_SIG_CHANGES_SUBTEST(txnFee *= 2023) |
| 317 | |
| 318 | // rctSigPrunable changes |
| 319 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.rangeSigs.push_back({})) |
| 320 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs.push_back({})) |
| 321 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus.push_back({})) |
| 322 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].A[13] -= 7) |
| 323 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].A1[13] -= 7) |
| 324 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].B[13] -= 7) |
| 325 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].r1[13] -= 7) |
| 326 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].s1[13] -= 7) |
| 327 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].d1[13] -= 7) |
| 328 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].L.push_back({})) |
| 329 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].L[2][13] -= 7) |
| 330 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].R.push_back({})) |
| 331 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].R[2][13] -= 7) |
| 332 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.MGs.push_back({})) |
| 333 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs.push_back({})) |
| 334 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs[0].s.push_back({})) |
| 335 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs[0].s[15][31] ^= 69) |
| 336 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs[0].c1[0] /= 3) |
| 337 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs[0].D[0] /= 3) |
| 338 | SERIALIZABLE_SIG_CHANGES_SUBTEST(p.pseudoOuts.push_back({})) |
| 339 | |
| 340 | // Uncomment line below to sanity check SERIALIZABLE_SIG_CHANGES_SUBTEST |
| 341 | // SERIALIZABLE_SIG_CHANGES_SUBTEST(message) // should fail |
| 342 | } |
| 343 | |
| 344 | #define UNSERIALIZABLE_SIG_CHANGES_SUBTEST(fieldmodifyclause) \ |
| 345 | do { \ |
| 346 | const auto sig_modifier_func = [](rct::rctSig& rs) { rs.fieldmodifyclause; }; \ |
| 347 | EXPECT_FALSE(modification_changes_do_serialize(original_sig, sig_modifier_func, false)); \ |
| 348 | } while (0); \ |
| 349 | |
| 350 | TEST(verRctNonSemanticsSimple, unserializable_sig_changes) |
| 351 | { |
| 352 | // Hello, future visitors. If this unit test fails, then congrats! ::do_serialize(rctSig) became |
| 353 | // better at uniquely representing rctSig. |
| 354 | const cryptonote::transaction tx = expand_transaction_from_bin_file_and_pubkeys |
| 355 | (tx1_file_name, tx1_input_pubkeys); |
| 356 | const rct::rctSig& original_sig = tx.rct_signatures; |
| 357 | |
| 358 | UNSERIALIZABLE_SIG_CHANGES_SUBTEST(p.CLSAGs[0].I[14]++) |
| 359 | UNSERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].V.push_back({})) |
| 360 | UNSERIALIZABLE_SIG_CHANGES_SUBTEST(p.bulletproofs_plus[0].V[1][31]--) |
| 361 | |
| 362 | // Uncomment line below to sanity check UNSERIALIZABLE_SIG_CHANGES_SUBTEST_SHORTCUT |
| 363 | // UNSERIALIZABLE_SIG_CHANGES_SUBTEST_SHORTCUT(message[2]++) // should fail |
| 364 | } |
| 365 | |
| 366 | #define SERIALIZABLE_MIXRING_CHANGES_SUBTEST(fieldmodifyclause) \ |
| 367 | do { \ |
| 368 | using mr_mod_func_t = std::function<void(rct::ctkeyM&)>; \ |
| 369 | const mr_mod_func_t mr_modifier_func = [&](rct::ctkeyM& mr) { mr fieldmodifyclause; }; \ |
| 370 | EXPECT_TRUE(modification_changes_do_serialize(original_mixring, mr_modifier_func, true)); \ |
| 371 | } while (0); \ |
| 372 | |
| 373 | TEST(verRctNonSemanticsSimple, serializable_mixring_changes) |
| 374 | { |
| 375 | // Hello, future Monero devs! If this unit test fails, a huge concensus-related assumption has |
| 376 | // been broken and verRctNonSemanticsSimpleCached needs to be reevalulated for validity. If it |
| 377 | // is not, there may be an exploit which allows for double-spending. See the implementation for |
| 378 | // more comments on the uniqueness of the internal cache hash. |
| 379 | |
| 380 | const rct::ctkeyM original_mixring = tx1_input_pubkeys; |
| 381 | |
| 382 | const size_t mlen = tx1_input_pubkeys.size(); |
| 383 | ASSERT_EQ(1, mlen); |
| 384 | const size_t nlen = tx1_input_pubkeys[0].size(); |
| 385 | ASSERT_EQ(16, nlen); |
| 386 | |
| 387 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST(.clear()) |
| 388 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST(.push_back({})) |
| 389 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([0].clear()) |
| 390 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([0].push_back({})) |
| 391 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([0][0].dest[4]--) |
| 392 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([0][15].mask[31]--) |
| 393 | |
| 394 | // Loop through all bytes of the mixRing and check for serialiable changes |
| 395 | for (size_t i = 0; i < mlen; ++i) |
| 396 | { |
| 397 | for (size_t j = 0; j < nlen; ++j) |
| 398 | { |
| 399 | static_assert(sizeof(rct::key) == 32, "rct::key size wrong"); |
| 400 | for (size_t k = 0; k < sizeof(rct::key); ++k) |
| 401 | { |
| 402 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([i][j].dest[k]++) |
| 403 | SERIALIZABLE_MIXRING_CHANGES_SUBTEST([i][j].mask[k]++) |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | #define EXPAND_TRANSACTION_2_FAILURES_SUBTEST(fieldmodifyclause) \ |
| 410 | do { \ |
| 411 | cryptonote::transaction test_tx = original_tx; \ |
| 412 | test_tx.fieldmodifyclause; \ |
| 413 | test_tx.invalidate_hashes(); \ |
| 414 | EXPECT_FALSE(check_tx_is_expanded(test_tx, original_mixring)); \ |
| 415 | } while (0); \ |
| 416 | |
| 417 | TEST(verRctNonSemanticsSimple, expand_transaction_2_failures) |
| 418 | { |
| 419 | cryptonote::transaction original_tx = expand_transaction_from_bin_file_and_pubkeys |
| 420 | (tx1_file_name, tx1_input_pubkeys); |
| 421 | rct::ctkeyM original_mixring = tx1_input_pubkeys; |
| 422 | |
| 423 | EXPAND_TRANSACTION_2_FAILURES_SUBTEST(rct_signatures.p.CLSAGs[0].I[0]++) |
| 424 | EXPAND_TRANSACTION_2_FAILURES_SUBTEST(rct_signatures.mixRing[0][15].dest[31]++) |
| 425 | EXPAND_TRANSACTION_2_FAILURES_SUBTEST(rct_signatures.mixRing[0][15].mask[31]++) |
| 426 | } |