2026-05-07 21:10:10 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
|
|
|
|
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* 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
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef FLOWEE_SCRIPT_BIGNUM_H
|
|
|
|
|
#define FLOWEE_SCRIPT_BIGNUM_H
|
|
|
|
|
|
|
|
|
|
#include "script.h"
|
2026-05-14 14:04:53 +02:00
|
|
|
#include "ScriptDefines.h"
|
2026-05-07 21:10:10 +02:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
|
|
|
|
|
|
|
|
|
class ScriptBigNum
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using BigInt = boost::multiprecision::cpp_int;
|
|
|
|
|
|
|
|
|
|
static const size_t MAXIMUM_ELEMENT_SIZE_BIG_INT = may2025::MAX_SCRIPT_ELEMENT_SIZE;
|
|
|
|
|
static const size_t MAX_BIG_INT_BITS = MAXIMUM_ELEMENT_SIZE_BIG_INT * 8 - 1;
|
|
|
|
|
|
|
|
|
|
explicit ScriptBigNum(int64_t value = 0);
|
|
|
|
|
explicit ScriptBigNum(const BigInt &value);
|
|
|
|
|
explicit ScriptBigNum(BigInt &&value);
|
|
|
|
|
explicit ScriptBigNum(const std::vector<unsigned char> &vch, bool fRequireMinimal,
|
|
|
|
|
size_t maxIntegerSize = MAXIMUM_ELEMENT_SIZE_BIG_INT);
|
|
|
|
|
|
|
|
|
|
static size_t absValNumBits(const BigInt &value);
|
|
|
|
|
static bool validRange(const BigInt &value);
|
|
|
|
|
|
|
|
|
|
bool safeIncr();
|
|
|
|
|
bool safeDecr();
|
|
|
|
|
bool safeAddInPlace(const ScriptBigNum &rhs);
|
|
|
|
|
bool safeAddInPlace(int64_t rhs);
|
|
|
|
|
bool safeSubInPlace(const ScriptBigNum &rhs);
|
|
|
|
|
bool safeSubInPlace(int64_t rhs);
|
|
|
|
|
bool safeMulInPlace(const ScriptBigNum &rhs);
|
|
|
|
|
bool safeMulInPlace(int64_t rhs);
|
|
|
|
|
|
|
|
|
|
ScriptBigNum& operator/=(const ScriptBigNum &rhs);
|
|
|
|
|
ScriptBigNum& operator%=(const ScriptBigNum &rhs);
|
|
|
|
|
ScriptBigNum& negate();
|
|
|
|
|
|
|
|
|
|
bool checkedLeftShift(unsigned bitcount);
|
|
|
|
|
bool checkedRightShift(unsigned bitcount);
|
|
|
|
|
bool isZero() const;
|
|
|
|
|
|
|
|
|
|
int32_t getint32() const;
|
|
|
|
|
std::optional<int64_t> getint64() const;
|
|
|
|
|
std::vector<unsigned char> getvch() const;
|
|
|
|
|
size_t absValNumBits() const;
|
|
|
|
|
|
|
|
|
|
int compare(const ScriptBigNum &rhs) const;
|
|
|
|
|
|
|
|
|
|
bool operator==(const ScriptBigNum &rhs) const { return compare(rhs) == 0; }
|
|
|
|
|
bool operator!=(const ScriptBigNum &rhs) const { return compare(rhs) != 0; }
|
|
|
|
|
bool operator<(const ScriptBigNum &rhs) const { return compare(rhs) < 0; }
|
|
|
|
|
bool operator<=(const ScriptBigNum &rhs) const { return compare(rhs) <= 0; }
|
|
|
|
|
bool operator>(const ScriptBigNum &rhs) const { return compare(rhs) > 0; }
|
|
|
|
|
bool operator>=(const ScriptBigNum &rhs) const { return compare(rhs) >= 0; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
BigInt m_value;
|
|
|
|
|
|
|
|
|
|
static BigInt set_vch(const std::vector<unsigned char> &vch);
|
|
|
|
|
static std::vector<unsigned char> serialize(const BigInt &value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|