|
| 1 | +// Copyright (c) 2019-2020 The Dash Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "test/test_pivx.h" |
| 6 | +#include "bls/bls_wrapper.h" |
| 7 | + |
| 8 | +#include <boost/test/unit_test.hpp> |
| 9 | + |
| 10 | +BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup) |
| 11 | + |
| 12 | +BOOST_AUTO_TEST_CASE(bls_sethexstr_tests) |
| 13 | +{ |
| 14 | + CBLSSecretKey sk; |
| 15 | + std::string strValidSecret = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"; |
| 16 | + // Note: invalid string passed to SetHexStr() should cause it to fail and reset key internal data |
| 17 | + BOOST_CHECK(sk.SetHexStr(strValidSecret)); |
| 18 | + BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1g")); // non-hex |
| 19 | + BOOST_CHECK(!sk.IsValid()); |
| 20 | + BOOST_CHECK(sk == CBLSSecretKey()); |
| 21 | + // Try few more invalid strings |
| 22 | + BOOST_CHECK(sk.SetHexStr(strValidSecret)); |
| 23 | + BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e")); // hex but too short |
| 24 | + BOOST_CHECK(!sk.IsValid()); |
| 25 | + BOOST_CHECK(sk.SetHexStr(strValidSecret)); |
| 26 | + BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")); // hex but too long |
| 27 | + BOOST_CHECK(!sk.IsValid()); |
| 28 | +} |
| 29 | + |
| 30 | +BOOST_AUTO_TEST_CASE(bls_sig_tests) |
| 31 | +{ |
| 32 | + CBLSSecretKey sk1, sk2; |
| 33 | + sk1.MakeNewKey(); |
| 34 | + sk2.MakeNewKey(); |
| 35 | + |
| 36 | + uint256 msgHash1 = uint256S("0000000000000000000000000000000000000000000000000000000000000001"); |
| 37 | + uint256 msgHash2 = uint256S("0000000000000000000000000000000000000000000000000000000000000002"); |
| 38 | + |
| 39 | + auto sig1 = sk1.Sign(msgHash1); |
| 40 | + auto sig2 = sk2.Sign(msgHash1); |
| 41 | + |
| 42 | + BOOST_CHECK(sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash1)); |
| 43 | + BOOST_CHECK(!sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash2)); |
| 44 | + |
| 45 | + BOOST_CHECK(sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash1)); |
| 46 | + BOOST_CHECK(!sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash2)); |
| 47 | + |
| 48 | + BOOST_CHECK(!sig1.VerifyInsecure(sk2.GetPublicKey(), msgHash1)); |
| 49 | + BOOST_CHECK(!sig1.VerifyInsecure(sk2.GetPublicKey(), msgHash2)); |
| 50 | + BOOST_CHECK(!sig2.VerifyInsecure(sk1.GetPublicKey(), msgHash1)); |
| 51 | + BOOST_CHECK(!sig2.VerifyInsecure(sk1.GetPublicKey(), msgHash2)); |
| 52 | +} |
| 53 | + |
| 54 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments