| 210 | }; |
| 211 | |
| 212 | class CScriptNum |
| 213 | { |
| 214 | /** |
| 215 | * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers. |
| 216 | * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1], |
| 217 | * but results may overflow (and are valid as long as they are not used in a subsequent |
| 218 | * numeric operation). CScriptNum enforces those semantics by storing results as |
| 219 | * an int64 and allowing out-of-range values to be returned as a vector of bytes but |
| 220 | * throwing an exception if arithmetic is done or the result is interpreted as an integer. |
| 221 | */ |
| 222 | public: |
| 223 | |
| 224 | explicit CScriptNum(const int64_t& n) |
| 225 | { |
| 226 | m_value = n; |
| 227 | } |
| 228 | |
| 229 | static const size_t nDefaultMaxNumSize = 4; |
| 230 | |
| 231 | explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal, |
| 232 | const size_t nMaxNumSize = nDefaultMaxNumSize) |
| 233 | { |
| 234 | if (vch.size() > nMaxNumSize) { |
| 235 | throw scriptnum_error("script number overflow"); |
| 236 | } |
| 237 | if (fRequireMinimal && vch.size() > 0) { |
| 238 | // Check that the number is encoded with the minimum possible |
| 239 | // number of bytes. |
| 240 | // |
| 241 | // If the most-significant-byte - excluding the sign bit - is zero |
| 242 | // then we're not minimal. Note how this test also rejects the |
| 243 | // negative-zero encoding, 0x80. |
| 244 | if ((vch.back() & 0x7f) == 0) { |
| 245 | // One exception: if there's more than one byte and the most |
| 246 | // significant bit of the second-most-significant-byte is set |
| 247 | // it would conflict with the sign bit. An example of this case |
| 248 | // is +-255, which encode to 0xff00 and 0xff80 respectively. |
| 249 | // (big-endian). |
| 250 | if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) { |
| 251 | throw scriptnum_error("non-minimally encoded script number"); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | m_value = set_vch(vch); |
| 256 | } |
| 257 | |
| 258 | inline bool operator==(const int64_t& rhs) const { return m_value == rhs; } |
| 259 | inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; } |
| 260 | inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; } |
| 261 | inline bool operator< (const int64_t& rhs) const { return m_value < rhs; } |
| 262 | inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; } |
| 263 | inline bool operator> (const int64_t& rhs) const { return m_value > rhs; } |
| 264 | |
| 265 | inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); } |
| 266 | inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); } |
| 267 | inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); } |
| 268 | inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); } |
| 269 | inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); } |