Commit 1ed76442decce5d2aeb099b2d664cf8ddc08b04d

Steffen Jaeckel 2019-09-03T08:34:33

Merge pull request #302 from fperrad/20190525_refactor some refactoring

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
diff --git a/bn_mp_addmod.c b/bn_mp_addmod.c
index 2636e2a..1dcfb67 100644
--- a/bn_mp_addmod.c
+++ b/bn_mp_addmod.c
@@ -14,10 +14,11 @@ mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
    }
 
    if ((err = mp_add(a, b, &t)) != MP_OKAY) {
-      mp_clear(&t);
-      return err;
+      goto LBL_ERR;
    }
    err = mp_mod(&t, c, d);
+
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_copy.c b/bn_mp_copy.c
index 141dd0e..e72fcf6 100644
--- a/bn_mp_copy.c
+++ b/bn_mp_copy.c
@@ -7,6 +7,7 @@
 mp_err mp_copy(const mp_int *a, mp_int *b)
 {
    int n;
+   mp_digit *tmpa, *tmpb;
    mp_err err;
 
    /* if dst == src do nothing */
@@ -22,26 +23,22 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
    }
 
    /* zero b and copy the parameters over */
-   {
-      mp_digit *tmpa, *tmpb;
+   /* pointer aliases */
 
-      /* pointer aliases */
+   /* source */
+   tmpa = a->dp;
 
-      /* source */
-      tmpa = a->dp;
+   /* destination */
+   tmpb = b->dp;
 
-      /* destination */
-      tmpb = b->dp;
-
-      /* copy all the digits */
-      for (n = 0; n < a->used; n++) {
-         *tmpb++ = *tmpa++;
-      }
-
-      /* clear high digits */
-      MP_ZERO_DIGITS(tmpb, b->used - n);
+   /* copy all the digits */
+   for (n = 0; n < a->used; n++) {
+      *tmpb++ = *tmpa++;
    }
 
+   /* clear high digits */
+   MP_ZERO_DIGITS(tmpb, b->used - n);
+
    /* copy used count and sign */
    b->used = a->used;
    b->sign = a->sign;
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index 2561e5a..f56ea81 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -7,6 +7,7 @@
 mp_err mp_div_2(const mp_int *a, mp_int *b)
 {
    int     x, oldused;
+   mp_digit r, rr, *tmpa, *tmpb;
    mp_err err;
 
    /* copy */
@@ -18,31 +19,29 @@ mp_err mp_div_2(const mp_int *a, mp_int *b)
 
    oldused = b->used;
    b->used = a->used;
-   {
-      mp_digit r, rr, *tmpa, *tmpb;
 
-      /* source alias */
-      tmpa = a->dp + b->used - 1;
+   /* source alias */
+   tmpa = a->dp + b->used - 1;
 
-      /* dest alias */
-      tmpb = b->dp + b->used - 1;
+   /* dest alias */
+   tmpb = b->dp + b->used - 1;
 
-      /* carry */
-      r = 0;
-      for (x = b->used - 1; x >= 0; x--) {
-         /* get the carry for the next iteration */
-         rr = *tmpa & 1u;
+   /* carry */
+   r = 0;
+   for (x = b->used - 1; x >= 0; x--) {
+      /* get the carry for the next iteration */
+      rr = *tmpa & 1u;
 
-         /* shift the current digit, add in carry and store */
-         *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
+      /* shift the current digit, add in carry and store */
+      *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
 
-         /* forward carry to next iteration */
-         r = rr;
-      }
-
-      /* zero excess digits */
-      MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
+      /* forward carry to next iteration */
+      r = rr;
    }
+
+   /* zero excess digits */
+   MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
+
    b->sign = a->sign;
    mp_clamp(b);
    return MP_OKAY;
diff --git a/bn_mp_export.c b/bn_mp_export.c
index c9de48b..9dea54e 100644
--- a/bn_mp_export.c
+++ b/bn_mp_export.c
@@ -53,19 +53,19 @@ mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
          *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
 
          if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
-            mp_clear(&t);
-            return err;
+            goto LBL_ERR;
          }
       }
    }
 
