test: added threshold_signature_tests (#5279)

## Issue being fixed or feature implemented

## What was done?
Added BLS threshold signature unit tests.
Similar test is already present in bls_signatures but it won't hurt us
to have it Dash repo as well.


## How Has This Been Tested?


## Breaking Changes


## Checklist:
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation

**For repository code-owners and collaborators only**
- [ ] I have assigned this pull request to a milestone

---------

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This commit is contained in:
Odysseas Gabrielides 2023-04-06 08:13:59 +03:00 committed by GitHub
parent 9b74c70d3d
commit 5354515dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@
#include <bls/bls_batchverifier.h> #include <bls/bls_batchverifier.h>
#include <random.h> #include <random.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <util/irange.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup)
@ -357,6 +357,56 @@ void FuncBatchVerifier(const bool legacy_scheme)
Verify(msgs); Verify(msgs);
} }
void FuncThresholdSignature(const bool legacy_scheme)
{
bls::bls_legacy_scheme.store(legacy_scheme);
[[maybe_unused]] const uint256 hash = GetRandHash();
constexpr size_t m_size = 20;
constexpr size_t m_threshold = 15;
std::vector<CBLSSecretKey> v_threshold_sks;
std::vector<CBLSPublicKey> v_threshold_pks;
for ([[maybe_unused]] const auto i : irange::range(m_threshold)) {
CBLSSecretKey sk;
sk.MakeNewKey();
v_threshold_sks.push_back(sk);
v_threshold_pks.emplace_back(sk.GetPublicKey());
}
CBLSSecretKey thr_sk = v_threshold_sks[0];
CBLSPublicKey thr_pk = v_threshold_pks[0];
CBLSSignature thr_sig = thr_sk.Sign(hash);
std::vector<CBLSId> v_size_ids;
std::vector<CBLSSecretKey> v_size_sk_shares;
std::vector<CBLSPublicKey> v_size_pk_shares;
for ([[maybe_unused]] const auto m_shares : irange::range(m_size)) {
v_size_ids.emplace_back(GetRandHash());
CBLSSecretKey sk;
BOOST_CHECK(sk.SecretKeyShare(v_threshold_sks, v_size_ids[m_shares]));
v_size_sk_shares.push_back(sk);
CBLSPublicKey pk;
BOOST_CHECK(pk.PublicKeyShare(v_threshold_pks, v_size_ids[m_shares]));
v_size_pk_shares.push_back(pk);
std::vector<CBLSSignature> v_share_sigs;
std::vector<CBLSId> v_share_ids;
for ([[maybe_unused]] const auto j : irange::range(m_shares)) {
v_share_sigs.emplace_back(v_size_sk_shares[j].Sign(hash));
BOOST_CHECK(v_share_sigs.back().VerifyInsecure(v_size_pk_shares[j], hash));
v_share_ids.push_back(v_size_ids[j]);
}
CBLSSignature rec_share_sig;
BOOST_CHECK_EQUAL(rec_share_sig.Recover(v_share_sigs, v_share_ids), m_shares >= 2);
BOOST_CHECK_EQUAL(rec_share_sig.IsValid(), m_shares >= 2);
BOOST_CHECK_EQUAL(rec_share_sig == thr_sig, m_shares >= m_threshold);
BOOST_CHECK_EQUAL(rec_share_sig.VerifyInsecure(thr_pk, hash), m_shares >= m_threshold);
}
}
BOOST_AUTO_TEST_CASE(bls_sethexstr_tests) BOOST_AUTO_TEST_CASE(bls_sethexstr_tests)
{ {
FuncSetHexStr(true); FuncSetHexStr(true);
@ -411,4 +461,10 @@ BOOST_AUTO_TEST_CASE(batch_verifier_tests)
FuncBatchVerifier(false); FuncBatchVerifier(false);
} }
BOOST_AUTO_TEST_CASE(bls_threshold_signature_tests)
{
FuncThresholdSignature(true);
FuncThresholdSignature(false);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()