Commit c4fb2241dc752ae542008395b043036ce0b5e183

Daniel Mendler 2019-04-09T11:08:26

rename macros in tommath_private to use MP_* prefix

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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index 78f09c3..0552709 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -15,7 +15,7 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
    int     res, neg;
 
    /* 2. [modified] b must be odd   */
-   if (IS_EVEN(b)) {
+   if (MP_IS_EVEN(b)) {
       return MP_VAL;
    }
 
@@ -35,7 +35,7 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    /* if one of x,y is zero return an error! */
-   if (IS_ZERO(&x) || IS_ZERO(&y)) {
+   if (MP_IS_ZERO(&x) || MP_IS_ZERO(&y)) {
       res = MP_VAL;
       goto LBL_ERR;
    }
@@ -51,13 +51,13 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
 
 top:
    /* 4.  while u is even do */
-   while (IS_EVEN(&u)) {
+   while (MP_IS_EVEN(&u)) {
       /* 4.1 u = u/2 */
       if ((res = mp_div_2(&u, &u)) != MP_OKAY) {
          goto LBL_ERR;
       }
       /* 4.2 if B is odd then */
-      if (IS_ODD(&B)) {
+      if (MP_IS_ODD(&B)) {
          if ((res = mp_sub(&B, &x, &B)) != MP_OKAY) {
             goto LBL_ERR;
          }
@@ -69,13 +69,13 @@ top:
    }
 
    /* 5.  while v is even do */
-   while (IS_EVEN(&v)) {
+   while (MP_IS_EVEN(&v)) {
       /* 5.1 v = v/2 */
       if ((res = mp_div_2(&v, &v)) != MP_OKAY) {
          goto LBL_ERR;
       }
       /* 5.2 if D is odd then */
-      if (IS_ODD(&D)) {
+      if (MP_IS_ODD(&D)) {
          /* D = (D-x)/2 */
          if ((res = mp_sub(&D, &x, &D)) != MP_OKAY) {
             goto LBL_ERR;
@@ -109,7 +109,7 @@ top:
    }
 
    /* if not zero goto step 4 */
-   if (!IS_ZERO(&u)) {
+   if (!MP_IS_ZERO(&u)) {
       goto top;
    }
 
diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c
index d2f1735..4e3dd29 100644
--- a/bn_fast_s_mp_mul_digs.c
+++ b/bn_fast_s_mp_mul_digs.c
@@ -33,7 +33,7 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
    }
 
    /* number of output digits to produce */
-   pa = MIN(digs, a->used + b->used);
+   pa = MP_MIN(digs, a->used + b->used);
 
    /* clear the carry */
    _W = 0;
@@ -43,7 +43,7 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
       mp_digit *tmpx, *tmpy;
 
       /* get offsets into the two bignums */
-      ty = MIN(b->used-1, ix);
+      ty = MP_MIN(b->used-1, ix);
       tx = ix - ty;
 
       /* setup temp aliases */
@@ -53,7 +53,7 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
       /* this is the number of times the loop will iterrate, essentially
          while (tx++ < a->used && ty-- >= 0) { ... }
        */
-      iy = MIN(a->used-tx, ty+1);
+      iy = MP_MIN(a->used-tx, ty+1);
 
       /* execute loop */
       for (iz = 0; iz < iy; ++iz) {
diff --git a/bn_fast_s_mp_mul_high_digs.c b/bn_fast_s_mp_mul_high_digs.c
index 8fb541e..12846b8 100644
--- a/bn_fast_s_mp_mul_high_digs.c
+++ b/bn_fast_s_mp_mul_high_digs.c
@@ -34,7 +34,7 @@ int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int dig
       mp_digit *tmpx, *tmpy;
 
       /* get offsets into the two bignums */
-      ty = MIN(b->used-1, ix);
+      ty = MP_MIN(b->used-1, ix);
       tx = ix - ty;
 
       /* setup temp aliases */
@@ -44,7 +44,7 @@ int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int dig
       /* this is the number of times the loop will iterrate, essentially its
          while (tx++ < a->used && ty-- >= 0) { ... }
        */
-      iy = MIN(a->used-tx, ty+1);
+      iy = MP_MIN(a->used-tx, ty+1);
 
       /* execute loop */
       for (iz = 0; iz < iy; iz++) {
diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c
index 3394d88..c07bcc9 100644
--- a/bn_fast_s_mp_sqr.c
+++ b/bn_fast_s_mp_sqr.c
@@ -38,7 +38,7 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b)
       _W = 0;
 
       /* get offsets into the two bignums */
-      ty = MIN(a->used-1, ix);
+      ty = MP_MIN(a->used-1, ix);
       tx = ix - ty;
 
       /* setup temp aliases */
@@ -48,13 +48,13 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b)
       /* this is the number of times the loop will iterrate, essentially
          while (tx++ < a->used && ty-- >= 0) { ... }
        */
-      iy = MIN(a->used-tx, ty+1);
+      iy = MP_MIN(a->used-tx, ty+1);
 
       /* now for squaring tx can never equal ty
        * we halve the distance since they approach at a rate of 2x
        * and we have to round because odd cases need to be executed
        */
-      iy = MIN(iy, ((ty-tx)+1)>>1);
+      iy = MP_MIN(iy, ((ty-tx)+1)>>1);
 
       /* execute loop */
       for (iz = 0; iz < iy; iz++) {
diff --git a/bn_mp_balance_mul.c b/bn_mp_balance_mul.c
index 78c5a82..c5d4ea5 100644
--- a/bn_mp_balance_mul.c
+++ b/bn_mp_balance_mul.c
@@ -12,8 +12,8 @@ int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
    len_a = a->used;
    len_b = b->used;
 
-   nblocks = MAX(a->used, b->used) / MIN(a->used, b->used);
-   bsize = MIN(a->used, b->used) ;
+   nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used);
+   bsize = MP_MIN(a->used, b->used) ;
 
    if ((e = mp_init_size(&a0, bsize + 2)) != MP_OKAY) {
       return e;
diff --git a/bn_mp_clear.c b/bn_mp_clear.c
index 3180852..cf52d15 100644
--- a/bn_mp_clear.c
+++ b/bn_mp_clear.c
@@ -16,7 +16,7 @@ void mp_clear(mp_int *a)
       }
 
       /* free ram */
-      XFREE(a->dp, sizeof(mp_digit) * (size_t)a->alloc);
+      MP_FREE(a->dp, sizeof(mp_digit) * (size_t)a->alloc);
 
       /* reset members to make debugging easier */
       a->dp    = NULL;
diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c
index 9806232..ceae48a 100644
--- a/bn_mp_cnt_lsb.c
+++ b/bn_mp_cnt_lsb.c
@@ -14,7 +14,7 @@ int mp_cnt_lsb(const mp_int *a)
    mp_digit q, qq;
 
    /* easy out */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return 0;
    }
 
diff --git a/bn_mp_count_bits.c b/bn_mp_count_bits.c
index 3b532d9..44e7ffe 100644
--- a/bn_mp_count_bits.c
+++ b/bn_mp_count_bits.c
@@ -10,7 +10,7 @@ int mp_count_bits(const mp_int *a)
    mp_digit q;
 
    /* shortcut */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return 0;
    }
 
diff --git a/bn_mp_decr.c b/bn_mp_decr.c
index e605f2c..7bd248d 100644
--- a/bn_mp_decr.c
+++ b/bn_mp_decr.c
@@ -7,7 +7,7 @@
 int mp_decr(mp_int *a)
 {
    int e = MP_OKAY;
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       mp_set(a,1uL);
       a->sign = MP_NEG;
       return MP_OKAY;
@@ -17,7 +17,7 @@ int mp_decr(mp_int *a)
          return e;
       }
       /* There is no -0 in LTM */
-      if (!IS_ZERO(a)) {
+      if (!MP_IS_ZERO(a)) {
          a->sign = MP_NEG;
       }
       return MP_OKAY;
diff --git a/bn_mp_div.c b/bn_mp_div.c
index 4f03144..f36456d 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -12,7 +12,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
    int    res, n, n2;
 
    /* is divisor zero ? */
-   if (IS_ZERO(b)) {
+   if (MP_IS_ZERO(b)) {
       return MP_VAL;
    }
 
@@ -62,11 +62,11 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
    n2 = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
    if (c != NULL) {
       mp_exch(c, &q);
-      c->sign  = IS_ZERO(c) ? MP_ZPOS : n2;
+      c->sign  = MP_IS_ZERO(c) ? MP_ZPOS : n2;
    }
    if (d != NULL) {
       mp_exch(d, &ta);
-      d->sign = IS_ZERO(d) ? MP_ZPOS : n;
+      d->sign = MP_IS_ZERO(d) ? MP_ZPOS : n;
    }
 LBL_ERR:
    mp_clear_multi(&ta, &tb, &tq, &q, NULL);
@@ -94,7 +94,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
    int     res, n, t, i, norm, neg;
 
    /* is divisor zero ? */
-   if (IS_ZERO(b)) {
+   if (MP_IS_ZERO(b)) {
       return MP_VAL;
    }
 
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index 7fff466..e7e5870 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -35,7 +35,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
    }
 
    /* quick outs */
-   if ((b == 1u) || IS_ZERO(a)) {
+   if ((b == 1u) || MP_IS_ZERO(a)) {
       if (d != NULL) {
          *d = 0;
       }
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index 63c3b56..e531e4b 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -76,7 +76,7 @@ int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
 
    /* if the modulus is odd or dr != 0 use the montgomery method */
 #ifdef BN_MP_EXPTMOD_FAST_C
-   if (IS_ODD(P) || (dr !=  0)) {
+   if (MP_IS_ODD(P) || (dr !=  0)) {
       return mp_exptmod_fast(G, X, P, Y, dr);
    } else {
 #endif
diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c
index 6d281d4..baeeede 100644
--- a/bn_mp_exteuclid.c
+++ b/bn_mp_exteuclid.c
@@ -28,7 +28,7 @@ int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_in
    }
 
    /* loop while v3 != 0 */
-   while (!IS_ZERO(&v3)) {
+   while (!MP_IS_ZERO(&v3)) {
       /* q = u3/v3 */
       if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) {
          goto LBL_ERR;
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index c4b9dce..540b734 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -13,24 +13,24 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
       return err;
    }
 
-   buf = (char *) XMALLOC((size_t)len);
+   buf = (char *) MP_MALLOC((size_t)len);
    if (buf == NULL) {
       return MP_MEM;
    }
 
    if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
-      XFREE(buf, len);
+      MP_FREE(buf, len);
       return err;
    }
 
    for (x = 0; x < len; x++) {
       if (fputc((int)buf[x], stream) == EOF) {
-         XFREE(buf, len);
+         MP_FREE(buf, len);
          return MP_VAL;
       }
    }
 
-   XFREE(buf, len);
+   MP_FREE(buf, len);
    return MP_OKAY;
 }
 #endif
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index b91ba31..51cb195 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -10,10 +10,10 @@ int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
    int     k, u_lsb, v_lsb, res;
 
    /* either zero than gcd is the largest */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return mp_abs(b, c);
    }
-   if (IS_ZERO(b)) {
+   if (MP_IS_ZERO(b)) {
       return mp_abs(a, c);
    }
 
@@ -32,7 +32,7 @@ int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
    /* B1.  Find the common power of two for u and v */
    u_lsb = mp_cnt_lsb(&u);
    v_lsb = mp_cnt_lsb(&v);
-   k     = MIN(u_lsb, v_lsb);
+   k     = MP_MIN(u_lsb, v_lsb);
 
    if (k > 0) {
       /* divide the power of two out */
@@ -58,7 +58,7 @@ int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
       }
    }
 
-   while (!IS_ZERO(&v)) {
+   while (!MP_IS_ZERO(&v)) {
       /* make sure v is the largest */
       if (mp_cmp_mag(&u, &v) == MP_GT) {
          /* swap u and v to make sure v is >= u */
diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index 86aa666..6ca1fbd 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -9,12 +9,12 @@ unsigned long mp_get_long(const mp_int *a)
    int i;
    unsigned long res;
 
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return 0;
    }
 
    /* get number of digits of the lsb we have to read */
-   i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
+   i = MP_MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
 
    /* get most significant digit of result */
    res = (unsigned long)a->dp[i];
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
index c694560..3fa2021 100644
--- a/bn_mp_get_long_long.c
+++ b/bn_mp_get_long_long.c
@@ -9,12 +9,12 @@ unsigned long long mp_get_long_long(const mp_int *a)
    int i;
    unsigned long long res;
 
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return 0;
    }
 
    /* get number of digits of the lsb we have to read */
-   i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
+   i = MP_MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
 
    /* get most significant digit of result */
    res = (unsigned long long)a->dp[i];
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 094b9e5..8011923 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -20,9 +20,9 @@ int mp_grow(mp_int *a, int size)
        * in case the operation failed we don't want
        * to overwrite the dp member of a.
        */
-      tmp = (mp_digit *) XREALLOC(a->dp,
-                                  (size_t)a->alloc * sizeof(mp_digit),
-                                  (size_t)size * sizeof(mp_digit));
+      tmp = (mp_digit *) MP_REALLOC(a->dp,
+                                    (size_t)a->alloc * sizeof(mp_digit),
+                                    (size_t)size * sizeof(mp_digit));
       if (tmp == NULL) {
          /* reallocation failed but "a" is still valid [can be freed] */
          return MP_MEM;
diff --git a/bn_mp_incr.c b/bn_mp_incr.c
index 052caef..15a9ef5 100644
--- a/bn_mp_incr.c
+++ b/bn_mp_incr.c
@@ -7,7 +7,7 @@
 int mp_incr(mp_int *a)
 {
    int e = MP_OKAY;
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       mp_set(a,1uL);
       return MP_OKAY;
    } else if (a->sign == MP_NEG) {
@@ -16,7 +16,7 @@ int mp_incr(mp_int *a)
          return e;
       }
       /* There is no -0 in LTM */
-      if (!IS_ZERO(a)) {
+      if (!MP_IS_ZERO(a)) {
          a->sign = MP_NEG;
       }
       return MP_OKAY;
diff --git a/bn_mp_init.c b/bn_mp_init.c
index c0b311d..99c74aa 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -7,7 +7,7 @@
 int mp_init(mp_int *a)
 {
    /* allocate memory required and clear it */
-   a->dp = (mp_digit *) XCALLOC((size_t)MP_PREC, sizeof(mp_digit));
+   a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit));
    if (a->dp == NULL) {
       return MP_MEM;
    }
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index bb086f0..35136f5 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -10,7 +10,7 @@ int mp_init_size(mp_int *a, int size)
    size += (MP_PREC * 2) - (size % MP_PREC);
 
    /* alloc mem */
-   a->dp = (mp_digit *) XCALLOC((size_t)size, sizeof(mp_digit));
+   a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
    if (a->dp == NULL) {
       return MP_MEM;
    }
diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c
index f4dc65d..1b26a57 100644
--- a/bn_mp_invmod.c
+++ b/bn_mp_invmod.c
@@ -13,7 +13,7 @@ int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
 
 #ifdef BN_FAST_MP_INVMOD_C
    /* if the modulus is odd we can use a faster routine instead */
-   if (IS_ODD(b)) {
+   if (MP_IS_ODD(b)) {
       return fast_mp_invmod(a, b, c);
    }
 #endif
diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c
index 661f67b..9f7f665 100644
--- a/bn_mp_invmod_slow.c
+++ b/bn_mp_invmod_slow.c
@@ -10,7 +10,7 @@ int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
    int     res;
 
    /* b cannot be negative */
-   if ((b->sign == MP_NEG) || IS_ZERO(b)) {
+   if ((b->sign == MP_NEG) || MP_IS_ZERO(b)) {
       return MP_VAL;
    }
 
@@ -29,7 +29,7 @@ int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    /* 2. [modified] if x,y are both even then return an error! */
-   if (IS_EVEN(&x) && IS_EVEN(&y)) {
+   if (MP_IS_EVEN(&x) && MP_IS_EVEN(&y)) {
       res = MP_VAL;
       goto LBL_ERR;
    }
@@ -46,13 +46,13 @@ int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
 
 top:
    /* 4.  while u is even do */
-   while (IS_EVEN(&u)) {
+   while (MP_IS_EVEN(&u)) {
       /* 4.1 u = u/2 */
       if ((res = mp_div_2(&u, &u)) != MP_OKAY) {
          goto LBL_ERR;
       }
       /* 4.2 if A or B is odd then */
-      if (IS_ODD(&A) || IS_ODD(&B)) {
+      if (MP_IS_ODD(&A) || MP_IS_ODD(&B)) {
          /* A = (A+y)/2, B = (B-x)/2 */
          if ((res = mp_add(&A, &y, &A)) != MP_OKAY) {
             goto LBL_ERR;
@@ -71,13 +71,13 @@ top:
    }
 
    /* 5.  while v is even do */
-   while (IS_EVEN(&v)) {
+   while (MP_IS_EVEN(&v)) {
       /* 5.1 v = v/2 */
       if ((res = mp_div_2(&v, &v)) != MP_OKAY) {
          goto LBL_ERR;
       }
       /* 5.2 if C or D is odd then */
-      if (IS_ODD(&C) || IS_ODD(&D)) {
+      if (MP_IS_ODD(&C) || MP_IS_ODD(&D)) {
          /* C = (C+y)/2, D = (D-x)/2 */
          if ((res = mp_add(&C, &y, &C)) != MP_OKAY) {
             goto LBL_ERR;
@@ -125,7 +125,7 @@ top:
    }
 
    /* if not zero goto step 4 */
-   if (!IS_ZERO(&u))
+   if (!MP_IS_ZERO(&u))
       goto top;
 
    /* now a = C, b = D, gcd == g*v */
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index a05f284..efc1157 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -40,7 +40,7 @@ int mp_is_square(const mp_int *arg, int *ret)
       return MP_VAL;
    }
 
-   if (IS_ZERO(arg)) {
+   if (MP_IS_ZERO(arg)) {
       return MP_OKAY;
    }
 
diff --git a/bn_mp_iseven.c b/bn_mp_iseven.c
index d896622..baea2ad 100644
--- a/bn_mp_iseven.c
+++ b/bn_mp_iseven.c
@@ -5,6 +5,6 @@
 
 int mp_iseven(const mp_int *a)
 {
-   return IS_EVEN(a) ? MP_YES : MP_NO;
+   return MP_IS_EVEN(a) ? MP_YES : MP_NO;
 }
 #endif
diff --git a/bn_mp_isodd.c b/bn_mp_isodd.c
index 822d451..1f11e2f 100644
--- a/bn_mp_isodd.c
+++ b/bn_mp_isodd.c
@@ -5,6 +5,6 @@
 
 int mp_isodd(const mp_int *a)
 {
-   return IS_ODD(a) ? MP_YES : MP_NO;
+   return MP_IS_ODD(a) ? MP_YES : MP_NO;
 }
 #endif
diff --git a/bn_mp_karatsuba_mul.c b/bn_mp_karatsuba_mul.c
index 3356526..716fdcc 100644
--- a/bn_mp_karatsuba_mul.c
+++ b/bn_mp_karatsuba_mul.c
@@ -41,7 +41,7 @@ int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
    err = MP_MEM;
 
    /* min # of digits */
-   B = MIN(a->used, b->used);
+   B = MP_MIN(a->used, b->used);
 
    /* now divide in two */
    B = B >> 1;
diff --git a/bn_mp_kronecker.c b/bn_mp_kronecker.c
index de31ad6..a40e7c1 100644
--- a/bn_mp_kronecker.c
+++ b/bn_mp_kronecker.c
@@ -26,7 +26,7 @@ int mp_kronecker(const mp_int *a, const mp_int *p, int *c)
 
    static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
 
-   if (IS_ZERO(p)) {
+   if (MP_IS_ZERO(p)) {
       if ((a->used == 1) && (a->dp[0] == 1u)) {
          *c = 1;
          return e;
@@ -36,7 +36,7 @@ int mp_kronecker(const mp_int *a, const mp_int *p, int *c)
       }
    }
 
-   if (IS_EVEN(a) && IS_EVEN(p)) {
+   if (MP_IS_EVEN(a) && MP_IS_EVEN(p)) {
       *c = 0;
       return e;
    }
@@ -71,7 +71,7 @@ int mp_kronecker(const mp_int *a, const mp_int *p, int *c)
    }
 
    for (;;) {
-      if (IS_ZERO(&a1)) {
+      if (MP_IS_ZERO(&a1)) {
          if (mp_cmp_d(&p1, 1uL) == MP_EQ) {
             *c = k;
             goto LBL_KRON;
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index 217324d..7faec30 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -13,7 +13,7 @@ int mp_lshd(mp_int *a, int b)
       return MP_OKAY;
    }
    /* no need to shift 0 around */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       return MP_OKAY;
    }
 
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index e17f4cd..0ef738e 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -18,7 +18,7 @@ int mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
       return res;
    }
 
-   if (IS_ZERO(&t) || (t.sign == b->sign)) {
+   if (MP_IS_ZERO(&t) || (t.sign == b->sign)) {
       res = MP_OKAY;
       mp_exch(&t, c);
    } else {
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index ff85110..c457a9e 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -26,14 +26,14 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
     * Using it to cut the input into slices small enough for fast_s_mp_mul_digs
     * was actually slower on the author's machine, but YMMV.
     */
-   if ((MIN(len_a, len_b) < KARATSUBA_MUL_CUTOFF)
-       || ((MAX(len_a, len_b) / 2) < KARATSUBA_MUL_CUTOFF)) {
+   if ((MP_MIN(len_a, len_b) < KARATSUBA_MUL_CUTOFF)
+       || ((MP_MAX(len_a, len_b) / 2) < KARATSUBA_MUL_CUTOFF)) {
       goto GO_ON;
    }
    /*
     * Not much effect was observed below a ratio of 1:2, but again: YMMV.
     */
-   if ((MAX(len_a, len_b) /  MIN(len_a, len_b)) < 2) {
+   if ((MP_MAX(len_a, len_b) /  MP_MIN(len_a, len_b)) < 2) {
       goto GO_ON;
    }
 
@@ -45,13 +45,13 @@ GO_ON:
 
    /* use Toom-Cook? */
 #ifdef BN_MP_TOOM_MUL_C
-   if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
+   if (MP_MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
       res = mp_toom_mul(a, b, c);
    } else
 #endif
 #ifdef BN_MP_KARATSUBA_MUL_C
       /* use Karatsuba? */
-      if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
+      if (MP_MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
          res = mp_karatsuba_mul(a, b, c);
       } else
 #endif
@@ -66,7 +66,7 @@ GO_ON:
 
 #ifdef BN_FAST_S_MP_MUL_DIGS_C
          if ((digs < (int)MP_WARRAY) &&
-             (MIN(a->used, b->used) <=
+             (MP_MIN(a->used, b->used) <=
               (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
             res = fast_s_mp_mul_digs(a, b, c, digs);
          } else
diff --git a/bn_mp_neg.c b/bn_mp_neg.c
index 8852f25..1c5b9a0 100644
--- a/bn_mp_neg.c
+++ b/bn_mp_neg.c
@@ -13,7 +13,7 @@ int mp_neg(const mp_int *a, mp_int *b)
       }
    }
 
-   if (!IS_ZERO(b)) {
+   if (!MP_IS_ZERO(b)) {
       b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
    } else {
       b->sign = MP_ZPOS;
diff --git a/bn_mp_prime_frobenius_underwood.c b/bn_mp_prime_frobenius_underwood.c
index b7a9ca3..71de5e9 100644
--- a/bn_mp_prime_frobenius_underwood.c
+++ b/bn_mp_prime_frobenius_underwood.c
@@ -171,7 +171,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
    if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
       goto LBL_FU_ERR;
    }
-   if (IS_ZERO(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
+   if (MP_IS_ZERO(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
       *result = MP_YES;
       goto LBL_FU_ERR;
    }
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index 18f7fcf..ff44419 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -42,7 +42,7 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
    }
 
    /* N must be odd */
-   if (IS_EVEN(a)) {
+   if (MP_IS_EVEN(a)) {
       return MP_OKAY;
    }
    /* N is not a perfect square: floor(sqrt(N))^2 != N */
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 0035089..857ae16 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -69,7 +69,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
          };
       }
    } else {
-      if (IS_EVEN(a)) {
+      if (MP_IS_EVEN(a)) {
          /* force odd */
          if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
             return err;
@@ -121,7 +121,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
          goto LBL_ERR;
       }
 
-      /* if didn't pass sieve and step == MAX then skip test */
+      /* if didn't pass sieve and step == MP_MAX then skip test */
       if ((y == 1) && (step >= (((mp_digit)1 << DIGIT_BIT) - kstep))) {
          continue;
       }
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index 284438d..815f0d2 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -37,7 +37,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
    bsize = (size>>3) + ((size&7)?1:0);
 
    /* we need a buffer of bsize bytes */
-   tmp = (unsigned char *) XMALLOC((size_t)bsize);
+   tmp = (unsigned char *) MP_MALLOC((size_t)bsize);
    if (tmp == NULL) {
       return MP_MEM;
    }
@@ -114,7 +114,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
 
    err = MP_OKAY;
 error:
-   XFREE(tmp, bsize);
+   MP_FREE(tmp, bsize);
    return err;
 }
 
diff --git a/bn_mp_prime_strong_lucas_selfridge.c b/bn_mp_prime_strong_lucas_selfridge.c
index d15c2e2..74d8a5b 100644
--- a/bn_mp_prime_strong_lucas_selfridge.c
+++ b/bn_mp_prime_strong_lucas_selfridge.c
@@ -287,7 +287,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
          if ((e = mp_add(&T1z, &T2z, &Uz)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
-         if (IS_ODD(&Uz)) {
+         if (MP_IS_ODD(&Uz)) {
             if ((e = mp_add(&Uz, a, &Uz)) != MP_OKAY) {
                goto LBL_LS_ERR;
             }
@@ -297,7 +297,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
           * Thomas R. Nicely used GMP's mpz_fdiv_q_2exp().
           * But mp_div_2() does not do so, it is truncating instead.
           */
-         oddness = IS_ODD(&Uz) ? MP_YES : MP_NO;
+         oddness = MP_IS_ODD(&Uz) ? MP_YES : MP_NO;
          if ((e = mp_div_2(&Uz, &Uz)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
@@ -309,12 +309,12 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
          if ((e = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
-         if (IS_ODD(&Vz)) {
+         if (MP_IS_ODD(&Vz)) {
             if ((e = mp_add(&Vz, a, &Vz)) != MP_OKAY) {
                goto LBL_LS_ERR;
             }
          }
-         oddness = IS_ODD(&Vz) ? MP_YES : MP_NO;
+         oddness = MP_IS_ODD(&Vz) ? MP_YES : MP_NO;
          if ((e = mp_div_2(&Vz, &Vz)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
@@ -341,7 +341,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
 
    /* If U_d or V_d is congruent to 0 mod N, then N is a prime or a
       strong Lucas pseudoprime. */
-   if (IS_ZERO(&Uz) || IS_ZERO(&Vz)) {
+   if (MP_IS_ZERO(&Uz) || MP_IS_ZERO(&Vz)) {
       *result = MP_YES;
       goto LBL_LS_ERR;
    }
@@ -372,7 +372,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
       if ((e = mp_mod(&Vz, a, &Vz)) != MP_OKAY) {
          goto LBL_LS_ERR;
       }
-      if (IS_ZERO(&Vz)) {
+      if (MP_IS_ZERO(&Vz)) {
          *result = MP_YES;
          goto LBL_LS_ERR;
       }
diff --git a/bn_mp_radix_size.c b/bn_mp_radix_size.c
index c76b356..8895ae2 100644
--- a/bn_mp_radix_size.c
+++ b/bn_mp_radix_size.c
@@ -17,7 +17,7 @@ int mp_radix_size(const mp_int *a, int radix, int *size)
       return MP_VAL;
    }
 
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       *size = 2;
       return MP_OKAY;
    }
@@ -45,7 +45,7 @@ int mp_radix_size(const mp_int *a, int radix, int *size)
    t.sign = MP_ZPOS;
 
    /* fetch out all of the digits */
-   while (!IS_ZERO(&t)) {
+   while (!MP_IS_ZERO(&t)) {
       if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
          mp_clear(&t);
          return res;
diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c
index 77c7370..913c217 100644
--- a/bn_mp_read_radix.c
+++ b/bn_mp_read_radix.c
@@ -69,7 +69,7 @@ int mp_read_radix(mp_int *a, const char *str, int radix)
    }
 
    /* set the sign only if a != 0 */
-   if (!IS_ZERO(a)) {
+   if (!MP_IS_ZERO(a)) {
       a->sign = neg;
    }
    return MP_OKAY;
diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c
index 9b729b2..0ccd340 100644
--- a/bn_mp_set_double.c
+++ b/bn_mp_set_double.c
@@ -32,7 +32,7 @@ int mp_set_double(mp_int *a, double b)
       return res;
    }
 
-   if (((cast.bits >> 63) != 0ULL) && !IS_ZERO(a)) {
+   if (((cast.bits >> 63) != 0ULL) && !MP_IS_ZERO(a)) {
       a->sign = MP_NEG;
    }
 
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index 443ee1a..e7a204b 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -14,9 +14,9 @@ int mp_shrink(mp_int *a)
    }
 
    if (a->alloc != used) {
-      if ((tmp = (mp_digit *) XREALLOC(a->dp,
-                                       (size_t)a->alloc * sizeof(mp_digit),
-                                       (size_t)used * sizeof(mp_digit))) == NULL) {
+      if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
+                                         (size_t)a->alloc * sizeof(mp_digit),
+                                         (size_t)used * sizeof(mp_digit))) == NULL) {
          return MP_MEM;
       }
       a->dp    = tmp;
diff --git a/bn_mp_sqrt.c b/bn_mp_sqrt.c
index 4770a00..ab9141c 100644
--- a/bn_mp_sqrt.c
+++ b/bn_mp_sqrt.c
@@ -15,7 +15,7 @@ int mp_sqrt(const mp_int *arg, mp_int *ret)
    }
 
    /* easy out */
-   if (IS_ZERO(arg)) {
+   if (MP_IS_ZERO(arg)) {
       mp_zero(ret);
       return MP_OKAY;
    }
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index 9d3dad2..a04e585 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -50,7 +50,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* Q = prime - 1 */
    mp_zero(&S);
    /* S = 0 */
-   while (IS_EVEN(&Q)) {
+   while (MP_IS_EVEN(&Q)) {
       if ((res = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto cleanup;
       /* Q = Q / 2 */
       if ((res = mp_add_d(&S, 1uL, &S)) != MP_OKAY)               goto cleanup;
diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c
index e07d16b..9a6f528 100644
--- a/bn_mp_tc_and.c
+++ b/bn_mp_tc_and.c
@@ -13,7 +13,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
       abits = mp_count_bits(a);
       bbits = mp_count_bits(b);
-      bits = MAX(abits, bbits);
+      bits = MP_MAX(abits, bbits);
       res = mp_init_set_int(&_mx, 1uL);
       if (res != MP_OKAY) {
          goto end;
diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c
index 10ea495..93cb520 100644
--- a/bn_mp_tc_or.c
+++ b/bn_mp_tc_or.c
@@ -13,7 +13,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
       abits = mp_count_bits(a);
       bbits = mp_count_bits(b);
-      bits = MAX(abits, bbits);
+      bits = MP_MAX(abits, bbits);
       res = mp_init_set_int(&_mx, 1uL);
       if (res != MP_OKAY) {
          goto end;
diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c
index d14ea2a..ff4189c 100644
--- a/bn_mp_tc_xor.c
+++ b/bn_mp_tc_xor.c
@@ -13,7 +13,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
       abits = mp_count_bits(a);
       bbits = mp_count_bits(b);
-      bits = MAX(abits, bbits);
+      bits = MP_MAX(abits, bbits);
       res = mp_init_set_int(&_mx, 1uL);
       if (res != MP_OKAY) {
          goto end;
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 9f1cac3..7de9466 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -14,7 +14,7 @@ int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
    }
 
    x = 0;
-   while (!IS_ZERO(&t)) {
+   while (!MP_IS_ZERO(&t)) {
 #ifndef MP_8BIT
       b[x++] = (unsigned char)(t.dp[0] & 255u);
 #else
diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c
index 255e8ad..d1de7b5 100644
--- a/bn_mp_toom_mul.c
+++ b/bn_mp_toom_mul.c
@@ -23,7 +23,7 @@ int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    /* B */
-   B = MIN(a->used, b->used) / 3;
+   B = MP_MIN(a->used, b->used) / 3;
 
    /* a = a2 * B**2 + a1 * B + a0 */
    if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
index 882efec..7be0715 100644
--- a/bn_mp_toradix.c
+++ b/bn_mp_toradix.c
@@ -17,7 +17,7 @@ int mp_toradix(const mp_int *a, char *str, int radix)
    }
 
    /* quick out if its zero */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       *str++ = '0';
       *str = '\0';
       return MP_OKAY;
@@ -35,7 +35,7 @@ int mp_toradix(const mp_int *a, char *str, int radix)
    }
 
    digs = 0;
-   while (!IS_ZERO(&t)) {
+   while (!MP_IS_ZERO(&t)) {
       if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
          mp_clear(&t);
          return res;
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
index 4fb42a3..4ebaa0b 100644
--- a/bn_mp_toradix_n.c
+++ b/bn_mp_toradix_n.c
@@ -20,7 +20,7 @@ int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
    }
 
    /* quick out if its zero */
-   if (IS_ZERO(a)) {
+   if (MP_IS_ZERO(a)) {
       *str++ = '0';
       *str = '\0';
       return MP_OKAY;
@@ -44,7 +44,7 @@ int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
    }
 
    digs = 0;
-   while (!IS_ZERO(&t)) {
+   while (!MP_IS_ZERO(&t)) {
       if (--maxlen < 1) {
          /* no more room */
          break;
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index 4089432..d457cc2 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -17,7 +17,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 
    /* can we use the fast multiplier? */
    if ((digs < (int)MP_WARRAY) &&
-       (MIN(a->used, b->used) <
+       (MP_MIN(a->used, b->used) <
         (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
       return fast_s_mp_mul_digs(a, b, c, digs);
    }
@@ -34,7 +34,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
       u = 0;
 
       /* limit ourselves to making digs digits of output */
-      pb = MIN(b->used, digs - ix);
+      pb = MP_MIN(b->used, digs - ix);
 
       /* setup some aliases */
       /* copy of the digit from a used within the nested loop */
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index f5e86e1..928d982 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -17,7 +17,7 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
    /* can we use the fast multiplier? */
 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
    if (((a->used + b->used + 1) < (int)MP_WARRAY)
-       && (MIN(a->used, b->used) < (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
+       && (MP_MIN(a->used, b->used) < (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
       return fast_s_mp_mul_high_digs(a, b, c, digs);
    }
 #endif
diff --git a/tommath_private.h b/tommath_private.h
index 4d6e587..35639a5 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -6,37 +6,32 @@
 
 #include "tommath.h"
 
-#ifndef MIN
-#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-#endif
-
-#ifndef MAX
-#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-#endif
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 /* define heap macros */
-#ifndef XMALLOC
+#ifndef MP_MALLOC
 /* default to libc stuff */
-#   define XMALLOC(size)                   malloc(size)
-#   define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
-#   define XCALLOC(nmemb, size)            calloc(nmemb, size)
-#   define XFREE(mem, size)                free(mem)
+#   define MP_MALLOC(size)                   malloc(size)
+#   define MP_REALLOC(mem, oldsize, newsize) realloc(mem, newsize)
+#   define MP_CALLOC(nmemb, size)            calloc(nmemb, size)
+#   define MP_FREE(mem, size)                free(mem)
 #else
 /* prototypes for our heap functions */
-extern void *XMALLOC(size_t size);
-extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
-extern void *XCALLOC(size_t nmemb, size_t size);
-extern void XFREE(void *mem, size_t size);
+extern void *MP_MALLOC(size_t size);
+extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
+extern void *MP_CALLOC(size_t nmemb, size_t size);
+extern void MP_FREE(void *mem, size_t size);
 #endif
 
+#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
+#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
+
 /* ---> Basic Manipulations <--- */
-#define IS_ZERO(a) ((a)->used == 0)
-#define IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
-#define IS_ODD(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
+#define MP_IS_ZERO(a) ((a)->used == 0)
+#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
+#define MP_IS_ODD(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
 
 /* lowlevel functions, do not call! */
 int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);