-   mp_clear(&t);
-
    if (countp != NULL) {
       *countp = count;
    }
+   err = MP_OKAY;
 
-   return MP_OKAY;
+LBL_ERR:
+   mp_clear(&t);
+   return err;
 }
 
 #endif
diff --git a/bn_mp_expt_u32.c b/bn_mp_expt_u32.c
index 4ec725e..2ab67ba 100644
--- a/bn_mp_expt_u32.c
+++ b/bn_mp_expt_u32.c
@@ -21,16 +21,14 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
       /* if the bit is set multiply */
       if ((b & 1u) != 0u) {
          if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
-            mp_clear(&g);
-            return err;
+            goto LBL_ERR;
          }
       }
 
       /* square */
       if (b > 1u) {
          if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
-            mp_clear(&g);
-            return err;
+            goto LBL_ERR;
          }
       }
 
@@ -38,8 +36,11 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
       b >>= 1;
    }
 
+   err = MP_OKAY;
+
+LBL_ERR:
    mp_clear(&g);
-   return MP_OKAY;
+   return err;
 }
 
 #endif
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index f6846cc..bc4b643 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -26,27 +26,23 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
          return MP_VAL;
       }
 
-      /* first compute 1/G mod P */
-      if ((err = mp_init(&tmpG)) != MP_OKAY) {
+      if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) {
          return err;
       }
+
+      /* first compute 1/G mod P */
       if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
-         mp_clear(&tmpG);
-         return err;
+         goto LBL_ERR;
       }
 
       /* now get |X| */
-      if ((err = mp_init(&tmpX)) != MP_OKAY) {
-         mp_clear(&tmpG);
-         return err;
-      }
       if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
-         mp_clear_multi(&tmpG, &tmpX, NULL);
-         return err;
+         goto LBL_ERR;
       }
 
       /* and now compute (1/G)**|X| instead of G**X [X < 0] */
       err = mp_exptmod(&tmpG, &tmpX, P, Y);
+LBL_ERR:
       mp_clear_multi(&tmpG, &tmpX, NULL);
       return err;
    }
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index 5b4719f..eaa5d06 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -20,17 +20,18 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
    }
 
    if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) {
-      MP_FREE_BUFFER(buf, (size_t)len);
-      return err;
+      goto LBL_ERR;
    }
 
    if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) {
-      MP_FREE_BUFFER(buf, (size_t)len);
-      return MP_ERR;
+      err = MP_ERR;
+      goto LBL_ERR;
    }
+   err = MP_OKAY;
 
+LBL_ERR:
    MP_FREE_BUFFER(buf, (size_t)len);
-   return MP_OKAY;
+   return err;
 }
 #endif
 
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index 18b4e6b..8fbfe08 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -14,8 +14,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) {
-      mp_clear(&t);
-      return err;
+      goto LBL_ERR;
    }
 
    if (MP_IS_ZERO(&t) || (t.sign == b->sign)) {
@@ -25,6 +24,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
       err = mp_add(b, &t, c);
    }
 
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_mulmod.c b/bn_mp_mulmod.c
index 763f923..160d162 100644
--- a/bn_mp_mulmod.c
+++ b/bn_mp_mulmod.c
@@ -14,10 +14,11 @@ mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
    }
 
    if ((err = mp_mul(a, b, &t)) != MP_OKAY) {
-      mp_clear(&t);
-      return err;
+      goto LBL_ERR;
    }
    err = mp_mod(&t, c, d);
+
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_radix_size.c b/bn_mp_radix_size.c
index 6d7f912..339b21b 100644
--- a/bn_mp_radix_size.c
+++ b/bn_mp_radix_size.c
@@ -48,16 +48,18 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
    /* fetch out all of the digits */
    while (!MP_IS_ZERO(&t)) {
       if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
-         mp_clear(&t);
-         return err;
+         goto LBL_ERR;
       }
       ++digs;
    }
-   mp_clear(&t);
 
    /* return digs + 1, the 1 is for the NULL byte that would be required. */
    *size = digs + 1;
