| 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 | |
| 30 | #pragma once |
| 31 | |
| 32 | #include "ringct/rctTypes.h" |
| 33 | |
| 34 | #include <set> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace cryptonote { |
| 38 | |
| 39 | class transaction; |
| 40 | struct tx_source_entry; |
| 41 | struct tx_destination_entry; |
| 42 | struct account_keys; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | namespace multisig { |
| 47 | |
| 48 | namespace signing { |
| 49 | |
| 50 | class CLSAG_context_t; |
| 51 | |
| 52 | // number of parallel signing nonces to use per signer (2 nonces as in musig2 and FROST) |
| 53 | constexpr std::size_t kAlphaComponents = 2; |
| 54 | |
| 55 | class tx_builder_ringct_t final { |
| 56 | private: |
| 57 | // the tx builder has been initialized |
| 58 | bool initialized; |
| 59 | // the tx builder is 'reconstructing' a tx that has already been created using this object |
| 60 | bool reconstruction; |
| 61 | // cached: mu_P*(local keys and sender-receiver secret and subaddress material) + mu_C*(commitment-to-zero secret) |
| 62 | // - these are only used for the initial building of a tx (not reconstructions) |
| 63 | rct::keyV cached_w; |
| 64 | // contexts for making CLSAG challenges with multisig nonces |
| 65 | std::vector<CLSAG_context_t> CLSAG_contexts; |
| 66 | public: |
| 67 | tx_builder_ringct_t(); |
| 68 | ~tx_builder_ringct_t(); |
| 69 | |
| 70 | // prepare an unsigned transaction (and get tx privkeys for outputs) |
| 71 | bool init( |
| 72 | const cryptonote::account_keys& account_keys, |
| 73 | const std::vector<std::uint8_t>& extra, |
| 74 | const std::uint32_t subaddr_account, |
| 75 | const std::set<std::uint32_t>& subaddr_minor_indices, |
| 76 | std::vector<cryptonote::tx_source_entry>& sources, |
| 77 | std::vector<cryptonote::tx_destination_entry>& destinations, |
| 78 | const cryptonote::tx_destination_entry& change, |
| 79 | const rct::RCTConfig& rct_config, |
| 80 | const bool use_rct, |
| 81 | const bool reconstruction, |
| 82 | crypto::secret_key& tx_secret_key, |
| 83 | std::vector<crypto::secret_key>& tx_aux_secret_keys, |
| 84 | crypto::secret_key& tx_secret_key_entropy, |
| 85 | cryptonote::transaction& unsigned_tx |
| 86 | ); |
| 87 | |
| 88 | // get the first partial signature for the specified input ('source') |
| 89 | bool first_partial_sign( |
| 90 | const std::size_t source, |
| 91 | const rct::keyV& total_alpha_G, |
| 92 | const rct::keyV& total_alpha_H, |
| 93 | const rct::keyV& alpha, |
| 94 | rct::key& c_0, |
| 95 | rct::key& s |
| 96 | ); |
| 97 | |
| 98 | // get intermediate partial signatures for all the inputs |
| 99 | bool next_partial_sign( |
| 100 | const rct::keyM& total_alpha_G, |
| 101 | const rct::keyM& total_alpha_H, |
| 102 | const rct::keyM& alpha, |
| 103 | const rct::key& x, |
| 104 | rct::keyV& c_0, |
| 105 | rct::keyV& s |
| 106 | ); |
| 107 | |
| 108 | // finalize an unsigned transaction (add challenges and real responses to incomplete CLSAG signatures) |
| 109 | static bool finalize_tx( |
| 110 | const std::vector<cryptonote::tx_source_entry>& sources, |
| 111 | const rct::keyV& c_0, |
| 112 | const rct::keyV& s, |
| 113 | cryptonote::transaction& unsigned_tx |
| 114 | ); |
| 115 | }; |
| 116 | |
| 117 | } //namespace signing |
| 118 | |
| 119 | } //namespace multisig |