diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-11 18:26:11 +0200 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-11 18:26:11 +0200 |
commit | a9e588bd1a90175f411c7bda93969a9bec090af7 (patch) | |
tree | c912f15f43be3d32fb447a91982c0dddaffc595b | |
parent | 5f866c4a4b44d39b827a99d82006fa2d375c0ba9 (diff) |
Finishing up the BigInt class.
-rw-r--r-- | includes/BigInt.h | 16 | ||||
-rw-r--r-- | src/BigInt.cc | 78 |
2 files changed, 94 insertions, 0 deletions
diff --git a/includes/BigInt.h b/includes/BigInt.h index a74df6e..a7f5a76 100644 --- a/includes/BigInt.h +++ b/includes/BigInt.h @@ -70,6 +70,22 @@ class BigInt { bool operator<(const BigInt &) const; bool operator>(const BigInt &) const; + BigInt modadd(const BigInt & a, const BigInt & m) const throw (GeneralException); + BigInt modsub(const BigInt & a, const BigInt & m) const throw (GeneralException); + BigInt modmul(const BigInt & a, const BigInt & m) const throw (GeneralException); + BigInt modsqr(const BigInt & m) const throw (GeneralException); + BigInt modinv(const BigInt & m) const throw (GeneralException); + BigInt modpow(const BigInt & a, const BigInt & m) const throw (GeneralException); + + BigInt & do_modadd(const BigInt & a, const BigInt & m) throw (GeneralException); + BigInt & do_modsub(const BigInt & a, const BigInt & m) throw (GeneralException); + BigInt & do_modmul(const BigInt & a, const BigInt & m) throw (GeneralException); + BigInt & do_modsqr(const BigInt & m) throw (GeneralException); + BigInt & do_modinv(const BigInt & m) throw (GeneralException); + BigInt & do_modpow(const BigInt & a, const BigInt & m) throw (GeneralException); + + bool isPrime() const; + String toString(int radix = 10) const; char * makeString(int radix = 10) const; diff --git a/src/BigInt.cc b/src/BigInt.cc index 7a30e7e..04d5c1e 100644 --- a/src/BigInt.cc +++ b/src/BigInt.cc @@ -365,6 +365,84 @@ bool Balau::BigInt::operator>(const BigInt & a) const { 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) + throw GeneralException("Error while calling mp_addmod"); + return r; +} + +Balau::BigInt Balau::BigInt::modsub(const BigInt & a, const BigInt & m) const throw (GeneralException) { + BigInt r; + if (mp_submod(m_bi, a.m_bi, m.m_bi, r.m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_submod"); + return r; +} + +Balau::BigInt Balau::BigInt::modmul(const BigInt & a, const BigInt & m) const throw (GeneralException) { + BigInt r; + if (mp_mulmod(m_bi, a.m_bi, m.m_bi, r.m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_mulmod"); + return r; +} + +Balau::BigInt Balau::BigInt::modsqr(const BigInt & m) const throw (GeneralException) { + BigInt r; + if (mp_sqrmod(m_bi, m.m_bi, r.m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_sqrmod"); + return r; +} + +Balau::BigInt Balau::BigInt::modinv(const BigInt & m) const throw (GeneralException) { + BigInt r; + if (mp_invmod(m_bi, m.m_bi, r.m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_invmod"); + return r; +} + +Balau::BigInt Balau::BigInt::modpow(const BigInt & a, const BigInt & m) const throw (GeneralException) { + BigInt r; + if (mp_exptmod(m_bi, a.m_bi, m.m_bi, r.m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_exptmod"); + return r; +} + +Balau::BigInt & Balau::BigInt::do_modadd(const BigInt & a, const BigInt & m) throw (GeneralException) { + if (mp_addmod(m_bi, a.m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_addmod"); + return *this; +} + +Balau::BigInt & Balau::BigInt::do_modsub(const BigInt & a, const BigInt & m) throw (GeneralException) { + if (mp_submod(m_bi, a.m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_submod"); + return *this; +} + +Balau::BigInt & Balau::BigInt::do_modmul(const BigInt & a, const BigInt & m) throw (GeneralException) { + if (mp_mulmod(m_bi, a.m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_mulmod"); + return *this; +} + +Balau::BigInt & Balau::BigInt::do_modsqr(const BigInt & m) throw (GeneralException) { + if (mp_sqrmod(m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_sqrmod"); + return *this; +} + +Balau::BigInt & Balau::BigInt::do_modinv(const BigInt & m) throw (GeneralException) { + if (mp_invmod(m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_invmod"); + return *this; +} + +Balau::BigInt & Balau::BigInt::do_modpow(const BigInt & a, const BigInt & m) throw (GeneralException) { + if (mp_exptmod(m_bi, a.m_bi, m.m_bi, m_bi) != CRYPT_OK) + throw GeneralException("Error while calling mp_exptmod"); + return *this; +} + Balau::String Balau::BigInt::toString(int radix) const { char * out = (char *) alloca(mp_count_bits(m_bi) / (radix >= 10 ? 3 : 1) + 3); mp_toradix(m_bi, out, radix); |