| 1 | // Copyright (c) 2017-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 "cryptonote_basic/cryptonote_basic.h" |
| 33 | #include "multisig/multisig.h" |
| 34 | #include "multisig/multisig_tx_builder_ringct.h" |
| 35 | #include "common/apply_permutation.h" |
| 36 | #include "chaingen.h" |
| 37 | #include "multisig.h" |
| 38 | |
| 39 | #include "common/apply_permutation.h" |
| 40 | #include "crypto/crypto.h" |
| 41 | #include "cryptonote_basic/cryptonote_basic.h" |
| 42 | #include "device/device.hpp" |
| 43 | #include "multisig/multisig.h" |
| 44 | #include "multisig/multisig_account.h" |
| 45 | #include "multisig/multisig_kex_msg.h" |
| 46 | #include "ringct/rctOps.h" |
| 47 | #include "ringct/rctSigs.h" |
| 48 | |
| 49 | using namespace epee; |
| 50 | using namespace crypto; |
| 51 | using namespace cryptonote; |
| 52 | using namespace multisig; |
| 53 | |
| 54 | //#define NO_MULTISIG |
| 55 | |
| 56 | static bool make_multisig_accounts(std::vector<cryptonote::account_base> &accounts, const uint32_t threshold) |
| 57 | { |
| 58 | CHECK_AND_ASSERT_MES(accounts.size() > 0, false, "Invalid multisig scheme"); |
| 59 | |
| 60 | std::vector<multisig_account> multisig_accounts; |
| 61 | std::vector<crypto::public_key> signers; |
| 62 | std::vector<multisig_kex_msg> round_msgs; |
| 63 | multisig_accounts.reserve(accounts.size()); |
| 64 | signers.reserve(accounts.size()); |
| 65 | round_msgs.reserve(accounts.size()); |
| 66 | |
| 67 | // create multisig accounts |
| 68 | for (std::size_t account_index{0}; account_index < accounts.size(); ++account_index) |
| 69 | { |
| 70 | // create account and collect signer |
| 71 | multisig_accounts.emplace_back( |
| 72 | multisig_account{ |
| 73 | get_multisig_blinded_secret_key(accounts[account_index].get_keys().m_spend_secret_key), |
| 74 | get_multisig_blinded_secret_key(accounts[account_index].get_keys().m_view_secret_key) |
| 75 | } |
| 76 | ); |
| 77 | |
| 78 | signers.emplace_back(multisig_accounts.back().get_base_pubkey()); |
| 79 | |
| 80 | // collect account's first kex msg |
| 81 | round_msgs.emplace_back(multisig_accounts.back().get_next_kex_round_msg()); |
| 82 | } |
| 83 | |
| 84 | // initialize accounts and collect kex messages for the next round |
| 85 | std::vector<multisig_kex_msg> temp_round_msgs(multisig_accounts.size()); |
| 86 | for (std::size_t account_index{0}; account_index < accounts.size(); ++account_index) |
| 87 | { |
| 88 | multisig_accounts[account_index].initialize_kex(threshold, signers, round_msgs); |
| 89 | temp_round_msgs[account_index] = multisig_accounts[account_index].get_next_kex_round_msg(); |
| 90 | } |
| 91 | |
| 92 | // perform key exchange rounds |
| 93 | while (!multisig_accounts[0].multisig_is_ready()) |
| 94 | { |
| 95 | round_msgs = temp_round_msgs; |
| 96 | |
| 97 | for (std::size_t account_index{0}; account_index < multisig_accounts.size(); ++account_index) |
| 98 | { |
| 99 | multisig_accounts[account_index].kex_update(round_msgs); |
| 100 | temp_round_msgs[account_index] = multisig_accounts[account_index].get_next_kex_round_msg(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // update accounts post key exchange |
| 105 | for (std::size_t account_index{0}; account_index < accounts.size(); ++account_index) |
| 106 | { |
| 107 | accounts[account_index].make_multisig(multisig_accounts[account_index].get_common_privkey(), |
| 108 | multisig_accounts[account_index].get_base_privkey(), |
| 109 | multisig_accounts[account_index].get_multisig_pubkey(), |
| 110 | multisig_accounts[account_index].get_multisig_privkeys()); |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | //---------------------------------------------------------------------------------------------------------------------- |
| 117 | // Tests |
| 118 | |
| 119 | bool gen_multisig_tx_validation_base::generate_with(std::vector<test_event_entry>& events, |
| 120 | size_t inputs, size_t mixin, uint64_t amount_paid, bool valid, |
| 121 | size_t threshold, size_t total, size_t creator, std::vector<size_t> other_signers, |
| 122 | const std::function<void(std::vector<tx_source_entry> &sources, std::vector<tx_destination_entry> &destinations)> &pre_tx, |
| 123 | const std::function<void(transaction &tx)> &post_tx) const |
| 124 | { |
| 125 | uint64_t ts_start = 1338224400; |
| 126 | bool r; |
| 127 | |
| 128 | CHECK_AND_ASSERT_MES(total >= 2, false, "Bad scheme"); |
| 129 | CHECK_AND_ASSERT_MES(threshold <= total, false, "Bad scheme"); |
| 130 | CHECK_AND_ASSERT_MES(inputs >= 1 && inputs <= 8, false, "Inputs should between 1 and 8"); |
| 131 | |
| 132 | // given as 1 based for clarity |
| 133 | --creator; |
| 134 | for (size_t &signer: other_signers) |
| 135 | --signer; |
| 136 | |
| 137 | CHECK_AND_ASSERT_MES(creator < total, false, "invalid creator"); |
| 138 | for (size_t signer: other_signers) |
| 139 | CHECK_AND_ASSERT_MES(signer < total, false, "invalid signer"); |
| 140 | |
| 141 | GENERATE_MULTISIG_ACCOUNT(miner_account, threshold, total); |
| 142 | |
| 143 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account[creator], ts_start); |
| 144 | |
| 145 | // create 16 miner accounts, and have them mine the next 16 blocks |
| 146 | // they will have a coinbase with a single out that's pseudo rct |
| 147 | constexpr size_t n_coinbases = 16; |
| 148 | cryptonote::account_base miner_accounts[n_coinbases]; |
| 149 | const cryptonote::block *prev_block = &blk_0; |
| 150 | cryptonote::block blocks[n_coinbases]; |
| 151 | for (size_t n = 0; n < n_coinbases; ++n) { |
| 152 | // the first block goes to the multisig account |
| 153 | miner_accounts[n].generate(); |
| 154 | account_base &account = n < inputs ? miner_account[creator] : miner_accounts[n]; |
| 155 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blocks[n], *prev_block, account, |
| 156 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp | test_generator::bf_hf_version | test_generator::bf_max_outs, |
| 157 | HF_VERSION_BULLETPROOF_PLUS, HF_VERSION_BULLETPROOF_PLUS, prev_block->timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 158 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 1, 4), |
| 159 | false, "Failed to generate block"); |
| 160 | events.push_back(blocks[n]); |
| 161 | prev_block = blocks + n; |
| 162 | } |
| 163 | |
| 164 | // rewind |
| 165 | cryptonote::block blk_r, blk_last; |
| 166 | { |
| 167 | blk_last = blocks[n_coinbases - 1]; |
| 168 | for (size_t i = 0; i < CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; ++i) |
| 169 | { |
| 170 | cryptonote::block blk; |
| 171 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blk, blk_last, miner_accounts[0], |
| 172 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp | test_generator::bf_hf_version | test_generator::bf_max_outs, |
| 173 | HF_VERSION_BULLETPROOF_PLUS, HF_VERSION_BULLETPROOF_PLUS, blk_last.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 174 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 1, 4), |
| 175 | false, "Failed to generate block"); |
| 176 | events.push_back(blk); |
| 177 | blk_last = blk; |
| 178 | } |
| 179 | blk_r = blk_last; |
| 180 | } |
| 181 | |
| 182 | cryptonote::keypair in_ephemeral; |
| 183 | crypto::public_key tx_pub_key[n_coinbases]; |
| 184 | crypto::public_key output_pub_key[n_coinbases]; |
| 185 | for (size_t n = 0; n < n_coinbases; ++n) |
| 186 | { |
| 187 | tx_pub_key[n] = get_tx_pub_key_from_extra(blocks[n].miner_tx); |
| 188 | MDEBUG("tx_pub_key: " << tx_pub_key); |
| 189 | cryptonote::get_output_public_key(blocks[n].miner_tx.vout[0], output_pub_key[n]); |
| 190 | MDEBUG("output_pub_key: " << output_pub_key); |
| 191 | } |
| 192 | |
| 193 | std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; |
| 194 | subaddresses[miner_account[0].get_keys().m_account_address.m_spend_public_key] = {0,0}; |
| 195 | |
| 196 | // create k/L/R/ki for that output we're going to spend |
| 197 | std::vector<std::vector<std::vector<crypto::secret_key>>> account_k(total); |
| 198 | std::vector<std::vector<std::vector<crypto::public_key>>> account_L(total); |
| 199 | std::vector<std::vector<std::vector<crypto::public_key>>> account_R(total); |
| 200 | std::vector<std::vector<std::vector<crypto::key_image>>> account_ki(total); |
| 201 | std::vector<crypto::public_key> additional_tx_keys; |
| 202 | for (size_t msidx = 0; msidx < total; ++msidx) |
| 203 | { |
| 204 | CHECK_AND_ASSERT_MES(miner_account[msidx].get_keys().m_account_address.m_spend_public_key == miner_account[0].get_keys().m_account_address.m_spend_public_key, |
| 205 | false, "Mismatched spend public keys"); |
| 206 | |
| 207 | size_t nlr = threshold < total ? threshold - 1 : 1; |
| 208 | nlr *= multisig::signing::kAlphaComponents; |
| 209 | account_k[msidx].resize(inputs); |
| 210 | account_L[msidx].resize(inputs); |
| 211 | account_R[msidx].resize(inputs); |
| 212 | account_ki[msidx].resize(inputs); |
| 213 | for (size_t tdidx = 0; tdidx < inputs; ++tdidx) |
| 214 | { |
| 215 | account_L[msidx][tdidx].resize(nlr); |
| 216 | account_R[msidx][tdidx].resize(nlr); |
| 217 | for (size_t n = 0; n < nlr; ++n) |
| 218 | { |
| 219 | account_k[msidx][tdidx].push_back(rct::rct2sk(rct::skGen())); |
| 220 | multisig::generate_multisig_LR(output_pub_key[tdidx], account_k[msidx][tdidx][n], account_L[msidx][tdidx][n], account_R[msidx][tdidx][n]); |
| 221 | } |
| 222 | size_t num_account_partial_ki = miner_account[msidx].get_multisig_keys().size(); |
| 223 | account_ki[msidx][tdidx].resize(num_account_partial_ki); |
| 224 | for (size_t kiidx = 0; kiidx < num_account_partial_ki; ++kiidx) |
| 225 | { |
| 226 | r = multisig::generate_multisig_key_image(miner_account[msidx].get_keys(), kiidx, output_pub_key[tdidx], account_ki[msidx][tdidx][kiidx]); |
| 227 | CHECK_AND_ASSERT_MES(r, false, "Failed to generate multisig export key image"); |
| 228 | } |
| 229 | MDEBUG("Party " << msidx << ":"); |
| 230 | MDEBUG("spend: sec " << crypto::secret_key_explicit_print_ref{miner_account[msidx].get_keys().m_spend_secret_key} << ", pub " << miner_account[msidx].get_keys().m_account_address.m_spend_public_key); |
| 231 | MDEBUG("view: sec " << crypto::secret_key_explicit_print_ref{miner_account[msidx].get_keys().m_view_secret_key} << ", pub " << miner_account[msidx].get_keys().m_account_address.m_view_public_key); |
| 232 | for (const auto &k: miner_account[msidx].get_multisig_keys()) |
| 233 | MDEBUG("msk: " << crypto::secret_key_explicit_print_ref{k}); |
| 234 | for (size_t n = 0; n < account_k[msidx][tdidx].size(); ++n) |
| 235 | { |
| 236 | MDEBUG("k: " << crypto::secret_key_explicit_print_ref{account_k[msidx][tdidx][n]}); |
| 237 | MDEBUG("L: " << account_L[msidx][tdidx][n]); |
| 238 | MDEBUG("R: " << account_R[msidx][tdidx][n]); |
| 239 | } |
| 240 | for (const auto &ki: account_ki[msidx][tdidx]) |
| 241 | MDEBUG("ki: " << ki); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // create kLRki |
| 246 | std::vector<rct::multisig_kLRki> kLRkis; |
| 247 | std::unordered_set<crypto::public_key> used_L; |
| 248 | for (size_t tdidx = 0; tdidx < inputs; ++tdidx) |
| 249 | { |
| 250 | kLRkis.push_back(rct::multisig_kLRki()); |
| 251 | rct::multisig_kLRki &kLRki = kLRkis.back(); |
| 252 | std::vector<crypto::key_image> pkis; |
| 253 | for (size_t msidx = 0; msidx < total; ++msidx) |
| 254 | for (size_t n = 0; n < account_ki[msidx][tdidx].size(); ++n) |
| 255 | pkis.push_back(account_ki[msidx][tdidx][n]); |
| 256 | r = multisig::generate_multisig_composite_key_image(miner_account[0].get_keys(), subaddresses, output_pub_key[tdidx], tx_pub_key[tdidx], additional_tx_keys, 0, pkis, (crypto::key_image&)kLRki.ki); |
| 257 | CHECK_AND_ASSERT_MES(r, false, "Failed to generate composite key image"); |
| 258 | MDEBUG("composite ki: " << kLRki.ki); |
| 259 | for (size_t n = 1; n < total; ++n) |
| 260 | { |
| 261 | rct::key ki; |
| 262 | r = multisig::generate_multisig_composite_key_image(miner_account[n].get_keys(), subaddresses, output_pub_key[tdidx], tx_pub_key[tdidx], additional_tx_keys, 0, pkis, (crypto::key_image&)ki); |
| 263 | CHECK_AND_ASSERT_MES(r, false, "Failed to generate composite key image"); |
| 264 | CHECK_AND_ASSERT_MES(kLRki.ki == ki, false, "Composite key images do not match"); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // prepare a tx: we have 8 outputs, all from coinbase, so "fake" rct - use 2 |
| 269 | std::vector<tx_source_entry> sources; |
| 270 | for (size_t n = 0; n < inputs; ++n) |
| 271 | { |
| 272 | sources.resize(sources.size() + 1); |
| 273 | tx_source_entry& src = sources.back(); |
| 274 | |
| 275 | src.real_output = n; |
| 276 | src.amount = blocks[n].miner_tx.vout[0].amount; |
| 277 | src.real_out_tx_key = tx_pub_key[n]; |
| 278 | src.real_output_in_tx_index = 0; |
| 279 | src.mask = rct::identity(); |
| 280 | src.rct = true; |
| 281 | src.multisig_kLRki = kLRkis[n]; |
| 282 | |
| 283 | for (size_t m = 0; m <= mixin; ++m) |
| 284 | { |
| 285 | rct::ctkey ctkey; |
| 286 | crypto::public_key output_public_key; |
| 287 | cryptonote::get_output_public_key(blocks[m].miner_tx.vout[0], output_public_key); |
| 288 | ctkey.dest = rct::pk2rct(output_public_key); |
| 289 | MDEBUG("using " << (m == n ? "real" : "fake") << " input " << ctkey.dest); |
| 290 | ctkey.mask = rct::commit(blocks[m].miner_tx.vout[0].amount, rct::identity()); // since those are coinbases, the masks are known |
| 291 | src.outputs.push_back(std::make_pair(m, ctkey)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | //fill outputs entry |
| 296 | tx_destination_entry td; |
| 297 | td.addr = miner_account[creator].get_keys().m_account_address; |
| 298 | td.amount = amount_paid; |
| 299 | std::vector<tx_destination_entry> destinations; //tx need two outputs since HF_VERSION_MIN_2_OUTPUTS |
| 300 | destinations.push_back(td); |
| 301 | td.amount = 0; |
| 302 | destinations.push_back(td); |
| 303 | |
| 304 | if (pre_tx) |
| 305 | pre_tx(sources, destinations); |
| 306 | |
| 307 | transaction tx; |
| 308 | crypto::secret_key tx_key; |
| 309 | std::vector<crypto::secret_key> additional_tx_secret_keys; |
| 310 | crypto::secret_key multisig_tx_key_entropy; |
| 311 | auto sources_copy = sources; |
| 312 | multisig::signing::tx_builder_ringct_t tx_builder; |
| 313 | CHECK_AND_ASSERT_MES(tx_builder.init(miner_account[creator].get_keys(), {}, 0, {0}, sources, destinations, {}, {rct::RangeProofPaddedBulletproof, 4}, true, false, tx_key, additional_tx_secret_keys, multisig_tx_key_entropy, tx), false, "error: multisig::signing::tx_builder_ringct_t::init"); |
| 314 | |
| 315 | // work out the permutation done on sources |
| 316 | std::vector<size_t> ins_order; |
| 317 | for (size_t n = 0; n < sources.size(); ++n) |
| 318 | { |
| 319 | for (size_t idx = 0; idx < sources_copy.size(); ++idx) |
| 320 | { |
| 321 | CHECK_AND_ASSERT_MES((size_t)sources_copy[idx].real_output < sources_copy[idx].outputs.size(), |
| 322 | false, "Invalid real_output"); |
| 323 | if (sources_copy[idx].outputs[sources_copy[idx].real_output].second.dest == sources[n].outputs[sources[n].real_output].second.dest) |
| 324 | ins_order.push_back(idx); |
| 325 | } |
| 326 | } |
| 327 | CHECK_AND_ASSERT_MES(ins_order.size() == sources.size(), false, "Failed to work out sources permutation"); |
| 328 | |
| 329 | struct { |
| 330 | rct::keyM total_alpha_G; |
| 331 | rct::keyM total_alpha_H; |
| 332 | rct::keyV c_0; |
| 333 | rct::keyV s; |
| 334 | } sig; |
| 335 | { |
| 336 | used_L.clear(); |
| 337 | sig.total_alpha_G.resize(sources.size(), rct::keyV(multisig::signing::kAlphaComponents, rct::identity())); |
| 338 | sig.total_alpha_H.resize(sources.size(), rct::keyV(multisig::signing::kAlphaComponents, rct::identity())); |
| 339 | sig.c_0.resize(sources.size()); |
| 340 | sig.s.resize(sources.size()); |
| 341 | for (std::size_t i = 0; i < sources.size(); ++i) { |
| 342 | rct::keyV alpha(multisig::signing::kAlphaComponents); |
| 343 | for (std::size_t m = 0; m < multisig::signing::kAlphaComponents; ++m) { |
| 344 | alpha[m] = rct::sk2rct(account_k[creator][ins_order[i]][m]); |
| 345 | sig.total_alpha_G[i][m] = rct::pk2rct(account_L[creator][ins_order[i]][m]); |
| 346 | sig.total_alpha_H[i][m] = rct::pk2rct(account_R[creator][ins_order[i]][m]); |
| 347 | for (size_t j = 0; j < total; ++j) { |
| 348 | if (j == creator) |
| 349 | continue; |
| 350 | if (std::find(other_signers.begin(), other_signers.end(), j) == other_signers.end()) |
| 351 | continue; |
| 352 | for (std::size_t n = 0; n < account_L[j][ins_order[i]].size(); ++n) { |
| 353 | if (used_L.find(account_L[j][ins_order[i]][n]) == used_L.end()) { |
| 354 | used_L.insert(account_L[j][ins_order[i]][n]); |
| 355 | rct::addKeys(sig.total_alpha_G[i][m], sig.total_alpha_G[i][m], rct::pk2rct(account_L[j][ins_order[i]][n])); |
| 356 | rct::addKeys(sig.total_alpha_H[i][m], sig.total_alpha_H[i][m], rct::pk2rct(account_R[j][ins_order[i]][n])); |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | CHECK_AND_ASSERT_MES(tx_builder.first_partial_sign(i, sig.total_alpha_G[i], sig.total_alpha_H[i], alpha, sig.c_0[i], sig.s[i]), false, "error: multisig::signing::tx_builder_ringct_t::first_partial_sign"); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // sign |
| 367 | std::unordered_set<crypto::secret_key> used_keys; |
| 368 | const std::vector<crypto::secret_key> &msk0 = miner_account[creator].get_multisig_keys(); |
| 369 | for (const auto &sk: msk0) |
| 370 | used_keys.insert(sk); //these were used in 'tx_builder.init() -> tx_builder.first_partial_sign()' |
| 371 | for (size_t signer: other_signers) |
| 372 | { |
| 373 | rct::key skey = rct::zero(); |
| 374 | const std::vector<crypto::secret_key> &msk1 = miner_account[signer].get_multisig_keys(); |
| 375 | for (size_t n = 0; n < msk1.size(); ++n) |
| 376 | { |
| 377 | const crypto::secret_key &sk1 = msk1[n]; |
| 378 | if (used_keys.find(sk1) == used_keys.end()) |
| 379 | { |
| 380 | used_keys.insert(sk1); |
| 381 | sc_add(skey.bytes, skey.bytes, rct::sk2rct(sk1).bytes); |
| 382 | } |
| 383 | } |
| 384 | CHECK_AND_ASSERT_MES(!(skey == rct::zero()), false, "failed to find secret multisig key to sign transaction"); |
| 385 | rct::keyM k(sources.size(), rct::keyV(multisig::signing::kAlphaComponents)); |
| 386 | for (std::size_t i = 0; i < sources.size(); ++i) { |
| 387 | for (std::size_t j = 0; j < multisig::signing::kAlphaComponents; ++j) { |
| 388 | for (std::size_t n = 0; n < account_k[signer][i].size(); ++n) { |
| 389 | crypto::public_key L; |
| 390 | rct::scalarmultBase((rct::key&)L, rct::sk2rct(account_k[signer][i][n])); |
| 391 | if (used_L.find(L) != used_L.end()) { |
| 392 | k[i][j] = rct::sk2rct(account_k[signer][i][n]); |
| 393 | account_k[signer][i][n] = rct::rct2sk(rct::zero()); //demo: always clear nonces from long-term storage after use |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | CHECK_AND_ASSERT_MES(!(k[i][j] == rct::zero()), false, "failed to find k to sign transaction"); |
| 398 | } |
| 399 | } |
| 400 | tools::apply_permutation(ins_order, k); |
| 401 | multisig::signing::tx_builder_ringct_t signer_tx_builder; |
| 402 | CHECK_AND_ASSERT_MES(signer_tx_builder.init(miner_account[signer].get_keys(), {}, 0, {0}, sources, destinations, {}, {rct::RangeProofPaddedBulletproof, 4}, true, true, tx_key, additional_tx_secret_keys, multisig_tx_key_entropy, tx), false, "error: multisig::signing::tx_builder_ringct_t::init"); |
| 403 | |
| 404 | MDEBUG("signing with k size " << k.size()); |
| 405 | for (size_t n = 0; n < multisig::signing::kAlphaComponents; ++n) |
| 406 | MDEBUG("signing with k " << k.back()[n]); |
| 407 | MDEBUG("signing with sk " << skey); |
| 408 | for (const auto &sk: used_keys) |
| 409 | MDEBUG(" created with sk " << crypto::secret_key_explicit_print_ref{sk}); |
| 410 | CHECK_AND_ASSERT_MES(signer_tx_builder.next_partial_sign(sig.total_alpha_G, sig.total_alpha_H, k, skey, sig.c_0, sig.s), false, "error: multisig::signing::tx_builder_ringct_t::next_partial_sign"); |
| 411 | |
| 412 | // in round-robin signing, the last signer finalizes the tx |
| 413 | if (signer == other_signers.back()) |
| 414 | CHECK_AND_ASSERT_MES(signer_tx_builder.finalize_tx(sources, sig.c_0, sig.s, tx), false, "error: multisig::signing::tx_builder_ringct_t::finalize_tx"); |
| 415 | } |
| 416 | |
| 417 | // verify this tx is really to the expected address |
| 418 | const crypto::public_key tx_pub_key2 = get_tx_pub_key_from_extra(tx, 0); |
| 419 | crypto::key_derivation derivation; |
| 420 | r = crypto::generate_key_derivation(tx_pub_key2, miner_account[creator].get_keys().m_view_secret_key, derivation); |
| 421 | CHECK_AND_ASSERT_MES(r, false, "Failed to generate derivation"); |
| 422 | uint64_t n_outs = 0, amount = 0; |
| 423 | std::vector<crypto::key_derivation> additional_derivations; |
| 424 | crypto::public_key output_public_key; |
| 425 | for (size_t n = 0; n < tx.vout.size(); ++n) |
| 426 | { |
| 427 | CHECK_AND_ASSERT_MES(typeid(txout_to_tagged_key) == tx.vout[n].target.type(), false, "Unexpected tx out type"); |
| 428 | cryptonote::get_output_public_key(tx.vout[n], output_public_key); |
| 429 | if (is_out_to_acc_precomp(subaddresses, output_public_key, derivation, additional_derivations, n, hw::get_device(("default")))) |
| 430 | { |
| 431 | ++n_outs; |
| 432 | CHECK_AND_ASSERT_MES(tx.vout[n].amount == 0, false, "Destination amount is not zero"); |
| 433 | rct::key Ctmp; |
| 434 | crypto::secret_key scalar1; |
| 435 | crypto::derivation_to_scalar(derivation, n, scalar1); |
| 436 | rct::ecdhTuple ecdh_info = tx.rct_signatures.ecdhInfo[n]; |
| 437 | rct::ecdhDecode(ecdh_info, rct::sk2rct(scalar1), tx.rct_signatures.type == rct::RCTTypeBulletproof2 || tx.rct_signatures.type == rct::RCTTypeCLSAG || tx.rct_signatures.type == rct::RCTTypeBulletproofPlus); |
| 438 | rct::key C = tx.rct_signatures.outPk[n].mask; |
| 439 | rct::addKeys2(Ctmp, ecdh_info.mask, ecdh_info.amount, rct::H); |
| 440 | CHECK_AND_ASSERT_MES(rct::equalKeys(C, Ctmp), false, "Failed to decode amount"); |
| 441 | amount += rct::h2d(ecdh_info.amount); |
| 442 | } |
| 443 | } |
| 444 | CHECK_AND_ASSERT_MES(n_outs == 2, false, "Not exactly 2 outputs were received"); |
| 445 | CHECK_AND_ASSERT_MES(amount == amount_paid, false, "Amount paid was not the expected amount"); |
| 446 | |
| 447 | if (post_tx) |
| 448 | post_tx(tx); |
| 449 | |
| 450 | if (!valid) |
| 451 | DO_CALLBACK(events, "mark_invalid_tx"); |
| 452 | events.push_back(tx); |
| 453 | LOG_PRINT_L0("Test tx: " << obj_to_json_str(tx)); |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | bool gen_multisig_tx_valid_22_1_2::generate(std::vector<test_event_entry>& events) const |
| 459 | { |
| 460 | const size_t mixin = 10; |
| 461 | const uint64_t amount_paid = 10000; |
| 462 | return generate_with(events, 2, mixin, amount_paid, true, 2, 2, 1, {2}, NULL, NULL); |
| 463 | } |
| 464 | |
| 465 | bool gen_multisig_tx_valid_22_1_2_many_inputs::generate(std::vector<test_event_entry>& events) const |
| 466 | { |
| 467 | const size_t mixin = 10; |
| 468 | const uint64_t amount_paid = 10000; |
| 469 | return generate_with(events, 4, mixin, amount_paid, true, 2, 2, 1, {2}, NULL, NULL); |
| 470 | } |
| 471 | |
| 472 | bool gen_multisig_tx_valid_22_2_1::generate(std::vector<test_event_entry>& events) const |
| 473 | { |
| 474 | const size_t mixin = 10; |
| 475 | const uint64_t amount_paid = 10000; |
| 476 | return generate_with(events, 2, mixin, amount_paid, true, 2, 2, 2, {1}, NULL, NULL); |
| 477 | } |
| 478 | |
| 479 | bool gen_multisig_tx_valid_33_1_23::generate(std::vector<test_event_entry>& events) const |
| 480 | { |
| 481 | const size_t mixin = 10; |
| 482 | const uint64_t amount_paid = 10000; |
| 483 | return generate_with(events, 2, mixin, amount_paid, true, 3, 3, 1, {2, 3}, NULL, NULL); |
| 484 | } |
| 485 | |
| 486 | bool gen_multisig_tx_valid_33_3_21::generate(std::vector<test_event_entry>& events) const |
| 487 | { |
| 488 | const size_t mixin = 10; |
| 489 | const uint64_t amount_paid = 10000; |
| 490 | return generate_with(events, 2, mixin, amount_paid, true, 3, 3, 3, {2, 1}, NULL, NULL); |
| 491 | } |
| 492 | |
| 493 | bool gen_multisig_tx_valid_23_1_2::generate(std::vector<test_event_entry>& events) const |
| 494 | { |
| 495 | const size_t mixin = 10; |
| 496 | const uint64_t amount_paid = 10000; |
| 497 | return generate_with(events, 2, mixin, amount_paid, true, 2, 3, 1, {2}, NULL, NULL); |
| 498 | } |
| 499 | |
| 500 | bool gen_multisig_tx_valid_23_1_3::generate(std::vector<test_event_entry>& events) const |
| 501 | { |
| 502 | const size_t mixin = 10; |
| 503 | const uint64_t amount_paid = 10000; |
| 504 | return generate_with(events, 2, mixin, amount_paid, true, 2, 3, 1, {3}, NULL, NULL); |
| 505 | } |
| 506 | |
| 507 | bool gen_multisig_tx_valid_23_2_1::generate(std::vector<test_event_entry>& events) const |
| 508 | { |
| 509 | const size_t mixin = 10; |
| 510 | const uint64_t amount_paid = 10000; |
| 511 | return generate_with(events, 2, mixin, amount_paid, true, 2, 3, 2, {1}, NULL, NULL); |
| 512 | } |
| 513 | |
| 514 | bool gen_multisig_tx_valid_23_2_3::generate(std::vector<test_event_entry>& events) const |
| 515 | { |
| 516 | const size_t mixin = 10; |
| 517 | const uint64_t amount_paid = 10000; |
| 518 | return generate_with(events, 2, mixin, amount_paid, true, 2, 3, 2, {3}, NULL, NULL); |
| 519 | } |
| 520 | |
| 521 | bool gen_multisig_tx_valid_45_1_234::generate(std::vector<test_event_entry>& events) const |
| 522 | { |
| 523 | const size_t mixin = 10; |
| 524 | const uint64_t amount_paid = 10000; |
| 525 | return generate_with(events, 2, mixin, amount_paid, true, 4, 5, 1, {2, 3, 4}, NULL, NULL); |
| 526 | } |
| 527 | |
| 528 | bool gen_multisig_tx_valid_45_4_135_many_inputs::generate(std::vector<test_event_entry>& events) const |
| 529 | { |
| 530 | const size_t mixin = 10; |
| 531 | const uint64_t amount_paid = 10000; |
| 532 | return generate_with(events, 4, mixin, amount_paid, true, 4, 5, 4, {1, 3, 5}, NULL, NULL); |
| 533 | } |
| 534 | |
| 535 | bool gen_multisig_tx_valid_89_3_1245789::generate(std::vector<test_event_entry>& events) const |
| 536 | { |
| 537 | const size_t mixin = 10; |
| 538 | const uint64_t amount_paid = 10000; |
| 539 | return generate_with(events, 2, mixin, amount_paid, true, 8, 9, 3, {1, 2, 4, 5, 7, 8, 9}, NULL, NULL); |
| 540 | } |
| 541 | |
| 542 | bool gen_multisig_tx_valid_24_1_2::generate(std::vector<test_event_entry>& events) const |
| 543 | { |
| 544 | const size_t mixin = 10; |
| 545 | const uint64_t amount_paid = 10000; |
| 546 | return generate_with(events, 2, mixin, amount_paid, true, 2, 4, 1, {2}, NULL, NULL); |
| 547 | } |
| 548 | |
| 549 | bool gen_multisig_tx_valid_24_1_2_many_inputs::generate(std::vector<test_event_entry>& events) const |
| 550 | { |
| 551 | const size_t mixin = 10; |
| 552 | const uint64_t amount_paid = 10000; |
| 553 | return generate_with(events, 4, mixin, amount_paid, true, 2, 4, 1, {2}, NULL, NULL); |
| 554 | } |
| 555 | |
| 556 | bool gen_multisig_tx_valid_25_1_2::generate(std::vector<test_event_entry>& events) const |
| 557 | { |
| 558 | const size_t mixin = 10; |
| 559 | const uint64_t amount_paid = 10000; |
| 560 | return generate_with(events, 2, mixin, amount_paid, true, 2, 5, 1, {2}, NULL, NULL); |
| 561 | } |
| 562 | |
| 563 | bool gen_multisig_tx_valid_25_1_2_many_inputs::generate(std::vector<test_event_entry>& events) const |
| 564 | { |
| 565 | const size_t mixin = 10; |
| 566 | const uint64_t amount_paid = 10000; |
| 567 | return generate_with(events, 4, mixin, amount_paid, true, 2, 5, 1, {2}, NULL, NULL); |
| 568 | } |
| 569 | |
| 570 | bool gen_multisig_tx_valid_48_1_234::generate(std::vector<test_event_entry>& events) const |
| 571 | { |
| 572 | const size_t mixin = 10; |
| 573 | const uint64_t amount_paid = 10000; |
| 574 | return generate_with(events, 2, mixin, amount_paid, true, 4, 8, 1, {2, 3, 4}, NULL, NULL); |
| 575 | } |
| 576 | |
| 577 | bool gen_multisig_tx_valid_48_1_234_many_inputs::generate(std::vector<test_event_entry>& events) const |
| 578 | { |
| 579 | const size_t mixin = 10; |
| 580 | const uint64_t amount_paid = 10000; |
| 581 | return generate_with(events, 4, mixin, amount_paid, true, 4, 8, 1, {2, 3, 4}, NULL, NULL); |
| 582 | } |
| 583 | |
| 584 | bool gen_multisig_tx_invalid_22_1__no_threshold::generate(std::vector<test_event_entry>& events) const |
| 585 | { |
| 586 | const size_t mixin = 10; |
| 587 | const uint64_t amount_paid = 10000; |
| 588 | return generate_with(events, 2, mixin, amount_paid, false, 2, 2, 1, {}, NULL, NULL); |
| 589 | } |
| 590 | |
| 591 | bool gen_multisig_tx_invalid_33_1__no_threshold::generate(std::vector<test_event_entry>& events) const |
| 592 | { |
| 593 | const size_t mixin = 10; |
| 594 | const uint64_t amount_paid = 10000; |
| 595 | return generate_with(events, 2, mixin, amount_paid, false, 3, 3, 1, {}, NULL, NULL); |
| 596 | } |
| 597 | |
| 598 | bool gen_multisig_tx_invalid_33_1_2_no_threshold::generate(std::vector<test_event_entry>& events) const |
| 599 | { |
| 600 | const size_t mixin = 10; |
| 601 | const uint64_t amount_paid = 10000; |
| 602 | return generate_with(events, 2, mixin, amount_paid, false, 3, 3, 1, {2}, NULL, NULL); |
| 603 | } |
| 604 | |
| 605 | bool gen_multisig_tx_invalid_33_1_3_no_threshold::generate(std::vector<test_event_entry>& events) const |
| 606 | { |
| 607 | const size_t mixin = 10; |
| 608 | const uint64_t amount_paid = 10000; |
| 609 | return generate_with(events, 2, mixin, amount_paid, false, 3, 3, 1, {3}, NULL, NULL); |
| 610 | } |
| 611 | |
| 612 | bool gen_multisig_tx_invalid_23_1__no_threshold::generate(std::vector<test_event_entry>& events) const |
| 613 | { |
| 614 | const size_t mixin = 10; |
| 615 | const uint64_t amount_paid = 10000; |
| 616 | return generate_with(events, 2, mixin, amount_paid, false, 2, 3, 1, {}, NULL, NULL); |
| 617 | } |
| 618 | |
| 619 | bool gen_multisig_tx_invalid_45_5_23_no_threshold::generate(std::vector<test_event_entry>& events) const |
| 620 | { |
| 621 | const size_t mixin = 10; |
| 622 | const uint64_t amount_paid = 10000; |
| 623 | return generate_with(events, 2, mixin, amount_paid, false, 4, 5, 5, {2, 3}, NULL, NULL); |
| 624 | } |
| 625 | |
| 626 | bool gen_multisig_tx_invalid_24_1_no_signers::generate(std::vector<test_event_entry>& events) const |
| 627 | { |
| 628 | const size_t mixin = 10; |
| 629 | const uint64_t amount_paid = 10000; |
| 630 | return generate_with(events, 2, mixin, amount_paid, false, 2, 4, 1, {}, NULL, NULL); |
| 631 | } |
| 632 | |
| 633 | bool gen_multisig_tx_invalid_25_1_no_signers::generate(std::vector<test_event_entry>& events) const |
| 634 | { |
| 635 | const size_t mixin = 10; |
| 636 | const uint64_t amount_paid = 10000; |
| 637 | return generate_with(events, 2, mixin, amount_paid, false, 2, 5, 1, {}, NULL, NULL); |
| 638 | } |
| 639 | |
| 640 | bool gen_multisig_tx_invalid_48_1_no_signers::generate(std::vector<test_event_entry>& events) const |
| 641 | { |
| 642 | const size_t mixin = 10; |
| 643 | const uint64_t amount_paid = 10000; |
| 644 | return generate_with(events, 2, mixin, amount_paid, false, 4, 8, 1, {}, NULL, NULL); |
| 645 | } |
| 646 | |
| 647 | bool gen_multisig_tx_invalid_48_1_23_no_threshold::generate(std::vector<test_event_entry>& events) const |
| 648 | { |
| 649 | const size_t mixin = 10; |
| 650 | const uint64_t amount_paid = 10000; |
| 651 | return generate_with(events, 2, mixin, amount_paid, false, 4, 8, 1, {2, 3}, NULL, NULL); |
| 652 | } |