aboutsummaryrefslogtreecommitdiff
path: root/src/script.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/script.h')
-rw-r--r--src/script.h198
1 files changed, 155 insertions, 43 deletions
diff --git a/src/script.h b/src/script.h
index 657ac0b388..1742ce81f8 100644
--- a/src/script.h
+++ b/src/script.h
@@ -6,7 +6,6 @@
#ifndef H_BITCOIN_SCRIPT
#define H_BITCOIN_SCRIPT
-#include "bignum.h"
#include "key.h"
#include "util.h"
@@ -25,6 +24,155 @@ class CTransaction;
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
static const unsigned int MAX_OP_RETURN_RELAY = 40; // bytes
+class scriptnum_error : public std::runtime_error
+{
+public:
+ explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
+};
+
+class CScriptNum
+{
+// Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
+// The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
+// but results may overflow (and are valid as long as they are not used in a subsequent
+// numeric operation). CScriptNum enforces those semantics by storing results as
+// an int64 and allowing out-of-range values to be returned as a vector of bytes but
+// throwing an exception if arithmetic is done or the result is interpreted as an integer.
+public:
+
+ explicit CScriptNum(const int64_t& n)
+ {
+ m_value = n;
+ }
+
+ explicit CScriptNum(const std::vector<unsigned char>& vch)
+ {
+ if (vch.size() > nMaxNumSize)
+ throw scriptnum_error("CScriptNum(const std::vector<unsigned char>&) : overflow");
+ m_value = set_vch(vch);
+ }
+
+ inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
+ inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; }
+ inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
+ inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
+ inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
+ inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
+
+ inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
+ inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
+ inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
+ inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
+ inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
+ inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
+
+ inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
+ inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
+ inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
+ inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
+
+ inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
+ inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
+
+ inline CScriptNum operator-() const
+ {
+ assert(m_value != std::numeric_limits<int64_t>::min());
+ return CScriptNum(-m_value);
+ }
+
+ inline CScriptNum& operator=( const int64_t& rhs)
+ {
+ m_value = rhs;
+ return *this;
+ }
+
+ inline CScriptNum& operator+=( const int64_t& rhs)
+ {
+ assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
+ (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
+ m_value += rhs;
+ return *this;
+ }
+
+ inline CScriptNum& operator-=( const int64_t& rhs)
+ {
+ assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
+ (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
+ m_value -= rhs;
+ return *this;
+ }
+
+ int getint() const
+ {
+ if (m_value > std::numeric_limits<int>::max())
+ return std::numeric_limits<int>::max();
+ else if (m_value < std::numeric_limits<int>::min())
+ return std::numeric_limits<int>::min();
+ return m_value;
+ }
+
+ std::vector<unsigned char> getvch() const
+ {
+ return serialize(m_value);
+ }
+
+ static std::vector<unsigned char> serialize(const int64_t& value)
+ {
+ if(value == 0)
+ return std::vector<unsigned char>();
+
+ std::vector<unsigned char> result;
+ const bool neg = value < 0;
+ uint64_t absvalue = neg ? -value : value;
+
+ while(absvalue)
+ {
+ result.push_back(absvalue & 0xff);
+ absvalue >>= 8;
+ }
+
+
+// - If the most significant byte is >= 0x80 and the value is positive, push a
+// new zero-byte to make the significant byte < 0x80 again.
+
+// - If the most significant byte is >= 0x80 and the value is negative, push a
+// new 0x80 byte that will be popped off when converting to an integral.
+
+// - If the most significant byte is < 0x80 and the value is negative, add
+// 0x80 to it, since it will be subtracted and interpreted as a negative when
+// converting to an integral.
+
+ if (result.back() & 0x80)
+ result.push_back(neg ? 0x80 : 0);
+ else if (neg)
+ result.back() |= 0x80;
+
+ return result;
+ }
+
+ static const size_t nMaxNumSize = 4;
+
+private:
+ static int64_t set_vch(const std::vector<unsigned char>& vch)
+ {
+ if (vch.empty())
+ return 0;
+
+ int64_t result = 0;
+ for (size_t i = 0; i != vch.size(); ++i)
+ result |= static_cast<int64_t>(vch[i]) << 8*i;
+
+ // If the input vector's most significant byte is 0x80, remove it from
+ // the result's msb and return a negative.
+ if (vch.back() & 0x80)
+ return -(result & ~(0x80 << (8 * (vch.size() - 1))));
+
+ return result;
+ }
+
+ int64_t m_value;
+};
+
/** Signature hash types/flags */
enum
{
@@ -225,7 +373,7 @@ const char* GetOpName(opcodetype opcode);
inline std::string ValueString(const std::vector<unsigned char>& vch)
{
if (vch.size() <= 4)
- return strprintf("%d", CBigNum(vch).getint());
+ return strprintf("%d", CScriptNum(vch).getint());
else
return HexStr(vch);
}
@@ -261,26 +409,10 @@ protected:
}
else
{
- CBigNum bn(n);
- *this << bn.getvch();
+ *this << CScriptNum::serialize(n);
}
return *this;
}
-
- CScript& push_uint64(uint64_t n)
- {
- if (n >= 1 && n <= 16)
- {
- push_back(n + (OP_1 - 1));
- }
- else
- {
- CBigNum bn(n);
- *this << bn.getvch();
- }
- return *this;
- }
-
public:
CScript() { }
CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
@@ -303,35 +435,15 @@ public:
}
- //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
- explicit CScript(signed char b) { operator<<(b); }
- explicit CScript(short b) { operator<<(b); }
- explicit CScript(int b) { operator<<(b); }
- explicit CScript(long b) { operator<<(b); }
- explicit CScript(long long b) { operator<<(b); }
- explicit CScript(unsigned char b) { operator<<(b); }
- explicit CScript(unsigned int b) { operator<<(b); }
- explicit CScript(unsigned short b) { operator<<(b); }
- explicit CScript(unsigned long b) { operator<<(b); }
- explicit CScript(unsigned long long b) { operator<<(b); }
+ CScript(int64_t b) { operator<<(b); }
explicit CScript(opcodetype b) { operator<<(b); }
explicit CScript(const uint256& b) { operator<<(b); }
- explicit CScript(const CBigNum& b) { operator<<(b); }
+ explicit CScript(const CScriptNum& b) { operator<<(b); }
explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
- //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
- CScript& operator<<(signed char b) { return push_int64(b); }
- CScript& operator<<(short b) { return push_int64(b); }
- CScript& operator<<(int b) { return push_int64(b); }
- CScript& operator<<(long b) { return push_int64(b); }
- CScript& operator<<(long long b) { return push_int64(b); }
- CScript& operator<<(unsigned char b) { return push_uint64(b); }
- CScript& operator<<(unsigned int b) { return push_uint64(b); }
- CScript& operator<<(unsigned short b) { return push_uint64(b); }
- CScript& operator<<(unsigned long b) { return push_uint64(b); }
- CScript& operator<<(unsigned long long b) { return push_uint64(b); }
+ CScript& operator<<(int64_t b) { return push_int64(b); }
CScript& operator<<(opcodetype opcode)
{
@@ -363,7 +475,7 @@ public:
return *this;
}
- CScript& operator<<(const CBigNum& b)
+ CScript& operator<<(const CScriptNum& b)
{
*this << b.getvch();
return *this;