adding bigint
This commit is contained in:
parent
34e33d058b
commit
9245b69202
2 changed files with 126 additions and 0 deletions
42
include/fc/bigint.hpp
Normal file
42
include/fc/bigint.hpp
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
#ifndef _FC_BIGINT_HPP
|
||||||
|
#define _FC_BIGINT_HPP
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <fc/string.hpp>
|
||||||
|
|
||||||
|
struct bignum_st;
|
||||||
|
typedef bignum_st BIGNUM;
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
class bigint {
|
||||||
|
public:
|
||||||
|
bigint( const char* bige, uint32_t l );
|
||||||
|
bigint( unsigned long i = 0 );
|
||||||
|
bigint( const bigint& c );
|
||||||
|
bigint( bigint&& c );
|
||||||
|
~bigint();
|
||||||
|
|
||||||
|
bool is_negative()const;
|
||||||
|
int64_t to_int64()const;
|
||||||
|
|
||||||
|
int64_t log2()const;
|
||||||
|
bool operator < ( const bigint& c )const;
|
||||||
|
bool operator > ( const bigint& c )const;
|
||||||
|
bool operator >= ( const bigint& c )const;
|
||||||
|
bool operator == ( const bigint& c )const;
|
||||||
|
|
||||||
|
bigint operator + ( const bigint& a )const;
|
||||||
|
bigint operator * ( const bigint& a )const;
|
||||||
|
bigint operator / ( const bigint& a )const;
|
||||||
|
bigint operator - ( const bigint& a )const;
|
||||||
|
|
||||||
|
bigint& operator = ( const bigint& a );
|
||||||
|
bigint& operator = ( bigint&& a );
|
||||||
|
|
||||||
|
operator fc::string()const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BIGNUM* n;
|
||||||
|
};
|
||||||
|
} // namespace fc
|
||||||
|
|
||||||
|
#endif
|
||||||
84
src/bigint.cpp
Normal file
84
src/bigint.cpp
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
#include <fc/bigint.hpp>
|
||||||
|
#include <fc/utility.hpp>
|
||||||
|
#include <openssl/bn.h>
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
bigint::bigint( const char* bige, uint32_t l ) {
|
||||||
|
n = BN_bin2bn( (const unsigned char*)bige, l, NULL );
|
||||||
|
}
|
||||||
|
|
||||||
|
bigint::bigint( unsigned long i )
|
||||||
|
:n(BN_new()) {
|
||||||
|
BN_set_word( n, i );
|
||||||
|
}
|
||||||
|
|
||||||
|
bigint::bigint( const bigint& c ) {
|
||||||
|
n = BN_dup( c.n );
|
||||||
|
}
|
||||||
|
|
||||||
|
bigint::bigint( bigint&& b ) {
|
||||||
|
n = b.n;
|
||||||
|
b.n = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bigint::~bigint() {
|
||||||
|
if(n!=0) BN_free(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bigint::is_negative()const { return BN_is_negative(n); }
|
||||||
|
int64_t bigint::to_int64()const { return BN_get_word(n); }
|
||||||
|
|
||||||
|
int64_t bigint::log2()const { return BN_num_bits(n); }
|
||||||
|
bool bigint::operator < ( const bigint& c )const {
|
||||||
|
return BN_cmp( n, c.n ) < 0;
|
||||||
|
}
|
||||||
|
bool bigint::operator > ( const bigint& c )const {
|
||||||
|
return BN_cmp( n, c.n ) > 0;
|
||||||
|
}
|
||||||
|
bool bigint::operator >= ( const bigint& c )const {
|
||||||
|
return BN_cmp( n, c.n ) >= 0;
|
||||||
|
}
|
||||||
|
bool bigint::operator == ( const bigint& c )const {
|
||||||
|
return BN_cmp( n, c.n ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bigint bigint::operator + ( const bigint& a )const {
|
||||||
|
bigint tmp(*this);
|
||||||
|
BN_add( tmp.n, n, a.n );
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
bigint bigint::operator * ( const bigint& a )const {
|
||||||
|
BN_CTX* ctx = BN_CTX_new();
|
||||||
|
bigint tmp(*this);
|
||||||
|
BN_mul( tmp.n, n, a.n, ctx );
|
||||||
|
BN_CTX_free(ctx);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
bigint bigint::operator / ( const bigint& a ) const {
|
||||||
|
BN_CTX* ctx = BN_CTX_new();
|
||||||
|
bigint tmp(*this);
|
||||||
|
BN_div( tmp.n, NULL, n, a.n, ctx );
|
||||||
|
BN_CTX_free(ctx);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
bigint bigint::operator - ( const bigint& a )const {
|
||||||
|
bigint tmp(*this);
|
||||||
|
BN_sub( tmp.n, n, a.n );
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bigint& bigint::operator = ( bigint&& a ) {
|
||||||
|
fc::swap( a.n, n );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
bigint& bigint::operator = ( const bigint& a ) {
|
||||||
|
if( &a == this )
|
||||||
|
return *this;
|
||||||
|
BN_copy( n, a.n );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
bigint::operator fc::string()const {
|
||||||
|
return BN_bn2dec(n);
|
||||||
|
}
|
||||||
|
} // namespace fc
|
||||||
Loading…
Reference in a new issue