Changeset View
Changeset View
Standalone View
Standalone View
src/test/sigencoding_tests.cpp
Show All 13 Lines | |||||
static valtype SignatureWithHashType(valtype vchSig, SigHashType sigHash) { | static valtype SignatureWithHashType(valtype vchSig, SigHashType sigHash) { | ||||
vchSig.push_back(static_cast<uint8_t>(sigHash.getRawSigHashType())); | vchSig.push_back(static_cast<uint8_t>(sigHash.getRawSigHashType())); | ||||
return vchSig; | return vchSig; | ||||
} | } | ||||
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++) { | |||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
BOOST_CHECK(!CheckDataSignatureEncoding(vchSig, flags, &err)); | |||||
BOOST_CHECK_EQUAL(err, expected_error); | |||||
for (int i = 0; i <= 0xff; i++) { | |||||
valtype sig = SignatureWithHashType(vchSig, SigHashType(i)); | valtype sig = SignatureWithHashType(vchSig, SigHashType(i)); | ||||
BOOST_CHECK(!CheckTransactionSignatureEncoding(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) { | ||||
ScriptError err = SCRIPT_ERR_OK; | |||||
BOOST_CHECK(CheckDataSignatureEncoding(vchSig, flags, &err)); | |||||
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; | ||||
std::vector<BaseSigHashType> allBaseTypes{ | std::vector<BaseSigHashType> allBaseTypes{ | ||||
BaseSigHashType::ALL, BaseSigHashType::NONE, BaseSigHashType::SINGLE}; | BaseSigHashType::ALL, BaseSigHashType::NONE, BaseSigHashType::SINGLE}; | ||||
std::vector<SigHashType> baseSigHashes; | std::vector<SigHashType> baseSigHashes; | ||||
for (const BaseSigHashType baseType : allBaseTypes) { | for (const BaseSigHashType baseType : allBaseTypes) { | ||||
const SigHashType baseSigHash = SigHashType().withBaseType(baseType); | const SigHashType baseSigHash = SigHashType().withBaseType(baseType); | ||||
baseSigHashes.push_back(baseSigHash); | baseSigHashes.push_back(baseSigHash); | ||||
baseSigHashes.push_back(baseSigHash.withAnyoneCanPay(true)); | baseSigHashes.push_back(baseSigHash.withAnyoneCanPay(true)); | ||||
} | } | ||||
for (const SigHashType baseSigHash : baseSigHashes) { | for (const SigHashType baseSigHash : baseSigHashes) { | ||||
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(CheckTransactionSignatureEncoding(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), | ||||
▲ Show 20 Lines • Show All 111 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(CheckDataSignatureEncoding({}, flags, &err)); | |||||
BOOST_CHECK(CheckTransactionSignatureEncoding({}, 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, | ||||
▲ Show 20 Lines • Show All 187 Lines • Show Last 20 Lines |