| 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 | #include "crypto/crypto.h" |
| 30 | #include "cryptonote_basic/account.h" |
| 31 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 32 | #include "cryptonote_config.h" |
| 33 | #include "include_base_utils.h" |
| 34 | #include "multisig.h" |
| 35 | #include "ringct/rctOps.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | #include <unordered_map> |
| 39 | #include <unordered_set> |
| 40 | #include <vector> |
| 41 | |
| 42 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 43 | #define MONERO_DEFAULT_LOG_CATEGORY "multisig" |
| 44 | |
| 45 | namespace multisig |
| 46 | { |
| 47 | //---------------------------------------------------------------------------------------------------------------------- |
| 48 | crypto::secret_key get_multisig_blinded_secret_key(const crypto::secret_key &key) |
| 49 | { |
| 50 | CHECK_AND_ASSERT_THROW_MES(key != crypto::null_skey, "Unexpected null secret key (danger!)."); |
| 51 | |
| 52 | rct::key multisig_salt; |
| 53 | static_assert(sizeof(rct::key) == sizeof(config::HASH_KEY_MULTISIG), "Hash domain separator is an unexpected size"); |
| 54 | memcpy(multisig_salt.bytes, config::HASH_KEY_MULTISIG, sizeof(rct::key)); |
| 55 | |
| 56 | // private key = H(key, domain-sep) |
| 57 | rct::keyV data; |
| 58 | data.reserve(2); |
| 59 | data.push_back(rct::sk2rct(key)); |
| 60 | data.push_back(multisig_salt); |
| 61 | crypto::secret_key result = rct::rct2sk(rct::hash_to_scalar(data)); |
| 62 | memwipe(&data[0], sizeof(rct::key)); |
| 63 | return result; |
| 64 | } |
| 65 | //---------------------------------------------------------------------------------------------------------------------- |
| 66 | bool generate_multisig_key_image(const cryptonote::account_keys &keys, |
| 67 | std::size_t multisig_key_index, |
| 68 | const crypto::public_key& out_key, |
| 69 | crypto::key_image& ki) |
| 70 | { |
| 71 | if (multisig_key_index >= keys.m_multisig_keys.size()) |
| 72 | return false; |
| 73 | crypto::generate_key_image(out_key, keys.m_multisig_keys[multisig_key_index], ki); |
| 74 | return true; |
| 75 | } |
| 76 | //---------------------------------------------------------------------------------------------------------------------- |
| 77 | void generate_multisig_LR(const crypto::public_key pkey, |
| 78 | const crypto::secret_key &k, |
| 79 | crypto::public_key &L, |
| 80 | crypto::public_key &R) |
| 81 | { |
| 82 | rct::scalarmultBase((rct::key&)L, rct::sk2rct(k)); |
| 83 | crypto::generate_key_image(pkey, k, (crypto::key_image&)R); |
| 84 | } |
| 85 | //---------------------------------------------------------------------------------------------------------------------- |
| 86 | bool generate_multisig_composite_key_image(const cryptonote::account_keys &keys, |
| 87 | const std::unordered_map<crypto::public_key, cryptonote::subaddress_index> &subaddresses, |
| 88 | const crypto::public_key &out_key, |
| 89 | const crypto::public_key &tx_public_key, |
| 90 | const std::vector<crypto::public_key> &additional_tx_public_keys, |
| 91 | std::size_t real_output_index, |
| 92 | const std::vector<crypto::key_image> &pkis, |
| 93 | crypto::key_image &ki) |
| 94 | { |
| 95 | // create a multisig partial key image |
| 96 | // KI_partial = ([view key component] + [subaddress component] + [multisig privkeys]) * Hp(output one-time address) |
| 97 | // - the 'multisig priv keys' here are those held by the local account |
| 98 | // - later, we add in the components held by other participants |
| 99 | cryptonote::keypair in_ephemeral; |
| 100 | if (!cryptonote::generate_key_image_helper(keys, subaddresses, out_key, tx_public_key, additional_tx_public_keys, real_output_index, in_ephemeral, ki, keys.get_device())) |
| 101 | return false; |
| 102 | std::unordered_set<crypto::key_image> used; |
| 103 | |
| 104 | // create a key image component for each of the local account's multisig private keys |
| 105 | for (std::size_t m = 0; m < keys.m_multisig_keys.size(); ++m) |
| 106 | { |
| 107 | crypto::key_image pki; |
| 108 | // pki = keys.m_multisig_keys[m] * Hp(out_key) |
| 109 | // pki = key image component |
| 110 | // out_key = one-time address of an output owned by the multisig group |
| 111 | bool r = generate_multisig_key_image(keys, m, out_key, pki); |
| 112 | if (!r) |
| 113 | return false; |
| 114 | |
| 115 | // this KI component is 'used' because it was included in the partial key image 'ki' above |
| 116 | used.insert(pki); |
| 117 | } |
| 118 | |
| 119 | // add the KI components from other participants to the partial KI |
| 120 | // if they not included yet |
| 121 | for (const auto &pki: pkis) |
| 122 | { |
| 123 | if (used.find(pki) == used.end()) |
| 124 | { |
| 125 | // ignore components that have already been 'used' |
| 126 | used.insert(pki); |
| 127 | |
| 128 | // KI_partial = KI_partial + KI_component[...] |
| 129 | rct::addKeys((rct::key&)ki, rct::ki2rct(ki), rct::ki2rct(pki)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // at the end, 'ki' will hold the true key image for our output if inputs were sufficient |
| 134 | // - if 'pkis' (the other participants' KI components) is missing some components |
| 135 | // then 'ki' will not be complete |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | //---------------------------------------------------------------------------------------------------------------------- |
| 140 | } //namespace multisig |