-
Notifications
You must be signed in to change notification settings - Fork 176
/
relic_bn_util.c
523 lines (429 loc) · 8.86 KB
/
relic_bn_util.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* RELIC is an Efficient LIbrary for Cryptography
* Copyright (c) 2009 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
* for contact information.
*
* RELIC is free software; you can redistribute it and/or modify it under the
* terms of the version 2.1 (or later) of the GNU Lesser General Public License
* as published by the Free Software Foundation; or version 2.0 of the Apache
* License as published by the Apache Software Foundation. See the LICENSE files
* for more details.
*
* RELIC 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 LICENSE files for more details.
*
* You should have received a copy of the GNU Lesser General Public or the
* Apache License along with RELIC. If not, see <https://www.gnu.org/licenses/>
* or <https://www.apache.org/licenses/>.
*/
/**
* @file
*
* Implementation of the multiple precision utilities.
*
* @ingroup bn
*/
#include <inttypes.h>
#include "relic_core.h"
/*============================================================================*/
/* Private definitions */
/*============================================================================*/
/**
* Statistical distance 1/2^\lambda between sampling and uniform distribution.
*/
#define RAND_DIST 40
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
void bn_copy(bn_t c, const bn_t a) {
if (c->dp == a->dp) {
return;
}
bn_grow(c, a->used);
dv_copy(c->dp, a->dp, a->used);
c->used = a->used;
c->sign = a->sign;
bn_trim(c);
}
void bn_abs(bn_t c, const bn_t a) {
if (c->dp != a->dp) {
bn_copy(c, a);
}
c->sign = RLC_POS;
}
void bn_neg(bn_t c, const bn_t a) {
if (c->dp != a->dp) {
bn_copy(c, a);
}
if (!bn_is_zero(c)) {
c->sign = a->sign ^ 1;
}
}
int bn_sign(const bn_t a) {
return a->sign;
}
void bn_zero(bn_t a) {
a->sign = RLC_POS;
a->used = 1;
dv_zero(a->dp, a->alloc);
}
int bn_is_zero(const bn_t a) {
if (a->used == 0) {
return 1;
}
if ((a->used == 1) && (a->dp[0] == 0)) {
return 1;
}
return 0;
}
int bn_is_even(const bn_t a) {
if (bn_is_zero(a)) {
return 1;
}
if ((a->dp[0] & 0x01) == 0) {
return 1;
}
return 0;
}
size_t bn_bits(const bn_t a) {
uint_t bits;
if (bn_is_zero(a)) {
return 0;
}
/* Bits in lower digits. */
bits = (a->used - 1) * RLC_DIG;
return bits + util_bits_dig(a->dp[a->used - 1]);
}
int bn_get_bit(const bn_t a, uint_t bit) {
int d;
if (bit > bn_bits(a)) {
return 0;
}
RLC_RIP(bit, d, bit);
if (d >= a->used) {
return 0;
} else {
return (a->dp[d] >> bit) & (dig_t)1;
}
}
void bn_set_bit(bn_t a, uint_t bit, int value) {
int d;
RLC_RIP(bit, d, bit);
bn_grow(a, d);
if (value == 1) {
a->dp[d] |= ((dig_t)1 << bit);
if ((d + 1) > a->used) {
a->used = d + 1;
}
} else {
a->dp[d] &= ~((dig_t)1 << bit);
bn_trim(a);
}
}
uint_t bn_ham(const bn_t a) {
int c = 0;
for (int i = 0; i < bn_bits(a); i++) {
c += bn_get_bit(a, i);
}
return c;
}
void bn_get_dig(dig_t *c, const bn_t a) {
*c = a->dp[0];
}
void bn_set_dig(bn_t a, dig_t digit) {
bn_zero(a);
a->dp[0] = digit;
a->used = 1;
a->sign = RLC_POS;
}
void bn_set_2b(bn_t a, size_t b) {
int i, d;
if (b >= RLC_BN_SIZE * RLC_DIG) {
RLC_THROW(ERR_NO_VALID);
} else {
RLC_RIP(b, d, b);
bn_grow(a, d + 1);
for (i = 0; i < d; i++) {
a->dp[i] = 0;
}
a->used = d + 1;
a->dp[d] = ((dig_t)1 << b);
a->sign = RLC_POS;
}
}
void bn_rand(bn_t a, int sign, size_t bits) {
int digits;
RLC_RIP(bits, digits, bits);
digits += (bits > 0 ? 1 : 0);
bn_grow(a, digits);
rand_bytes((uint8_t *)a->dp, digits * sizeof(dig_t));
a->used = digits;
a->sign = sign;
if (bits > 0) {
dig_t mask = ((dig_t)1 << (dig_t)bits) - 1;
a->dp[a->used - 1] &= mask;
}
bn_trim(a);
}
void bn_rand_mod(bn_t a, const bn_t b) {
bn_t t;
bn_null(t);
RLC_TRY {
bn_new(t);
bn_copy(t, b);
do {
bn_rand(a, bn_sign(t), bn_bits(t) + RAND_DIST);
bn_mod(a, a, t);
} while (bn_is_zero(a) || bn_cmp_abs(a, t) != RLC_LT);
} RLC_CATCH_ANY {
RLC_THROW(ERR_CAUGHT);
} RLC_FINALLY {
bn_free(t);
}
}
void bn_print(const bn_t a) {
int i;
if (a->sign == RLC_NEG) {
util_print("-");
}
if (a->used == 0) {
util_print("0\n");
} else {
#if WSIZE == 64
util_print_dig(a->dp[a->used - 1], 0);
for (i = a->used - 2; i >= 0; i--) {
util_print_dig(a->dp[i], 1);
}
#else
util_print_dig(a->dp[a->used - 1], 0);
for (i = a->used - 2; i >= 0; i--) {
util_print_dig(a->dp[i], 1);
}
#endif
util_print("\n");
}
}
size_t bn_size_str(const bn_t a, uint_t radix) {
size_t digits = 0;
bn_t t;
bn_null(t);
/* Check the radix. */
if (radix < 2 || radix > 64) {
RLC_THROW(ERR_NO_VALID);
return 0;
}
if (bn_is_zero(a)) {
return 2;
}
/* Binary case requires the bits, a sign and the null terminator. */
if (radix == 2) {
return bn_bits(a) + (a->sign == RLC_NEG ? 1 : 0) + 1;
}
if (a->sign == RLC_NEG) {
digits++;
}
RLC_TRY {
bn_new(t);
bn_copy(t, a);
t->sign = RLC_POS;
while (!bn_is_zero(t)) {
bn_div_dig(t, t, (dig_t)radix);
digits++;
}
} RLC_CATCH_ANY {
RLC_THROW(ERR_CAUGHT);
} RLC_FINALLY {
bn_free(t);
}
return digits + 1;
}
void bn_read_str(bn_t a, const char *str, size_t len, uint_t radix) {
int sign, i, j;
char c;
bn_zero(a);
if (radix < 2 || radix > 64) {
RLC_THROW(ERR_NO_VALID);
return;
}
j = 0;
if (str[0] == '-') {
j++;
sign = RLC_NEG;
} else {
sign = RLC_POS;
}
RLC_TRY {
bn_grow(a, RLC_CEIL(len * util_bits_dig(radix), RLC_DIG));
while (j < len) {
if (str[j] == 0) {
break;
}
c = (char)((radix < 36) ? RLC_UPP(str[j]) : str[j]);
for (i = 0; i < 64; i++) {
if (c == util_conv_char(i)) {
break;
}
}
if (i < radix) {
bn_mul_dig(a, a, (dig_t)radix);
bn_add_dig(a, a, (dig_t)i);
} else {
break;
}
j++;
}
a->sign = sign;
bn_trim(a);
} RLC_CATCH_ANY {
RLC_THROW(ERR_CAUGHT);
}
}
void bn_write_str(char *str, size_t len, const bn_t a, uint_t radix) {
bn_t t;
dig_t d;
int l, i, j;
char c;
bn_null(t);
l = bn_size_str(a, radix);
if (len < l) {
RLC_THROW(ERR_NO_BUFFER);
return;
}
if (radix < 2 || radix > 64) {
RLC_THROW(ERR_NO_VALID);
return;
}
if (bn_is_zero(a) == 1) {
*str++ = '0';
*str = '\0';
return;
}
RLC_TRY {
bn_new(t);
bn_copy(t, a);
j = 0;
if (t->sign == RLC_NEG) {
str[j] = '-';
j++;
t->sign = RLC_POS;
}
while (!bn_is_zero(t) && j < len) {
bn_div_rem_dig(t, &d, t, (dig_t)radix);
str[j++] = util_conv_char(d);
}
/* Reverse the digits of the string. */
i = 0;
if (str[0] == '-') {
i = 1;
}
j = l - 2;
while (i < j) {
c = str[i];
str[i] = str[j];
str[j] = c;
++i;
--j;
}
str[l - 1] = '\0';
}
RLC_CATCH_ANY {
RLC_THROW(ERR_CAUGHT);
}
RLC_FINALLY {
bn_free(t);
}
}
size_t bn_size_bin(const bn_t a) {
dig_t d;
int digits;
digits = (a->used - 1) * (RLC_DIG / 8);
d = a->dp[a->used - 1];
while (d != 0) {
d = d >> 8;
digits++;
}
return digits;
}
void bn_read_bin(bn_t a, const uint8_t *bin, size_t len) {
int i, j;
dig_t d = (RLC_DIG / 8);
int digs = (len % d == 0 ? len / d : len / d + 1);
bn_grow(a, digs);
bn_zero(a);
a->used = digs;
for (i = 0; i < digs - 1; i++) {
d = 0;
for (j = (RLC_DIG / 8) - 1; j >= 0; j--) {
d = d << 8;
d |= bin[len - 1 - (i * (RLC_DIG / 8) + j)];
}
a->dp[i] = d;
}
d = 0;
for (j = (RLC_DIG / 8) - 1; j >= 0; j--) {
if ((int)(i * (RLC_DIG / 8) + j) < len) {
d = d << 8;
d |= bin[len - 1 - (i * (RLC_DIG / 8) + j)];
}
}
a->dp[i] = d;
a->sign = RLC_POS;
bn_trim(a);
}
void bn_write_bin(uint8_t *bin, size_t len, const bn_t a) {
size_t size, k;
dig_t d;
size = bn_size_bin(a);
if (len < size) {
RLC_THROW(ERR_NO_BUFFER);
return;
}
k = 0;
for (int i = 0; i < a->used - 1; i++) {
d = a->dp[i];
for (int j = 0; j < (int)(RLC_DIG / 8); j++) {
bin[len - 1 - k++] = d & 0xFF;
d = d >> 8;
}
}
d = a->dp[a->used - 1];
while (d != 0) {
bin[len - 1 - k++] = d & 0xFF;
d = d >> 8;
}
while (k < len) {
bin[len - 1 - k++] = 0;
}
}
size_t bn_size_raw(const bn_t a) {
return a->used;
}
void bn_read_raw(bn_t a, const dig_t *raw, size_t len) {
RLC_TRY {
bn_grow(a, len);
a->used = len;
a->sign = RLC_POS;
dv_copy(a->dp, raw, len);
bn_trim(a);
} RLC_CATCH_ANY {
RLC_THROW(ERR_CAUGHT);
}
}
void bn_write_raw(dig_t *raw, size_t len, const bn_t a) {
int i, size;
size = a->used;
if (len < size) {
RLC_THROW(ERR_NO_BUFFER);
return;
}
for (i = 0; i < size; i++) {
raw[i] = a->dp[i];
}
for (; i < len; i++) {
raw[i] = 0;
}
}