crypto: replace rand<T>()%N idiom with unbiased rand_idx(N)

This commit is contained in:
stoffu
2019-04-04 22:38:19 +09:00
parent b6726aaa6c
commit a2195b9b7f
5 changed files with 36 additions and 12 deletions
+4 -4
View File
@@ -5719,7 +5719,7 @@ namespace
{
CHECK_AND_ASSERT_MES(!vec.empty(), T(), "Vector must be non-empty");
size_t idx = crypto::rand<size_t>() % vec.size();
size_t idx = crypto::rand_idx(vec.size());
return pop_index (vec, idx);
}
@@ -5822,7 +5822,7 @@ size_t wallet2::pop_best_value_from(const transfer_container &transfers, std::ve
}
else
{
idx = crypto::rand<size_t>() % candidates.size();
idx = crypto::rand_idx(candidates.size());
}
return pop_index (unused_indices, candidates[idx]);
}
@@ -7474,7 +7474,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
if (n_rct == 0)
return rct_offsets[block_offset] ? rct_offsets[block_offset] - 1 : 0;
MDEBUG("Picking 1/" << n_rct << " in " << (last_block_offset - first_block_offset + 1) << " blocks centered around " << block_offset + rct_start_height);
return first_rct + crypto::rand<uint64_t>() % n_rct;
return first_rct + crypto::rand_idx(n_rct);
};
size_t num_selected_transfers = 0;
@@ -9603,7 +9603,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_all(uint64_t below
if (unused_transfer_dust_indices_per_subaddr.count(0) == 1 && unused_transfer_dust_indices_per_subaddr.size() > 1)
unused_transfer_dust_indices_per_subaddr.erase(0);
auto i = unused_transfer_dust_indices_per_subaddr.begin();
std::advance(i, crypto::rand<size_t>() % unused_transfer_dust_indices_per_subaddr.size());
std::advance(i, crypto::rand_idx(unused_transfer_dust_indices_per_subaddr.size()));
unused_transfers_indices = i->second.first;
unused_dust_indices = i->second.second;
LOG_PRINT_L2("Spending from subaddress index " << i->first);