Changeset View
Changeset View
Standalone View
Standalone View
src/arith_uint256.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_ARITH_UINT256_H | #ifndef BITCOIN_ARITH_UINT256_H | ||||
#define BITCOIN_ARITH_UINT256_H | #define BITCOIN_ARITH_UINT256_H | ||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <cstring> | #include <cstring> | ||||
#include <limits> | |||||
#include <stdexcept> | #include <stdexcept> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
class uint256; | class uint256; | ||||
class uint_error : public std::runtime_error { | class uint_error : public std::runtime_error { | ||||
public: | public: | ||||
explicit uint_error(const std::string &str) : std::runtime_error(str) {} | explicit uint_error(const std::string &str) : std::runtime_error(str) {} | ||||
}; | }; | ||||
/** Template base class for unsigned big integers. */ | /** Template base class for unsigned big integers. */ | ||||
template <unsigned int BITS> class base_uint { | template <unsigned int BITS> class base_uint { | ||||
protected: | protected: | ||||
static constexpr int WIDTH = BITS / 32; | static constexpr int WIDTH = BITS / 32; | ||||
uint32_t pn[WIDTH]; | uint32_t pn[WIDTH]; | ||||
public: | public: | ||||
base_uint() { | base_uint() { | ||||
static_assert( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"Template parameter BITS must be a positive multiple of 32."); | |||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
base_uint(const base_uint &b) { | base_uint(const base_uint &b) { | ||||
static_assert( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"Template parameter BITS must be a positive multiple of 32."); | |||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
} | } | ||||
base_uint &operator=(const base_uint &b) { | base_uint &operator=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
return *this; | return *this; | ||||
} | } | ||||
base_uint(uint64_t b) { | base_uint(uint64_t b) { | ||||
static_assert( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"Template parameter BITS must be a positive multiple of 32."); | |||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
explicit base_uint(const std::string &str); | explicit base_uint(const std::string &str); | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | public: | ||||
base_uint &operator*=(uint32_t b32); | base_uint &operator*=(uint32_t b32); | ||||
base_uint &operator*=(const base_uint &b); | base_uint &operator*=(const base_uint &b); | ||||
base_uint &operator/=(const base_uint &b); | base_uint &operator/=(const base_uint &b); | ||||
base_uint &operator++() { | base_uint &operator++() { | ||||
// prefix operator | // prefix operator | ||||
int i = 0; | int i = 0; | ||||
while (++pn[i] == 0 && i < WIDTH - 1) | while (i < WIDTH && ++pn[i] == 0) | ||||
i++; | i++; | ||||
return *this; | return *this; | ||||
} | } | ||||
const base_uint operator++(int) { | const base_uint operator++(int) { | ||||
// postfix operator | // postfix operator | ||||
const base_uint ret = *this; | const base_uint ret = *this; | ||||
++(*this); | ++(*this); | ||||
return ret; | return ret; | ||||
} | } | ||||
base_uint &operator--() { | base_uint &operator--() { | ||||
// prefix operator | // prefix operator | ||||
int i = 0; | int i = 0; | ||||
while (--pn[i] == (uint32_t)-1 && i < WIDTH - 1) | while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max()) | ||||
i++; | i++; | ||||
return *this; | return *this; | ||||
} | } | ||||
const base_uint operator--(int) { | const base_uint operator--(int) { | ||||
// postfix operator | // postfix operator | ||||
const base_uint ret = *this; | const base_uint ret = *this; | ||||
--(*this); | --(*this); | ||||
▲ Show 20 Lines • Show All 126 Lines • Show Last 20 Lines |