-   return MP_OKAY;
+   err = MP_OKAY;
+
+LBL_ERR:
+   mp_clear(&t);
+   return err;
 }
 
 #endif
diff --git a/bn_mp_sqrmod.c b/bn_mp_sqrmod.c
index b3c44e5..626ea2c 100644
--- a/bn_mp_sqrmod.c
+++ b/bn_mp_sqrmod.c
@@ -14,10 +14,11 @@ mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    if ((err = mp_sqr(a, &t)) != MP_OKAY) {
-      mp_clear(&t);
-      return err;
+      goto LBL_ERR;
    }
    err = mp_mod(&t, b, c);
+
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index f803760..a833ed7 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -61,7 +61,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* find a Z such that the Legendre symbol (Z|prime) == -1 */
    mp_set_u32(&Z, 2u);
    /* Z = 2 */
-   while (1) {
+   for (;;) {
       if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
       if (legendre == -1) break;
       if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY)               goto cleanup;
@@ -81,10 +81,10 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* M = S */
    mp_set_u32(&two, 2u);
 
-   while (1) {
+   for (;;) {
       if ((err = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup;
       i = 0;
-      while (1) {
+      for (;;) {
          if (mp_cmp_d(&t1, 1uL) == MP_EQ) break;
          if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
          i++;
diff --git a/bn_mp_submod.c b/bn_mp_submod.c
index 61c2d0f..5ebd374 100644
--- a/bn_mp_submod.c
+++ b/bn_mp_submod.c
@@ -14,10 +14,11 @@ mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
    }
 
    if ((err = mp_sub(a, b, &t)) != MP_OKAY) {
-      mp_clear(&t);
-      return err;
+      goto LBL_ERR;
    }
    err = mp_mod(&t, c, d);
+
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_to_radix.c b/bn_mp_to_radix.c
index c75ee5b..d6e8fcc 100644
--- a/bn_mp_to_radix.c
+++ b/bn_mp_to_radix.c
@@ -52,8 +52,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
          break;
       }
       if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
-         mp_clear(&t);
-         return err;
+         goto LBL_ERR;
       }
       *str++ = mp_s_rmap[d];
       ++digs;
@@ -67,6 +66,9 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
    /* append a NULL so the string is properly terminated */
    *str = '\0';
 
+   err = MP_OKAY;
+
+LBL_ERR:
    mp_clear(&t);
    return err;
 }
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 093b01a..bc6372c 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -22,12 +22,14 @@ mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
       b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
 #endif
       if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
-         mp_clear(&t);
-         return err;
+         goto LBL_ERR;
       }
    }
    s_mp_reverse(b, x);
+   err = MP_OKAY;
+
+LBL_ERR:
    mp_clear(&t);
-   return MP_OKAY;
+   return err;
 }
 #endif
