summaryrefslogtreecommitdiff
path: root/src/BigInt.cc
blob: 0df2e14decb419037494f281cbf9ece1526afdb4 (plain)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include <malloc.h>
#include <math.h>
#include "tommath.h"
#include "BigInt.h"
#include "Main.h"

namespace {

class InitMP : public Balau::AtStart {
  public:
      InitMP() : AtStart(20) { }
    void doStart() {
        m_initialized = true;
        static Balau::BigInt s2p32;
        s2p32.set2expt(32);
        m_2p32 = &s2p32;
    }
    const Balau::BigInt & get2p32() {
        return *m_2p32;
    }
    bool initialized() { return m_initialized; }
  private:
    Balau::BigInt * m_2p32 = NULL;
    bool m_initialized = false;
};

static InitMP s_MP;

};

Balau::BigInt::BigInt() throw (GeneralException) {
    AAssert(s_MP.initialized(), "You can't statically declare a BigInt.");

    m_bi = calloc(1, sizeof(mp_int));
    if (m_bi == NULL)
        throw GeneralException("Unable to allocate ");

    if (mp_init((mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_init");
}

Balau::BigInt::BigInt(const BigInt & v) throw (GeneralException) {
    AAssert(s_MP.initialized(), "You can't statically declare a BigInt.");

    m_bi = calloc(1, sizeof(mp_int));
    if (m_bi == NULL)
        throw GeneralException("Unable to allocate ");

    if (mp_init_copy((mp_int *)m_bi, (mp_int *)v.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_init_copy");
}

Balau::BigInt::BigInt(BigInt && v) {
    m_bi = v.m_bi;
    v.m_bi = NULL;
}

Balau::BigInt::~BigInt() {
    if (!m_bi)
        return;
    mp_clear((mp_int *) m_bi);
    free(m_bi);
    m_bi = NULL;
}

Balau::BigInt & Balau::BigInt::operator=(const BigInt & v) throw (GeneralException) {
    if (&v == this)
        return *this;

    if (mp_copy((mp_int *) v.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_init_copy");

    return *this;
}

void Balau::BigInt::set(uint64_t v) throw (GeneralException) {
    uint32_t low  = v & 0xffffffff;
    uint32_t high = v >> 32;
    if (high == 0) {
        if (mp_set_int((mp_int *) m_bi, low) != MP_OKAY)
            throw GeneralException("Error while calling mp_set_init");
    } else {
        if (mp_set_int((mp_int *) m_bi, high) != MP_OKAY)
            throw GeneralException("Error while calling mp_set_init");
        operator*=(s_MP.get2p32());
        operator+=(low);
    }
}

void Balau::BigInt::set(int64_t v) {
    if (v >= 0) {
        set((uint64_t) v);
    } else {
        v = -v;
        set((uint64_t) v);
        do_neg();
    }
}

void Balau::BigInt::set(uint32_t v) throw (GeneralException) {
    if (mp_set_int((mp_int *) m_bi, v) != MP_OKAY)
        throw GeneralException("Error while calling mp_set_init");
}

void Balau::BigInt::set(int32_t v) {
    if (v >= 0) {
        set((uint32_t) v);
    } else {
        v = -v;
        set((uint32_t) v);
        do_neg();
    }
}

void Balau::BigInt::set(double v) throw (GeneralException) {
    double f, i;
    f = modf(v, &i);
    AAssert(f == 0.0, "Can't set a BigInt with a double that has a fractional value");

    int e;

    f = frexp(v, &e);
    if (mp_set_int((mp_int *) m_bi, 0) != MP_OKAY)
        throw GeneralException("Error while calling mp_set_init");

    for (e -= 1.0; e > 0.0; e -= 1.0) {
        f *= 2.0;
        if (f >= 1.0) {
            operator+=(1);
            f -= 1.0;
        }
        operator*=(2);
    }
}

void Balau::BigInt::set(const String & v, int radix) throw (GeneralException) {
    if (mp_read_radix((mp_int *) m_bi, v.to_charp(), radix) != MP_OKAY)
        throw GeneralException("Error while calling mp_read_radix");
}

void Balau::BigInt::set2expt(int i) throw (GeneralException) {
    if (mp_2expt((mp_int *) m_bi, i) != MP_OKAY)
        throw GeneralException("Error while calling mp_2expt");
}

static unsigned long get_digit(void * a, int n) {
    mp_int * A = (mp_int *) a;
    return (n >= A->used || n < 0) ? 0 : A->dp[n];
}

uint64_t Balau::BigInt::to_uint64() const throw (GeneralException) {
    if (mp_count_bits((mp_int *) m_bi) > 64)
        throw GeneralException("BigInt too big to fit in a uint64");
    uint64_t v = 0;
    int shift = 0;
    int digit = 0;
    while (shift <= 64) {
        v |= get_digit(m_bi, digit++) << shift;
        shift += MP_DIGIT_BIT;
    }
    return v;
}

int64_t Balau::BigInt::to_int64() const throw (GeneralException) {
    if (mp_count_bits((mp_int *) m_bi) > 63)
        throw GeneralException("BigInt too big to fit in a int64");
    int64_t v = 0;
    int shift = 0;
    int digit = 0;
    while (shift <= 63) {
        v |= get_digit(m_bi, digit++) << shift;
        shift += MP_DIGIT_BIT;
    }
    return comp(0) == LT ? -v : v;
}

uint32_t Balau::BigInt::to_uint32() const throw (GeneralException) {
    if (mp_count_bits((mp_int *) m_bi) > 32)
        throw GeneralException("BigInt too big to fit in a uint32");
    uint64_t v = 0;
    int shift = 0;
    int digit = 0;
    while (shift <= 32) {
        v |= get_digit(m_bi, digit++) << shift;
        shift += MP_DIGIT_BIT;
    }
    return v;
}

int32_t Balau::BigInt::to_int32() const throw (GeneralException) {
    if (mp_count_bits((mp_int *) m_bi) > 31)
        throw GeneralException("BigInt too big to fit in a uint32");
    int64_t v = 0;
    int shift = 0;
    int digit = 0;
    while (shift <= 31) {
        v |= get_digit(m_bi, digit++) << shift;
        shift += MP_DIGIT_BIT;
    }
    return comp(0) == LT ? -v : v;
}

Balau::BigInt Balau::BigInt::operator^(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_xor((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_xor");
    return r;
}

Balau::BigInt Balau::BigInt::operator|(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_or((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_or");
    return r;
}

Balau::BigInt Balau::BigInt::operator&(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_and((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_and");
    return r;
}

Balau::BigInt Balau::BigInt::operator+(unsigned int i) const throw (GeneralException) {
    BigInt r;
    if (mp_add_d((mp_int *) m_bi, i, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_add_d");
    return r;
}

Balau::BigInt Balau::BigInt::operator+(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_add((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_add");
    return r;
}

Balau::BigInt Balau::BigInt::operator-(unsigned int i) const throw (GeneralException) {
    BigInt r;
    if (mp_sub_d((mp_int *) m_bi, i, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sub_d");
    return r;
}

Balau::BigInt Balau::BigInt::operator-(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_sub((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sub");
    return r;
}

Balau::BigInt Balau::BigInt::operator*(unsigned int i) const throw (GeneralException) {
    BigInt r;
    if (mp_mul_d((mp_int *) m_bi, i, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mul_d");
    return r;
}

Balau::BigInt Balau::BigInt::operator*(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_mul((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mul");
    return r;
}

Balau::BigInt Balau::BigInt::operator/(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_div((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi, NULL) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return r;
}

Balau::BigInt Balau::BigInt::operator%(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_div((mp_int *) m_bi, (mp_int *) a.m_bi, NULL, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return r;
}

Balau::BigInt Balau::BigInt::operator<<(unsigned int a) const throw (GeneralException) {
    BigInt r;
    if (mp_mul_d((mp_int *) m_bi, 1 << a, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return *this;
}

Balau::BigInt Balau::BigInt::operator>>(unsigned int a) const {
    BigInt s;
    s.set2expt(a);
    return operator/(s);
}

Balau::BigInt & Balau::BigInt::operator^=(const BigInt & a) throw (GeneralException) {
    if (mp_xor((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_xor");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator|=(const BigInt & a) throw (GeneralException) {
    BigInt r;
    if (mp_or((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_or");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator&=(const BigInt & a) throw (GeneralException) {
    BigInt r;
    if (mp_and((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_and");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator+=(unsigned int i) throw (GeneralException) {
    if (mp_add_d((mp_int *) m_bi, i, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_add_d");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator+=(const BigInt & a) throw (GeneralException) {
    if (mp_add((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_add");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator-=(unsigned int i) throw (GeneralException) {
    if (mp_sub_d((mp_int *) m_bi, i, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sub_d");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator-=(const BigInt & a) throw (GeneralException) {
    if (mp_sub((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sub");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator*=(unsigned int i) throw (GeneralException) {
    if (mp_mul_d((mp_int *) m_bi, i, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mul_d");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator*=(const BigInt & a) throw (GeneralException) {
    if (mp_mul((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mul");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator/=(const BigInt & a) throw (GeneralException) {
    if (mp_div((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m_bi, NULL) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator%=(const BigInt & a) throw (GeneralException) {
    if (mp_div((mp_int *) m_bi, (mp_int *) a.m_bi, NULL, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator<<=(unsigned int a) throw (GeneralException) {
    if (mp_mul_d((mp_int *) m_bi, 1 << a, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_div");
    return *this;
}

Balau::BigInt & Balau::BigInt::operator>>=(unsigned int a) {
    BigInt s;
    s.set2expt(a);
    return operator/=(s);
}

Balau::BigInt Balau::BigInt::operator-() const throw (GeneralException) {
    return neg();
}

Balau::BigInt & Balau::BigInt::operator++() {
    operator+=(1);
    return *this;
}

Balau::BigInt Balau::BigInt::operator++(int) {
    BigInt r(*this);
    operator+=(1);
    return r;
}

Balau::BigInt & Balau::BigInt::operator--() {
    operator-=(1);
    return *this;
}

Balau::BigInt Balau::BigInt::operator--(int) {
    BigInt r(*this);
    operator-=(1);
    return r;
}

Balau::BigInt Balau::BigInt::neg() const throw (GeneralException) {
    BigInt r;
    if (mp_neg((mp_int *) m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_neg");
    return r;
}

Balau::BigInt & Balau::BigInt::do_neg() throw (GeneralException) {
    if (mp_neg((mp_int *) m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_neg");
    return *this;
}

Balau::BigInt Balau::BigInt::sqrt() const throw (GeneralException) {
    BigInt r;
    if (mp_sqr((mp_int *) m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sqr");
    return r;
}

Balau::BigInt & Balau::BigInt::do_sqrt() throw (GeneralException) {
    if (mp_sqr((mp_int *) m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sqr");
    return *this;
}

Balau::BigInt Balau::BigInt::gcd(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_gcd((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_gcd");
    return r;
}

Balau::BigInt Balau::BigInt::lcm(const BigInt & a) const throw (GeneralException) {
    BigInt r;
    if (mp_lcm((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_lcm");
    return r;
}

Balau::BigInt::comp_t Balau::BigInt::comp(const BigInt & a) const throw (GeneralException) {
    int r = mp_cmp((mp_int *) m_bi, (mp_int *) a.m_bi);
    switch (r) {
    case MP_LT:
        return LT;
    case MP_GT:
        return GT;
    case MP_EQ:
        return EQ;
    default:
        throw GeneralException("Unknown result from mp_cmp");
    }
}

Balau::BigInt::comp_t Balau::BigInt::comp(unsigned int a) const throw (GeneralException) {
    int r = mp_cmp_d((mp_int *) m_bi, a);
    switch (r) {
    case MP_LT:
        return LT;
    case MP_GT:
        return GT;
    case MP_EQ:
        return EQ;
    default:
        throw GeneralException("Unknown result from mp_cmp_d");
    }
}

bool Balau::BigInt::operator==(const BigInt & a) const {
    comp_t r = comp(a);
    return r == EQ;
}

bool Balau::BigInt::operator!=(const BigInt & a) const {
    comp_t r = comp(a);
    return r != EQ;
}

bool Balau::BigInt::operator<=(const BigInt & a) const {
    comp_t r = comp(a);
    return r == LT || r == EQ;
}

bool Balau::BigInt::operator>=(const BigInt & a) const {
    comp_t r = comp(a);
    return r == GT || r == EQ;
}

bool Balau::BigInt::operator<(const BigInt & a) const {
    comp_t r = comp(a);
    return r == LT;
}

bool Balau::BigInt::operator>(const BigInt & a) const {
    comp_t r = comp(a);
    return r == GT;
}

bool Balau::BigInt::operator==(unsigned int a) const {
    comp_t r = comp(a);
    return r == EQ;
}

bool Balau::BigInt::operator!=(unsigned int a) const {
    comp_t r = comp(a);
    return r != EQ;
}

bool Balau::BigInt::operator<=(unsigned int a) const {
    comp_t r = comp(a);
    return r == LT || r == EQ;
}

bool Balau::BigInt::operator>=(unsigned int a) const {
    comp_t r = comp(a);
    return r == GT || r == EQ;
}

bool Balau::BigInt::operator<(unsigned int a) const {
    comp_t r = comp(a);
    return r == LT;
}

bool Balau::BigInt::operator>(unsigned int a) const {
    comp_t r = comp(a);
    return r == GT;
}

Balau::BigInt Balau::BigInt::modadd(const BigInt & a, const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_addmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_addmod");
    return r;
}

Balau::BigInt Balau::BigInt::modsub(const BigInt & a, const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_submod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_submod");
    return r;
}

Balau::BigInt Balau::BigInt::modmul(const BigInt & a, const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_mulmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mulmod");
    return r;
}

Balau::BigInt Balau::BigInt::modsqr(const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_sqrmod((mp_int *) m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sqrmod");
    return r;
}

Balau::BigInt Balau::BigInt::modinv(const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_invmod((mp_int *) m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_invmod");
    return r;
}

Balau::BigInt Balau::BigInt::modpow(const BigInt & a, const BigInt & m) const throw (GeneralException) {
    BigInt r;
    if (mp_exptmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) r.m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_exptmod");
    return r;
}

Balau::BigInt & Balau::BigInt::do_modadd(const BigInt & a, const BigInt & m) throw (GeneralException) {
    if (mp_addmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_addmod");
    return *this;
}

Balau::BigInt & Balau::BigInt::do_modsub(const BigInt & a, const BigInt & m) throw (GeneralException) {
    if (mp_submod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_submod");
    return *this;
}

Balau::BigInt & Balau::BigInt::do_modmul(const BigInt & a, const BigInt & m) throw (GeneralException) {
    if (mp_mulmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_mulmod");
    return *this;
}

Balau::BigInt & Balau::BigInt::do_modsqr(const BigInt & m) throw (GeneralException) {
    if (mp_sqrmod((mp_int *) m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_sqrmod");
    return *this;
}

Balau::BigInt & Balau::BigInt::do_modinv(const BigInt & m) throw (GeneralException) {
    if (mp_invmod((mp_int *) m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_invmod");
    return *this;
}

Balau::BigInt & Balau::BigInt::do_modpow(const BigInt & a, const BigInt & m) throw (GeneralException) {
    if (mp_exptmod((mp_int *) m_bi, (mp_int *) a.m_bi, (mp_int *) m.m_bi, (mp_int *) m_bi) != MP_OKAY)
        throw GeneralException("Error while calling mp_exptmod");
    return *this;
}

bool Balau::BigInt::isPrime() const throw (GeneralException) {
    int r = 0;
    if (mp_prime_is_prime((mp_int *) m_bi, NULL, &r) != MP_OKAY)
        throw GeneralException("Error while calling mp_prime_is_prime");
    return r == MP_YES;
}

size_t Balau::BigInt::exportSize() const {
    return mp_unsigned_bin_size((mp_int *) m_bi) + 1;
}

size_t Balau::BigInt::exportUSize() const {
    return mp_unsigned_bin_size((mp_int *) m_bi);
}

void Balau::BigInt::exportBin(void * _buf) const throw (GeneralException) {
    unsigned char * buf = (unsigned char *) _buf;
    buf[0] = comp(0) == LT ? 0xff : 0;
    if (mp_to_unsigned_bin((mp_int *) m_bi, buf + 1) != MP_OKAY)
        throw GeneralException("Error while calling mp_to_unsigned_bin");
}

void Balau::BigInt::exportUBin(void * _buf) const throw (GeneralException) {
    unsigned char * buf = (unsigned char *)_buf;
    if (mp_to_unsigned_bin((mp_int *) m_bi, buf) != MP_OKAY)
        throw GeneralException("Error while calling mp_to_unsigned_bin");
}

void Balau::BigInt::importBin(const void * _buf, size_t size) throw (GeneralException) {
    unsigned char * buf = (unsigned char *) _buf;
    bool isNeg = buf[0] != 0;
    if (mp_read_unsigned_bin((mp_int *) m_bi, buf + 1, size - 1) != MP_OKAY)
        throw GeneralException("Error while calling mp_read_unsigned_bin");
    if (isNeg)
        do_neg();
}

void Balau::BigInt::importUBin(const void * _buf, size_t size) throw (GeneralException) {
    unsigned char * buf = (unsigned char *)_buf;
    if (mp_read_unsigned_bin((mp_int *) m_bi, buf, size) != MP_OKAY)
        throw GeneralException("Error while calling mp_read_unsigned_bin");
}

Balau::String Balau::BigInt::toString(int radix) const {
    char * out = (char *) alloca(mp_count_bits((mp_int *) m_bi) / (radix >= 10 ? 3 : 1) + 3);
    mp_toradix((mp_int *) m_bi, out, radix);
    return String(out);
}

char * Balau::BigInt::makeString(int radix) const {
    char * out = (char *) malloc(mp_count_bits((mp_int *) m_bi) / (radix >= 10 ? 3 : 1) + 3);
    mp_toradix((mp_int *) m_bi, out, radix);
    return out;
}