2016-08-01 05:25:37 +00:00
|
|
|
/*
|
2017-02-02 10:06:28 +00:00
|
|
|
This file is part of solidity.
|
2016-08-01 05:25:37 +00:00
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-08-01 05:25:37 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2016-08-01 05:25:37 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-02-02 10:06:28 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-01 05:25:37 +00:00
|
|
|
*/
|
|
|
|
/** @file Common.h
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*
|
|
|
|
* Very common stuff (i.e. that every other header needs except vector_ref.h).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-09-07 11:54:57 +00:00
|
|
|
// way too many unsigned to size_t warnings in 32 bit build
|
2016-08-01 05:25:37 +00:00
|
|
|
#ifdef _M_IX86
|
|
|
|
#pragma warning(disable:4244)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if _MSC_VER && _MSC_VER < 1900
|
|
|
|
#define _ALLOW_KEYWORD_MACROS
|
|
|
|
#define noexcept throw()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __INTEL_COMPILER
|
|
|
|
#pragma warning(disable:3682) //call through incomplete class
|
|
|
|
#endif
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/vector_ref.h>
|
2016-08-01 05:25:37 +00:00
|
|
|
|
2019-01-18 18:13:59 +00:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
#if (BOOST_VERSION < 106500)
|
|
|
|
#error "Unsupported Boost version. At least 1.65 required."
|
|
|
|
#endif
|
|
|
|
|
2016-08-01 05:25:37 +00:00
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
|
|
|
|
2017-08-25 10:07:02 +00:00
|
|
|
#include <map>
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2017-08-25 10:07:02 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2016-08-01 05:25:37 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity
|
2016-08-01 05:25:37 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// Binary data types.
|
2018-11-07 11:04:46 +00:00
|
|
|
using bytes = std::vector<uint8_t>;
|
2019-12-11 16:31:36 +00:00
|
|
|
using bytesRef = util::vector_ref<uint8_t>;
|
|
|
|
using bytesConstRef = util::vector_ref<uint8_t const>;
|
2016-08-01 05:25:37 +00:00
|
|
|
|
|
|
|
// Numeric types.
|
|
|
|
using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;
|
2017-08-25 10:07:02 +00:00
|
|
|
using u256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
|
|
|
|
using s256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
|
|
|
|
using u160 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
|
2016-08-01 05:25:37 +00:00
|
|
|
|
|
|
|
// Map types.
|
|
|
|
using StringMap = std::map<std::string, std::string>;
|
|
|
|
|
|
|
|
// String types.
|
|
|
|
using strings = std::vector<std::string>;
|
|
|
|
|
|
|
|
/// Interprets @a _u as a two's complement signed number and returns the resulting s256.
|
|
|
|
inline s256 u2s(u256 _u)
|
|
|
|
{
|
2019-02-14 10:53:00 +00:00
|
|
|
static bigint const c_end = bigint(1) << 256;
|
2016-08-01 05:25:37 +00:00
|
|
|
if (boost::multiprecision::bit_test(_u, 255))
|
|
|
|
return s256(-(c_end - _u));
|
|
|
|
else
|
|
|
|
return s256(_u);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns the two's complement signed representation of the signed number _u.
|
|
|
|
inline u256 s2u(s256 _u)
|
|
|
|
{
|
2019-02-14 10:53:00 +00:00
|
|
|
static bigint const c_end = bigint(1) << 256;
|
2019-02-13 15:56:46 +00:00
|
|
|
if (_u >= 0)
|
2016-08-01 05:25:37 +00:00
|
|
|
return u256(_u);
|
2019-02-13 15:56:46 +00:00
|
|
|
else
|
2016-08-01 05:25:37 +00:00
|
|
|
return u256(c_end + _u);
|
|
|
|
}
|
|
|
|
|
2019-05-20 14:23:59 +00:00
|
|
|
inline u256 exp256(u256 _base, u256 _exponent)
|
|
|
|
{
|
|
|
|
using boost::multiprecision::limb_type;
|
|
|
|
u256 result = 1;
|
|
|
|
while (_exponent)
|
|
|
|
{
|
|
|
|
if (boost::multiprecision::bit_test(_exponent, 0))
|
|
|
|
result *= _base;
|
|
|
|
_base *= _base;
|
|
|
|
_exponent >>= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-18 00:01:28 +00:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, bytes const& _bytes)
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
2016-11-21 19:53:53 +00:00
|
|
|
ss << std::hex;
|
2016-11-18 00:01:28 +00:00
|
|
|
std::copy(_bytes.begin(), _bytes.end(), std::ostream_iterator<int>(ss, ","));
|
|
|
|
std::string result = ss.str();
|
|
|
|
result.pop_back();
|
|
|
|
os << "[" + result + "]";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2016-08-01 05:25:37 +00:00
|
|
|
/// RAII utility class whose destructor calls a given function.
|
|
|
|
class ScopeGuard
|
|
|
|
{
|
|
|
|
public:
|
2020-04-01 03:04:29 +00:00
|
|
|
explicit ScopeGuard(std::function<void(void)> _f): m_f(std::move(_f)) {}
|
2016-08-01 05:25:37 +00:00
|
|
|
~ScopeGuard() { m_f(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void(void)> m_f;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|