Skip to content

Instantly share code, notes, and snippets.

@nodech
Created May 21, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodech/36f9c4cd3072b1b4c68937df54ef05c9 to your computer and use it in GitHub Desktop.
Save nodech/36f9c4cd3072b1b4c68937df54ef05c9 to your computer and use it in GitHub Desktop.
// copyright (c) 2018 the bitcoin developers
// distributed under the mit software license, see the accompanying
// file copying or http://www.opensource.org/licenses/mit-license.php.
#include "test/test_bitcoin.h"
#include "policy/policy.h"
#include "script/interpreter.h"
#include <boost/test/unit_test.hpp>
#include <array>
typedef std::vector<uint8_t> valtype;
typedef std::vector<valtype> stacktype;
std::array<uint32_t, 3> flagset{0, standard_script_verify_flags,
mandatory_script_verify_flags};
boost_fixture_test_suite(monolith_opcodes_tests, basictestingsetup)
/**
* general utility functions to check for script passing/failing.
*/
static void checktestresultforallflags(const stacktype &original_stack,
const cscript &script,
const stacktype &expected) {
basesignaturechecker sigchecker;
for (uint32_t flags : flagset) {
scripterror err = script_err_ok;
stacktype stack{original_stack};
bool r =
evalscript(stack, script, flags | script_enable_monolith_opcodes,
sigchecker, &err);
if (!r)
std::cout << err << std::endl;
boost_check(r);
boost_check(stack == expected);
// make sure that if we do not pass the monolith flag, opcodes are still
// disabled.
stack = original_stack;
r = evalscript(stack, script, flags, sigchecker, &err);
boost_check(!r);
boost_check_equal(err, script_err_disabled_opcode);
}
}
static void checkerror(uint32_t flags, const stacktype &original_stack,
const cscript &script, scripterror expected_error) {
basesignaturechecker sigchecker;
scripterror err = script_err_ok;
stacktype stack{original_stack};
bool r = evalscript(stack, script, flags | script_enable_monolith_opcodes,
sigchecker, &err);
boost_check(!r);
boost_check_equal(err, expected_error);
// make sure that if we do not pass the monolith flag, opcodes are still
// disabled.
stack = original_stack;
r = evalscript(stack, script, flags, sigchecker, &err);
boost_check(!r);
boost_check_equal(err, script_err_disabled_opcode);
}
static void checkerrorforallflags(const stacktype &original_stack,
const cscript &script,
scripterror expected_error) {
for (uint32_t flags : flagset) {
checkerror(flags, original_stack, script, expected_error);
}
}
static void checkoperror(const stacktype &original_stack, opcodetype op,
scripterror expected_error) {
checkerrorforallflags(original_stack, cscript() << op, expected_error);
}
static void checkallbitwiseoperrors(const stacktype &stack,
scripterror expected_error) {
checkoperror(stack, op_and, expected_error);
checkoperror(stack, op_or, expected_error);
checkoperror(stack, op_xor, expected_error);
}
static void checkbinaryop(const valtype &a, const valtype &b, opcodetype op,
const valtype &expected) {
checktestresultforallflags({a, b}, cscript() << op, {expected});
}
static valtype negativevaltype(const valtype &v) {
valtype r(v);
if (r.size() > 0) {
r[r.size() - 1] ^= 0x80;
}
cscriptnum::minimallyencode(r);
return r;
}
boost_auto_test_case(negative_valtype_test) {
// test zero values
boost_check(negativevaltype({}) == valtype{});
boost_check(negativevaltype({0x00}) == valtype{});
boost_check(negativevaltype({0x80}) == valtype{});
boost_check(negativevaltype({0x00, 0x00}) == valtype{});
boost_check(negativevaltype({0x00, 0x80}) == valtype{});
// non-zero values
boost_check(negativevaltype({0x01}) == valtype{0x81});
boost_check(negativevaltype({0x81}) == valtype{0x01});
boost_check(negativevaltype({0x02, 0x01}) == (valtype{0x02, 0x81}));
boost_check(negativevaltype({0x02, 0x81}) == (valtype{0x02, 0x01}));
boost_check(negativevaltype({0xff, 0x02, 0x01}) ==
(valtype{0xff, 0x02, 0x81}));
boost_check(negativevaltype({0xff, 0x02, 0x81}) ==
(valtype{0xff, 0x02, 0x01}));
boost_check(negativevaltype({0xff, 0xff, 0x02, 0x01}) ==
(valtype{0xff, 0xff, 0x02, 0x81}));
boost_check(negativevaltype({0xff, 0xff, 0x02, 0x81}) ==
(valtype{0xff, 0xff, 0x02, 0x01}));
// should not be overly-minimized
boost_check(negativevaltype({0xff, 0x80}) == (valtype{0xff, 0x00}));
boost_check(negativevaltype({0xff, 0x00}) == (valtype{0xff, 0x80}));
}
/**
* bitwise opcodes
*/
static void runtestforallbitwiseopcodes(const valtype &a, const valtype &b,
const valtype &expected_and,
const valtype &expected_or,
const valtype &expected_xor) {
// bitwise ops are commutative, so we check both ways.
checkbinaryop(a, b, op_and, expected_and);
checkbinaryop(b, a, op_and, expected_and);
checkbinaryop(a, b, op_or, expected_or);
checkbinaryop(b, a, op_or, expected_or);
checkbinaryop(a, b, op_xor, expected_xor);
checkbinaryop(b, a, op_xor, expected_xor);
}
static void runtestforallbitwiseopcodessizes(const valtype &a, const valtype &b,
const valtype &expected_and,
const valtype &expected_or,
const valtype &expected_xor) {
valtype wa, wb, wand, wor, wxor;
for (size_t i = 0; i < a.size(); i++) {
wa.push_back(a[i]);
wb.push_back(b[i]);
wand.push_back(expected_and[i]);
wor.push_back(expected_or[i]);
wxor.push_back(expected_xor[i]);
runtestforallbitwiseopcodes(wa, wb, wand, wor, wxor);
}
}
static void testbitwiseopcodes(const valtype &a, const valtype &b,
const valtype &expected_and,
const valtype &expected_or) {
valtype expected_xor(expected_and.size());
for (size_t i = 0; i < a.size(); i++) {
// a ^ b = (a | b) & ~(a & b)
expected_xor[i] = expected_or[i] & ~expected_and[i];
}
runtestforallbitwiseopcodessizes(a, b, expected_and, expected_or,
expected_xor);
valtype nota(a.size());
valtype notb(b.size());
valtype nand(expected_and.size());
valtype nor(expected_or.size());
for (size_t i = 0; i < a.size(); i++) {
nota[i] = ~a[i];
notb[i] = ~b[i];
nand[i] = ~expected_and[i];
nor[i] = ~expected_or[i];
}
// ~a & ~b == ~(a | b)
// ~a | ~b == ~(a & b)
// ~a ^ ~b == a ^ b
runtestforallbitwiseopcodessizes(nota, notb, nor, nand, expected_xor);
}
boost_auto_test_case(bitwise_opcodes_test) {
// check that empty ops works.
runtestforallbitwiseopcodes({}, {}, {}, {}, {});
// run all variations of zeros and ones.
valtype allzeros(max_script_element_size, 0);
valtype allones(max_script_element_size, 0xff);
boost_check_equal(allzeros.size(), max_script_element_size);
boost_check_equal(allones.size(), max_script_element_size);
testbitwiseopcodes(allzeros, allzeros, allzeros, allzeros);
testbitwiseopcodes(allzeros, allones, allzeros, allones);
testbitwiseopcodes(allones, allones, allones, allones);
// let's use two random a and b.
valtype a{
0x34, 0x0e, 0x7e, 0x17, 0x83, 0x66, 0x1a, 0x81, 0x45, 0x8d, 0x26, 0x26,
0xbc, 0xbd, 0x56, 0xe7, 0xf2, 0x1c, 0xec, 0xf6, 0x79, 0x8c, 0x3e, 0x58,
0x0f, 0x86, 0xcf, 0x53, 0xbe, 0x66, 0x8f, 0xa7, 0xbe, 0xf6, 0x30, 0x12,
0x8d, 0x01, 0x00, 0x37, 0x7f, 0x5b, 0x64, 0x50, 0x63, 0x40, 0x6a, 0x44,
0xf5, 0x7e, 0x02, 0xc7, 0xab, 0x45, 0xcf, 0x6a, 0x98, 0x61, 0xe8, 0xb8,
0xc4, 0x9e, 0x11, 0xe8, 0x30, 0x71, 0x07, 0x73, 0xa2, 0x4d, 0xdd, 0xa6,
0x6c, 0xf4, 0x2a, 0x22, 0xa0, 0xac, 0xdc, 0xf4, 0xcc, 0xfb, 0x4d, 0xe3,
0x55, 0xde, 0x44, 0x46, 0x32, 0x36, 0x93, 0xb4, 0xd9, 0xd1, 0x3b, 0x06,
0x09, 0x6a, 0x64, 0xc3, 0x18, 0x58, 0xc4, 0x9f, 0x1b, 0x6a, 0xa3, 0xab,
0x59, 0x37, 0xbd, 0x36, 0x97, 0x35, 0x26, 0x87, 0x63, 0x58, 0x08, 0x6e,
0x5e, 0x46, 0xcf, 0x15, 0x33, 0xfc, 0x46, 0x45, 0x97, 0x61, 0x4b, 0xb8,
0xec, 0xdd, 0x1b, 0x69, 0x6e, 0x8a, 0x27, 0xf9, 0xcd, 0x4b, 0x5c, 0xa4,
0x84, 0x18, 0xd5, 0x23, 0x50, 0xc6, 0x63, 0xbe, 0xca, 0xd3, 0xd0, 0x91,
0x39, 0x16, 0x6a, 0x6e, 0xd6, 0x09, 0x18, 0x52, 0x05, 0x6a, 0xa7, 0xf7,
0x64, 0xa3, 0xf0, 0xba, 0x75, 0xc5, 0x9c, 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x75, 0xe1, 0xbc, 0x38,
0x31, 0xdc, 0xe9, 0x61, 0x53, 0x1e, 0xc8, 0x4b, 0x80, 0x94, 0x5c, 0x03,
0xdd, 0x4b, 0xae, 0xa8, 0x54, 0xe9, 0x8b, 0x23, 0x20, 0x21, 0xc8, 0x03,
0x83, 0x33, 0x5f, 0x11, 0x37, 0xfc, 0xd5, 0xb3, 0x11, 0x9a, 0x06, 0x0d,
0xbf, 0xcd, 0xc7, 0x22, 0x88, 0xb8, 0xc9, 0x3f, 0xec, 0x7c, 0x11, 0x96,
0x6a, 0xa0, 0x57, 0xdf, 0x5b, 0xde, 0xa2, 0x09, 0x11, 0xd3, 0xfd, 0xbf,
0x84, 0x7a, 0x9d, 0x3a, 0xba, 0x0f, 0x6d, 0x01, 0xad, 0xbc, 0xb9, 0xd8,
0x8a, 0xe4, 0xd6, 0xa2, 0x04, 0x93, 0xe0, 0x02, 0xd2, 0x45, 0x49, 0x14,
0x8e, 0x84, 0x9c, 0x7c, 0x57, 0x1b, 0x05, 0x27, 0xf6, 0x59, 0x83, 0xd1,
0xf4, 0xb6, 0x2f, 0xbe, 0x6e, 0x35, 0x7e, 0x97, 0x10, 0xf5, 0x42, 0x1a,
0xc9, 0x4d, 0xb9, 0x07, 0x71, 0x6d, 0xd1, 0x96, 0xc3, 0x88, 0xb6, 0xe6,
0x0e, 0x8a, 0x8a, 0xd7};
boost_check_equal(a.size(), max_script_element_size);
boost_check_equal(b.size(), max_script_element_size);
valtype aandb{
0x10, 0x0e, 0x18, 0x01, 0x83, 0x00, 0x1a, 0x00, 0x41, 0x8c, 0x00, 0x00,
0x90, 0x1c, 0x54, 0xa0, 0x20, 0x14, 0x2c, 0x44, 0x71, 0x88, 0x0a, 0x48,
0x01, 0x80, 0xc8, 0x03, 0x8e, 0x42, 0x03, 0x06, 0x3e, 0x16, 0x30, 0x00,
0x81, 0x00, 0x00, 0x17, 0x70, 0x49, 0x24, 0x10, 0x43, 0x40, 0x62, 0x04,
0x65, 0x2a, 0x02, 0x00, 0x8b, 0x04, 0xc7, 0x2a, 0x18, 0x61, 0x08, 0x00,
0x04, 0x0a, 0x10, 0x48, 0x30, 0x00, 0x05, 0x20, 0xa2, 0x01, 0xd4, 0x22,
0x6c, 0x14, 0x0a, 0x20, 0xa0, 0x00, 0x1c, 0x04, 0x4c, 0xca, 0x45, 0xc3,
0x40, 0x5a, 0x44, 0x00, 0x22, 0x36, 0x92, 0x00, 0x90, 0x50, 0x23, 0x02,
0x09, 0x28, 0x60, 0xc3, 0x08, 0x08, 0xc4, 0x0c, 0x0b, 0x6a, 0xa0, 0x82,
0x49, 0x31, 0x19, 0x30, 0x15, 0x20, 0x24, 0x85, 0x61, 0x58, 0x08, 0x2e,
0x16, 0x40, 0xcb, 0x14, 0x22, 0xd0, 0x46, 0x00, 0x87, 0x21, 0x41, 0x38,
0x44, 0xd8, 0x10, 0x28, 0x22, 0x80, 0x03, 0xd0, 0xc8, 0x01, 0x0c, 0x24,
0x80, 0x08, 0x85, 0x01, 0x50, 0x84, 0x43, 0x04, 0x48, 0x40, 0xc0, 0x10,
0x29, 0x02, 0x40, 0x04, 0x44, 0x08, 0x10, 0x50, 0x00, 0x4a, 0x26, 0xe5,
0x44, 0x00, 0x90, 0x28, 0x15, 0x81, 0x9c, 0xf0, 0x98, 0x30, 0x48, 0x65,
0x42, 0x9b, 0x00, 0x16, 0x00, 0xfb, 0x00, 0x32, 0x26, 0xa0, 0x01, 0x80,
0x25, 0x09, 0x21, 0x04, 0x00, 0x4c, 0x80, 0x10, 0x08, 0x84, 0x24, 0x60,
0x04, 0x8a, 0xa2, 0x4a, 0xe1, 0x00, 0x23, 0xb0, 0x02, 0x00, 0x22, 0x93,
0x20, 0x21, 0xc8, 0x4a, 0x44, 0x58, 0x04, 0x60, 0x00, 0x30, 0x28, 0x22,
0x20, 0x2e, 0x40, 0x04, 0x04, 0x54, 0x00, 0x25, 0x10, 0x12, 0x40, 0x13,
0x20, 0x20, 0x00, 0x58, 0x06, 0x14, 0x80, 0x50, 0x00, 0x04, 0x80, 0x00,
0x0a, 0x00, 0xc0, 0x10, 0x21, 0x40, 0x48, 0x89, 0x05, 0xa1, 0x14, 0xe2,
0x04, 0x01, 0x2b, 0x80, 0x20, 0x19, 0x43, 0x30, 0x49, 0x30, 0x48, 0x00,
0x20, 0xa0, 0xe1, 0x00, 0x40, 0x28, 0x4a, 0x0a, 0xa0, 0x49, 0x44, 0x04,
0x30, 0xe0, 0x11, 0x00, 0x10, 0x90, 0x09, 0x24, 0x09, 0x01, 0x10, 0x64,
0x92, 0x84, 0x04, 0x2b, 0x02, 0x18, 0x0c, 0x48, 0x10, 0x02, 0x00, 0x10,
0xb1, 0x88, 0x0a, 0x00, 0xa6, 0x08, 0xa0, 0x88, 0x51, 0x88, 0x80, 0x95,
0x85, 0x84, 0x10, 0x6d, 0x00, 0x63, 0x24, 0xc4, 0x14, 0xc8, 0x28, 0x82,
0x0b, 0xc3, 0x80, 0x13, 0x00, 0x46, 0x43, 0x86, 0x00, 0x02, 0x10, 0x89,
0x28, 0x09, 0x42, 0x55, 0x80, 0x03, 0x24, 0x46, 0x06, 0x80, 0x00, 0x34,
0x28, 0x21, 0xa0, 0x02, 0x20, 0x28, 0x81, 0x09, 0x08, 0x01, 0x74, 0x04,
0x58, 0x28, 0xc8, 0x88, 0x2c, 0x34, 0x00, 0x0d, 0x08, 0x82, 0x30, 0x22,
0x87, 0x10, 0x00, 0x4c, 0x9a, 0x03, 0xd8, 0x86, 0x11, 0xe0, 0x88, 0x28,
0x11, 0x50, 0x01, 0x41, 0x50, 0x0c, 0x08, 0x43, 0x00, 0x84, 0x5c, 0x03,
0x1c, 0x0b, 0xa8, 0x00, 0x40, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
0x00, 0x21, 0x1c, 0x10, 0x27, 0x58, 0x00, 0xa3, 0x00, 0x80, 0x02, 0x0d,
0x2d, 0xc4, 0x44, 0x02, 0x88, 0x00, 0x48, 0x15, 0x0c, 0x10, 0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xff, 0xbf, 0xfa,
0xdf, 0xf7, 0xff, 0xaa, 0x3d, 0x9b, 0xfc, 0x6e, 0xf3, 0xcd, 0xcb, 0xd5,
0xaf, 0xdc, 0xff, 0xfc, 0x77, 0x9b, 0x87, 0xff, 0xf6, 0x7f, 0x93, 0xfb,
0xf5, 0xfe, 0xaf, 0xbe, 0x6e, 0x75, 0xff, 0xff, 0xf4, 0xfd, 0xe7, 0x5a,
0xe9, 0xff, 0xfb, 0x17, 0xf1, 0x6d, 0xf3, 0xd7, 0xd3, 0xbc, 0xf7, 0xe7,
0x4e, 0x9e, 0x9a, 0xf7};
testbitwiseopcodes(a, b, aandb, aorb);
// check errors conditions.
// 1. less than 2 elements on stack.
checkallbitwiseoperrors({}, script_err_invalid_stack_operation);
checkallbitwiseoperrors({{}}, script_err_invalid_stack_operation);
checkallbitwiseoperrors({{0x00}}, script_err_invalid_stack_operation);
checkallbitwiseoperrors({{0xab, 0xcd, 0xef}},
script_err_invalid_stack_operation);
checkallbitwiseoperrors({a}, script_err_invalid_stack_operation);
checkallbitwiseoperrors({b}, script_err_invalid_stack_operation);
// 2. operand of mismatching length
checkallbitwiseoperrors({{}, {0x00}}, script_err_invalid_operand_size);
checkallbitwiseoperrors({{0x00}, {}}, script_err_invalid_operand_size);
checkallbitwiseoperrors({{0x00}, {0xab, 0xcd, 0xef}},
script_err_invalid_operand_size);
checkallbitwiseoperrors({{0xab, 0xcd, 0xef}, {0x00}},
script_err_invalid_operand_size);
checkallbitwiseoperrors({{}, a}, script_err_invalid_operand_size);
checkallbitwiseoperrors({b, {}}, script_err_invalid_operand_size);
}
/**
* string opcodes.
*/
static void checkstringop(const valtype &a, const valtype &b,
const valtype &n) {
checkbinaryop(a, b, op_cat, n);
// check concatenation with empty elements.
checkbinaryop(a, {}, op_cat, a);
checkbinaryop(b, {}, op_cat, b);
checkbinaryop({}, a, op_cat, a);
checkbinaryop({}, b, op_cat, b);
// split n into a and b.
checktestresultforallflags({n}, cscript() << a.size() << op_split, {a, b});
// combine split and cat.
checktestresultforallflags({n}, cscript() << a.size() << op_split << op_cat,
{n});
checktestresultforallflags(
{a, b}, cscript() << op_cat << a.size() << op_split, {a, b});
// split away empty elements.
checktestresultforallflags({a}, cscript() << 0 << op_split, {{}, a});
checktestresultforallflags({b}, cscript() << 0 << op_split, {{}, b});
checktestresultforallflags({a}, cscript() << a.size() << op_split, {a, {}});
checktestresultforallflags({b}, cscript() << b.size() << op_split, {b, {}});
// out of bound split.
checkerrorforallflags({a}, cscript() << (a.size() + 1) << op_split,
script_err_invalid_split_range);
checkerrorforallflags({b}, cscript() << (b.size() + 1) << op_split,
script_err_invalid_split_range);
checkerrorforallflags({n}, cscript() << (n.size() + 1) << op_split,
script_err_invalid_split_range);
checkerrorforallflags({a}, cscript() << (-1) << op_split,
script_err_invalid_split_range);
}
boost_auto_test_case(string_opcodes_test) {
// check for empty string.
checkstringop({}, {}, {});
// check for simple concats.
checkstringop({0x00}, {0x00}, {0x00, 0x00});
checkstringop({0xab}, {0xcd}, {0xab, 0xcd});
checkstringop({0xab, 0xcd, 0xef}, {0x12, 0x34, 0x56, 0x78},
{0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78});
const valtype n{
0x7b, 0x59, 0xf8, 0x07, 0xc6, 0xc0, 0x70, 0xbc, 0x52, 0x7b, 0xf5, 0xaf,
0xf5, 0xdd, 0xeb, 0xdc, 0x41, 0xaa, 0x07, 0xf6, 0x80, 0x8d, 0x5d, 0x4d,
0xbc, 0x91, 0xcd, 0x0a, 0x14, 0x85, 0xd9, 0x98, 0xb6, 0xab, 0x2e, 0x37,
0x76, 0x78, 0x34, 0x8b, 0x2b, 0xfb, 0x59, 0x3b, 0xea, 0x45, 0x46, 0x72,
0x64, 0x64, 0x83, 0x73, 0xc3, 0x1d, 0xca, 0x86, 0x03, 0x91, 0xfc, 0xc0,
0xc4, 0xdf, 0x17, 0x83, 0x22, 0x5d, 0x50, 0xc5, 0x31, 0x45, 0xaf, 0xbc,
0xf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x3e, 0x38, 0x51,
0x51, 0xd1, 0xd5, 0x4e, 0xf7, 0x65, 0xe5, 0x42, 0x3c, 0xa8, 0x28, 0x19,
0x02, 0xa7, 0xc9, 0x1c, 0x24, 0xa7, 0x91, 0xfe, 0xa1, 0xbc, 0xb9, 0x15,
0xba, 0x49, 0xac, 0xeb, 0x81, 0xf7, 0xc1, 0xfc, 0xf9, 0x51, 0x0d, 0xa1,
0xe8, 0x71, 0x2c, 0x4e, 0x59, 0xc1, 0x3a, 0x2a, 0xcc, 0x61, 0xee, 0xe5,
0x2a, 0x88, 0xf8, 0xec, 0xbd, 0x90, 0xc0, 0x96, 0xe0, 0x93, 0x1f, 0x78,
0xbe, 0x6b, 0xb1, 0x4c, 0x46, 0x2a, 0x86, 0xd9, 0x2d, 0x20, 0x29, 0xb4,
0x44, 0x15, 0xb2, 0x7e};
boost_check_equal(n.size(), max_script_element_size);
for (size_t i = 0; i <= max_script_element_size; i++) {
valtype a(n.begin(), n.begin() + i);
valtype b(n.begin() + i, n.end());
checkstringop(a, b, n);
// one more char and we are oversize.
valtype extraa = a;
extraa.push_back(0xaf);
valtype extrab = b;
extrab.push_back(0xad);
checkoperror({extraa, b}, op_cat, script_err_push_size);
checkoperror({a, extrab}, op_cat, script_err_push_size);
checkoperror({extraa, extrab}, op_cat, script_err_push_size);
}
// check error conditions.
checkoperror({}, op_cat, script_err_invalid_stack_operation);
checkoperror({}, op_split, script_err_invalid_stack_operation);
checkoperror({{}}, op_cat, script_err_invalid_stack_operation);
checkoperror({{}}, op_split, script_err_invalid_stack_operation);
checkoperror({{0x00}}, op_cat, script_err_invalid_stack_operation);
checkoperror({{0x00}}, op_split, script_err_invalid_stack_operation);
checkoperror({{0xab, 0xcd, 0xef}}, op_cat,
script_err_invalid_stack_operation);
checkoperror({{0xab, 0xcd, 0xef}}, op_split,
script_err_invalid_stack_operation);
}
/**
* type conversion opcodes.
*/
static void checktypeconversionop(const valtype &bin, const valtype &num) {
// check bin2num.
//checktestresultforallflags({bin}, cscript() << op_bin2num, {num});
// check num2bin. negative 0 is rebuilt as regular zero, so we need a tweak.
valtype rebuilt_bin{bin};
if (num.size() == 0 && bin.size() > 0) {
rebuilt_bin[rebuilt_bin.size() - 1] &= 0x7f;
}
checktestresultforallflags({num}, cscript() << bin.size() << op_num2bin,
{rebuilt_bin});
// check roundtrip with num2bin.
//checktestresultforallflags(
// {bin}, cscript() << op_bin2num << bin.size() << op_num2bin,
// {rebuilt_bin});
// grow and shrink back down using num2bin.
//checktestresultforallflags({bin},
// cscript()
// << max_script_element_size << op_num2bin
// << bin.size() << op_num2bin,
// {rebuilt_bin});
//checktestresultforallflags({num},
// cscript()
// << max_script_element_size << op_num2bin
// << bin.size() << op_num2bin,
// {rebuilt_bin});
//// bin2num is indempotent.
//checktestresultforallflags({bin}, cscript() << op_bin2num << op_bin2num,
// {num});
}
static void checkbin2numerror(const stacktype &original_stack,
scripterror expected_error) {
checkerrorforallflags(original_stack, cscript() << op_bin2num,
expected_error);
}
static void checknum2binerror(const stacktype &original_stack,
scripterror expected_error) {
checkerrorforallflags(original_stack, cscript() << op_num2bin,
expected_error);
}
boost_auto_test_case(type_conversion_test) {
valtype empty;
checktypeconversionop(empty, empty);
valtype paddedzero, paddednegzero;
for (size_t i = 0; i < max_script_element_size; i++) {
checktypeconversionop(paddedzero, empty);
paddedzero.push_back(0x00);
paddednegzero.push_back(0x80);
checktypeconversionop(paddednegzero, empty);
paddednegzero[paddednegzero.size() - 1] = 0x00;
}
// merge leading byte when sign bit isn't used.
std::vector<uint8_t> k{0x7f}, negk{0xff};
std::vector<uint8_t> kpadded = k, negkpadded = negk;
for (size_t i = 0; i < max_script_element_size; i++) {
checktypeconversionop(kpadded, k);
kpadded.push_back(0x00);
checktypeconversionop(negkpadded, negk);
negkpadded[negkpadded.size() - 1] &= 0x7f;
negkpadded.push_back(0x80);
}
// some known values.
checktypeconversionop({0xab, 0xcd, 0xef, 0x00}, {0xab, 0xcd, 0xef, 0x00});
checktypeconversionop({0xab, 0xcd, 0x7f, 0x00}, {0xab, 0xcd, 0x7f});
checktypeconversionop({0xab, 0xcd, 0x7f, 0x00}, {0xab, 0xcd, 0x7f});
checktypeconversionop({
0xab, 0xcd, 0x7f, 0x10, 0x01, 0x00
}, {
0xab, 0xcd, 0x7f, 0x10, 0x01
});
// reductions
checktypeconversionop({0xab, 0xcd, 0xef, 0x42, 0x80},
{0xab, 0xcd, 0xef, 0xc2});
checktypeconversionop({0xab, 0xcd, 0x7f, 0x42, 0x00},
{0xab, 0xcd, 0x7f, 0x42});
// empty stack is an error.
checkbin2numerror({}, script_err_invalid_stack_operation);
checknum2binerror({}, script_err_invalid_stack_operation);
// num2bin require 2 elements on the stack.
checknum2binerror({{0x00}}, script_err_invalid_stack_operation);
// values that do not fit in 4 bytes are considered out of range for
// bin2num.
checkbin2numerror({{0xab, 0xcd, 0xef, 0xc2, 0x80}},
script_err_invalid_number_range);
checkbin2numerror({{0x00, 0x00, 0x00, 0x80, 0x80}},
script_err_invalid_number_range);
// num2bin must not generate oversized push.
valtype largezero(max_script_element_size, 0);
boost_check_equal(largezero.size(), max_script_element_size);
checktypeconversionop(largezero, {});
checknum2binerror({{}, {0x09, 0x02}}, script_err_push_size);
// check that the requested encoding is possible.
checknum2binerror({{0xab, 0xcd, 0xef, 0x80}, {0x03}},
script_err_impossible_encoding);
}
/**
* arithmetic opcodes
*/
static void checkdivmod(const valtype &a, const valtype &b,
const valtype &divexpected,
const valtype &modexpected) {
// negative values for division
checkbinaryop(a, b, op_div, divexpected);
checkbinaryop(a, negativevaltype(b), op_div, negativevaltype(divexpected));
checkbinaryop(negativevaltype(a), b, op_div, negativevaltype(divexpected));
checkbinaryop(negativevaltype(a), negativevaltype(b), op_div, divexpected);
// negative values for modulo
checkbinaryop(a, b, op_mod, modexpected);
checkbinaryop(a, negativevaltype(b), op_mod, modexpected);
checkbinaryop(negativevaltype(a), b, op_mod, negativevaltype(modexpected));
checkbinaryop(negativevaltype(a), negativevaltype(b), op_mod,
negativevaltype(modexpected));
// div/mod by zero
for (uint32_t flags : flagset) {
checkerror(flags, {a, {}}, cscript() << op_div, script_err_div_by_zero);
checkerror(flags, {b, {}}, cscript() << op_div, script_err_div_by_zero);
if (flags & script_verify_minimaldata) {
checkerror(flags, {a, {0x00}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {a, {0x80}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {a, {0x00, 0x00}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {a, {0x00, 0x80}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {b, {0x00}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {b, {0x80}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {b, {0x00, 0x00}}, cscript() << op_div,
script_err_unknown_error);
checkerror(flags, {b, {0x00, 0x80}}, cscript() << op_div,
script_err_unknown_error);
} else {
checkerror(flags, {a, {0x00}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {a, {0x80}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {a, {0x00, 0x00}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {a, {0x00, 0x80}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {b, {0x00}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {b, {0x80}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {b, {0x00, 0x00}}, cscript() << op_div,
script_err_div_by_zero);
checkerror(flags, {b, {0x00, 0x80}}, cscript() << op_div,
script_err_div_by_zero);
}
}
// division identities
checkbinaryop(a, {0x01}, op_div, a);
checkbinaryop(a, {0x81}, op_div, negativevaltype(a));
checkbinaryop(a, a, op_div, {0x01});
checkbinaryop(a, negativevaltype(a), op_div, {0x81});
checkbinaryop(negativevaltype(a), a, op_div, {0x81});
checkbinaryop(b, {0x01}, op_div, b);
checkbinaryop(b, {0x81}, op_div, negativevaltype(b));
checkbinaryop(b, b, op_div, {0x01});
checkbinaryop(b, negativevaltype(b), op_div, {0x81});
checkbinaryop(negativevaltype(b), b, op_div, {0x81});
// modulo identities
// a % b % b = a % b
checktestresultforallflags(
{a, b}, cscript() << op_mod << cscriptnum(b, true).getint() << op_mod,
{modexpected});
}
static void checkdivmoderror(const stacktype &original_stack,
scripterror expected_error) {
checkoperror(original_stack, op_div, expected_error);
checkoperror(original_stack, op_mod, expected_error);
}
boost_auto_test_case(div_and_mod_opcode_tests) {
checkdivmoderror({}, script_err_invalid_stack_operation);
checkdivmoderror({{}}, script_err_invalid_stack_operation);
// checkops not valid numbers
checkdivmoderror(
{{0x01, 0x02, 0x03, 0x04, 0x05}, {0x01, 0x02, 0x03, 0x04, 0x05}},
script_err_unknown_error);
checkdivmoderror({{0x01, 0x02, 0x03, 0x04, 0x05}, {0x01}},
script_err_unknown_error);
checkdivmoderror({{0x01, 0x05}, {0x01, 0x02, 0x03, 0x04, 0x05}},
script_err_unknown_error);
// 0x185377af / 0x85f41b01 = -4
// 0x185377af % 0x85f41b01 = 0x00830bab
// 408123311 / -99883777 = -4
// 408123311 % -99883777 = 8588203
checkdivmod({0xaf, 0x77, 0x53, 0x18}, {0x01, 0x1b, 0xf4, 0x85}, {0x84},
{0xab, 0x0b, 0x83, 0x00});
// 0x185377af / 0x00001b01 = 0xe69d
// 0x185377af % 0x00001b01 = 0x0212
// 408123311 / 6913 = 59037
// 408123311 % 6913 = 530
checkdivmod({0xaf, 0x77, 0x53, 0x18}, {0x01, 0x1b}, {0x9d, 0xe6, 0x00},
{0x12, 0x02});
// 15/4 = 3 (and negative operands)
checkdivmod({0x0f}, {0x04}, {0x03}, {0x03});
// 15000/4 = 3750 (and negative operands)
checkdivmod({0x98, 0x3a}, {0x04}, {0xa6, 0x0e}, {});
// 15000/4000 = 3 (and negative operands)
checkdivmod({0x98, 0x3a}, {0xa0, 0x0f}, {0x03}, {0xb8, 0x0b});
// 15000000/4000 = 3750 (and negative operands)
checkdivmod({0xc0, 0xe1, 0xe4, 0x00}, {0xa0, 0x0f}, {0xa6, 0x0e}, {});
// 15000000/4 = 3750000 (and negative operands)
checkdivmod({0xc0, 0xe1, 0xe4, 0x00}, {0x04}, {0x70, 0x38, 0x39}, {});
// 56488123 % 321 = 148 (and negative operands)
checkdivmod({0xbb, 0xf0, 0x5d, 0x03}, {0x41, 0x01}, {0x67, 0xaf, 0x02},
{0x94, 0x00});
// 56488123 % 3 = 1 (and negative operands)
checkdivmod({0xbb, 0xf0, 0x5d, 0x03}, {0x03}, {0x3e, 0x50, 0x1f, 0x01},
{0x01});
// 56488123 % 564881230 = 56488123 (and negative operands)
checkdivmod({0xbb, 0xf0, 0x5d, 0x03}, {0x4e, 0x67, 0xab, 0x21}, {},
{0xbb, 0xf0, 0x5d, 0x03});
}
boost_auto_test_suite_end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment