Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bigint.cpp
Go to the documentation of this file.
2#include <fc/utility.hpp>
3#include <openssl/bn.h>
4#include <fc/variant.hpp>
6
8#include "../byteswap.hpp"
9
10namespace fc {
11 bigint::bigint( const char* bige, uint32_t l ) {
12 n = BN_bin2bn( (const unsigned char*)bige, l, NULL );
13 FC_ASSERT( n != nullptr );
14 }
15 bigint::bigint( const std::vector<char>& bige ) {
16 n = BN_bin2bn( (const unsigned char*)bige.data(), bige.size(), NULL );
17 FC_ASSERT( n != nullptr );
18 }
20 {
21 n = BN_dup(in);
22 }
24 :n(BN_new())
25 { }
26
28 {
29 return BN_dup( n );
30 }
31
33 {
34 uint64_t big_endian_value = bswap_64(value);
35 n = BN_bin2bn((const unsigned char*)&big_endian_value, sizeof(big_endian_value), NULL);
36 }
37
38 bigint::bigint( const bigint& c ) {
39 n = BN_dup( c.n );
40 }
41
43 n = b.n;
44 b.n = 0;
45 }
46
48 if(n!=0) BN_free(n);
49 }
50
51 bool bigint::is_negative()const { return BN_is_negative(n); }
52
54 {
55 FC_ASSERT(BN_num_bits(n) <= 63);
56 size_t size = BN_num_bytes(n);
57 uint64_t abs_value = 0;
58 BN_bn2bin(n, (unsigned char*)&abs_value + (sizeof(uint64_t) - size));
59 return BN_is_negative(n) ? -(int64_t)bswap_64(abs_value) : bswap_64(abs_value);
60 }
61
62 int64_t bigint::log2()const { return BN_num_bits(n); }
63 bool bigint::operator < ( const bigint& c )const {
64 return BN_cmp( n, c.n ) < 0;
65 }
66 bool bigint::operator > ( const bigint& c )const {
67 return BN_cmp( n, c.n ) > 0;
68 }
69 bool bigint::operator >= ( const bigint& c )const {
70 return BN_cmp( n, c.n ) >= 0;
71 }
72 bool bigint::operator == ( const bigint& c )const {
73 return BN_cmp( n, c.n ) == 0;
74 }
75 bool bigint::operator != ( const bigint& c )const {
76 return BN_cmp( n, c.n ) != 0;
77 }
78 bigint::operator bool()const
79 {
80 return !BN_is_zero( n );
81 }
83 {
84 bigint tmp = *this;
85 *this = *this + bigint(1);
86 return tmp;
87 }
89 {
90 return *this = *this + bigint(1);
91 }
93 {
94 bigint tmp = *this;
95 *this = *this - bigint(1);
96 return tmp;
97 }
99 {
100 return *this = *this - bigint(1);
101 }
102
104 bigint tmp(*this);
105 BN_add( tmp.n, n, a.n );
106 return tmp;
107 }
109 bigint tmp(*this);
110 BN_add( tmp.n, n, a.n );
111 std::swap(*this,tmp);
112 return *this;
113 }
115 bigint tmp(*this);
116 BN_sub( tmp.n, n, a.n );
117 std::swap(*this,tmp);
118 return *this;
119 }
120
121
123 BN_CTX* ctx = BN_CTX_new();
124 bigint tmp(*this);
125 BN_mul( tmp.n, n, a.n, ctx );
126 BN_CTX_free(ctx);
127 return tmp;
128 }
130 BN_CTX* ctx = BN_CTX_new();
131 bigint tmp;//(*this);
132 BN_div( tmp.n, NULL, n, a.n, ctx );
133 BN_CTX_free(ctx);
134 return tmp;
135 }
137 BN_CTX* ctx = BN_CTX_new();
138 bigint tmp;//(*this);
139 BN_mod( tmp.n, n, a.n, ctx );
140 BN_CTX_free(ctx);
141 return tmp;
142 }
143
145 BN_CTX* ctx = BN_CTX_new();
146 bigint tmp;//*this);
147 BN_div( tmp.n, NULL, n, a.n, ctx );
148 fc_swap( tmp.n, n );
149 BN_CTX_free(ctx);
150 return tmp;
151 }
153 auto tmp = *this * a;
154 *this = std::move(tmp);
155 return *this;
156 }
158 {
159 bigint tmp;
160 BN_rshift( tmp.n, n, i );
161 std::swap(*this,tmp);
162 return *this;
163 }
164
166 {
167 bigint tmp;
168 FC_ASSERT( tmp.n != nullptr );
169 FC_ASSERT( n != nullptr );
170 BN_lshift( tmp.n, n, i );
171 std::swap(*this,tmp);
172 return *this;
173 }
174
176 bigint tmp;
177 BN_sub( tmp.n, n, a.n );
178 return tmp;
179 }
180 bigint bigint::exp( const bigint& a )const
181 {
182 BN_CTX* ctx = BN_CTX_new();
183 bigint tmp;
184 BN_exp( tmp.n, n, a.n, ctx );
185 BN_CTX_free(ctx);
186 return tmp;
187 }
188
189
191 fc_swap( a.n, n );
192 return *this;
193 }
195 if( &a == this )
196 return *this;
197 BN_copy( n, a.n );
198 return *this;
199 }
200 bigint::operator fc::string()const {
201 return BN_bn2dec(n);
202 }
203
204 bigint::operator std::vector<char>()const {
205 std::vector<char> to(BN_num_bytes(n));
206 BN_bn2bin(n,(unsigned char*)to.data());
207 return to;
208 }
209
211 void to_variant( const bigint& bi, variant& v )
212 {
213 std::vector<char> ve = bi;
214 v = fc::variant(base64_encode((unsigned char*)ve.data(),ve.size()));
215 }
216
218 void from_variant( const variant& v, bigint& bi )
219 {
220 if( v.is_numeric() ) bi = bigint( static_cast<unsigned long>(v.as_uint64()) );
221 else
222 {
223 std::string b64 = v.as_string();
224 std::string bin = base64_decode(b64);
225 bi = bigint(bin.c_str(), bin.size() );
226 }
227 }
228
229} // namespace fc
bigint & operator--()
Definition bigint.cpp:98
bool operator>=(const bigint &c) const
Definition bigint.cpp:69
bigint & operator=(const bigint &a)
Definition bigint.cpp:194
bigint operator*(const bigint &a) const
Definition bigint.cpp:122
bigint operator+(const bigint &a) const
Definition bigint.cpp:103
bigint & operator<<=(uint32_t i)
Definition bigint.cpp:165
BIGNUM * dup() const
Definition bigint.cpp:27
bigint operator/(const bigint &a) const
Definition bigint.cpp:129
bool operator==(const bigint &c) const
Definition bigint.cpp:72
bool operator>(const bigint &c) const
Definition bigint.cpp:66
bigint operator%(const bigint &a) const
Definition bigint.cpp:136
bigint & operator+=(const bigint &a)
Definition bigint.cpp:108
bool is_negative() const
Definition bigint.cpp:51
bigint exp(const bigint &c) const
Definition bigint.cpp:180
bigint & operator-=(const bigint &a)
Definition bigint.cpp:114
bool operator<(const bigint &c) const
Definition bigint.cpp:63
int64_t to_int64() const
Definition bigint.cpp:53
bigint operator*=(const bigint &a)
Definition bigint.cpp:152
bool operator!=(const bigint &c) const
Definition bigint.cpp:75
bigint & operator>>=(uint32_t i)
Definition bigint.cpp:157
bigint operator-(const bigint &a) const
Definition bigint.cpp:175
bigint & operator++()
Definition bigint.cpp:88
bigint operator/=(const bigint &a)
Definition bigint.cpp:144
int64_t log2() const
Definition bigint.cpp:62
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
uint64_t as_uint64() const
Definition variant.cpp:398
bool is_numeric() const
Definition variant.cpp:348
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
bignum_st BIGNUM
Definition bigint.hpp:7
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:152
void swap(picojson::value &x, picojson::value &y)
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
void fc_swap(T &a, T &b)
Definition utility.hpp:211
int l