From 9245b692020ec6a6a4ba895d333e1a056df7dc82 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 8 Sep 2012 15:05:47 -0400 Subject: [PATCH] adding bigint --- include/fc/bigint.hpp | 42 ++++++++++++++++++++++ src/bigint.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 include/fc/bigint.hpp create mode 100644 src/bigint.cpp diff --git a/include/fc/bigint.hpp b/include/fc/bigint.hpp new file mode 100644 index 0000000..7c68f1d --- /dev/null +++ b/include/fc/bigint.hpp @@ -0,0 +1,42 @@ +#ifndef _FC_BIGINT_HPP +#define _FC_BIGINT_HPP +#include +#include + +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 diff --git a/src/bigint.cpp b/src/bigint.cpp new file mode 100644 index 0000000..76b8864 --- /dev/null +++ b/src/bigint.cpp @@ -0,0 +1,84 @@ +#include +#include +#include + +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