Commit 1af0de1f910dabbc203908801d0080d015932f9e

Daniel Mendler 2019-05-20T19:11:25

deprecate mp_tc_(and|or|xor) in favor of mp_(and|or|xor) * same behavior for positive numbers * generalisation for negative numbers, treating them as two complement * improve algorithm, iterate once over the digits, manually perform two complement * simplify mp_add_d, mp_sub_d * functions are safe in case of a==c or b==c * renamed mp_tc_div_2d to mp_signed_rsh (signed right shift)

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
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
diff --git a/bn_deprecated.c b/bn_deprecated.c
index eb1a6e1..90a4064 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -122,4 +122,28 @@ void bn_reverse(unsigned char *s, int len)
    s_mp_reverse(s, len);
 }
 #endif
+#ifdef BN_MP_TC_AND_C
+mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
+{
+   return mp_and(a, b, c);
+}
+#endif
+#ifdef BN_MP_TC_OR_C
+mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
+{
+   return mp_or(a, b, c);
+}
+#endif
+#ifdef BN_MP_TC_XOR_C
+mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
+{
+   return mp_xor(a, b, c);
+}
+#endif
+#ifdef BN_MP_TC_DIV_2D_C
+mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
+{
+   return mp_signed_rsh(a, b, c);
+}
+#endif
 #endif
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index 5c02077..f301575 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -8,7 +8,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
 {
    mp_err     err;
    int ix, oldused;
-   mp_digit *tmpa, *tmpc, mu;
+   mp_digit *tmpa, *tmpc;
 
    /* grow c as required */
    if (c->alloc < (a->used + 1)) {
@@ -46,15 +46,9 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
 
    /* if a is positive */
    if (a->sign == MP_ZPOS) {
-      /* add digit, after this we're propagating
-       * the carry.
-       */
-      *tmpc   = *tmpa++ + b;
-      mu      = *tmpc >> MP_DIGIT_BIT;
-      *tmpc++ &= MP_MASK;
-
-      /* now handle rest of the digits */
-      for (ix = 1; ix < a->used; ix++) {
+      /* add digits, mu is carry */
+      mp_digit mu = b;
+      for (ix = 0; ix < a->used; ix++) {
          *tmpc   = *tmpa++ + mu;
          mu      = *tmpc >> MP_DIGIT_BIT;
          *tmpc++ &= MP_MASK;
diff --git a/bn_mp_and.c b/bn_mp_and.c
index c6c1efe..1ee14e8 100644
--- a/bn_mp_and.c
+++ b/bn_mp_and.c
@@ -3,38 +3,54 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* AND two ints together */
+/* two complement and */
 mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int ix, px;
+   int used = MP_MAX(a->used, b->used) + 1, i;
    mp_err err;
-   mp_int  t;
-   const mp_int *x;
+   mp_digit ac = 1, bc = 1, cc = 1;
+   mp_sign csign = (a->sign == MP_NEG && b->sign == MP_NEG) ? MP_NEG : MP_ZPOS;
 
-   if (a->used > b->used) {
-      if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
+   if (c->alloc < used) {
+      if ((err = mp_grow(c, used)) != MP_OKAY) {
          return err;
       }
-      px = b->used;
-      x = b;
-   } else {
-      if ((err = mp_init_copy(&t, b)) != MP_OKAY) {
-         return err;
-      }
-      px = a->used;
-      x = a;
    }
 
-   for (ix = 0; ix < px; ix++) {
-      t.dp[ix] &= x->dp[ix];
-   }
+   for (i = 0; i < used; i++) {
+      mp_digit x, y;
+
+      /* convert to two complement if negative */
+      if (a->sign == MP_NEG) {
+         ac += i >= a->used ? MP_MASK : ~a->dp[i] & MP_MASK;
+         x = ac & MP_MASK;
+         ac >>= MP_DIGIT_BIT;
+      } else {
+         x = i >= a->used ? 0 : a->dp[i];
+      }
 
-   /* zero digits above the last from the smallest mp_int */
-   MP_ZERO_DIGITS(t.dp + ix, t.used - ix);
+      /* convert to two complement if negative */
+      if (b->sign == MP_NEG) {
+         bc += i >= b->used ? MP_MASK : ~b->dp[i] & MP_MASK;
+         y = bc & MP_MASK;
+         bc >>= MP_DIGIT_BIT;
+      } else {
+         y = i >= b->used ? 0 : b->dp[i];
+      }
+
+      c->dp[i] = x & y;
+
+      /* convert to to sign-magnitude if negative */
+      if (csign == MP_NEG) {
+         cc += ~c->dp[i] & MP_MASK;
+         c->dp[i] = cc & MP_MASK;
+         cc >>= MP_DIGIT_BIT;
+      }
+   }
 
-   mp_clamp(&t);
-   mp_exch(c, &t);
-   mp_clear(&t);
+   c->used = used;
+   c->sign = csign;
+   mp_clamp(c);
    return MP_OKAY;
 }
 #endif
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index d7b6944..8234580 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -44,10 +44,7 @@ mp_err mp_lshd(mp_int *a, int b)
    }
 
    /* zero the lower digits */
-   top = a->dp;
-   for (x = 0; x < b; x++) {
-      *top++ = 0;
-   }
+   MP_ZERO_DIGITS(a->dp, b);
 
    return MP_OKAY;
 }
diff --git a/bn_mp_or.c b/bn_mp_or.c
index 254a5f9..6102a8a 100644
--- a/bn_mp_or.c
+++ b/bn_mp_or.c
@@ -3,34 +3,54 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* OR two ints together */
+/* two complement or */
 mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     ix, px;
-   mp_err  err;
-   mp_int  t;
-   const mp_int *x;
+   int used = MP_MAX(a->used, b->used) + 1, i;
+   mp_err err;
+   mp_digit ac = 1, bc = 1, cc = 1;
+   mp_sign csign = (a->sign == MP_NEG || b->sign == MP_NEG) ? MP_NEG : MP_ZPOS;
 
-   if (a->used > b->used) {
-      if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
+   if (c->alloc < used) {
+      if ((err = mp_grow(c, used)) != MP_OKAY) {
          return err;
       }
-      px = b->used;
-      x = b;
-   } else {
-      if ((err = mp_init_copy(&t, b)) != MP_OKAY) {
-         return err;
-      }
-      px = a->used;
-      x = a;
    }
 
-   for (ix = 0; ix < px; ix++) {
-      t.dp[ix] |= x->dp[ix];
+   for (i = 0; i < used; i++) {
+      mp_digit x, y;
+
+      /* convert to two complement if negative */
+      if (a->sign == MP_NEG) {
+         ac += i >= a->used ? MP_MASK : ~a->dp[i] & MP_MASK;
+         x = ac & MP_MASK;
+         ac >>= MP_DIGIT_BIT;
+      } else {
+         x = i >= a->used ? 0 : a->dp[i];
+      }
+
+      /* convert to two complement if negative */
+      if (b->sign == MP_NEG) {
+         bc += i >= b->used ? MP_MASK : ~b->dp[i] & MP_MASK;
+         y = bc & MP_MASK;
+         bc >>= MP_DIGIT_BIT;
+      } else {
+         y = i >= b->used ? 0 : b->dp[i];
+      }
+
+      c->dp[i] = x | y;
+
+      /* convert to to sign-magnitude if negative */
+      if (csign == MP_NEG) {
+         cc += ~c->dp[i] & MP_MASK;
+         c->dp[i] = cc & MP_MASK;
+         cc >>= MP_DIGIT_BIT;
+      }
    }
-   mp_clamp(&t);
-   mp_exch(c, &t);
-   mp_clear(&t);
+
+   c->used = used;
+   c->sign = csign;
+   mp_clamp(c);
    return MP_OKAY;
 }
 #endif
diff --git a/bn_mp_rshd.c b/bn_mp_rshd.c
index 1ab9ba4..bb8743e 100644
--- a/bn_mp_rshd.c
+++ b/bn_mp_rshd.c
@@ -43,9 +43,7 @@ void mp_rshd(mp_int *a, int b)
    }
 
    /* zero the top digits */
-   for (; x < a->used; x++) {
-      *bottom++ = 0;
-   }
+   MP_ZERO_DIGITS(bottom, a->used - x);
 
    /* remove excess digits */
    a->used -= b;
diff --git a/bn_mp_signed_rsh.c b/bn_mp_signed_rsh.c
new file mode 100644
index 0000000..8d8d841
--- /dev/null
+++ b/bn_mp_signed_rsh.c
@@ -0,0 +1,22 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SIGNED_RSH_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* shift right by a certain bit count with sign extension */
+mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c)
+{
+   mp_err res;
+   if (a->sign == MP_ZPOS) {
+      return mp_div_2d(a, b, c, NULL);
+   }
+
+   res = mp_add_d(a, 1uL, c);
+   if (res != MP_OKAY) {
+      return res;
+   }
+
+   res = mp_div_2d(c, b, c, NULL);
+   return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res;
+}
+#endif
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index d826052..3ebf9b4 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -6,7 +6,7 @@
 /* single digit subtraction */
 mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
 {
-   mp_digit *tmpa, *tmpc, mu;
+   mp_digit *tmpa, *tmpc;
    mp_err    err;
    int       ix, oldused;
 
@@ -50,17 +50,14 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
       c->sign = MP_NEG;
       c->used = 1;
    } else {
+      mp_digit mu = b;
+
       /* positive/size */
       c->sign = MP_ZPOS;
       c->used = a->used;
 
-      /* subtract first digit */
-      *tmpc    = *tmpa++ - b;
-      mu       = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
-      *tmpc++ &= MP_MASK;
-
-      /* handle rest of the digits */
-      for (ix = 1; ix < a->used; ix++) {
+      /* subtract digits, mu is carry */
+      for (ix = 0; ix < a->used; ix++) {
          *tmpc    = *tmpa++ - mu;
          mu       = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
          *tmpc++ &= MP_MASK;
diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c
deleted file mode 100644
index 8f4a724..0000000
--- a/bn_mp_tc_and.c
+++ /dev/null
@@ -1,78 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TC_AND_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* two complement and */
-mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
-{
-   mp_err err = MP_OKAY;
-   int bits, abits, bbits;
-   mp_sign sa = a->sign, sb = b->sign;
-   mp_int *mx = NULL, _mx, acpy, bcpy;
-
-   if ((sa == MP_NEG) || (sb == MP_NEG)) {
-      abits = mp_count_bits(a);
-      bbits = mp_count_bits(b);
-      bits = MP_MAX(abits, bbits);
-      err = mp_init_set_int(&_mx, 1uL);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      mx = &_mx;
-      err = mp_mul_2d(mx, bits + 1, mx);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      if (sa == MP_NEG) {
-         err = mp_init(&acpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, a, &acpy);
-         if (err != MP_OKAY) {
-            mp_clear(&acpy);
-            goto end;
-         }
-         a = &acpy;
-      }
-      if (sb == MP_NEG) {
-         err = mp_init(&bcpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, b, &bcpy);
-         if (err != MP_OKAY) {
-            mp_clear(&bcpy);
-            goto end;
-         }
-         b = &bcpy;
-      }
-   }
-
-   err = mp_and(a, b, c);
-
-   if ((sa == MP_NEG) && (sb == MP_NEG) && (err == MP_OKAY)) {
-      err = mp_sub(c, mx, c);
-   }
-
-end:
-   if (a == &acpy) {
-      mp_clear(&acpy);
-   }
-
-   if (b == &bcpy) {
-      mp_clear(&bcpy);
-   }
-
-   if (mx == &_mx) {
-      mp_clear(mx);
-   }
-
-   return err;
-}
-#endif
diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c
deleted file mode 100644
index d710208..0000000
--- a/bn_mp_tc_div_2d.c
+++ /dev/null
@@ -1,22 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TC_DIV_2D_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* two complement right shift */
-mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
-{
-   mp_err err;
-   if (a->sign == MP_ZPOS) {
-      return mp_div_2d(a, b, c, NULL);
-   }
-
-   err = mp_add_d(a, 1uL, c);
-   if (err != MP_OKAY) {
-      return err;
-   }
-
-   err = mp_div_2d(c, b, c, NULL);
-   return (err == MP_OKAY) ? mp_sub_d(c, 1uL, c) : err;
-}
-#endif
diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c
deleted file mode 100644
index 2b9eecc..0000000
--- a/bn_mp_tc_or.c
+++ /dev/null
@@ -1,78 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TC_OR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* two complement or */
-mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
-{
-   mp_err err = MP_OKAY;
-   int bits, abits, bbits;
-   mp_sign sa = a->sign, sb = b->sign;
-   mp_int *mx = NULL, _mx, acpy, bcpy;
-
-   if ((sa == MP_NEG) || (sb == MP_NEG)) {
-      abits = mp_count_bits(a);
-      bbits = mp_count_bits(b);
-      bits = MP_MAX(abits, bbits);
-      err = mp_init_set_int(&_mx, 1uL);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      mx = &_mx;
-      err = mp_mul_2d(mx, bits + 1, mx);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      if (sa == MP_NEG) {
-         err = mp_init(&acpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, a, &acpy);
-         if (err != MP_OKAY) {
-            mp_clear(&acpy);
-            goto end;
-         }
-         a = &acpy;
-      }
-      if (sb == MP_NEG) {
-         err = mp_init(&bcpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, b, &bcpy);
-         if (err != MP_OKAY) {
-            mp_clear(&bcpy);
-            goto end;
-         }
-         b = &bcpy;
-      }
-   }
-
-   err = mp_or(a, b, c);
-
-   if (((sa == MP_NEG) || (sb == MP_NEG)) && (err == MP_OKAY)) {
-      err = mp_sub(c, mx, c);
-   }
-
-end:
-   if (a == &acpy) {
-      mp_clear(&acpy);
-   }
-
-   if (b == &bcpy) {
-      mp_clear(&bcpy);
-   }
-
-   if (mx == &_mx) {
-      mp_clear(mx);
-   }
-
-   return err;
-}
-#endif
diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c
deleted file mode 100644
index 0af0ed2..0000000
--- a/bn_mp_tc_xor.c
+++ /dev/null
@@ -1,78 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TC_XOR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* two complement xor */
-mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
-{
-   mp_err err = MP_OKAY;
-   int bits, abits, bbits;
-   mp_sign sa = a->sign, sb = b->sign;
-   mp_int *mx = NULL, _mx, acpy, bcpy;
-
-   if ((sa == MP_NEG) || (sb == MP_NEG)) {
-      abits = mp_count_bits(a);
-      bbits = mp_count_bits(b);
-      bits = MP_MAX(abits, bbits);
-      err = mp_init_set_int(&_mx, 1uL);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      mx = &_mx;
-      err = mp_mul_2d(mx, bits + 1, mx);
-      if (err != MP_OKAY) {
-         goto end;
-      }
-
-      if (sa == MP_NEG) {
-         err = mp_init(&acpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, a, &acpy);
-         if (err != MP_OKAY) {
-            mp_clear(&acpy);
-            goto end;
-         }
-         a = &acpy;
-      }
-      if (sb == MP_NEG) {
-         err = mp_init(&bcpy);
-         if (err != MP_OKAY) {
-            goto end;
-         }
-
-         err = mp_add(mx, b, &bcpy);
-         if (err != MP_OKAY) {
-            mp_clear(&bcpy);
-            goto end;
-         }
-         b = &bcpy;
-      }
-   }
-
-   err = mp_xor(a, b, c);
-
-   if ((((sa == MP_NEG) && (sb != MP_NEG)) || ((sa != MP_NEG) && (sb == MP_NEG))) && (err == MP_OKAY)) {
-      err = mp_sub(c, mx, c);
-   }
-
-end:
-   if (a == &acpy) {
-      mp_clear(&acpy);
-   }
-
-   if (b == &bcpy) {
-      mp_clear(&bcpy);
-   }
-
-   if (mx == &_mx) {
-      mp_clear(mx);
-   }
-
-   return err;
-}
-#endif
diff --git a/bn_mp_xor.c b/bn_mp_xor.c
index d647062..c066e65 100644
--- a/bn_mp_xor.c
+++ b/bn_mp_xor.c
@@ -3,34 +3,54 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* XOR two ints together */
+/* two complement xor */
 mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     ix, px;
-   mp_err  err;
-   mp_int  t;
-   const mp_int *x;
+   int used = MP_MAX(a->used, b->used) + 1, i;
+   mp_err err;
+   mp_digit ac = 1, bc = 1, cc = 1;
+   mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
 
-   if (a->used > b->used) {
-      if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
+   if (c->alloc < used) {
+      if ((err = mp_grow(c, used)) != MP_OKAY) {
          return err;
       }
-      px = b->used;
-      x = b;
-   } else {
-      if ((err = mp_init_copy(&t, b)) != MP_OKAY) {
-         return err;
-      }
-      px = a->used;
-      x = a;
    }
 
-   for (ix = 0; ix < px; ix++) {
-      t.dp[ix] ^= x->dp[ix];
+   for (i = 0; i < used; i++) {
+      mp_digit x, y;
+
+      /* convert to two complement if negative */
+      if (a->sign == MP_NEG) {
+         ac += i >= a->used ? MP_MASK : ~a->dp[i] & MP_MASK;
+         x = ac & MP_MASK;
+         ac >>= MP_DIGIT_BIT;
+      } else {
+         x = i >= a->used ? 0 : a->dp[i];
+      }
+
+      /* convert to two complement if negative */
+      if (b->sign == MP_NEG) {
+         bc += i >= b->used ? MP_MASK : ~b->dp[i] & MP_MASK;
+         y = bc & MP_MASK;
+         bc >>= MP_DIGIT_BIT;
+      } else {
+         y = i >= b->used ? 0 : b->dp[i];
+      }
+
+      c->dp[i] = x ^ y;
+
+      /* convert to to sign-magnitude if negative */
+      if (csign == MP_NEG) {
+         cc += ~c->dp[i] & MP_MASK;
+         c->dp[i] = cc & MP_MASK;
+         cc >>= MP_DIGIT_BIT;
+      }
    }
-   mp_clamp(&t);
-   mp_exch(c, &t);
-   mp_clear(&t);
+
+   c->used = used;
+   c->sign = csign;
+   mp_clamp(c);
    return MP_OKAY;
 }
 #endif
diff --git a/demo/test.c b/demo/test.c
index 82825b5..12d2078 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -347,7 +347,7 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
-static int test_mp_tc_div_2d(void)
+static int test_mp_signed_rsh(void)
 {
    int i;
 
@@ -371,9 +371,9 @@ static int test_mp_tc_div_2d(void)
       if ((l >> em) < 0)
          mp_neg(&d, &d);
 
-      mp_tc_div_2d(&a, em, &b);
+      mp_signed_rsh(&a, em, &b);
       if (mp_cmp(&b, &d) != MP_EQ) {
-         printf("\nmp_tc_div_2d() bad result!");
+         printf("\nmp_signed_rsh() bad result!");
          goto LBL_ERR;
       }
    }
@@ -386,7 +386,7 @@ LBL_ERR:
 
 }
 
-static int test_mp_tc_xor(void)
+static int test_mp_xor(void)
 {
    int i;
 
@@ -412,9 +412,9 @@ static int test_mp_tc_xor(void)
       if ((l ^ em) < 0)
          mp_neg(&d, &d);
 
-      mp_tc_xor(&a, &b, &c);
+      mp_xor(&a, &b, &c);
       if (mp_cmp(&c, &d) != MP_EQ) {
-         printf("\nmp_tc_xor() bad result!");
+         printf("\nmp_xor() bad result!");
          goto LBL_ERR;
       }
    }
@@ -427,7 +427,7 @@ LBL_ERR:
 
 }
 
-static int test_mp_tc_or(void)
+static int test_mp_or(void)
 {
    int i;
 
@@ -453,9 +453,9 @@ static int test_mp_tc_or(void)
       if ((l | em) < 0)
          mp_neg(&d, &d);
 
-      mp_tc_or(&a, &b, &c);
+      mp_or(&a, &b, &c);
       if (mp_cmp(&c, &d) != MP_EQ) {
-         printf("\nmp_tc_or() bad result!");
+         printf("\nmp_or() bad result!");
          goto LBL_ERR;
       }
    }
@@ -467,7 +467,7 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
-static int test_mp_tc_and(void)
+static int test_mp_and(void)
 {
    int i;
 
@@ -493,9 +493,9 @@ static int test_mp_tc_and(void)
       if ((l & em) < 0)
          mp_neg(&d, &d);
 
-      mp_tc_and(&a, &b, &c);
+      mp_and(&a, &b, &c);
       if (mp_cmp(&c, &d) != MP_EQ) {
-         printf("\nmp_tc_and() bad result!");
+         printf("\nmp_and() bad result!");
          goto LBL_ERR;
       }
    }
@@ -2021,6 +2021,7 @@ int unit_tests(int argc, char **argv)
    } test[] = {
 #define T(n) { #n, test_##n }
       T(trivial_stuff),
+      T(mp_and),
       T(mp_cnt_lsb),
       T(mp_complement),
       T(mp_decr),
@@ -2037,6 +2038,7 @@ int unit_tests(int argc, char **argv)
       T(mp_kronecker),
       T(mp_montgomery_reduce),
       T(mp_n_root),
+      T(mp_or),
       T(mp_prime_is_prime),
       T(mp_prime_rand),
       T(mp_rand),
@@ -2044,12 +2046,10 @@ int unit_tests(int argc, char **argv)
       T(mp_reduce_2k),
       T(mp_reduce_2k_l),
       T(mp_set_double),
+      T(mp_signed_rsh),
       T(mp_sqrt),
       T(mp_sqrtmod_prime),
-      T(mp_tc_and),
-      T(mp_tc_div_2d),
-      T(mp_tc_or),
-      T(mp_tc_xor),
+      T(mp_xor),
       T(s_mp_balance_mul),
       T(s_mp_jacobi),
       T(s_mp_karatsuba_mul),
diff --git a/doc/bn.tex b/doc/bn.tex
index f4bfda5..f9f8f6b 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -1238,13 +1238,6 @@ function simply copies $a$ over to ``c'' and zeroes $d$.  The variable $d$ may b
 value to signal that the remainder is not desired.  The division itself is implemented as a left-shift
 operation of $a$ by $b$ bits.
 
-\index{mp\_tc\_div\_2d}\label{arithrightshift}
-\begin{alltt}
-int mp_tc_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
-\end{alltt}
-The two-co,mplement version of the function above. This can be used to implement arbitrary-precision two-complement integers together with the two-complement bit-wise operations at page \ref{tcbitwiseops}.
-
-
 It is also not very uncommon to need just the power of two $2^b$;  for example the startvalue for the Newton method.
 
 \index{mp\_2expt}
@@ -1280,30 +1273,20 @@ in place and no new digits are required to complete it.
 
 \subsection{AND, OR, XOR and COMPLEMENT Operations}
 
-While AND, OR and XOR operations are not typical ``bignum functions'' they can be useful in several instances.  The
-three functions are prototyped as follows.
+While AND, OR and XOR operations compute arbitrary-precision bitwise operations. Negative numbers
+are treated as if they are in two-complement representation, while internally they are sign-magnitude however.
 
-\index{mp\_or} \index{mp\_and} \index{mp\_xor}
+\index{mp\_or} \index{mp\_and} \index{mp\_xor} \index{mp\_complement}
 \begin{alltt}
 int mp_or  (mp_int * a, mp_int * b, mp_int * c);
 int mp_and (mp_int * a, mp_int * b, mp_int * c);
 int mp_xor (mp_int * a, mp_int * b, mp_int * c);
-\end{alltt}
-
-Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR.
-
-The following four functions allow implementing arbitrary-precision two-complement numbers.
-
-\index{mp\_tc\_or} \index{mp\_tc\_and} \index{mp\_tc\_xor} \index{mp\_complement} \label{tcbitwiseops}
-\begin{alltt}
-int mp_tc_or  (mp_int * a, mp_int * b, mp_int * c);
-int mp_tc_and (mp_int * a, mp_int * b, mp_int * c);
-int mp_tc_xor (mp_int * a, mp_int * b, mp_int * c);
 int mp_complement(const mp_int *a, mp_int *b);
+int mp_signed_rsh(mp_int * a, int b, mp_int * c, mp_int * d);
 \end{alltt}
 
-They compute $c = a \odot b$ as above if both $a$ and $b$ are positive. Negative values are converted into their two-complement representations first. The function \texttt{mp\_complement} computes a two-complement $b = \sim a$.
-
+The function \texttt{mp\_complement} computes a two-complement $b = \sim a$. The function \texttt{mp\_signed\_rsh} performs
+sign extending right shift. For positive numbers it is equivalent to \texttt{mp\_div\_2d}.
 
 \subsection{Bit Picking}
 \index{mp\_get\_bit}
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 7b05416..84e0b16 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -729,6 +729,10 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_mp_signed_rsh.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_mp_sqr.c"
 			>
 		</File>
@@ -757,22 +761,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_tc_and.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_tc_div_2d.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_tc_or.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_tc_xor.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_to_signed_bin.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 16232cb..6dbe1ab 100644
--- a/makefile
+++ b/makefile
@@ -45,15 +45,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.
 bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
 bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
-bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \
-bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \
-bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
-bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
-bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
-bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
-bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
-bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
-bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
+bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
+bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \
+bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \
+bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \
+bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \
+bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \
+bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.mingw b/makefile.mingw
index 1659203..2ed79b3 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -48,15 +48,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.
 bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
 bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
-bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \
-bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \
-bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
-bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
-bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
-bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
-bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
-bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
-bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
+bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
+bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \
+bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \
+bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \
+bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \
+bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \
+bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/makefile.msvc b/makefile.msvc
index b7a28c8..e957530 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -40,15 +40,15 @@ bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj bn_mp_read_unsigne
 bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj bn_mp_reduce_2k_setup_l.obj \
 bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_rshd.obj bn_mp_set.obj \
 bn_mp_set_double.obj bn_mp_set_int.obj bn_mp_set_long.obj bn_mp_set_long_long.obj bn_mp_shrink.obj \
-bn_mp_signed_bin_size.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj bn_mp_sub.obj \
-bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_tc_and.obj bn_mp_tc_div_2d.obj bn_mp_tc_or.obj bn_mp_tc_xor.obj \
-bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin.obj bn_mp_to_unsigned_bin_n.obj \
-bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj \
-bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj \
-bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj \
-bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj \
-bn_s_mp_mul_high_digs_fast.obj bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj \
-bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
+bn_mp_signed_bin_size.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj \
+bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj \
+bn_mp_to_unsigned_bin.obj bn_mp_to_unsigned_bin_n.obj bn_mp_toradix.obj bn_mp_toradix_n.obj \
+bn_mp_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj bn_s_mp_add.obj bn_s_mp_balance_mul.obj \
+bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj \
+bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj \
+bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj bn_s_mp_rand_jenkins.obj \
+bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj \
+bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/makefile.shared b/makefile.shared
index b4be47c..371291b 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -42,15 +42,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.
 bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
 bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
-bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \
-bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \
-bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
-bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
-bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
-bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
-bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
-bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
-bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
+bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
+bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \
+bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \
+bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \
+bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \
+bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \
+bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.unix b/makefile.unix
index 7a1548f..2831040 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -49,15 +49,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.
 bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
 bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
-bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \
-bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \
-bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
-bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
-bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
-bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
-bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
-bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
-bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
+bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
+bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \
+bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \
+bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \
+bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \
+bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \
+bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/tommath.h b/tommath.h
index ba14826..2349306 100644
--- a/tommath.h
+++ b/tommath.h
@@ -367,14 +367,6 @@ extern void (*ltm_rng_callback)(void);
 #endif
 
 /* ---> binary operations <--- */
-/* c = a XOR b  */
-mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-
-/* c = a OR b */
-mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-
-/* c = a AND b */
-mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* Checks the bit at position b and returns MP_YES
  * if the bit is 1, MP_NO if it is 0 and MP_VAL
@@ -383,22 +375,26 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR;
 
 /* c = a XOR b (two complement) */
-mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a OR b (two complement) */
-mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a AND b (two complement) */
-mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+
+/* b = ~a (bitwise not, two complement) */
+mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
 
-/* right shift (two complement) */
-mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
+/* right shift with sign extension */
+MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
+mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR;
 
 /* ---> Basic arithmetic <--- */
 
-/* b = ~a */
-mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
-
 /* b = -a */
 mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR;
 
diff --git a/tommath_class.h b/tommath_class.h
index d51c7f8..b7e59b2 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -114,6 +114,7 @@
 #   define BN_MP_SET_LONG_LONG_C
 #   define BN_MP_SHRINK_C
 #   define BN_MP_SIGNED_BIN_SIZE_C
+#   define BN_MP_SIGNED_RSH_C
 #   define BN_MP_SQR_C
 #   define BN_MP_SQRMOD_C
 #   define BN_MP_SQRT_C
@@ -121,10 +122,6 @@
 #   define BN_MP_SUB_C
 #   define BN_MP_SUB_D_C
 #   define BN_MP_SUBMOD_C
-#   define BN_MP_TC_AND_C
-#   define BN_MP_TC_DIV_2D_C
-#   define BN_MP_TC_OR_C
-#   define BN_MP_TC_XOR_C
 #   define BN_MP_TO_SIGNED_BIN_C
 #   define BN_MP_TO_SIGNED_BIN_N_C
 #   define BN_MP_TO_UNSIGNED_BIN_C
@@ -197,6 +194,14 @@
 #   define BN_MP_TOOM_SQR_C
 #   define BN_S_MP_TOOM_SQR_C
 #   define BN_S_MP_REVERSE_C
+#   define BN_MP_TC_AND_C
+#   define BN_MP_AND_C
+#   define BN_MP_TC_OR_C
+#   define BN_MP_OR_C
+#   define BN_MP_TC_XOR_C
+#   define BN_MP_XOR_C
+#   define BN_MP_TC_DIV_2D_C
+#   define BN_MP_SIGNED_RSH_C
 #endif
 
 #if defined(BN_MP_2EXPT_C)
@@ -228,10 +233,8 @@
 #endif
 
 #if defined(BN_MP_AND_C)
-#   define BN_MP_INIT_COPY_C
+#   define BN_MP_GROW_C
 #   define BN_MP_CLAMP_C
-#   define BN_MP_EXCH_C
-#   define BN_MP_CLEAR_C
 #endif
 
 #if defined(BN_MP_CLAMP_C)
@@ -632,10 +635,8 @@
 #endif
 
 #if defined(BN_MP_OR_C)
-#   define BN_MP_INIT_COPY_C
+#   define BN_MP_GROW_C
 #   define BN_MP_CLAMP_C
-#   define BN_MP_EXCH_C
-#   define BN_MP_CLEAR_C
 #endif
 
 #if defined(BN_MP_PRIME_FERMAT_C)
@@ -887,6 +888,12 @@
 #   define BN_MP_UNSIGNED_BIN_SIZE_C
 #endif
 
+#if defined(BN_MP_SIGNED_RSH_C)
+#   define BN_MP_DIV_2D_C
+#   define BN_MP_ADD_D_C
+#   define BN_MP_SUB_D_C
+#endif
+
 #if defined(BN_MP_SQR_C)
 #   define BN_S_MP_TOOM_SQR_C
 #   define BN_S_MP_KARATSUBA_SQR_C
@@ -950,45 +957,6 @@
 #   define BN_MP_MOD_C
 #endif
 
-#if defined(BN_MP_TC_AND_C)
-#   define BN_MP_COUNT_BITS_C
-#   define BN_MP_INIT_SET_INT_C
-#   define BN_MP_MUL_2D_C
-#   define BN_MP_INIT_C
-#   define BN_MP_ADD_C
-#   define BN_MP_CLEAR_C
-#   define BN_MP_AND_C
-#   define BN_MP_SUB_C
-#endif
-
-#if defined(BN_MP_TC_DIV_2D_C)
-#   define BN_MP_DIV_2D_C
-#   define BN_MP_ADD_D_C
-#   define BN_MP_SUB_D_C
-#endif
-
-#if defined(BN_MP_TC_OR_C)
-#   define BN_MP_COUNT_BITS_C
-#   define BN_MP_INIT_SET_INT_C
-#   define BN_MP_MUL_2D_C
-#   define BN_MP_INIT_C
-#   define BN_MP_ADD_C
-#   define BN_MP_CLEAR_C
-#   define BN_MP_OR_C
-#   define BN_MP_SUB_C
-#endif
-
-#if defined(BN_MP_TC_XOR_C)
-#   define BN_MP_COUNT_BITS_C
-#   define BN_MP_INIT_SET_INT_C
-#   define BN_MP_MUL_2D_C
-#   define BN_MP_INIT_C
-#   define BN_MP_ADD_C
-#   define BN_MP_CLEAR_C
-#   define BN_MP_XOR_C
-#   define BN_MP_SUB_C
-#endif
-
 #if defined(BN_MP_TO_SIGNED_BIN_C)
 #   define BN_MP_TO_UNSIGNED_BIN_C
 #endif
@@ -1029,10 +997,8 @@
 #endif
 
 #if defined(BN_MP_XOR_C)
-#   define BN_MP_INIT_COPY_C
+#   define BN_MP_GROW_C
 #   define BN_MP_CLAMP_C
-#   define BN_MP_EXCH_C
-#   define BN_MP_CLEAR_C
 #endif
 
 #if defined(BN_MP_ZERO_C)