aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/gnump/gmp_wrap.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-28 19:29:24 +0000
committerlloyd <[email protected]>2008-09-28 19:29:24 +0000
commit9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch)
treefe5e8ae9813b853549558b59833022e87e83981b /src/engine/gnump/gmp_wrap.cpp
parent9822a701516396b7de4e41339faecd48ff8dc8ff (diff)
Move all modules into src/ directory
Diffstat (limited to 'src/engine/gnump/gmp_wrap.cpp')
-rw-r--r--src/engine/gnump/gmp_wrap.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/engine/gnump/gmp_wrap.cpp b/src/engine/gnump/gmp_wrap.cpp
new file mode 100644
index 000000000..4c73c2562
--- /dev/null
+++ b/src/engine/gnump/gmp_wrap.cpp
@@ -0,0 +1,96 @@
+/*************************************************
+* GMP Wrapper Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/gmp_wrap.h>
+
+#define GNU_MP_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c))
+
+#define GNU_MP_VERSION_CODE \
+ GNU_MP_VERSION_CODE_FOR(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, \
+ __GNU_MP_VERSION_PATCHLEVEL)
+
+#if GNU_MP_VERSION_CODE < GNU_MP_VERSION_CODE_FOR(4,1,0)
+ #error Your GNU MP install is too old, upgrade to 4.1 or later
+#endif
+
+namespace Botan {
+
+/*************************************************
+* GMP_MPZ Constructor *
+*************************************************/
+GMP_MPZ::GMP_MPZ(const BigInt& in)
+ {
+ mpz_init(value);
+ if(in != 0)
+ mpz_import(value, in.sig_words(), -1, sizeof(word), 0, 0, in.data());
+ }
+
+/*************************************************
+* GMP_MPZ Constructor *
+*************************************************/
+GMP_MPZ::GMP_MPZ(const byte in[], u32bit length)
+ {
+ mpz_init(value);
+ mpz_import(value, length, 1, 1, 0, 0, in);
+ }
+
+/*************************************************
+* GMP_MPZ Copy Constructor *
+*************************************************/
+GMP_MPZ::GMP_MPZ(const GMP_MPZ& other)
+ {
+ mpz_init_set(value, other.value);
+ }
+
+/*************************************************
+* GMP_MPZ Destructor *
+*************************************************/
+GMP_MPZ::~GMP_MPZ()
+ {
+ mpz_clear(value);
+ }
+
+/*************************************************
+* GMP_MPZ Assignment Operator *
+*************************************************/
+GMP_MPZ& GMP_MPZ::operator=(const GMP_MPZ& other)
+ {
+ mpz_set(value, other.value);
+ return (*this);
+ }
+
+/*************************************************
+* Export the mpz_t as a bytestring *
+*************************************************/
+void GMP_MPZ::encode(byte out[], u32bit length) const
+ {
+ size_t dummy = 0;
+ mpz_export(out + (length - bytes()), &dummy, 1, 1, 0, 0, value);
+ }
+
+/*************************************************
+* Return the number of significant bytes *
+*************************************************/
+u32bit GMP_MPZ::bytes() const
+ {
+ return ((mpz_sizeinbase(value, 2) + 7) / 8);
+ }
+
+/*************************************************
+* GMP to BigInt Conversions *
+*************************************************/
+BigInt GMP_MPZ::to_bigint() const
+ {
+ BigInt out(BigInt::Positive, (bytes() + sizeof(word) - 1) / sizeof(word));
+ size_t dummy = 0;
+ mpz_export(out.get_reg(), &dummy, -1, sizeof(word), 0, 0, value);
+
+ if(mpz_sgn(value) < 0)
+ out.flip_sign();
+
+ return out;
+ }
+
+}