Files

80 lines
3.0 KiB
C++
Raw Permalink Normal View History

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_FASTBIGNUM_H
#define FLOWEE_SCRIPT_FASTBIGNUM_H
#include "script.h"
#include <optional>
#include <variant>
class FastBigNum
{
public:
FastBigNum(const std::vector<unsigned char> &vch, bool fRequireMinimal, size_t maxIntegerSize);
explicit FastBigNum(const CScriptNum &value);
explicit FastBigNum(const ScriptBigNum &value);
static FastBigNum fromIntUnchecked(int64_t value, bool useBigInt = false);
bool usesNative() const;
int32_t getint32() const;
std::optional<int64_t> getint64() const;
std::vector<unsigned char> getvch() const;
size_t absValNumBits() const;
bool safeAddInPlace(const FastBigNum &rhs);
bool safeIncr();
bool safeSubInPlace(const FastBigNum &rhs);
bool safeDecr();
bool safeMulInPlace(const FastBigNum &rhs);
FastBigNum& operator/=(const FastBigNum &rhs);
FastBigNum& operator%=(const FastBigNum &rhs);
FastBigNum& negate();
bool checkedLeftShift(unsigned bitcount);
bool checkedRightShift(unsigned bitcount);
bool isZero() const;
int compare(const FastBigNum &rhs) const;
bool operator==(const FastBigNum &rhs) const { return compare(rhs) == 0; }
bool operator!=(const FastBigNum &rhs) const { return compare(rhs) != 0; }
bool operator<(const FastBigNum &rhs) const { return compare(rhs) < 0; }
bool operator<=(const FastBigNum &rhs) const { return compare(rhs) <= 0; }
bool operator>(const FastBigNum &rhs) const { return compare(rhs) > 0; }
bool operator>=(const FastBigNum &rhs) const { return compare(rhs) >= 0; }
bool operator==(int64_t rhs) const { return compare(FastBigNum::fromIntUnchecked(rhs)) == 0; }
bool operator!=(int64_t rhs) const { return !(*this == rhs); }
bool operator<(int64_t rhs) const { return compare(FastBigNum::fromIntUnchecked(rhs)) < 0; }
bool operator<=(int64_t rhs) const { return compare(FastBigNum::fromIntUnchecked(rhs)) <= 0; }
bool operator>(int64_t rhs) const { return compare(FastBigNum::fromIntUnchecked(rhs)) > 0; }
bool operator>=(int64_t rhs) const { return compare(FastBigNum::fromIntUnchecked(rhs)) >= 0; }
private:
std::variant<CScriptNum, ScriptBigNum> m_value;
ScriptBigNum asBigNum() const;
};
#endif