diff --git a/bn_s_mp_toom_mul.c b/bn_s_mp_toom_mul.c
index ce47f25..e68eff1 100644
--- a/bn_s_mp_toom_mul.c
+++ b/bn_s_mp_toom_mul.c
@@ -44,7 +44,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
 
    /** a = a2 * x^2 + a1 * x + a0; */
    if ((err = mp_init_size(&a0, B)) != MP_OKAY) {
-      goto LTM_ERRa0;
+      goto LBL_ERRa0;
    }
    for (count = 0; count < B; count++) {
       a0.dp[count] = a->dp[count];
@@ -52,7 +52,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    }
    mp_clamp(&a0);
    if ((err = mp_init_size(&a1, B)) != MP_OKAY) {
-      goto LTM_ERRa1;
+      goto LBL_ERRa1;
    }
    for (; count < (2 * B); count++) {
       a1.dp[count - B] = a->dp[count];
@@ -60,7 +60,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    }
    mp_clamp(&a1);
    if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) {
-      goto LTM_ERRa2;
+      goto LBL_ERRa2;
    }
    for (; count < a->used; count++) {
       a2.dp[count - (2 * B)] = a->dp[count];
@@ -69,7 +69,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
 
    /** b = b2 * x^2 + b1 * x + b0; */
    if ((err = mp_init_size(&b0, B)) != MP_OKAY) {
-      goto LTM_ERRb0;
+      goto LBL_ERRb0;
    }
    for (count = 0; count < B; count++) {
       b0.dp[count] = b->dp[count];
@@ -77,7 +77,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    }
    mp_clamp(&b0);
    if ((err = mp_init_size(&b1, B)) != MP_OKAY) {
-      goto LTM_ERRb1;
+      goto LBL_ERRb1;
    }
    for (; count < (2 * B); count++) {
       b1.dp[count - B] = b->dp[count];
@@ -85,7 +85,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    }
    mp_clamp(&b1);
    if ((err = mp_init_size(&b2, B + (b->used - (3 * B)))) != MP_OKAY) {
-      goto LTM_ERRb2;
+      goto LBL_ERRb2;
    }
    for (; count < b->used; count++) {
       b2.dp[count - (2 * B)] = b->dp[count];
@@ -95,174 +95,174 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
    /** \\ S1 = (a2+a1+a0) * (b2+b1+b0); */
    /** T1 = a2 + a1; */
    if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = T1 + a0; */
    if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** c = b2 + b1; */
    if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S1 = c + b0; */
    if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S1 = S1 * S2; */
    if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S2 = (4*a2+2*a1+a0) * (4*b2+2*b1+b0); */
    /** T1 = T1 + a2; */
    if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** T1 = T1 << 1; */
    if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** T1 = T1 + a0; */
    if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** c = c + b2; */
    if ((err = mp_add(c, &b2, c)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** c = c << 1; */
    if ((err = mp_mul_2(c, c)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** c = c + b0; */
    if ((err = mp_add(c, &b0, c)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = T1 * c; */
    if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S3 = (a2-a1+a0) * (b2-b1+b0); */
    /** a1 = a2 - a1; */
    if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a1 = a1 + a0; */
    if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** b1 = b2 - b1; */
    if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** b1 = b1 + b0; */
    if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a1 = a1 * b1; */
    if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** b1 = a2 * b2; */
    if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S2 = (S2 - S3)/3; */
    /** S2 = S2 - a1; */
    if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = S2 / 3; \\ this is an exact division  */
    if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a1 = S1 - a1; */
    if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a1 = a1 >> 1; */
    if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a0 = a0 * b0; */
    if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S1 = S1 - a0; */
    if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = S2 - S1; */
    if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = S2 >> 1; */
    if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S1 = S1 - a1; */
    if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S1 = S1 - b1; */
    if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** T1 = b1 << 1; */
    if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** S2 = S2 - T1; */
    if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a1 = a1 - S2; */
    if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** P = b1*x^4+ S2*x^3+ S1*x^2+ a1*x + a0; */
    if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** a * b - P */
 
 
-LTM_ERR:
+LBL_ERR:
    mp_clear(&b2);
-LTM_ERRb2:
+LBL_ERRb2:
    mp_clear(&b1);
-LTM_ERRb1:
+LBL_ERRb1:
    mp_clear(&b0);
-LTM_ERRb0:
+LBL_ERRb0:
    mp_clear(&a2);
-LTM_ERRa2:
+LBL_ERRa2:
    mp_clear(&a1);
-LTM_ERRa1:
+LBL_ERRa1:
    mp_clear(&a0);
-LTM_ERRa0:
+LBL_ERRa0:
    mp_clear_multi(&S1, &S2, &T1, NULL);
    return err;
 }
diff --git a/bn_s_mp_toom_sqr.c b/bn_s_mp_toom_sqr.c
index a6bf85e..5cd0de3 100644
--- a/bn_s_mp_toom_sqr.c
+++ b/bn_s_mp_toom_sqr.c
@@ -35,15 +35,15 @@ mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b)
 
    /** a = a2 * x^2 + a1 * x + a0; */
    if ((err = mp_init_size(&a0, B)) != MP_OKAY) {
-      goto LTM_ERRa0;
+      goto LBL_ERRa0;
    }
    a0.used = B;
    if ((err = mp_init_size(&a1, B)) != MP_OKAY) {
-      goto LTM_ERRa1;
+      goto LBL_ERRa1;
    }
    a1.used = B;
    if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) {
-      goto LTM_ERRa2;
+      goto LBL_ERRa2;
    }
    tmpa = a->dp;
    tmpc = a0.dp;
@@ -64,88 +64,88 @@ mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b)
 
    /** S0 = a0^2;  */
    if ((err = mp_sqr(&a0, &S0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S1 = (a2 + a1 + a0)^2 */
    /** \\S2 = (a2 - a1 + a0)^2  */
    /** \\S1 = a0 + a2; */
    /** a0 = a0 + a2; */
    if ((err = mp_add(&a0, &a2, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S2 = S1 - a1; */
    /** b = a0 - a1; */
    if ((err = mp_sub(&a0, &a1, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S1 = S1 + a1; */
    /** a0 = a0 + a1; */
    if ((err = mp_add(&a0, &a1, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S1 = S1^2;  */
    /** a0 = a0^2; */
    if ((err = mp_sqr(&a0, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S2 = S2^2;  */
    /** b = b^2; */
    if ((err = mp_sqr(b, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** \\ S3 = 2 * a1 * a2  */
    /** \\S3 = a1 * a2;  */
    /** a1 = a1 * a2; */
    if ((err = mp_mul(&a1, &a2, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S3 = S3 << 1;  */
    /** a1 = a1 << 1; */
    if ((err = mp_mul_2(&a1, &a1)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** \\S4 = a2^2;  */
    /** a2 = a2^2; */
    if ((err = mp_sqr(&a2, &a2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** \\ tmp = (S1 + S2)/2  */
    /** \\tmp = S1 + S2; */
    /** b = a0 + b; */
    if ((err = mp_add(&a0, b, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\tmp = tmp >> 1; */
    /** b = b >> 1; */
    if ((err = mp_div_2(b, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** \\ S1 = S1 - tmp - S3  */
    /** \\S1 = S1 - tmp; */
    /** a0 = a0 - b; */
    if ((err = mp_sub(&a0, b, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S1 = S1 - S3;  */
    /** a0 = a0 - a1; */
    if ((err = mp_sub(&a0, &a1, &a0)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
    /** \\S2 = tmp - S4 -S0  */
    /** \\S2 = tmp - S4;  */
    /** b = b - a2; */
    if ((err = mp_sub(b, &a2, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** \\S2 = S2 - S0;  */
    /** b = b - S0; */
    if ((err = mp_sub(b, &S0, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
 
 
@@ -153,39 +153,39 @@ mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b)
    /** P = a2*x^4 + a1*x^3 + b*x^2 + a0*x + S0; */
 
    if ((err = mp_lshd(&a2, 4 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(&a1, 3 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(b, 2 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_lshd(&a0, 1 * B)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&a2, &a1, &a2)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(&a2, b, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(b, &a0, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    if ((err = mp_add(b, &S0, b)) != MP_OKAY) {
-      goto LTM_ERR;
+      goto LBL_ERR;
    }
    /** a^2 - P  */
 
 
-LTM_ERR:
+LBL_ERR:
    mp_clear(&a2);
-LTM_ERRa2:
+LBL_ERRa2:
    mp_clear(&a1);
-LTM_ERRa1:
+LBL_ERRa1:
    mp_clear(&a0);
-LTM_ERRa0:
+LBL_ERRa0:
    mp_clear(&S0);
 
    return err;
diff --git a/tommath_class.h b/tommath_class.h
index 1bd0c0b..24122d1 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -405,10 +405,9 @@
 
 #if defined(BN_MP_EXPTMOD_C)
 #   define BN_MP_ABS_C
-#   define BN_MP_CLEAR_C
 #   define BN_MP_CLEAR_MULTI_C
 #   define BN_MP_DR_IS_MODULUS_C
-#   define BN_MP_INIT_C
+#   define BN_MP_INIT_MULTI_C
 #   define BN_MP_INVMOD_C
 #   define BN_MP_REDUCE_IS_2K_C
 #   define BN_MP_REDUCE_IS_2K_L_C