| 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 | #include "ringct/rctSigs.h" |
| 32 | #include "ringct/bulletproofs.h" |
| 33 | #include "chaingen.h" |
| 34 | #include "rct2.h" |
| 35 | #include "device/device.hpp" |
| 36 | |
| 37 | using namespace epee; |
| 38 | using namespace crypto; |
| 39 | using namespace cryptonote; |
| 40 | |
| 41 | //---------------------------------------------------------------------------------------------------------------------- |
| 42 | // Tests |
| 43 | |
| 44 | bool gen_rct2_tx_validation_base::generate_with(std::vector<test_event_entry>& events, |
| 45 | size_t mixin, size_t n_txes, const uint64_t *amounts_paid, bool valid, const rct::RCTConfig *rct_config, uint8_t hf_version, |
| 46 | const std::function<bool(std::vector<tx_source_entry> &sources, std::vector<tx_destination_entry> &destinations, size_t tx_idx)> &pre_tx, |
| 47 | const std::function<bool(transaction &tx, size_t tx_idx)> &post_tx) const |
| 48 | { |
| 49 | uint64_t ts_start = 1338224400; |
| 50 | |
| 51 | GENERATE_ACCOUNT(miner_account); |
| 52 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); |
| 53 | |
| 54 | // create 12 miner accounts, and have them mine the next 12 blocks |
| 55 | cryptonote::account_base miner_accounts[12]; |
| 56 | const cryptonote::block *prev_block = &blk_0; |
| 57 | cryptonote::block blocks[12 + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW]; |
| 58 | for (size_t n = 0; n < 12; ++n) { |
| 59 | miner_accounts[n].generate(); |
| 60 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blocks[n], *prev_block, miner_accounts[n], |
| 61 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp | test_generator::bf_hf_version, |
| 62 | 2, 2, prev_block->timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 63 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0, 2), |
| 64 | false, "Failed to generate block"); |
| 65 | events.push_back(blocks[n]); |
| 66 | prev_block = blocks + n; |
| 67 | LOG_PRINT_L0("Initial miner tx " << n << ": " << obj_to_json_str(blocks[n].miner_tx)); |
| 68 | } |
| 69 | |
| 70 | // rewind |
| 71 | cryptonote::block blk_r, blk_last; |
| 72 | { |
| 73 | blk_last = blocks[11]; |
| 74 | for (size_t i = 0; i < CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; ++i) |
| 75 | { |
| 76 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blocks[12+i], blk_last, miner_account, |
| 77 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp | test_generator::bf_hf_version, |
| 78 | 2, 2, blk_last.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 79 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0, 2), |
| 80 | false, "Failed to generate block"); |
| 81 | events.push_back(blocks[12+i]); |
| 82 | blk_last = blocks[12+i]; |
| 83 | } |
| 84 | blk_r = blk_last; |
| 85 | } |
| 86 | |
| 87 | // create 4 txes from these miners in another block, to generate some rct outputs |
| 88 | std::vector<transaction> rct_txes; |
| 89 | cryptonote::block blk_txes; |
| 90 | std::vector<crypto::hash> starting_rct_tx_hashes; |
| 91 | static const uint64_t input_amounts_available[] = {5000000000000, 30000000000000, 100000000000, 80000000000}; |
| 92 | for (size_t n = 0; n < n_txes; ++n) |
| 93 | { |
| 94 | std::vector<tx_source_entry> sources; |
| 95 | |
| 96 | sources.resize(1); |
| 97 | tx_source_entry& src = sources.back(); |
| 98 | |
| 99 | const uint64_t needed_amount = input_amounts_available[n]; |
| 100 | src.amount = input_amounts_available[n]; |
| 101 | size_t real_index_in_tx = 0; |
| 102 | for (size_t m = 0; m <= mixin; ++m) { |
| 103 | size_t index_in_tx = 0; |
| 104 | for (size_t i = 0; i < blocks[m].miner_tx.vout.size(); ++i) |
| 105 | if (blocks[m].miner_tx.vout[i].amount == needed_amount) |
| 106 | index_in_tx = i; |
| 107 | CHECK_AND_ASSERT_MES(blocks[m].miner_tx.vout[index_in_tx].amount == needed_amount, false, "Expected amount not found"); |
| 108 | src.push_output(m, boost::get<txout_to_key>(blocks[m].miner_tx.vout[index_in_tx].target).key, src.amount); |
| 109 | if (m == n) |
| 110 | real_index_in_tx = index_in_tx; |
| 111 | } |
| 112 | src.real_out_tx_key = cryptonote::get_tx_pub_key_from_extra(blocks[n].miner_tx); |
| 113 | src.real_output = n; |
| 114 | src.real_output_in_tx_index = real_index_in_tx; |
| 115 | src.mask = rct::identity(); |
| 116 | src.rct = false; |
| 117 | |
| 118 | //fill outputs entry |
| 119 | tx_destination_entry td; |
| 120 | td.addr = miner_accounts[n].get_keys().m_account_address; |
| 121 | std::vector<tx_destination_entry> destinations; |
| 122 | for (int o = 0; amounts_paid[o] != (uint64_t)-1; ++o) |
| 123 | { |
| 124 | td.amount = amounts_paid[o]; |
| 125 | destinations.push_back(td); |
| 126 | } |
| 127 | |
| 128 | if (pre_tx && !pre_tx(sources, destinations, n)) |
| 129 | { |
| 130 | MDEBUG("pre_tx returned failure"); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | crypto::secret_key tx_key; |
| 135 | std::vector<crypto::secret_key> additional_tx_keys; |
| 136 | std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; |
| 137 | subaddresses[miner_accounts[n].get_keys().m_account_address.m_spend_public_key] = {0,0}; |
| 138 | rct_txes.resize(rct_txes.size() + 1); |
| 139 | bool r = construct_tx_and_get_tx_key(miner_accounts[n].get_keys(), subaddresses, sources, destinations, boost::none, std::vector<uint8_t>(), rct_txes.back(), tx_key, additional_tx_keys, true, rct_config[n]); |
| 140 | CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); |
| 141 | |
| 142 | if (post_tx && !post_tx(rct_txes.back(), n)) |
| 143 | { |
| 144 | MDEBUG("post_tx returned failure"); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | //events.push_back(rct_txes.back()); |
| 149 | starting_rct_tx_hashes.push_back(get_transaction_hash(rct_txes.back())); |
| 150 | LOG_PRINT_L0("Test tx: " << obj_to_json_str(rct_txes.back())); |
| 151 | |
| 152 | for (int o = 0; amounts_paid[o] != (uint64_t)-1; ++o) |
| 153 | { |
| 154 | crypto::key_derivation derivation; |
| 155 | bool r = crypto::generate_key_derivation(destinations[o].addr.m_view_public_key, tx_key, derivation); |
| 156 | CHECK_AND_ASSERT_MES(r, false, "Failed to generate key derivation"); |
| 157 | crypto::secret_key amount_key; |
| 158 | crypto::derivation_to_scalar(derivation, o, amount_key); |
| 159 | rct::key rct_tx_mask; |
| 160 | const uint8_t type = rct_txes.back().rct_signatures.type; |
| 161 | if (rct::is_rct_simple(type)) |
| 162 | rct::decodeRctSimple(rct_txes.back().rct_signatures, rct::sk2rct(amount_key), o, rct_tx_mask, hw::get_device("default")); |
| 163 | else |
| 164 | rct::decodeRct(rct_txes.back().rct_signatures, rct::sk2rct(amount_key), o, rct_tx_mask, hw::get_device("default")); |
| 165 | } |
| 166 | |
| 167 | while (amounts_paid[0] != (size_t)-1) |
| 168 | ++amounts_paid; |
| 169 | ++amounts_paid; |
| 170 | } |
| 171 | if (!valid) |
| 172 | DO_CALLBACK(events, "mark_invalid_tx"); |
| 173 | events.push_back(rct_txes); |
| 174 | |
| 175 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blk_txes, blk_last, miner_account, |
| 176 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp | test_generator::bf_tx_hashes | test_generator::bf_hf_version | test_generator::bf_max_outs, |
| 177 | hf_version, hf_version, blk_last.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 178 | crypto::hash(), 0, transaction(), starting_rct_tx_hashes, 0, 6, hf_version), |
| 179 | false, "Failed to generate block"); |
| 180 | if (!valid) |
| 181 | DO_CALLBACK(events, "mark_invalid_block"); |
| 182 | events.push_back(blk_txes); |
| 183 | blk_last = blk_txes; |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool gen_rct2_tx_validation_base::check_bp(const cryptonote::transaction &tx, size_t tx_idx, const size_t *sizes, const char *context) const |
| 189 | { |
| 190 | DEFINE_TESTS_ERROR_CONTEXT(context); |
| 191 | CHECK_TEST_CONDITION(tx.version >= 2); |
| 192 | CHECK_TEST_CONDITION(rct::is_rct_bulletproof(tx.rct_signatures.type)); |
| 193 | size_t n_sizes = 0, n_amounts = 0; |
| 194 | for (size_t n = 0; n < tx_idx; ++n) |
| 195 | { |
| 196 | while (sizes[0] != (size_t)-1) |
| 197 | ++sizes; |
| 198 | ++sizes; |
| 199 | } |
| 200 | while (sizes[n_sizes] != (size_t)-1) |
| 201 | n_amounts += sizes[n_sizes++]; |
| 202 | CHECK_TEST_CONDITION(tx.rct_signatures.p.bulletproofs.size() == n_sizes); |
| 203 | CHECK_TEST_CONDITION(rct::n_bulletproof_max_amounts(tx.rct_signatures.p.bulletproofs) == n_amounts); |
| 204 | for (size_t n = 0; n < n_sizes; ++n) |
| 205 | CHECK_TEST_CONDITION(rct::n_bulletproof_max_amounts(tx.rct_signatures.p.bulletproofs[n]) == sizes[n]); |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | bool gen_rct2_tx_clsag_malleability::generate(std::vector<test_event_entry>& events) const |
| 210 | { |
| 211 | DEFINE_TESTS_ERROR_CONTEXT("gen_rct_tx_clsag_malleability"); |
| 212 | const int mixin = 10; |
| 213 | const uint64_t amounts_paid[] = {5000, 5000, (uint64_t)-1}; |
| 214 | const rct::RCTConfig rct_config[] = { { rct::RangeProofPaddedBulletproof, 3 } }; |
| 215 | return generate_with(events, mixin, 1, amounts_paid, false, rct_config, HF_VERSION_CLSAG + 1, NULL, [&](cryptonote::transaction &tx, size_t tx_idx) { |
| 216 | CHECK_TEST_CONDITION(tx.version == 2); |
| 217 | CHECK_TEST_CONDITION(tx.rct_signatures.type == rct::RCTTypeCLSAG); |
| 218 | CHECK_TEST_CONDITION(!tx.rct_signatures.p.CLSAGs.empty()); |
| 219 | rct::key x; |
| 220 | CHECK_TEST_CONDITION(epee::string_tools::hex_to_pod("c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", x)); |
| 221 | tx.rct_signatures.p.CLSAGs[0].D = rct::addKeys(tx.rct_signatures.p.CLSAGs[0].D, x); |
| 222 | return true; |
| 223 | }); |
| 224 | } |