2 #include <openssl/bn.h>
8 #include <boost/endian/buffers.hpp>
12 n = BN_bin2bn( (
const unsigned char*)bige, l, NULL );
16 n = BN_bin2bn( (
const unsigned char*)bige.data(), bige.size(), NULL );
34 boost::endian::big_uint64_buf_t big_endian_value;
35 big_endian_value = value;
36 n = BN_bin2bn((
const unsigned char*)&big_endian_value,
sizeof(big_endian_value), NULL);
57 size_t size = BN_num_bytes(n);
58 boost::endian::big_int64_buf_t abs_value;
60 BN_bn2bin(n, (
unsigned char*)&abs_value + (
sizeof(abs_value) - size));
61 return BN_is_negative(n) ? -abs_value.value() : abs_value.value();
66 return BN_cmp( n, c.n ) < 0;
69 return BN_cmp( n, c.n ) > 0;
72 return BN_cmp( n, c.n ) >= 0;
75 return BN_cmp( n, c.n ) == 0;
78 return BN_cmp( n, c.n ) != 0;
80 bigint::operator bool()
const
82 return !BN_is_zero( n );
92 return *
this = *
this +
bigint(1);
102 return *
this = *
this -
bigint(1);
107 BN_add( tmp.n, n, a.n );
112 BN_add( tmp.n, n, a.n );
113 std::swap(*
this,tmp);
118 BN_sub( tmp.n, n, a.n );
119 std::swap(*
this,tmp);
125 BN_CTX* ctx = BN_CTX_new();
127 BN_mul( tmp.n, n, a.n, ctx );
132 BN_CTX* ctx = BN_CTX_new();
134 BN_div( tmp.n, NULL, n, a.n, ctx );
139 BN_CTX* ctx = BN_CTX_new();
141 BN_mod( tmp.n, n, a.n, ctx );
147 BN_CTX* ctx = BN_CTX_new();
149 BN_div( tmp.n, NULL, n, a.n, ctx );
150 std::swap( tmp.n, n );
155 auto tmp = *
this * a;
156 *
this = std::move(tmp);
162 BN_rshift( tmp.n, n, i );
163 std::swap(*
this,tmp);
172 BN_lshift( tmp.n, n, i );
173 std::swap(*
this,tmp);
179 BN_sub( tmp.n, n, a.n );
184 BN_CTX* ctx = BN_CTX_new();
186 BN_exp( tmp.n, n, a.n, ctx );
204 bigint::operator std::string()
const {
208 bigint::operator std::vector<char>()
const {
209 std::vector<char> to(BN_num_bytes(n));
210 BN_bn2bin(n,(
unsigned char*)to.data());
217 std::vector<char> ve = bi;
229 bi =
bigint(bin.c_str(), bin.size() );