-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathripple_CBigNum.h
151 lines (126 loc) · 4.65 KB
/
ripple_CBigNum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//------------------------------------------------------------------------------
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
#ifndef RIPPLE_CBIGNUM_H
#define RIPPLE_CBIGNUM_H
//------------------------------------------------------------------------------
class bignum_error : public std::runtime_error
{
public:
explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
};
//------------------------------------------------------------------------------
class CAutoBN_CTX
{
private:
CAutoBN_CTX(const CAutoBN_CTX&); // no implementation
CAutoBN_CTX& operator=(const CAutoBN_CTX&); // no implementation
protected:
BN_CTX* pctx;
CAutoBN_CTX& operator=(BN_CTX* pnew) { pctx = pnew; return *this; }
public:
CAutoBN_CTX()
{
pctx = BN_CTX_new();
if (pctx == NULL)
throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL");
}
~CAutoBN_CTX()
{
if (pctx != NULL)
BN_CTX_free(pctx);
}
operator BN_CTX*() { return pctx; }
BN_CTX& operator*() { return *pctx; }
BN_CTX** operator&() { return &pctx; }
bool operator!() { return (pctx == NULL); }
};
//------------------------------------------------------------------------------
// VFALCO TODO figure out a way to remove the dependency on openssl in the
// header. Maybe rewrite this to use cryptopp.
class CBigNum : public BIGNUM
{
public:
CBigNum();
CBigNum(const CBigNum& b);
CBigNum& operator=(const CBigNum& b);
CBigNum(char n);
CBigNum(short n);
CBigNum(int n);
CBigNum(long n);
CBigNum(int64 n);
CBigNum(unsigned char n);
CBigNum(unsigned short n);
CBigNum(unsigned int n);
CBigNum(uint64 n);
explicit CBigNum(uint256 n);
explicit CBigNum(Blob const& vch);
~CBigNum();
void setuint(unsigned int n);
unsigned int getuint() const;
int getint() const;
void setint64(int64 n);
uint64 getuint64() const;
void setuint64(uint64 n);
void setuint256(uint256 const& n);
uint256 getuint256();
void setvch(Blob const& vch);
Blob getvch() const;
CBigNum& SetCompact(unsigned int nCompact);
unsigned int GetCompact() const;
void SetHex(const std::string& str);
std::string ToString(int nBase=10) const;
std::string GetHex() const;
bool operator!() const;
CBigNum& operator+=(const CBigNum& b);
CBigNum& operator-=(const CBigNum& b);
CBigNum& operator*=(const CBigNum& b);
CBigNum& operator/=(const CBigNum& b);
CBigNum& operator%=(const CBigNum& b);
CBigNum& operator<<=(unsigned int shift);
CBigNum& operator>>=(unsigned int shift);
CBigNum& operator++();
CBigNum& operator--();
const CBigNum operator++(int);
const CBigNum operator--(int);
friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b);
friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b);
private:
// private because the size of an unsigned long varies by platform
void setulong(unsigned long n);
unsigned long getulong() const;
};
const CBigNum operator+(const CBigNum& a, const CBigNum& b);
const CBigNum operator-(const CBigNum& a, const CBigNum& b);
const CBigNum operator-(const CBigNum& a);
const CBigNum operator*(const CBigNum& a, const CBigNum& b);
const CBigNum operator/(const CBigNum& a, const CBigNum& b);
const CBigNum operator%(const CBigNum& a, const CBigNum& b);
const CBigNum operator<<(const CBigNum& a, unsigned int shift);
const CBigNum operator>>(const CBigNum& a, unsigned int shift);
bool operator==(const CBigNum& a, const CBigNum& b);
bool operator!=(const CBigNum& a, const CBigNum& b);
bool operator<=(const CBigNum& a, const CBigNum& b);
bool operator>=(const CBigNum& a, const CBigNum& b);
bool operator<(const CBigNum& a, const CBigNum& b);
bool operator>(const CBigNum& a, const CBigNum& b);
//------------------------------------------------------------------------------
// VFALCO NOTE this seems as good a place as any for this.
// Here's the old implementation using macros, in case something broke
//#if (ULONG_MAX > UINT_MAX)
//#define BN_add_word64(bn, word) BN_add_word(bn, word)
//#define BN_sub_word64(bn, word) BN_sub_word(bn, word)
//#define BN_mul_word64(bn, word) BN_mul_word(bn, word)
//#define BN_div_word64(bn, word) BN_div_word(bn, word)
//#endif
// VFALCO I believe only STAmount uses these
extern int BN_add_word64 (BIGNUM *a, uint64 w);
extern int BN_sub_word64 (BIGNUM *a, uint64 w);
extern int BN_mul_word64 (BIGNUM *a, uint64 w);
extern uint64 BN_div_word64 (BIGNUM *a, uint64 w);
#endif
// vim:ts=4