MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / CScriptNum

Class CScriptNum

src/script/script.h:227–394  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 14

ParseScriptNumberFunction · 0.70
GetScriptNumberFunction · 0.70
EvalScriptFunction · 0.70
operator+Method · 0.70
operator-Method · 0.70
operator&Method · 0.70
CheckAfterMethod · 0.70
CheckOlderMethod · 0.70
ScriptToAsmStrFunction · 0.50
CheckCreateIntFunction · 0.50
PopulateMempoolMethod · 0.50

Calls

no outgoing calls

Tested by 3

CheckCreateIntFunction · 0.40
PopulateMempoolMethod · 0.40