Permalink
Browse files

added libtommath-0.25

  • Loading branch information...
1 parent 03cc01b commit c1da6aa2de997d954d745ce67dee3598ed6ee71f Tom St Denis committed with sjaeckel Aug 5, 2003
Showing with 2,945 additions and 2,593 deletions.
  1. +4 −0 LICENSE
  2. BIN bn.pdf
  3. +1 −1 bn.tex
  4. +41 −0 bn_error.c
  5. +10 −11 bn_fast_mp_invmod.c
  6. +22 −3 bn_fast_mp_montgomery_reduce.c
  7. +2 −2 bn_fast_s_mp_mul_digs.c
  8. +3 −3 bn_fast_s_mp_mul_high_digs.c
  9. +19 −8 bn_fast_s_mp_sqr.c
  10. +9 −2 bn_mp_2expt.c
  11. +11 −4 bn_mp_abs.c
  12. +2 −2 bn_mp_add.c
  13. +7 −3 bn_mp_add_d.c
  14. +2 −2 bn_mp_addmod.c
  15. +2 −2 bn_mp_and.c
  16. +7 −2 bn_mp_clamp.c
  17. +5 −3 bn_mp_clear.c
  18. +28 −0 bn_mp_clear_multi.c
  19. +2 −2 bn_mp_cmp.c
  20. +5 −3 bn_mp_cmp_d.c
  21. +2 −2 bn_mp_cmp_mag.c
  22. +3 −2 bn_mp_cnt_lsb.c
  23. +12 −4 bn_mp_copy.c
  24. +2 −2 bn_mp_count_bits.c
  25. +2 −2 bn_mp_div.c
  26. +2 −2 bn_mp_div_2.c
  27. +2 −2 bn_mp_div_2d.c
  28. +14 −5 bn_mp_div_3.c
  29. +2 −2 bn_mp_div_d.c
  30. +37 −34 bn_mp_dr_is_modulus.c
  31. +3 −3 bn_mp_dr_reduce.c
  32. +26 −26 bn_mp_dr_setup.c
  33. +3 −3 bn_mp_exch.c
  34. +2 −2 bn_mp_expt_d.c
  35. +6 −2 bn_mp_exptmod.c
  36. +21 −12 bn_mp_exptmod_fast.c
  37. +61 −61 bn_mp_fread.c
  38. +2 −2 bn_mp_fwrite.c
  39. +23 −14 bn_mp_gcd.c
  40. +3 −3 bn_mp_grow.c
  41. +4 −4 bn_mp_init.c
  42. +2 −2 bn_mp_init_copy.c
  43. +3 −15 bn_mp_multi.c → bn_mp_init_multi.c
  44. +6 −7 bn_mp_init_size.c
  45. +2 −2 bn_mp_invmod.c
  46. +12 −8 bn_mp_jacobi.c
  47. +2 −2 bn_mp_karatsuba_mul.c
  48. +2 −2 bn_mp_karatsuba_sqr.c
  49. +2 −2 bn_mp_lcm.c
  50. +2 −2 bn_mp_lshd.c
  51. +2 −2 bn_mp_mod.c
  52. +2 −3 bn_mp_mod_2d.c
  53. +2 −2 bn_mp_mod_d.c
  54. +2 −2 bn_mp_montgomery_calc_normalization.c
  55. +2 −2 bn_mp_montgomery_reduce.c
  56. +2 −2 bn_mp_montgomery_setup.c
  57. +5 −4 bn_mp_mul.c
  58. +3 −3 bn_mp_mul_2.c
  59. +3 −3 bn_mp_mul_2d.c
  60. +2 −2 bn_mp_mul_d.c
  61. +2 −2 bn_mp_mulmod.c
  62. +3 −3 bn_mp_n_root.c
  63. +2 −2 bn_mp_neg.c
  64. +2 −2 bn_mp_or.c
  65. +3 −3 bn_mp_prime_fermat.c
  66. +2 −2 bn_mp_prime_is_divisible.c
  67. +2 −2 bn_mp_prime_is_prime.c
  68. +2 −2 bn_mp_prime_miller_rabin.c
  69. +5 −7 bn_mp_prime_next_prime.c
  70. +54 −54 bn_mp_radix_size.c
  71. +18 −18 bn_mp_radix_smap.c
  72. +2 −2 bn_mp_rand.c
  73. +77 −77 bn_mp_read_radix.c
  74. +2 −2 bn_mp_read_signed_bin.c
  75. +2 −2 bn_mp_read_unsigned_bin.c
  76. +2 −2 bn_mp_reduce.c
  77. +56 −56 bn_mp_reduce_2k.c
  78. +42 −42 bn_mp_reduce_2k_setup.c
  79. +38 −38 bn_mp_reduce_is_2k.c
  80. +29 −29 bn_mp_reduce_setup.c
  81. +2 −2 bn_mp_rshd.c
  82. +3 −3 bn_mp_set.c
  83. +2 −2 bn_mp_set_int.c
  84. +2 −2 bn_mp_shrink.c
  85. +2 −2 bn_mp_signed_bin_size.c
  86. +5 −4 bn_mp_sqr.c
  87. +2 −3 bn_mp_sqrmod.c
  88. +2 −2 bn_mp_sub.c
  89. +7 −3 bn_mp_sub_d.c
  90. +2 −2 bn_mp_submod.c
  91. +2 −2 bn_mp_to_signed_bin.c
  92. +2 −2 bn_mp_to_unsigned_bin.c
  93. +273 −273 bn_mp_toom_mul.c
  94. +220 −220 bn_mp_toom_sqr.c
  95. +2 −2 bn_mp_toradix.c
  96. +2 −2 bn_mp_unsigned_bin_size.c
  97. +2 −2 bn_mp_xor.c
  98. +2 −2 bn_mp_zero.c
  99. +2 −2 bn_prime_tab.c
  100. +2 −2 bn_reverse.c
  101. +2 −2 bn_s_mp_add.c
  102. +13 −8 bn_s_mp_exptmod.c
  103. +2 −2 bn_s_mp_mul_digs.c
  104. +2 −2 bn_s_mp_mul_high_digs.c
  105. +2 −2 bn_s_mp_sqr.c
  106. +2 −2 bn_s_mp_sub.c
  107. +2 −2 bncore.c
  108. +13 −0 changes.txt
  109. +8 −8 demo/demo.c
  110. +2 −1 etc/2kprime.1
  111. +0 −1 etc/pprime.c
  112. +0 −17 logs/mult.log
  113. +9 −5 makefile
  114. +5 −4 makefile.bcc
  115. +3 −2 makefile.msvc
  116. BIN pics/design_process.sxd
  117. +34 −29 pics/makefile
  118. BIN poster.pdf
  119. +1,482 −1,309 pre_gen/mpi.c
  120. BIN tdcal.pdf
  121. +9 −7 tommath.h
