comparison mpi.c @ 15:6362d3854bb4 libtomcrypt-orig

0.96 release of LibTomCrypt
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 14:07:21 +0000
parents 7faae8f46238
children 5d99163f7e32
comparison
equal deleted inserted replaced
3:7faae8f46238 15:6362d3854bb4
11 * The library is free for all purposes without any express 11 * The library is free for all purposes without any express
12 * guarantee it works. 12 * guarantee it works.
13 * 13 *
14 * Tom St Denis, [email protected], http://math.libtomcrypt.org 14 * Tom St Denis, [email protected], http://math.libtomcrypt.org
15 */ 15 */
16 #include <tommath.h> 16 #include <ltc_tommath.h>
17 17
18 static const struct { 18 static const struct {
19 int code; 19 int code;
20 char *msg; 20 char *msg;
21 } msgs[] = { 21 } msgs[] = {
56 * The library is free for all purposes without any express 56 * The library is free for all purposes without any express
57 * guarantee it works. 57 * guarantee it works.
58 * 58 *
59 * Tom St Denis, [email protected], http://math.libtomcrypt.org 59 * Tom St Denis, [email protected], http://math.libtomcrypt.org
60 */ 60 */
61 #include <tommath.h> 61 #include <ltc_tommath.h>
62 62
63 /* computes the modular inverse via binary extended euclidean algorithm, 63 /* computes the modular inverse via binary extended euclidean algorithm,
64 * that is c = 1/a mod b 64 * that is c = 1/a mod b
65 * 65 *
66 * Based on mp_invmod except this is optimized for the case where b is 66 * Based on mp_invmod except this is optimized for the case where b is
203 * The library is free for all purposes without any express 203 * The library is free for all purposes without any express
204 * guarantee it works. 204 * guarantee it works.
205 * 205 *
206 * Tom St Denis, [email protected], http://math.libtomcrypt.org 206 * Tom St Denis, [email protected], http://math.libtomcrypt.org
207 */ 207 */
208 #include <tommath.h> 208 #include <ltc_tommath.h>
209 209
210 /* computes xR**-1 == x (mod N) via Montgomery Reduction 210 /* computes xR**-1 == x (mod N) via Montgomery Reduction
211 * 211 *
212 * This is an optimized implementation of mp_montgomery_reduce 212 * This is an optimized implementation of mp_montgomery_reduce
213 * which uses the comba method to quickly calculate the columns of the 213 * which uses the comba method to quickly calculate the columns of the
374 * The library is free for all purposes without any express 374 * The library is free for all purposes without any express
375 * guarantee it works. 375 * guarantee it works.
376 * 376 *
377 * Tom St Denis, [email protected], http://math.libtomcrypt.org 377 * Tom St Denis, [email protected], http://math.libtomcrypt.org
378 */ 378 */
379 #include <tommath.h> 379 #include <ltc_tommath.h>
380 380
381 /* Fast (comba) multiplier 381 /* Fast (comba) multiplier
382 * 382 *
383 * This is the fast column-array [comba] multiplier. It is 383 * This is the fast column-array [comba] multiplier. It is
384 * designed to compute the columns of the product first 384 * designed to compute the columns of the product first
450 } 450 }
451 451
452 } 452 }
453 453
454 /* setup dest */ 454 /* setup dest */
455 olduse = c->used; 455 olduse = c->used;
456 c->used = digs; 456 c->used = digs;
457 457
458 { 458 {
459 register mp_digit *tmpc; 459 register mp_digit *tmpc;
460 460
508 * The library is free for all purposes without any express 508 * The library is free for all purposes without any express
509 * guarantee it works. 509 * guarantee it works.
510 * 510 *
511 * Tom St Denis, [email protected], http://math.libtomcrypt.org 511 * Tom St Denis, [email protected], http://math.libtomcrypt.org
512 */ 512 */
513 #include <tommath.h> 513 #include <ltc_tommath.h>
514 514
515 /* this is a modified version of fast_s_mp_mul_digs that only produces 515 /* this is a modified version of fast_s_mp_mul_digs that only produces
516 * output digits *above* digs. See the comments for fast_s_mp_mul_digs 516 * output digits *above* digs. See the comments for fast_s_mp_mul_digs
517 * to see how it works. 517 * to see how it works.
518 * 518 *
610 * The library is free for all purposes without any express 610 * The library is free for all purposes without any express
611 * guarantee it works. 611 * guarantee it works.
612 * 612 *
613 * Tom St Denis, [email protected], http://math.libtomcrypt.org 613 * Tom St Denis, [email protected], http://math.libtomcrypt.org
614 */ 614 */
615 #include <tommath.h> 615 #include <ltc_tommath.h>
616 616
617 /* fast squaring 617 /* fast squaring
618 * 618 *
619 * This is the comba method where the columns of the product 619 * This is the comba method where the columns of the product
620 * are computed first then the carries are computed. This 620 * are computed first then the carries are computed. This
753 * The library is free for all purposes without any express 753 * The library is free for all purposes without any express
754 * guarantee it works. 754 * guarantee it works.
755 * 755 *
756 * Tom St Denis, [email protected], http://math.libtomcrypt.org 756 * Tom St Denis, [email protected], http://math.libtomcrypt.org
757 */ 757 */
758 #include <tommath.h> 758 #include <ltc_tommath.h>
759 759
760 /* computes a = 2**b 760 /* computes a = 2**b
761 * 761 *
762 * Simple algorithm which zeroes the int, grows it then just sets one bit 762 * Simple algorithm which zeroes the int, grows it then just sets one bit
763 * as required. 763 * as required.
799 * The library is free for all purposes without any express 799 * The library is free for all purposes without any express
800 * guarantee it works. 800 * guarantee it works.
801 * 801 *
802 * Tom St Denis, [email protected], http://math.libtomcrypt.org 802 * Tom St Denis, [email protected], http://math.libtomcrypt.org
803 */ 803 */
804 #include <tommath.h> 804 #include <ltc_tommath.h>
805 805
806 /* b = |a| 806 /* b = |a|
807 * 807 *
808 * Simple function copies the input and fixes the sign to positive 808 * Simple function copies the input and fixes the sign to positive
809 */ 809 */
840 * The library is free for all purposes without any express 840 * The library is free for all purposes without any express
841 * guarantee it works. 841 * guarantee it works.
842 * 842 *
843 * Tom St Denis, [email protected], http://math.libtomcrypt.org 843 * Tom St Denis, [email protected], http://math.libtomcrypt.org
844 */ 844 */
845 #include <tommath.h> 845 #include <ltc_tommath.h>
846 846
847 /* high level addition (handles signs) */ 847 /* high level addition (handles signs) */
848 int mp_add (mp_int * a, mp_int * b, mp_int * c) 848 int mp_add (mp_int * a, mp_int * b, mp_int * c)
849 { 849 {
850 int sa, sb, res; 850 int sa, sb, res;
891 * The library is free for all purposes without any express 891 * The library is free for all purposes without any express
892 * guarantee it works. 892 * guarantee it works.
893 * 893 *
894 * Tom St Denis, [email protected], http://math.libtomcrypt.org 894 * Tom St Denis, [email protected], http://math.libtomcrypt.org
895 */ 895 */
896 #include <tommath.h> 896 #include <ltc_tommath.h>
897 897
898 /* single digit addition */ 898 /* single digit addition */
899 int 899 int
900 mp_add_d (mp_int * a, mp_digit b, mp_int * c) 900 mp_add_d (mp_int * a, mp_digit b, mp_int * c)
901 { 901 {
998 * The library is free for all purposes without any express 998 * The library is free for all purposes without any express
999 * guarantee it works. 999 * guarantee it works.
1000 * 1000 *
1001 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1001 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1002 */ 1002 */
1003 #include <tommath.h> 1003 #include <ltc_tommath.h>
1004 1004
1005 /* d = a + b (mod c) */ 1005 /* d = a + b (mod c) */
1006 int 1006 int
1007 mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) 1007 mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
1008 { 1008 {
1037 * The library is free for all purposes without any express 1037 * The library is free for all purposes without any express
1038 * guarantee it works. 1038 * guarantee it works.
1039 * 1039 *
1040 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1040 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1041 */ 1041 */
1042 #include <tommath.h> 1042 #include <ltc_tommath.h>
1043 1043
1044 /* AND two ints together */ 1044 /* AND two ints together */
1045 int 1045 int
1046 mp_and (mp_int * a, mp_int * b, mp_int * c) 1046 mp_and (mp_int * a, mp_int * b, mp_int * c)
1047 { 1047 {
1092 * The library is free for all purposes without any express 1092 * The library is free for all purposes without any express
1093 * guarantee it works. 1093 * guarantee it works.
1094 * 1094 *
1095 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1095 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1096 */ 1096 */
1097 #include <tommath.h> 1097 #include <ltc_tommath.h>
1098 1098
1099 /* trim unused digits 1099 /* trim unused digits
1100 * 1100 *
1101 * This is used to ensure that leading zero digits are 1101 * This is used to ensure that leading zero digits are
1102 * trimed and the leading "used" digit will be non-zero 1102 * trimed and the leading "used" digit will be non-zero
1134 * The library is free for all purposes without any express 1134 * The library is free for all purposes without any express
1135 * guarantee it works. 1135 * guarantee it works.
1136 * 1136 *
1137 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1137 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1138 */ 1138 */
1139 #include <tommath.h> 1139 #include <ltc_tommath.h>
1140 1140
1141 /* clear one (frees) */ 1141 /* clear one (frees) */
1142 void 1142 void
1143 mp_clear (mp_int * a) 1143 mp_clear (mp_int * a)
1144 { 1144 {
1172 * The library is free for all purposes without any express 1172 * The library is free for all purposes without any express
1173 * guarantee it works. 1173 * guarantee it works.
1174 * 1174 *
1175 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1175 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1176 */ 1176 */
1177 #include <tommath.h> 1177 #include <ltc_tommath.h>
1178 #include <stdarg.h> 1178 #include <stdarg.h>
1179 1179
1180 void mp_clear_multi(mp_int *mp, ...) 1180 void mp_clear_multi(mp_int *mp, ...)
1181 { 1181 {
1182 mp_int* next_mp = mp; 1182 mp_int* next_mp = mp;
1204 * The library is free for all purposes without any express 1204 * The library is free for all purposes without any express
1205 * guarantee it works. 1205 * guarantee it works.
1206 * 1206 *
1207 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1207 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1208 */ 1208 */
1209 #include <tommath.h> 1209 #include <ltc_tommath.h>
1210 1210
1211 /* compare two ints (signed)*/ 1211 /* compare two ints (signed)*/
1212 int 1212 int
1213 mp_cmp (mp_int * a, mp_int * b) 1213 mp_cmp (mp_int * a, mp_int * b)
1214 { 1214 {
1245 * The library is free for all purposes without any express 1245 * The library is free for all purposes without any express
1246 * guarantee it works. 1246 * guarantee it works.
1247 * 1247 *
1248 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1248 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1249 */ 1249 */
1250 #include <tommath.h> 1250 #include <ltc_tommath.h>
1251 1251
1252 /* compare a digit */ 1252 /* compare a digit */
1253 int mp_cmp_d(mp_int * a, mp_digit b) 1253 int mp_cmp_d(mp_int * a, mp_digit b)
1254 { 1254 {
1255 /* compare based on sign */ 1255 /* compare based on sign */
1287 * The library is free for all purposes without any express 1287 * The library is free for all purposes without any express
1288 * guarantee it works. 1288 * guarantee it works.
1289 * 1289 *
1290 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1290 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1291 */ 1291 */
1292 #include <tommath.h> 1292 #include <ltc_tommath.h>
1293 1293
1294 /* compare maginitude of two ints (unsigned) */ 1294 /* compare maginitude of two ints (unsigned) */
1295 int mp_cmp_mag (mp_int * a, mp_int * b) 1295 int mp_cmp_mag (mp_int * a, mp_int * b)
1296 { 1296 {
1297 int n; 1297 int n;
1340 * The library is free for all purposes without any express 1340 * The library is free for all purposes without any express
1341 * guarantee it works. 1341 * guarantee it works.
1342 * 1342 *
1343 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1343 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1344 */ 1344 */
1345 #include <tommath.h> 1345 #include <ltc_tommath.h>
1346 1346
1347 static const int lnz[16] = { 1347 static const int lnz[16] = {
1348 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 1348 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1349 }; 1349 };
1350 1350
1391 * The library is free for all purposes without any express 1391 * The library is free for all purposes without any express
1392 * guarantee it works. 1392 * guarantee it works.
1393 * 1393 *
1394 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1394 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1395 */ 1395 */
1396 #include <tommath.h> 1396 #include <ltc_tommath.h>
1397 1397
1398 /* copy, b = a */ 1398 /* copy, b = a */
1399 int 1399 int
1400 mp_copy (mp_int * a, mp_int * b) 1400 mp_copy (mp_int * a, mp_int * b)
1401 { 1401 {
1457 * The library is free for all purposes without any express 1457 * The library is free for all purposes without any express
1458 * guarantee it works. 1458 * guarantee it works.
1459 * 1459 *
1460 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1460 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1461 */ 1461 */
1462 #include <tommath.h> 1462 #include <ltc_tommath.h>
1463 1463
1464 /* returns the number of bits in an int */ 1464 /* returns the number of bits in an int */
1465 int 1465 int
1466 mp_count_bits (mp_int * a) 1466 mp_count_bits (mp_int * a)
1467 { 1467 {
1500 * The library is free for all purposes without any express 1500 * The library is free for all purposes without any express
1501 * guarantee it works. 1501 * guarantee it works.
1502 * 1502 *
1503 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1503 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1504 */ 1504 */
1505 #include <tommath.h> 1505 #include <ltc_tommath.h>
1506 1506
1507 /* integer signed division. 1507 /* integer signed division.
1508 * c*b + d == a [e.g. a/b, c=quotient, d=remainder] 1508 * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
1509 * HAC pp.598 Algorithm 14.20 1509 * HAC pp.598 Algorithm 14.20
1510 * 1510 *
1715 * The library is free for all purposes without any express 1715 * The library is free for all purposes without any express
1716 * guarantee it works. 1716 * guarantee it works.
1717 * 1717 *
1718 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1718 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1719 */ 1719 */
1720 #include <tommath.h> 1720 #include <ltc_tommath.h>
1721 1721
1722 /* b = a/2 */ 1722 /* b = a/2 */
1723 int mp_div_2(mp_int * a, mp_int * b) 1723 int mp_div_2(mp_int * a, mp_int * b)
1724 { 1724 {
1725 int x, res, oldused; 1725 int x, res, oldused;
1781 * The library is free for all purposes without any express 1781 * The library is free for all purposes without any express
1782 * guarantee it works. 1782 * guarantee it works.
1783 * 1783 *
1784 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1784 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1785 */ 1785 */
1786 #include <tommath.h> 1786 #include <ltc_tommath.h>
1787 1787
1788 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ 1788 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
1789 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) 1789 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
1790 { 1790 {
1791 mp_digit D, r, rr; 1791 mp_digit D, r, rr;
1876 * The library is free for all purposes without any express 1876 * The library is free for all purposes without any express
1877 * guarantee it works. 1877 * guarantee it works.
1878 * 1878 *
1879 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1879 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1880 */ 1880 */
1881 #include <tommath.h> 1881 #include <ltc_tommath.h>
1882 1882
1883 /* divide by three (based on routine from MPI and the GMP manual) */ 1883 /* divide by three (based on routine from MPI and the GMP manual) */
1884 int 1884 int
1885 mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) 1885 mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
1886 { 1886 {
1953 * The library is free for all purposes without any express 1953 * The library is free for all purposes without any express
1954 * guarantee it works. 1954 * guarantee it works.
1955 * 1955 *
1956 * Tom St Denis, [email protected], http://math.libtomcrypt.org 1956 * Tom St Denis, [email protected], http://math.libtomcrypt.org
1957 */ 1957 */
1958 #include <tommath.h> 1958 #include <ltc_tommath.h>
1959 1959
1960 static int s_is_power_of_two(mp_digit b, int *p) 1960 static int s_is_power_of_two(mp_digit b, int *p)
1961 { 1961 {
1962 int x; 1962 int x;
1963 1963
2059 * The library is free for all purposes without any express 2059 * The library is free for all purposes without any express
2060 * guarantee it works. 2060 * guarantee it works.
2061 * 2061 *
2062 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2062 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2063 */ 2063 */
2064 #include <tommath.h> 2064 #include <ltc_tommath.h>
2065 2065
2066 /* determines if a number is a valid DR modulus */ 2066 /* determines if a number is a valid DR modulus */
2067 int mp_dr_is_modulus(mp_int *a) 2067 int mp_dr_is_modulus(mp_int *a)
2068 { 2068 {
2069 int ix; 2069 int ix;
2100 * The library is free for all purposes without any express 2100 * The library is free for all purposes without any express
2101 * guarantee it works. 2101 * guarantee it works.
2102 * 2102 *
2103 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2103 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2104 */ 2104 */
2105 #include <tommath.h> 2105 #include <ltc_tommath.h>
2106 2106
2107 /* reduce "x" in place modulo "n" using the Diminished Radix algorithm. 2107 /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
2108 * 2108 *
2109 * Based on algorithm from the paper 2109 * Based on algorithm from the paper
2110 * 2110 *
2192 * The library is free for all purposes without any express 2192 * The library is free for all purposes without any express
2193 * guarantee it works. 2193 * guarantee it works.
2194 * 2194 *
2195 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2195 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2196 */ 2196 */
2197 #include <tommath.h> 2197 #include <ltc_tommath.h>
2198 2198
2199 /* determines the setup value */ 2199 /* determines the setup value */
2200 void mp_dr_setup(mp_int *a, mp_digit *d) 2200 void mp_dr_setup(mp_int *a, mp_digit *d)
2201 { 2201 {
2202 /* the casts are required if DIGIT_BIT is one less than 2202 /* the casts are required if DIGIT_BIT is one less than
2222 * The library is free for all purposes without any express 2222 * The library is free for all purposes without any express
2223 * guarantee it works. 2223 * guarantee it works.
2224 * 2224 *
2225 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2225 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2226 */ 2226 */
2227 #include <tommath.h> 2227 #include <ltc_tommath.h>
2228 2228
2229 /* swap the elements of two integers, for cases where you can't simply swap the 2229 /* swap the elements of two integers, for cases where you can't simply swap the
2230 * mp_int pointers around 2230 * mp_int pointers around
2231 */ 2231 */
2232 void 2232 void
2254 * The library is free for all purposes without any express 2254 * The library is free for all purposes without any express
2255 * guarantee it works. 2255 * guarantee it works.
2256 * 2256 *
2257 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2257 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2258 */ 2258 */
2259 #include <tommath.h> 2259 #include <ltc_tommath.h>
2260 2260
2261 /* calculate c = a**b using a square-multiply algorithm */ 2261 /* calculate c = a**b using a square-multiply algorithm */
2262 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) 2262 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
2263 { 2263 {
2264 int res, x; 2264 int res, x;
2309 * The library is free for all purposes without any express 2309 * The library is free for all purposes without any express
2310 * guarantee it works. 2310 * guarantee it works.
2311 * 2311 *
2312 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2312 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2313 */ 2313 */
2314 #include <tommath.h> 2314 #include <ltc_tommath.h>
2315 2315
2316 2316
2317 /* this is a shell function that calls either the normal or Montgomery 2317 /* this is a shell function that calls either the normal or Montgomery
2318 * exptmod functions. Originally the call to the montgomery code was 2318 * exptmod functions. Originally the call to the montgomery code was
2319 * embedded in the normal function but that wasted alot of stack space 2319 * embedded in the normal function but that wasted alot of stack space
2391 * The library is free for all purposes without any express 2391 * The library is free for all purposes without any express
2392 * guarantee it works. 2392 * guarantee it works.
2393 * 2393 *
2394 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2394 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2395 */ 2395 */
2396 #include <tommath.h> 2396 #include <ltc_tommath.h>
2397 2397
2398 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85 2398 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
2399 * 2399 *
2400 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation. 2400 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
2401 * The value of k changes based on the size of the exponent. 2401 * The value of k changes based on the size of the exponent.
2682 * The library is free for all purposes without any express 2682 * The library is free for all purposes without any express
2683 * guarantee it works. 2683 * guarantee it works.
2684 * 2684 *
2685 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2685 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2686 */ 2686 */
2687 #include <tommath.h> 2687 #include <ltc_tommath.h>
2688 2688
2689 /* Extended euclidean algorithm of (a, b) produces 2689 /* Extended euclidean algorithm of (a, b) produces
2690 a*u1 + b*u2 = u3 2690 a*u1 + b*u2 = u3
2691 */ 2691 */
2692 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) 2692 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
2755 * The library is free for all purposes without any express 2755 * The library is free for all purposes without any express
2756 * guarantee it works. 2756 * guarantee it works.
2757 * 2757 *
2758 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2758 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2759 */ 2759 */
2760 #include <tommath.h> 2760 #include <ltc_tommath.h>
2761 2761
2762 /* read a bigint from a file stream in ASCII */ 2762 /* read a bigint from a file stream in ASCII */
2763 int mp_fread(mp_int *a, int radix, FILE *stream) 2763 int mp_fread(mp_int *a, int radix, FILE *stream)
2764 { 2764 {
2765 int err, ch, neg, y; 2765 int err, ch, neg, y;
2820 * The library is free for all purposes without any express 2820 * The library is free for all purposes without any express
2821 * guarantee it works. 2821 * guarantee it works.
2822 * 2822 *
2823 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2823 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2824 */ 2824 */
2825 #include <tommath.h> 2825 #include <ltc_tommath.h>
2826 2826
2827 int mp_fwrite(mp_int *a, int radix, FILE *stream) 2827 int mp_fwrite(mp_int *a, int radix, FILE *stream)
2828 { 2828 {
2829 char *buf; 2829 char *buf;
2830 int err, len, x; 2830 int err, len, x;
2870 * The library is free for all purposes without any express 2870 * The library is free for all purposes without any express
2871 * guarantee it works. 2871 * guarantee it works.
2872 * 2872 *
2873 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2873 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2874 */ 2874 */
2875 #include <tommath.h> 2875 #include <ltc_tommath.h>
2876 2876
2877 /* Greatest Common Divisor using the binary method */ 2877 /* Greatest Common Divisor using the binary method */
2878 int mp_gcd (mp_int * a, mp_int * b, mp_int * c) 2878 int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
2879 { 2879 {
2880 mp_int u, v; 2880 mp_int u, v;
2981 * The library is free for all purposes without any express 2981 * The library is free for all purposes without any express
2982 * guarantee it works. 2982 * guarantee it works.
2983 * 2983 *
2984 * Tom St Denis, [email protected], http://math.libtomcrypt.org 2984 * Tom St Denis, [email protected], http://math.libtomcrypt.org
2985 */ 2985 */
2986 #include <tommath.h> 2986 #include <ltc_tommath.h>
2987 2987
2988 /* get the lower 32-bits of an mp_int */ 2988 /* get the lower 32-bits of an mp_int */
2989 unsigned long mp_get_int(mp_int * a) 2989 unsigned long mp_get_int(mp_int * a)
2990 { 2990 {
2991 int i; 2991 int i;
3024 * The library is free for all purposes without any express 3024 * The library is free for all purposes without any express
3025 * guarantee it works. 3025 * guarantee it works.
3026 * 3026 *
3027 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3027 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3028 */ 3028 */
3029 #include <tommath.h> 3029 #include <ltc_tommath.h>
3030 3030
3031 /* grow as required */ 3031 /* grow as required */
3032 int mp_grow (mp_int * a, int size) 3032 int mp_grow (mp_int * a, int size)
3033 { 3033 {
3034 int i; 3034 int i;
3079 * The library is free for all purposes without any express 3079 * The library is free for all purposes without any express
3080 * guarantee it works. 3080 * guarantee it works.
3081 * 3081 *
3082 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3082 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3083 */ 3083 */
3084 #include <tommath.h> 3084 #include <ltc_tommath.h>
3085 3085
3086 /* init a new bigint */ 3086 /* init a new bigint */
3087 int mp_init (mp_int * a) 3087 int mp_init (mp_int * a)
3088 { 3088 {
3089 /* allocate memory required and clear it */ 3089 /* allocate memory required and clear it */
3116 * The library is free for all purposes without any express 3116 * The library is free for all purposes without any express
3117 * guarantee it works. 3117 * guarantee it works.
3118 * 3118 *
3119 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3119 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3120 */ 3120 */
3121 #include <tommath.h> 3121 #include <ltc_tommath.h>
3122 3122
3123 /* creates "a" then copies b into it */ 3123 /* creates "a" then copies b into it */
3124 int mp_init_copy (mp_int * a, mp_int * b) 3124 int mp_init_copy (mp_int * a, mp_int * b)
3125 { 3125 {
3126 int res; 3126 int res;
3146 * The library is free for all purposes without any express 3146 * The library is free for all purposes without any express
3147 * guarantee it works. 3147 * guarantee it works.
3148 * 3148 *
3149 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3149 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3150 */ 3150 */
3151 #include <tommath.h> 3151 #include <ltc_tommath.h>
3152 #include <stdarg.h> 3152 #include <stdarg.h>
3153 3153
3154 int mp_init_multi(mp_int *mp, ...) 3154 int mp_init_multi(mp_int *mp, ...)
3155 { 3155 {
3156 mp_err res = MP_OKAY; /* Assume ok until proven otherwise */ 3156 mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
3203 * The library is free for all purposes without any express 3203 * The library is free for all purposes without any express
3204 * guarantee it works. 3204 * guarantee it works.
3205 * 3205 *
3206 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3206 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3207 */ 3207 */
3208 #include <tommath.h> 3208 #include <ltc_tommath.h>
3209 3209
3210 /* initialize and set a digit */ 3210 /* initialize and set a digit */
3211 int mp_init_set (mp_int * a, mp_digit b) 3211 int mp_init_set (mp_int * a, mp_digit b)
3212 { 3212 {
3213 int err; 3213 int err;
3233 * The library is free for all purposes without any express 3233 * The library is free for all purposes without any express
3234 * guarantee it works. 3234 * guarantee it works.
3235 * 3235 *
3236 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3236 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3237 */ 3237 */
3238 #include <tommath.h> 3238 #include <ltc_tommath.h>
3239 3239
3240 /* initialize and set a digit */ 3240 /* initialize and set a digit */
3241 int mp_init_set_int (mp_int * a, unsigned long b) 3241 int mp_init_set_int (mp_int * a, unsigned long b)
3242 { 3242 {
3243 int err; 3243 int err;
3262 * The library is free for all purposes without any express 3262 * The library is free for all purposes without any express
3263 * guarantee it works. 3263 * guarantee it works.
3264 * 3264 *
3265 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3265 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3266 */ 3266 */
3267 #include <tommath.h> 3267 #include <ltc_tommath.h>
3268 3268
3269 /* init an mp_init for a given size */ 3269 /* init an mp_init for a given size */
3270 int mp_init_size (mp_int * a, int size) 3270 int mp_init_size (mp_int * a, int size)
3271 { 3271 {
3272 /* pad size so there are always extra digits */ 3272 /* pad size so there are always extra digits */
3299 * The library is free for all purposes without any express 3299 * The library is free for all purposes without any express
3300 * guarantee it works. 3300 * guarantee it works.
3301 * 3301 *
3302 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3302 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3303 */ 3303 */
3304 #include <tommath.h> 3304 #include <ltc_tommath.h>
3305 3305
3306 /* hac 14.61, pp608 */ 3306 /* hac 14.61, pp608 */
3307 int mp_invmod (mp_int * a, mp_int * b, mp_int * c) 3307 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
3308 { 3308 {
3309 mp_int x, y, u, v, A, B, C, D; 3309 mp_int x, y, u, v, A, B, C, D;
3477 * The library is free for all purposes without any express 3477 * The library is free for all purposes without any express
3478 * guarantee it works. 3478 * guarantee it works.
3479 * 3479 *
3480 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3480 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3481 */ 3481 */
3482 #include <tommath.h> 3482 #include <ltc_tommath.h>
3483 3483
3484 /* Check if remainders are possible squares - fast exclude non-squares */ 3484 /* Check if remainders are possible squares - fast exclude non-squares */
3485 static const char rem_128[128] = { 3485 static const char rem_128[128] = {
3486 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 3486 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
3487 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 3487 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
3584 * The library is free for all purposes without any express 3584 * The library is free for all purposes without any express
3585 * guarantee it works. 3585 * guarantee it works.
3586 * 3586 *
3587 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3587 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3588 */ 3588 */
3589 #include <tommath.h> 3589 #include <ltc_tommath.h>
3590 3590
3591 /* computes the jacobi c = (a | n) (or Legendre if n is prime) 3591 /* computes the jacobi c = (a | n) (or Legendre if n is prime)
3592 * HAC pp. 73 Algorithm 2.149 3592 * HAC pp. 73 Algorithm 2.149
3593 */ 3593 */
3594 int mp_jacobi (mp_int * a, mp_int * p, int *c) 3594 int mp_jacobi (mp_int * a, mp_int * p, int *c)
3687 * The library is free for all purposes without any express 3687 * The library is free for all purposes without any express
3688 * guarantee it works. 3688 * guarantee it works.
3689 * 3689 *
3690 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3690 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3691 */ 3691 */
3692 #include <tommath.h> 3692 #include <ltc_tommath.h>
3693 3693
3694 /* c = |a| * |b| using Karatsuba Multiplication using 3694 /* c = |a| * |b| using Karatsuba Multiplication using
3695 * three half size multiplications 3695 * three half size multiplications
3696 * 3696 *
3697 * Let B represent the radix [e.g. 2**DIGIT_BIT] and 3697 * Let B represent the radix [e.g. 2**DIGIT_BIT] and
3855 * The library is free for all purposes without any express 3855 * The library is free for all purposes without any express
3856 * guarantee it works. 3856 * guarantee it works.
3857 * 3857 *
3858 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3858 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3859 */ 3859 */
3860 #include <tommath.h> 3860 #include <ltc_tommath.h>
3861 3861
3862 /* Karatsuba squaring, computes b = a*a using three 3862 /* Karatsuba squaring, computes b = a*a using three
3863 * half size squarings 3863 * half size squarings
3864 * 3864 *
3865 * See comments of mp_karatsuba_mul for details. It 3865 * See comments of mp_karatsuba_mul for details. It
3974 * The library is free for all purposes without any express 3974 * The library is free for all purposes without any express
3975 * guarantee it works. 3975 * guarantee it works.
3976 * 3976 *
3977 * Tom St Denis, [email protected], http://math.libtomcrypt.org 3977 * Tom St Denis, [email protected], http://math.libtomcrypt.org
3978 */ 3978 */
3979 #include <tommath.h> 3979 #include <ltc_tommath.h>
3980 3980
3981 /* computes least common multiple as |a*b|/(a, b) */ 3981 /* computes least common multiple as |a*b|/(a, b) */
3982 int mp_lcm (mp_int * a, mp_int * b, mp_int * c) 3982 int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
3983 { 3983 {
3984 int res; 3984 int res;
4032 * The library is free for all purposes without any express 4032 * The library is free for all purposes without any express
4033 * guarantee it works. 4033 * guarantee it works.
4034 * 4034 *
4035 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4035 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4036 */ 4036 */
4037 #include <tommath.h> 4037 #include <ltc_tommath.h>
4038 4038
4039 /* shift left a certain amount of digits */ 4039 /* shift left a certain amount of digits */
4040 int mp_lshd (mp_int * a, int b) 4040 int mp_lshd (mp_int * a, int b)
4041 { 4041 {
4042 int x, res; 4042 int x, res;
4097 * The library is free for all purposes without any express 4097 * The library is free for all purposes without any express
4098 * guarantee it works. 4098 * guarantee it works.
4099 * 4099 *
4100 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4100 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4101 */ 4101 */
4102 #include <tommath.h> 4102 #include <ltc_tommath.h>
4103 4103
4104 /* c = a mod b, 0 <= c < b */ 4104 /* c = a mod b, 0 <= c < b */
4105 int 4105 int
4106 mp_mod (mp_int * a, mp_int * b, mp_int * c) 4106 mp_mod (mp_int * a, mp_int * b, mp_int * c)
4107 { 4107 {
4143 * The library is free for all purposes without any express 4143 * The library is free for all purposes without any express
4144 * guarantee it works. 4144 * guarantee it works.
4145 * 4145 *
4146 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4146 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4147 */ 4147 */
4148 #include <tommath.h> 4148 #include <ltc_tommath.h>
4149 4149
4150 /* calc a value mod 2**b */ 4150 /* calc a value mod 2**b */
4151 int 4151 int
4152 mp_mod_2d (mp_int * a, int b, mp_int * c) 4152 mp_mod_2d (mp_int * a, int b, mp_int * c)
4153 { 4153 {
4196 * The library is free for all purposes without any express 4196 * The library is free for all purposes without any express
4197 * guarantee it works. 4197 * guarantee it works.
4198 * 4198 *
4199 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4199 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4200 */ 4200 */
4201 #include <tommath.h> 4201 #include <ltc_tommath.h>
4202 4202
4203 int 4203 int
4204 mp_mod_d (mp_int * a, mp_digit b, mp_digit * c) 4204 mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
4205 { 4205 {
4206 return mp_div_d(a, b, NULL, c); 4206 return mp_div_d(a, b, NULL, c);
4221 * The library is free for all purposes without any express 4221 * The library is free for all purposes without any express
4222 * guarantee it works. 4222 * guarantee it works.
4223 * 4223 *
4224 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4224 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4225 */ 4225 */
4226 #include <tommath.h> 4226 #include <ltc_tommath.h>
4227 4227
4228 /* calculates a = B^n mod b for Montgomery reduction 4228 /* calculates a = B^n mod b for Montgomery reduction
4229 * Where B is the base [e.g. 2^DIGIT_BIT]. 4229 * Where B is the base [e.g. 2^DIGIT_BIT].
4230 * B^n mod b is computed by first computing 4230 * B^n mod b is computed by first computing
4231 * A = B^(n-1) which doesn't require a reduction but a simple OR. 4231 * A = B^(n-1) which doesn't require a reduction but a simple OR.
4278 * The library is free for all purposes without any express 4278 * The library is free for all purposes without any express
4279 * guarantee it works. 4279 * guarantee it works.
4280 * 4280 *
4281 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4281 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4282 */ 4282 */
4283 #include <tommath.h> 4283 #include <ltc_tommath.h>
4284 4284
4285 /* computes xR**-1 == x (mod N) via Montgomery Reduction */ 4285 /* computes xR**-1 == x (mod N) via Montgomery Reduction */
4286 int 4286 int
4287 mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) 4287 mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
4288 { 4288 {
4394 * The library is free for all purposes without any express 4394 * The library is free for all purposes without any express
4395 * guarantee it works. 4395 * guarantee it works.
4396 * 4396 *
4397 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4397 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4398 */ 4398 */
4399 #include <tommath.h> 4399 #include <ltc_tommath.h>
4400 4400
4401 /* setups the montgomery reduction stuff */ 4401 /* setups the montgomery reduction stuff */
4402 int 4402 int
4403 mp_montgomery_setup (mp_int * n, mp_digit * rho) 4403 mp_montgomery_setup (mp_int * n, mp_digit * rho)
4404 { 4404 {
4451 * The library is free for all purposes without any express 4451 * The library is free for all purposes without any express
4452 * guarantee it works. 4452 * guarantee it works.
4453 * 4453 *
4454 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4454 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4455 */ 4455 */
4456 #include <tommath.h> 4456 #include <ltc_tommath.h>
4457 4457
4458 /* high level multiplication (handles sign) */ 4458 /* high level multiplication (handles sign) */
4459 int mp_mul (mp_int * a, mp_int * b, mp_int * c) 4459 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
4460 { 4460 {
4461 int res, neg; 4461 int res, neg;
4503 * The library is free for all purposes without any express 4503 * The library is free for all purposes without any express
4504 * guarantee it works. 4504 * guarantee it works.
4505 * 4505 *
4506 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4506 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4507 */ 4507 */
4508 #include <tommath.h> 4508 #include <ltc_tommath.h>
4509 4509
4510 /* b = a*2 */ 4510 /* b = a*2 */
4511 int mp_mul_2(mp_int * a, mp_int * b) 4511 int mp_mul_2(mp_int * a, mp_int * b)
4512 { 4512 {
4513 int x, res, oldused; 4513 int x, res, oldused;
4583 * The library is free for all purposes without any express 4583 * The library is free for all purposes without any express
4584 * guarantee it works. 4584 * guarantee it works.
4585 * 4585 *
4586 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4586 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4587 */ 4587 */
4588 #include <tommath.h> 4588 #include <ltc_tommath.h>
4589 4589
4590 /* shift left by a certain bit count */ 4590 /* shift left by a certain bit count */
4591 int mp_mul_2d (mp_int * a, int b, mp_int * c) 4591 int mp_mul_2d (mp_int * a, int b, mp_int * c)
4592 { 4592 {
4593 mp_digit d; 4593 mp_digit d;
4666 * The library is free for all purposes without any express 4666 * The library is free for all purposes without any express
4667 * guarantee it works. 4667 * guarantee it works.
4668 * 4668 *
4669 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4669 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4670 */ 4670 */
4671 #include <tommath.h> 4671 #include <ltc_tommath.h>
4672 4672
4673 /* multiply by a digit */ 4673 /* multiply by a digit */
4674 int 4674 int
4675 mp_mul_d (mp_int * a, mp_digit b, mp_int * c) 4675 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
4676 { 4676 {
4742 * The library is free for all purposes without any express 4742 * The library is free for all purposes without any express
4743 * guarantee it works. 4743 * guarantee it works.
4744 * 4744 *
4745 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4745 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4746 */ 4746 */
4747 #include <tommath.h> 4747 #include <ltc_tommath.h>
4748 4748
4749 /* d = a * b (mod c) */ 4749 /* d = a * b (mod c) */
4750 int 4750 int
4751 mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) 4751 mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
4752 { 4752 {
4781 * The library is free for all purposes without any express 4781 * The library is free for all purposes without any express
4782 * guarantee it works. 4782 * guarantee it works.
4783 * 4783 *
4784 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4784 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4785 */ 4785 */
4786 #include <tommath.h> 4786 #include <ltc_tommath.h>
4787 4787
4788 /* find the n'th root of an integer 4788 /* find the n'th root of an integer
4789 * 4789 *
4790 * Result found such that (c)**b <= a and (c+1)**b > a 4790 * Result found such that (c)**b <= a and (c+1)**b > a
4791 * 4791 *
4911 * The library is free for all purposes without any express 4911 * The library is free for all purposes without any express
4912 * guarantee it works. 4912 * guarantee it works.
4913 * 4913 *
4914 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4914 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4915 */ 4915 */
4916 #include <tommath.h> 4916 #include <ltc_tommath.h>
4917 4917
4918 /* b = -a */ 4918 /* b = -a */
4919 int mp_neg (mp_int * a, mp_int * b) 4919 int mp_neg (mp_int * a, mp_int * b)
4920 { 4920 {
4921 int res; 4921 int res;
4943 * The library is free for all purposes without any express 4943 * The library is free for all purposes without any express
4944 * guarantee it works. 4944 * guarantee it works.
4945 * 4945 *
4946 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4946 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4947 */ 4947 */
4948 #include <tommath.h> 4948 #include <ltc_tommath.h>
4949 4949
4950 /* OR two ints together */ 4950 /* OR two ints together */
4951 int mp_or (mp_int * a, mp_int * b, mp_int * c) 4951 int mp_or (mp_int * a, mp_int * b, mp_int * c)
4952 { 4952 {
4953 int res, ix, px; 4953 int res, ix, px;
4991 * The library is free for all purposes without any express 4991 * The library is free for all purposes without any express
4992 * guarantee it works. 4992 * guarantee it works.
4993 * 4993 *
4994 * Tom St Denis, [email protected], http://math.libtomcrypt.org 4994 * Tom St Denis, [email protected], http://math.libtomcrypt.org
4995 */ 4995 */
4996 #include <tommath.h> 4996 #include <ltc_tommath.h>
4997 4997
4998 /* performs one Fermat test. 4998 /* performs one Fermat test.
4999 * 4999 *
5000 * If "a" were prime then b**a == b (mod a) since the order of 5000 * If "a" were prime then b**a == b (mod a) since the order of
5001 * the multiplicative sub-group would be phi(a) = a-1. That means 5001 * the multiplicative sub-group would be phi(a) = a-1. That means
5051 * The library is free for all purposes without any express 5051 * The library is free for all purposes without any express
5052 * guarantee it works. 5052 * guarantee it works.
5053 * 5053 *
5054 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5054 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5055 */ 5055 */
5056 #include <tommath.h> 5056 #include <ltc_tommath.h>
5057 5057
5058 /* determines if an integers is divisible by one 5058 /* determines if an integers is divisible by one
5059 * of the first PRIME_SIZE primes or not 5059 * of the first PRIME_SIZE primes or not
5060 * 5060 *
5061 * sets result to 0 if not, 1 if yes 5061 * sets result to 0 if not, 1 if yes
5099 * The library is free for all purposes without any express 5099 * The library is free for all purposes without any express
5100 * guarantee it works. 5100 * guarantee it works.
5101 * 5101 *
5102 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5102 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5103 */ 5103 */
5104 #include <tommath.h> 5104 #include <ltc_tommath.h>
5105 5105
5106 /* performs a variable number of rounds of Miller-Rabin 5106 /* performs a variable number of rounds of Miller-Rabin
5107 * 5107 *
5108 * Probability of error after t rounds is no more than 5108 * Probability of error after t rounds is no more than
5109 * (1/4)^t when 1 <= t <= PRIME_SIZE 5109 * (1/4)^t when 1 <= t <= PRIME_SIZE
5180 * The library is free for all purposes without any express 5180 * The library is free for all purposes without any express
5181 * guarantee it works. 5181 * guarantee it works.
5182 * 5182 *
5183 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5183 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5184 */ 5184 */
5185 #include <tommath.h> 5185 #include <ltc_tommath.h>
5186 5186
5187 /* Miller-Rabin test of "a" to the base of "b" as described in 5187 /* Miller-Rabin test of "a" to the base of "b" as described in
5188 * HAC pp. 139 Algorithm 4.24 5188 * HAC pp. 139 Algorithm 4.24
5189 * 5189 *
5190 * Sets result to 0 if definitely composite or 1 if probably prime. 5190 * Sets result to 0 if definitely composite or 1 if probably prime.
5281 * The library is free for all purposes without any express 5281 * The library is free for all purposes without any express
5282 * guarantee it works. 5282 * guarantee it works.
5283 * 5283 *
5284 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5284 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5285 */ 5285 */
5286 #include <tommath.h> 5286 #include <ltc_tommath.h>
5287 5287
5288 /* finds the next prime after the number "a" using "t" trials 5288 /* finds the next prime after the number "a" using "t" trials
5289 * of Miller-Rabin. 5289 * of Miller-Rabin.
5290 * 5290 *
5291 * bbs_style = 1 means the prime must be congruent to 3 mod 4 5291 * bbs_style = 1 means the prime must be congruent to 3 mod 4
5449 * The library is free for all purposes without any express 5449 * The library is free for all purposes without any express
5450 * guarantee it works. 5450 * guarantee it works.
5451 * 5451 *
5452 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5452 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5453 */ 5453 */
5454 #include <tommath.h> 5454 #include <ltc_tommath.h>
5455 5455
5456 /* makes a truly random prime of a given size (bits), 5456 /* makes a truly random prime of a given size (bits),
5457 * 5457 *
5458 * Flags are as follows: 5458 * Flags are as follows:
5459 * 5459 *
5571 * The library is free for all purposes without any express 5571 * The library is free for all purposes without any express
5572 * guarantee it works. 5572 * guarantee it works.
5573 * 5573 *
5574 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5574 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5575 */ 5575 */
5576 #include <tommath.h> 5576 #include <ltc_tommath.h>
5577 5577
5578 /* returns size of ASCII reprensentation */ 5578 /* returns size of ASCII reprensentation */
5579 int mp_radix_size (mp_int * a, int radix, int *size) 5579 int mp_radix_size (mp_int * a, int radix, int *size)
5580 { 5580 {
5581 int res, digs; 5581 int res, digs;
5640 * The library is free for all purposes without any express 5640 * The library is free for all purposes without any express
5641 * guarantee it works. 5641 * guarantee it works.
5642 * 5642 *
5643 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5643 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5644 */ 5644 */
5645 #include <tommath.h> 5645 #include <ltc_tommath.h>
5646 5646
5647 /* chars used in radix conversions */ 5647 /* chars used in radix conversions */
5648 const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; 5648 const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
5649 5649
5650 /* End: bn_mp_radix_smap.c */ 5650 /* End: bn_mp_radix_smap.c */
5662 * The library is free for all purposes without any express 5662 * The library is free for all purposes without any express
5663 * guarantee it works. 5663 * guarantee it works.
5664 * 5664 *
5665 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5665 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5666 */ 5666 */
5667 #include <tommath.h> 5667 #include <ltc_tommath.h>
5668 5668
5669 /* makes a pseudo-random int of a given size */ 5669 /* makes a pseudo-random int of a given size */
5670 int 5670 int
5671 mp_rand (mp_int * a, int digits) 5671 mp_rand (mp_int * a, int digits)
5672 { 5672 {
5715 * The library is free for all purposes without any express 5715 * The library is free for all purposes without any express
5716 * guarantee it works. 5716 * guarantee it works.
5717 * 5717 *
5718 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5718 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5719 */ 5719 */
5720 #include <tommath.h> 5720 #include <ltc_tommath.h>
5721 5721
5722 /* read a string [ASCII] in a given radix */ 5722 /* read a string [ASCII] in a given radix */
5723 int mp_read_radix (mp_int * a, char *str, int radix) 5723 int mp_read_radix (mp_int * a, char *str, int radix)
5724 { 5724 {
5725 int y, res, neg; 5725 int y, res, neg;
5795 * The library is free for all purposes without any express 5795 * The library is free for all purposes without any express
5796 * guarantee it works. 5796 * guarantee it works.
5797 * 5797 *
5798 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5798 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5799 */ 5799 */
5800 #include <tommath.h> 5800 #include <ltc_tommath.h>
5801 5801
5802 /* read signed bin, big endian, first byte is 0==positive or 1==negative */ 5802 /* read signed bin, big endian, first byte is 0==positive or 1==negative */
5803 int 5803 int
5804 mp_read_signed_bin (mp_int * a, unsigned char *b, int c) 5804 mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
5805 { 5805 {
5835 * The library is free for all purposes without any express 5835 * The library is free for all purposes without any express
5836 * guarantee it works. 5836 * guarantee it works.
5837 * 5837 *
5838 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5838 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5839 */ 5839 */
5840 #include <tommath.h> 5840 #include <ltc_tommath.h>
5841 5841
5842 /* reads a unsigned char array, assumes the msb is stored first [big endian] */ 5842 /* reads a unsigned char array, assumes the msb is stored first [big endian] */
5843 int 5843 int
5844 mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c) 5844 mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
5845 { 5845 {
5889 * The library is free for all purposes without any express 5889 * The library is free for all purposes without any express
5890 * guarantee it works. 5890 * guarantee it works.
5891 * 5891 *
5892 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5892 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5893 */ 5893 */
5894 #include <tommath.h> 5894 #include <ltc_tommath.h>
5895 5895
5896 /* reduces x mod m, assumes 0 < x < m**2, mu is 5896 /* reduces x mod m, assumes 0 < x < m**2, mu is
5897 * precomputed via mp_reduce_setup. 5897 * precomputed via mp_reduce_setup.
5898 * From HAC pp.604 Algorithm 14.42 5898 * From HAC pp.604 Algorithm 14.42
5899 */ 5899 */
5977 * The library is free for all purposes without any express 5977 * The library is free for all purposes without any express
5978 * guarantee it works. 5978 * guarantee it works.
5979 * 5979 *
5980 * Tom St Denis, [email protected], http://math.libtomcrypt.org 5980 * Tom St Denis, [email protected], http://math.libtomcrypt.org
5981 */ 5981 */
5982 #include <tommath.h> 5982 #include <ltc_tommath.h>
5983 5983
5984 /* reduces a modulo n where n is of the form 2**p - d */ 5984 /* reduces a modulo n where n is of the form 2**p - d */
5985 int 5985 int
5986 mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) 5986 mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
5987 { 5987 {
6037 * The library is free for all purposes without any express 6037 * The library is free for all purposes without any express
6038 * guarantee it works. 6038 * guarantee it works.
6039 * 6039 *
6040 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6040 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6041 */ 6041 */
6042 #include <tommath.h> 6042 #include <ltc_tommath.h>
6043 6043
6044 /* determines the setup value */ 6044 /* determines the setup value */
6045 int 6045 int
6046 mp_reduce_2k_setup(mp_int *a, mp_digit *d) 6046 mp_reduce_2k_setup(mp_int *a, mp_digit *d)
6047 { 6047 {
6083 * The library is free for all purposes without any express 6083 * The library is free for all purposes without any express
6084 * guarantee it works. 6084 * guarantee it works.
6085 * 6085 *
6086 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6086 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6087 */ 6087 */
6088 #include <tommath.h> 6088 #include <ltc_tommath.h>
6089 6089
6090 /* determines if mp_reduce_2k can be used */ 6090 /* determines if mp_reduce_2k can be used */
6091 int mp_reduce_is_2k(mp_int *a) 6091 int mp_reduce_is_2k(mp_int *a)
6092 { 6092 {
6093 int ix, iy, iz, iw; 6093 int ix, iy, iz, iw;
6132 * The library is free for all purposes without any express 6132 * The library is free for all purposes without any express
6133 * guarantee it works. 6133 * guarantee it works.
6134 * 6134 *
6135 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6135 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6136 */ 6136 */
6137 #include <tommath.h> 6137 #include <ltc_tommath.h>
6138 6138
6139 /* pre-calculate the value required for Barrett reduction 6139 /* pre-calculate the value required for Barrett reduction
6140 * For a given modulus "b" it calulates the value required in "a" 6140 * For a given modulus "b" it calulates the value required in "a"
6141 */ 6141 */
6142 int 6142 int
6165 * The library is free for all purposes without any express 6165 * The library is free for all purposes without any express
6166 * guarantee it works. 6166 * guarantee it works.
6167 * 6167 *
6168 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6168 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6169 */ 6169 */
6170 #include <tommath.h> 6170 #include <ltc_tommath.h>
6171 6171
6172 /* shift right a certain amount of digits */ 6172 /* shift right a certain amount of digits */
6173 void mp_rshd (mp_int * a, int b) 6173 void mp_rshd (mp_int * a, int b)
6174 { 6174 {
6175 int x; 6175 int x;
6235 * The library is free for all purposes without any express 6235 * The library is free for all purposes without any express
6236 * guarantee it works. 6236 * guarantee it works.
6237 * 6237 *
6238 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6238 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6239 */ 6239 */
6240 #include <tommath.h> 6240 #include <ltc_tommath.h>
6241 6241
6242 /* set to a digit */ 6242 /* set to a digit */
6243 void mp_set (mp_int * a, mp_digit b) 6243 void mp_set (mp_int * a, mp_digit b)
6244 { 6244 {
6245 mp_zero (a); 6245 mp_zero (a);
6262 * The library is free for all purposes without any express 6262 * The library is free for all purposes without any express
6263 * guarantee it works. 6263 * guarantee it works.
6264 * 6264 *
6265 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6265 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6266 */ 6266 */
6267 #include <tommath.h> 6267 #include <ltc_tommath.h>
6268 6268
6269 /* set a 32-bit const */ 6269 /* set a 32-bit const */
6270 int mp_set_int (mp_int * a, unsigned long b) 6270 int mp_set_int (mp_int * a, unsigned long b)
6271 { 6271 {
6272 int x, res; 6272 int x, res;
6308 * The library is free for all purposes without any express 6308 * The library is free for all purposes without any express
6309 * guarantee it works. 6309 * guarantee it works.
6310 * 6310 *
6311 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6311 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6312 */ 6312 */
6313 #include <tommath.h> 6313 #include <ltc_tommath.h>
6314 6314
6315 /* shrink a bignum */ 6315 /* shrink a bignum */
6316 int mp_shrink (mp_int * a) 6316 int mp_shrink (mp_int * a)
6317 { 6317 {
6318 mp_digit *tmp; 6318 mp_digit *tmp;
6341 * The library is free for all purposes without any express 6341 * The library is free for all purposes without any express
6342 * guarantee it works. 6342 * guarantee it works.
6343 * 6343 *
6344 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6344 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6345 */ 6345 */
6346 #include <tommath.h> 6346 #include <ltc_tommath.h>
6347 6347
6348 /* get the size for an signed equivalent */ 6348 /* get the size for an signed equivalent */
6349 int mp_signed_bin_size (mp_int * a) 6349 int mp_signed_bin_size (mp_int * a)
6350 { 6350 {
6351 return 1 + mp_unsigned_bin_size (a); 6351 return 1 + mp_unsigned_bin_size (a);
6366 * The library is free for all purposes without any express 6366 * The library is free for all purposes without any express
6367 * guarantee it works. 6367 * guarantee it works.
6368 * 6368 *
6369 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6369 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6370 */ 6370 */
6371 #include <tommath.h> 6371 #include <ltc_tommath.h>
6372 6372
6373 /* computes b = a*a */ 6373 /* computes b = a*a */
6374 int 6374 int
6375 mp_sqr (mp_int * a, mp_int * b) 6375 mp_sqr (mp_int * a, mp_int * b)
6376 { 6376 {
6411 * The library is free for all purposes without any express 6411 * The library is free for all purposes without any express
6412 * guarantee it works. 6412 * guarantee it works.
6413 * 6413 *
6414 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6414 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6415 */ 6415 */
6416 #include <tommath.h> 6416 #include <ltc_tommath.h>
6417 6417
6418 /* c = a * a (mod b) */ 6418 /* c = a * a (mod b) */
6419 int 6419 int
6420 mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) 6420 mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
6421 { 6421 {
6450 * The library is free for all purposes without any express 6450 * The library is free for all purposes without any express
6451 * guarantee it works. 6451 * guarantee it works.
6452 * 6452 *
6453 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6453 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6454 */ 6454 */
6455 #include <tommath.h> 6455 #include <ltc_tommath.h>
6456 6456
6457 /* this function is less generic than mp_n_root, simpler and faster */ 6457 /* this function is less generic than mp_n_root, simpler and faster */
6458 int mp_sqrt(mp_int *arg, mp_int *ret) 6458 int mp_sqrt(mp_int *arg, mp_int *ret)
6459 { 6459 {
6460 int res; 6460 int res;
6529 * The library is free for all purposes without any express 6529 * The library is free for all purposes without any express
6530 * guarantee it works. 6530 * guarantee it works.
6531 * 6531 *
6532 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6532 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6533 */ 6533 */
6534 #include <tommath.h> 6534 #include <ltc_tommath.h>
6535 6535
6536 /* high level subtraction (handles signs) */ 6536 /* high level subtraction (handles signs) */
6537 int 6537 int
6538 mp_sub (mp_int * a, mp_int * b, mp_int * c) 6538 mp_sub (mp_int * a, mp_int * b, mp_int * c)
6539 { 6539 {
6586 * The library is free for all purposes without any express 6586 * The library is free for all purposes without any express
6587 * guarantee it works. 6587 * guarantee it works.
6588 * 6588 *
6589 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6589 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6590 */ 6590 */
6591 #include <tommath.h> 6591 #include <ltc_tommath.h>
6592 6592
6593 /* single digit subtraction */ 6593 /* single digit subtraction */
6594 int 6594 int
6595 mp_sub_d (mp_int * a, mp_digit b, mp_int * c) 6595 mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
6596 { 6596 {
6673 * The library is free for all purposes without any express 6673 * The library is free for all purposes without any express
6674 * guarantee it works. 6674 * guarantee it works.
6675 * 6675 *
6676 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6676 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6677 */ 6677 */
6678 #include <tommath.h> 6678 #include <ltc_tommath.h>
6679 6679
6680 /* d = a - b (mod c) */ 6680 /* d = a - b (mod c) */
6681 int 6681 int
6682 mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) 6682 mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
6683 { 6683 {
6713 * The library is free for all purposes without any express 6713 * The library is free for all purposes without any express
6714 * guarantee it works. 6714 * guarantee it works.
6715 * 6715 *
6716 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6716 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6717 */ 6717 */
6718 #include <tommath.h> 6718 #include <ltc_tommath.h>
6719 6719
6720 /* store in signed [big endian] format */ 6720 /* store in signed [big endian] format */
6721 int 6721 int
6722 mp_to_signed_bin (mp_int * a, unsigned char *b) 6722 mp_to_signed_bin (mp_int * a, unsigned char *b)
6723 { 6723 {
6745 * The library is free for all purposes without any express 6745 * The library is free for all purposes without any express
6746 * guarantee it works. 6746 * guarantee it works.
6747 * 6747 *
6748 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6748 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6749 */ 6749 */
6750 #include <tommath.h> 6750 #include <ltc_tommath.h>
6751 6751
6752 /* store in unsigned [big endian] format */ 6752 /* store in unsigned [big endian] format */
6753 int 6753 int
6754 mp_to_unsigned_bin (mp_int * a, unsigned char *b) 6754 mp_to_unsigned_bin (mp_int * a, unsigned char *b)
6755 { 6755 {
6792 * The library is free for all purposes without any express 6792 * The library is free for all purposes without any express
6793 * guarantee it works. 6793 * guarantee it works.
6794 * 6794 *
6795 * Tom St Denis, [email protected], http://math.libtomcrypt.org 6795 * Tom St Denis, [email protected], http://math.libtomcrypt.org
6796 */ 6796 */
6797 #include <tommath.h> 6797 #include <ltc_tommath.h>
6798 6798
6799 /* multiplication using the Toom-Cook 3-way algorithm */ 6799 /* multiplication using the Toom-Cook 3-way algorithm */
6800 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) 6800 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
6801 { 6801 {
6802 mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2; 6802 mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
7068 * The library is free for all purposes without any express 7068 * The library is free for all purposes without any express
7069 * guarantee it works. 7069 * guarantee it works.
7070 * 7070 *
7071 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7071 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7072 */ 7072 */
7073 #include <tommath.h> 7073 #include <ltc_tommath.h>
7074 7074
7075 /* squaring using Toom-Cook 3-way algorithm */ 7075 /* squaring using Toom-Cook 3-way algorithm */
7076 int 7076 int
7077 mp_toom_sqr(mp_int *a, mp_int *b) 7077 mp_toom_sqr(mp_int *a, mp_int *b)
7078 { 7078 {
7292 * The library is free for all purposes without any express 7292 * The library is free for all purposes without any express
7293 * guarantee it works. 7293 * guarantee it works.
7294 * 7294 *
7295 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7295 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7296 */ 7296 */
7297 #include <tommath.h> 7297 #include <ltc_tommath.h>
7298 7298
7299 /* stores a bignum as a ASCII string in a given radix (2..64) */ 7299 /* stores a bignum as a ASCII string in a given radix (2..64) */
7300 int mp_toradix (mp_int * a, char *str, int radix) 7300 int mp_toradix (mp_int * a, char *str, int radix)
7301 { 7301 {
7302 int res, digs; 7302 int res, digs;
7365 * The library is free for all purposes without any express 7365 * The library is free for all purposes without any express
7366 * guarantee it works. 7366 * guarantee it works.
7367 * 7367 *
7368 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7368 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7369 */ 7369 */
7370 #include <tommath.h> 7370 #include <ltc_tommath.h>
7371 7371
7372 /* stores a bignum as a ASCII string in a given radix (2..64) 7372 /* stores a bignum as a ASCII string in a given radix (2..64)
7373 * 7373 *
7374 * Stores upto maxlen-1 chars and always a NULL byte 7374 * Stores upto maxlen-1 chars and always a NULL byte
7375 */ 7375 */
7452 * The library is free for all purposes without any express 7452 * The library is free for all purposes without any express
7453 * guarantee it works. 7453 * guarantee it works.
7454 * 7454 *
7455 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7455 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7456 */ 7456 */
7457 #include <tommath.h> 7457 #include <ltc_tommath.h>
7458 7458
7459 /* get the size for an unsigned equivalent */ 7459 /* get the size for an unsigned equivalent */
7460 int 7460 int
7461 mp_unsigned_bin_size (mp_int * a) 7461 mp_unsigned_bin_size (mp_int * a)
7462 { 7462 {
7479 * The library is free for all purposes without any express 7479 * The library is free for all purposes without any express
7480 * guarantee it works. 7480 * guarantee it works.
7481 * 7481 *
7482 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7482 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7483 */ 7483 */
7484 #include <tommath.h> 7484 #include <ltc_tommath.h>
7485 7485
7486 /* XOR two ints together */ 7486 /* XOR two ints together */
7487 int 7487 int
7488 mp_xor (mp_int * a, mp_int * b, mp_int * c) 7488 mp_xor (mp_int * a, mp_int * b, mp_int * c)
7489 { 7489 {
7528 * The library is free for all purposes without any express 7528 * The library is free for all purposes without any express
7529 * guarantee it works. 7529 * guarantee it works.
7530 * 7530 *
7531 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7531 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7532 */ 7532 */
7533 #include <tommath.h> 7533 #include <ltc_tommath.h>
7534 7534
7535 /* set to zero */ 7535 /* set to zero */
7536 void 7536 void
7537 mp_zero (mp_int * a) 7537 mp_zero (mp_int * a)
7538 { 7538 {
7556 * The library is free for all purposes without any express 7556 * The library is free for all purposes without any express
7557 * guarantee it works. 7557 * guarantee it works.
7558 * 7558 *
7559 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7559 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7560 */ 7560 */
7561 #include <tommath.h> 7561 #include <ltc_tommath.h>
7562 7562
7563 /* this table gives the # of rabin miller trials for a prob of failure lower than 2^-96 */ 7563 /* this table gives the # of rabin miller trials for a prob of failure lower than 2^-96 */
7564 static const struct { 7564 static const struct {
7565 int k, t; 7565 int k, t;
7566 } sizes[] = { 7566 } sizes[] = {
7611 * The library is free for all purposes without any express 7611 * The library is free for all purposes without any express
7612 * guarantee it works. 7612 * guarantee it works.
7613 * 7613 *
7614 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7614 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7615 */ 7615 */
7616 #include <tommath.h> 7616 #include <ltc_tommath.h>
7617 const mp_digit __prime_tab[] = { 7617 const mp_digit __prime_tab[] = {
7618 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, 7618 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
7619 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, 7619 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
7620 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059, 7620 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
7621 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 7621 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
7670 * The library is free for all purposes without any express 7670 * The library is free for all purposes without any express
7671 * guarantee it works. 7671 * guarantee it works.
7672 * 7672 *
7673 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7673 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7674 */ 7674 */
7675 #include <tommath.h> 7675 #include <ltc_tommath.h>
7676 7676
7677 /* reverse an array, used for radix code */ 7677 /* reverse an array, used for radix code */
7678 void 7678 void
7679 bn_reverse (unsigned char *s, int len) 7679 bn_reverse (unsigned char *s, int len)
7680 { 7680 {
7707 * The library is free for all purposes without any express 7707 * The library is free for all purposes without any express
7708 * guarantee it works. 7708 * guarantee it works.
7709 * 7709 *
7710 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7710 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7711 */ 7711 */
7712 #include <tommath.h> 7712 #include <ltc_tommath.h>
7713 7713
7714 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ 7714 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
7715 int 7715 int
7716 s_mp_add (mp_int * a, mp_int * b, mp_int * c) 7716 s_mp_add (mp_int * a, mp_int * b, mp_int * c)
7717 { 7717 {
7814 * The library is free for all purposes without any express 7814 * The library is free for all purposes without any express
7815 * guarantee it works. 7815 * guarantee it works.
7816 * 7816 *
7817 * Tom St Denis, [email protected], http://math.libtomcrypt.org 7817 * Tom St Denis, [email protected], http://math.libtomcrypt.org
7818 */ 7818 */
7819 #include <tommath.h> 7819 #include <ltc_tommath.h>
7820 7820
7821 #ifdef MP_LOW_MEM 7821 #ifdef MP_LOW_MEM
7822 #define TAB_SIZE 32 7822 #define TAB_SIZE 32
7823 #else 7823 #else
7824 #define TAB_SIZE 256 7824 #define TAB_SIZE 256
8052 * The library is free for all purposes without any express 8052 * The library is free for all purposes without any express
8053 * guarantee it works. 8053 * guarantee it works.
8054 * 8054 *
8055 * Tom St Denis, [email protected], http://math.libtomcrypt.org 8055 * Tom St Denis, [email protected], http://math.libtomcrypt.org
8056 */ 8056 */
8057 #include <tommath.h> 8057 #include <ltc_tommath.h>
8058 8058
8059 /* multiplies |a| * |b| and only computes upto digs digits of result 8059 /* multiplies |a| * |b| and only computes upto digs digits of result
8060 * HAC pp. 595, Algorithm 14.12 Modified so you can control how 8060 * HAC pp. 595, Algorithm 14.12 Modified so you can control how
8061 * many digits of output are created. 8061 * many digits of output are created.
8062 */ 8062 */
8141 * The library is free for all purposes without any express 8141 * The library is free for all purposes without any express
8142 * guarantee it works. 8142 * guarantee it works.
8143 * 8143 *
8144 * Tom St Denis, [email protected], http://math.libtomcrypt.org 8144 * Tom St Denis, [email protected], http://math.libtomcrypt.org
8145 */ 8145 */
8146 #include <tommath.h> 8146 #include <ltc_tommath.h>
8147 8147
8148 /* multiplies |a| * |b| and does not compute the lower digs digits 8148 /* multiplies |a| * |b| and does not compute the lower digs digits
8149 * [meant to get the higher part of the product] 8149 * [meant to get the higher part of the product]
8150 */ 8150 */
8151 int 8151 int
8218 * The library is free for all purposes without any express 8218 * The library is free for all purposes without any express
8219 * guarantee it works. 8219 * guarantee it works.
8220 * 8220 *
8221 * Tom St Denis, [email protected], http://math.libtomcrypt.org 8221 * Tom St Denis, [email protected], http://math.libtomcrypt.org
8222 */ 8222 */
8223 #include <tommath.h> 8223 #include <ltc_tommath.h>
8224 8224
8225 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ 8225 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
8226 int 8226 int
8227 s_mp_sqr (mp_int * a, mp_int * b) 8227 s_mp_sqr (mp_int * a, mp_int * b)
8228 { 8228 {
8301 * The library is free for all purposes without any express 8301 * The library is free for all purposes without any express
8302 * guarantee it works. 8302 * guarantee it works.
8303 * 8303 *
8304 * Tom St Denis, [email protected], http://math.libtomcrypt.org 8304 * Tom St Denis, [email protected], http://math.libtomcrypt.org
8305 */ 8305 */
8306 #include <tommath.h> 8306 #include <ltc_tommath.h>
8307 8307
8308 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ 8308 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
8309 int 8309 int
8310 s_mp_sub (mp_int * a, mp_int * b, mp_int * c) 8310 s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
8311 { 8311 {
8388 * The library is free for all purposes without any express 8388 * The library is free for all purposes without any express
8389 * guarantee it works. 8389 * guarantee it works.
8390 * 8390 *
8391 * Tom St Denis, [email protected], http://math.libtomcrypt.org 8391 * Tom St Denis, [email protected], http://math.libtomcrypt.org
8392 */ 8392 */
8393 #include <tommath.h> 8393 #include <ltc_tommath.h>
8394 8394
8395 /* Known optimal configurations 8395 /* Known optimal configurations
8396 8396
8397 CPU /Compiler /MUL CUTOFF/SQR CUTOFF 8397 CPU /Compiler /MUL CUTOFF/SQR CUTOFF
8398 ------------------------------------------------------------- 8398 -------------------------------------------------------------
8399 Intel P4 /GCC v3.2 / 70/ 108 8399 Intel P4 Northwood /GCC v3.3.3 / 59/ 81/profiled build
8400 AMD Athlon XP /GCC v3.2 / 109/ 127 8400 Intel P4 Northwood /GCC v3.3.3 / 59/ 80/profiled_single build
8401 8401 Intel P4 Northwood /ICC v8.0 / 57/ 70/profiled build
8402 Intel P4 Northwood /ICC v8.0 / 54/ 76/profiled_single build
8403 AMD Athlon XP /GCC v3.2 / 109/ 127/
8404
8402 */ 8405 */
8403 8406
8404 /* configured for a AMD XP Thoroughbred core with etc/tune.c */ 8407 int KARATSUBA_MUL_CUTOFF = 57, /* Min. number of digits before Karatsuba multiplication is used. */
8405 int KARATSUBA_MUL_CUTOFF = 70, /* Min. number of digits before Karatsuba multiplication is used. */ 8408 KARATSUBA_SQR_CUTOFF = 70, /* Min. number of digits before Karatsuba squaring is used. */
8406 KARATSUBA_SQR_CUTOFF = 108, /* Min. number of digits before Karatsuba squaring is used. */
8407 8409
8408 TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */ 8410 TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
8409 TOOM_SQR_CUTOFF = 400; 8411 TOOM_SQR_CUTOFF = 400;
8410 8412
8411 /* End: bncore.c */ 8413 /* End: bncore.c */