| 1 | // Copyright (c) 2021, 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 "multisig_tx_builder_ringct.h" |
| 30 | |
| 31 | #include "int-util.h" |
| 32 | #include "memwipe.h" |
| 33 | |
| 34 | #include "cryptonote_basic/cryptonote_basic.h" |
| 35 | #include "cryptonote_basic/account.h" |
| 36 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 37 | #include "cryptonote_config.h" |
| 38 | #include "cryptonote_core/cryptonote_tx_utils.h" |
| 39 | #include "device/device.hpp" |
| 40 | #include "multisig_clsag_context.h" |
| 41 | #include "ringct/bulletproofs.h" |
| 42 | #include "ringct/bulletproofs_plus.h" |
| 43 | #include "ringct/rctSigs.h" |
| 44 | |
| 45 | #include <boost/multiprecision/cpp_int.hpp> |
| 46 | |
| 47 | #include <algorithm> |
| 48 | #include <cstring> |
| 49 | #include <limits> |
| 50 | #include <set> |
| 51 | #include <string> |
| 52 | #include <unordered_map> |
| 53 | #include <unordered_set> |
| 54 | #include <vector> |
| 55 | |
| 56 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 57 | #define MONERO_DEFAULT_LOG_CATEGORY "multisig" |
| 58 | |
| 59 | namespace multisig { |
| 60 | |
| 61 | namespace signing { |
| 62 | //---------------------------------------------------------------------------------------------------------------------- |
| 63 | //---------------------------------------------------------------------------------------------------------------------- |
| 64 | bool view_tag_required(const int bp_version) |
| 65 | { |
| 66 | // view tags were introduced at the same time as BP+, so they are needed after BP+ (v4 and later) |
| 67 | if (bp_version <= 3) |
| 68 | return false; |
| 69 | else |
| 70 | return true; |
| 71 | } |
| 72 | //---------------------------------------------------------------------------------------------------------------------- |
| 73 | //---------------------------------------------------------------------------------------------------------------------- |
| 74 | static void sort_sources( |
| 75 | std::vector<cryptonote::tx_source_entry>& sources |
| 76 | ) |
| 77 | { |
| 78 | std::sort(sources.begin(), sources.end(), [](const auto& lhs, const auto& rhs){ |
| 79 | const rct::key& ki0 = lhs.multisig_kLRki.ki; |
| 80 | const rct::key& ki1 = rhs.multisig_kLRki.ki; |
| 81 | return memcmp(&ki0, &ki1, sizeof(rct::key)) > 0; |
| 82 | }); |
| 83 | } |
| 84 | //---------------------------------------------------------------------------------------------------------------------- |
| 85 | //---------------------------------------------------------------------------------------------------------------------- |
| 86 | static bool compute_keys_for_sources( |
| 87 | const cryptonote::account_keys& account_keys, |
| 88 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 89 | const std::uint32_t subaddr_account, |
| 90 | const std::set<std::uint32_t>& subaddr_minor_indices, |
| 91 | rct::keyV& input_secret_keys |
| 92 | ) |
| 93 | { |
| 94 | const std::size_t num_sources = sources.size(); |
| 95 | hw::device& hwdev = account_keys.get_device(); |
| 96 | std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; |
| 97 | for (const std::uint32_t minor_index: subaddr_minor_indices) { |
| 98 | subaddresses[hwdev.get_subaddress_spend_public_key( |
| 99 | account_keys, |
| 100 | {subaddr_account, minor_index} |
| 101 | )] = {subaddr_account, minor_index}; |
| 102 | } |
| 103 | input_secret_keys.resize(num_sources); |
| 104 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 105 | const auto& src = sources[i]; |
| 106 | crypto::key_image tmp_key_image; |
| 107 | cryptonote::keypair tmp_keys; |
| 108 | if (src.real_output >= src.outputs.size()) |
| 109 | return false; |
| 110 | if (not cryptonote::generate_key_image_helper( |
| 111 | account_keys, |
| 112 | subaddresses, |
| 113 | rct::rct2pk(src.outputs[src.real_output].second.dest), |
| 114 | src.real_out_tx_key, |
| 115 | src.real_out_additional_tx_keys, |
| 116 | src.real_output_in_tx_index, |
| 117 | tmp_keys, |
| 118 | tmp_key_image, |
| 119 | hwdev |
| 120 | )) { |
| 121 | return false; |
| 122 | } |
| 123 | input_secret_keys[i] = rct::sk2rct(tmp_keys.sec); |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | //---------------------------------------------------------------------------------------------------------------------- |
| 128 | //---------------------------------------------------------------------------------------------------------------------- |
| 129 | static void shuffle_destinations( |
| 130 | std::vector<cryptonote::tx_destination_entry>& destinations |
| 131 | ) |
| 132 | { |
| 133 | std::shuffle(destinations.begin(), destinations.end(), crypto::random_device{}); |
| 134 | } |
| 135 | //---------------------------------------------------------------------------------------------------------------------- |
| 136 | //---------------------------------------------------------------------------------------------------------------------- |
| 137 | static bool set_tx_extra( |
| 138 | const cryptonote::account_keys& account_keys, |
| 139 | const std::vector<cryptonote::tx_destination_entry>& destinations, |
| 140 | const cryptonote::tx_destination_entry& change, |
| 141 | const crypto::secret_key& tx_secret_key, |
| 142 | const crypto::public_key& tx_public_key, |
| 143 | const std::vector<crypto::public_key>& tx_aux_public_keys, |
| 144 | const std::vector<std::uint8_t>& extra, |
| 145 | cryptonote::transaction& tx |
| 146 | ) |
| 147 | { |
| 148 | hw::device &hwdev = account_keys.get_device(); |
| 149 | tx.extra = extra; |
| 150 | // if we have a stealth payment id, find it and encrypt it with the tx key now |
| 151 | std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 152 | if (cryptonote::parse_tx_extra(tx.extra, tx_extra_fields)) |
| 153 | { |
| 154 | bool add_dummy_payment_id = true; |
| 155 | cryptonote::tx_extra_nonce extra_nonce; |
| 156 | if (cryptonote::find_tx_extra_field_by_type(tx_extra_fields, extra_nonce)) |
| 157 | { |
| 158 | crypto::hash payment_id = crypto::null_hash; |
| 159 | crypto::hash8 payment_id8 = crypto::null_hash8; |
| 160 | if (cryptonote::get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8)) |
| 161 | { |
| 162 | LOG_PRINT_L2("Encrypting payment id " << payment_id8); |
| 163 | crypto::public_key view_key_pub = cryptonote::get_destination_view_key_pub(destinations, change.addr); |
| 164 | if (view_key_pub == crypto::null_pkey) |
| 165 | { |
| 166 | // valid combinations: |
| 167 | // - 1 output with encrypted payment ID, dummy change output (0 amount) |
| 168 | // - 0 outputs, 1 change output with encrypted payment ID |
| 169 | // - 1 output with encrypted payment ID, 1 change output |
| 170 | LOG_ERROR("Destinations have to have exactly one output to support encrypted payment ids"); |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (!hwdev.encrypt_payment_id(payment_id8, view_key_pub, tx_secret_key)) |
| 175 | { |
| 176 | LOG_ERROR("Failed to encrypt payment id"); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | std::string extra_nonce_updated; |
| 181 | cryptonote::set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce_updated, payment_id8); |
| 182 | cryptonote::remove_field_from_tx_extra(tx.extra, typeid(cryptonote::tx_extra_nonce)); |
| 183 | if (!cryptonote::add_extra_nonce_to_tx_extra(tx.extra, extra_nonce_updated)) |
| 184 | { |
| 185 | LOG_ERROR("Failed to add encrypted payment id to tx extra"); |
| 186 | return false; |
| 187 | } |
| 188 | LOG_PRINT_L1("Encrypted payment ID: " << payment_id8); |
| 189 | add_dummy_payment_id = false; |
| 190 | } |
| 191 | else if (cryptonote::get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id)) |
| 192 | { |
| 193 | add_dummy_payment_id = false; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // we don't add one if we've got more than the usual 1 destination plus change |
| 198 | if (destinations.size() > 2) |
| 199 | add_dummy_payment_id = false; |
| 200 | |
| 201 | if (add_dummy_payment_id) |
| 202 | { |
| 203 | // if we have neither long nor short payment id, add a dummy short one, |
| 204 | // this should end up being the vast majority of txes as time goes on |
| 205 | std::string extra_nonce_updated; |
| 206 | crypto::hash8 payment_id8 = crypto::null_hash8; |
| 207 | crypto::public_key view_key_pub = cryptonote::get_destination_view_key_pub(destinations, change.addr); |
| 208 | if (view_key_pub == crypto::null_pkey) |
| 209 | { |
| 210 | LOG_ERROR("Failed to get key to encrypt dummy payment id with"); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | hwdev.encrypt_payment_id(payment_id8, view_key_pub, tx_secret_key); |
| 215 | cryptonote::set_encrypted_payment_id_to_tx_extra_nonce(extra_nonce_updated, payment_id8); |
| 216 | if (!cryptonote::add_extra_nonce_to_tx_extra(tx.extra, extra_nonce_updated)) |
| 217 | { |
| 218 | LOG_ERROR("Failed to add dummy encrypted payment id to tx extra"); |
| 219 | // continue anyway |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | MWARNING("Failed to parse tx extra"); |
| 227 | tx_extra_fields.clear(); |
| 228 | } |
| 229 | |
| 230 | cryptonote::remove_field_from_tx_extra(tx.extra, typeid(cryptonote::tx_extra_pub_key)); |
| 231 | cryptonote::add_tx_pub_key_to_extra(tx.extra, tx_public_key); |
| 232 | cryptonote::remove_field_from_tx_extra(tx.extra, typeid(cryptonote::tx_extra_additional_pub_keys)); |
| 233 | LOG_PRINT_L2("tx pubkey: " << tx_public_key); |
| 234 | if (tx_aux_public_keys.size()) |
| 235 | { |
| 236 | LOG_PRINT_L2("additional tx pubkeys: "); |
| 237 | for (size_t i = 0; i < tx_aux_public_keys.size(); ++i) |
| 238 | LOG_PRINT_L2(tx_aux_public_keys[i]); |
| 239 | cryptonote::add_additional_tx_pub_keys_to_extra(tx.extra, tx_aux_public_keys); |
| 240 | } |
| 241 | if (not cryptonote::sort_tx_extra(tx.extra, tx.extra)) |
| 242 | return false; |
| 243 | return true; |
| 244 | } |
| 245 | //---------------------------------------------------------------------------------------------------------------------- |
| 246 | //---------------------------------------------------------------------------------------------------------------------- |
| 247 | static void make_tx_secret_key_seed(const crypto::secret_key& tx_secret_key_entropy, |
| 248 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 249 | crypto::secret_key& tx_secret_key_seed) |
| 250 | { |
| 251 | // seed = H(H("domain separator"), entropy, {KI}) |
| 252 | static const std::string domain_separator{config::HASH_KEY_MULTISIG_TX_PRIVKEYS_SEED}; |
| 253 | |
| 254 | rct::keyV hash_context; |
| 255 | hash_context.reserve(2 + sources.size()); |
| 256 | auto hash_context_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 257 | memwipe(hash_context.data(), hash_context.size()); |
| 258 | }); |
| 259 | hash_context.emplace_back(); |
| 260 | rct::cn_fast_hash(hash_context.back(), domain_separator.data(), domain_separator.size()); //domain sep |
| 261 | hash_context.emplace_back(rct::sk2rct(tx_secret_key_entropy)); //entropy |
| 262 | |
| 263 | for (const cryptonote::tx_source_entry& source : sources) |
| 264 | hash_context.emplace_back(source.multisig_kLRki.ki); //{KI} |
| 265 | |
| 266 | // set the seed |
| 267 | tx_secret_key_seed = rct::rct2sk(rct::cn_fast_hash(hash_context)); |
| 268 | } |
| 269 | //---------------------------------------------------------------------------------------------------------------------- |
| 270 | //---------------------------------------------------------------------------------------------------------------------- |
| 271 | static void make_tx_secret_keys(const crypto::secret_key& tx_secret_key_seed, |
| 272 | const std::size_t num_tx_keys, |
| 273 | std::vector<crypto::secret_key>& tx_secret_keys) |
| 274 | { |
| 275 | // make tx secret keys as a hash chain of the seed |
| 276 | // h1 = H_n(seed || H("domain separator")) |
| 277 | // h2 = H_n(seed || h1) |
| 278 | // h3 = H_n(seed || h2) |
| 279 | // ... |
| 280 | static const std::string domain_separator{config::HASH_KEY_MULTISIG_TX_PRIVKEYS}; |
| 281 | |
| 282 | rct::keyV hash_context; |
| 283 | hash_context.resize(2); |
| 284 | auto hash_context_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 285 | memwipe(hash_context.data(), hash_context.size()); |
| 286 | }); |
| 287 | hash_context[0] = rct::sk2rct(tx_secret_key_seed); |
| 288 | rct::cn_fast_hash(hash_context[1], domain_separator.data(), domain_separator.size()); |
| 289 | |
| 290 | tx_secret_keys.clear(); |
| 291 | tx_secret_keys.resize(num_tx_keys); |
| 292 | |
| 293 | for (crypto::secret_key& tx_secret_key : tx_secret_keys) |
| 294 | { |
| 295 | // advance the hash chain |
| 296 | hash_context[1] = rct::hash_to_scalar(hash_context); |
| 297 | |
| 298 | // set this key |
| 299 | tx_secret_key = rct::rct2sk(hash_context[1]); |
| 300 | } |
| 301 | } |
| 302 | //---------------------------------------------------------------------------------------------------------------------- |
| 303 | //---------------------------------------------------------------------------------------------------------------------- |
| 304 | static bool collect_tx_secret_keys(const std::vector<crypto::secret_key>& tx_secret_keys, |
| 305 | crypto::secret_key& tx_secret_key, |
| 306 | std::vector<crypto::secret_key>& tx_aux_secret_keys) |
| 307 | { |
| 308 | if (tx_secret_keys.size() == 0) |
| 309 | return false; |
| 310 | |
| 311 | tx_secret_key = tx_secret_keys[0]; |
| 312 | tx_aux_secret_keys.clear(); |
| 313 | tx_aux_secret_keys.reserve(tx_secret_keys.size() - 1); |
| 314 | for (std::size_t tx_key_index{1}; tx_key_index < tx_secret_keys.size(); ++tx_key_index) |
| 315 | tx_aux_secret_keys.emplace_back(tx_secret_keys[tx_key_index]); |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | //---------------------------------------------------------------------------------------------------------------------- |
| 320 | //---------------------------------------------------------------------------------------------------------------------- |
| 321 | static bool compute_keys_for_destinations( |
| 322 | const cryptonote::account_keys& account_keys, |
| 323 | const std::uint32_t subaddr_account, |
| 324 | const std::vector<cryptonote::tx_destination_entry>& destinations, |
| 325 | const cryptonote::tx_destination_entry& change, |
| 326 | const std::vector<std::uint8_t>& extra, |
| 327 | const bool use_view_tags, |
| 328 | const bool reconstruction, |
| 329 | const crypto::secret_key& tx_secret_key_seed, |
| 330 | crypto::secret_key& tx_secret_key, |
| 331 | std::vector<crypto::secret_key>& tx_aux_secret_keys, |
| 332 | rct::keyV& output_public_keys, |
| 333 | rct::keyV& output_amount_secret_keys, |
| 334 | std::vector<crypto::view_tag>& view_tags, |
| 335 | cryptonote::transaction& unsigned_tx |
| 336 | ) |
| 337 | { |
| 338 | hw::device &hwdev = account_keys.get_device(); |
| 339 | |
| 340 | // check non-zero change amount case |
| 341 | if (change.amount > 0) |
| 342 | { |
| 343 | // the change output must be directed to the local account |
| 344 | if (change.addr != hwdev.get_subaddress(account_keys, {subaddr_account})) |
| 345 | return false; |
| 346 | |
| 347 | // expect the change destination to be in the destination set |
| 348 | if (std::find_if(destinations.begin(), destinations.end(), |
| 349 | [&change](const auto &destination) -> bool |
| 350 | { |
| 351 | return destination.addr == change.addr; |
| 352 | }) == destinations.end()) |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // collect non-change recipients into normal/subaddress buckets |
| 357 | std::unordered_set<cryptonote::account_public_address> unique_subbaddr_recipients; |
| 358 | std::unordered_set<cryptonote::account_public_address> unique_std_recipients; |
| 359 | for(const auto& dst_entr: destinations) { |
| 360 | if (dst_entr.addr == change.addr) |
| 361 | continue; |
| 362 | if (dst_entr.is_subaddress) |
| 363 | unique_subbaddr_recipients.insert(dst_entr.addr); |
| 364 | else |
| 365 | unique_std_recipients.insert(dst_entr.addr); |
| 366 | } |
| 367 | |
| 368 | // figure out how many tx secret keys are needed |
| 369 | // - tx aux keys: add if there are > 1 non-change recipients, with at least one to a subaddress |
| 370 | const std::size_t num_destinations = destinations.size(); |
| 371 | const bool need_tx_aux_keys = unique_subbaddr_recipients.size() + bool(unique_std_recipients.size()) > 1; |
| 372 | |
| 373 | const std::size_t num_tx_keys = 1 + (need_tx_aux_keys ? num_destinations : 0); |
| 374 | |
| 375 | // make tx secret keys |
| 376 | std::vector<crypto::secret_key> all_tx_secret_keys; |
| 377 | make_tx_secret_keys(tx_secret_key_seed, num_tx_keys, all_tx_secret_keys); |
| 378 | |
| 379 | // split up tx secret keys |
| 380 | crypto::secret_key tx_secret_key_temp; |
| 381 | std::vector<crypto::secret_key> tx_aux_secret_keys_temp; |
| 382 | if (not collect_tx_secret_keys(all_tx_secret_keys, tx_secret_key_temp, tx_aux_secret_keys_temp)) |
| 383 | return false; |
| 384 | |
| 385 | if (reconstruction) |
| 386 | { |
| 387 | // when reconstructing, the tx secret keys should be reproducible from input seed |
| 388 | if (!(tx_secret_key == tx_secret_key_temp)) |
| 389 | return false; |
| 390 | if (!(tx_aux_secret_keys == tx_aux_secret_keys_temp)) |
| 391 | return false; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | tx_secret_key = tx_secret_key_temp; |
| 396 | tx_aux_secret_keys = std::move(tx_aux_secret_keys_temp); |
| 397 | } |
| 398 | |
| 399 | // tx pub key: R |
| 400 | crypto::public_key tx_public_key; |
| 401 | if (unique_std_recipients.empty() && unique_subbaddr_recipients.size() == 1) { |
| 402 | // if there is exactly 1 non-change recipient, and it's to a subaddress, then the tx pubkey = r*Ksi_nonchange_recipient |
| 403 | tx_public_key = rct::rct2pk( |
| 404 | hwdev.scalarmultKey( |
| 405 | rct::pk2rct(unique_subbaddr_recipients.begin()->m_spend_public_key), |
| 406 | rct::sk2rct(tx_secret_key) |
| 407 | )); |
| 408 | } |
| 409 | else { |
| 410 | // otherwise, the tx pub key = r*G |
| 411 | // - if there are > 1 non-change recipients, with at least one to a subaddress, then the tx pubkey is not used |
| 412 | // (additional tx keys will be used instead) |
| 413 | // - if all non-change recipients are to normal addresses, then the tx pubkey will be used by all recipients |
| 414 | // (including change recipient, even if change is to a subaddress) |
| 415 | tx_public_key = rct::rct2pk(hwdev.scalarmultBase(rct::sk2rct(tx_secret_key))); |
| 416 | } |
| 417 | |
| 418 | // additional tx pubkeys: R_t |
| 419 | output_public_keys.resize(num_destinations); |
| 420 | view_tags.resize(num_destinations); |
| 421 | std::vector<crypto::public_key> tx_aux_public_keys; |
| 422 | crypto::public_key temp_output_public_key; |
| 423 | |
| 424 | for (std::size_t i = 0; i < num_destinations; ++i) { |
| 425 | if (not hwdev.generate_output_ephemeral_keys( |
| 426 | unsigned_tx.version, |
| 427 | account_keys, |
| 428 | tx_public_key, |
| 429 | tx_secret_key, |
| 430 | destinations[i], |
| 431 | change.addr, |
| 432 | i, |
| 433 | need_tx_aux_keys, |
| 434 | tx_aux_secret_keys, |
| 435 | tx_aux_public_keys, |
| 436 | output_amount_secret_keys, |
| 437 | temp_output_public_key, |
| 438 | use_view_tags, |
| 439 | view_tags[i] //unused variable if use_view_tags is not set |
| 440 | )) { |
| 441 | return false; |
| 442 | } |
| 443 | output_public_keys[i] = rct::pk2rct(temp_output_public_key); |
| 444 | } |
| 445 | |
| 446 | if (num_destinations != output_amount_secret_keys.size()) |
| 447 | return false; |
| 448 | |
| 449 | CHECK_AND_ASSERT_MES( |
| 450 | tx_aux_public_keys.size() == tx_aux_secret_keys.size(), |
| 451 | false, |
| 452 | "Internal error creating additional public keys" |
| 453 | ); |
| 454 | |
| 455 | if (not set_tx_extra(account_keys, destinations, change, tx_secret_key, tx_public_key, tx_aux_public_keys, extra, unsigned_tx)) |
| 456 | return false; |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | //---------------------------------------------------------------------------------------------------------------------- |
| 461 | //---------------------------------------------------------------------------------------------------------------------- |
| 462 | static void set_tx_inputs( |
| 463 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 464 | cryptonote::transaction& unsigned_tx |
| 465 | ) |
| 466 | { |
| 467 | const std::size_t num_sources = sources.size(); |
| 468 | unsigned_tx.vin.resize(num_sources); |
| 469 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 470 | std::vector<std::uint64_t> offsets; |
| 471 | offsets.reserve(sources[i].outputs.size()); |
| 472 | for (const auto& e: sources[i].outputs) |
| 473 | offsets.emplace_back(e.first); |
| 474 | unsigned_tx.vin[i] = cryptonote::txin_to_key{ |
| 475 | .amount = 0, |
| 476 | .key_offsets = cryptonote::absolute_output_offsets_to_relative(offsets), |
| 477 | .k_image = rct::rct2ki(sources[i].multisig_kLRki.ki), |
| 478 | }; |
| 479 | } |
| 480 | } |
| 481 | //---------------------------------------------------------------------------------------------------------------------- |
| 482 | //---------------------------------------------------------------------------------------------------------------------- |
| 483 | static bool onetime_addresses_are_unique(const rct::keyV& output_public_keys) |
| 484 | { |
| 485 | for (auto addr_it = output_public_keys.begin(); addr_it != output_public_keys.end(); ++addr_it) |
| 486 | { |
| 487 | if (std::find(output_public_keys.begin(), addr_it, *addr_it) != addr_it) |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | return true; |
| 492 | } |
| 493 | //---------------------------------------------------------------------------------------------------------------------- |
| 494 | //---------------------------------------------------------------------------------------------------------------------- |
| 495 | static bool set_tx_outputs(const rct::keyV& output_public_keys, cryptonote::transaction& unsigned_tx) |
| 496 | { |
| 497 | // sanity check: all onetime addresses should be unique |
| 498 | if (not onetime_addresses_are_unique(output_public_keys)) |
| 499 | return false; |
| 500 | |
| 501 | // set the tx outputs |
| 502 | const std::size_t num_destinations = output_public_keys.size(); |
| 503 | unsigned_tx.vout.resize(num_destinations); |
| 504 | for (std::size_t i = 0; i < num_destinations; ++i) |
| 505 | cryptonote::set_tx_out(0, rct::rct2pk(output_public_keys[i]), false, crypto::view_tag{}, unsigned_tx.vout[i]); |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | //---------------------------------------------------------------------------------------------------------------------- |
| 510 | //---------------------------------------------------------------------------------------------------------------------- |
| 511 | static bool set_tx_outputs_with_view_tags( |
| 512 | const rct::keyV& output_public_keys, |
| 513 | const std::vector<crypto::view_tag>& view_tags, |
| 514 | cryptonote::transaction& unsigned_tx |
| 515 | ) |
| 516 | { |
| 517 | // sanity check: all onetime addresses should be unique |
| 518 | if (not onetime_addresses_are_unique(output_public_keys)) |
| 519 | return false; |
| 520 | |
| 521 | // set the tx outputs (with view tags) |
| 522 | const std::size_t num_destinations = output_public_keys.size(); |
| 523 | CHECK_AND_ASSERT_MES(view_tags.size() == num_destinations, false, |
| 524 | "multisig signing protocol: internal error, view tag size mismatch."); |
| 525 | unsigned_tx.vout.resize(num_destinations); |
| 526 | for (std::size_t i = 0; i < num_destinations; ++i) |
| 527 | cryptonote::set_tx_out(0, rct::rct2pk(output_public_keys[i]), true, view_tags[i], unsigned_tx.vout[i]); |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | //---------------------------------------------------------------------------------------------------------------------- |
| 532 | //---------------------------------------------------------------------------------------------------------------------- |
| 533 | static void make_new_range_proofs(const int bp_version, |
| 534 | const std::vector<std::uint64_t>& output_amounts, |
| 535 | const rct::keyV& output_amount_masks, |
| 536 | rct::rctSigPrunable& sigs) |
| 537 | { |
| 538 | sigs.bulletproofs.clear(); |
| 539 | sigs.bulletproofs_plus.clear(); |
| 540 | |
| 541 | if (bp_version == 3) |
| 542 | sigs.bulletproofs.push_back(rct::bulletproof_PROVE(output_amounts, output_amount_masks)); |
| 543 | else if (bp_version == 4) |
| 544 | sigs.bulletproofs_plus.push_back(rct::bulletproof_plus_PROVE(output_amounts, output_amount_masks)); |
| 545 | } |
| 546 | //---------------------------------------------------------------------------------------------------------------------- |
| 547 | //---------------------------------------------------------------------------------------------------------------------- |
| 548 | static bool try_reconstruct_range_proofs(const int bp_version, |
| 549 | const rct::rctSigPrunable& original_sigs, |
| 550 | const std::size_t num_destinations, |
| 551 | const rct::ctkeyV& output_public_keys, |
| 552 | rct::rctSigPrunable& reconstructed_sigs) |
| 553 | { |
| 554 | auto try_reconstruct_range_proofs = |
| 555 | [&](const auto &original_range_proofs, auto &new_range_proofs) -> bool |
| 556 | { |
| 557 | if (original_range_proofs.size() != 1) |
| 558 | return false; |
| 559 | |
| 560 | new_range_proofs = original_range_proofs; |
| 561 | new_range_proofs[0].V.resize(num_destinations); |
| 562 | for (std::size_t i = 0; i < num_destinations; ++i) |
| 563 | new_range_proofs[0].V[i] = rct::scalarmultKey(output_public_keys[i].mask, rct::INV_EIGHT); |
| 564 | |
| 565 | return true; |
| 566 | }; |
| 567 | |
| 568 | if (bp_version == 3) |
| 569 | { |
| 570 | if (not try_reconstruct_range_proofs(original_sigs.bulletproofs, reconstructed_sigs.bulletproofs)) |
| 571 | return false; |
| 572 | return rct::bulletproof_VERIFY(reconstructed_sigs.bulletproofs); |
| 573 | } |
| 574 | else if (bp_version == 4) |
| 575 | { |
| 576 | if (not try_reconstruct_range_proofs(original_sigs.bulletproofs_plus, reconstructed_sigs.bulletproofs_plus)) |
| 577 | return false; |
| 578 | return rct::bulletproof_plus_VERIFY(reconstructed_sigs.bulletproofs_plus); |
| 579 | } |
| 580 | |
| 581 | return false; |
| 582 | } |
| 583 | //---------------------------------------------------------------------------------------------------------------------- |
| 584 | //---------------------------------------------------------------------------------------------------------------------- |
| 585 | static bool set_tx_rct_signatures( |
| 586 | const std::uint64_t fee, |
| 587 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 588 | const std::vector<cryptonote::tx_destination_entry>& destinations, |
| 589 | const rct::keyV& input_secret_keys, |
| 590 | const rct::keyV& output_public_keys, |
| 591 | const rct::keyV& output_amount_secret_keys, |
| 592 | const rct::RCTConfig& rct_config, |
| 593 | const bool reconstruction, |
| 594 | cryptonote::transaction& unsigned_tx, |
| 595 | std::vector<CLSAG_context_t>& CLSAG_contexts, |
| 596 | rct::keyV& cached_w |
| 597 | ) |
| 598 | { |
| 599 | if (rct_config.bp_version != 3 && |
| 600 | rct_config.bp_version != 4) |
| 601 | return false; |
| 602 | if (rct_config.range_proof_type != rct::RangeProofPaddedBulletproof) |
| 603 | return false; |
| 604 | |
| 605 | const std::size_t num_destinations = destinations.size(); |
| 606 | const std::size_t num_sources = sources.size(); |
| 607 | |
| 608 | // rct_signatures component of tx |
| 609 | rct::rctSig rv{}; |
| 610 | |
| 611 | // set misc. fields |
| 612 | if (rct_config.bp_version == 3) |
| 613 | rv.type = rct::RCTTypeCLSAG; |
| 614 | else if (rct_config.bp_version == 4) |
| 615 | rv.type = rct::RCTTypeBulletproofPlus; |
| 616 | else |
| 617 | return false; |
| 618 | rv.txnFee = fee; |
| 619 | rv.message = rct::hash2rct(cryptonote::get_transaction_prefix_hash(unsigned_tx)); |
| 620 | |
| 621 | // define outputs |
| 622 | std::vector<std::uint64_t> output_amounts(num_destinations); |
| 623 | rct::keyV output_amount_masks(num_destinations); |
| 624 | rv.ecdhInfo.resize(num_destinations); |
| 625 | rv.outPk.resize(num_destinations); |
| 626 | for (std::size_t i = 0; i < num_destinations; ++i) { |
| 627 | rv.outPk[i].dest = output_public_keys[i]; |
| 628 | output_amounts[i] = destinations[i].amount; |
| 629 | output_amount_masks[i] = genCommitmentMask(output_amount_secret_keys[i]); |
| 630 | rv.ecdhInfo[i].amount = rct::d2h(output_amounts[i]); |
| 631 | rct::addKeys2( |
| 632 | rv.outPk[i].mask, |
| 633 | output_amount_masks[i], |
| 634 | rv.ecdhInfo[i].amount, |
| 635 | rct::H |
| 636 | ); |
| 637 | rct::ecdhEncode(rv.ecdhInfo[i], output_amount_secret_keys[i], true); |
| 638 | } |
| 639 | |
| 640 | // output range proofs |
| 641 | if (not reconstruction) { |
| 642 | make_new_range_proofs(rct_config.bp_version, output_amounts, output_amount_masks, rv.p); |
| 643 | } |
| 644 | else { |
| 645 | if (not try_reconstruct_range_proofs(rct_config.bp_version, |
| 646 | unsigned_tx.rct_signatures.p, |
| 647 | num_destinations, |
| 648 | rv.outPk, |
| 649 | rv.p)) |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | // prepare rings for input CLSAGs |
| 654 | rv.mixRing.resize(num_sources); |
| 655 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 656 | const std::size_t ring_size = sources[i].outputs.size(); |
| 657 | rv.mixRing[i].resize(ring_size); |
| 658 | for (std::size_t j = 0; j < ring_size; ++j) { |
| 659 | rv.mixRing[i][j].dest = sources[i].outputs[j].second.dest; |
| 660 | rv.mixRing[i][j].mask = sources[i].outputs[j].second.mask; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // make pseudo-output commitments |
| 665 | rct::keyV a; //pseudo-output commitment blinding factors |
| 666 | auto a_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 667 | memwipe(static_cast<rct::key *>(a.data()), a.size() * sizeof(rct::key)); |
| 668 | }); |
| 669 | if (not reconstruction) { |
| 670 | a.resize(num_sources); |
| 671 | rv.p.pseudoOuts.resize(num_sources); |
| 672 | a[num_sources - 1] = rct::zero(); |
| 673 | for (std::size_t i = 0; i < num_destinations; ++i) { |
| 674 | sc_add( |
| 675 | a[num_sources - 1].bytes, |
| 676 | a[num_sources - 1].bytes, |
| 677 | output_amount_masks[i].bytes |
| 678 | ); |
| 679 | } |
| 680 | for (std::size_t i = 0; i < num_sources - 1; ++i) { |
| 681 | rct::skGen(a[i]); |
| 682 | sc_sub( |
| 683 | a[num_sources - 1].bytes, |
| 684 | a[num_sources - 1].bytes, |
| 685 | a[i].bytes |
| 686 | ); |
| 687 | rct::genC(rv.p.pseudoOuts[i], a[i], sources[i].amount); |
| 688 | } |
| 689 | rct::genC( |
| 690 | rv.p.pseudoOuts[num_sources - 1], |
| 691 | a[num_sources - 1], |
| 692 | sources[num_sources - 1].amount |
| 693 | ); |
| 694 | } |
| 695 | // check balance if reconstructing the tx |
| 696 | else { |
| 697 | rv.p.pseudoOuts = unsigned_tx.rct_signatures.p.pseudoOuts; |
| 698 | if (num_sources != rv.p.pseudoOuts.size()) |
| 699 | return false; |
| 700 | rct::key balance_accumulator = rct::scalarmultH(rct::d2h(fee)); |
| 701 | for (const auto& e: rv.outPk) |
| 702 | rct::addKeys(balance_accumulator, balance_accumulator, e.mask); |
| 703 | for (const auto& pseudoOut: rv.p.pseudoOuts) |
| 704 | rct::subKeys(balance_accumulator, balance_accumulator, pseudoOut); |
| 705 | if (not (balance_accumulator == rct::identity())) |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | // prepare input CLSAGs for signing |
| 710 | const rct::key message = get_pre_mlsag_hash(rv, hw::get_device("default")); |
| 711 | |
| 712 | rv.p.CLSAGs.resize(num_sources); |
| 713 | if (reconstruction) { |
| 714 | if (num_sources != unsigned_tx.rct_signatures.p.CLSAGs.size()) |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | CLSAG_contexts.resize(num_sources); |
| 719 | if (not reconstruction) |
| 720 | cached_w.resize(num_sources); |
| 721 | |
| 722 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 723 | const std::size_t ring_size = rv.mixRing[i].size(); |
| 724 | const rct::key& I = sources[i].multisig_kLRki.ki; |
| 725 | const std::size_t l = sources[i].real_output; |
| 726 | if (l >= ring_size) |
| 727 | return false; |
| 728 | rct::keyV& s = rv.p.CLSAGs[i].s; |
| 729 | const rct::key& C_offset = rv.p.pseudoOuts[i]; |
| 730 | rct::keyV P(ring_size); |
| 731 | rct::keyV C_nonzero(ring_size); |
| 732 | |
| 733 | if (not reconstruction) { |
| 734 | s.resize(ring_size); |
| 735 | for (std::size_t j = 0; j < ring_size; ++j) { |
| 736 | if (j != l) |
| 737 | s[j] = rct::skGen(); //make fake responses |
| 738 | } |
| 739 | } |
| 740 | else { |
| 741 | if (ring_size != unsigned_tx.rct_signatures.p.CLSAGs[i].s.size()) |
| 742 | return false; |
| 743 | s = unsigned_tx.rct_signatures.p.CLSAGs[i].s; |
| 744 | } |
| 745 | |
| 746 | for (std::size_t j = 0; j < ring_size; ++j) { |
| 747 | P[j] = rv.mixRing[i][j].dest; |
| 748 | C_nonzero[j] = rv.mixRing[i][j].mask; |
| 749 | } |
| 750 | |
| 751 | rct::key D; |
| 752 | rct::key z; |
| 753 | auto z_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 754 | memwipe(static_cast<rct::key *>(&z), sizeof(rct::key)); |
| 755 | }); |
| 756 | if (not reconstruction) { |
| 757 | sc_sub(z.bytes, sources[i].mask.bytes, a[i].bytes); //commitment to zero privkey |
| 758 | ge_p3 H_p3; |
| 759 | rct::hash_to_p3(H_p3, rv.mixRing[i][l].dest); |
| 760 | rct::key H_l; |
| 761 | ge_p3_tobytes(H_l.bytes, &H_p3); |
| 762 | D = rct::scalarmultKey(H_l, z); //auxilliary key image (for commitment to zero) |
| 763 | rv.p.CLSAGs[i].D = rct::scalarmultKey(D, rct::INV_EIGHT); |
| 764 | rv.p.CLSAGs[i].I = I; |
| 765 | } |
| 766 | else { |
| 767 | rv.p.CLSAGs[i].D = unsigned_tx.rct_signatures.p.CLSAGs[i].D; |
| 768 | rv.p.CLSAGs[i].I = I; |
| 769 | D = rct::scalarmultKey(rv.p.CLSAGs[i].D, rct::EIGHT); |
| 770 | } |
| 771 | |
| 772 | if (not CLSAG_contexts[i].init(P, C_nonzero, C_offset, message, I, D, l, s, kAlphaComponents)) |
| 773 | return false; |
| 774 | |
| 775 | if (not reconstruction) { |
| 776 | rct::key mu_P; |
| 777 | rct::key mu_C; |
| 778 | if (not CLSAG_contexts[i].get_mu(mu_P, mu_C)) |
| 779 | return false; |
| 780 | sc_mul(cached_w[i].bytes, mu_P.bytes, input_secret_keys[i].bytes); |
| 781 | sc_muladd(cached_w[i].bytes, mu_C.bytes, z.bytes, cached_w[i].bytes); |
| 782 | } |
| 783 | } |
| 784 | unsigned_tx.rct_signatures = std::move(rv); |
| 785 | return true; |
| 786 | } |
| 787 | //---------------------------------------------------------------------------------------------------------------------- |
| 788 | //---------------------------------------------------------------------------------------------------------------------- |
| 789 | static bool compute_tx_fee( |
| 790 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 791 | const std::vector<cryptonote::tx_destination_entry>& destinations, |
| 792 | std::uint64_t& fee |
| 793 | ) |
| 794 | { |
| 795 | boost::multiprecision::uint128_t in_amount = 0; |
| 796 | for (const auto& src: sources) |
| 797 | in_amount += src.amount; |
| 798 | |
| 799 | boost::multiprecision::uint128_t out_amount = 0; |
| 800 | for (const auto& dst: destinations) |
| 801 | out_amount += dst.amount; |
| 802 | |
| 803 | if (out_amount > in_amount) |
| 804 | return false; |
| 805 | |
| 806 | if (in_amount - out_amount > std::numeric_limits<std::uint64_t>::max()) |
| 807 | return false; |
| 808 | |
| 809 | fee = static_cast<std::uint64_t>(in_amount - out_amount); |
| 810 | return true; |
| 811 | } |
| 812 | //---------------------------------------------------------------------------------------------------------------------- |
| 813 | tx_builder_ringct_t::tx_builder_ringct_t(): initialized(false) {} |
| 814 | //---------------------------------------------------------------------------------------------------------------------- |
| 815 | tx_builder_ringct_t::~tx_builder_ringct_t() |
| 816 | { |
| 817 | memwipe(static_cast<rct::key *>(cached_w.data()), cached_w.size() * sizeof(rct::key)); |
| 818 | } |
| 819 | //---------------------------------------------------------------------------------------------------------------------- |
| 820 | bool tx_builder_ringct_t::init( |
| 821 | const cryptonote::account_keys& account_keys, |
| 822 | const std::vector<std::uint8_t>& extra, |
| 823 | const std::uint32_t subaddr_account, |
| 824 | const std::set<std::uint32_t>& subaddr_minor_indices, |
| 825 | std::vector<cryptonote::tx_source_entry>& sources, |
| 826 | std::vector<cryptonote::tx_destination_entry>& destinations, |
| 827 | const cryptonote::tx_destination_entry& change, |
| 828 | const rct::RCTConfig& rct_config, |
| 829 | const bool use_rct, |
| 830 | const bool reconstruction, |
| 831 | crypto::secret_key& tx_secret_key, |
| 832 | std::vector<crypto::secret_key>& tx_aux_secret_keys, |
| 833 | crypto::secret_key& tx_secret_key_entropy, |
| 834 | cryptonote::transaction& unsigned_tx |
| 835 | ) |
| 836 | { |
| 837 | initialized = false; |
| 838 | this->reconstruction = reconstruction; |
| 839 | if (not use_rct) |
| 840 | return false; |
| 841 | if (sources.empty()) |
| 842 | return false; |
| 843 | |
| 844 | if (not reconstruction) |
| 845 | unsigned_tx.set_null(); |
| 846 | |
| 847 | std::uint64_t fee; |
| 848 | if (not compute_tx_fee(sources, destinations, fee)) |
| 849 | return false; |
| 850 | |
| 851 | // decide if view tags are needed |
| 852 | const bool use_view_tags{view_tag_required(rct_config.bp_version)}; |
| 853 | |
| 854 | // misc. fields |
| 855 | unsigned_tx.version = 2; //rct = 2 |
| 856 | unsigned_tx.unlock_time = 0; |
| 857 | |
| 858 | // sort inputs |
| 859 | sort_sources(sources); |
| 860 | |
| 861 | // prepare tx secret key seed (must be AFTER sorting sources) |
| 862 | // - deriving the seed from sources plus entropy ensures uniqueness for every new tx attempt |
| 863 | // - the goal is that two multisig txs added to the chain will never have outputs with the same onetime addresses, |
| 864 | // which would burn funds (embedding the inputs' key images guarantees this) |
| 865 | // - it is acceptable if two tx attempts use the same input set and entropy (only a malicious tx proposer will do |
| 866 | // that, but all it can accomplish is leaking information about the recipients - which a malicious proposer can |
| 867 | // easily do outside the signing ritual anyway) |
| 868 | if (not reconstruction) |
| 869 | tx_secret_key_entropy = rct::rct2sk(rct::skGen()); |
| 870 | |
| 871 | // expect not null (note: wallet serialization code may set this to null if handling an old partial tx) |
| 872 | if (tx_secret_key_entropy == crypto::null_skey) |
| 873 | return false; |
| 874 | |
| 875 | crypto::secret_key tx_secret_key_seed; |
| 876 | make_tx_secret_key_seed(tx_secret_key_entropy, sources, tx_secret_key_seed); |
| 877 | |
| 878 | // get secret keys for signing input CLSAGs (multisig: or for the initial partial signature) |
| 879 | rct::keyV input_secret_keys; |
| 880 | auto input_secret_keys_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 881 | memwipe(static_cast<rct::key *>(input_secret_keys.data()), input_secret_keys.size() * sizeof(rct::key)); |
| 882 | }); |
| 883 | if (not compute_keys_for_sources(account_keys, sources, subaddr_account, subaddr_minor_indices, input_secret_keys)) |
| 884 | return false; |
| 885 | |
| 886 | // randomize output order |
| 887 | if (not reconstruction) |
| 888 | shuffle_destinations(destinations); |
| 889 | |
| 890 | // prepare outputs |
| 891 | rct::keyV output_public_keys; |
| 892 | rct::keyV output_amount_secret_keys; |
| 893 | std::vector<crypto::view_tag> view_tags; |
| 894 | auto output_amount_secret_keys_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 895 | memwipe(static_cast<rct::key *>(output_amount_secret_keys.data()), output_amount_secret_keys.size() * sizeof(rct::key)); |
| 896 | }); |
| 897 | if (not compute_keys_for_destinations(account_keys, |
| 898 | subaddr_account, |
| 899 | destinations, |
| 900 | change, |
| 901 | extra, |
| 902 | use_view_tags, |
| 903 | reconstruction, |
| 904 | tx_secret_key_seed, |
| 905 | tx_secret_key, |
| 906 | tx_aux_secret_keys, |
| 907 | output_public_keys, |
| 908 | output_amount_secret_keys, |
| 909 | view_tags, |
| 910 | unsigned_tx)) |
| 911 | return false; |
| 912 | |
| 913 | // add inputs to tx |
| 914 | set_tx_inputs(sources, unsigned_tx); |
| 915 | |
| 916 | // add output one-time addresses to tx |
| 917 | bool set_tx_outputs_result{false}; |
| 918 | if (use_view_tags) |
| 919 | set_tx_outputs_result = set_tx_outputs_with_view_tags(output_public_keys, view_tags, unsigned_tx); |
| 920 | else |
| 921 | set_tx_outputs_result = set_tx_outputs(output_public_keys, unsigned_tx); |
| 922 | |
| 923 | if (not set_tx_outputs_result) |
| 924 | return false; |
| 925 | |
| 926 | // prepare input signatures |
| 927 | if (not set_tx_rct_signatures(fee, sources, destinations, input_secret_keys, output_public_keys, output_amount_secret_keys, |
| 928 | rct_config, reconstruction, unsigned_tx, CLSAG_contexts, cached_w)) |
| 929 | return false; |
| 930 | |
| 931 | initialized = true; |
| 932 | return true; |
| 933 | } |
| 934 | //---------------------------------------------------------------------------------------------------------------------- |
| 935 | bool tx_builder_ringct_t::first_partial_sign( |
| 936 | const std::size_t source, |
| 937 | const rct::keyV& total_alpha_G, |
| 938 | const rct::keyV& total_alpha_H, |
| 939 | const rct::keyV& alpha, |
| 940 | rct::key& c_0, |
| 941 | rct::key& s |
| 942 | ) |
| 943 | { |
| 944 | if (not initialized or reconstruction) |
| 945 | return false; |
| 946 | const std::size_t num_sources = CLSAG_contexts.size(); |
| 947 | if (source >= num_sources) |
| 948 | return false; |
| 949 | rct::key c; |
| 950 | rct::key alpha_combined; |
| 951 | auto alpha_combined_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 952 | memwipe(static_cast<rct::key *>(&alpha_combined), sizeof(rct::key)); |
| 953 | }); |
| 954 | if (not CLSAG_contexts[source].combine_alpha_and_compute_challenge( |
| 955 | total_alpha_G, |
| 956 | total_alpha_H, |
| 957 | alpha, |
| 958 | alpha_combined, |
| 959 | c_0, |
| 960 | c |
| 961 | )) { |
| 962 | return false; |
| 963 | } |
| 964 | |
| 965 | // initial partial response: |
| 966 | // s = alpha_combined_local - challenge*[mu_P*(local keys and sender-receiver secret and subaddress material) + |
| 967 | // mu_C*(commitment-to-zero secret)] |
| 968 | sc_mulsub(s.bytes, c.bytes, cached_w[source].bytes, alpha_combined.bytes); |
| 969 | return true; |
| 970 | } |
| 971 | //---------------------------------------------------------------------------------------------------------------------- |
| 972 | bool tx_builder_ringct_t::next_partial_sign( |
| 973 | const rct::keyM& total_alpha_G, |
| 974 | const rct::keyM& total_alpha_H, |
| 975 | const rct::keyM& alpha, |
| 976 | const rct::key& x, |
| 977 | rct::keyV& c_0, |
| 978 | rct::keyV& s |
| 979 | ) |
| 980 | { |
| 981 | if (not initialized or not reconstruction) |
| 982 | return false; |
| 983 | const std::size_t num_sources = CLSAG_contexts.size(); |
| 984 | if (num_sources != total_alpha_G.size()) |
| 985 | return false; |
| 986 | if (num_sources != total_alpha_H.size()) |
| 987 | return false; |
| 988 | if (num_sources != alpha.size()) |
| 989 | return false; |
| 990 | if (num_sources != c_0.size()) |
| 991 | return false; |
| 992 | if (num_sources != s.size()) |
| 993 | return false; |
| 994 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 995 | rct::key c; |
| 996 | rct::key alpha_combined; |
| 997 | auto alpha_combined_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 998 | memwipe(static_cast<rct::key *>(&alpha_combined), sizeof(rct::key)); |
| 999 | }); |
| 1000 | if (not CLSAG_contexts[i].combine_alpha_and_compute_challenge( |
| 1001 | total_alpha_G[i], |
| 1002 | total_alpha_H[i], |
| 1003 | alpha[i], |
| 1004 | alpha_combined, |
| 1005 | c_0[i], |
| 1006 | c |
| 1007 | )) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | rct::key mu_P; |
| 1011 | rct::key mu_C; |
| 1012 | if (not CLSAG_contexts[i].get_mu(mu_P, mu_C)) |
| 1013 | return false; |
| 1014 | rct::key w; |
| 1015 | auto w_wiper = epee::misc_utils::create_scope_leave_handler([&]{ |
| 1016 | memwipe(static_cast<rct::key *>(&w), sizeof(rct::key)); |
| 1017 | }); |
| 1018 | sc_mul(w.bytes, mu_P.bytes, x.bytes); |
| 1019 | |
| 1020 | // include local signer's response: |
| 1021 | // s += alpha_combined_local - challenge*[mu_P*(local keys)] |
| 1022 | sc_add(s[i].bytes, s[i].bytes, alpha_combined.bytes); |
| 1023 | sc_mulsub(s[i].bytes, c.bytes, w.bytes, s[i].bytes); |
| 1024 | } |
| 1025 | return true; |
| 1026 | } |
| 1027 | //---------------------------------------------------------------------------------------------------------------------- |
| 1028 | bool tx_builder_ringct_t::finalize_tx( |
| 1029 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 1030 | const rct::keyV& c_0, |
| 1031 | const rct::keyV& s, |
| 1032 | cryptonote::transaction& unsigned_tx |
| 1033 | ) |
| 1034 | { |
| 1035 | // checks |
| 1036 | const std::size_t num_sources = sources.size(); |
| 1037 | if (num_sources != unsigned_tx.rct_signatures.p.CLSAGs.size()) |
| 1038 | return false; |
| 1039 | if (num_sources != c_0.size()) |
| 1040 | return false; |
| 1041 | if (num_sources != s.size()) |
| 1042 | return false; |
| 1043 | |
| 1044 | // finalize tx signatures |
| 1045 | for (std::size_t i = 0; i < num_sources; ++i) { |
| 1046 | const std::size_t ring_size = unsigned_tx.rct_signatures.p.CLSAGs[i].s.size(); |
| 1047 | if (sources[i].real_output >= ring_size) |
| 1048 | return false; |
| 1049 | unsigned_tx.rct_signatures.p.CLSAGs[i].s[sources[i].real_output] = s[i]; |
| 1050 | unsigned_tx.rct_signatures.p.CLSAGs[i].c1 = c_0[i]; |
| 1051 | } |
| 1052 | |
| 1053 | return true; |
| 1054 | } |
| 1055 | //---------------------------------------------------------------------------------------------------------------------- |
| 1056 | } //namespace signing |
| 1057 | |
| 1058 | } //namespace multisig |