View
@@ -0,0 +1,4 @@
+LibTomMath is hereby released into the Public Domain.
+
+-- Tom St Denis
+
View
BIN bn.pdf
Binary file not shown.
View
2 bn.tex
@@ -1,7 +1,7 @@
\documentclass[]{article}
\begin{document}
-\title{LibTomMath v0.24 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
+\title{LibTomMath v0.25 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
\newpage
View
@@ -0,0 +1,41 @@
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ */
+#include <tommath.h>
+
+static const struct {
+ int code;
+ char *msg;
+} msgs[] = {
+ { MP_OKAY, "Successful" },
+ { MP_MEM, "Out of heap" },
+ { MP_VAL, "Value out of range" }
+};
+
+/* return a char * string for a given code */
+char *mp_error_to_string(int code)
+{
+ int x;
+
+ /* scan the lookup table for the given message */
+ for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
+ if (msgs[x].code == code) {
+ return msgs[x].msg;
+ }
+ }
+
+ /* generic reply for invalid code */
+ return "Invalid error code";
+}
+
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -26,6 +26,14 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
mp_int x, y, u, v, B, D;
int res, neg;
+ /* 2. [modified] if a,b are both even then return an error!
+ *
+ * That is if gcd(a,b) = 2**k * q then obviously there is no inverse.
+ */
+ if (mp_iseven (a) == 1 && mp_iseven (b) == 1) {
+ return MP_VAL;
+ }
+
/* init all our temps */
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
return res;
@@ -41,15 +49,6 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
goto __ERR;
}
- /* 2. [modified] if x,y are both even then return an error!
- *
- * That is if gcd(x,y) = 2 * k then obviously there is no inverse.
- */
- if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
- res = MP_VAL;
- goto __ERR;
- }
-
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto __ERR;
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -38,6 +38,9 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
}
}
+ /* first we have to get the digits of the input into
+ * an array of double precision words W[...]
+ */
{
register mp_word *_W;
register mp_digit *tmpx;
@@ -56,6 +59,9 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
}
}
+ /* now we proceed to zero successive digits
+ * from the least significant upwards
+ */
for (ix = 0; ix < n->used; ix++) {
/* mu = ai * m' mod b
*
@@ -101,12 +107,20 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
}
+ /* now we have to propagate the carries and
+ * shift the words downward [all those least
+ * significant digits we zeroed].
+ */
{
register mp_digit *tmpx;
register mp_word *_W, *_W1;
/* nox fix rest of carries */
+
+ /* alias for current word */
_W1 = W + ix;
+
+ /* alias for next word, where the carry goes */
_W = W + ++ix;
for (; ix <= n->used * 2 + 1; ix++) {
@@ -119,15 +133,20 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* array of mp_word to mp_digit than calling mp_rshd
* we just copy them in the right order
*/
+
+ /* alias for destination word */
tmpx = x->dp;
+
+ /* alias for shifted double precision result */
_W = W + n->used;
for (ix = 0; ix < n->used + 1; ix++) {
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
}
/* zero oldused digits, if the input a was larger than
- * m->used+1 we'll have to clear the digits */
+ * m->used+1 we'll have to clear the digits
+ */
for (; ix < olduse; ix++) {
*tmpx++ = 0;
}
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -12,7 +12,7 @@
*
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
-#include <tommath.h>
+ #include <tommath.h>
/* this is a modified version of fast_s_mp_mul_digs that only produces
* output digits *above* digs. See the comments for fast_s_mp_mul_digs
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -48,14 +48,14 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
/* zero temp buffer (columns)
* Note that there are two buffers. Since squaring requires
- * a outter and inner product and the inner product requires
+ * a outer and inner product and the inner product requires
* computing a product and doubling it (a relatively expensive
* op to perform n**2 times if you don't have to) the inner and
* outer products are computed in different buffers. This way
* the inner product can be doubled using n doublings instead of
* n**2
*/
- memset (W, 0, newused * sizeof (mp_word));
+ memset (W, 0, newused * sizeof (mp_word));
memset (W2, 0, newused * sizeof (mp_word));
/* This computes the inner product. To simplify the inner N**2 loop
@@ -67,6 +67,7 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
* Note that every outer product is computed
* for a particular column only once which means that
* there is no need todo a double precision addition
+ * into the W2[] array.
*/
W2[ix + ix] = ((mp_word)a->dp[ix]) * ((mp_word)a->dp[ix]);
@@ -95,7 +96,12 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
olduse = b->used;
b->used = newused;
- /* now compute digits */
+ /* now compute digits
+ *
+ * We have to double the inner product sums, add in the
+ * outer product sums, propagate carries and convert
+ * to single precision.
+ */
{
register mp_digit *tmpb;
@@ -109,16 +115,21 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
/* double/add next digit */
W[ix] += W[ix] + W2[ix];
+ /* propagate carry forwards [from the previous digit] */
W[ix] = W[ix] + (W[ix - 1] >> ((mp_word) DIGIT_BIT));
+
+ /* store the current digit now that the carry isn't
+ * needed
+ */
*tmpb++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
}
- /* set the last value. Note even if the carry is zero
- * this is required since the next step will not zero
+ /* set the last value. Note even if the carry is zero
+ * this is required since the next step will not zero
* it if b originally had a value at b->dp[2*a.used]
*/
*tmpb++ = (mp_digit) (W[(newused) - 1] & ((mp_word) MP_MASK));
- /* clear high digits */
+ /* clear high digits of b if there were any originally */
for (; ix < olduse; ix++) {
*tmpb++ = 0;
}
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -24,11 +24,18 @@ mp_2expt (mp_int * a, int b)
{
int res;
+ /* zero a as per default */
mp_zero (a);
+
+ /* grow a to accomodate the single bit */
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
return res;
}
+
+ /* set the used count of where the bit will go */
a->used = b / DIGIT_BIT + 1;
+
+ /* put the single bit in its place */
a->dp[b / DIGIT_BIT] = 1 << (b % DIGIT_BIT);
return MP_OKAY;
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -22,9 +22,16 @@ int
mp_abs (mp_int * a, mp_int * b)
{
int res;
- if ((res = mp_copy (a, b)) != MP_OKAY) {
- return res;
+
+ /* copy a to b */
+ if (a != b) {
+ if ((res = mp_copy (a, b)) != MP_OKAY) {
+ return res;
+ }
}
+
+ /* force the sign of b to positive */
b->sign = MP_ZPOS;
+
return MP_OKAY;
}
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
@@ -82,7 +82,11 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
c->used = 1;
/* the result is a single digit */
- *tmpc++ = b - a->dp[0];
+ if (a->used == 1) {
+ *tmpc++ = b - a->dp[0];
+ } else {
+ *tmpc++ = b;
+ }
/* setup count so the clearing of oldused
* can fall through correctly
View
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
- * LibTomMath is library that provides for multiple-precision
+ * LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
- * The library is designed directly after the MPI library by
+ * The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Oops, something went wrong.

0 comments on commit c1da6aa

Please sign in to comment.