diff --git a/src/test/bls_tests.cpp b/src/test/bls_tests.cpp index dea482ad88..7516f47f84 100644 --- a/src/test/bls_tests.cpp +++ b/src/test/bls_tests.cpp @@ -6,7 +6,7 @@ #include #include #include - +#include #include BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup) @@ -357,6 +357,56 @@ void FuncBatchVerifier(const bool legacy_scheme) 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 v_threshold_sks; + std::vector 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 v_size_ids; + std::vector v_size_sk_shares; + std::vector 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 v_share_sigs; + std::vector 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) { FuncSetHexStr(true); @@ -411,4 +461,10 @@ BOOST_AUTO_TEST_CASE(batch_verifier_tests) FuncBatchVerifier(false); } +BOOST_AUTO_TEST_CASE(bls_threshold_signature_tests) +{ + FuncThresholdSignature(true); + FuncThresholdSignature(false); +} + BOOST_AUTO_TEST_SUITE_END()