| 1 | // Copyright (c) 2021-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 | #pragma once |
| 30 | |
| 31 | #include "crypto/crypto.h" |
| 32 | #include "multisig_kex_msg.h" |
| 33 | |
| 34 | #include <cstdint> |
| 35 | #include <string> |
| 36 | #include <unordered_map> |
| 37 | #include <unordered_set> |
| 38 | #include <vector> |
| 39 | |
| 40 | |
| 41 | namespace multisig |
| 42 | { |
| 43 | /** |
| 44 | * multisig account: |
| 45 | * |
| 46 | * - handles account keys for an M-of-N multisig participant (M <= N; M >= 1; N >= 2) |
| 47 | * - encapsulates multisig account construction process (via key exchange [kex]) |
| 48 | * - TODO: encapsulates key preparation for aggregation-style signing |
| 49 | * |
| 50 | * :: multisig pubkey: the private key is split, M group participants are required to reassemble (e.g. to sign something) |
| 51 | * - in cryptonote, this is the multisig spend key |
| 52 | * :: multisig common pubkey: the private key is known to all participants (e.g. for authenticating as a group member) |
| 53 | * - in cryptonote, this is the multisig view key |
| 54 | * |
| 55 | * |
| 56 | * multisig key exchange: |
| 57 | * |
| 58 | * An 'M-of-N' (M <= N; M >= 1; N >= 2) multisignature key is a public key where at least 'M' out of 'N' |
| 59 | * possible co-signers must collaborate in order to create a signature. |
| 60 | * |
| 61 | * Constructing a multisig key involves a series of Diffie-Hellman exchanges between participants. |
| 62 | * At the end of key exchange (kex), each participant will hold a number of private keys. Each private |
| 63 | * key is shared by a group of (N - M + 1) participants. This way if (N - M) co-signers are missing, every |
| 64 | * private key will be held by at least one of the remaining M people. |
| 65 | * |
| 66 | * Note on MULTISIG_MAX_SIGNERS: During key exchange, participants will have up to '(N - 1) choose (N - M)' |
| 67 | * key shares. If N is large, then the max number of key shares (when M = (N-1)/2) can be huge. A limit of N <= 16 was |
| 68 | * arbitrarily chosen as a power of 2 that can accomodate the vast majority of practical use-cases. To increase the |
| 69 | * limit, FROST-style key aggregation should be used instead (it is more efficient than DH-based key generation |
| 70 | * when N - M > 1). |
| 71 | * |
| 72 | * - Further reading |
| 73 | * - MRL-0009: https://www.getmonero.org/resources/research-lab/pubs/MRL-0009.pdf |
| 74 | * - MuSig2: https://eprint.iacr.org/2020/1261 |
| 75 | * - ZtM2: https://web.getmonero.org/library/Zero-to-Monero-2-0-0.pdf Ch. 9, especially Section 9.6.3 |
| 76 | * - FROST: https://eprint.iacr.org/2018/417 |
| 77 | */ |
| 78 | using multisig_keyset_map_memsafe_t = |
| 79 | std::unordered_map<crypto::public_key_memsafe, std::unordered_set<crypto::public_key>>; |
| 80 | |
| 81 | class multisig_account final |
| 82 | { |
| 83 | public: |
| 84 | //constructors |
| 85 | // default constructor |
| 86 | multisig_account() = default; |
| 87 | |
| 88 | /** |
| 89 | * construct from base privkeys |
| 90 | * |
| 91 | * - prepares a kex msg for the first round of multisig key construction. |
| 92 | * - the local account's kex msgs are signed with the base_privkey |
| 93 | * - the first kex msg transmits the local base_common_privkey to other participants, for creating the group's common_privkey |
| 94 | */ |
| 95 | multisig_account(const crypto::secret_key &base_privkey, |
| 96 | const crypto::secret_key &base_common_privkey); |
| 97 | |
| 98 | // reconstruct from full account details (not recommended) |
| 99 | multisig_account(const std::uint32_t threshold, |
| 100 | std::vector<crypto::public_key> signers, |
| 101 | const crypto::secret_key &base_privkey, |
| 102 | const crypto::secret_key &base_common_privkey, |
| 103 | std::vector<crypto::secret_key> multisig_privkeys, |
| 104 | const crypto::secret_key &common_privkey, |
| 105 | const crypto::public_key &multisig_pubkey, |
| 106 | const crypto::public_key &common_pubkey, |
| 107 | const std::uint32_t kex_rounds_complete, |
| 108 | multisig_keyset_map_memsafe_t kex_origins_map, |
| 109 | std::string next_round_kex_message); |
| 110 | |
| 111 | // copy constructor: default |
| 112 | |
| 113 | //destructor: default |
| 114 | ~multisig_account() = default; |
| 115 | |
| 116 | //overloaded operators: none |
| 117 | |
| 118 | //getters |
| 119 | // get threshold |
| 120 | std::uint32_t get_threshold() const { return m_threshold; } |
| 121 | // get signers |
| 122 | const std::vector<crypto::public_key>& get_signers() const { return m_signers; } |
| 123 | // get base privkey |
| 124 | const crypto::secret_key& get_base_privkey() const { return m_base_privkey; } |
| 125 | // get base pubkey |
| 126 | const crypto::public_key& get_base_pubkey() const { return m_base_pubkey; } |
| 127 | // get base common privkey |
| 128 | const crypto::secret_key& get_base_common_privkey() const { return m_base_common_privkey; } |
| 129 | // get multisig privkeys |
| 130 | const std::vector<crypto::secret_key>& get_multisig_privkeys() const { return m_multisig_privkeys; } |
| 131 | // get common privkey |
| 132 | const crypto::secret_key& get_common_privkey() const { return m_common_privkey; } |
| 133 | // get multisig pubkey |
| 134 | const crypto::public_key& get_multisig_pubkey() const { return m_multisig_pubkey; } |
| 135 | // get common pubkey |
| 136 | const crypto::public_key& get_common_pubkey() const { return m_common_pubkey; } |
| 137 | // get kex rounds complete |
| 138 | std::uint32_t get_kex_rounds_complete() const { return m_kex_rounds_complete; } |
| 139 | // get kex keys to origins map |
| 140 | const multisig_keyset_map_memsafe_t& get_kex_keys_to_origins_map() const { return m_kex_keys_to_origins_map; } |
| 141 | // get the kex msg for the next round |
| 142 | const std::string& get_next_kex_round_msg() const { return m_next_round_kex_message; } |
| 143 | |
| 144 | //account status functions |
| 145 | // account has been intialized, and the account holder can use the 'common' key |
| 146 | bool account_is_active() const; |
| 147 | // account has gone through main kex rounds, only remaining step is to verify all other participants are ready |
| 148 | bool main_kex_rounds_done() const; |
| 149 | // account is ready to make multisig signatures |
| 150 | bool multisig_is_ready() const; |
| 151 | |
| 152 | //account helpers |
| 153 | private: |
| 154 | // set the threshold (M) and signers (N) |
| 155 | void set_multisig_config(const std::size_t threshold, std::vector<crypto::public_key> signers); |
| 156 | |
| 157 | //account mutators: key exchange to set up account |
| 158 | public: |
| 159 | /** |
| 160 | * brief: initialize_kex - initialize key exchange |
| 161 | * - Updates the account with a 'transactional' model. This account will only be mutated if the update succeeds. |
| 162 | */ |
| 163 | void initialize_kex(const std::uint32_t threshold, |
| 164 | std::vector<crypto::public_key> signers, |
| 165 | const std::vector<multisig_kex_msg> &expanded_msgs_rnd1); |
| 166 | /** |
| 167 | * brief: kex_update - Complete the 'in progress' kex round and set the kex message for the next round. |
| 168 | * - Updates the account with a 'transactional' model. This account will only be mutated if the update succeeds. |
| 169 | * - The main interface for multisig key exchange, this handles all the work of processing input messages, |
| 170 | * creating new messages for new rounds, and finalizing the multisig shared public key when kex is complete. |
| 171 | * param: expanded_msgs - kex messages corresponding to the account's 'in progress' round |
| 172 | * param: force_update_use_with_caution - try to force the account to update with messages from an incomplete signer set. |
| 173 | * - If this is the post-kex verification round, only require one input message. |
| 174 | * - Force updating here should only be done if we can safely assume an honest signer subgroup of size 'threshold' |
| 175 | * will complete the account. |
| 176 | * - If this is an intermediate round, only require messages from 'num signers - 1 - (round - 1)' other signers. |
| 177 | * - If force updating with maliciously-crafted messages, the resulting account will be invalid (either unable |
| 178 | * to complete signatures, or a 'hostage' to the malicious signer [i.e. can't sign without his participation]). |
| 179 | */ |
| 180 | void kex_update(const std::vector<multisig_kex_msg> &expanded_msgs, |
| 181 | const bool force_update_use_with_caution = false); |
| 182 | |
| 183 | private: |
| 184 | // implementation of kex_update() (non-transactional) |
| 185 | void kex_update_impl(const std::vector<multisig_kex_msg> &expanded_msgs, const bool incomplete_signer_set); |
| 186 | /** |
| 187 | * brief: initialize_kex_update - Helper for kex_update_impl() |
| 188 | * - Collect the local signer's shared keys to ignore in incoming messages, build the aggregate ancillary key |
| 189 | * if appropriate. |
| 190 | * param: expanded_msgs - set of multisig kex messages to process |
| 191 | * param: kex_rounds_required - number of rounds required for kex (not including post-kex verification round) |
| 192 | * outparam: exclude_pubkeys_out - keys held by the local account corresponding to round 'current_round' |
| 193 | * - If 'current_round' is the final round, these are the local account's shares of the final aggregate key. |
| 194 | */ |
| 195 | void initialize_kex_update(const std::vector<multisig_kex_msg> &expanded_msgs, |
| 196 | const std::uint32_t kex_rounds_required, |
| 197 | std::vector<crypto::public_key> &exclude_pubkeys_out); |
| 198 | /** |
| 199 | * brief: finalize_kex_update - Helper for kex_update_impl() |
| 200 | * param: kex_rounds_required - number of rounds required for kex (not including post-kex verification round) |
| 201 | * param: result_keys_to_origins_map - map between keys for the next round and the other participants they correspond to |
| 202 | * inoutparam: temp_account_inout - account to perform last update steps on |
| 203 | */ |
| 204 | void finalize_kex_update(const std::uint32_t kex_rounds_required, |
| 205 | multisig_keyset_map_memsafe_t result_keys_to_origins_map); |
| 206 | |
| 207 | //member variables |
| 208 | private: |
| 209 | /// misc. account details |
| 210 | // [M] minimum number of co-signers to sign a message with the aggregate pubkey |
| 211 | std::uint32_t m_threshold{0}; |
| 212 | // [N] base keys of all participants in the multisig (used to initiate key exchange, and as participant ids for msg signing) |
| 213 | std::vector<crypto::public_key> m_signers; |
| 214 | |
| 215 | /// local participant's personal keys |
| 216 | // base keypair of the participant |
| 217 | // - used for signing messages, as the initial base key for key exchange, and to make DH derivations for key exchange |
| 218 | crypto::secret_key m_base_privkey; |
| 219 | crypto::public_key m_base_pubkey; |
| 220 | // common base privkey, used to produce the aggregate common privkey |
| 221 | crypto::secret_key m_base_common_privkey; |
| 222 | |
| 223 | /// core multisig account keys |
| 224 | // the account's private key shares of the multisig address |
| 225 | // TODO: also record which other signers have these privkeys, to enable aggregation signing (instead of round-robin) |
| 226 | std::vector<crypto::secret_key> m_multisig_privkeys; |
| 227 | // a privkey owned by all multisig participants (e.g. a cryptonote view key) |
| 228 | crypto::secret_key m_common_privkey; |
| 229 | // the multisig public key (e.g. a cryptonote spend key) |
| 230 | crypto::public_key m_multisig_pubkey; |
| 231 | // the common public key (e.g. a view spend key) |
| 232 | crypto::public_key m_common_pubkey; |
| 233 | |
| 234 | /// kex variables |
| 235 | // number of key exchange rounds that have been completed (all messages for the round collected and processed) |
| 236 | std::uint32_t m_kex_rounds_complete{0}; |
| 237 | // this account's pubkeys for the in-progress key exchange round |
| 238 | // - either DH derivations (intermediate rounds), H(derivation)*G (final round), empty (when kex is done) |
| 239 | multisig_keyset_map_memsafe_t m_kex_keys_to_origins_map; |
| 240 | // the account's message for the in-progress key exchange round |
| 241 | std::string m_next_round_kex_message; |
| 242 | }; |
| 243 | |
| 244 | /** |
| 245 | * brief: multisig_kex_rounds_required - The number of key exchange rounds required to produce an M-of-N shared key. |
| 246 | * - Key exchange (kex) is a synchronous series of 'rounds'. In an 'active round', participants send messages |
| 247 | * to each other. |
| 248 | * - A participant considers a round 'complete' when they have collected sufficient messages |
| 249 | * from other participants, processed those messages, and updated their multisig account state. |
| 250 | * - Typically (as implemented in this module), completing a round coincides with making a message for the next round. |
| 251 | * param: num_signers - number of participants in multisig (N) |
| 252 | * param: threshold - threshold of multisig (M) |
| 253 | * return: number of kex rounds required |
| 254 | */ |
| 255 | std::uint32_t multisig_kex_rounds_required(const std::uint32_t num_signers, const std::uint32_t threshold); |
| 256 | |
| 257 | /** |
| 258 | * brief: multisig_setup_rounds_required - The number of setup rounds required to produce an M-of-N shared key. |
| 259 | * - A participant must complete all kex rounds and 1 initialization round. |
| 260 | * param: num_signers - number of participants in multisig (N) |
| 261 | * param: threshold - threshold of multisig (M) |
| 262 | * return: number of setup rounds required |
| 263 | */ |
| 264 | std::uint32_t multisig_setup_rounds_required(const std::uint32_t num_signers, const std::uint32_t threshold); |
| 265 | } //namespace multisig |