Changeset View
Changeset View
Standalone View
Standalone View
src/test/sigencoding_tests.cpp
Show All 16 Lines | |||||
} | } | ||||
static void | static void | ||||
CheckSignatureErrorForAllSigHashType(const valtype &vchSig, uint32_t flags, | CheckSignatureErrorForAllSigHashType(const valtype &vchSig, uint32_t flags, | ||||
const ScriptError expected_error) { | const ScriptError expected_error) { | ||||
for (int i = 0; i <= 0xff; i++) { | for (int i = 0; i <= 0xff; i++) { | ||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
valtype sig = SignatureWithHashType(vchSig, SigHashType(i)); | valtype sig = SignatureWithHashType(vchSig, SigHashType(i)); | ||||
BOOST_CHECK(!CheckSignatureEncoding(sig, flags, &err)); | BOOST_CHECK(!CheckTransactionSignatureEncoding(sig, flags, &err)); | ||||
BOOST_CHECK_EQUAL(err, expected_error); | BOOST_CHECK_EQUAL(err, expected_error); | ||||
} | } | ||||
} | } | ||||
static void CheckSignatureEncodingWithSigHashType(const valtype &vchSig, | static void CheckSignatureEncodingWithSigHashType(const valtype &vchSig, | ||||
uint32_t flags) { | uint32_t flags) { | ||||
const bool hasForkId = (flags & SCRIPT_ENABLE_SIGHASH_FORKID) != 0; | const bool hasForkId = (flags & SCRIPT_ENABLE_SIGHASH_FORKID) != 0; | ||||
const bool hasStrictEnc = (flags & SCRIPT_VERIFY_STRICTENC) != 0; | const bool hasStrictEnc = (flags & SCRIPT_VERIFY_STRICTENC) != 0; | ||||
Show All 9 Lines | static void CheckSignatureEncodingWithSigHashType(const valtype &vchSig, | ||||
} | } | ||||
for (const SigHashType baseSigHash : baseSigHashes) { | for (const SigHashType baseSigHash : baseSigHashes) { | ||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
// Check the signature with the proper forkid flag. | // Check the signature with the proper forkid flag. | ||||
SigHashType sigHash = baseSigHash.withForkId(hasForkId); | SigHashType sigHash = baseSigHash.withForkId(hasForkId); | ||||
valtype validSig = SignatureWithHashType(vchSig, sigHash); | valtype validSig = SignatureWithHashType(vchSig, sigHash); | ||||
BOOST_CHECK(CheckSignatureEncoding(validSig, flags, &err)); | BOOST_CHECK(CheckTransactionSignatureEncoding(validSig, flags, &err)); | ||||
// If we have strict encoding, we prevent the use of undefined flags. | // If we have strict encoding, we prevent the use of undefined flags. | ||||
std::array<SigHashType, 2> undefSigHashes{ | std::array<SigHashType, 2> undefSigHashes{ | ||||
SigHashType(sigHash.getRawSigHashType() | 0x20), | SigHashType(sigHash.getRawSigHashType() | 0x20), | ||||
sigHash.withBaseType(BaseSigHashType::UNSUPPORTED), | sigHash.withBaseType(BaseSigHashType::UNSUPPORTED), | ||||
}; | }; | ||||
for (SigHashType undefSigHash : undefSigHashes) { | for (SigHashType undefSigHash : undefSigHashes) { | ||||
valtype undefSighash = SignatureWithHashType(vchSig, undefSigHash); | valtype undefSighash = SignatureWithHashType(vchSig, undefSigHash); | ||||
BOOST_CHECK_EQUAL(CheckSignatureEncoding(undefSighash, flags, &err), | BOOST_CHECK_EQUAL( | ||||
CheckTransactionSignatureEncoding(undefSighash, flags, &err), | |||||
!hasStrictEnc); | !hasStrictEnc); | ||||
if (hasStrictEnc) { | if (hasStrictEnc) { | ||||
BOOST_CHECK_EQUAL(err, SCRIPT_ERR_SIG_HASHTYPE); | BOOST_CHECK_EQUAL(err, SCRIPT_ERR_SIG_HASHTYPE); | ||||
} | } | ||||
} | } | ||||
// If we check strict encoding, then invalid forkid is an error. | // If we check strict encoding, then invalid forkid is an error. | ||||
SigHashType invalidSigHash = baseSigHash.withForkId(!hasForkId); | SigHashType invalidSigHash = baseSigHash.withForkId(!hasForkId); | ||||
valtype invalidSig = SignatureWithHashType(vchSig, invalidSigHash); | valtype invalidSig = SignatureWithHashType(vchSig, invalidSigHash); | ||||
BOOST_CHECK_EQUAL(CheckSignatureEncoding(invalidSig, flags, &err), | BOOST_CHECK_EQUAL( | ||||
CheckTransactionSignatureEncoding(invalidSig, flags, &err), | |||||
!hasStrictEnc); | !hasStrictEnc); | ||||
if (hasStrictEnc) { | if (hasStrictEnc) { | ||||
BOOST_CHECK_EQUAL(err, | BOOST_CHECK_EQUAL(err, | ||||
hasForkId ? SCRIPT_ERR_MUST_USE_FORKID | hasForkId ? SCRIPT_ERR_MUST_USE_FORKID | ||||
: SCRIPT_ERR_ILLEGAL_FORKID); | : SCRIPT_ERR_ILLEGAL_FORKID); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(checksignatureencoding_test) { | ||||
}; | }; | ||||
// If we add many more flags, this loop can get too expensive, but we can | // If we add many more flags, this loop can get too expensive, but we can | ||||
// rewrite in the future to randomly pick a set of flags to evaluate. | // rewrite in the future to randomly pick a set of flags to evaluate. | ||||
for (uint32_t flags = 0; flags < (1U << 17); flags++) { | for (uint32_t flags = 0; flags < (1U << 17); flags++) { | ||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
// Empty sig is always valid. | // Empty sig is always valid. | ||||
BOOST_CHECK(CheckSignatureEncoding({}, flags, &err)); | BOOST_CHECK(CheckTransactionSignatureEncoding({}, flags, &err)); | ||||
// Signature are valid as long as the forkid flag is correct. | // Signature are valid as long as the forkid flag is correct. | ||||
CheckSignatureEncodingWithSigHashType(minimalSig, flags); | CheckSignatureEncodingWithSigHashType(minimalSig, flags); | ||||
if (flags & SCRIPT_VERIFY_LOW_S) { | if (flags & SCRIPT_VERIFY_LOW_S) { | ||||
// If we do enforce low S, then high S sigs are rejected. | // If we do enforce low S, then high S sigs are rejected. | ||||
CheckSignatureErrorForAllSigHashType(highSSig, flags, | CheckSignatureErrorForAllSigHashType(highSSig, flags, | ||||
SCRIPT_ERR_SIG_HIGH_S); | SCRIPT_ERR_SIG_HIGH_S); | ||||
▲ Show 20 Lines • Show All 185 Lines • Show Last 20 Lines |