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.
This commit is contained in:
Jason Rhinelander 2020-12-08 22:31:54 -04:00
parent 0450f3dad6
commit 33242dff47
13 changed files with 40 additions and 34 deletions

View file

@ -471,12 +471,18 @@ namespace cryptonote
crypto::public_key pub; crypto::public_key pub;
crypto::secret_key sec; crypto::secret_key sec;
static inline keypair generate(hw::device &hwdev) keypair() = default;
{
keypair k; // Constructs from a copied public/secret key
hwdev.generate_keys(k.pub, k.sec); keypair(const crypto::public_key& pub, const crypto::secret_key& sec) : pub{pub}, sec{sec} {}
return k; // 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<uint64_t, uint64_t>; using byte_and_output_fees = std::pair<uint64_t, uint64_t>;

View file

@ -273,7 +273,7 @@ namespace cryptonote
tx.type = txtype::standard; tx.type = txtype::standard;
tx.version = transaction::get_max_version_for_hf(hard_fork_version); 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 keypair const gov_key = get_deterministic_keypair_from_height(height); // NOTE: Always need since we use same key for service node
// NOTE: TX Extra // NOTE: TX Extra
@ -1016,7 +1016,7 @@ namespace cryptonote
{ {
additional_tx_keys.clear(); additional_tx_keys.clear();
for (const auto &d: destinations) 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); 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);

View file

@ -303,7 +303,7 @@ namespace hw {
} }
bool device_default::open_tx(crypto::secret_key &tx_key, cryptonote::txversion /*version*/, cryptonote::txtype /*type*/) { 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; tx_key = txkey.sec;
return true; return true;
} }

View file

@ -425,7 +425,7 @@ static bool construct_miner_tx_with_extra_output(cryptonote::transaction& tx,
uint64_t already_generated_coins, uint64_t already_generated_coins,
const cryptonote::account_public_address& extra_address) 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_extra_pub_key>(tx, txkey.pub); add_tx_extra<tx_extra_pub_key>(tx, txkey.pub);
keypair gov_key = get_deterministic_keypair_from_height(height); keypair gov_key = get_deterministic_keypair_from_height(height);

View file

@ -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_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_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<uint64_t>& voters = {}, uint64_t fee = 0, 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<uint64_t>& 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); 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<cryptonote::transaction>& txs = {}, cryptonote::checkpoint_t const *checkpoint = nullptr, bool can_be_added_to_blockchain = true, std::string const &fail_msg = {}); loki_blockchain_entry &create_and_add_next_block (const std::vector<cryptonote::transaction>& 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_ // 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_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, 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_portions = STAKING_PORTIONS,
uint64_t src_operator_cut = 0, uint64_t src_operator_cut = 0,
std::array<loki_service_node_contribution, 3> const &contributors = {}, std::array<loki_service_node_contribution, 3> const &contributors = {},

View file

@ -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 // 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; service_nodes::service_node_keys invalid_keys;
invalid_keys.pub = invalid_kp.pub; invalid_keys.pub = invalid_kp.pub;
invalid_keys.key = invalid_kp.sec; invalid_keys.key = invalid_kp.sec;
@ -500,7 +500,7 @@ bool loki_core_block_reward_unpenalized_post_pulse::generate(std::vector<test_ev
} }
txs[i] = gen.create_registration_tx(gen.first_miner(), txs[i] = gen.create_registration_tx(gen.first_miner(),
cryptonote::keypair::generate(hw::get_device("default")), cryptonote::keypair{hw::get_device("default")},
STAKING_PORTIONS / 4, /*operator portions*/ STAKING_PORTIONS / 4, /*operator portions*/
0, /*operator cut*/ 0, /*operator cut*/
contributions, contributions,
@ -2976,10 +2976,10 @@ bool loki_service_nodes_insufficient_contribution::generate(std::vector<test_eve
gen.add_blocks_until_version(hard_forks.back().first); gen.add_blocks_until_version(hard_forks.back().first);
gen.add_mined_money_unlock_blocks(); gen.add_mined_money_unlock_blocks();
uint64_t operator_portions = STAKING_PORTIONS / 2; uint64_t operator_portions = STAKING_PORTIONS / 2;
uint64_t remaining_portions = STAKING_PORTIONS - operator_portions; uint64_t remaining_portions = STAKING_PORTIONS - operator_portions;
cryptonote::keypair sn_keys = cryptonote::keypair::generate(hw::get_device("default")); cryptonote::keypair sn_keys{hw::get_device("default")};
cryptonote::transaction register_tx = gen.create_registration_tx(gen.first_miner_, sn_keys, operator_portions); cryptonote::transaction register_tx = gen.create_registration_tx(gen.first_miner_, sn_keys, operator_portions);
gen.add_tx(register_tx); gen.add_tx(register_tx);
gen.create_and_add_next_block({register_tx}); gen.create_and_add_next_block({register_tx});

View file

@ -48,7 +48,7 @@ namespace
m_tx.version = version; m_tx.version = version;
m_tx.unlock_time = unlock_time; m_tx.unlock_time = unlock_time;
m_tx_key = keypair::generate(hw::get_device("default")); m_tx_key = keypair{hw::get_device("default")};
add_tx_extra<tx_extra_pub_key>(m_tx, m_tx_key.pub); add_tx_extra<tx_extra_pub_key>(m_tx, m_tx_key.pub);
} }
@ -304,7 +304,7 @@ bool gen_tx_no_inputs_no_outputs::generate(std::vector<test_event_entry>& events
transaction tx = {}; transaction tx = {};
tx.version = cryptonote::txversion::v2_ringct; tx.version = cryptonote::txversion::v2_ringct;
add_tx_extra<tx_extra_pub_key>(tx, keypair::generate(hw::get_device("default")).pub); add_tx_extra<tx_extra_pub_key>(tx, keypair{hw::get_device("default")}.pub);
DO_CALLBACK(events, "mark_invalid_tx"); DO_CALLBACK(events, "mark_invalid_tx");
events.push_back(tx); events.push_back(tx);
@ -535,7 +535,7 @@ bool gen_tx_key_image_not_derive_from_tx_key::generate(std::vector<test_event_en
txin_to_key& in_to_key = var::get<txin_to_key>(tx.vin.front()); txin_to_key& in_to_key = var::get<txin_to_key>(tx.vin.front());
// Use fake key image // Use fake key image
keypair keys = keypair::generate(hw::get_device("default")); keypair keys{hw::get_device("default")};
key_image fake_key_image; key_image fake_key_image;
crypto::generate_key_image(keys.pub, keys.sec, fake_key_image); crypto::generate_key_image(keys.pub, keys.sec, fake_key_image);
in_to_key.k_image = fake_key_image; in_to_key.k_image = fake_key_image;

View file

@ -45,7 +45,7 @@ public:
bool test() bool test()
{ {
cryptonote::keypair::generate(hw::get_device("default")); cryptonote::keypair x{hw::get_device("default")};
return true; return true;
} }
}; };

View file

@ -47,7 +47,7 @@ public:
return false; return false;
message = crypto::rand<crypto::hash>(); message = crypto::rand<crypto::hash>();
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); crypto::generate_signature(message, keys.pub, keys.sec, m_signature);
return true; return true;

View file

@ -32,7 +32,7 @@
TEST(account, encrypt_keys) 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; cryptonote::account_base account;
crypto::secret_key key = account.generate(recovery_key.sec); crypto::secret_key key = account.generate(recovery_key.sec);
const cryptonote::account_keys keys = account.get_keys(); const cryptonote::account_keys keys = account.get_keys();

View file

@ -58,7 +58,7 @@ crypto::chacha_key generate_chacha_key()
crypto::key_image generate_key_image() crypto::key_image generate_key_image()
{ {
crypto::key_image 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); crypto::generate_key_image(keypair.pub, keypair.sec, key_image);
return key_image; return key_image;
} }

View file

@ -130,7 +130,7 @@ static bool verify_vote(service_nodes::quorum_vote_t const &vote,
TEST(service_nodes, vote_validation) TEST(service_nodes, vote_validation)
{ {
// Generate a quorum and the voter // 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; int voter_index = 0;
service_nodes::service_node_keys voter_keys; 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) 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.validators[i] = (i == voter_index) ? service_node_voter.pub : cryptonote::keypair{hw::get_device("default")}.pub;
state.workers[i] = cryptonote::keypair::generate(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 // Signature not valid
{ {
auto vote = valid_vote; auto vote = valid_vote;
cryptonote::keypair other_voter = cryptonote::keypair::generate(hw::get_device("default")); cryptonote::keypair other_voter{hw::get_device("default")};
vote.signature = {}; vote.signature = {};
cryptonote::vote_verification_context vvc = {}; cryptonote::vote_verification_context vvc = {};
bool result = verify_vote(vote, block_height, vvc, state); 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) 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].pub = voter.pub;
voters[i].key = voter.sec; voters[i].key = voter.sec;
state.validators[i] = voters[i].pub; 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;
} }
} }

View file

@ -41,7 +41,7 @@
using namespace service_nodes; using namespace service_nodes;
crypto::public_key newPubKey() { 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) { size_t calculateExcess(const swarm_snode_map_t& swarm_to_snodes) {