/
damgard_jurik.cpp
300 lines (247 loc) · 8.25 KB
/
damgard_jurik.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
libdamgard_jurik - A library implementing the Damgård–Jurik Cryptosystem.
Copyright (C) 2016 Maxxie,XJTU.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <cstring>
#include "damgard_jurik.h"
damgard_jurik_text_t::damgard_jurik_text_t(mpz_t text) {
mpz_init(this->text);
mpz_set(this->text, text);
}
damgard_jurik_text_t::damgard_jurik_text_t() {
mpz_init(text);
}
damgard_jurik_text_t::damgard_jurik_text_t(unsigned long integer) {
mpz_init(text);
mpz_init_set_ui(text, integer);
}
damgard_jurik_text_t::damgard_jurik_text_t(void *bytes, int len) {
mpz_init(text);
mpz_import(text, len, 1, 1, 0, 0, bytes);
}
damgard_jurik_text_t::damgard_jurik_text_t(const char str[]) {
mpz_init(text);
mpz_import(text, strlen(str), 1, 1, 0, 0, str);
}
char* damgard_jurik_text_t::to_str() {
char *buf;
size_t len;
buf = (char *)mpz_export(NULL, &len, 1, 1, 0, 0, text);
buf[len] = 0;
return buf;
}
void* damgard_jurik_text_t::to_bytes() {
void *buf;
void *buf_1 = malloc(this->size());
memset(buf_1, 0, this->size());
size_t written;
buf = mpz_export(NULL, &written, 1, 1, 0, 0, text);
if (written == this->size())
return buf;
memcpy((unsigned char *)buf_1 + (this->size() - written), buf, written);
free(buf);
return buf_1;
}
void* damgard_jurik_text_t::to_bytes(size_t len) {
void *buf;
void *buf_1 = malloc(len);
memset(buf_1, 0, len);
size_t written;
buf = mpz_export(NULL, &written, 1, 1, 0, 0, text);
if (written == len)
return buf;
if (written > len)
return NULL;
memcpy((unsigned char *)buf_1 + (len - written), buf, written);
free(buf);
return buf_1;
}
size_t damgard_jurik_text_t::size() {
return mpz_size(text) * sizeof(mp_limb_t);
}
damgard_jurik_ciphertext_t operator*(damgard_jurik_ciphertext_t a, damgard_jurik_ciphertext_t b) {
damgard_jurik_ciphertext_t return_object;
mpz_init(return_object.text);
mpz_mul(return_object.text, a.text, b.text);
mpz_mod(return_object.text, return_object.text, a.n_s);
mpz_set(return_object.n_s, a.n_s);
return_object.s = a.s;
return return_object;
}
damgard_jurik_ciphertext_t operator^(damgard_jurik_ciphertext_t &a, damgard_jurik_text_t &b) {
damgard_jurik_ciphertext_t return_object;
mpz_init(return_object.text);
mpz_powm(return_object.text, a.text, b.text, a.n_s);
mpz_set(return_object.n_s, a.n_s);
return_object.s = a.s;
return return_object;
}
void damgard_jurik::init_rand(gmp_randstate_t rand, int bytes) {
void* buf;
mpz_t s;
buf = new char[bytes];
this->rand_func(buf, bytes);
gmp_randinit_default(rand);
mpz_init(s);
mpz_import(s, bytes, 1, 1, 0, 0, buf);
gmp_randseed(rand, s);
mpz_clear(s);
free(buf);
}
mpz_t* damgard_jurik::get_ns(int s) {
return &pubkey->n_j[s];
}
void damgard_jurik_function_l(mpz_t result ,mpz_t b, mpz_t n) {
mpz_sub_ui(result, b, 1);
mpz_div(result, result, n);
}
void damgard_jurik::key_gen(int modulusbits) {
mpz_t p, q;
gmp_randstate_t rand;
mpz_init(pubkey->n);
mpz_init(pubkey->g);
mpz_init(prvkey->lambda);
mpz_init(p);
mpz_init(q);
init_rand(rand, modulusbits / 8 + 1);
do
{
do
mpz_urandomb(p, rand, modulusbits / 2);
while( !mpz_probab_prime_p(p, 10) );
do
mpz_urandomb(q, rand, modulusbits / 2);
while( !mpz_probab_prime_p(q, 10) );
/* compute the public modulus n = p q */
mpz_mul(pubkey->n, p, q);
} while( !mpz_tstbit(pubkey->n, modulusbits - 1));
pubkey->bits = modulusbits;
mpz_add_ui(pubkey->g, pubkey->n, 1);
pubkey->n_j = new mpz_t[s_max + 2];
pubkey->k_n = new mpz_t[s_max + 2];
mpz_init(pubkey->n_j[0]);
mpz_set_ui(pubkey->n_j[0], 1);
mpz_init(pubkey->k_n[0]);
mpz_set_ui(pubkey->k_n[0], 1);
for (int i = 1;i <= s_max + 1;i++) {
mpz_init(pubkey->n_j[i]);
mpz_mul(pubkey->n_j[i], pubkey->n_j[i - 1], pubkey->n);
mpz_init(pubkey->k_n[i]);
mpz_mul_ui(pubkey->k_n[i], pubkey->k_n[i - 1], i);
}
/* Compute Private Key */
mpz_sub_ui(p, p, 1);
mpz_sub_ui(q, q, 1);
mpz_lcm(prvkey->lambda, p, q);
mpz_clear(p);
mpz_clear(q);
gmp_randclear(rand);
}
damgard_jurik::damgard_jurik(unsigned long s, int bitsmodule, damgard_jurik_get_rand_t rand_func) {
pubkey = new damgard_jurik_pubkey_t();
prvkey = new damgard_jurik_prvkey_t();
this->s_max = s;
this->rand_func = rand_func;
key_gen(bitsmodule);
}
damgard_jurik_ciphertext_t* damgard_jurik::encrypt(damgard_jurik_plaintext_t* pt, unsigned long s) {
mpz_t r;
gmp_randstate_t rand;
mpz_t x;
damgard_jurik_ciphertext_t *res = new damgard_jurik_ciphertext_t();
/* pick random blinding factor */
mpz_init(r);
init_rand(rand, pubkey->bits / 8 + 1);
do
mpz_urandomb(r, rand, pubkey->bits);
while( mpz_cmp(r, pubkey->n) >= 0 );
mpz_init(x);
mpz_powm(res->text, pubkey->g, pt->text, pubkey->n_j[s + 1]);
mpz_powm(x, r, pubkey->n_j[s], pubkey->n_j[s + 1]);
mpz_mul(res->text, res->text, x);
mpz_mod(res->text, res->text, pubkey->n_j[s + 1]);
mpz_set(res->n_s, pubkey->n_j[s + 1]);
res->s = s;
mpz_clear(x);
mpz_clear(r);
gmp_randclear(rand);
return res;
}
void damgard_jurik::encrypt(damgard_jurik_ciphertext_t **list, damgard_jurik_plaintext_t **text, unsigned long s, int size) {
mpz_t r;
gmp_randstate_t rand;
mpz_t x;
/* pick random blinding factor */
mpz_init(r);
init_rand(rand, pubkey->bits / 8 + 1);
do
mpz_urandomb(r, rand, pubkey->bits);
while (mpz_cmp(r, pubkey->n) >= 0);
mpz_init(x);
mpz_powm(x, r, pubkey->n_j[s], pubkey->n_j[s + 1]);
for (int i = 0; i < size; i++) {
list[i] = new damgard_jurik_ciphertext_t();
mpz_powm(list[i]->text, pubkey->g, text[i]->text, pubkey->n_j[s + 1]);
mpz_mul(list[i]->text, list[i]->text, x);
mpz_mod(list[i]->text, list[i]->text, pubkey->n_j[s + 1]);
mpz_set(list[i]->n_s, pubkey->n_j[s + 1]);
list[i]->s = s;
}
mpz_clear(x);
mpz_clear(r);
gmp_randclear(rand);
}
damgard_jurik_ciphertext_t* damgard_jurik::encrypt(damgard_jurik_plaintext_t* pt) {
return encrypt(pt, get_s(pt));
}
damgard_jurik_plaintext_t* damgard_jurik::decrypt(damgard_jurik_ciphertext_t* ct) {
damgard_jurik_plaintext_t *res = new damgard_jurik_plaintext_t();
mpz_t c_r;
mpz_init(c_r);
mpz_powm(c_r, ct->text, prvkey->lambda, pubkey->n_j[ct->s + 1]);
int i, j;
mpz_t t1, t2, t3, i_lamda;
mpz_init(t1);
mpz_init(t2);
mpz_init(t3);
mpz_init(i_lamda);
mpz_set_ui(i_lamda, 0);
for (i = 1;i <= ct->s;++i) {
mpz_mod(t1, c_r, pubkey->n_j[i + 1]);
damgard_jurik_function_l(t1, t1, pubkey->n_j[1]);
mpz_set(t2, i_lamda);
for (j = 2;j <= i;j++) {
mpz_sub_ui(i_lamda, i_lamda, 1);
mpz_mul(t2, t2, i_lamda);
mpz_mod(t2, t2, pubkey->n_j[i]);
mpz_set(t3, pubkey->k_n[j]);
mpz_invert(t3, t3, pubkey->n_j[i]);
mpz_mul(t3, t2, t3);
mpz_mod(t3, t3, pubkey->n_j[i]);
mpz_mul(t3, t3, pubkey->n_j[j - 1]);
mpz_mod(t3, t3, pubkey->n_j[i]);
mpz_sub(t1, t1, t3);
mpz_mod(t1, t1, pubkey->n_j[i]);
}
mpz_set(i_lamda, t1);
}
mpz_invert(t3, prvkey->lambda, pubkey->n_j[ct->s]);
mpz_mul(res->text, i_lamda, t3);
mpz_mod(res->text, res->text, pubkey->n_j[ct->s]);
return res;
}
unsigned long damgard_jurik::get_s(damgard_jurik_text_t *te) {
size_t len = te->size();
return len / pubkey->bits * 8 + 1;
}