| 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 "chaingen.h" |
| 32 | #include "ring_signature_1.h" |
| 33 | |
| 34 | using namespace epee; |
| 35 | using namespace cryptonote; |
| 36 | |
| 37 | |
| 38 | //////// |
| 39 | // class gen_ring_signature_1; |
| 40 | |
| 41 | gen_ring_signature_1::gen_ring_signature_1() |
| 42 | { |
| 43 | REGISTER_CALLBACK("check_balances_1", gen_ring_signature_1::check_balances_1); |
| 44 | REGISTER_CALLBACK("check_balances_2", gen_ring_signature_1::check_balances_2); |
| 45 | } |
| 46 | |
| 47 | namespace |
| 48 | { |
| 49 | // To be sure that miner tx outputs don't match any bob_account and some_accounts inputs |
| 50 | const uint64_t rnd_11 = 475921; |
| 51 | const uint64_t rnd_20 = 360934; |
| 52 | const uint64_t rnd_29 = 799665; |
| 53 | } |
| 54 | |
| 55 | bool gen_ring_signature_1::generate(std::vector<test_event_entry>& events) const |
| 56 | { |
| 57 | uint64_t ts_start = 1338224400; |
| 58 | |
| 59 | GENERATE_ACCOUNT(miner_account); |
| 60 | |
| 61 | // events |
| 62 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); // 0 |
| 63 | MAKE_ACCOUNT(events, some_account_1); // 1 |
| 64 | MAKE_ACCOUNT(events, some_account_2); // 2 |
| 65 | MAKE_ACCOUNT(events, bob_account); // 3 |
| 66 | MAKE_ACCOUNT(events, alice_account); // 4 |
| 67 | MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_account); // 5 |
| 68 | MAKE_NEXT_BLOCK(events, blk_2, blk_1, miner_account); // 6 |
| 69 | MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_account); // 7 |
| 70 | MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account); // 8 |
| 71 | REWIND_BLOCKS(events, blk_5, blk_4, miner_account); // <N blocks> |
| 72 | REWIND_BLOCKS(events, blk_5r, blk_5, miner_account); // <N blocks> |
| 73 | MAKE_TX_LIST_START(events, txs_blk_6, miner_account, bob_account, MK_COINS(1), blk_5); // 9 + 2N |
| 74 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(11) + rnd_11, blk_5); // 10 + 2N |
| 75 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(11) + rnd_11, blk_5); // 11 + 2N |
| 76 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(20) + rnd_20, blk_5); // 12 + 2N |
| 77 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(29) + rnd_29, blk_5); // 13 + 2N |
| 78 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(29) + rnd_29, blk_5); // 14 + 2N |
| 79 | MAKE_TX_LIST(events, txs_blk_6, miner_account, bob_account, MK_COINS(29) + rnd_29, blk_5); // 15 + 2N |
| 80 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_1, MK_COINS(11) + rnd_11, blk_5); // 16 + 2N |
| 81 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_1, MK_COINS(11) + rnd_11, blk_5); // 17 + 2N |
| 82 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_1, MK_COINS(11) + rnd_11, blk_5); // 18 + 2N |
| 83 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_1, MK_COINS(11) + rnd_11, blk_5); // 19 + 2N |
| 84 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_1, MK_COINS(20) + rnd_20, blk_5); // 20 + 2N |
| 85 | MAKE_TX_LIST(events, txs_blk_6, miner_account, some_account_2, MK_COINS(20) + rnd_20, blk_5); // 21 + 2N |
| 86 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_6, blk_5r, miner_account, txs_blk_6); // 22 + 2N |
| 87 | DO_CALLBACK(events, "check_balances_1"); // 23 + 2N |
| 88 | REWIND_BLOCKS(events, blk_6r, blk_6, miner_account); // <N blocks> |
| 89 | // 129 = 11 + 11 + 20 + 29 + 29 + 29 |
| 90 | MAKE_TX_MIX(events, tx_0, bob_account, alice_account, MK_COINS(129) + 2 * rnd_11 + rnd_20 + 3 * rnd_29 - TESTS_DEFAULT_FEE, 2, blk_6); // 24 + 3N |
| 91 | MAKE_NEXT_BLOCK_TX1(events, blk_7, blk_6r, miner_account, tx_0); // 25 + 3N |
| 92 | DO_CALLBACK(events, "check_balances_2"); // 26 + 3N |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool gen_ring_signature_1::check_balances_1(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 98 | { |
| 99 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_1::check_balances_1"); |
| 100 | |
| 101 | m_bob_account = boost::get<account_base>(events[3]); |
| 102 | m_alice_account = boost::get<account_base>(events[4]); |
| 103 | |
| 104 | std::vector<block> blocks; |
| 105 | bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 106 | CHECK_TEST_CONDITION(r); |
| 107 | |
| 108 | std::vector<cryptonote::block> chain; |
| 109 | map_hash2tx_t mtx; |
| 110 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 111 | CHECK_TEST_CONDITION(r); |
| 112 | CHECK_EQ(MK_COINS(130) + 2 * rnd_11 + rnd_20 + 3 * rnd_29, get_balance(m_bob_account, chain, mtx)); |
| 113 | CHECK_EQ(0, get_balance(m_alice_account, chain, mtx)); |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool gen_ring_signature_1::check_balances_2(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 119 | { |
| 120 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_1::check_balances_2"); |
| 121 | |
| 122 | std::vector<block> blocks; |
| 123 | bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 124 | CHECK_TEST_CONDITION(r); |
| 125 | |
| 126 | std::vector<cryptonote::block> chain; |
| 127 | map_hash2tx_t mtx; |
| 128 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 129 | CHECK_TEST_CONDITION(r); |
| 130 | CHECK_EQ(MK_COINS(1), get_balance(m_bob_account, chain, mtx)); |
| 131 | CHECK_EQ(MK_COINS(129) + 2 * rnd_11 + rnd_20 + 3 * rnd_29 - TESTS_DEFAULT_FEE, get_balance(m_alice_account, chain, mtx)); |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | //////// |
| 138 | // class gen_ring_signature_2; |
| 139 | |
| 140 | gen_ring_signature_2::gen_ring_signature_2() |
| 141 | { |
| 142 | REGISTER_CALLBACK("check_balances_1", gen_ring_signature_2::check_balances_1); |
| 143 | REGISTER_CALLBACK("check_balances_2", gen_ring_signature_2::check_balances_2); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Bob has 4 inputs by 13 coins. He sends 4 * 13 coins to Alice, using ring signature with nmix = 3. Each Bob's input |
| 148 | * is used as mix for 3 others. |
| 149 | */ |
| 150 | bool gen_ring_signature_2::generate(std::vector<test_event_entry>& events) const |
| 151 | { |
| 152 | uint64_t ts_start = 1338224400; |
| 153 | |
| 154 | GENERATE_ACCOUNT(miner_account); |
| 155 | |
| 156 | // events |
| 157 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); // 0 |
| 158 | MAKE_ACCOUNT(events, bob_account); // 1 |
| 159 | MAKE_ACCOUNT(events, alice_account); // 2 |
| 160 | MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_account); // 3 |
| 161 | MAKE_NEXT_BLOCK(events, blk_2, blk_1, miner_account); // 4 |
| 162 | MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_account); // 5 |
| 163 | REWIND_BLOCKS(events, blk_3r, blk_3, miner_account); // <N blocks> |
| 164 | MAKE_TX_LIST_START(events, txs_blk_4, miner_account, bob_account, MK_COINS(13), blk_3); // 6 + N |
| 165 | MAKE_TX_LIST(events, txs_blk_4, miner_account, bob_account, MK_COINS(13), blk_3); // 7 + N |
| 166 | MAKE_TX_LIST(events, txs_blk_4, miner_account, bob_account, MK_COINS(13), blk_3); // 8 + N |
| 167 | MAKE_TX_LIST(events, txs_blk_4, miner_account, bob_account, MK_COINS(13), blk_3); // 9 + N |
| 168 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_4, blk_3r, miner_account, txs_blk_4); // 10 + N |
| 169 | DO_CALLBACK(events, "check_balances_1"); // 11 + N |
| 170 | REWIND_BLOCKS(events, blk_4r, blk_4, miner_account); // <N blocks> |
| 171 | MAKE_TX_MIX(events, tx_0, bob_account, alice_account, MK_COINS(52) - TESTS_DEFAULT_FEE, 3, blk_4); // 12 + 2N |
| 172 | MAKE_NEXT_BLOCK_TX1(events, blk_5, blk_4r, miner_account, tx_0); // 13 + 2N |
| 173 | DO_CALLBACK(events, "check_balances_2"); // 14 + 2N |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool gen_ring_signature_2::check_balances_1(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 179 | { |
| 180 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_2::check_balances_1"); |
| 181 | |
| 182 | m_bob_account = boost::get<account_base>(events[1]); |
| 183 | m_alice_account = boost::get<account_base>(events[2]); |
| 184 | |
| 185 | std::vector<block> blocks; |
| 186 | bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 187 | CHECK_TEST_CONDITION(r); |
| 188 | |
| 189 | std::vector<cryptonote::block> chain; |
| 190 | map_hash2tx_t mtx; |
| 191 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 192 | CHECK_TEST_CONDITION(r); |
| 193 | CHECK_EQ(MK_COINS(52), get_balance(m_bob_account, chain, mtx)); |
| 194 | CHECK_EQ(0, get_balance(m_alice_account, chain, mtx)); |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool gen_ring_signature_2::check_balances_2(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 200 | { |
| 201 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_2::check_balances_2"); |
| 202 | |
| 203 | std::vector<block> blocks; |
| 204 | bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 205 | CHECK_TEST_CONDITION(r); |
| 206 | |
| 207 | std::vector<cryptonote::block> chain; |
| 208 | map_hash2tx_t mtx; |
| 209 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 210 | CHECK_TEST_CONDITION(r); |
| 211 | CHECK_EQ(0, get_balance(m_bob_account, chain, mtx)); |
| 212 | CHECK_EQ(MK_COINS(52) - TESTS_DEFAULT_FEE, get_balance(m_alice_account, chain, mtx)); |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | //////// |
| 219 | // class gen_ring_signature_big; |
| 220 | |
| 221 | gen_ring_signature_big::gen_ring_signature_big() |
| 222 | : m_test_size(100) |
| 223 | , m_tx_amount(MK_COINS(29)) |
| 224 | { |
| 225 | REGISTER_CALLBACK("check_balances_1", gen_ring_signature_big::check_balances_1); |
| 226 | REGISTER_CALLBACK("check_balances_2", gen_ring_signature_big::check_balances_2); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Check ring signature with m_test_size-1 sources. |
| 231 | * - Create 100 accounts. |
| 232 | * - Create 100 blocks, each block contains transaction from the miner to account[i]. |
| 233 | * - Create transaction with ring signature from account[99] to Alice with nmix = 99. |
| 234 | * - Check balances. |
| 235 | */ |
| 236 | bool gen_ring_signature_big::generate(std::vector<test_event_entry>& events) const |
| 237 | { |
| 238 | std::vector<account_base> accounts(m_test_size); |
| 239 | std::vector<block> blocks; |
| 240 | blocks.reserve(m_test_size + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW); |
| 241 | |
| 242 | uint64_t ts_start = 1338224400; |
| 243 | GENERATE_ACCOUNT(miner_account); |
| 244 | |
| 245 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); |
| 246 | |
| 247 | for (size_t i = 0; i < m_test_size; ++i) |
| 248 | { |
| 249 | MAKE_ACCOUNT(events, an_account); |
| 250 | accounts[i] = an_account; |
| 251 | } |
| 252 | MAKE_ACCOUNT(events, alice_account); |
| 253 | |
| 254 | size_t blk_0r_idx = events.size(); |
| 255 | REWIND_BLOCKS(events, blk_0r, blk_0, miner_account); |
| 256 | blocks.push_back(blk_0); |
| 257 | for (size_t i = blk_0r_idx; i < events.size(); ++i) |
| 258 | { |
| 259 | blocks.push_back(boost::get<block>(events[i])); |
| 260 | } |
| 261 | |
| 262 | for (size_t i = 0; i < m_test_size; ++i) |
| 263 | { |
| 264 | block blk_with_unlocked_out = blocks[blocks.size() - 1 - CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW]; |
| 265 | MAKE_TX_LIST_START(events, txs_blk_i, miner_account, accounts[i], m_tx_amount, blk_with_unlocked_out); |
| 266 | for (size_t j = 0; j <= i; ++j) |
| 267 | { |
| 268 | MAKE_TX_LIST(events, txs_blk_i, miner_account, accounts[i], TESTS_DEFAULT_FEE, blk_with_unlocked_out); |
| 269 | } |
| 270 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_i, blocks.back(), miner_account, txs_blk_i); |
| 271 | blocks.push_back(blk_i); |
| 272 | |
| 273 | std::vector<cryptonote::block> chain; |
| 274 | map_hash2tx_t mtx; |
| 275 | bool r = find_block_chain(events, chain, mtx, get_block_hash(blk_i)); |
| 276 | CHECK_AND_NO_ASSERT_MES(r, false, "failed to call find_block_chain"); |
| 277 | std::cout << i << ": " << get_balance(accounts[i], chain, mtx) << std::endl; |
| 278 | } |
| 279 | |
| 280 | DO_CALLBACK(events, "check_balances_1"); |
| 281 | MAKE_TX_MIX(events, tx_0, accounts[0], alice_account, m_tx_amount, m_test_size - 1, blocks.back()); |
| 282 | MAKE_NEXT_BLOCK_TX1(events, blk_1, blocks.back(), miner_account, tx_0); |
| 283 | DO_CALLBACK(events, "check_balances_2"); |
| 284 | |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool gen_ring_signature_big::check_balances_1(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 289 | { |
| 290 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_big::check_balances_1"); |
| 291 | |
| 292 | m_bob_account = boost::get<account_base>(events[1]); |
| 293 | m_alice_account = boost::get<account_base>(events[1 + m_test_size]); |
| 294 | |
| 295 | std::vector<block> blocks; |
| 296 | bool r = c.get_blocks(0, 2 * m_test_size + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 297 | CHECK_TEST_CONDITION(r); |
| 298 | |
| 299 | std::vector<cryptonote::block> chain; |
| 300 | map_hash2tx_t mtx; |
| 301 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 302 | CHECK_TEST_CONDITION(r); |
| 303 | CHECK_EQ(m_tx_amount + TESTS_DEFAULT_FEE, get_balance(m_bob_account, chain, mtx)); |
| 304 | CHECK_EQ(0, get_balance(m_alice_account, chain, mtx)); |
| 305 | |
| 306 | for (size_t i = 2; i < 1 + m_test_size; ++i) |
| 307 | { |
| 308 | const account_base& an_account = boost::get<account_base>(events[i]); |
| 309 | uint64_t balance = m_tx_amount + TESTS_DEFAULT_FEE * i; |
| 310 | CHECK_EQ(balance, get_balance(an_account, chain, mtx)); |
| 311 | } |
| 312 | |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | bool gen_ring_signature_big::check_balances_2(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 317 | { |
| 318 | DEFINE_TESTS_ERROR_CONTEXT("gen_ring_signature_big::check_balances_2"); |
| 319 | |
| 320 | std::vector<block> blocks; |
| 321 | bool r = c.get_blocks(0, 2 * m_test_size + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks); |
| 322 | CHECK_TEST_CONDITION(r); |
| 323 | |
| 324 | std::vector<cryptonote::block> chain; |
| 325 | map_hash2tx_t mtx; |
| 326 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 327 | CHECK_TEST_CONDITION(r); |
| 328 | CHECK_EQ(0, get_balance(m_bob_account, chain, mtx)); |
| 329 | CHECK_EQ(m_tx_amount, get_balance(m_alice_account, chain, mtx)); |
| 330 | |
| 331 | for (size_t i = 2; i < 1 + m_test_size; ++i) |
| 332 | { |
| 333 | const account_base& an_account = boost::get<account_base>(events[i]); |
| 334 | uint64_t balance = m_tx_amount + TESTS_DEFAULT_FEE * i; |
| 335 | CHECK_EQ(balance, get_balance(an_account, chain, mtx)); |
| 336 | } |
| 337 | |
| 338 | std::vector<size_t> tx_outs; |
| 339 | uint64_t transfered; |
| 340 | const transaction& tx = boost::get<transaction>(events[events.size() - 3]); |
| 341 | lookup_acc_outs(m_alice_account.get_keys(), boost::get<transaction>(events[events.size() - 3]), get_tx_pub_key_from_extra(tx), get_additional_tx_pub_keys_from_extra(tx), tx_outs, transfered); |
| 342 | CHECK_EQ(m_tx_amount, transfered); |
| 343 | |
| 344 | return true; |
| 345 | } |