From d5a38318b3876bbedffe52ca3b378b03ad4d995a Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sun, 11 Aug 2013 18:43:08 +0200 Subject: Forgot the decimal versions of the comparators. --- includes/BigInt.h | 7 +++++++ src/BigInt.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/includes/BigInt.h b/includes/BigInt.h index a7f5a76..29a2740 100644 --- a/includes/BigInt.h +++ b/includes/BigInt.h @@ -54,6 +54,7 @@ class BigInt { enum comp_t { LT, GT, EQ }; comp_t comp(const BigInt &) const throw (GeneralException); + comp_t comp(unsigned int) const throw (GeneralException); BigInt & neg() throw (GeneralException); @@ -63,11 +64,17 @@ class BigInt { BigInt gcd(const BigInt &) const throw (GeneralException); BigInt lcm(const BigInt &) const throw (GeneralException); + bool operator==(unsigned int) const; bool operator==(const BigInt &) const; + bool operator!=(unsigned int) const; bool operator!=(const BigInt &) const; + bool operator<=(unsigned int) const; bool operator<=(const BigInt &) const; + bool operator>=(unsigned int) const; bool operator>=(const BigInt &) const; + bool operator<(unsigned int) const; bool operator<(const BigInt &) const; + bool operator>(unsigned int) const; bool operator>(const BigInt &) const; BigInt modadd(const BigInt & a, const BigInt & m) const throw (GeneralException); diff --git a/src/BigInt.cc b/src/BigInt.cc index 04d5c1e..0c1b1a5 100644 --- a/src/BigInt.cc +++ b/src/BigInt.cc @@ -335,6 +335,20 @@ Balau::BigInt::comp_t Balau::BigInt::comp(const BigInt & a) const throw (General } } +Balau::BigInt::comp_t Balau::BigInt::comp(unsigned int a) const throw (GeneralException) { + int r = mp_cmp_d(m_bi, a); + switch (r) { + case LTC_MP_LT: + return LT; + case LTC_MP_GT: + return GT; + case LTC_MP_EQ: + return EQ; + default: + throw GeneralException("Unknown result from mp_cmp_d"); + } +} + bool Balau::BigInt::operator==(const BigInt & a) const { comp_t r = comp(a); return r == EQ; @@ -365,6 +379,36 @@ bool Balau::BigInt::operator>(const BigInt & a) const { return r == GT; } +bool Balau::BigInt::operator==(unsigned int a) const { + comp_t r = comp(a); + return r == EQ; +} + +bool Balau::BigInt::operator!=(unsigned int a) const { + comp_t r = comp(a); + return r != EQ; +} + +bool Balau::BigInt::operator<=(unsigned int a) const { + comp_t r = comp(a); + return r == LT || r == EQ; +} + +bool Balau::BigInt::operator>=(unsigned int a) const { + comp_t r = comp(a); + return r == GT || r == EQ; +} + +bool Balau::BigInt::operator<(unsigned int a) const { + comp_t r = comp(a); + return r == LT; +} + +bool Balau::BigInt::operator>(unsigned int a) const { + comp_t r = comp(a); + return r == GT; +} + Balau::BigInt Balau::BigInt::modadd(const BigInt & a, const BigInt & m) const throw (GeneralException) { BigInt r; if (mp_addmod(m_bi, a.m_bi, m.m_bi, r.m_bi) != CRYPT_OK) -- cgit v1.2.3