-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbigintmath.cpp
More file actions
58 lines (48 loc) · 1.51 KB
/
bigintmath.cpp
File metadata and controls
58 lines (48 loc) · 1.51 KB
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
#include <gmpxx.h>
#include "bigintmath.hpp"
QCA::BigInteger operator-(const QCA::BigInteger &a, const QCA::BigInteger &b)
{
QCA::BigInteger c(a);
c -= b;
return c;
}
QCA::BigInteger operator/(const QCA::BigInteger &a, const QCA::BigInteger &b)
{
QCA::BigInteger c(a);
c /= b;
return c;
}
QCA::BigInteger operator%(const QCA::BigInteger &a, const QCA::BigInteger &b)
{
QCA::BigInteger c(a);
c %= b;
return c;
}
QCA::BigInteger operator*(const QCA::BigInteger &a, const QCA::BigInteger &b)
{
QCA::BigInteger c(a);
c *= b;
return c;
}
QCA::BigInteger operator+(const QCA::BigInteger &a, const QCA::BigInteger &b)
{
QCA::BigInteger c(a);
c += b;
return c;
}
QCA::BigInteger randomInRange(const QCA::BigInteger &min, const QCA::BigInteger &max)
{
gmp_randclass rng(gmp_randinit_default);
rng.seed(mpz_class(QCA::BigInteger(QCA::Random::randomArray(32)).toString().toLocal8Bit().data()));
QCA::BigInteger rand = mpz_class(rng.get_z_range(mpz_class((max - min).toString().toLocal8Bit().data()))).get_str().c_str();
//rand is now in [0,max-min); shift it up to [min, max)
return rand + min;
}
QCA::SecureArray randomBytes(unsigned int numBytes, QCA::SecureArray seed)
{
gmp_randclass rng(gmp_randinit_default);
rng.seed(mpz_class(QCA::BigInteger(seed).toString().toLocal8Bit().data()));
QCA::SecureArray result = QCA::BigInteger(mpz_class(rng.get_z_bits(numBytes*8 + 1)).get_str().c_str()).toArray();
result.resize(numBytes);
return result;
}