Skip to content

Commit

Permalink
[QA] Add test for BLS sign/verify message and sethexstr
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Sep 14, 2021
1 parent c4c6efe commit 0253ea7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ BITCOIN_TESTS =\
test/base64_tests.cpp \
test/bech32_tests.cpp \
test/bip32_tests.cpp \
test/bls_tests.cpp \
test/budget_tests.cpp \
test/checkblock_tests.cpp \
test/Checkpoints_tests.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ set(BITCOIN_TESTS
${CMAKE_CURRENT_SOURCE_DIR}/bech32_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/budget_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/bip32_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/bls_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/checkblock_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Checkpoints_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/coins_tests.cpp
Expand Down
54 changes: 54 additions & 0 deletions src/test/bls_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2019-2020 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "test/test_pivx.h"
#include "bls/bls_wrapper.h"

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(bls_sethexstr_tests)
{
CBLSSecretKey sk;
std::string strValidSecret = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
// Note: invalid string passed to SetHexStr() should cause it to fail and reset key internal data
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1g")); // non-hex
BOOST_CHECK(!sk.IsValid());
BOOST_CHECK(sk == CBLSSecretKey());
// Try few more invalid strings
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e")); // hex but too short
BOOST_CHECK(!sk.IsValid());
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")); // hex but too long
BOOST_CHECK(!sk.IsValid());
}

BOOST_AUTO_TEST_CASE(bls_sig_tests)
{
CBLSSecretKey sk1, sk2;
sk1.MakeNewKey();
sk2.MakeNewKey();

uint256 msgHash1 = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
uint256 msgHash2 = uint256S("0000000000000000000000000000000000000000000000000000000000000002");

auto sig1 = sk1.Sign(msgHash1);
auto sig2 = sk2.Sign(msgHash1);

BOOST_CHECK(sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash2));

BOOST_CHECK(sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash2));

BOOST_CHECK(!sig1.VerifyInsecure(sk2.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig1.VerifyInsecure(sk2.GetPublicKey(), msgHash2));
BOOST_CHECK(!sig2.VerifyInsecure(sk1.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig2.VerifyInsecure(sk1.GetPublicKey(), msgHash2));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 0253ea7

Please sign in to comment.