-
+ DB7E56DDDA012B89ED40A6F8A319215A993F9E701B04FDC1B4CFF8CD0A78B193468FCADDE96DFCB78BEA09C5B649B1136FB5CE66585B46EF658BF1E2C86568A4
bitcoin/src/test/script_tests.cpp
(0 . 0)(1 . 204)
23154 // /****************************\
23155 // * EXPERIMENTAL BRANCH. *
23156 // * FOR LABORATORY USE ONLY. *
23157 // ********************************
23158 // ************
23159 // **************
23160 // ****************
23161 // **** **** ****
23162 // *** *** ***
23163 // *** *** ***
23164 // *** * * **
23165 // ******** ********
23166 // ******* ******
23167 // *** **
23168 // * ******* **
23169 // ** * * * * *
23170 // ** * * ***
23171 // **** * * * * ****
23172 // **** *** * * ** ***
23173 // **** ********* ******
23174 // ******* ***** *******
23175 // ********* ****** **
23176 // ** ****** ******
23177 // ** ******* **
23178 // ** ******* ***
23179 // **** ******** ************
23180 // ************ ************
23181 // ******** *******
23182 // ****** ****
23183 // *** ***
23184 // ********************************
23185 #include <vector>
23186 #include <boost/test/unit_test.hpp>
23187 #include <boost/foreach.hpp>
23188
23189 #include "../main.h"
23190 #include "../wallet.h"
23191
23192 using namespace std;
23193 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
23194 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
23195 extern bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType);
23196
23197 BOOST_AUTO_TEST_SUITE(script_tests)
23198
23199 BOOST_AUTO_TEST_CASE(script_PushData)
23200 {
23201 // Check that PUSHDATA1, PUSHDATA2, and PUSHDATA4 create the same value on
23202 // the stack as the 1-75 opcodes do.
23203 static const unsigned char direct[] = { 1, 0x5a };
23204 static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
23205 static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
23206 static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
23207
23208 vector<vector<unsigned char> > directStack;
23209 BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0));
23210
23211 vector<vector<unsigned char> > pushdata1Stack;
23212 BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0));
23213 BOOST_CHECK(pushdata1Stack == directStack);
23214
23215 vector<vector<unsigned char> > pushdata2Stack;
23216 BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0));
23217 BOOST_CHECK(pushdata2Stack == directStack);
23218
23219 vector<vector<unsigned char> > pushdata4Stack;
23220 BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0));
23221 BOOST_CHECK(pushdata4Stack == directStack);
23222 }
23223
23224 CScript
23225 sign_multisig(CScript scriptPubKey, std::vector<CKey> keys, CTransaction transaction)
23226 {
23227 uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL);
23228
23229 CScript result;
23230 //
23231 // NOTE: CHECKMULTISIG has an unfortunate bug; it requires
23232 // one extra item on the stack, before the signatures.
23233 // Putting OP_0 on the stack is the workaround;
23234 // fixing the bug would mean splitting the blockchain (old
23235 // clients would not accept new CHECKMULTISIG transactions,
23236 // and vice-versa)
23237 //
23238 result << OP_0;
23239 BOOST_FOREACH(CKey key, keys)
23240 {
23241 vector<unsigned char> vchSig;
23242 BOOST_CHECK(key.Sign(hash, vchSig));
23243 vchSig.push_back((unsigned char)SIGHASH_ALL);
23244 result << vchSig;
23245 }
23246 return result;
23247 }
23248 CScript
23249 sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
23250 {
23251 std::vector<CKey> keys;
23252 keys.push_back(key);
23253 return sign_multisig(scriptPubKey, keys, transaction);
23254 }
23255
23256 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
23257 {
23258 CKey key1, key2, key3;
23259 key1.MakeNewKey();
23260 key2.MakeNewKey();
23261 key3.MakeNewKey();
23262
23263 CScript scriptPubKey12;
23264 scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG;
23265
23266 CTransaction txFrom12;
23267 txFrom12.vout.resize(1);
23268 txFrom12.vout[0].scriptPubKey = scriptPubKey12;
23269
23270 CTransaction txTo12;
23271 txTo12.vin.resize(1);
23272 txTo12.vout.resize(1);
23273 txTo12.vin[0].prevout.n = 0;
23274 txTo12.vin[0].prevout.hash = txFrom12.GetHash();
23275 txTo12.vout[0].nValue = 1;
23276
23277 CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
23278 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
23279 txTo12.vout[0].nValue = 2;
23280 BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
23281
23282 CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
23283 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
23284
23285 CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
23286 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
23287 }
23288
23289 BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
23290 {
23291 CKey key1, key2, key3, key4;
23292 key1.MakeNewKey();
23293 key2.MakeNewKey();
23294 key3.MakeNewKey();
23295 key4.MakeNewKey();
23296
23297 CScript scriptPubKey23;
23298 scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG;
23299
23300 CTransaction txFrom23;
23301 txFrom23.vout.resize(1);
23302 txFrom23.vout[0].scriptPubKey = scriptPubKey23;
23303
23304 CTransaction txTo23;
23305 txTo23.vin.resize(1);
23306 txTo23.vout.resize(1);
23307 txTo23.vin[0].prevout.n = 0;
23308 txTo23.vin[0].prevout.hash = txFrom23.GetHash();
23309 txTo23.vout[0].nValue = 1;
23310
23311 std::vector<CKey> keys;
23312 keys.push_back(key1); keys.push_back(key2);
23313 CScript goodsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
23314 BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, 0));
23315
23316 keys.clear();
23317 keys.push_back(key1); keys.push_back(key3);
23318 CScript goodsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
23319 BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, 0));
23320
23321 keys.clear();
23322 keys.push_back(key2); keys.push_back(key3);
23323 CScript goodsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
23324 BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, 0));
23325
23326 keys.clear();
23327 keys.push_back(key2); keys.push_back(key2); // Can't re-use sig
23328 CScript badsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
23329 BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, 0));
23330
23331 keys.clear();
23332 keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
23333 CScript badsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
23334 BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, 0));
23335
23336 keys.clear();
23337 keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
23338 CScript badsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
23339 BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, 0));
23340
23341 keys.clear();
23342 keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
23343 CScript badsig4 = sign_multisig(scriptPubKey23, keys, txTo23);
23344 BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, 0));
23345
23346 keys.clear();
23347 keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
23348 CScript badsig5 = sign_multisig(scriptPubKey23, keys, txTo23);
23349 BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, 0));
23350
23351 keys.clear(); // Must have signatures
23352 CScript badsig6 = sign_multisig(scriptPubKey23, keys, txTo23);
23353 BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, 0));
23354 }
23355
23356
23357 BOOST_AUTO_TEST_SUITE_END()