diff options
author | naughtyfox <mail.for.milo@gmail.com> | 2018-07-12 12:55:52 +0300 |
---|---|---|
committer | naughtyfox <mail.for.milo@gmail.com> | 2018-10-01 19:16:56 +0300 |
commit | 9f3963e8235826704b7bc6ef9e3b90613a72e16c (patch) | |
tree | 303e65a2dcbaf40520e68d11cdcb7efcb41cff57 /tests | |
parent | Merge pull request #4423 (diff) | |
download | monero-9f3963e8235826704b7bc6ef9e3b90613a72e16c.tar.xz |
Arbitrary M/N multisig schemes:
* support in wallet2
* support in monero-wallet-cli
* support in monero-wallet-rpc
* support in wallet api
* support in monero-gen-trusted-multisig
* unit tests for multisig wallets creation
Diffstat (limited to 'tests')
-rw-r--r-- | tests/core_tests/chaingen.h | 2 | ||||
-rw-r--r-- | tests/unit_tests/multisig.cpp | 179 |
2 files changed, 76 insertions, 105 deletions
diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index 34b205ae5..c9e1640aa 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -593,7 +593,7 @@ inline bool do_replay_file(const std::string& filename) std::vector<crypto::public_key> spend_public_keys; \ for (const auto &k: all_multisig_keys) \ spend_public_keys.push_back(k); \ - crypto::public_key spend_pkey = cryptonote::generate_multisig_N1_N_spend_public_key(spend_public_keys); \ + crypto::public_key spend_pkey = cryptonote::generate_multisig_M_N_spend_public_key(spend_public_keys); \ for (size_t msidx = 0; msidx < total; ++msidx) \ account[msidx].finalize_multisig(spend_pkey); \ } \ diff --git a/tests/unit_tests/multisig.cpp b/tests/unit_tests/multisig.cpp index 83924c7af..7268f2690 100644 --- a/tests/unit_tests/multisig.cpp +++ b/tests/unit_tests/multisig.cpp @@ -49,9 +49,19 @@ static const struct { "9t6Hn946u3eah5cuncH1hB5hGzsTUoevtf4SY7MHN5NgJZh2SFWsyVt3vUhuHyRKyrCQvr71Lfc1AevG3BXE11PQFoXDtD8", "bbd3175ef9fd9f5eefdc43035f882f74ad14c4cf1799d8b6f9001bc197175d02" + }, + { + "9zmAWoNyNPbgnYSm3nJNpAKHm6fCcs3MR94gBWxp9MCDUiMUhyYFfyQETUDLPF7DP6ZsmNo6LRxwPP9VmhHNxKrER9oGigT", + "f2efae45bef1917a7430cda8fcffc4ee010e3178761aa41d4628e23b1fe2d501" + }, + { + "9ue8NJMg3WzKxTtmjeXzWYF5KmU6dC7LHEt9wvYdPn2qMmoFUa8hJJHhSHvJ46UEwpDyy5jSboNMRaDBKwU54NT42YcNUp5", + "a4cef54ed3fd61cd78a2ceb82ecf85a903ad2db9a86fb77ff56c35c56016280a" } }; +static const size_t KEYS_COUNT = 5; + static void make_wallet(unsigned int idx, tools::wallet2 &wallet) { ASSERT_TRUE(idx < sizeof(test_addresses) / sizeof(test_addresses[0])); @@ -76,126 +86,87 @@ static void make_wallet(unsigned int idx, tools::wallet2 &wallet) } } -static void make_M_2_wallet(tools::wallet2 &wallet0, tools::wallet2 &wallet1, unsigned int M) +static std::vector<std::string> exchange_round(std::vector<tools::wallet2>& wallets, const std::vector<std::string>& mis) { - ASSERT_TRUE(M <= 2); - - make_wallet(0, wallet0); - make_wallet(1, wallet1); - - std::vector<crypto::secret_key> sk0(1), sk1(1); - std::vector<crypto::public_key> pk0(1), pk1(1); - - wallet0.decrypt_keys(""); - std::string mi0 = wallet0.get_multisig_info(); - wallet0.encrypt_keys(""); - wallet1.decrypt_keys(""); - std::string mi1 = wallet1.get_multisig_info(); - wallet1.encrypt_keys(""); - - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk0[0], pk0[0])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk1[0], pk1[0])); - - ASSERT_FALSE(wallet0.multisig() || wallet1.multisig()); - wallet0.make_multisig("", sk0, pk0, M); - wallet1.make_multisig("", sk1, pk1, M); - - ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet1.get_account().get_public_address_str(cryptonote::TESTNET)); - - bool ready; - uint32_t threshold, total; - ASSERT_TRUE(wallet0.multisig(&ready, &threshold, &total)); - ASSERT_TRUE(ready); - ASSERT_TRUE(threshold == M); - ASSERT_TRUE(total == 2); - ASSERT_TRUE(wallet1.multisig(&ready, &threshold, &total)); - ASSERT_TRUE(ready); - ASSERT_TRUE(threshold == M); - ASSERT_TRUE(total == 2); + std::vector<std::string> new_infos; + for (size_t i = 0; i < wallets.size(); ++i) { + new_infos.push_back(wallets[i].exchange_multisig_keys("", mis)); + } + + return new_infos; } -static void make_M_3_wallet(tools::wallet2 &wallet0, tools::wallet2 &wallet1, tools::wallet2 &wallet2, unsigned int M) +static void make_wallets(std::vector<tools::wallet2>& wallets, unsigned int M) { - ASSERT_TRUE(M <= 3); - - make_wallet(0, wallet0); - make_wallet(1, wallet1); - make_wallet(2, wallet2); - - std::vector<crypto::secret_key> sk0(2), sk1(2), sk2(2); - std::vector<crypto::public_key> pk0(2), pk1(2), pk2(2); - - wallet0.decrypt_keys(""); - std::string mi0 = wallet0.get_multisig_info(); - wallet0.encrypt_keys(""); - wallet1.decrypt_keys(""); - std::string mi1 = wallet1.get_multisig_info(); - wallet1.encrypt_keys(""); - wallet2.decrypt_keys(""); - std::string mi2 = wallet2.get_multisig_info(); - wallet2.encrypt_keys(""); - - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk0[0], pk0[0])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi2, sk0[1], pk0[1])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk1[0], pk1[0])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi2, sk1[1], pk1[1])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi0, sk2[0], pk2[0])); - ASSERT_TRUE(tools::wallet2::verify_multisig_info(mi1, sk2[1], pk2[1])); - - ASSERT_FALSE(wallet0.multisig() || wallet1.multisig() || wallet2.multisig()); - std::string mxi0 = wallet0.make_multisig("", sk0, pk0, M); - std::string mxi1 = wallet1.make_multisig("", sk1, pk1, M); - std::string mxi2 = wallet2.make_multisig("", sk2, pk2, M); - - const size_t nset = !mxi0.empty() + !mxi1.empty() + !mxi2.empty(); - ASSERT_TRUE((M < 3 && nset == 3) || (M == 3 && nset == 0)); - - if (nset > 0) - { - std::unordered_set<crypto::public_key> pkeys; - std::vector<crypto::public_key> signers(3, crypto::null_pkey); - ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi0, pkeys, signers[0])); - ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi1, pkeys, signers[1])); - ASSERT_TRUE(tools::wallet2::verify_extra_multisig_info(mxi2, pkeys, signers[2])); - ASSERT_TRUE(pkeys.size() == 3); - ASSERT_TRUE(wallet0.finalize_multisig("", pkeys, signers)); - ASSERT_TRUE(wallet1.finalize_multisig("", pkeys, signers)); - ASSERT_TRUE(wallet2.finalize_multisig("", pkeys, signers)); + ASSERT_TRUE(wallets.size() > 1 && wallets.size() <= KEYS_COUNT); + ASSERT_TRUE(M <= wallets.size()); + + std::vector<std::string> mis(wallets.size()); + + for (size_t i = 0; i < wallets.size(); ++i) { + make_wallet(i, wallets[i]); + + wallets[i].decrypt_keys(""); + mis[i] = wallets[i].get_multisig_info(); + wallets[i].encrypt_keys(""); + } + + for (auto& wallet: wallets) { + ASSERT_FALSE(wallet.multisig() || wallet.multisig() || wallet.multisig()); + } + + std::vector<std::string> mxis; + for (size_t i = 0; i < wallets.size(); ++i) { + // it's ok to put all of multisig keys in this function. it throws in case of error + mxis.push_back(wallets[i].make_multisig("", mis, M)); } - ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet1.get_account().get_public_address_str(cryptonote::TESTNET)); - ASSERT_TRUE(wallet0.get_account().get_public_address_str(cryptonote::TESTNET) == wallet2.get_account().get_public_address_str(cryptonote::TESTNET)); - - bool ready; - uint32_t threshold, total; - ASSERT_TRUE(wallet0.multisig(&ready, &threshold, &total)); - ASSERT_TRUE(ready); - ASSERT_TRUE(threshold == M); - ASSERT_TRUE(total == 3); - ASSERT_TRUE(wallet1.multisig(&ready, &threshold, &total)); - ASSERT_TRUE(ready); - ASSERT_TRUE(threshold == M); - ASSERT_TRUE(total == 3); - ASSERT_TRUE(wallet2.multisig(&ready, &threshold, &total)); - ASSERT_TRUE(ready); - ASSERT_TRUE(threshold == M); - ASSERT_TRUE(total == 3); + while (!mxis[0].empty()) { + mxis = exchange_round(wallets, mxis); + } + + for (size_t i = 0; i < wallets.size(); ++i) { + ASSERT_TRUE(mxis[i].empty()); + bool ready; + uint32_t threshold, total; + ASSERT_TRUE(wallets[i].multisig(&ready, &threshold, &total)); + ASSERT_TRUE(ready); + ASSERT_TRUE(threshold == M); + ASSERT_TRUE(total == wallets.size()); + + if (i != 0) { + // "equals" is transitive relation so we need only to compare first wallet's address to each others' addresses. no need to compare 0's address with itself. + ASSERT_TRUE(wallets[0].get_account().get_public_address_str(cryptonote::TESTNET) == wallets[i].get_account().get_public_address_str(cryptonote::TESTNET)); + } + } } TEST(multisig, make_2_2) { - tools::wallet2 wallet0, wallet1; - make_M_2_wallet(wallet0, wallet1, 2); + std::vector<tools::wallet2> wallets(2); + make_wallets(wallets, 2); } TEST(multisig, make_3_3) { - tools::wallet2 wallet0, wallet1, wallet2; - make_M_3_wallet(wallet0, wallet1, wallet2, 3); + std::vector<tools::wallet2> wallets(3); + make_wallets(wallets, 3); } TEST(multisig, make_2_3) { - tools::wallet2 wallet0, wallet1, wallet2; - make_M_3_wallet(wallet0, wallet1, wallet2, 2); + std::vector<tools::wallet2> wallets(3); + make_wallets(wallets, 2); +} + +TEST(multisig, make_2_4) +{ + std::vector<tools::wallet2> wallets(4); + make_wallets(wallets, 2); +} + +TEST(multisig, make_2_5) +{ + std::vector<tools::wallet2> wallets(5); + make_wallets(wallets, 2); } |