Changeset View
Changeset View
Standalone View
Standalone View
src/test/schnorr_tests.cpp
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(opcodes_random_flags) { | ||||
// for variety we start off at a different seed than sigencoding_tests | // for variety we start off at a different seed than sigencoding_tests | ||||
// The first lcg.next() call is still 0x00000000 though. | // The first lcg.next() call is still 0x00000000 though. | ||||
MMIXLinearCongruentialGenerator lcg(1234); | MMIXLinearCongruentialGenerator lcg(1234); | ||||
for (int i = 0; i < 4096; i++) { | for (int i = 0; i < 4096; i++) { | ||||
uint32_t flags = lcg.next(); | uint32_t flags = lcg.next(); | ||||
const bool hasForkId = (flags & SCRIPT_ENABLE_SIGHASH_FORKID) != 0; | const bool hasForkId = (flags & SCRIPT_ENABLE_SIGHASH_FORKID) != 0; | ||||
const bool hasSchnorr = (flags & SCRIPT_ENABLE_SCHNORR) != 0; | |||||
const bool hasStricts = | const bool hasStricts = | ||||
(flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | | (flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | | ||||
SCRIPT_VERIFY_STRICTENC)) != 0; | SCRIPT_VERIFY_STRICTENC)) != 0; | ||||
const bool hasNullFail = (flags & SCRIPT_VERIFY_NULLFAIL) != 0; | const bool hasNullFail = (flags & SCRIPT_VERIFY_NULLFAIL) != 0; | ||||
// Prepare 65-byte transaction sigs with right hashtype byte. | // Prepare 65-byte transaction sigs with right hashtype byte. | ||||
valtype DER64_with_hashtype = | valtype DER64_with_hashtype = | ||||
SignatureWithHashType(DER64, SigHashType().withForkId(hasForkId)); | SignatureWithHashType(DER64, SigHashType().withForkId(hasForkId)); | ||||
valtype Zero64_with_hashtype = | valtype Zero64_with_hashtype = | ||||
SignatureWithHashType(Zero64, SigHashType().withForkId(hasForkId)); | SignatureWithHashType(Zero64, SigHashType().withForkId(hasForkId)); | ||||
// Test CHECKSIG & CHECKDATASIG with he non-DER sig, which can fail from | // Test CHECKSIG & CHECKDATASIG with he non-DER sig, which can fail from | ||||
// encoding, otherwise upon verification. | // encoding, otherwise upon verification. | ||||
if (hasStricts && !hasSchnorr) { | if (hasNullFail) { | ||||
CheckError(flags, {Zero64_with_hashtype, pubkeyC}, scriptCHECKSIG, | |||||
SCRIPT_ERR_SIG_DER); | |||||
CheckError(flags, {Zero64_with_hashtype, pubkeyC}, | |||||
scriptCHECKSIGVERIFY, SCRIPT_ERR_SIG_DER); | |||||
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIG, | |||||
SCRIPT_ERR_SIG_DER); | |||||
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY, | |||||
SCRIPT_ERR_SIG_DER); | |||||
} else if (hasNullFail) { | |||||
CheckError(flags, {Zero64_with_hashtype, pubkeyC}, scriptCHECKSIG, | CheckError(flags, {Zero64_with_hashtype, pubkeyC}, scriptCHECKSIG, | ||||
SCRIPT_ERR_SIG_NULLFAIL); | SCRIPT_ERR_SIG_NULLFAIL); | ||||
CheckError(flags, {Zero64_with_hashtype, pubkeyC}, | CheckError(flags, {Zero64_with_hashtype, pubkeyC}, | ||||
scriptCHECKSIGVERIFY, SCRIPT_ERR_SIG_NULLFAIL); | scriptCHECKSIGVERIFY, SCRIPT_ERR_SIG_NULLFAIL); | ||||
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIG, | CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIG, | ||||
SCRIPT_ERR_SIG_NULLFAIL); | SCRIPT_ERR_SIG_NULLFAIL); | ||||
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY, | CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY, | ||||
SCRIPT_ERR_SIG_NULLFAIL); | SCRIPT_ERR_SIG_NULLFAIL); | ||||
Show All 24 Lines | for (int i = 0; i < 4096; i++) { | ||||
CheckError(flags, {DER64_with_hashtype, pubkeyC}, | CheckError(flags, {DER64_with_hashtype, pubkeyC}, | ||||
scriptCHECKSIGVERIFY, SCRIPT_ERR_CHECKSIGVERIFY); | scriptCHECKSIGVERIFY, SCRIPT_ERR_CHECKSIGVERIFY); | ||||
CheckPass(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIG, {}); | CheckPass(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIG, {}); | ||||
CheckError(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY, | CheckError(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY, | ||||
SCRIPT_ERR_CHECKDATASIGVERIFY); | SCRIPT_ERR_CHECKDATASIGVERIFY); | ||||
} | } | ||||
// test OP_CHECKMULTISIG/VERIFY | // test OP_CHECKMULTISIG/VERIFY | ||||
if (hasSchnorr) { | // We fail with BADLENGTH no matter what. | ||||
// When Schnorr flag is on, we always fail with BADLENGTH no matter | |||||
// what. | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | ||||
scriptCHECKMULTISIG, SCRIPT_ERR_SIG_BADLENGTH); | scriptCHECKMULTISIG, SCRIPT_ERR_SIG_BADLENGTH); | ||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | ||||
scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_BADLENGTH); | scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_BADLENGTH); | ||||
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | ||||
scriptCHECKMULTISIG, SCRIPT_ERR_SIG_BADLENGTH); | scriptCHECKMULTISIG, SCRIPT_ERR_SIG_BADLENGTH); | ||||
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | ||||
scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_BADLENGTH); | scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_BADLENGTH); | ||||
} else { | |||||
// Otherwise, the failure depends on signature content. | |||||
// The non-DER sig can fail from encoding, otherwise upon | |||||
// verification. | |||||
if (hasStricts) { | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIG, SCRIPT_ERR_SIG_DER); | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_DER); | |||||
} else if (hasNullFail) { | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIG, SCRIPT_ERR_SIG_NULLFAIL); | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_NULLFAIL); | |||||
} else { | |||||
CheckPass(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIG, {}); | |||||
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIGVERIFY, | |||||
SCRIPT_ERR_CHECKMULTISIGVERIFY); | |||||
} | |||||
// The DER sig fails upon verification. | |||||
if (hasNullFail) { | |||||
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIG, SCRIPT_ERR_SIG_NULLFAIL); | |||||
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIGVERIFY, SCRIPT_ERR_SIG_NULLFAIL); | |||||
} else { | |||||
CheckPass(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIG, {}); | |||||
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}}, | |||||
scriptCHECKMULTISIGVERIFY, | |||||
SCRIPT_ERR_CHECKMULTISIGVERIFY); | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |