1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*************************************************
* GMP Wrapper Source File *
* (C) 1999-2008 The Botan Project *
*************************************************/
#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;
}
}
|