-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargeinteger.h
More file actions
193 lines (162 loc) · 5.77 KB
/
Copy pathlargeinteger.h
File metadata and controls
193 lines (162 loc) · 5.77 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef __RSA_LARGE_INTEGER_WCH_H
#define __RSA_LARGE_INTEGER_WCH_H
#include <cmath>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gmpxx.h>
#include "largeinteger.h"
using namespace std;
#define USE_HEX 16
#define USE_BIN 2
#define USE_DEC 10
// LargeInteger make use of gmp library
class LargeInteger{
private:
static bool rand_state_inited;
static gmp_randstate_t rand_state;
public:
LargeInteger(){ mpz_init(num); }
LargeInteger(std::string s, int m = USE_DEC){
mpz_init(num);
mpz_init_set_str(num, s.c_str(), m);
}
LargeInteger(int i){
mpz_init(num);
mpz_init_set_ui(num, i);
}
LargeInteger(mpz_t n){
mpz_init(num);
mpz_set(num, n);
}
LargeInteger(const LargeInteger& n){
mpz_init(num);
mpz_set(num, n.num);
}
mpz_t num;
void Show(){ gmp_printf("%Zd\n",num); }
void SetValue(std::string s, int m = USE_DEC){ mpz_init_set_str(num, s.c_str(), m); }
void SetValue(mpz_t n){ mpz_set(num,n); }
void SetValue(int i){ mpz_set_ui(num,i); }
void Delete() { mpz_clear(num); }
//Randomize
bool isPrime(){ return mpz_probab_prime_p(num, mpz_sizeinbase(num, 2) / 4); }
bool isCoprimeWith(LargeInteger LI){
LargeInteger tmp;
GCD(tmp, *this, LI);
bool result = mpz_cmp_ui(tmp.num, 1) == 0;
tmp.Delete();
return result;
}
void PrepareRandom(){
gmp_randinit_default(rand_state);
gmp_randseed_ui(rand_state, time(NULL));
rand_state_inited = true;
}
void ToPrimeRandom(int bits){
if(!rand_state_inited) PrepareRandom();
mpz_t temp;
mpz_init(temp);
mpz_ui_pow_ui(temp,2, bits - 1);
do{
mpz_urandomb(num, rand_state, bits - 1);
mpz_add(num, num, temp);
mpz_sub_ui(num, num, 1);
}while(!isPrime() || mpz_cmp(num, temp) <= 0);
mpz_clear(temp);
}
void ToCoprimeRandom(LargeInteger ref_LI, int bits){
if(!rand_state_inited) PrepareRandom();
mpz_t temp;
mpz_init(temp);
mpz_ui_pow_ui(temp,2, bits - 1);
do{
mpz_urandomb(num, rand_state, bits - 1);
mpz_add(num, num, temp);
mpz_sub_ui(num, num, 1);
}while(!isCoprimeWith(ref_LI) || mpz_cmp(num, temp) <= 0);
mpz_clear(temp);
}
//Operations
int Compare(int i){
return mpz_cmp_ui(num, i);
}
static void Totient(LargeInteger& out_LI, LargeInteger p, LargeInteger q){
mpz_t temp;
mpz_init(temp);
mpz_sub_ui(out_LI.num, q.num, 1);
mpz_sub_ui(temp, p.num, 1);
mpz_mul(out_LI.num, out_LI.num, temp);
mpz_clear(temp);
}
static void Add(LargeInteger& out_LI, LargeInteger p, LargeInteger q){
mpz_add(out_LI.num, p.num, q.num);
}
static void Subtract(LargeInteger& out_LI, LargeInteger p, LargeInteger q){
mpz_sub(out_LI.num, p.num, q.num);
}
static void Multiply(LargeInteger& out_LI, LargeInteger p, LargeInteger q){
mpz_mul(out_LI.num, p.num, q.num);
}
static void Divide(LargeInteger& out_LI, LargeInteger p, LargeInteger q){
mpz_div(out_LI.num, p.num, q.num);
}
static void Modulus(LargeInteger& out_LI, LargeInteger m, LargeInteger n){
mpz_mod(out_LI.num, m.num, n.num);
}
static void ModuloMod(LargeInteger& out_LI, LargeInteger m, LargeInteger n, LargeInteger o ){
mpz_powm(out_LI.num, m.num, n.num, o.num);
}
static void GCD(LargeInteger& out_LI ,LargeInteger m, LargeInteger n){
if( n.Compare(0) == 0 ) {
out_LI.SetValue(m.num);
}else {
Modulus(m,m,n);
GCD(out_LI, n, m);
}
}
static void ExtendedGCD(LargeInteger a, LargeInteger b, LargeInteger& x, LargeInteger& y, LargeInteger& res){
// Base Case
if (a.Compare(0) == 0){
x.SetValue(0);
y.SetValue(1);
res.SetValue(b.num);
return;
}
LargeInteger x1, y1; // To store results of recursive call
LargeInteger tmp;
LargeInteger::Modulus(tmp,b,a);
ExtendedGCD(tmp, a, x1, y1, res);
// Update x and y using results of recursive
// call
LargeInteger::Divide(x,b,a);
LargeInteger::Multiply(x,x,x1);
LargeInteger::Subtract(x, y1, x);
y.SetValue(x1.num);
x1.Delete();
y1.Delete();
tmp.Delete();
}
static bool ModularMultiplicativeInverse(LargeInteger a, LargeInteger m, LargeInteger &out_LI){
LargeInteger x, y;
LargeInteger g;
ExtendedGCD(a, m, x, y, g);
if (g.Compare(1) != 0){
g.Delete();
x.Delete();
y.Delete();
return false;
}else{
LargeInteger::Modulus(out_LI, x ,m);
LargeInteger::Add(out_LI, out_LI, m);
LargeInteger::Modulus(out_LI, out_LI, m);
g.Delete();
x.Delete();
y.Delete();
return true;
}
}
};
#endif