From 33242dff4797509f5156bc9757fb69dae79eb9ef Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 8 Dec 2020 22:31:54 -0400 Subject: [PATCH] Replace `keypair::generate` with a `keypair` constructor taking a hwdev This makes it a bit nicer, and allows in-place construction rather than needing to construct-and-copy. --- src/cryptonote_basic/cryptonote_basic.h | 18 ++++++++++++------ src/cryptonote_core/cryptonote_tx_utils.cpp | 4 ++-- src/device/device_default.cpp | 2 +- tests/core_tests/block_validation.cpp | 2 +- tests/core_tests/chaingen.h | 4 ++-- tests/core_tests/loki_tests.cpp | 12 ++++++------ tests/core_tests/tx_validation.cpp | 6 +++--- tests/performance_tests/generate_keypair.h | 2 +- tests/performance_tests/signature.h | 2 +- tests/unit_tests/account.cpp | 2 +- tests/unit_tests/ringdb.cpp | 2 +- tests/unit_tests/service_nodes.cpp | 16 ++++++++-------- tests/unit_tests/service_nodes_swarm.cpp | 2 +- 13 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/cryptonote_basic/cryptonote_basic.h b/src/cryptonote_basic/cryptonote_basic.h index e051fe3bb..ad39a8412 100644 --- a/src/cryptonote_basic/cryptonote_basic.h +++ b/src/cryptonote_basic/cryptonote_basic.h @@ -471,12 +471,18 @@ namespace cryptonote crypto::public_key pub; crypto::secret_key sec; - static inline keypair generate(hw::device &hwdev) - { - keypair k; - hwdev.generate_keys(k.pub, k.sec); - return k; - } + keypair() = default; + + // Constructs from a copied public/secret key + keypair(const crypto::public_key& pub, const crypto::secret_key& sec) : pub{pub}, sec{sec} {} + // Default copy and move + keypair(const keypair&) = default; + keypair(keypair&&) = default; + keypair& operator=(const keypair&) = default; + keypair& operator=(keypair&&) = default; + + // Constructs by generating a keypair via the given hardware device: + explicit keypair(hw::device& hwdev) { hwdev.generate_keys(pub, sec); } }; using byte_and_output_fees = std::pair; diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp index 2a0e9eaff..9631216cf 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.cpp +++ b/src/cryptonote_core/cryptonote_tx_utils.cpp @@ -273,7 +273,7 @@ namespace cryptonote tx.type = txtype::standard; tx.version = transaction::get_max_version_for_hf(hard_fork_version); - keypair const txkey = keypair::generate(hw::get_device("default")); + keypair const txkey{hw::get_device("default")}; keypair const gov_key = get_deterministic_keypair_from_height(height); // NOTE: Always need since we use same key for service node // NOTE: TX Extra @@ -1016,7 +1016,7 @@ namespace cryptonote { additional_tx_keys.clear(); for (const auto &d: destinations) - additional_tx_keys.push_back(keypair::generate(sender_account_keys.get_device()).sec); + additional_tx_keys.push_back(keypair{sender_account_keys.get_device()}.sec); } bool r = construct_tx_with_tx_key(sender_account_keys, subaddresses, sources, destinations, change_addr, extra, tx, unlock_time, tx_key, additional_tx_keys, rct_config, msout, true /*shuffle_outs*/, tx_params); diff --git a/src/device/device_default.cpp b/src/device/device_default.cpp index 3140bfc81..49049f4cb 100644 --- a/src/device/device_default.cpp +++ b/src/device/device_default.cpp @@ -303,7 +303,7 @@ namespace hw { } bool device_default::open_tx(crypto::secret_key &tx_key, cryptonote::txversion /*version*/, cryptonote::txtype /*type*/) { - cryptonote::keypair txkey = cryptonote::keypair::generate(*this); + cryptonote::keypair txkey{*this}; tx_key = txkey.sec; return true; } diff --git a/tests/core_tests/block_validation.cpp b/tests/core_tests/block_validation.cpp index 7f68191e6..2fc0df6f1 100644 --- a/tests/core_tests/block_validation.cpp +++ b/tests/core_tests/block_validation.cpp @@ -425,7 +425,7 @@ static bool construct_miner_tx_with_extra_output(cryptonote::transaction& tx, uint64_t already_generated_coins, const cryptonote::account_public_address& extra_address) { - keypair txkey = keypair::generate(hw::get_device("default")); + keypair txkey{hw::get_device("default")}; add_tx_extra(tx, txkey.pub); keypair gov_key = get_deterministic_keypair_from_height(height); diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index 111478746..f369310b2 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -1464,14 +1464,14 @@ struct loki_chain_generator cryptonote::transaction create_and_add_loki_name_system_tx_renew(cryptonote::account_base const &src, uint8_t hf_version, lns::mapping_type type, std::string const &name, bool kept_by_block = false); cryptonote::transaction create_and_add_tx (const cryptonote::account_base& src, const cryptonote::account_public_address& dest, uint64_t amount, uint64_t fee = TESTS_DEFAULT_FEE, bool kept_by_block = false); cryptonote::transaction create_and_add_state_change_tx(service_nodes::new_state state, const crypto::public_key& pub_key, uint64_t height = -1, const std::vector& voters = {}, uint64_t fee = 0, bool kept_by_block = false); - cryptonote::transaction create_and_add_registration_tx(const cryptonote::account_base& src, const cryptonote::keypair& sn_keys = cryptonote::keypair::generate(hw::get_device("default")), bool kept_by_block = false); + cryptonote::transaction create_and_add_registration_tx(const cryptonote::account_base& src, const cryptonote::keypair& sn_keys = cryptonote::keypair{hw::get_device("default")}, bool kept_by_block = false); cryptonote::transaction create_and_add_staking_tx (const crypto::public_key &pub_key, const cryptonote::account_base &src, uint64_t amount, bool kept_by_block = false); loki_blockchain_entry &create_and_add_next_block (const std::vector& txs = {}, cryptonote::checkpoint_t const *checkpoint = nullptr, bool can_be_added_to_blockchain = true, std::string const &fail_msg = {}); // NOTE: Create transactions but don't add to events_ cryptonote::transaction create_tx(const cryptonote::account_base &src, const cryptonote::account_public_address &dest, uint64_t amount, uint64_t fee) const; cryptonote::transaction create_registration_tx(const cryptonote::account_base &src, - const cryptonote::keypair &service_node_keys = cryptonote::keypair::generate(hw::get_device("default")), + const cryptonote::keypair &service_node_keys = cryptonote::keypair{hw::get_device("default")}, uint64_t src_portions = STAKING_PORTIONS, uint64_t src_operator_cut = 0, std::array const &contributors = {}, diff --git a/tests/core_tests/loki_tests.cpp b/tests/core_tests/loki_tests.cpp index 786719788..e6d54374d 100644 --- a/tests/core_tests/loki_tests.cpp +++ b/tests/core_tests/loki_tests.cpp @@ -347,7 +347,7 @@ bool loki_checkpointing_service_node_checkpoint_from_votes::generate(std::vector // NOTE: Submit invalid vote using service node keys not in the quorum { - const cryptonote::keypair invalid_kp = cryptonote::keypair::generate(hw::get_device("default")); + const cryptonote::keypair invalid_kp{hw::get_device("default")}; service_nodes::service_node_keys invalid_keys; invalid_keys.pub = invalid_kp.pub; invalid_keys.key = invalid_kp.sec; @@ -500,7 +500,7 @@ bool loki_core_block_reward_unpenalized_post_pulse::generate(std::vector(m_tx, m_tx_key.pub); } @@ -304,7 +304,7 @@ bool gen_tx_no_inputs_no_outputs::generate(std::vector& events transaction tx = {}; tx.version = cryptonote::txversion::v2_ringct; - add_tx_extra(tx, keypair::generate(hw::get_device("default")).pub); + add_tx_extra(tx, keypair{hw::get_device("default")}.pub); DO_CALLBACK(events, "mark_invalid_tx"); events.push_back(tx); @@ -535,7 +535,7 @@ bool gen_tx_key_image_not_derive_from_tx_key::generate(std::vector(tx.vin.front()); // Use fake key image - keypair keys = keypair::generate(hw::get_device("default")); + keypair keys{hw::get_device("default")}; key_image fake_key_image; crypto::generate_key_image(keys.pub, keys.sec, fake_key_image); in_to_key.k_image = fake_key_image; diff --git a/tests/performance_tests/generate_keypair.h b/tests/performance_tests/generate_keypair.h index 91c830166..67d0f8802 100644 --- a/tests/performance_tests/generate_keypair.h +++ b/tests/performance_tests/generate_keypair.h @@ -45,7 +45,7 @@ public: bool test() { - cryptonote::keypair::generate(hw::get_device("default")); + cryptonote::keypair x{hw::get_device("default")}; return true; } }; diff --git a/tests/performance_tests/signature.h b/tests/performance_tests/signature.h index 21110b525..8f399c6c6 100644 --- a/tests/performance_tests/signature.h +++ b/tests/performance_tests/signature.h @@ -47,7 +47,7 @@ public: return false; message = crypto::rand(); - keys = cryptonote::keypair::generate(hw::get_device("default")); + keys = cryptonote::keypair{hw::get_device("default")}; crypto::generate_signature(message, keys.pub, keys.sec, m_signature); return true; diff --git a/tests/unit_tests/account.cpp b/tests/unit_tests/account.cpp index 113622b5e..2232d3996 100644 --- a/tests/unit_tests/account.cpp +++ b/tests/unit_tests/account.cpp @@ -32,7 +32,7 @@ TEST(account, encrypt_keys) { - cryptonote::keypair recovery_key = cryptonote::keypair::generate(hw::get_device("default")); + cryptonote::keypair recovery_key{hw::get_device("default")}; cryptonote::account_base account; crypto::secret_key key = account.generate(recovery_key.sec); const cryptonote::account_keys keys = account.get_keys(); diff --git a/tests/unit_tests/ringdb.cpp b/tests/unit_tests/ringdb.cpp index 971b9a2ee..dca4194b6 100644 --- a/tests/unit_tests/ringdb.cpp +++ b/tests/unit_tests/ringdb.cpp @@ -58,7 +58,7 @@ crypto::chacha_key generate_chacha_key() crypto::key_image generate_key_image() { crypto::key_image key_image; - cryptonote::keypair keypair = cryptonote::keypair::generate(hw::get_device("default")); + cryptonote::keypair keypair{hw::get_device("default")}; crypto::generate_key_image(keypair.pub, keypair.sec, key_image); return key_image; } diff --git a/tests/unit_tests/service_nodes.cpp b/tests/unit_tests/service_nodes.cpp index fbcdc58dc..37802e52e 100644 --- a/tests/unit_tests/service_nodes.cpp +++ b/tests/unit_tests/service_nodes.cpp @@ -130,7 +130,7 @@ static bool verify_vote(service_nodes::quorum_vote_t const &vote, TEST(service_nodes, vote_validation) { // Generate a quorum and the voter - cryptonote::keypair service_node_voter = cryptonote::keypair::generate(hw::get_device("default")); + cryptonote::keypair service_node_voter{hw::get_device("default")}; int voter_index = 0; service_nodes::service_node_keys voter_keys; @@ -144,8 +144,8 @@ TEST(service_nodes, vote_validation) for (size_t i = 0; i < state.validators.size(); ++i) { - state.validators[i] = (i == voter_index) ? service_node_voter.pub : cryptonote::keypair::generate(hw::get_device("default")).pub; - state.workers[i] = cryptonote::keypair::generate(hw::get_device("default")).pub; + state.validators[i] = (i == voter_index) ? service_node_voter.pub : cryptonote::keypair{hw::get_device("default")}.pub; + state.workers[i] = cryptonote::keypair{hw::get_device("default")}.pub; } } @@ -185,9 +185,9 @@ TEST(service_nodes, vote_validation) // Signature not valid { - auto vote = valid_vote; - cryptonote::keypair other_voter = cryptonote::keypair::generate(hw::get_device("default")); - vote.signature = {}; + auto vote = valid_vote; + cryptonote::keypair other_voter{hw::get_device("default")}; + vote.signature = {}; cryptonote::vote_verification_context vvc = {}; bool result = verify_vote(vote, block_height, vvc, state); @@ -221,11 +221,11 @@ TEST(service_nodes, tx_extra_state_change_validation) for (size_t i = 0; i < state.validators.size(); ++i) { - cryptonote::keypair voter = cryptonote::keypair::generate(hw::get_device("default")); + cryptonote::keypair voter{hw::get_device("default")}; voters[i].pub = voter.pub; voters[i].key = voter.sec; state.validators[i] = voters[i].pub; - state.workers[i] = cryptonote::keypair::generate(hw::get_device("default")).pub; + state.workers[i] = cryptonote::keypair{hw::get_device("default")}.pub; } } diff --git a/tests/unit_tests/service_nodes_swarm.cpp b/tests/unit_tests/service_nodes_swarm.cpp index 230276db2..82d1b673c 100644 --- a/tests/unit_tests/service_nodes_swarm.cpp +++ b/tests/unit_tests/service_nodes_swarm.cpp @@ -41,7 +41,7 @@ using namespace service_nodes; crypto::public_key newPubKey() { - return cryptonote::keypair::generate(hw::get_device("default")).pub; + return cryptonote::keypair{hw::get_device("default")}.pub; }; size_t calculateExcess(const swarm_snode_map_t& swarm_to_snodes) {