-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnum_gmp.cpp
171 lines (133 loc) · 3.24 KB
/
num_gmp.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <assert.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <gmp.h>
#include "num_gmp.h"
namespace secp256k1 {
class NumberState {
private:
gmp_randstate_t rng;
public:
NumberState() {
gmp_randinit_default(rng);
}
~NumberState() {
gmp_randclear(rng);
}
void gen(mpz_t out, mpz_t size) {
mpz_urandomm(out, rng, size);
}
};
static NumberState number_state;
Number::Number(const Number &x) {
mpz_init_set(bn, x.bn);
}
Number::Number() {
mpz_init(bn);
}
Number::~Number() {
mpz_clear(bn);
}
Number &Number::operator=(const Number &x) {
mpz_set(bn, x.bn);
return *this;
}
void Number::SetNumber(const Number &x) {
mpz_set(bn, x.bn);
}
Number::Number(const unsigned char *bin, int len) {
mpz_init(bn);
SetBytes(bin,len);
}
void Number::SetBytes(const unsigned char *bin, unsigned int len) {
mpz_import(bn, len, 1, 1, 1, 0, bin);
}
bool Number::CheckBit(int pos) const {
return mpz_tstbit(bn, pos);
}
void Number::GetBytes(unsigned char *bin, unsigned int len) {
unsigned int size = (mpz_sizeinbase(bn,2)+7)/8;
assert(size <= len);
memset(bin,0,len);
size_t count = 0;
mpz_export(bin + len - size, &count, 1, 1, 1, 0, bn);
assert(count == 0 || size == count);
}
void Number::SetInt(int x) {
mpz_set_si(bn, x);
}
void Number::SetModInverse(const Number &x, const Number &m) {
mpz_invert(bn, x.bn, m.bn);
}
void Number::SetModMul(const Number &a, const Number &b, const Number &m) {
mpz_mul(bn, a.bn, b.bn);
mpz_mod(bn, bn, m.bn);
}
void Number::SetAdd(const Number &a1, const Number &a2) {
mpz_add(bn, a1.bn, a2.bn);
}
void Number::SetSub(const Number &a1, const Number &a2) {
mpz_sub(bn, a1.bn, a2.bn);
}
void Number::SetMult(const Number &a1, const Number &a2) {
mpz_mul(bn, a1.bn, a2.bn);
}
void Number::SetDiv(const Number &a1, const Number &a2) {
mpz_tdiv_q(bn, a1.bn, a2.bn);
}
void Number::SetMod(const Number &a, const Number &m) {
mpz_mod(bn, a.bn, m.bn);
}
int Number::Compare(const Number &a) const {
return mpz_cmp(bn, a.bn);
}
int Number::GetBits() const {
return mpz_sizeinbase(bn,2);
}
int Number::ShiftLowBits(int bits) {
int ret = mpz_get_ui(bn) & ((1 << bits) - 1);
mpz_fdiv_q_2exp(bn, bn, bits);
return ret;
}
bool Number::IsZero() const {
return mpz_size(bn) == 0;
}
bool Number::IsOdd() const {
return mpz_get_ui(bn) & 1;
}
bool Number::IsNeg() const {
return mpz_sgn(bn) < 0;
}
void Number::Negate() {
mpz_neg(bn, bn);
}
void Number::Shift1() {
mpz_fdiv_q_2exp(bn, bn, 1);
}
void Number::Inc() {
mpz_add_ui(bn, bn, 1);
}
void Number::SetHex(const std::string &str) {
mpz_set_str(bn, str.c_str(), 16);
}
void Number::SetPseudoRand(const Number &max) {
number_state.gen(bn, max.bn);
}
void Number::SplitInto(int bits, Number &low, Number &high) const {
mpz_t tmp;
mpz_init_set_ui(tmp,1);
mpz_mul_2exp(tmp,tmp,bits);
mpz_sub_ui(tmp,tmp,1);
mpz_and(low.bn, bn, tmp);
mpz_clear(tmp);
mpz_fdiv_q_2exp(high.bn, bn, bits);
}
std::string Number::ToString() const {
char *str = (char*)malloc(mpz_sizeinbase(bn,16) + 2);
mpz_get_str(str, 16, bn);
std::string ret(str);
free(str);
return ret;
}
}