What changed, and why it matters
This patch fixes how Monero validates the number of participants in a multisig wallet. It replaces hand-rolled math for counting combinations with a safer library function and corrects a check that was looking at the wrong variable (threshold instead of total). The old code could allow invalid or oversized multisig setups, which might cause wallet creation failures or incorrect multisig behavior.
Review the new tools::combinator implementation for overflow safety and correctness, ensure config::MULTISIG_MAX_SIGNERS is consistently enforced across all multisig entry points, and add regression tests for edge-case participant counts (e.g., total > threshold near the maximum).
Security signals we found
Incorrect variable used in input validation (threshold vs total)
Use of floating-point arithmetic for combinatorial counts replaced with integer library function
Manual factorial-based combination formula replaced with audited utility
Validation bound tied to config constant rather than hardcoded literal
Potential for malformed multisig seed to pass size check due to wrong participant count validation
Evidence from the diff
The commit refactors multisig combination-count calculations to use tools::combinations_count() instead of a custom floating-point binomial coefficient and a manual factorial loop. In wallet2.cpp, the validation changes from threshold > 16 to total > config::MULTISIG_MAX_SIGNERS, which is the correct bound for total signers. The previous threshold check could permit total signer counts above the intended maximum, potentially leading to buffer size mismatches during multisig seed parsing or incorrect key expectation counts in the KEX protocol.
Changed components
src/wallet/wallet2.cppsrc/multisig/multisig_account_kex_impl.cppMonero multisig wallet creation and key exchange protocolInspect captured patch +8 / −36
### src/multisig/multisig_account_kex_impl.cpp
@@ -28,19 +28,16 @@
#include "multisig_account.h"
+#include "common/combinator.h"
#include "crypto/crypto.h"
#include "cryptonote_config.h"
#include "include_base_utils.h"
#include "multisig.h"
#include "multisig_kex_msg.h"
#include "ringct/rctOps.h"
-#include <boost/math/special_functions/binomial.hpp>
-
#include <algorithm>
-#include <cmath>
#include <cstdint>
-#include <limits>
#include <memory>
#include <unordered_map>
#include <unordered_set>
@@ -474,27 +471,6 @@ namespace multisig
// 3. each origin should recommend a precise number of pubkeys
- // TODO: move to a 'math' library, with unit tests
- auto n_choose_k_f =
- [](const std::uint32_t n, const std::uint32_t k) -> std::uint32_t
- {
- static_assert(std::numeric_limits<std::int32_t>::digits <= std::numeric_limits<double>::digits,
- "n_choose_k requires no rounding issues when converting between int32 <-> double.");
-
- if (n < k)
- return 0;
-
- double fp_result = boost::math::binomial_coefficient<double>(n, k);
-
- if (fp_result < 0)
- return 0;
-
- if (fp_result > std::numeric_limits<std::int32_t>::max()) // note: std::round() returns std::int32_t
- return 0;
-
- return static_cast<std::uint32_t>(std::round(fp_result));
- };
-
// other signers: (N - 2) choose (msg_round_num - 1)
// - Each signer recommends keys they share with other signers.
// - In each round, every group of size 'round num' will have a key. From a single signer's perspective,
@@ -504,12 +480,12 @@ namespace multisig
// - Other signers will recommend (N - 2) choose (msg_round_num - 1) pubkeys (after removing keys shared with local).
// Note: Keys shared with local are filtered out to facilitate kex round boosting, where one or more signers may
// have boosted the local signer (implying they didn't have access to the local signer's previous round msg).
- const std::uint32_t expected_recommendations_others = n_choose_k_f(signers.size() - 2, round - 1);
+ const std::uint64_t expected_recommendations_others = tools::combinations_count(round - 1, signers.size() - 2);
// local: (N - 1) choose (msg_round_num - 1)
- const std::uint32_t expected_recommendations_self = n_choose_k_f(signers.size() - 1, round - 1);
+ const std::uint64_t expected_recommendations_self = tools::combinations_count(round - 1, signers.size() - 1);
- // note: expected_recommendations_others would be 0 in the last round of 1-of-N, but we don't call this function for
+ // note: the last round of 1-of-N would require choosing (N - 1) from (N - 2), but we don't call this function for
// that case
CHECK_AND_ASSERT_THROW_MES(expected_recommendations_self > 0 && expected_recommendations_others > 0,
"Bad num signers or round num (possibly numerical limits exceeded).");
### src/wallet/wallet2.cpp
@@ -1031,17 +1031,13 @@ bool get_pruned_tx(const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry &entry,
// Given M (threshold) and N (total), calculate the number of private multisig keys each
// signer should have. This value is equal to (N - 1) choose (N - M)
-// Prereq: M >= 1 && N >= M && N <= 16
+// Prereq: M >= 1 && N >= M && N <= config::MULTISIG_MAX_SIGNERS
uint64_t num_priv_multisig_keys_post_setup(uint64_t threshold, uint64_t total)
{
- THROW_WALLET_EXCEPTION_IF(threshold < 1 || total < threshold || threshold > 16,
+ THROW_WALLET_EXCEPTION_IF(threshold < 1 || total < threshold || total > config::MULTISIG_MAX_SIGNERS,
tools::error::wallet_internal_error, "Invalid arguments to num_priv_multisig_keys_post_setup");
- uint64_t n_multisig_keys = 1;
- for (uint64_t i = 2; i <= total - 1; ++i) n_multisig_keys *= i; // multiply by (N - 1)!
- for (uint64_t i = 2; i <= total - threshold; ++i) n_multisig_keys /= i; // divide by (N - M)!
- for (uint64_t i = 2; i <= threshold - 1; ++i) n_multisig_keys /= i; // divide by ((N - 1) - (N - M))!
- return n_multisig_keys;
+ return tools::combinations_count(total - threshold, total - 1);
}
/**
@@ -5899,7 +5895,7 @@ void wallet2::generate(const std::string& wallet_, const epee::wipeable_string&
THROW_WALLET_EXCEPTION_IF(threshold < 1, error::invalid_multisig_seed);
THROW_WALLET_EXCEPTION_IF(total < threshold, error::invalid_multisig_seed);
- THROW_WALLET_EXCEPTION_IF(threshold > 16, error::invalid_multisig_seed); // doing N choose (N - M + 1) might overflow
+ THROW_WALLET_EXCEPTION_IF(total > config::MULTISIG_MAX_SIGNERS, error::invalid_multisig_seed);
const uint64_t n_multisig_keys = num_priv_multisig_keys_post_setup(threshold, total);
THROW_WALLET_EXCEPTION_IF(multisig_data.size() != 8 + 32 * (4 + n_multisig_keys + total), error::invalid_multisig_seed);
Why this scored 57/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.