Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1002if (opcode == OP_NUMEQUALVERIFY)1003{1004if (CastToBool(stacktop(-1)))1005popstack(stack);1006else1007return set_error(serror, SCRIPT_ERR_NUMEQUALVERIFY);1008}1009}1010break;1012case OP_WITHIN:1013{1014// (x min max -- out)1015if (stack.size() < 3)1016return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1017CScriptNum bn1(stacktop(-3), fRequireMinimal);1018CScriptNum bn2(stacktop(-2), fRequireMinimal);1019CScriptNum bn3(stacktop(-1), fRequireMinimal);1020bool fValue = (bn2 <= bn1 && bn1 < bn3);1021popstack(stack);1022popstack(stack);1023popstack(stack);1024stack.push_back(fValue ? vchTrue : vchFalse);1025}1026break;1029//1030// Crypto1031//1032case OP_RIPEMD160:1033case OP_SHA1:1034case OP_SHA256:1035case OP_HASH160:1036case OP_HASH256:1037{1038// (in -- hash)1039if (stack.size() < 1)1040return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1041valtype& vch = stacktop(-1);1042valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);1043if (opcode == OP_RIPEMD160)1044CRIPEMD160().Write(vch.data(), vch.size()).Finalize(vchHash.data());1045else if (opcode == OP_SHA1)1046CSHA1().Write(vch.data(), vch.size()).Finalize(vchHash.data());1047else if (opcode == OP_SHA256)1048CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());1049else if (opcode == OP_HASH160)1050CHash160().Write(vch).Finalize(vchHash);1051else if (opcode == OP_HASH256)1052CHash256().Write(vch).Finalize(vchHash);1053popstack(stack);1054stack.push_back(vchHash);1055}1056break;1058case OP_CODESEPARATOR:1059{1060// If SCRIPT_VERIFY_CONST_SCRIPTCODE flag is set, use of OP_CODESEPARATOR is rejected in pre-segwit1061// script, even in an unexecuted branch (this is checked above the opcode case statement).1063// Hash starts after the code separator1064pbegincodehash = pc;1065execdata.m_codeseparator_pos = opcode_pos;1066}1067break;1069case OP_CHECKSIG:1070case OP_CHECKSIGVERIFY:1071{1072// (sig pubkey -- bool)1073if (stack.size() < 2)1074return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1076valtype& vchSig = stacktop(-2);1077valtype& vchPubKey = stacktop(-1);1079bool fSuccess = true;1080if (!EvalChecksig(vchSig, vchPubKey, pbegincodehash, pend, execdata, flags, checker, sigversion, serror, fSuccess)) return false;1081popstack(stack);1082popstack(stack);1083stack.push_back(fSuccess ? vchTrue : vchFalse);1084if (opcode == OP_CHECKSIGVERIFY)1085{1086if (fSuccess)1087popstack(stack);1088else1089return set_error(serror, SCRIPT_ERR_CHECKSIGVERIFY);1090}1091}1092break;1094case OP_CHECKSIGADD:1095{1096// OP_CHECKSIGADD is only available in Tapscript1097if (sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) return set_error(serror, SCRIPT_ERR_BAD_OPCODE);1099// (sig num pubkey -- num)1100if (stack.size() < 3) return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1102const valtype& sig = stacktop(-3);1103const CScriptNum num(stacktop(-2), fRequireMinimal);1104const valtype& pubkey = stacktop(-1);1106bool success = true;1107if (!EvalChecksig(sig, pubkey, pbegincodehash, pend, execdata, flags, checker, sigversion, serror, success)) return false;1108popstack(stack);1109popstack(stack);1110popstack(stack);1111stack.push_back((num + (success ? 1 : 0)).getvch());1112}1113break;1115case OP_CHECKMULTISIG:1116case OP_CHECKMULTISIGVERIFY:1117{1118if (sigversion == SigVersion::TAPSCRIPT) return set_error(serror, SCRIPT_ERR_TAPSCRIPT_CHECKMULTISIG);1120// ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)1122int i = 1;1123if ((int)stack.size() < i)1124return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1126int nKeysCount = CScriptNum(stacktop(-i), fRequireMinimal).getint();1127if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)1128return set_error(serror, SCRIPT_ERR_PUBKEY_COUNT);1129nOpCount += nKeysCount;1130if (nOpCount > MAX_OPS_PER_SCRIPT)1131return set_error(serror, SCRIPT_ERR_OP_COUNT);1132int ikey = ++i;1133// ikey2 is the position of last non-signature item in the stack. Top stack item = 1.1134// With SCRIPT_VERIFY_NULLFAIL, this is used for cleanup if operation fails.1135int ikey2 = nKeysCount + 2;1136i += nKeysCount;1137if ((int)stack.size() < i)1138return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1140int nSigsCount = CScriptNum(stacktop(-i), fRequireMinimal).getint();1141if (nSigsCount < 0 || nSigsCount > nKeysCount)1142return set_error(serror, SCRIPT_ERR_SIG_COUNT);1143int isig = ++i;1144i += nSigsCount;1145if ((int)stack.size() < i)1146return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1148// Subset of script starting at the most recent codeseparator1149CScript scriptCode(pbegincodehash, pend);1151// Drop the signature in pre-segwit scripts but not segwit scripts1152for (int k = 0; k < nSigsCount; k++)1153{1154valtype& vchSig = stacktop(-isig-k);1155if (sigversion == SigVersion::BASE) {1156int found = FindAndDelete(scriptCode, CScript() << vchSig);1157if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE))1158return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE);1159}1160}1162bool fSuccess = true;1163while (fSuccess && nSigsCount > 0)1164{1165valtype& vchSig = stacktop(-isig);1166valtype& vchPubKey = stacktop(-ikey);1168// Note how this makes the exact order of pubkey/signature evaluation1169// distinguishable by CHECKMULTISIG NOT if the STRICTENC flag is set.1170// See the script_(in)valid tests for details.1171if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {1172// serror is set1173return false;1174}1176// Check signature1177bool fOk = checker.CheckECDSASignature(vchSig, vchPubKey, scriptCode, sigversion);1179if (fOk) {1180isig++;1181nSigsCount--;1182}1183ikey++;1184nKeysCount--;1186// If there are more signatures left than keys left,1187// then too many signatures have failed. Exit early,1188// without checking any further signatures.1189if (nSigsCount > nKeysCount)1190fSuccess = false;1191}1193// Clean up stack of actual arguments1194while (i-- > 1) {1195// If the operation failed, we require that all signatures must be empty vector1196if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && !ikey2 && stacktop(-1).size())1197return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL);1198if (ikey2 > 0)1199ikey2--;1200popstack(stack);1201}1203// A bug causes CHECKMULTISIG to consume one extra argument1204// whose contents were not checked in any way.1205//1206// Unfortunately this is a potential source of mutability,1207// so optionally verify it is exactly equal to zero prior1208// to removing it from the stack.1209if (stack.size() < 1)1210return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);1211if ((flags & SCRIPT_VERIFY_NULLDUMMY) && stacktop(-1).size())1212return set_error(serror, SCRIPT_ERR_SIG_NULLDUMMY);1213popstack(stack);1215stack.push_back(fSuccess ? vchTrue : vchFalse);1217if (opcode == OP_CHECKMULTISIGVERIFY)1218{1219if (fSuccess)1220popstack(stack);1221else1222return set_error(serror, SCRIPT_ERR_CHECKMULTISIGVERIFY);1223}1224}1225break;1227default:1228return set_error(serror, SCRIPT_ERR_BAD_OPCODE);1229}1231// Size limits1232if (stack.size() + altstack.size() > MAX_STACK_SIZE)1233return set_error(serror, SCRIPT_ERR_STACK_SIZE);1234}1235}1236catch (const scriptnum_error&)1237{1238return set_error(serror, SCRIPT_ERR_SCRIPTNUM);1239}1240catch (...)1241{1242return set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR);1243}1245if (!vfExec.empty())1246return set_error(serror, SCRIPT_ERR_UNBALANCED_CONDITIONAL);1248return set_success(serror);1249}