Changeset View
Changeset View
Standalone View
Standalone View
src/test/scriptnum63bit.h
- This file was added.
// Copyright (c) 2009-2010 Satoshi Nakamoto | |||||
// Copyright (c) 2009-2015 The Bitcoin Core developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#ifndef BITCOIN_TEST_SCRIPTNUM63BIT_H | |||||
#define BITCOIN_TEST_SCRIPTNUM63BIT_H | |||||
#include <boost/multiprecision/cpp_int.hpp> | |||||
namespace scriptnum_emulated { | |||||
#undef BITCOIN_SCRIPT_SCRIPT_H | |||||
#undef HAVE_DECL___BUILTIN_SADDLL_OVERFLOW | |||||
#undef HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW | |||||
#include <script/script.h> | |||||
} // namespace scriptnum_emulated | |||||
typedef boost::multiprecision::checked_int128_t i128_t; | |||||
typedef std::vector<uint8_t> valtype; | |||||
typedef std::vector<valtype> stacktype; | |||||
const i128_t MAX_SCRIPT_63_BIT_INT = 0x7fff'ffff'ffff'ffff; | |||||
const i128_t MIN_SCRIPT_63_BIT_INT = -0x7fff'ffff'ffff'ffff; | |||||
static const size_t MAXIMUM_63_BIT_ELEMENT_SIZE = 8; | |||||
const static std::vector<valtype> INTERESTING_63_BIT_NUMBERS({ | |||||
{}, // 0 | |||||
{1}, | |||||
{0x81}, // -1 | |||||
{2}, | |||||
{0x82}, // -2 | |||||
{4}, | |||||
{0x84}, // -4 | |||||
{10}, | |||||
{0x8a}, // -10 | |||||
{100}, | |||||
{0xe4}, // -100 | |||||
{127}, | |||||
{0xff}, // -127 | |||||
{0, 1}, // 256 | |||||
{0, 0x81}, // -256 | |||||
{0xe8, 0x03}, // 1000 | |||||
{0xe8, 0x83}, // -1000 | |||||
{0xb0, 0x13}, // 5040 | |||||
{0xb0, 0x93}, // -5040 | |||||
{0xff, 0x7f}, | |||||
{0xff, 0xff}, | |||||
{0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff}, | |||||
{0x00, 0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff, 0xff}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, | |||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}, | |||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, | |||||
}); | |||||
static i128_t ToInt128(const valtype &vch) { | |||||
if (vch.empty()) { | |||||
return 0; | |||||
} | |||||
i128_t result = 0; | |||||
for (size_t i = 0; i < vch.size(); ++i) { | |||||
if (i == vch.size() - 1 && vch[i] & 0x80) { | |||||
result |= i128_t(vch[i] & 0x7f) << (8 * i); | |||||
return -result; | |||||
} | |||||
result |= i128_t(vch[i]) << (8 * i); | |||||
} | |||||
return result; | |||||
} | |||||
static bool IsInScriptBounds(const i128_t &num_int) { | |||||
return num_int >= MIN_SCRIPT_63_BIT_INT && num_int <= MAX_SCRIPT_63_BIT_INT; | |||||
} | |||||
#endif // BITCOIN_TEST_SCRIPTNUM63BIT_H |