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