Check-in [e024610976]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Import of libtommath 0.36
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e024610976f35de473eebd5ea1dd6d5d8100df58
User & Date: kennykb 2005-09-26 16:31:55.000
Context
2005-09-26
16:53
re-import of three damaged PDF's check-in: 770751cc4a user: kennykb tags: trunk
16:31
Import of libtommath 0.36 check-in: e024610976 user: kennykb tags: trunk
2005-09-16
01:40
silence compiler warning check-in: a95dcb3914 user: dgp tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to libtommath/bn.pdf.

cannot compute difference between binary files

Changes to libtommath/bn.tex.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
\newcommand{\emailaddr}[1]{\mbox{$<${#1}$>$}}
\def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}}
\def\gap{\vspace{0.5ex}}
\makeindex
\begin{document}
\frontmatter
\pagestyle{empty}
\title{LibTomMath User Manual \\ v0.35}
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
This text, the library and the accompanying textbook are all hereby placed in the public domain.  This book has been 
formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.

\vspace{10cm}








|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
\newcommand{\emailaddr}[1]{\mbox{$<${#1}$>$}}
\def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}}
\def\gap{\vspace{0.5ex}}
\makeindex
\begin{document}
\frontmatter
\pagestyle{empty}
\title{LibTomMath User Manual \\ v0.36}
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
This text, the library and the accompanying textbook are all hereby placed in the public domain.  This book has been 
formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.

\vspace{10cm}

Changes to libtommath/bn_error.c.
37
38
39
40
41
42
43




   }

   /* generic reply for invalid code */
   return "Invalid error code";
}

#endif











>
>
>
>
37
38
39
40
41
42
43
44
45
46
47
   }

   /* generic reply for invalid code */
   return "Invalid error code";
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_error.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_fast_mp_invmod.c.
138
139
140
141
142
143
144




  c->sign = neg;
  res = MP_OKAY;

LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
  return res;
}
#endif











>
>
>
>
138
139
140
141
142
143
144
145
146
147
148
  c->sign = neg;
  res = MP_OKAY;

LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_fast_mp_invmod.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_fast_mp_montgomery_reduce.c.
162
163
164
165
166
167
168




  /* if A >= m then A = A - m */
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }
  return MP_OKAY;
}
#endif











>
>
>
>
162
163
164
165
166
167
168
169
170
171
172
  /* if A >= m then A = A - m */
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_fast_mp_montgomery_reduce.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_fast_s_mp_mul_digs.c.
66
67
68
69
70
71
72

73
74
75
76
77
78
79
         while (tx++ < a->used && ty-- >= 0) { ... }
       */
      iy = MIN(a->used-tx, ty+1);

      /* execute loop */
      for (iz = 0; iz < iy; ++iz) {
         _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);

      }

      /* store term */
      W[ix] = ((mp_digit)_W) & MP_MASK;

      /* make next carry */
      _W = _W >> ((mp_word)DIGIT_BIT);







>







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
         while (tx++ < a->used && ty-- >= 0) { ... }
       */
      iy = MIN(a->used-tx, ty+1);

      /* execute loop */
      for (iz = 0; iz < iy; ++iz) {
         _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);

      }

      /* store term */
      W[ix] = ((mp_digit)_W) & MP_MASK;

      /* make next carry */
      _W = _W >> ((mp_word)DIGIT_BIT);
99
100
101
102
103
104
105




      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif











>
>
>
>
100
101
102
103
104
105
106
107
108
109
110
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_fast_s_mp_mul_digs.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_fast_s_mp_mul_high_digs.c.
91
92
93
94
95
96
97




      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif











>
>
>
>
91
92
93
94
95
96
97
98
99
100
101
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_fast_s_mp_mul_high_digs.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_fast_s_mp_sqr.c.
104
105
106
107
108
109
110




      *tmpb++ = 0;
    }
  }
  mp_clamp (b);
  return MP_OKAY;
}
#endif











>
>
>
>
104
105
106
107
108
109
110
111
112
113
114
      *tmpb++ = 0;
    }
  }
  mp_clamp (b);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_fast_s_mp_sqr.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_2expt.c.
38
39
40
41
42
43
44





  /* put the single bit in its place */
  a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);

  return MP_OKAY;
}
#endif











>
>
>
>
38
39
40
41
42
43
44
45
46
47
48

  /* put the single bit in its place */
  a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_2expt.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_abs.c.
33
34
35
36
37
38
39





  /* force the sign of b to positive */
  b->sign = MP_ZPOS;

  return MP_OKAY;
}
#endif











>
>
>
>
33
34
35
36
37
38
39
40
41
42
43

  /* force the sign of b to positive */
  b->sign = MP_ZPOS;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_abs.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_add.c.
43
44
45
46
47
48
49




      res = s_mp_sub (a, b, c);
    }
  }
  return res;
}

#endif











>
>
>
>
43
44
45
46
47
48
49
50
51
52
53
      res = s_mp_sub (a, b, c);
    }
  }
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_add.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_add_d.c.
99
100
101
102
103
104
105




  }
  mp_clamp(c);

  return MP_OKAY;
}

#endif











>
>
>
>
99
100
101
102
103
104
105
106
107
108
109
  }
  mp_clamp(c);

  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_add_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_addmod.c.
31
32
33
34
35
36
37




    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif











>
>
>
>
31
32
33
34
35
36
37
38
39
40
41
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_addmod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_and.c.
47
48
49
50
51
52
53





  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
47
48
49
50
51
52
53
54
55
56
57

  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_and.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_clamp.c.
34
35
36
37
38
39
40





  /* reset the sign flag if used == 0 */
  if (a->used == 0) {
    a->sign = MP_ZPOS;
  }
}
#endif











>
>
>
>
34
35
36
37
38
39
40
41
42
43
44

  /* reset the sign flag if used == 0 */
  if (a->used == 0) {
    a->sign = MP_ZPOS;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_clamp.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_clear.c.
34
35
36
37
38
39
40




    /* reset members to make debugging easier */
    a->dp    = NULL;
    a->alloc = a->used = 0;
    a->sign  = MP_ZPOS;
  }
}
#endif











>
>
>
>
34
35
36
37
38
39
40
41
42
43
44
    /* reset members to make debugging easier */
    a->dp    = NULL;
    a->alloc = a->used = 0;
    a->sign  = MP_ZPOS;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_clear.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_clear_multi.c.
24
25
26
27
28
29
30




    while (next_mp != NULL) {
        mp_clear(next_mp);
        next_mp = va_arg(args, mp_int*);
    }
    va_end(args);
}
#endif











>
>
>
>
24
25
26
27
28
29
30
31
32
33
34
    while (next_mp != NULL) {
        mp_clear(next_mp);
        next_mp = va_arg(args, mp_int*);
    }
    va_end(args);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_clear_multi.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_cmp.c.
33
34
35
36
37
38
39




     /* if negative compare opposite direction */
     return mp_cmp_mag(b, a);
  } else {
     return mp_cmp_mag(a, b);
  }
}
#endif











>
>
>
>
33
34
35
36
37
38
39
40
41
42
43
     /* if negative compare opposite direction */
     return mp_cmp_mag(b, a);
  } else {
     return mp_cmp_mag(a, b);
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_cmp.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_cmp_d.c.
34
35
36
37
38
39
40




  } else if (a->dp[0] < b) {
    return MP_LT;
  } else {
    return MP_EQ;
  }
}
#endif











>
>
>
>
34
35
36
37
38
39
40
41
42
43
44
  } else if (a->dp[0] < b) {
    return MP_LT;
  } else {
    return MP_EQ;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_cmp_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_cmp_mag.c.
45
46
47
48
49
50
51




    if (*tmpa < *tmpb) {
      return MP_LT;
    }
  }
  return MP_EQ;
}
#endif











>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
    if (*tmpa < *tmpb) {
      return MP_LT;
    }
  }
  return MP_EQ;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_cmp_mag.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_cnt_lsb.c.
43
44
45
46
47
48
49




         q >>= 4;
      } while (qq == 0);
   }
   return x;
}

#endif











>
>
>
>
43
44
45
46
47
48
49
50
51
52
53
         q >>= 4;
      } while (qq == 0);
   }
   return x;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_cnt_lsb.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_copy.c.
58
59
60
61
62
63
64





  /* copy used count and sign */
  b->used = a->used;
  b->sign = a->sign;
  return MP_OKAY;
}
#endif











>
>
>
>
58
59
60
61
62
63
64
65
66
67
68

  /* copy used count and sign */
  b->used = a->used;
  b->sign = a->sign;
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_copy.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_count_bits.c.
35
36
37
38
39
40
41




  while (q > ((mp_digit) 0)) {
    ++r;
    q >>= ((mp_digit) 1);
  }
  return r;
}
#endif











>
>
>
>
35
36
37
38
39
40
41
42
43
44
45
  while (q > ((mp_digit) 0)) {
    ++r;
    q >>= ((mp_digit) 1);
  }
  return r;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_count_bits.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_div.c.
282
283
284
285
286
287
288




LBL_Q:mp_clear (&q);
  return res;
}

#endif

#endif











>
>
>
>
282
283
284
285
286
287
288
289
290
291
292
LBL_Q:mp_clear (&q);
  return res;
}

#endif

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_div.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_div_2.c.
58
59
60
61
62
63
64




    }
  }
  b->sign = a->sign;
  mp_clamp (b);
  return MP_OKAY;
}
#endif











>
>
>
>
58
59
60
61
62
63
64
65
66
67
68
    }
  }
  b->sign = a->sign;
  mp_clamp (b);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_div_2.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_div_2d.c.
87
88
89
90
91
92
93




  if (d != NULL) {
    mp_exch (&t, d);
  }
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
87
88
89
90
91
92
93
94
95
96
97
  if (d != NULL) {
    mp_exch (&t, d);
  }
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_div_2d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_div_3.c.
69
70
71
72
73
74
75




  }
  mp_clear(&q);
  
  return res;
}

#endif











>
>
>
>
69
70
71
72
73
74
75
76
77
78
79
  }
  mp_clear(&q);
  
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_div_3.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_div_d.c.
100
101
102
103
104
105
106




  }
  mp_clear(&q);
  
  return res;
}

#endif











>
>
>
>
100
101
102
103
104
105
106
107
108
109
110
  }
  mp_clear(&q);
  
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_div_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_dr_is_modulus.c.
33
34
35
36
37
38
39




          return 0;
       }
   }
   return 1;
}

#endif











>
>
>
>
33
34
35
36
37
38
39
40
41
42
43
          return 0;
       }
   }
   return 1;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_dr_is_modulus.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_dr_reduce.c.
84
85
86
87
88
89
90




  if (mp_cmp_mag (x, n) != MP_LT) {
    s_mp_sub(x, n, x);
    goto top;
  }
  return MP_OKAY;
}
#endif











>
>
>
>
84
85
86
87
88
89
90
91
92
93
94
  if (mp_cmp_mag (x, n) != MP_LT) {
    s_mp_sub(x, n, x);
    goto top;
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_dr_reduce.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_dr_setup.c.
22
23
24
25
26
27
28




    * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
    */
   *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
        ((mp_word)a->dp[0]));
}

#endif











>
>
>
>
22
23
24
25
26
27
28
29
30
31
32
    * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
    */
   *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
        ((mp_word)a->dp[0]));
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_dr_setup.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_exch.c.
24
25
26
27
28
29
30




  mp_int  t;

  t  = *a;
  *a = *b;
  *b = t;
}
#endif











>
>
>
>
24
25
26
27
28
29
30
31
32
33
34
  mp_int  t;

  t  = *a;
  *a = *b;
  *b = t;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_exch.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_expt_d.c.
47
48
49
50
51
52
53




    b <<= 1;
  }

  mp_clear (&g);
  return MP_OKAY;
}
#endif











>
>
>
>
47
48
49
50
51
52
53
54
55
56
57
    b <<= 1;
  }

  mp_clear (&g);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_expt_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_exptmod.c.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#else 
     /* no invmod */
     return MP_VAL;
#endif
  }

/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C)
  if (mp_reduce_is_2k_l(P) == MP_YES) {
     return s_mp_exptmod(G, X, P, Y, 1);
  }
#endif

#ifdef BN_MP_DR_IS_MODULUS_C
  /* is it a DR modulus? */







|







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#else 
     /* no invmod */
     return MP_VAL;
#endif
  }

/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
  if (mp_reduce_is_2k_l(P) == MP_YES) {
     return s_mp_exptmod(G, X, P, Y, 1);
  }
#endif

#ifdef BN_MP_DR_IS_MODULUS_C
  /* is it a DR modulus? */
102
103
104
105
106
107
108




#endif
#ifdef BN_MP_EXPTMOD_FAST_C
  }
#endif
}

#endif











>
>
>
>
102
103
104
105
106
107
108
109
110
111
112
#endif
#ifdef BN_MP_EXPTMOD_FAST_C
  }
#endif
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_exptmod.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_exptmod_fast.c.
311
312
313
314
315
316
317




  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif












>
>
>
>
311
312
313
314
315
316
317
318
319
320
321
  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif


/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_exptmod_fast.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_exteuclid.c.
72
73
74
75
76
77
78




   if (U3 != NULL) { mp_exch(U3, &u3); }

   err = MP_OKAY;
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
   return err;
}
#endif











>
>
>
>
72
73
74
75
76
77
78
79
80
81
82
   if (U3 != NULL) { mp_exch(U3, &u3); }

   err = MP_OKAY;
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
   return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_exteuclid.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_fread.c.
57
58
59
60
61
62
63




      a->sign = neg;
   }
   
   return MP_OKAY;
}

#endif











>
>
>
>
57
58
59
60
61
62
63
64
65
66
67
      a->sign = neg;
   }
   
   return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_fread.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_fwrite.c.
42
43
44
45
46
47
48




   }
   
   XFREE (buf);
   return MP_OKAY;
}

#endif











>
>
>
>
42
43
44
45
46
47
48
49
50
51
52
   }
   
   XFREE (buf);
   return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_fwrite.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_gcd.c.
103
104
105
106
107
108
109




  c->sign = MP_ZPOS;
  res = MP_OKAY;
LBL_V:mp_clear (&u);
LBL_U:mp_clear (&v);
  return res;
}
#endif











>
>
>
>
103
104
105
106
107
108
109
110
111
112
113
  c->sign = MP_ZPOS;
  res = MP_OKAY;
LBL_V:mp_clear (&u);
LBL_U:mp_clear (&v);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_gcd.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_get_int.c.
35
36
37
38
39
40
41




    res = (res << DIGIT_BIT) | DIGIT(a,i);
  }

  /* force result to 32-bits always so it is consistent on non 32-bit platforms */
  return res & 0xFFFFFFFFUL;
}
#endif











>
>
>
>
35
36
37
38
39
40
41
42
43
44
45
    res = (res << DIGIT_BIT) | DIGIT(a,i);
  }

  /* force result to 32-bits always so it is consistent on non 32-bit platforms */
  return res & 0xFFFFFFFFUL;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_get_int.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_grow.c.
47
48
49
50
51
52
53




    for (; i < a->alloc; i++) {
      a->dp[i] = 0;
    }
  }
  return MP_OKAY;
}
#endif











>
>
>
>
47
48
49
50
51
52
53
54
55
56
57
    for (; i < a->alloc; i++) {
      a->dp[i] = 0;
    }
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_grow.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init.c.
36
37
38
39
40
41
42




  a->used  = 0;
  a->alloc = MP_PREC;
  a->sign  = MP_ZPOS;

  return MP_OKAY;
}
#endif











>
>
>
>
36
37
38
39
40
41
42
43
44
45
46
  a->used  = 0;
  a->alloc = MP_PREC;
  a->sign  = MP_ZPOS;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init_copy.c.
22
23
24
25
26
27
28





  if ((res = mp_init (a)) != MP_OKAY) {
    return res;
  }
  return mp_copy (b, a);
}
#endif











>
>
>
>
22
23
24
25
26
27
28
29
30
31
32

  if ((res = mp_init (a)) != MP_OKAY) {
    return res;
  }
  return mp_copy (b, a);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init_copy.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init_multi.c.
49
50
51
52
53
54
55




        cur_arg = va_arg(args, mp_int*);
    }
    va_end(args);
    return res;                /* Assumed ok, if error flagged above. */
}

#endif











>
>
>
>
49
50
51
52
53
54
55
56
57
58
59
        cur_arg = va_arg(args, mp_int*);
    }
    va_end(args);
    return res;                /* Assumed ok, if error flagged above. */
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init_multi.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init_set.c.
22
23
24
25
26
27
28




  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  mp_set(a, b);
  return err;
}
#endif











>
>
>
>
22
23
24
25
26
27
28
29
30
31
32
  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  mp_set(a, b);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init_set.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init_set_int.c.
21
22
23
24
25
26
27




  int err;
  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  return mp_set_int(a, b);
}
#endif











>
>
>
>
21
22
23
24
25
26
27
28
29
30
31
  int err;
  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  return mp_set_int(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init_set_int.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_init_size.c.
38
39
40
41
42
43
44




  for (x = 0; x < size; x++) {
      a->dp[x] = 0;
  }

  return MP_OKAY;
}
#endif











>
>
>
>
38
39
40
41
42
43
44
45
46
47
48
  for (x = 0; x < size; x++) {
      a->dp[x] = 0;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_init_size.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_invmod.c.
33
34
35
36
37
38
39




#ifdef BN_MP_INVMOD_SLOW_C
  return mp_invmod_slow(a, b, c);
#endif

  return MP_VAL;
}
#endif











>
>
>
>
33
34
35
36
37
38
39
40
41
42
43
#ifdef BN_MP_INVMOD_SLOW_C
  return mp_invmod_slow(a, b, c);
#endif

  return MP_VAL;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_invmod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_invmod_slow.c.
165
166
167
168
169
170
171




  /* C is now the inverse */
  mp_exch (&C, c);
  res = MP_OKAY;
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  return res;
}
#endif











>
>
>
>
165
166
167
168
169
170
171
172
173
174
175
  /* C is now the inverse */
  mp_exch (&C, c);
  res = MP_OKAY;
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_invmod_slow.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_is_square.c.
99
100
101
102
103
104
105




  }

  *ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t);
  return res;
}
#endif











>
>
>
>
99
100
101
102
103
104
105
106
107
108
109
  }

  *ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_is_square.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_jacobi.c.
95
96
97
98
99
100
101




  /* done */
  res = MP_OKAY;
LBL_P1:mp_clear (&p1);
LBL_A1:mp_clear (&a1);
  return res;
}
#endif











>
>
>
>
95
96
97
98
99
100
101
102
103
104
105
  /* done */
  res = MP_OKAY;
LBL_P1:mp_clear (&p1);
LBL_A1:mp_clear (&a1);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_jacobi.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_karatsuba_mul.c.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * let n represent half of the number of digits in 
 * the min(a,b)
 *
 * a = a1 * B**n + a0
 * b = b1 * B**n + b0
 *
 * Then, a * b => 
   a1b1 * B**2n + ((a1 - a0)(b1 - b0) + a0b0 + a1b1) * B + a0b0
 *
 * Note that a1b1 and a0b0 are used twice and only need to be 
 * computed once.  So in total three half size (half # of 
 * digit) multiplications are performed, a0b0, a1b1 and 
 * (a1-b1)(a0-b0)
 *
 * Note that a multiplication of half the digits requires
 * 1/4th the number of single precision multiplications so in 
 * total after one call 25% of the single precision multiplications 
 * are saved.  Note also that the call to mp_mul can end up back 
 * in this function if the a0, a1, b0, or b1 are above the threshold.  
 * This is known as divide-and-conquer and leads to the famous 







|




|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * let n represent half of the number of digits in 
 * the min(a,b)
 *
 * a = a1 * B**n + a0
 * b = b1 * B**n + b0
 *
 * Then, a * b => 
   a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
 *
 * Note that a1b1 and a0b0 are used twice and only need to be 
 * computed once.  So in total three half size (half # of 
 * digit) multiplications are performed, a0b0, a1b1 and 
 * (a1+b1)(a0+b0)
 *
 * Note that a multiplication of half the digits requires
 * 1/4th the number of single precision multiplications so in 
 * total after one call 25% of the single precision multiplications 
 * are saved.  Note also that the call to mp_mul can end up back 
 * in this function if the a0, a1, b0, or b1 are above the threshold.  
 * This is known as divide-and-conquer and leads to the famous 
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  /* now calc the products x0y0 and x1y1 */
  /* after this x0 is no longer required, free temp [x0==t2]! */
  if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
    goto X1Y1;          /* x0y0 = x0*y0 */
  if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1*y1 */

  /* now calc x1-x0 and y1-y0 */
  if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x1 - x0 */
  if (mp_sub (&y1, &y0, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = y1 - y0 */
  if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1 - x0) * (y1 - y0) */

  /* add x0y0 */
  if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = x0y0 + x1y1 */
  if (mp_sub (&x0, &t1, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x0y0 + x1y1 - (x1-x0)*(y1-y0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1y1 << 2*B */








|
|

|


|




|
|







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  /* now calc the products x0y0 and x1y1 */
  /* after this x0 is no longer required, free temp [x0==t2]! */
  if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
    goto X1Y1;          /* x0y0 = x0*y0 */
  if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1*y1 */

  /* now calc x1+x0 and y1+y0 */
  if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x1 - x0 */
  if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = y1 - y0 */
  if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1 + x0) * (y1 + y0) */

  /* add x0y0 */
  if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = x0y0 + x1y1 */
  if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1y1 << 2*B */

157
158
159
160
161
162
163




Y0:mp_clear (&y0);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif











>
>
>
>
157
158
159
160
161
162
163
164
165
166
167
Y0:mp_clear (&y0);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_karatsuba_mul.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_karatsuba_sqr.c.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

  /* now calc the products x0*x0 and x1*x1 */
  if (mp_sqr (&x0, &x0x0) != MP_OKAY)
    goto X1X1;           /* x0x0 = x0*x0 */
  if (mp_sqr (&x1, &x1x1) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1*x1 */

  /* now calc (x1-x0)**2 */
  if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x1 - x0 */
  if (mp_sqr (&t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */

  /* add x0y0 */
  if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
    goto X1X1;           /* t2 = x0x0 + x1x1 */
  if (mp_sub (&t2, &t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x0x0 + x1x1 - (x1-x0)*(x1-x0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
  if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1x1 << 2*B */








|
|







|
|







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

  /* now calc the products x0*x0 and x1*x1 */
  if (mp_sqr (&x0, &x0x0) != MP_OKAY)
    goto X1X1;           /* x0x0 = x0*x0 */
  if (mp_sqr (&x1, &x1x1) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1*x1 */

  /* now calc (x1+x0)**2 */
  if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x1 - x0 */
  if (mp_sqr (&t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */

  /* add x0y0 */
  if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
    goto X1X1;           /* t2 = x0x0 + x1x1 */
  if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
  if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1x1 << 2*B */

111
112
113
114
115
116
117




T1:mp_clear (&t1);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif











>
>
>
>
111
112
113
114
115
116
117
118
119
120
121
T1:mp_clear (&t1);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_karatsuba_sqr.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_lcm.c.
50
51
52
53
54
55
56




  c->sign = MP_ZPOS;

LBL_T:
  mp_clear_multi (&t1, &t2, NULL);
  return res;
}
#endif











>
>
>
>
50
51
52
53
54
55
56
57
58
59
60
  c->sign = MP_ZPOS;

LBL_T:
  mp_clear_multi (&t1, &t2, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_lcm.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_lshd.c.
57
58
59
60
61
62
63




    for (x = 0; x < b; x++) {
      *top++ = 0;
    }
  }
  return MP_OKAY;
}
#endif











>
>
>
>
57
58
59
60
61
62
63
64
65
66
67
    for (x = 0; x < b; x++) {
      *top++ = 0;
    }
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_lshd.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mod.c.
38
39
40
41
42
43
44




    mp_exch (&t, c);
  }

  mp_clear (&t);
  return res;
}
#endif











>
>
>
>
38
39
40
41
42
43
44
45
46
47
48
    mp_exch (&t, c);
  }

  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mod_2d.c.
45
46
47
48
49
50
51




  /* clear the digit that is not completely outside/inside the modulus */
  c->dp[b / DIGIT_BIT] &=
    (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  mp_clamp (c);
  return MP_OKAY;
}
#endif











>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
  /* clear the digit that is not completely outside/inside the modulus */
  c->dp[b / DIGIT_BIT] &=
    (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mod_2d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mod_d.c.
17
18
19
20
21
22
23





int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
{
  return mp_div_d(a, b, NULL, c);
}
#endif











>
>
>
>
17
18
19
20
21
22
23
24
25
26
27

int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
{
  return mp_div_d(a, b, NULL, c);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mod_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_montgomery_calc_normalization.c.
49
50
51
52
53
54
55




      }
    }
  }

  return MP_OKAY;
}
#endif











>
>
>
>
49
50
51
52
53
54
55
56
57
58
59
      }
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_montgomery_calc_normalization.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_montgomery_reduce.c.
108
109
110
111
112
113
114




  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }

  return MP_OKAY;
}
#endif











>
>
>
>
108
109
110
111
112
113
114
115
116
117
118
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_montgomery_reduce.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_montgomery_setup.c.
49
50
51
52
53
54
55





  /* rho = -1/m mod b */
  *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;

  return MP_OKAY;
}
#endif











>
>
>
>
49
50
51
52
53
54
55
56
57
58
59

  /* rho = -1/m mod b */
  *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_montgomery_setup.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mul.c.
56
57
58
59
60
61
62




#endif

  }
  c->sign = (c->used > 0) ? neg : MP_ZPOS;
  return res;
}
#endif











>
>
>
>
56
57
58
59
60
61
62
63
64
65
66
#endif

  }
  c->sign = (c->used > 0) ? neg : MP_ZPOS;
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mul.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mul_2.c.
72
73
74
75
76
77
78




      *tmpb++ = 0;
    }
  }
  b->sign = a->sign;
  return MP_OKAY;
}
#endif











>
>
>
>
72
73
74
75
76
77
78
79
80
81
82
      *tmpb++ = 0;
    }
  }
  b->sign = a->sign;
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mul_2.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mul_2d.c.
75
76
77
78
79
80
81




       c->dp[(c->used)++] = r;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif











>
>
>
>
75
76
77
78
79
80
81
82
83
84
85
       c->dp[(c->used)++] = r;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mul_2d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mul_d.c.
69
70
71
72
73
74
75




  /* set used count */
  c->used = a->used + 1;
  mp_clamp(c);

  return MP_OKAY;
}
#endif











>
>
>
>
69
70
71
72
73
74
75
76
77
78
79
  /* set used count */
  c->used = a->used + 1;
  mp_clamp(c);

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mul_d.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_mulmod.c.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37




 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* d = a * b (mod c) */
int
mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
  int     res;
  mp_int  t;

  if ((res = mp_init (&t)) != MP_OKAY) {
    return res;
  }

  if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
    mp_clear (&t);
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif











<
|

















>
>
>
>
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* d = a * b (mod c) */

int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
  int     res;
  mp_int  t;

  if ((res = mp_init (&t)) != MP_OKAY) {
    return res;
  }

  if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
    mp_clear (&t);
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_mulmod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_n_root.c.
122
123
124
125
126
127
128





LBL_T3:mp_clear (&t3);
LBL_T2:mp_clear (&t2);
LBL_T1:mp_clear (&t1);
  return res;
}
#endif











>
>
>
>
122
123
124
125
126
127
128
129
130
131
132

LBL_T3:mp_clear (&t3);
LBL_T2:mp_clear (&t2);
LBL_T1:mp_clear (&t1);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_n_root.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_neg.c.
30
31
32
33
34
35
36




  } else {
     b->sign = MP_ZPOS;
  }

  return MP_OKAY;
}
#endif











>
>
>
>
30
31
32
33
34
35
36
37
38
39
40
  } else {
     b->sign = MP_ZPOS;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_neg.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_or.c.
40
41
42
43
44
45
46




  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
40
41
42
43
44
45
46
47
48
49
50
  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_or.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_fermat.c.
52
53
54
55
56
57
58




  }

  err = MP_OKAY;
LBL_T:mp_clear (&t);
  return err;
}
#endif











>
>
>
>
52
53
54
55
56
57
58
59
60
61
62
  }

  err = MP_OKAY;
LBL_T:mp_clear (&t);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_fermat.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_is_divisible.c.
40
41
42
43
44
45
46




      return MP_OKAY;
    }
  }

  return MP_OKAY;
}
#endif











>
>
>
>
40
41
42
43
44
45
46
47
48
49
50
      return MP_OKAY;
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_is_divisible.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_is_prime.c.
73
74
75
76
77
78
79





  /* passed the test */
  *result = MP_YES;
LBL_B:mp_clear (&b);
  return err;
}
#endif











>
>
>
>
73
74
75
76
77
78
79
80
81
82
83

  /* passed the test */
  *result = MP_YES;
LBL_B:mp_clear (&b);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_is_prime.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_miller_rabin.c.
93
94
95
96
97
98
99




  *result = MP_YES;
LBL_Y:mp_clear (&y);
LBL_R:mp_clear (&r);
LBL_N1:mp_clear (&n1);
  return err;
}
#endif











>
>
>
>
93
94
95
96
97
98
99
100
101
102
103
  *result = MP_YES;
LBL_Y:mp_clear (&y);
LBL_R:mp_clear (&r);
LBL_N1:mp_clear (&n1);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_miller_rabin.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_next_prime.c.
160
161
162
163
164
165
166




   err = MP_OKAY;
LBL_ERR:
   mp_clear(&b);
   return err;
}

#endif











>
>
>
>
160
161
162
163
164
165
166
167
168
169
170
   err = MP_OKAY;
LBL_ERR:
   mp_clear(&b);
   return err;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_next_prime.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_rabin_miller_trials.c.
42
43
44
45
46
47
48




       }
   }
   return sizes[x-1].t + 1;
}


#endif











>
>
>
>
42
43
44
45
46
47
48
49
50
51
52
       }
   }
   return sizes[x-1].t + 1;
}


#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_rabin_miller_trials.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_prime_random_ex.c.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
   /* calc the maskAND value for the MSbyte*/
   maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));

   /* calc the maskOR_msb */
   maskOR_msb        = 0;
   maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
   if (flags & LTM_PRIME_2MSB_ON) {
      maskOR_msb     |= 1 << ((size - 2) & 7);
   } else if (flags & LTM_PRIME_2MSB_OFF) {
      maskAND        &= ~(1 << ((size - 2) & 7));
   } 

   /* get the maskOR_lsb */
   maskOR_lsb         = 1;
   if (flags & LTM_PRIME_BBS) {
      maskOR_lsb     |= 3;
   }








|
<
<
|







58
59
60
61
62
63
64
65


66
67
68
69
70
71
72
73
   /* calc the maskAND value for the MSbyte*/
   maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));

   /* calc the maskOR_msb */
   maskOR_msb        = 0;
   maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
   if (flags & LTM_PRIME_2MSB_ON) {
      maskOR_msb       |= 0x80 >> ((9 - size) & 7);


   }  

   /* get the maskOR_lsb */
   maskOR_lsb         = 1;
   if (flags & LTM_PRIME_BBS) {
      maskOR_lsb     |= 3;
   }

117
118
119
120
121
122
123




error:
   XFREE(tmp);
   return err;
}


#endif











>
>
>
>
115
116
117
118
119
120
121
122
123
124
125
error:
   XFREE(tmp);
   return err;
}


#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_prime_random_ex.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_radix_smap.c.
14
15
16
17
18
19
20




 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* chars used in radix conversions */
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#endif











>
>
>
>
14
15
16
17
18
19
20
21
22
23
24
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* chars used in radix conversions */
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_radix_smap.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_rand.c.
45
46
47
48
49
50
51




      return res;
    }
  }

  return MP_OKAY;
}
#endif











>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
      return res;
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_rand.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_read_signed_bin.c.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38




 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* read signed bin, big endian, first byte is 0==positive or 1==negative */
int
mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
{
  int     res;

  /* read magnitude */
  if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
    return res;
  }

  /* first byte is 0 for positive, non-zero for negative */
  if (b[0] == 0) {
     a->sign = MP_ZPOS;
  } else {
     a->sign = MP_NEG;
  }

  return MP_OKAY;
}
#endif











<
|


















>
>
>
>
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* read signed bin, big endian, first byte is 0==positive or 1==negative */

int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
{
  int     res;

  /* read magnitude */
  if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
    return res;
  }

  /* first byte is 0 for positive, non-zero for negative */
  if (b[0] == 0) {
     a->sign = MP_ZPOS;
  } else {
     a->sign = MP_NEG;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_read_signed_bin.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_read_unsigned_bin.c.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* reads a unsigned char array, assumes the msb is stored first [big endian] */
int
mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
{
  int     res;

  /* make sure there are at least two digits */
  if (a->alloc < 2) {
     if ((res = mp_grow(a, 2)) != MP_OKAY) {
        return res;







<
|







12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* reads a unsigned char array, assumes the msb is stored first [big endian] */

int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
{
  int     res;

  /* make sure there are at least two digits */
  if (a->alloc < 2) {
     if ((res = mp_grow(a, 2)) != MP_OKAY) {
        return res;
46
47
48
49
50
51
52




      a->used += 2;
#endif
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif











>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
      a->used += 2;
#endif
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_read_unsigned_bin.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce.c.
90
91
92
93
94
95
96




  
CLEANUP:
  mp_clear (&q);

  return res;
}
#endif











>
>
>
>
90
91
92
93
94
95
96
97
98
99
100
  
CLEANUP:
  mp_clear (&q);

  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_2k.c.
51
52
53
54
55
56
57




   
ERR:
   mp_clear(&q);
   return res;
}

#endif











>
>
>
>
51
52
53
54
55
56
57
58
59
60
61
   
ERR:
   mp_clear(&q);
   return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_2k.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_2k_l.c.
52
53
54
55
56
57
58




   
ERR:
   mp_clear(&q);
   return res;
}

#endif











>
>
>
>
52
53
54
55
56
57
58
59
60
61
62
   
ERR:
   mp_clear(&q);
   return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_2k_l.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_2k_setup.c.
37
38
39
40
41
42
43




   }
   
   *d = tmp.dp[0];
   mp_clear(&tmp);
   return MP_OKAY;
}
#endif











>
>
>
>
37
38
39
40
41
42
43
44
45
46
47
   }
   
   *d = tmp.dp[0];
   mp_clear(&tmp);
   return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_2k_setup.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_2k_setup_l.c.
34
35
36
37
38
39
40




   }
   
ERR:
   mp_clear(&tmp);
   return res;
}
#endif











>
>
>
>
34
35
36
37
38
39
40
41
42
43
44
   }
   
ERR:
   mp_clear(&tmp);
   return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_2k_setup_l.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_is_2k.c.
42
43
44
45
46
47
48




          }
      }
   }
   return MP_YES;
}

#endif











>
>
>
>
42
43
44
45
46
47
48
49
50
51
52
          }
      }
   }
   return MP_YES;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_is_2k.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_is_2k_l.c.
34
35
36
37
38
39
40




      return (iy >= (a->used/2)) ? MP_YES : MP_NO;
      
   }
   return MP_NO;
}

#endif











>
>
>
>
34
35
36
37
38
39
40
41
42
43
44
      return (iy >= (a->used/2)) ? MP_YES : MP_NO;
      
   }
   return MP_NO;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_is_2k_l.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_reduce_setup.c.
24
25
26
27
28
29
30




  
  if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
    return res;
  }
  return mp_div (a, b, a, NULL);
}
#endif











>
>
>
>
24
25
26
27
28
29
30
31
32
33
34
  
  if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
    return res;
  }
  return mp_div (a, b, a, NULL);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_reduce_setup.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_rshd.c.
62
63
64
65
66
67
68




    }
  }
  
  /* remove excess digits */
  a->used -= b;
}
#endif











>
>
>
>
62
63
64
65
66
67
68
69
70
71
72
    }
  }
  
  /* remove excess digits */
  a->used -= b;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_rshd.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_set.c.
19
20
21
22
23
24
25




void mp_set (mp_int * a, mp_digit b)
{
  mp_zero (a);
  a->dp[0] = b & MP_MASK;
  a->used  = (a->dp[0] != 0) ? 1 : 0;
}
#endif











>
>
>
>
19
20
21
22
23
24
25
26
27
28
29
void mp_set (mp_int * a, mp_digit b)
{
  mp_zero (a);
  a->dp[0] = b & MP_MASK;
  a->used  = (a->dp[0] != 0) ? 1 : 0;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_set.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_set_int.c.
38
39
40
41
42
43
44




    /* ensure that digits are not clamped off */
    a->used += 1;
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif











>
>
>
>
38
39
40
41
42
43
44
45
46
47
48
    /* ensure that digits are not clamped off */
    a->used += 1;
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_set_int.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_shrink.c.
25
26
27
28
29
30
31




    }
    a->dp    = tmp;
    a->alloc = a->used;
  }
  return MP_OKAY;
}
#endif











>
>
>
>
25
26
27
28
29
30
31
32
33
34
35
    }
    a->dp    = tmp;
    a->alloc = a->used;
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_shrink.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_signed_bin_size.c.
17
18
19
20
21
22
23





/* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a)
{
  return 1 + mp_unsigned_bin_size (a);
}
#endif











>
>
>
>
17
18
19
20
21
22
23
24
25
26
27

/* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a)
{
  return 1 + mp_unsigned_bin_size (a);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_signed_bin_size.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_sqr.c.
48
49
50
51
52
53
54




      res = MP_VAL;
#endif
  }
  b->sign = MP_ZPOS;
  return res;
}
#endif











>
>
>
>
48
49
50
51
52
53
54
55
56
57
58
      res = MP_VAL;
#endif
  }
  b->sign = MP_ZPOS;
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sqr.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_sqrmod.c.
31
32
33
34
35
36
37




    return res;
  }
  res = mp_mod (&t, b, c);
  mp_clear (&t);
  return res;
}
#endif











>
>
>
>
31
32
33
34
35
36
37
38
39
40
41
    return res;
  }
  res = mp_mod (&t, b, c);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sqrmod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_sqrt.c.
71
72
73
74
75
76
77





E1: mp_clear(&t2);
E2: mp_clear(&t1);
  return res;
}

#endif











>
>
>
>
71
72
73
74
75
76
77
78
79
80
81

E1: mp_clear(&t2);
E2: mp_clear(&t1);
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sqrt.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_sub.c.
49
50
51
52
53
54
55




      res = s_mp_sub (b, a, c);
    }
  }
  return res;
}

#endif











>
>
>
>
49
50
51
52
53
54
55
56
57
58
59
      res = s_mp_sub (b, a, c);
    }
  }
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sub.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_sub_d.c.
79
80
81
82
83
84
85




     *tmpc++ = 0;
  }
  mp_clamp(c);
  return MP_OKAY;
}

#endif











>
>
>
>
79
80
81
82
83
84
85
86
87
88
89
     *tmpc++ = 0;
  }
  mp_clamp(c);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sub_d.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_submod.c.
32
33
34
35
36
37
38




    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif











>
>
>
>
32
33
34
35
36
37
38
39
40
41
42
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_submod.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_to_signed_bin.c.
23
24
25
26
27
28
29




  if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) {
    return res;
  }
  b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
  return MP_OKAY;
}
#endif











>
>
>
>
23
24
25
26
27
28
29
30
31
32
33
  if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) {
    return res;
  }
  b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_to_signed_bin.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_to_signed_bin_n.c.
21
22
23
24
25
26
27




   if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_signed_bin_size(a);
   return mp_to_signed_bin(a, b);
}
#endif











>
>
>
>
21
22
23
24
25
26
27
28
29
30
31
   if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_signed_bin_size(a);
   return mp_to_signed_bin(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_to_signed_bin_n.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_to_unsigned_bin.c.
38
39
40
41
42
43
44




    }
  }
  bn_reverse (b, x);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
38
39
40
41
42
43
44
45
46
47
48
    }
  }
  bn_reverse (b, x);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_to_unsigned_bin.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_to_unsigned_bin_n.c.
21
22
23
24
25
26
27




   if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_unsigned_bin_size(a);
   return mp_to_unsigned_bin(a, b);
}
#endif











>
>
>
>
21
22
23
24
25
26
27
28
29
30
31
   if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_unsigned_bin_size(a);
   return mp_to_unsigned_bin(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_to_unsigned_bin_n.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_toom_mul.c.
274
275
276
277
278
279
280




     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, 
                    &a0, &a1, &a2, &b0, &b1, 
                    &b2, &tmp1, &tmp2, NULL);
     return res;
}     
     
#endif











>
>
>
>
274
275
276
277
278
279
280
281
282
283
284
     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, 
                    &a0, &a1, &a2, &b0, &b1, 
                    &b2, &tmp1, &tmp2, NULL);
     return res;
}     
     
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_toom_mul.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_toom_sqr.c.
216
217
218
219
220
221
222





ERR:
     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
     return res;
}

#endif











>
>
>
>
216
217
218
219
220
221
222
223
224
225
226

ERR:
     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
     return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_toom_sqr.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_toradix.c.
65
66
67
68
69
70
71




  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif











>
>
>
>
65
66
67
68
69
70
71
72
73
74
75
  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_toradix.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_toradix_n.c.
79
80
81
82
83
84
85




  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif











>
>
>
>
79
80
81
82
83
84
85
86
87
88
89
  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_toradix_n.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_unsigned_bin_size.c.
18
19
20
21
22
23
24




/* get the size for an unsigned equivalent */
int mp_unsigned_bin_size (mp_int * a)
{
  int     size = mp_count_bits (a);
  return (size / 8 + ((size & 7) != 0 ? 1 : 0));
}
#endif











>
>
>
>
18
19
20
21
22
23
24
25
26
27
28
/* get the size for an unsigned equivalent */
int mp_unsigned_bin_size (mp_int * a)
{
  int     size = mp_count_bits (a);
  return (size / 8 + ((size & 7) != 0 ? 1 : 0));
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_unsigned_bin_size.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_xor.c.
41
42
43
44
45
46
47




  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
41
42
43
44
45
46
47
48
49
50
51
  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_xor.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_mp_zero.c.
26
27
28
29
30
31
32





  tmp = a->dp;
  for (n = 0; n < a->alloc; n++) {
     *tmp++ = 0;
  }
}
#endif











>
>
>
>
26
27
28
29
30
31
32
33
34
35
36

  tmp = a->dp;
  for (n = 0; n < a->alloc; n++) {
     *tmp++ = 0;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_zero.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_prime_tab.c.
51
52
53
54
55
56
57




  0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
  0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
#endif
};
#endif











>
>
>
>
51
52
53
54
55
56
57
58
59
60
61
  0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
  0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
#endif
};
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_prime_tab.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_reverse.c.
29
30
31
32
33
34
35




    s[ix] = s[iy];
    s[iy] = t;
    ++ix;
    --iy;
  }
}
#endif











>
>
>
>
29
30
31
32
33
34
35
36
37
38
39
    s[ix] = s[iy];
    s[iy] = t;
    ++ix;
    --iy;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_reverse.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_add.c.
99
100
101
102
103
104
105




    }
  }

  mp_clamp (c);
  return MP_OKAY;
}
#endif











>
>
>
>
99
100
101
102
103
104
105
106
107
108
109
    }
  }

  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_add.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_exptmod.c.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * 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
 */

#ifdef MP_LOW_MEM
   #define TAB_SIZE 32
#else
   #define TAB_SIZE 256
#endif

int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)







<







10
11
12
13
14
15
16

17
18
19
20
21
22
23
 * 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
 */

#ifdef MP_LOW_MEM
   #define TAB_SIZE 32
#else
   #define TAB_SIZE 256
#endif

int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
243
244
245
246
247
248
249




  mp_clear(&M[1]);
  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif











>
>
>
>
242
243
244
245
246
247
248
249
250
251
252
  mp_clear(&M[1]);
  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_exptmod.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_mul_digs.c.
80
81
82
83
84
85
86




  mp_clamp (&t);
  mp_exch (&t, c);

  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
80
81
82
83
84
85
86
87
88
89
90
  mp_clamp (&t);
  mp_exch (&t, c);

  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_mul_digs.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_mul_high_digs.c.
71
72
73
74
75
76
77




  }
  mp_clamp (&t);
  mp_exch (&t, c);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
71
72
73
74
75
76
77
78
79
80
81
  }
  mp_clamp (&t);
  mp_exch (&t, c);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_mul_high_digs.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_sqr.c.
74
75
76
77
78
79
80





  mp_clamp (&t);
  mp_exch (&t, b);
  mp_clear (&t);
  return MP_OKAY;
}
#endif











>
>
>
>
74
75
76
77
78
79
80
81
82
83
84

  mp_clamp (&t);
  mp_exch (&t, b);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_sqr.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bn_s_mp_sub.c.
79
80
81
82
83
84
85




  }

  mp_clamp (c);
  return MP_OKAY;
}

#endif











>
>
>
>
79
80
81
82
83
84
85
86
87
88
89
  }

  mp_clamp (c);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_s_mp_sub.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/bncore.c.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32




 */

/* Known optimal configurations

 CPU                    /Compiler     /MUL CUTOFF/SQR CUTOFF
-------------------------------------------------------------
 Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-)
 AMD Athlon64           /GCC v3.4.4   /        74/       124/LTM 0.34
 
*/

int     KARATSUBA_MUL_CUTOFF = 74,      /* Min. number of digits before Karatsuba multiplication is used. */
        KARATSUBA_SQR_CUTOFF = 124,     /* Min. number of digits before Karatsuba squaring is used. */
        
        TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */
        TOOM_SQR_CUTOFF      = 400; 
#endif











|



|
|




>
>
>
>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 */

/* Known optimal configurations

 CPU                    /Compiler     /MUL CUTOFF/SQR CUTOFF
-------------------------------------------------------------
 Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-)
 AMD Athlon64           /GCC v3.4.4   /        80/       120/LTM 0.35
 
*/

int     KARATSUBA_MUL_CUTOFF = 80,      /* Min. number of digits before Karatsuba multiplication is used. */
        KARATSUBA_SQR_CUTOFF = 120,     /* Min. number of digits before Karatsuba squaring is used. */
        
        TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */
        TOOM_SQR_CUTOFF      = 400; 
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bncore.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:56 $ */
Changes to libtommath/booker.pl.
85
86
87
88
89
90
91



92
93
94
95
96
97
98
            last if ($_ =~ /math\.libtomcrypt\.org/);
         }
         <SRC>;   
      }
      
      $inline = 0;
      while (<SRC>) {



         $text[$line++] = $_;
         ++$inline;
         chomp($_);
         $_ =~ s/\t/"    "/ge;
         $_ =~ s/{/"^{"/ge;
         $_ =~ s/}/"^}"/ge;
         $_ =~ s/\\/'\symbol{92}'/ge;







>
>
>







85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
            last if ($_ =~ /math\.libtomcrypt\.org/);
         }
         <SRC>;   
      }
      
      $inline = 0;
      while (<SRC>) {
      next if ($_ =~ /\$Source/);
      next if ($_ =~ /\$Revision/);
      next if ($_ =~ /\$Date/);
         $text[$line++] = $_;
         ++$inline;
         chomp($_);
         $_ =~ s/\t/"    "/ge;
         $_ =~ s/{/"^{"/ge;
         $_ =~ s/}/"^}"/ge;
         $_ =~ s/\\/'\symbol{92}'/ge;
Changes to libtommath/changes.txt.












1
2
3
4
5
6
7












March 12th, 2005
v0.35  -- Stupid XOR function missing line again... oops.
       -- Fixed bug in invmod not handling negative inputs correctly [Wolfgang Ehrhardt]
       -- Made exteuclid always give positive u3 output...[ Wolfgang Ehrhardt ]
       -- [Wolfgang Ehrhardt] Suggested a fix for mp_reduce() which avoided underruns.  ;-)
       -- mp_rand() would emit one too many digits and it was possible to get a 0 out of it ... oops
       -- Added montgomery to the testing to make sure it handles 1..10 digit moduli correctly
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
August 1st, 2005
v0.36  -- LTM_PRIME_2MSB_ON was fixed and the "OFF" flag was removed.
       -- [Peter LaDow] found a typo in the XREALLOC macro
       -- [Peter LaDow] pointed out that mp_read_(un)signed_bin should have "const" on the input
       -- Ported LTC patch to fix the prime_random_ex() function to get the bitsize correct [and the maskOR flags]
       -- Kevin Kenny pointed out a stray //
       -- David Hulton pointed out a typo in the textbook [mp_montgomery_setup() pseudo-code]
       -- Neal Hamilton (Elliptic Semiconductor) pointed out that my Karatsuba notation was backwards and that I could use 
          unsigned operations in the routine.  
       -- Paul Schmidt pointed out a linking error in mp_exptmod() when BN_S_MP_EXPTMOD_C is undefined (and another for read_radix)
       -- Updated makefiles to be way more flexible

March 12th, 2005
v0.35  -- Stupid XOR function missing line again... oops.
       -- Fixed bug in invmod not handling negative inputs correctly [Wolfgang Ehrhardt]
       -- Made exteuclid always give positive u3 output...[ Wolfgang Ehrhardt ]
       -- [Wolfgang Ehrhardt] Suggested a fix for mp_reduce() which avoided underruns.  ;-)
       -- mp_rand() would emit one too many digits and it was possible to get a 0 out of it ... oops
       -- Added montgomery to the testing to make sure it handles 1..10 digit moduli correctly
Changes to libtommath/demo/demo.c.
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#endif

   div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n =
      sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n =
      sub_d_n = 0;

   /* force KARA and TOOM to enable despite cutoffs */
   KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 110;
   TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 150;

   for (;;) {
      /* randomly clear and re-init one variable, this has the affect of triming the alloc space */
      switch (abs(rand()) % 7) {
      case 0:
	 mp_clear(&a);
	 mp_init(&a);







|
|







385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#endif

   div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n =
      sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n =
      sub_d_n = 0;

   /* force KARA and TOOM to enable despite cutoffs */
   KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8;
   TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 16;

   for (;;) {
      /* randomly clear and re-init one variable, this has the affect of triming the alloc space */
      switch (abs(rand()) % 7) {
      case 0:
	 mp_clear(&a);
	 mp_init(&a);
730
731
732
733
734
735
736




	    printf("d == %d\n", ix);
	    return 0;
	 }
      }
   }
   return 0;
}











>
>
>
>
730
731
732
733
734
735
736
737
738
739
740
	    printf("d == %d\n", ix);
	    return 0;
	 }
      }
   }
   return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/demo/demo.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/demo/timing.c.
309
310
311
312
313
314
315




	     mp_count_bits(&a), CLK_PER_SEC / tt, tt);
      fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt);
   }
   fclose(log);

   return 0;
}











>
>
>
>
309
310
311
312
313
314
315
316
317
318
319
	     mp_count_bits(&a), CLK_PER_SEC / tt, tt);
      fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt);
   }
   fclose(log);

   return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/demo/timing.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/2kprime.c.
74
75
76
77
78
79
80




   return 0;
}   
       
         
            
            
          











>
>
>
>
74
75
76
77
78
79
80
81
82
83
84
   return 0;
}   
       
         
            
            
          

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/2kprime.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/drprime.c.
54
55
56
57
58
59
60




   
   mp_clear(&a);
   mp_clear(&b);
   
   return 0;
}












>
>
>
>
54
55
56
57
58
59
60
61
62
63
64
   
   mp_clear(&a);
   mp_clear(&b);
   
   return 0;
}


/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/drprime.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/makefile.icc.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#   K - PIII
#   W - first P4 [Williamette]
#   N - P4 Northwood
#   P - P4 Prescott
#   B - Blend of P4 and PM [mobile]
#
# Default to just generic max opts
CFLAGS += -O3 -xN -ip

# default lib name (requires install with root)
# LIBNAME=-ltommath

# libname when you can't install the lib with install
LIBNAME=../libtommath.a








|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#   K - PIII
#   W - first P4 [Williamette]
#   N - P4 Northwood
#   P - P4 Prescott
#   B - Blend of P4 and PM [mobile]
#
# Default to just generic max opts
CFLAGS += -O3 -xP -ip

# default lib name (requires install with root)
# LIBNAME=-ltommath

# libname when you can't install the lib with install
LIBNAME=../libtommath.a

Changes to libtommath/etc/mersenne.c.
134
135
136
137
138
139
140




    /* but make sure its prime */
    while (isprime (k) == 0) {
      k += 2;
    }
  }
  return 0;
}











>
>
>
>
134
135
136
137
138
139
140
141
142
143
144
    /* but make sure its prime */
    while (isprime (k) == 0) {
      k += 2;
    }
  }
  return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/mersenne.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/mont.c.
40
41
42
43
44
45
46




    return 0;
}
















>
>
>
>
40
41
42
43
44
45
46
47
48
49
50
    return 0;
}






/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/mont.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/pprime.c.
390
391
392
393
394
395
396




  mp_toradix (&p, buf, 10);
  printf ("P == %s\n", buf);
  mp_toradix (&q, buf, 10);
  printf ("Q == %s\n", buf);

  return 0;
}











>
>
>
>
390
391
392
393
394
395
396
397
398
399
400
  mp_toradix (&p, buf, 10);
  printf ("P == %s\n", buf);
  mp_toradix (&q, buf, 10);
  printf ("Q == %s\n", buf);

  return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/pprime.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/etc/tune.c.
132
133
134
135
136
137
138




     if (t2 < t1) break;
  }
  printf("KARATSUBA_MUL_CUTOFF = %d\n", y);
  printf("KARATSUBA_SQR_CUTOFF = %d\n", x);

  return 0;
}











>
>
>
>
132
133
134
135
136
137
138
139
140
141
142
     if (t2 < t1) break;
  }
  printf("KARATSUBA_MUL_CUTOFF = %d\n", y);
  printf("KARATSUBA_SQR_CUTOFF = %d\n", x);

  return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/etc/tune.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/logs/expt.log.
1
2
3
4
5
6
7
513   1489160
769   3688476
1025   8162061
2049  49260015
2561  89579052
3073 148797060
4097 324449263
|
|
|
|
|
|
|
1
2
3
4
5
6
7
513   1435869
769   3544970
1025   7791638
2049  46902238
2561  85334899
3073 141451412
4097 308770310
Changes to libtommath/logs/expt_2k.log.
1
2
3
4
5
607   2272809
1279   9557382
2203  36250309
3217  87666486
4253 174168369
|
|
|
|
|
1
2
3
4
5
607   2109225
1279  10148314
2203  34126877
3217  82716424
4253 161569606
Changes to libtommath/logs/expt_2kl.log.
1
2
3
4
1024   6954080
2048  35993987
4096 176068521
521   1683720
|
|
|
|
1
2
3
4
1024   7705271
2048  34286851
4096 165207491
521   1618631
Changes to libtommath/logs/expt_dr.log.
1
2
3
4
5
6
7
532   1989592
784   3898697
1036   6519700
1540  15676650
2072  33128187
3080  82963362
4116 168358337
|
|
|
|
|
|
|
1
2
3
4
5
6
7
532   1928550
784   3763908
1036   7564221
1540  16566059
2072  32283784
3080  79851565
4116 157843530
Changes to libtommath/logs/index.html.
18
19
20
21
22
23
24




<h1>Modular Inverse</h1>
<center><img src=invmod.png></center>
<hr>

</body>
</html>










>
>
>
18
19
20
21
22
23
24
25
26
27

<h1>Modular Inverse</h1>
<center><img src=invmod.png></center>
<hr>

</body>
</html>
/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/logs/index.html,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:16 $ */
Changes to libtommath/makefile.
1
2
3
4
5
6
7
8
9


10
11
12
13
14
15
16
17
18
19
20
21


22







23
24



25
26
27
28

29

30
31
32
33
34
35
36
#Makefile for GCC
#
#Tom St Denis

#version of library 
VERSION=0.35

CFLAGS  +=  -I./ -Wall -W -Wshadow -Wsign-compare



#for speed 
CFLAGS += -O3 -funroll-all-loops

#for size 
#CFLAGS += -Os

#x86 optimizations [should be valid for any GCC install though]
CFLAGS  += -fomit-frame-pointer

#debug
#CFLAGS += -g3



#install as this user







USER=root
GROUP=root




default: libtommath.a

#default files to install

LIBNAME=libtommath.a

HEADERS=tommath.h tommath_class.h tommath_superclass.h

#LIBPATH-The directory for libtommath to be installed to.
#INCPATH-The directory to install the header files for libtommath.
#DATAPATH-The directory to install the pdf docs.
DESTDIR=
LIBPATH=/usr/lib





|



>
>

|










>
>

>
>
>
>
>
>
>
|
<
>
>
>




>
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Makefile for GCC
#
#Tom St Denis

#version of library 
VERSION=0.36

CFLAGS  +=  -I./ -Wall -W -Wshadow -Wsign-compare

ifndef IGNORE_SPEED

#for speed 
CFLAGS += -O3 -funroll-loops

#for size 
#CFLAGS += -Os

#x86 optimizations [should be valid for any GCC install though]
CFLAGS  += -fomit-frame-pointer

#debug
#CFLAGS += -g3

endif

#install as this user
ifndef INSTALL_GROUP
   GROUP=wheel
else
   GROUP=$(INSTALL_GROUP)
endif

ifndef INSTALL_USER
   USER=root

else
   USER=$(INSTALL_USER)
endif

default: libtommath.a

#default files to install
ifndef LIBNAME
   LIBNAME=libtommath.a
endif
HEADERS=tommath.h tommath_class.h tommath_superclass.h

#LIBPATH-The directory for libtommath to be installed to.
#INCPATH-The directory to install the header files for libtommath.
#DATAPATH-The directory to install the pdf docs.
DESTDIR=
LIBPATH=/usr/lib
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o

libtommath.a:  $(OBJECTS)
	$(AR) $(ARFLAGS) libtommath.a $(OBJECTS)
	ranlib libtommath.a

#make a profiled library (takes a while!!!)
#
# This will build the library with profile generation
# then run the test demo and rebuild the library.
# 
# So far I've seen improvements in the MP math







|
|
|







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o

$(LIBNAME):  $(OBJECTS)
	$(AR) $(ARFLAGS) $@ $(OBJECTS)
	ranlib $@

#make a profiled library (takes a while!!!)
#
# This will build the library with profile generation
# then run the test demo and rebuild the library.
# 
# So far I've seen improvements in the MP math
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
profiled_single:
	perl gen.pl
	$(CC) $(CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o
	$(CC) $(CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -o ltmtest
	./ltmtest
	rm -f *.o ltmtest
	$(CC) $(CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o
	$(AR) $(ARFLAGS) libtommath.a mpi.o
	ranlib libtommath.a	

install: libtommath.a
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH)
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
	install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH)
	install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)

test: libtommath.a demo/demo.o
	$(CC) $(CFLAGS) demo/demo.o libtommath.a -o test
	
mtest: test	
	cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest
        
timing: libtommath.a
	$(CC) $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest

# makes the LTM book DVI file, requires tetex, perl and makeindex [part of tetex I think]
docdvi: tommath.src
	cd pics ; make 
	echo "hello" > tommath.ind
	perl booker.pl
	latex tommath > /dev/null







|
|

|





|
|




|
|







100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
profiled_single:
	perl gen.pl
	$(CC) $(CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o
	$(CC) $(CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -o ltmtest
	./ltmtest
	rm -f *.o ltmtest
	$(CC) $(CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o
	$(AR) $(ARFLAGS) $(LIBNAME) mpi.o
	ranlib $(LIBNAME)	

install: $(LIBNAME)
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH)
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
	install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH)
	install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)

test: $(LIBNAME) demo/demo.o
	$(CC) $(CFLAGS) demo/demo.o $(LIBNAME) -o test
	
mtest: test	
	cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest
        
timing: $(LIBNAME)
	$(CC) $(CFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o ltmtest

# makes the LTM book DVI file, requires tetex, perl and makeindex [part of tetex I think]
docdvi: tommath.src
	cd pics ; make 
	echo "hello" > tommath.ind
	perl booker.pl
	latex tommath > /dev/null
147
148
149
150
151
152
153






154
155
156
157
158
159
clean:
	rm -f *.bat *.pdf *.o *.a *.obj *.lib *.exe *.dll etclib/*.o demo/demo.o test ltmtest mpitest mtest/mtest mtest/mtest.exe \
        *.idx *.toc *.log *.aux *.dvi *.lof *.ind *.ilg *.ps *.log *.s mpi.c *.da *.dyn *.dpi tommath.tex `find -type f | grep [~] | xargs` *.lo *.la
	rm -rf .libs
	cd etc ; make clean
	cd pics ; make clean







zipup: clean manual poster docs
	perl gen.pl ; mv mpi.c pre_gen/ ; \
	cd .. ; rm -rf ltm* libtommath-$(VERSION) ; mkdir libtommath-$(VERSION) ; \
	cp -R ./libtommath/* ./libtommath-$(VERSION)/ ; \
	tar -c libtommath-$(VERSION)/* | bzip2 -9vvc > ltm-$(VERSION).tar.bz2 ; \
	zip -9 -r ltm-$(VERSION).zip libtommath-$(VERSION)/*







>
>
>
>
>
>






162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
clean:
	rm -f *.bat *.pdf *.o *.a *.obj *.lib *.exe *.dll etclib/*.o demo/demo.o test ltmtest mpitest mtest/mtest mtest/mtest.exe \
        *.idx *.toc *.log *.aux *.dvi *.lof *.ind *.ilg *.ps *.log *.s mpi.c *.da *.dyn *.dpi tommath.tex `find -type f | grep [~] | xargs` *.lo *.la
	rm -rf .libs
	cd etc ; make clean
	cd pics ; make clean

#zipup the project (take that!)
no_oops: clean
	cd .. ; cvs commit 
	echo Scanning for scratch/dirty files
	find . -type f | grep -v CVS | xargs -n 1 bash mess.sh

zipup: clean manual poster docs
	perl gen.pl ; mv mpi.c pre_gen/ ; \
	cd .. ; rm -rf ltm* libtommath-$(VERSION) ; mkdir libtommath-$(VERSION) ; \
	cp -R ./libtommath/* ./libtommath-$(VERSION)/ ; \
	tar -c libtommath-$(VERSION)/* | bzip2 -9vvc > ltm-$(VERSION).tar.bz2 ; \
	zip -9 -r ltm-$(VERSION).zip libtommath-$(VERSION)/*
Changes to libtommath/makefile.cygwin_dll.
45
46
47
48
49
50
51




	gcc -mno-cygwin -mdll -o libtommath.dll -Wl,--out-implib=libtommath.dll.a -Wl,--export-all-symbols *.o
	ranlib libtommath.dll.a

# build the test program using the windows DLL
test: $(OBJECTS) windll
	gcc $(CFLAGS) demo/demo.c libtommath.dll.a -Wl,--enable-auto-import -o test -s
	cd mtest ; $(CC) -O3 -fomit-frame-pointer -funroll-loops mtest.c -o mtest -s











>
>
>
>
45
46
47
48
49
50
51
52
53
54
55
	gcc -mno-cygwin -mdll -o libtommath.dll -Wl,--out-implib=libtommath.dll.a -Wl,--export-all-symbols *.o
	ranlib libtommath.dll.a

# build the test program using the windows DLL
test: $(OBJECTS) windll
	gcc $(CFLAGS) demo/demo.c libtommath.dll.a -Wl,--enable-auto-import -o test -s
	cd mtest ; $(CC) -O3 -fomit-frame-pointer -funroll-loops mtest.c -o mtest -s

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/makefile.cygwin_dll,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:31:57 $ */
Changes to libtommath/makefile.icc.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#   K - PIII
#   W - first P4 [Williamette]
#   N - P4 Northwood
#   P - P4 Prescott
#   B - Blend of P4 and PM [mobile]
#
# Default to just generic max opts
CFLAGS += -O3 -xN

#install as this user
USER=root
GROUP=root

default: libtommath.a








|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#   K - PIII
#   W - first P4 [Williamette]
#   N - P4 Northwood
#   P - P4 Prescott
#   B - Blend of P4 and PM [mobile]
#
# Default to just generic max opts
CFLAGS += -O3 -xP -ip

#install as this user
USER=root
GROUP=root

default: libtommath.a

Changes to libtommath/makefile.msvc.
1
2
3
4
5
6
7
8
9
10
11
12
#MSVC Makefile
#
#Tom St Denis

CFLAGS = /I. /Ox /DWIN32 /W4

default: library

OBJECTS=bncore.obj bn_mp_init.obj bn_mp_clear.obj bn_mp_exch.obj bn_mp_grow.obj bn_mp_shrink.obj \
bn_mp_clamp.obj bn_mp_zero.obj  bn_mp_set.obj bn_mp_set_int.obj bn_mp_init_size.obj bn_mp_copy.obj \
bn_mp_init_copy.obj bn_mp_abs.obj bn_mp_neg.obj bn_mp_cmp_mag.obj bn_mp_cmp.obj bn_mp_cmp_d.obj \
bn_mp_rshd.obj bn_mp_lshd.obj bn_mp_mod_2d.obj bn_mp_div_2d.obj bn_mp_mul_2d.obj bn_mp_div_2.obj \




|







1
2
3
4
5
6
7
8
9
10
11
12
#MSVC Makefile
#
#Tom St Denis

CFLAGS = /I. /Ox /DWIN32 /W3 /Fo$@

default: library

OBJECTS=bncore.obj bn_mp_init.obj bn_mp_clear.obj bn_mp_exch.obj bn_mp_grow.obj bn_mp_shrink.obj \
bn_mp_clamp.obj bn_mp_zero.obj  bn_mp_set.obj bn_mp_set_int.obj bn_mp_init_size.obj bn_mp_copy.obj \
bn_mp_init_copy.obj bn_mp_abs.obj bn_mp_neg.obj bn_mp_cmp_mag.obj bn_mp_cmp.obj bn_mp_cmp_d.obj \
bn_mp_rshd.obj bn_mp_lshd.obj bn_mp_mod_2d.obj bn_mp_div_2d.obj bn_mp_mul_2d.obj bn_mp_div_2.obj \
29
30
31
32
33
34
35


36
37
38
bn_mp_reduce_2k_l.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_2k_setup_l.obj \
bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \
bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \
bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \
bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \
bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj \
bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin_n.obj



library: $(OBJECTS)
	lib /out:tommath.lib $(OBJECTS)







>
>



29
30
31
32
33
34
35
36
37
38
39
40
bn_mp_reduce_2k_l.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_2k_setup_l.obj \
bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \
bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \
bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \
bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \
bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj \
bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin_n.obj

HEADERS=tommath.h tommath_class.h tommath_superclass.h

library: $(OBJECTS)
	lib /out:tommath.lib $(OBJECTS)
Changes to libtommath/makefile.shared.
1
2
3
4
5
6

7


8
9
10
11
12
13
14
15
16
17


18







19
20



21
22
23
24

25




26
27
28
29
30
31
32
#Makefile for GCC
#
#Tom St Denis
VERSION=0:35

CC = libtool --mode=compile gcc

CFLAGS  +=  -I./ -Wall -W -Wshadow -Wsign-compare



#for speed 
CFLAGS += -O3 -funroll-loops

#for size 
#CFLAGS += -Os

#x86 optimizations [should be valid for any GCC install though]
CFLAGS  += -fomit-frame-pointer



#install as this user







USER=root
GROUP=root




default: libtommath.la

#default files to install

LIBNAME=libtommath.la




HEADERS=tommath.h tommath_class.h tommath_superclass.h

#LIBPATH-The directory for libtommath to be installed to.
#INCPATH-The directory to install the header files for libtommath.
#DATAPATH-The directory to install the pdf docs.
DESTDIR=
LIBPATH=/usr/lib



|


>

>
>










>
>

>
>
>
>
>
>
>
|
<
>
>
>




>
|
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Makefile for GCC
#
#Tom St Denis
VERSION=0:36

CC = libtool --mode=compile gcc

CFLAGS  +=  -I./ -Wall -W -Wshadow -Wsign-compare

ifndef IGNORE_SPEED

#for speed 
CFLAGS += -O3 -funroll-loops

#for size 
#CFLAGS += -Os

#x86 optimizations [should be valid for any GCC install though]
CFLAGS  += -fomit-frame-pointer

endif

#install as this user
ifndef INSTALL_GROUP
   GROUP=wheel
else
   GROUP=$(INSTALL_GROUP)
endif

ifndef INSTALL_USER
   USER=root

else
   USER=$(INSTALL_USER)
endif

default: libtommath.la

#default files to install
ifndef LIBNAME
   LIBNAME=libtommath.la
endif
ifndef LIBNAME_S
   LIBNAME_S=libtommath.a
endif
HEADERS=tommath.h tommath_class.h tommath_superclass.h

#LIBPATH-The directory for libtommath to be installed to.
#INCPATH-The directory to install the header files for libtommath.
#DATAPATH-The directory to install the pdf docs.
DESTDIR=
LIBPATH=/usr/lib
57
58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
75
76
77
78
79
80
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o


libtommath.la:  $(OBJECTS)
	libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION)
	libtool --mode=link gcc *.o -o libtommath.a 

	libtool --mode=install install -c libtommath.la $(LIBPATH)/libtommath.la
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
	install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)

test: libtommath.a demo/demo.o
	gcc $(CFLAGS) -c demo/demo.c -o demo/demo.o
	libtool --mode=link gcc -o test demo/demo.o libtommath.la
	
mtest: test	
	cd mtest ; gcc $(CFLAGS) mtest.c -o mtest -s
        
timing: libtommath.la
	gcc $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest -s







<
|
|
|
>
|



|

|


|

|
|
76
77
78
79
80
81
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o


$(LIBNAME):  $(OBJECTS)
	libtool --mode=link gcc *.lo -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION)
	libtool --mode=link gcc *.o -o $(LIBNAME_S)
	ranlib $(LIBNAME_S)
	libtool --mode=install install -c $(LIBNAME) $(LIBPATH)/$@
	install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
	install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)

test: $(LIBNAME) demo/demo.o
	gcc $(CFLAGS) -c demo/demo.c -o demo/demo.o
	libtool --mode=link gcc -o test demo/demo.o $(LIBNAME_S)
	
mtest: test	
	cd mtest ; gcc $(CFLAGS) mtest.c -o mtest
        
timing: $(LIBNAME)
	gcc $(CFLAGS) -DTIMER demo/timing.c $(LIBNAME_S) -o ltmtest
Added libtommath/mess.sh.








>
>
>
>
1
2
3
4
#!/bin/bash
if cvs log $1 >/dev/null 2>/dev/null; then exit 0; else echo "$1 shouldn't be here" ; exit 1; fi


Changes to libtommath/mtest/logtab.h.
14
15
16
17
18
19
20




   0.179052232, 0.178103594, 0.177183820, 0.176291434, 	/* 48 49 50 51 */
   0.175425064, 0.174583430, 0.173765343, 0.172969690, 	/* 52 53 54 55 */
   0.172195434, 0.171441601, 0.170707280, 0.169991616, 	/* 56 57 58 59 */
   0.169293808, 0.168613099, 0.167948779, 0.167300179, 	/* 60 61 62 63 */
   0.166666667
};












>
>
>
>
14
15
16
17
18
19
20
21
22
23
24
   0.179052232, 0.178103594, 0.177183820, 0.176291434, 	/* 48 49 50 51 */
   0.175425064, 0.174583430, 0.173765343, 0.172969690, 	/* 52 53 54 55 */
   0.172195434, 0.171441601, 0.170707280, 0.169991616, 	/* 56 57 58 59 */
   0.169293808, 0.168613099, 0.167948779, 0.167300179, 	/* 60 61 62 63 */
   0.166666667
};


/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/logtab.h,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/mtest/mpi-config.h.
1
2
3
4
5
6
7
8
9
/* Default configuration for MPI library */
/* $Id: mpi-config.h,v 1.1.1.1 2005/01/19 22:41:29 kennykb Exp $ */

#ifndef MPI_CONFIG_H_
#define MPI_CONFIG_H_

/*
  For boolean options, 
  0 = no

|







1
2
3
4
5
6
7
8
9
/* Default configuration for MPI library */
/* $Id: mpi-config.h,v 1.1.1.2 2005/09/26 16:32:17 kennykb Exp $ */

#ifndef MPI_CONFIG_H_
#define MPI_CONFIG_H_

/*
  For boolean options, 
  0 = no
80
81
82
83
84
85
86




#define MP_COMPAT_MACROS 1   /* define compatibility macros?    */
#endif

#endif /* ifndef MPI_CONFIG_H_ */


/* crc==3287762869, version==2, Sat Feb 02 06:43:53 2002 */











>
>
>
>
80
81
82
83
84
85
86
87
88
89
90
#define MP_COMPAT_MACROS 1   /* define compatibility macros?    */
#endif

#endif /* ifndef MPI_CONFIG_H_ */


/* crc==3287762869, version==2, Sat Feb 02 06:43:53 2002 */

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/mpi-config.h,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/mtest/mpi-types.h.
10
11
12
13
14
15
16




#define MP_WORD_BIT        (CHAR_BIT*sizeof(mp_word))
#define MP_WORD_MAX        UINT_MAX

#define MP_DIGIT_SIZE      2
#define DIGIT_FMT          "%04X"
#define RADIX              (MP_DIGIT_MAX+1)












>
>
>
>
10
11
12
13
14
15
16
17
18
19
20
#define MP_WORD_BIT        (CHAR_BIT*sizeof(mp_word))
#define MP_WORD_MAX        UINT_MAX

#define MP_DIGIT_SIZE      2
#define DIGIT_FMT          "%04X"
#define RADIX              (MP_DIGIT_MAX+1)


/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/mpi-types.h,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/mtest/mpi.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
    mpi.c

    by Michael J. Fromberger <sting@linguist.dartmouth.edu>
    Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved

    Arbitrary precision integer arithmetic library

    $Id: mpi.c,v 1.1.1.1 2005/01/19 22:41:29 kennykb Exp $
 */

#include "mpi.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>









|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
    mpi.c

    by Michael J. Fromberger <sting@linguist.dartmouth.edu>
    Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved

    Arbitrary precision integer arithmetic library

    $Id: mpi.c,v 1.1.1.2 2005/09/26 16:32:17 kennykb Exp $
 */

#include "mpi.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

3975
3976
3977
3978
3979
3980
3981




/* }}} */

/* }}} */

/*------------------------------------------------------------------------*/
/* HERE THERE BE DRAGONS                                                  */
/* crc==4242132123, version==2, Sat Feb 02 06:43:52 2002 */











>
>
>
>
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
/* }}} */

/* }}} */

/*------------------------------------------------------------------------*/
/* HERE THERE BE DRAGONS                                                  */
/* crc==4242132123, version==2, Sat Feb 02 06:43:52 2002 */

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/mpi.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/mtest/mpi.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
    mpi.h

    by Michael J. Fromberger <sting@linguist.dartmouth.edu>
    Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved

    Arbitrary precision integer arithmetic library

    $Id: mpi.h,v 1.1.1.1 2005/01/19 22:41:29 kennykb Exp $
 */

#ifndef _H_MPI_
#define _H_MPI_

#include "mpi-config.h"









|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
    mpi.h

    by Michael J. Fromberger <sting@linguist.dartmouth.edu>
    Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved

    Arbitrary precision integer arithmetic library

    $Id: mpi.h,v 1.1.1.2 2005/09/26 16:32:17 kennykb Exp $
 */

#ifndef _H_MPI_
#define _H_MPI_

#include "mpi-config.h"

221
222
223
224
225
226
227





/*------------------------------------------------------------------------*/
/* Error strings                                                          */

const  char  *mp_strerror(mp_err ec);

#endif /* end _H_MPI_ */











>
>
>
>
221
222
223
224
225
226
227
228
229
230
231

/*------------------------------------------------------------------------*/
/* Error strings                                                          */

const  char  *mp_strerror(mp_err ec);

#endif /* end _H_MPI_ */

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/mpi.h,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/mtest/mtest.c.
298
299
300
301
302
303
304




      mp_to64(&b, buf);
      printf("%s\n", buf);
   }
   }
   fclose(rng);
   return 0;
}











>
>
>
>
298
299
300
301
302
303
304
305
306
307
308
      mp_to64(&b, buf);
      printf("%s\n", buf);
   }
   }
   fclose(rng);
   return 0;
}

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/mtest/mtest.c,v $ */
/* $Revision: 1.1.1.2 $ */
/* $Date: 2005/09/26 16:32:17 $ */
Changes to libtommath/poster.pdf.

cannot compute difference between binary files

Changes to libtommath/pre_gen/mpi.c.
39
40
41
42
43
44
45




46
47
48
49
50
51
52

   /* generic reply for invalid code */
   return "Invalid error code";
}

#endif





/* End: bn_error.c */

/* Start: bn_fast_mp_invmod.c */
#include <tommath.h>
#ifdef BN_FAST_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

   /* generic reply for invalid code */
   return "Invalid error code";
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_error.c */

/* Start: bn_fast_mp_invmod.c */
#include <tommath.h>
#ifdef BN_FAST_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
186
187
188
189
190
191
192




193
194
195
196
197
198
199
  c->sign = neg;
  res = MP_OKAY;

LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
  return res;
}
#endif





/* End: bn_fast_mp_invmod.c */

/* Start: bn_fast_mp_montgomery_reduce.c */
#include <tommath.h>
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  c->sign = neg;
  res = MP_OKAY;

LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_fast_mp_invmod.c */

/* Start: bn_fast_mp_montgomery_reduce.c */
#include <tommath.h>
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
358
359
360
361
362
363
364




365
366
367
368
369
370
371
  /* if A >= m then A = A - m */
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }
  return MP_OKAY;
}
#endif





/* End: bn_fast_mp_montgomery_reduce.c */

/* Start: bn_fast_s_mp_mul_digs.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  /* if A >= m then A = A - m */
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_fast_mp_montgomery_reduce.c */

/* Start: bn_fast_s_mp_mul_digs.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
434
435
436
437
438
439
440

441
442
443
444
445
446
447
         while (tx++ < a->used && ty-- >= 0) { ... }
       */
      iy = MIN(a->used-tx, ty+1);

      /* execute loop */
      for (iz = 0; iz < iy; ++iz) {
         _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);

      }

      /* store term */
      W[ix] = ((mp_digit)_W) & MP_MASK;

      /* make next carry */
      _W = _W >> ((mp_word)DIGIT_BIT);







>







446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
         while (tx++ < a->used && ty-- >= 0) { ... }
       */
      iy = MIN(a->used-tx, ty+1);

      /* execute loop */
      for (iz = 0; iz < iy; ++iz) {
         _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);

      }

      /* store term */
      W[ix] = ((mp_digit)_W) & MP_MASK;

      /* make next carry */
      _W = _W >> ((mp_word)DIGIT_BIT);
467
468
469
470
471
472
473




474
475
476
477
478
479
480
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif





/* End: bn_fast_s_mp_mul_digs.c */

/* Start: bn_fast_s_mp_mul_high_digs.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_fast_s_mp_mul_digs.c */

/* Start: bn_fast_s_mp_mul_high_digs.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
568
569
570
571
572
573
574




575
576
577
578
579
580
581
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif





/* End: bn_fast_s_mp_mul_high_digs.c */

/* Start: bn_fast_s_mp_sqr.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
      *tmpc++ = 0;
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_fast_s_mp_mul_high_digs.c */

/* Start: bn_fast_s_mp_sqr.c */
#include <tommath.h>
#ifdef BN_FAST_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
683
684
685
686
687
688
689




690
691
692
693
694
695
696
    }
  }
  mp_clamp (b);
  return MP_OKAY;
}
#endif





/* End: bn_fast_s_mp_sqr.c */

/* Start: bn_mp_2expt.c */
#include <tommath.h>
#ifdef BN_MP_2EXPT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
    }
  }
  mp_clamp (b);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_fast_s_mp_sqr.c */

/* Start: bn_mp_2expt.c */
#include <tommath.h>
#ifdef BN_MP_2EXPT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
731
732
733
734
735
736
737




738
739
740
741
742
743
744
  /* put the single bit in its place */
  a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);

  return MP_OKAY;
}
#endif





/* End: bn_mp_2expt.c */

/* Start: bn_mp_abs.c */
#include <tommath.h>
#ifdef BN_MP_ABS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  /* put the single bit in its place */
  a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_2expt.c */

/* Start: bn_mp_abs.c */
#include <tommath.h>
#ifdef BN_MP_ABS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
773
774
775
776
777
778
779




780
781
782
783
784
785
786

  /* force the sign of b to positive */
  b->sign = MP_ZPOS;

  return MP_OKAY;
}
#endif





/* End: bn_mp_abs.c */

/* Start: bn_mp_add.c */
#include <tommath.h>
#ifdef BN_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

  /* force the sign of b to positive */
  b->sign = MP_ZPOS;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_abs.c */

/* Start: bn_mp_add.c */
#include <tommath.h>
#ifdef BN_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
826
827
828
829
830
831
832




833
834
835
836
837
838
839
      res = s_mp_sub (a, b, c);
    }
  }
  return res;
}

#endif





/* End: bn_mp_add.c */

/* Start: bn_mp_add_d.c */
#include <tommath.h>
#ifdef BN_MP_ADD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
      res = s_mp_sub (a, b, c);
    }
  }
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_add.c */

/* Start: bn_mp_add_d.c */
#include <tommath.h>
#ifdef BN_MP_ADD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
936
937
938
939
940
941
942




943
944
945
946
947
948
949
  mp_clamp(c);

  return MP_OKAY;
}

#endif





/* End: bn_mp_add_d.c */

/* Start: bn_mp_addmod.c */
#include <tommath.h>
#ifdef BN_MP_ADDMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
  mp_clamp(c);

  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_add_d.c */

/* Start: bn_mp_addmod.c */
#include <tommath.h>
#ifdef BN_MP_ADDMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
976
977
978
979
980
981
982




983
984
985
986
987
988
989
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif





/* End: bn_mp_addmod.c */

/* Start: bn_mp_and.c */
#include <tommath.h>
#ifdef BN_MP_AND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_addmod.c */

/* Start: bn_mp_and.c */
#include <tommath.h>
#ifdef BN_MP_AND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1034
1035
1036
1037
1038
1039
1040




1041
1042
1043
1044
1045
1046
1047
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_mp_and.c */

/* Start: bn_mp_clamp.c */
#include <tommath.h>
#ifdef BN_MP_CLAMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_and.c */

/* Start: bn_mp_clamp.c */
#include <tommath.h>
#ifdef BN_MP_CLAMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1077
1078
1079
1080
1081
1082
1083




1084
1085
1086
1087
1088
1089
1090

  /* reset the sign flag if used == 0 */
  if (a->used == 0) {
    a->sign = MP_ZPOS;
  }
}
#endif





/* End: bn_mp_clamp.c */

/* Start: bn_mp_clear.c */
#include <tommath.h>
#ifdef BN_MP_CLEAR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143

  /* reset the sign flag if used == 0 */
  if (a->used == 0) {
    a->sign = MP_ZPOS;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_clamp.c */

/* Start: bn_mp_clear.c */
#include <tommath.h>
#ifdef BN_MP_CLEAR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1122
1123
1124
1125
1126
1127
1128




1129
1130
1131
1132
1133
1134
1135
    a->dp    = NULL;
    a->alloc = a->used = 0;
    a->sign  = MP_ZPOS;
  }
}
#endif





/* End: bn_mp_clear.c */

/* Start: bn_mp_clear_multi.c */
#include <tommath.h>
#ifdef BN_MP_CLEAR_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
    a->dp    = NULL;
    a->alloc = a->used = 0;
    a->sign  = MP_ZPOS;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_clear.c */

/* Start: bn_mp_clear_multi.c */
#include <tommath.h>
#ifdef BN_MP_CLEAR_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1155
1156
1157
1158
1159
1160
1161




1162
1163
1164
1165
1166
1167
1168
    while (next_mp != NULL) {
        mp_clear(next_mp);
        next_mp = va_arg(args, mp_int*);
    }
    va_end(args);
}
#endif





/* End: bn_mp_clear_multi.c */

/* Start: bn_mp_cmp.c */
#include <tommath.h>
#ifdef BN_MP_CMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
    while (next_mp != NULL) {
        mp_clear(next_mp);
        next_mp = va_arg(args, mp_int*);
    }
    va_end(args);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_clear_multi.c */

/* Start: bn_mp_cmp.c */
#include <tommath.h>
#ifdef BN_MP_CMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1199
1200
1201
1202
1203
1204
1205




1206
1207
1208
1209
1210
1211
1212
     return mp_cmp_mag(b, a);
  } else {
     return mp_cmp_mag(a, b);
  }
}
#endif





/* End: bn_mp_cmp.c */

/* Start: bn_mp_cmp_d.c */
#include <tommath.h>
#ifdef BN_MP_CMP_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
     return mp_cmp_mag(b, a);
  } else {
     return mp_cmp_mag(a, b);
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_cmp.c */

/* Start: bn_mp_cmp_d.c */
#include <tommath.h>
#ifdef BN_MP_CMP_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1242
1243
1244
1245
1246
1247
1248




1249
1250
1251
1252
1253
1254
1255
  } else if (a->dp[0] < b) {
    return MP_LT;
  } else {
    return MP_EQ;
  }
}
#endif





/* End: bn_mp_cmp_d.c */

/* Start: bn_mp_cmp_mag.c */
#include <tommath.h>
#ifdef BN_MP_CMP_MAG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  } else if (a->dp[0] < b) {
    return MP_LT;
  } else {
    return MP_EQ;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_cmp_d.c */

/* Start: bn_mp_cmp_mag.c */
#include <tommath.h>
#ifdef BN_MP_CMP_MAG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1298
1299
1300
1301
1302
1303
1304




1305
1306
1307
1308
1309
1310
1311
      return MP_LT;
    }
  }
  return MP_EQ;
}
#endif





/* End: bn_mp_cmp_mag.c */

/* Start: bn_mp_cnt_lsb.c */
#include <tommath.h>
#ifdef BN_MP_CNT_LSB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
      return MP_LT;
    }
  }
  return MP_EQ;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_cmp_mag.c */

/* Start: bn_mp_cnt_lsb.c */
#include <tommath.h>
#ifdef BN_MP_CNT_LSB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1350
1351
1352
1353
1354
1355
1356




1357
1358
1359
1360
1361
1362
1363
         q >>= 4;
      } while (qq == 0);
   }
   return x;
}

#endif





/* End: bn_mp_cnt_lsb.c */

/* Start: bn_mp_copy.c */
#include <tommath.h>
#ifdef BN_MP_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
         q >>= 4;
      } while (qq == 0);
   }
   return x;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_cnt_lsb.c */

/* Start: bn_mp_copy.c */
#include <tommath.h>
#ifdef BN_MP_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1419
1420
1421
1422
1423
1424
1425




1426
1427
1428
1429
1430
1431
1432
  /* copy used count and sign */
  b->used = a->used;
  b->sign = a->sign;
  return MP_OKAY;
}
#endif





/* End: bn_mp_copy.c */

/* Start: bn_mp_count_bits.c */
#include <tommath.h>
#ifdef BN_MP_COUNT_BITS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
  /* copy used count and sign */
  b->used = a->used;
  b->sign = a->sign;
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_copy.c */

/* Start: bn_mp_count_bits.c */
#include <tommath.h>
#ifdef BN_MP_COUNT_BITS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1463
1464
1465
1466
1467
1468
1469




1470
1471
1472
1473
1474
1475
1476
  while (q > ((mp_digit) 0)) {
    ++r;
    q >>= ((mp_digit) 1);
  }
  return r;
}
#endif





/* End: bn_mp_count_bits.c */

/* Start: bn_mp_div.c */
#include <tommath.h>
#ifdef BN_MP_DIV_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
  while (q > ((mp_digit) 0)) {
    ++r;
    q >>= ((mp_digit) 1);
  }
  return r;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_count_bits.c */

/* Start: bn_mp_div.c */
#include <tommath.h>
#ifdef BN_MP_DIV_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1756
1757
1758
1759
1760
1761
1762




1763
1764
1765
1766
1767
1768
1769
  return res;
}

#endif

#endif





/* End: bn_mp_div.c */

/* Start: bn_mp_div_2.c */
#include <tommath.h>
#ifdef BN_MP_DIV_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
  return res;
}

#endif

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_div.c */

/* Start: bn_mp_div_2.c */
#include <tommath.h>
#ifdef BN_MP_DIV_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1823
1824
1825
1826
1827
1828
1829




1830
1831
1832
1833
1834
1835
1836
    }
  }
  b->sign = a->sign;
  mp_clamp (b);
  return MP_OKAY;
}
#endif





/* End: bn_mp_div_2.c */

/* Start: bn_mp_div_2d.c */
#include <tommath.h>
#ifdef BN_MP_DIV_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
    }
  }
  b->sign = a->sign;
  mp_clamp (b);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_div_2.c */

/* Start: bn_mp_div_2d.c */
#include <tommath.h>
#ifdef BN_MP_DIV_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
1921
1922
1923
1924
1925
1926
1927




1928
1929
1930
1931
1932
1933
1934
    mp_exch (&t, d);
  }
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_mp_div_2d.c */

/* Start: bn_mp_div_3.c */
#include <tommath.h>
#ifdef BN_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
    mp_exch (&t, d);
  }
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_div_2d.c */

/* Start: bn_mp_div_3.c */
#include <tommath.h>
#ifdef BN_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
1999
2000
2001
2002
2003
2004
2005




2006
2007
2008
2009
2010
2011
2012
  }
  mp_clear(&q);
  
  return res;
}

#endif





/* End: bn_mp_div_3.c */

/* Start: bn_mp_div_d.c */
#include <tommath.h>
#ifdef BN_MP_DIV_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
  }
  mp_clear(&q);
  
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_div_3.c */

/* Start: bn_mp_div_d.c */
#include <tommath.h>
#ifdef BN_MP_DIV_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2110
2111
2112
2113
2114
2115
2116




2117
2118
2119
2120
2121
2122
2123
  mp_clear(&q);
  
  return res;
}

#endif





/* End: bn_mp_div_d.c */

/* Start: bn_mp_dr_is_modulus.c */
#include <tommath.h>
#ifdef BN_MP_DR_IS_MODULUS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
  mp_clear(&q);
  
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_div_d.c */

/* Start: bn_mp_dr_is_modulus.c */
#include <tommath.h>
#ifdef BN_MP_DR_IS_MODULUS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
2152
2153
2154
2155
2156
2157
2158




2159
2160
2161
2162
2163
2164
2165
          return 0;
       }
   }
   return 1;
}

#endif





/* End: bn_mp_dr_is_modulus.c */

/* Start: bn_mp_dr_reduce.c */
#include <tommath.h>
#ifdef BN_MP_DR_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
          return 0;
       }
   }
   return 1;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_dr_is_modulus.c */

/* Start: bn_mp_dr_reduce.c */
#include <tommath.h>
#ifdef BN_MP_DR_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2247
2248
2249
2250
2251
2252
2253




2254
2255
2256
2257
2258
2259
2260
    s_mp_sub(x, n, x);
    goto top;
  }
  return MP_OKAY;
}
#endif





/* End: bn_mp_dr_reduce.c */

/* Start: bn_mp_dr_setup.c */
#include <tommath.h>
#ifdef BN_MP_DR_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
    s_mp_sub(x, n, x);
    goto top;
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_dr_reduce.c */

/* Start: bn_mp_dr_setup.c */
#include <tommath.h>
#ifdef BN_MP_DR_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
2278
2279
2280
2281
2282
2283
2284




2285
2286
2287
2288
2289
2290
2291
    * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
    */
   *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
        ((mp_word)a->dp[0]));
}

#endif





/* End: bn_mp_dr_setup.c */

/* Start: bn_mp_exch.c */
#include <tommath.h>
#ifdef BN_MP_EXCH_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
    * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
    */
   *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
        ((mp_word)a->dp[0]));
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_dr_setup.c */

/* Start: bn_mp_exch.c */
#include <tommath.h>
#ifdef BN_MP_EXCH_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2312
2313
2314
2315
2316
2317
2318




2319
2320
2321
2322
2323
2324
2325
  mp_int  t;

  t  = *a;
  *a = *b;
  *b = t;
}
#endif





/* End: bn_mp_exch.c */

/* Start: bn_mp_expt_d.c */
#include <tommath.h>
#ifdef BN_MP_EXPT_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
  mp_int  t;

  t  = *a;
  *a = *b;
  *b = t;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_exch.c */

/* Start: bn_mp_expt_d.c */
#include <tommath.h>
#ifdef BN_MP_EXPT_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2369
2370
2371
2372
2373
2374
2375




2376
2377
2378
2379
2380
2381
2382
    b <<= 1;
  }

  mp_clear (&g);
  return MP_OKAY;
}
#endif





/* End: bn_mp_expt_d.c */

/* Start: bn_mp_exptmod.c */
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
    b <<= 1;
  }

  mp_clear (&g);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_expt_d.c */

/* Start: bn_mp_exptmod.c */
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
#else 
     /* no invmod */
     return MP_VAL;
#endif
  }

/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C)
  if (mp_reduce_is_2k_l(P) == MP_YES) {
     return s_mp_exptmod(G, X, P, Y, 1);
  }
#endif

#ifdef BN_MP_DR_IS_MODULUS_C
  /* is it a DR modulus? */







|







2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
#else 
     /* no invmod */
     return MP_VAL;
#endif
  }

/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
  if (mp_reduce_is_2k_l(P) == MP_YES) {
     return s_mp_exptmod(G, X, P, Y, 1);
  }
#endif

#ifdef BN_MP_DR_IS_MODULUS_C
  /* is it a DR modulus? */
2481
2482
2483
2484
2485
2486
2487




2488
2489
2490
2491
2492
2493
2494
#endif
#ifdef BN_MP_EXPTMOD_FAST_C
  }
#endif
}

#endif





/* End: bn_mp_exptmod.c */

/* Start: bn_mp_exptmod_fast.c */
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_FAST_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
#endif
#ifdef BN_MP_EXPTMOD_FAST_C
  }
#endif
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_exptmod.c */

/* Start: bn_mp_exptmod_fast.c */
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_FAST_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2803
2804
2805
2806
2807
2808
2809




2810
2811
2812
2813
2814
2815
2816
    mp_clear (&M[x]);
  }
  return err;
}
#endif






/* End: bn_mp_exptmod_fast.c */

/* Start: bn_mp_exteuclid.c */
#include <tommath.h>
#ifdef BN_MP_EXTEUCLID_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
    mp_clear (&M[x]);
  }
  return err;
}
#endif


/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_exptmod_fast.c */

/* Start: bn_mp_exteuclid.c */
#include <tommath.h>
#ifdef BN_MP_EXTEUCLID_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
2885
2886
2887
2888
2889
2890
2891




2892
2893
2894
2895
2896
2897
2898

   err = MP_OKAY;
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
   return err;
}
#endif





/* End: bn_mp_exteuclid.c */

/* Start: bn_mp_fread.c */
#include <tommath.h>
#ifdef BN_MP_FREAD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035

   err = MP_OKAY;
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
   return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_exteuclid.c */

/* Start: bn_mp_fread.c */
#include <tommath.h>
#ifdef BN_MP_FREAD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
2952
2953
2954
2955
2956
2957
2958




2959
2960
2961
2962
2963
2964
2965
   }
   
   return MP_OKAY;
}

#endif





/* End: bn_mp_fread.c */

/* Start: bn_mp_fwrite.c */
#include <tommath.h>
#ifdef BN_MP_FWRITE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
   }
   
   return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_fread.c */

/* Start: bn_mp_fwrite.c */
#include <tommath.h>
#ifdef BN_MP_FWRITE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3003
3004
3005
3006
3007
3008
3009




3010
3011
3012
3013
3014
3015
3016
   }
   
   XFREE (buf);
   return MP_OKAY;
}

#endif





/* End: bn_mp_fwrite.c */

/* Start: bn_mp_gcd.c */
#include <tommath.h>
#ifdef BN_MP_GCD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
   }
   
   XFREE (buf);
   return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_fwrite.c */

/* Start: bn_mp_gcd.c */
#include <tommath.h>
#ifdef BN_MP_GCD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
3117
3118
3119
3120
3121
3122
3123




3124
3125
3126
3127
3128
3129
3130
  res = MP_OKAY;
LBL_V:mp_clear (&u);
LBL_U:mp_clear (&v);
  return res;
}
#endif





/* End: bn_mp_gcd.c */

/* Start: bn_mp_get_int.c */
#include <tommath.h>
#ifdef BN_MP_GET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
  res = MP_OKAY;
LBL_V:mp_clear (&u);
LBL_U:mp_clear (&v);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_gcd.c */

/* Start: bn_mp_get_int.c */
#include <tommath.h>
#ifdef BN_MP_GET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3161
3162
3163
3164
3165
3166
3167




3168
3169
3170
3171
3172
3173
3174
    res = (res << DIGIT_BIT) | DIGIT(a,i);
  }

  /* force result to 32-bits always so it is consistent on non 32-bit platforms */
  return res & 0xFFFFFFFFUL;
}
#endif





/* End: bn_mp_get_int.c */

/* Start: bn_mp_grow.c */
#include <tommath.h>
#ifdef BN_MP_GROW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
    res = (res << DIGIT_BIT) | DIGIT(a,i);
  }

  /* force result to 32-bits always so it is consistent on non 32-bit platforms */
  return res & 0xFFFFFFFFUL;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_get_int.c */

/* Start: bn_mp_grow.c */
#include <tommath.h>
#ifdef BN_MP_GROW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
3219
3220
3221
3222
3223
3224
3225




3226
3227
3228
3229
3230
3231
3232
      a->dp[i] = 0;
    }
  }
  return MP_OKAY;
}
#endif





/* End: bn_mp_grow.c */

/* Start: bn_mp_init.c */
#include <tommath.h>
#ifdef BN_MP_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
      a->dp[i] = 0;
    }
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_grow.c */

/* Start: bn_mp_init.c */
#include <tommath.h>
#ifdef BN_MP_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3265
3266
3267
3268
3269
3270
3271




3272
3273
3274
3275
3276
3277
3278
  a->alloc = MP_PREC;
  a->sign  = MP_ZPOS;

  return MP_OKAY;
}
#endif





/* End: bn_mp_init.c */

/* Start: bn_mp_init_copy.c */
#include <tommath.h>
#ifdef BN_MP_INIT_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
  a->alloc = MP_PREC;
  a->sign  = MP_ZPOS;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init.c */

/* Start: bn_mp_init_copy.c */
#include <tommath.h>
#ifdef BN_MP_INIT_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3296
3297
3298
3299
3300
3301
3302




3303
3304
3305
3306
3307
3308
3309

  if ((res = mp_init (a)) != MP_OKAY) {
    return res;
  }
  return mp_copy (b, a);
}
#endif





/* End: bn_mp_init_copy.c */

/* Start: bn_mp_init_multi.c */
#include <tommath.h>
#ifdef BN_MP_INIT_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474

  if ((res = mp_init (a)) != MP_OKAY) {
    return res;
  }
  return mp_copy (b, a);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init_copy.c */

/* Start: bn_mp_init_multi.c */
#include <tommath.h>
#ifdef BN_MP_INIT_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
3356
3357
3358
3359
3360
3361
3362




3363
3364
3365
3366
3367
3368
3369
    }
    va_end(args);
    return res;                /* Assumed ok, if error flagged above. */
}

#endif





/* End: bn_mp_init_multi.c */

/* Start: bn_mp_init_set.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
    }
    va_end(args);
    return res;                /* Assumed ok, if error flagged above. */
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init_multi.c */

/* Start: bn_mp_init_set.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3388
3389
3390
3391
3392
3393
3394




3395
3396
3397
3398
3399
3400
3401
     return err;
  }
  mp_set(a, b);
  return err;
}
#endif





/* End: bn_mp_init_set.c */

/* Start: bn_mp_init_set_int.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
     return err;
  }
  mp_set(a, b);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init_set.c */

/* Start: bn_mp_init_set_int.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3418
3419
3420
3421
3422
3423
3424




3425
3426
3427
3428
3429
3430
3431
  int err;
  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  return mp_set_int(a, b);
}
#endif





/* End: bn_mp_init_set_int.c */

/* Start: bn_mp_init_size.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
  int err;
  if ((err = mp_init(a)) != MP_OKAY) {
     return err;
  }
  return mp_set_int(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init_set_int.c */

/* Start: bn_mp_init_size.c */
#include <tommath.h>
#ifdef BN_MP_INIT_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
3467
3468
3469
3470
3471
3472
3473




3474
3475
3476
3477
3478
3479
3480
      a->dp[x] = 0;
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_init_size.c */

/* Start: bn_mp_invmod.c */
#include <tommath.h>
#ifdef BN_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
      a->dp[x] = 0;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_init_size.c */

/* Start: bn_mp_invmod.c */
#include <tommath.h>
#ifdef BN_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3509
3510
3511
3512
3513
3514
3515




3516
3517
3518
3519
3520
3521
3522
#ifdef BN_MP_INVMOD_SLOW_C
  return mp_invmod_slow(a, b, c);
#endif

  return MP_VAL;
}
#endif





/* End: bn_mp_invmod.c */

/* Start: bn_mp_invmod_slow.c */
#include <tommath.h>
#ifdef BN_MP_INVMOD_SLOW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
#ifdef BN_MP_INVMOD_SLOW_C
  return mp_invmod_slow(a, b, c);
#endif

  return MP_VAL;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_invmod.c */

/* Start: bn_mp_invmod_slow.c */
#include <tommath.h>
#ifdef BN_MP_INVMOD_SLOW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
3685
3686
3687
3688
3689
3690
3691




3692
3693
3694
3695
3696
3697
3698
  mp_exch (&C, c);
  res = MP_OKAY;
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  return res;
}
#endif





/* End: bn_mp_invmod_slow.c */

/* Start: bn_mp_is_square.c */
#include <tommath.h>
#ifdef BN_MP_IS_SQUARE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
  mp_exch (&C, c);
  res = MP_OKAY;
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_invmod_slow.c */

/* Start: bn_mp_is_square.c */
#include <tommath.h>
#ifdef BN_MP_IS_SQUARE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3794
3795
3796
3797
3798
3799
3800




3801
3802
3803
3804
3805
3806
3807

  *ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t);
  return res;
}
#endif





/* End: bn_mp_is_square.c */

/* Start: bn_mp_jacobi.c */
#include <tommath.h>
#ifdef BN_MP_JACOBI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000

  *ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_is_square.c */

/* Start: bn_mp_jacobi.c */
#include <tommath.h>
#ifdef BN_MP_JACOBI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3899
3900
3901
3902
3903
3904
3905




3906
3907
3908
3909
3910
3911
3912
  res = MP_OKAY;
LBL_P1:mp_clear (&p1);
LBL_A1:mp_clear (&a1);
  return res;
}
#endif





/* End: bn_mp_jacobi.c */

/* Start: bn_mp_karatsuba_mul.c */
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
  res = MP_OKAY;
LBL_P1:mp_clear (&p1);
LBL_A1:mp_clear (&a1);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_jacobi.c */

/* Start: bn_mp_karatsuba_mul.c */
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
 * let n represent half of the number of digits in 
 * the min(a,b)
 *
 * a = a1 * B**n + a0
 * b = b1 * B**n + b0
 *
 * Then, a * b => 
   a1b1 * B**2n + ((a1 - a0)(b1 - b0) + a0b0 + a1b1) * B + a0b0
 *
 * Note that a1b1 and a0b0 are used twice and only need to be 
 * computed once.  So in total three half size (half # of 
 * digit) multiplications are performed, a0b0, a1b1 and 
 * (a1-b1)(a0-b0)
 *
 * Note that a multiplication of half the digits requires
 * 1/4th the number of single precision multiplications so in 
 * total after one call 25% of the single precision multiplications 
 * are saved.  Note also that the call to mp_mul can end up back 
 * in this function if the a0, a1, b0, or b1 are above the threshold.  
 * This is known as divide-and-conquer and leads to the famous 







|




|







4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
 * let n represent half of the number of digits in 
 * the min(a,b)
 *
 * a = a1 * B**n + a0
 * b = b1 * B**n + b0
 *
 * Then, a * b => 
   a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
 *
 * Note that a1b1 and a0b0 are used twice and only need to be 
 * computed once.  So in total three half size (half # of 
 * digit) multiplications are performed, a0b0, a1b1 and 
 * (a1+b1)(a0+b0)
 *
 * Note that a multiplication of half the digits requires
 * 1/4th the number of single precision multiplications so in 
 * total after one call 25% of the single precision multiplications 
 * are saved.  Note also that the call to mp_mul can end up back 
 * in this function if the a0, a1, b0, or b1 are above the threshold.  
 * This is known as divide-and-conquer and leads to the famous 
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
  /* now calc the products x0y0 and x1y1 */
  /* after this x0 is no longer required, free temp [x0==t2]! */
  if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
    goto X1Y1;          /* x0y0 = x0*y0 */
  if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1*y1 */

  /* now calc x1-x0 and y1-y0 */
  if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x1 - x0 */
  if (mp_sub (&y1, &y0, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = y1 - y0 */
  if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1 - x0) * (y1 - y0) */

  /* add x0y0 */
  if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = x0y0 + x1y1 */
  if (mp_sub (&x0, &t1, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x0y0 + x1y1 - (x1-x0)*(y1-y0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1y1 << 2*B */








|
|

|


|




|
|







4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
  /* now calc the products x0y0 and x1y1 */
  /* after this x0 is no longer required, free temp [x0==t2]! */
  if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
    goto X1Y1;          /* x0y0 = x0*y0 */
  if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1*y1 */

  /* now calc x1+x0 and y1+y0 */
  if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = x1 - x0 */
  if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = y1 - y0 */
  if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1 + x0) * (y1 + y0) */

  /* add x0y0 */
  if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
    goto X1Y1;          /* t2 = x0y0 + x1y1 */
  if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
    goto X1Y1;          /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
    goto X1Y1;          /* x1y1 = x1y1 << 2*B */

4065
4066
4067
4068
4069
4070
4071




4072
4073
4074
4075
4076
4077
4078
Y0:mp_clear (&y0);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif





/* End: bn_mp_karatsuba_mul.c */

/* Start: bn_mp_karatsuba_sqr.c */
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
Y0:mp_clear (&y0);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_karatsuba_mul.c */

/* Start: bn_mp_karatsuba_sqr.c */
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175

  /* now calc the products x0*x0 and x1*x1 */
  if (mp_sqr (&x0, &x0x0) != MP_OKAY)
    goto X1X1;           /* x0x0 = x0*x0 */
  if (mp_sqr (&x1, &x1x1) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1*x1 */

  /* now calc (x1-x0)**2 */
  if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x1 - x0 */
  if (mp_sqr (&t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */

  /* add x0y0 */
  if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
    goto X1X1;           /* t2 = x0x0 + x1x1 */
  if (mp_sub (&t2, &t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x0x0 + x1x1 - (x1-x0)*(x1-x0) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
  if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1x1 << 2*B */








|
|







|
|







4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376

  /* now calc the products x0*x0 and x1*x1 */
  if (mp_sqr (&x0, &x0x0) != MP_OKAY)
    goto X1X1;           /* x0x0 = x0*x0 */
  if (mp_sqr (&x1, &x1x1) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1*x1 */

  /* now calc (x1+x0)**2 */
  if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = x1 - x0 */
  if (mp_sqr (&t1, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */

  /* add x0y0 */
  if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
    goto X1X1;           /* t2 = x0x0 + x1x1 */
  if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
    goto X1X1;           /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */

  /* shift by B */
  if (mp_lshd (&t1, B) != MP_OKAY)
    goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
  if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
    goto X1X1;           /* x1x1 = x1x1 << 2*B */

4186
4187
4188
4189
4190
4191
4192




4193
4194
4195
4196
4197
4198
4199
T1:mp_clear (&t1);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif





/* End: bn_mp_karatsuba_sqr.c */

/* Start: bn_mp_lcm.c */
#include <tommath.h>
#ifdef BN_MP_LCM_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
T1:mp_clear (&t1);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_karatsuba_sqr.c */

/* Start: bn_mp_lcm.c */
#include <tommath.h>
#ifdef BN_MP_LCM_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4246
4247
4248
4249
4250
4251
4252




4253
4254
4255
4256
4257
4258
4259
  c->sign = MP_ZPOS;

LBL_T:
  mp_clear_multi (&t1, &t2, NULL);
  return res;
}
#endif





/* End: bn_mp_lcm.c */

/* Start: bn_mp_lshd.c */
#include <tommath.h>
#ifdef BN_MP_LSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
  c->sign = MP_ZPOS;

LBL_T:
  mp_clear_multi (&t1, &t2, NULL);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_lcm.c */

/* Start: bn_mp_lshd.c */
#include <tommath.h>
#ifdef BN_MP_LSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4314
4315
4316
4317
4318
4319
4320




4321
4322
4323
4324
4325
4326
4327
      *top++ = 0;
    }
  }
  return MP_OKAY;
}
#endif





/* End: bn_mp_lshd.c */

/* Start: bn_mp_mod.c */
#include <tommath.h>
#ifdef BN_MP_MOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
      *top++ = 0;
    }
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_lshd.c */

/* Start: bn_mp_mod.c */
#include <tommath.h>
#ifdef BN_MP_MOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
4361
4362
4363
4364
4365
4366
4367




4368
4369
4370
4371
4372
4373
4374
    mp_exch (&t, c);
  }

  mp_clear (&t);
  return res;
}
#endif





/* End: bn_mp_mod.c */

/* Start: bn_mp_mod_2d.c */
#include <tommath.h>
#ifdef BN_MP_MOD_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
    mp_exch (&t, c);
  }

  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mod.c */

/* Start: bn_mp_mod_2d.c */
#include <tommath.h>
#ifdef BN_MP_MOD_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4417
4418
4419
4420
4421
4422
4423




4424
4425
4426
4427
4428
4429
4430
  c->dp[b / DIGIT_BIT] &=
    (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  mp_clamp (c);
  return MP_OKAY;
}
#endif





/* End: bn_mp_mod_2d.c */

/* Start: bn_mp_mod_d.c */
#include <tommath.h>
#ifdef BN_MP_MOD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
  c->dp[b / DIGIT_BIT] &=
    (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mod_2d.c */

/* Start: bn_mp_mod_d.c */
#include <tommath.h>
#ifdef BN_MP_MOD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
4443
4444
4445
4446
4447
4448
4449




4450
4451
4452
4453
4454
4455
4456

int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
{
  return mp_div_d(a, b, NULL, c);
}
#endif





/* End: bn_mp_mod_d.c */

/* Start: bn_mp_montgomery_calc_normalization.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681

int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
{
  return mp_div_d(a, b, NULL, c);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mod_d.c */

/* Start: bn_mp_montgomery_calc_normalization.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4502
4503
4504
4505
4506
4507
4508




4509
4510
4511
4512
4513
4514
4515
      }
    }
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_montgomery_calc_normalization.c */

/* Start: bn_mp_montgomery_reduce.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
      }
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_montgomery_calc_normalization.c */

/* Start: bn_mp_montgomery_reduce.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4620
4621
4622
4623
4624
4625
4626




4627
4628
4629
4630
4631
4632
4633
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_montgomery_reduce.c */

/* Start: bn_mp_montgomery_setup.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
  if (mp_cmp_mag (x, n) != MP_LT) {
    return s_mp_sub (x, n, x);
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_montgomery_reduce.c */

/* Start: bn_mp_montgomery_setup.c */
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4679
4680
4681
4682
4683
4684
4685




4686
4687
4688
4689
4690
4691
4692

  /* rho = -1/m mod b */
  *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;

  return MP_OKAY;
}
#endif





/* End: bn_mp_montgomery_setup.c */

/* Start: bn_mp_mul.c */
#include <tommath.h>
#ifdef BN_MP_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929

  /* rho = -1/m mod b */
  *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_montgomery_setup.c */

/* Start: bn_mp_mul.c */
#include <tommath.h>
#ifdef BN_MP_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4745
4746
4747
4748
4749
4750
4751




4752
4753
4754
4755
4756
4757
4758
#endif

  }
  c->sign = (c->used > 0) ? neg : MP_ZPOS;
  return res;
}
#endif





/* End: bn_mp_mul.c */

/* Start: bn_mp_mul_2.c */
#include <tommath.h>
#ifdef BN_MP_MUL_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
#endif

  }
  c->sign = (c->used > 0) ? neg : MP_ZPOS;
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mul.c */

/* Start: bn_mp_mul_2.c */
#include <tommath.h>
#ifdef BN_MP_MUL_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4827
4828
4829
4830
4831
4832
4833




4834
4835
4836
4837
4838
4839
4840
      *tmpb++ = 0;
    }
  }
  b->sign = a->sign;
  return MP_OKAY;
}
#endif





/* End: bn_mp_mul_2.c */

/* Start: bn_mp_mul_2d.c */
#include <tommath.h>
#ifdef BN_MP_MUL_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
      *tmpb++ = 0;
    }
  }
  b->sign = a->sign;
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mul_2.c */

/* Start: bn_mp_mul_2d.c */
#include <tommath.h>
#ifdef BN_MP_MUL_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4913
4914
4915
4916
4917
4918
4919




4920
4921
4922
4923
4924
4925
4926
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif





/* End: bn_mp_mul_2d.c */

/* Start: bn_mp_mul_d.c */
#include <tommath.h>
#ifdef BN_MP_MUL_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
    }
  }
  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mul_2d.c */

/* Start: bn_mp_mul_d.c */
#include <tommath.h>
#ifdef BN_MP_MUL_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
4992
4993
4994
4995
4996
4997
4998




4999
5000
5001
5002
5003
5004
5005
  c->used = a->used + 1;
  mp_clamp(c);

  return MP_OKAY;
}
#endif





/* End: bn_mp_mul_d.c */

/* Start: bn_mp_mulmod.c */
#include <tommath.h>
#ifdef BN_MP_MULMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
  c->used = a->used + 1;
  mp_clamp(c);

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mul_d.c */

/* Start: bn_mp_mulmod.c */
#include <tommath.h>
#ifdef BN_MP_MULMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038




5039
5040
5041
5042
5043
5044
5045
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* d = a * b (mod c) */
int
mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
  int     res;
  mp_int  t;

  if ((res = mp_init (&t)) != MP_OKAY) {
    return res;
  }

  if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
    mp_clear (&t);
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif





/* End: bn_mp_mulmod.c */

/* Start: bn_mp_n_root.c */
#include <tommath.h>
#ifdef BN_MP_N_ROOT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







<
|

















>
>
>
>







5266
5267
5268
5269
5270
5271
5272

5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* d = a * b (mod c) */

int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
  int     res;
  mp_int  t;

  if ((res = mp_init (&t)) != MP_OKAY) {
    return res;
  }

  if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
    mp_clear (&t);
    return res;
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_mulmod.c */

/* Start: bn_mp_n_root.c */
#include <tommath.h>
#ifdef BN_MP_N_ROOT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5165
5166
5167
5168
5169
5170
5171




5172
5173
5174
5175
5176
5177
5178
LBL_T3:mp_clear (&t3);
LBL_T2:mp_clear (&t2);
LBL_T1:mp_clear (&t1);
  return res;
}
#endif





/* End: bn_mp_n_root.c */

/* Start: bn_mp_neg.c */
#include <tommath.h>
#ifdef BN_MP_NEG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
LBL_T3:mp_clear (&t3);
LBL_T2:mp_clear (&t2);
LBL_T1:mp_clear (&t1);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_n_root.c */

/* Start: bn_mp_neg.c */
#include <tommath.h>
#ifdef BN_MP_NEG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
5204
5205
5206
5207
5208
5209
5210




5211
5212
5213
5214
5215
5216
5217
  } else {
     b->sign = MP_ZPOS;
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_neg.c */

/* Start: bn_mp_or.c */
#include <tommath.h>
#ifdef BN_MP_OR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
  } else {
     b->sign = MP_ZPOS;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_neg.c */

/* Start: bn_mp_or.c */
#include <tommath.h>
#ifdef BN_MP_OR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5254
5255
5256
5257
5258
5259
5260




5261
5262
5263
5264
5265
5266
5267
  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_mp_or.c */

/* Start: bn_mp_prime_fermat.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_FERMAT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
  }
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_or.c */

/* Start: bn_mp_prime_fermat.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_FERMAT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5317
5318
5319
5320
5321
5322
5323




5324
5325
5326
5327
5328
5329
5330

  err = MP_OKAY;
LBL_T:mp_clear (&t);
  return err;
}
#endif





/* End: bn_mp_prime_fermat.c */

/* Start: bn_mp_prime_is_divisible.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602

  err = MP_OKAY;
LBL_T:mp_clear (&t);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_fermat.c */

/* Start: bn_mp_prime_is_divisible.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
5366
5367
5368
5369
5370
5371
5372




5373
5374
5375
5376
5377
5378
5379
      return MP_OKAY;
    }
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_prime_is_divisible.c */

/* Start: bn_mp_prime_is_prime.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
      return MP_OKAY;
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_is_divisible.c */

/* Start: bn_mp_prime_is_prime.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5449
5450
5451
5452
5453
5454
5455




5456
5457
5458
5459
5460
5461
5462

  /* passed the test */
  *result = MP_YES;
LBL_B:mp_clear (&b);
  return err;
}
#endif





/* End: bn_mp_prime_is_prime.c */

/* Start: bn_mp_prime_miller_rabin.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_MILLER_RABIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742

  /* passed the test */
  *result = MP_YES;
LBL_B:mp_clear (&b);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_is_prime.c */

/* Start: bn_mp_prime_miller_rabin.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_MILLER_RABIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5552
5553
5554
5555
5556
5557
5558




5559
5560
5561
5562
5563
5564
5565
  *result = MP_YES;
LBL_Y:mp_clear (&y);
LBL_R:mp_clear (&r);
LBL_N1:mp_clear (&n1);
  return err;
}
#endif





/* End: bn_mp_prime_miller_rabin.c */

/* Start: bn_mp_prime_next_prime.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_NEXT_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
  *result = MP_YES;
LBL_Y:mp_clear (&y);
LBL_R:mp_clear (&r);
LBL_N1:mp_clear (&n1);
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_miller_rabin.c */

/* Start: bn_mp_prime_next_prime.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_NEXT_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5722
5723
5724
5725
5726
5727
5728




5729
5730
5731
5732
5733
5734
5735
   err = MP_OKAY;
LBL_ERR:
   mp_clear(&b);
   return err;
}

#endif





/* End: bn_mp_prime_next_prime.c */

/* Start: bn_mp_prime_rabin_miller_trials.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
   err = MP_OKAY;
LBL_ERR:
   mp_clear(&b);
   return err;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_next_prime.c */

/* Start: bn_mp_prime_rabin_miller_trials.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5774
5775
5776
5777
5778
5779
5780




5781
5782
5783
5784
5785
5786
5787
       }
   }
   return sizes[x-1].t + 1;
}


#endif





/* End: bn_mp_prime_rabin_miller_trials.c */

/* Start: bn_mp_prime_random_ex.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_RANDOM_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
       }
   }
   return sizes[x-1].t + 1;
}


#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_rabin_miller_trials.c */

/* Start: bn_mp_prime_random_ex.c */
#include <tommath.h>
#ifdef BN_MP_PRIME_RANDOM_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
   /* calc the maskAND value for the MSbyte*/
   maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));

   /* calc the maskOR_msb */
   maskOR_msb        = 0;
   maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
   if (flags & LTM_PRIME_2MSB_ON) {
      maskOR_msb     |= 1 << ((size - 2) & 7);
   } else if (flags & LTM_PRIME_2MSB_OFF) {
      maskAND        &= ~(1 << ((size - 2) & 7));
   } 

   /* get the maskOR_lsb */
   maskOR_lsb         = 1;
   if (flags & LTM_PRIME_BBS) {
      maskOR_lsb     |= 3;
   }








|
<
<
|







6134
6135
6136
6137
6138
6139
6140
6141


6142
6143
6144
6145
6146
6147
6148
6149
   /* calc the maskAND value for the MSbyte*/
   maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));

   /* calc the maskOR_msb */
   maskOR_msb        = 0;
   maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
   if (flags & LTM_PRIME_2MSB_ON) {
      maskOR_msb       |= 0x80 >> ((9 - size) & 7);


   }  

   /* get the maskOR_lsb */
   maskOR_lsb         = 1;
   if (flags & LTM_PRIME_BBS) {
      maskOR_lsb     |= 3;
   }

5901
5902
5903
5904
5905
5906
5907




5908
5909
5910
5911
5912
5913
5914
error:
   XFREE(tmp);
   return err;
}


#endif





/* End: bn_mp_prime_random_ex.c */

/* Start: bn_mp_radix_size.c */
#include <tommath.h>
#ifdef BN_MP_RADIX_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
error:
   XFREE(tmp);
   return err;
}


#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_prime_random_ex.c */

/* Start: bn_mp_radix_size.c */
#include <tommath.h>
#ifdef BN_MP_RADIX_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5980
5981
5982
5983
5984
5985
5986




5987
5988
5989
5990
5991
5992
5993
  /* return digs + 1, the 1 is for the NULL byte that would be required. */
  *size = digs + 1;
  return MP_OKAY;
}

#endif





/* End: bn_mp_radix_size.c */

/* Start: bn_mp_radix_smap.c */
#include <tommath.h>
#ifdef BN_MP_RADIX_SMAP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
  /* return digs + 1, the 1 is for the NULL byte that would be required. */
  *size = digs + 1;
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_radix_size.c */

/* Start: bn_mp_radix_smap.c */
#include <tommath.h>
#ifdef BN_MP_RADIX_SMAP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6003
6004
6005
6006
6007
6008
6009




6010
6011
6012
6013
6014
6015
6016
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* chars used in radix conversions */
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#endif





/* End: bn_mp_radix_smap.c */

/* Start: bn_mp_rand.c */
#include <tommath.h>
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* chars used in radix conversions */
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_radix_smap.c */

/* Start: bn_mp_rand.c */
#include <tommath.h>
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6058
6059
6060
6061
6062
6063
6064




6065
6066
6067
6068
6069
6070
6071
      return res;
    }
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_rand.c */

/* Start: bn_mp_read_radix.c */
#include <tommath.h>
#ifdef BN_MP_READ_RADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
      return res;
    }
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_rand.c */

/* Start: bn_mp_read_radix.c */
#include <tommath.h>
#ifdef BN_MP_READ_RADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6141
6142
6143
6144
6145
6146
6147




6148
6149
6150
6151
6152
6153
6154
  if (mp_iszero(a) != 1) {
     a->sign = neg;
  }
  return MP_OKAY;
}
#endif





/* End: bn_mp_read_radix.c */

/* Start: bn_mp_read_signed_bin.c */
#include <tommath.h>
#ifdef BN_MP_READ_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
  if (mp_iszero(a) != 1) {
     a->sign = neg;
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_read_radix.c */

/* Start: bn_mp_read_signed_bin.c */
#include <tommath.h>
#ifdef BN_MP_READ_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188




6189
6190
6191
6192
6193
6194
6195
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* read signed bin, big endian, first byte is 0==positive or 1==negative */
int
mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
{
  int     res;

  /* read magnitude */
  if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
    return res;
  }

  /* first byte is 0 for positive, non-zero for negative */
  if (b[0] == 0) {
     a->sign = MP_ZPOS;
  } else {
     a->sign = MP_NEG;
  }

  return MP_OKAY;
}
#endif





/* End: bn_mp_read_signed_bin.c */

/* Start: bn_mp_read_unsigned_bin.c */
#include <tommath.h>
#ifdef BN_MP_READ_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







<
|


















>
>
>
>







6472
6473
6474
6475
6476
6477
6478

6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* read signed bin, big endian, first byte is 0==positive or 1==negative */

int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
{
  int     res;

  /* read magnitude */
  if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
    return res;
  }

  /* first byte is 0 for positive, non-zero for negative */
  if (b[0] == 0) {
     a->sign = MP_ZPOS;
  } else {
     a->sign = MP_NEG;
  }

  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_read_signed_bin.c */

/* Start: bn_mp_read_unsigned_bin.c */
#include <tommath.h>
#ifdef BN_MP_READ_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* reads a unsigned char array, assumes the msb is stored first [big endian] */
int
mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
{
  int     res;

  /* make sure there are at least two digits */
  if (a->alloc < 2) {
     if ((res = mp_grow(a, 2)) != MP_OKAY) {
        return res;







<
|







6517
6518
6519
6520
6521
6522
6523

6524
6525
6526
6527
6528
6529
6530
6531
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* reads a unsigned char array, assumes the msb is stored first [big endian] */

int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
{
  int     res;

  /* make sure there are at least two digits */
  if (a->alloc < 2) {
     if ((res = mp_grow(a, 2)) != MP_OKAY) {
        return res;
6238
6239
6240
6241
6242
6243
6244




6245
6246
6247
6248
6249
6250
6251
      a->used += 2;
#endif
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif





/* End: bn_mp_read_unsigned_bin.c */

/* Start: bn_mp_reduce.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
      a->used += 2;
#endif
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_read_unsigned_bin.c */

/* Start: bn_mp_reduce.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6339
6340
6341
6342
6343
6344
6345




6346
6347
6348
6349
6350
6351
6352
CLEANUP:
  mp_clear (&q);

  return res;
}
#endif





/* End: bn_mp_reduce.c */

/* Start: bn_mp_reduce_2k.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
CLEANUP:
  mp_clear (&q);

  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce.c */

/* Start: bn_mp_reduce_2k.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6399
6400
6401
6402
6403
6404
6405




6406
6407
6408
6409
6410
6411
6412
   
ERR:
   mp_clear(&q);
   return res;
}

#endif





/* End: bn_mp_reduce_2k.c */

/* Start: bn_mp_reduce_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
   
ERR:
   mp_clear(&q);
   return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_2k.c */

/* Start: bn_mp_reduce_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6462
6463
6464
6465
6466
6467
6468




6469
6470
6471
6472
6473
6474
6475
ERR:
   mp_clear(&q);
   return res;
}

#endif





/* End: bn_mp_reduce_2k_l.c */

/* Start: bn_mp_reduce_2k_setup.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
ERR:
   mp_clear(&q);
   return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_2k_l.c */

/* Start: bn_mp_reduce_2k_setup.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6509
6510
6511
6512
6513
6514
6515




6516
6517
6518
6519
6520
6521
6522
   
   *d = tmp.dp[0];
   mp_clear(&tmp);
   return MP_OKAY;
}
#endif





/* End: bn_mp_reduce_2k_setup.c */

/* Start: bn_mp_reduce_2k_setup_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
   
   *d = tmp.dp[0];
   mp_clear(&tmp);
   return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_2k_setup.c */

/* Start: bn_mp_reduce_2k_setup_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6552
6553
6554
6555
6556
6557
6558




6559
6560
6561
6562
6563
6564
6565
   }
   
ERR:
   mp_clear(&tmp);
   return res;
}
#endif





/* End: bn_mp_reduce_2k_setup_l.c */

/* Start: bn_mp_reduce_is_2k.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
   }
   
ERR:
   mp_clear(&tmp);
   return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_2k_setup_l.c */

/* Start: bn_mp_reduce_is_2k.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6605
6606
6607
6608
6609
6610
6611




6612
6613
6614
6615
6616
6617
6618
      }
   }
   return MP_YES;
}

#endif





/* End: bn_mp_reduce_is_2k.c */

/* Start: bn_mp_reduce_is_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
      }
   }
   return MP_YES;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_is_2k.c */

/* Start: bn_mp_reduce_is_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6649
6650
6651
6652
6653
6654
6655




6656
6657
6658
6659
6660
6661
6662
      
   }
   return MP_NO;
}

#endif





/* End: bn_mp_reduce_is_2k_l.c */

/* Start: bn_mp_reduce_setup.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
      
   }
   return MP_NO;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_is_2k_l.c */

/* Start: bn_mp_reduce_setup.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6682
6683
6684
6685
6686
6687
6688




6689
6690
6691
6692
6693
6694
6695
  
  if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
    return res;
  }
  return mp_div (a, b, a, NULL);
}
#endif





/* End: bn_mp_reduce_setup.c */

/* Start: bn_mp_rshd.c */
#include <tommath.h>
#ifdef BN_MP_RSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
  
  if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
    return res;
  }
  return mp_div (a, b, a, NULL);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_reduce_setup.c */

/* Start: bn_mp_rshd.c */
#include <tommath.h>
#ifdef BN_MP_RSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6755
6756
6757
6758
6759
6760
6761




6762
6763
6764
6765
6766
6767
6768
  }
  
  /* remove excess digits */
  a->used -= b;
}
#endif





/* End: bn_mp_rshd.c */

/* Start: bn_mp_set.c */
#include <tommath.h>
#ifdef BN_MP_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
  }
  
  /* remove excess digits */
  a->used -= b;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_rshd.c */

/* Start: bn_mp_set.c */
#include <tommath.h>
#ifdef BN_MP_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6783
6784
6785
6786
6787
6788
6789




6790
6791
6792
6793
6794
6795
6796
void mp_set (mp_int * a, mp_digit b)
{
  mp_zero (a);
  a->dp[0] = b & MP_MASK;
  a->used  = (a->dp[0] != 0) ? 1 : 0;
}
#endif





/* End: bn_mp_set.c */

/* Start: bn_mp_set_int.c */
#include <tommath.h>
#ifdef BN_MP_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
void mp_set (mp_int * a, mp_digit b)
{
  mp_zero (a);
  a->dp[0] = b & MP_MASK;
  a->used  = (a->dp[0] != 0) ? 1 : 0;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_set.c */

/* Start: bn_mp_set_int.c */
#include <tommath.h>
#ifdef BN_MP_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6832
6833
6834
6835
6836
6837
6838




6839
6840
6841
6842
6843
6844
6845
    a->used += 1;
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif





/* End: bn_mp_set_int.c */

/* Start: bn_mp_shrink.c */
#include <tommath.h>
#ifdef BN_MP_SHRINK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
    a->used += 1;
  }
  mp_clamp (a);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_set_int.c */

/* Start: bn_mp_shrink.c */
#include <tommath.h>
#ifdef BN_MP_SHRINK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6867
6868
6869
6870
6871
6872
6873




6874
6875
6876
6877
6878
6879
6880
    a->dp    = tmp;
    a->alloc = a->used;
  }
  return MP_OKAY;
}
#endif





/* End: bn_mp_shrink.c */

/* Start: bn_mp_signed_bin_size.c */
#include <tommath.h>
#ifdef BN_MP_SIGNED_BIN_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
    a->dp    = tmp;
    a->alloc = a->used;
  }
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_shrink.c */

/* Start: bn_mp_signed_bin_size.c */
#include <tommath.h>
#ifdef BN_MP_SIGNED_BIN_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6893
6894
6895
6896
6897
6898
6899




6900
6901
6902
6903
6904
6905
6906

/* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a)
{
  return 1 + mp_unsigned_bin_size (a);
}
#endif





/* End: bn_mp_signed_bin_size.c */

/* Start: bn_mp_sqr.c */
#include <tommath.h>
#ifdef BN_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274

/* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a)
{
  return 1 + mp_unsigned_bin_size (a);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_signed_bin_size.c */

/* Start: bn_mp_sqr.c */
#include <tommath.h>
#ifdef BN_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
6952
6953
6954
6955
6956
6957
6958




6959
6960
6961
6962
6963
6964
6965
#endif
  }
  b->sign = MP_ZPOS;
  return res;
}
#endif





/* End: bn_mp_sqr.c */

/* Start: bn_mp_sqrmod.c */
#include <tommath.h>
#ifdef BN_MP_SQRMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
#endif
  }
  b->sign = MP_ZPOS;
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_sqr.c */

/* Start: bn_mp_sqrmod.c */
#include <tommath.h>
#ifdef BN_MP_SQRMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
6992
6993
6994
6995
6996
6997
6998




6999
7000
7001
7002
7003
7004
7005
    return res;
  }
  res = mp_mod (&t, b, c);
  mp_clear (&t);
  return res;
}
#endif





/* End: bn_mp_sqrmod.c */

/* Start: bn_mp_sqrt.c */
#include <tommath.h>
#ifdef BN_MP_SQRT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
    return res;
  }
  res = mp_mod (&t, b, c);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_sqrmod.c */

/* Start: bn_mp_sqrt.c */
#include <tommath.h>
#ifdef BN_MP_SQRT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
7074
7075
7076
7077
7078
7079
7080




7081
7082
7083
7084
7085
7086
7087
E1: mp_clear(&t2);
E2: mp_clear(&t1);
  return res;
}

#endif





/* End: bn_mp_sqrt.c */

/* Start: bn_mp_sub.c */
#include <tommath.h>
#ifdef BN_MP_SUB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
E1: mp_clear(&t2);
E2: mp_clear(&t1);
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_sqrt.c */

/* Start: bn_mp_sub.c */
#include <tommath.h>
#ifdef BN_MP_SUB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7132
7133
7134
7135
7136
7137
7138




7139
7140
7141
7142
7143
7144
7145
      res = s_mp_sub (b, a, c);
    }
  }
  return res;
}

#endif





/* End: bn_mp_sub.c */

/* Start: bn_mp_sub_d.c */
#include <tommath.h>
#ifdef BN_MP_SUB_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
      res = s_mp_sub (b, a, c);
    }
  }
  return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_sub.c */

/* Start: bn_mp_sub_d.c */
#include <tommath.h>
#ifdef BN_MP_SUB_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
7222
7223
7224
7225
7226
7227
7228




7229
7230
7231
7232
7233
7234
7235
  }
  mp_clamp(c);
  return MP_OKAY;
}

#endif





/* End: bn_mp_sub_d.c */

/* Start: bn_mp_submod.c */
#include <tommath.h>
#ifdef BN_MP_SUBMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
  }
  mp_clamp(c);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_sub_d.c */

/* Start: bn_mp_submod.c */
#include <tommath.h>
#ifdef BN_MP_SUBMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7264
7265
7266
7267
7268
7269
7270




7271
7272
7273
7274
7275
7276
7277
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif





/* End: bn_mp_submod.c */

/* Start: bn_mp_to_signed_bin.c */
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
  }
  res = mp_mod (&t, c, d);
  mp_clear (&t);
  return res;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_submod.c */

/* Start: bn_mp_to_signed_bin.c */
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7297
7298
7299
7300
7301
7302
7303




7304
7305
7306
7307
7308
7309
7310
    return res;
  }
  b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
  return MP_OKAY;
}
#endif





/* End: bn_mp_to_signed_bin.c */

/* Start: bn_mp_to_signed_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
    return res;
  }
  b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_to_signed_bin.c */

/* Start: bn_mp_to_signed_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7327
7328
7329
7330
7331
7332
7333




7334
7335
7336
7337
7338
7339
7340
   if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_signed_bin_size(a);
   return mp_to_signed_bin(a, b);
}
#endif





/* End: bn_mp_to_signed_bin_n.c */

/* Start: bn_mp_to_unsigned_bin.c */
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
   if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_signed_bin_size(a);
   return mp_to_signed_bin(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_to_signed_bin_n.c */

/* Start: bn_mp_to_unsigned_bin.c */
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
7376
7377
7378
7379
7380
7381
7382




7383
7384
7385
7386
7387
7388
7389
  }
  bn_reverse (b, x);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_mp_to_unsigned_bin.c */

/* Start: bn_mp_to_unsigned_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
  }
  bn_reverse (b, x);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_to_unsigned_bin.c */

/* Start: bn_mp_to_unsigned_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7406
7407
7408
7409
7410
7411
7412




7413
7414
7415
7416
7417
7418
7419
   if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_unsigned_bin_size(a);
   return mp_to_unsigned_bin(a, b);
}
#endif





/* End: bn_mp_to_unsigned_bin_n.c */

/* Start: bn_mp_toom_mul.c */
#include <tommath.h>
#ifdef BN_MP_TOOM_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
   if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
      return MP_VAL;
   }
   *outlen = mp_unsigned_bin_size(a);
   return mp_to_unsigned_bin(a, b);
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_to_unsigned_bin_n.c */

/* Start: bn_mp_toom_mul.c */
#include <tommath.h>
#ifdef BN_MP_TOOM_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
7691
7692
7693
7694
7695
7696
7697




7698
7699
7700
7701
7702
7703
7704
                    &a0, &a1, &a2, &b0, &b1, 
                    &b2, &tmp1, &tmp2, NULL);
     return res;
}     
     
#endif





/* End: bn_mp_toom_mul.c */

/* Start: bn_mp_toom_sqr.c */
#include <tommath.h>
#ifdef BN_MP_TOOM_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
                    &a0, &a1, &a2, &b0, &b1, 
                    &b2, &tmp1, &tmp2, NULL);
     return res;
}     
     
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_toom_mul.c */

/* Start: bn_mp_toom_sqr.c */
#include <tommath.h>
#ifdef BN_MP_TOOM_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7917
7918
7919
7920
7921
7922
7923




7924
7925
7926
7927
7928
7929
7930
ERR:
     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
     return res;
}

#endif





/* End: bn_mp_toom_sqr.c */

/* Start: bn_mp_toradix.c */
#include <tommath.h>
#ifdef BN_MP_TORADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
ERR:
     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
     return res;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_toom_sqr.c */

/* Start: bn_mp_toradix.c */
#include <tommath.h>
#ifdef BN_MP_TORADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
7991
7992
7993
7994
7995
7996
7997




7998
7999
8000
8001
8002
8003
8004
  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif





/* End: bn_mp_toradix.c */

/* Start: bn_mp_toradix_n.c */
#include <tommath.h>
#ifdef BN_MP_TORADIX_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
  *str = '\0';

  mp_clear (&t);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_toradix.c */

/* Start: bn_mp_toradix_n.c */
#include <tommath.h>
#ifdef BN_MP_TORADIX_N_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
8081
8082
8083
8084
8085
8086
8087




8088
8089
8090
8091
8092
8093
8094

  mp_clear (&t);
  return MP_OKAY;
}

#endif





/* End: bn_mp_toradix_n.c */

/* Start: bn_mp_unsigned_bin_size.c */
#include <tommath.h>
#ifdef BN_MP_UNSIGNED_BIN_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518

  mp_clear (&t);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_toradix_n.c */

/* Start: bn_mp_unsigned_bin_size.c */
#include <tommath.h>
#ifdef BN_MP_UNSIGNED_BIN_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
8108
8109
8110
8111
8112
8113
8114




8115
8116
8117
8118
8119
8120
8121
/* get the size for an unsigned equivalent */
int mp_unsigned_bin_size (mp_int * a)
{
  int     size = mp_count_bits (a);
  return (size / 8 + ((size & 7) != 0 ? 1 : 0));
}
#endif





/* End: bn_mp_unsigned_bin_size.c */

/* Start: bn_mp_xor.c */
#include <tommath.h>
#ifdef BN_MP_XOR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
/* get the size for an unsigned equivalent */
int mp_unsigned_bin_size (mp_int * a)
{
  int     size = mp_count_bits (a);
  return (size / 8 + ((size & 7) != 0 ? 1 : 0));
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_unsigned_bin_size.c */

/* Start: bn_mp_xor.c */
#include <tommath.h>
#ifdef BN_MP_XOR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
8160
8161
8162
8163
8164
8165
8166




8167
8168
8169
8170
8171
8172
8173
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_mp_xor.c */

/* Start: bn_mp_zero.c */
#include <tommath.h>
#ifdef BN_MP_ZERO_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
  mp_clamp (&t);
  mp_exch (c, &t);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_xor.c */

/* Start: bn_mp_zero.c */
#include <tommath.h>
#ifdef BN_MP_ZERO_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
8195
8196
8197
8198
8199
8200
8201




8202
8203
8204
8205
8206
8207
8208

  tmp = a->dp;
  for (n = 0; n < a->alloc; n++) {
     *tmp++ = 0;
  }
}
#endif





/* End: bn_mp_zero.c */

/* Start: bn_prime_tab.c */
#include <tommath.h>
#ifdef BN_PRIME_TAB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644

  tmp = a->dp;
  for (n = 0; n < a->alloc; n++) {
     *tmp++ = 0;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_mp_zero.c */

/* Start: bn_prime_tab.c */
#include <tommath.h>
#ifdef BN_PRIME_TAB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
8257
8258
8259
8260
8261
8262
8263




8264
8265
8266
8267
8268
8269
8270
  0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
#endif
};
#endif





/* End: bn_prime_tab.c */

/* Start: bn_reverse.c */
#include <tommath.h>
#ifdef BN_REVERSE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
  0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
#endif
};
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_prime_tab.c */

/* Start: bn_reverse.c */
#include <tommath.h>
#ifdef BN_REVERSE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
8295
8296
8297
8298
8299
8300
8301




8302
8303
8304
8305
8306
8307
8308
    s[ix] = s[iy];
    s[iy] = t;
    ++ix;
    --iy;
  }
}
#endif





/* End: bn_reverse.c */

/* Start: bn_s_mp_add.c */
#include <tommath.h>
#ifdef BN_S_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
    s[ix] = s[iy];
    s[iy] = t;
    ++ix;
    --iy;
  }
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_reverse.c */

/* Start: bn_s_mp_add.c */
#include <tommath.h>
#ifdef BN_S_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
8405
8406
8407
8408
8409
8410
8411




8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
  }

  mp_clamp (c);
  return MP_OKAY;
}
#endif





/* End: bn_s_mp_add.c */

/* Start: bn_s_mp_exptmod.c */
#include <tommath.h>
#ifdef BN_S_MP_EXPTMOD_C
/* 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
 */

#ifdef MP_LOW_MEM
   #define TAB_SIZE 32
#else
   #define TAB_SIZE 256
#endif

int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)







>
>
>
>



















<







8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878

8879
8880
8881
8882
8883
8884
8885
  }

  mp_clamp (c);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_add.c */

/* Start: bn_s_mp_exptmod.c */
#include <tommath.h>
#ifdef BN_S_MP_EXPTMOD_C
/* 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
 */

#ifdef MP_LOW_MEM
   #define TAB_SIZE 32
#else
   #define TAB_SIZE 256
#endif

int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
8658
8659
8660
8661
8662
8663
8664




8665
8666
8667
8668
8669
8670
8671
  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif





/* End: bn_s_mp_exptmod.c */

/* Start: bn_s_mp_mul_digs.c */
#include <tommath.h>
#ifdef BN_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
  for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
    mp_clear (&M[x]);
  }
  return err;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_exptmod.c */

/* Start: bn_s_mp_mul_digs.c */
#include <tommath.h>
#ifdef BN_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
8748
8749
8750
8751
8752
8753
8754




8755
8756
8757
8758
8759
8760
8761
  mp_exch (&t, c);

  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_s_mp_mul_digs.c */

/* Start: bn_s_mp_mul_high_digs.c */
#include <tommath.h>
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
  mp_exch (&t, c);

  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_mul_digs.c */

/* Start: bn_s_mp_mul_high_digs.c */
#include <tommath.h>
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
8828
8829
8830
8831
8832
8833
8834




8835
8836
8837
8838
8839
8840
8841
  }
  mp_clamp (&t);
  mp_exch (&t, c);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_s_mp_mul_high_digs.c */

/* Start: bn_s_mp_sqr.c */
#include <tommath.h>
#ifdef BN_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
  }
  mp_clamp (&t);
  mp_exch (&t, c);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_mul_high_digs.c */

/* Start: bn_s_mp_sqr.c */
#include <tommath.h>
#ifdef BN_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
8912
8913
8914
8915
8916
8917
8918




8919
8920
8921
8922
8923
8924
8925

  mp_clamp (&t);
  mp_exch (&t, b);
  mp_clear (&t);
  return MP_OKAY;
}
#endif





/* End: bn_s_mp_sqr.c */

/* Start: bn_s_mp_sub.c */
#include <tommath.h>
#ifdef BN_S_MP_SUB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis







>
>
>
>







9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388

  mp_clamp (&t);
  mp_exch (&t, b);
  mp_clear (&t);
  return MP_OKAY;
}
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_sqr.c */

/* Start: bn_s_mp_sub.c */
#include <tommath.h>
#ifdef BN_S_MP_SUB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
9002
9003
9004
9005
9006
9007
9008




9009
9010
9011
9012
9013
9014
9015

  mp_clamp (c);
  return MP_OKAY;
}

#endif





/* End: bn_s_mp_sub.c */

/* Start: bncore.c */
#include <tommath.h>
#ifdef BNCORE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *







>
>
>
>







9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482

  mp_clamp (c);
  return MP_OKAY;
}

#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bn_s_mp_sub.c */

/* Start: bncore.c */
#include <tommath.h>
#ifdef BNCORE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043




9044
9045
9046
9047
9048
 */

/* Known optimal configurations

 CPU                    /Compiler     /MUL CUTOFF/SQR CUTOFF
-------------------------------------------------------------
 Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-)
 AMD Athlon64           /GCC v3.4.4   /        74/       124/LTM 0.34
 
*/

int     KARATSUBA_MUL_CUTOFF = 74,      /* Min. number of digits before Karatsuba multiplication is used. */
        KARATSUBA_SQR_CUTOFF = 124,     /* Min. number of digits before Karatsuba squaring is used. */
        
        TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */
        TOOM_SQR_CUTOFF      = 400; 
#endif





/* End: bncore.c */


/* EOF */







|



|
|




>
>
>
>





9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
 */

/* Known optimal configurations

 CPU                    /Compiler     /MUL CUTOFF/SQR CUTOFF
-------------------------------------------------------------
 Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-)
 AMD Athlon64           /GCC v3.4.4   /        80/       120/LTM 0.35
 
*/

int     KARATSUBA_MUL_CUTOFF = 80,      /* Min. number of digits before Karatsuba multiplication is used. */
        KARATSUBA_SQR_CUTOFF = 120,     /* Min. number of digits before Karatsuba squaring is used. */
        
        TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */
        TOOM_SQR_CUTOFF      = 400; 
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/pre_gen/mpi.c,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:22 $ */

/* End: bncore.c */


/* EOF */
Changes to libtommath/tommath.pdf.

cannot compute difference between binary files

Changes to libtommath/tommath.src.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Greg Rose \\
QUALCOMM Australia \\
\end{tabular}
%\end{small}
}
}
\maketitle
This text has been placed in the public domain.  This text corresponds to the v0.35 release of the 
LibTomMath project.

\begin{alltt}
Tom St Denis
111 Banning Rd
Ottawa, Ontario
K2L 1C3







|







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Greg Rose \\
QUALCOMM Australia \\
\end{tabular}
%\end{small}
}
}
\maketitle
This text has been placed in the public domain.  This text corresponds to the v0.36 release of the 
LibTomMath project.

\begin{alltt}
Tom St Denis
111 Banning Rd
Ottawa, Ontario
K2L 1C3
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804

\subsection{Karatsuba Multiplication}
Karatsuba \cite{KARA} multiplication when originally proposed in 1962 was among the first set of algorithms to break the $O(n^2)$ barrier for
general purpose multiplication.  Given two polynomial basis representations $f(x) = ax + b$ and $g(x) = cx + d$, Karatsuba proved with 
light algebra \cite{KARAP} that the following polynomial is equivalent to multiplication of the two integers the polynomials represent.

\begin{equation}
f(x) \cdot g(x) = acx^2 + ((a - b)(c - d) - (ac + bd))x + bd
\end{equation}

Using the observation that $ac$ and $bd$ could be re-used only three half sized multiplications would be required to produce the product.  Applying
this algorithm recursively, the work factor becomes $O(n^{lg(3)})$ which is substantially better than the work factor $O(n^2)$ of the Comba technique.  It turns 
out what Karatsuba did not know or at least did not publish was that this is simply polynomial basis multiplication with the points 
$\zeta_0$, $\zeta_{\infty}$ and $-\zeta_{-1}$.  Consider the resultant system of equations.

\begin{center}
\begin{tabular}{rcrcrcrc}
$\zeta_{0}$ &      $=$ &  &  &  & & $w_0$ \\
$-\zeta_{-1}$ &    $=$ & $-w_2$ & $+$ & $w_1$ & $-$ & $w_0$ \\
$\zeta_{\infty}$ & $=$ & $w_2$ &  & &  & \\
\end{tabular}
\end{center}

By adding the first and last equation to the equation in the middle the term $w_1$ can be isolated and all three coefficients solved for.  The simplicity
of this system of equations has made Karatsuba fairly popular.  In fact the cutoff point is often fairly low\footnote{With LibTomMath 0.18 it is 70 and 109 digits for the Intel P4 and AMD Athlon respectively.}
making it an ideal algorithm to speed up certain public key cryptosystems such as RSA and Diffie-Hellman.  It is worth noting that the point 
$\zeta_1$ could be substituted for $-\zeta_{-1}$.  In this case the first and third row are subtracted instead of added to the second row.  

\newpage\begin{figure}[!here]
\begin{small}
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_karatsuba\_mul}. \\
\textbf{Input}.   mp\_int $a$ and mp\_int $b$ \\







|





|




|






|
<







2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796

2797
2798
2799
2800
2801
2802
2803

\subsection{Karatsuba Multiplication}
Karatsuba \cite{KARA} multiplication when originally proposed in 1962 was among the first set of algorithms to break the $O(n^2)$ barrier for
general purpose multiplication.  Given two polynomial basis representations $f(x) = ax + b$ and $g(x) = cx + d$, Karatsuba proved with 
light algebra \cite{KARAP} that the following polynomial is equivalent to multiplication of the two integers the polynomials represent.

\begin{equation}
f(x) \cdot g(x) = acx^2 + ((a + b)(c + d) - (ac + bd))x + bd
\end{equation}

Using the observation that $ac$ and $bd$ could be re-used only three half sized multiplications would be required to produce the product.  Applying
this algorithm recursively, the work factor becomes $O(n^{lg(3)})$ which is substantially better than the work factor $O(n^2)$ of the Comba technique.  It turns 
out what Karatsuba did not know or at least did not publish was that this is simply polynomial basis multiplication with the points 
$\zeta_0$, $\zeta_{\infty}$ and $\zeta_{1}$.  Consider the resultant system of equations.

\begin{center}
\begin{tabular}{rcrcrcrc}
$\zeta_{0}$ &      $=$ &  &  &  & & $w_0$ \\
$\zeta_{1}$ &      $=$ & $w_2$ & $+$ & $w_1$ & $+$ & $w_0$ \\
$\zeta_{\infty}$ & $=$ & $w_2$ &  & &  & \\
\end{tabular}
\end{center}

By adding the first and last equation to the equation in the middle the term $w_1$ can be isolated and all three coefficients solved for.  The simplicity
of this system of equations has made Karatsuba fairly popular.  In fact the cutoff point is often fairly low\footnote{With LibTomMath 0.18 it is 70 and 109 digits for the Intel P4 and AMD Athlon respectively.}
making it an ideal algorithm to speed up certain public key cryptosystems such as RSA and Diffie-Hellman.  


\newpage\begin{figure}[!here]
\begin{small}
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_karatsuba\_mul}. \\
\textbf{Input}.   mp\_int $a$ and mp\_int $b$ \\
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
5.  $y0 \leftarrow b \mbox{ (mod }\beta^B\mbox{)}$ \\
6.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_rshd}) \\
7.  $y1 \leftarrow \lfloor b / \beta^B \rfloor$ \\
\\
Calculate the three products. \\
8.  $x0y0 \leftarrow x0 \cdot y0$ (\textit{mp\_mul}) \\
9.  $x1y1 \leftarrow x1 \cdot y1$ \\
10.  $t1 \leftarrow x1 - x0$ (\textit{mp\_sub}) \\
11.  $x0 \leftarrow y1 - y0$ \\
12.  $t1 \leftarrow t1 \cdot x0$ \\
\\
Calculate the middle term. \\
13.  $x0 \leftarrow x0y0 + x1y1$ \\
14.  $t1 \leftarrow x0 - t1$ \\
\\
Calculate the final product. \\
15.  $t1 \leftarrow t1 \cdot \beta^B$ (\textit{mp\_lshd}) \\
16.  $x1y1 \leftarrow x1y1 \cdot \beta^{2B}$ \\
17.  $t1 \leftarrow x0y0 + t1$ \\
18.  $c \leftarrow t1 + x1y1$ \\
19.  Clear all of the temporary variables. \\







|
|




|







2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
5.  $y0 \leftarrow b \mbox{ (mod }\beta^B\mbox{)}$ \\
6.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_rshd}) \\
7.  $y1 \leftarrow \lfloor b / \beta^B \rfloor$ \\
\\
Calculate the three products. \\
8.  $x0y0 \leftarrow x0 \cdot y0$ (\textit{mp\_mul}) \\
9.  $x1y1 \leftarrow x1 \cdot y1$ \\
10.  $t1 \leftarrow x1 + x0$ (\textit{mp\_add}) \\
11.  $x0 \leftarrow y1 + y0$ \\
12.  $t1 \leftarrow t1 \cdot x0$ \\
\\
Calculate the middle term. \\
13.  $x0 \leftarrow x0y0 + x1y1$ \\
14.  $t1 \leftarrow t1 - x0$ (\textit{s\_mp\_sub}) \\
\\
Calculate the final product. \\
15.  $t1 \leftarrow t1 \cdot \beta^B$ (\textit{mp\_lshd}) \\
16.  $x1y1 \leftarrow x1y1 \cdot \beta^{2B}$ \\
17.  $t1 \leftarrow x0y0 + t1$ \\
18.  $c \leftarrow t1 + x1y1$ \\
19.  Clear all of the temporary variables. \\
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
\index{radix point}
In order to split the two inputs into their respective halves, a suitable \textit{radix point} must be chosen.  The radix point chosen must
be used for both of the inputs meaning that it must be smaller than the smallest input.  Step 3 chooses the radix point $B$ as half of the 
smallest input \textbf{used} count.  After the radix point is chosen the inputs are split into lower and upper halves.  Step 4 and 5 
compute the lower halves.  Step 6 and 7 computer the upper halves.  

After the halves have been computed the three intermediate half-size products must be computed.  Step 8 and 9 compute the trivial products
$x0 \cdot y0$ and $x1 \cdot y1$.  The mp\_int $x0$ is used as a temporary variable after $x1 - x0$ has been computed.  By using $x0$ instead
of an additional temporary variable, the algorithm can avoid an addition memory allocation operation.

The remaining steps 13 through 18 compute the Karatsuba polynomial through a variety of digit shifting and addition operations.

EXAM,bn_mp_karatsuba_mul.c

The new coding element in this routine, not  seen in previous routines, is the usage of goto statements.  The conventional







|







2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
\index{radix point}
In order to split the two inputs into their respective halves, a suitable \textit{radix point} must be chosen.  The radix point chosen must
be used for both of the inputs meaning that it must be smaller than the smallest input.  Step 3 chooses the radix point $B$ as half of the 
smallest input \textbf{used} count.  After the radix point is chosen the inputs are split into lower and upper halves.  Step 4 and 5 
compute the lower halves.  Step 6 and 7 computer the upper halves.  

After the halves have been computed the three intermediate half-size products must be computed.  Step 8 and 9 compute the trivial products
$x0 \cdot y0$ and $x1 \cdot y1$.  The mp\_int $x0$ is used as a temporary variable after $x1 + x0$ has been computed.  By using $x0$ instead
of an additional temporary variable, the algorithm can avoid an addition memory allocation operation.

The remaining steps 13 through 18 compute the Karatsuba polynomial through a variety of digit shifting and addition operations.

EXAM,bn_mp_karatsuba_mul.c

The new coding element in this routine, not  seen in previous routines, is the usage of goto statements.  The conventional
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259

\subsection{Karatsuba Squaring}
Let $f(x) = ax + b$ represent the polynomial basis representation of a number to square.  
Let $h(x) = \left ( f(x) \right )^2$ represent the square of the polynomial.  The Karatsuba equation can be modified to square a 
number with the following equation.

\begin{equation}
h(x) = a^2x^2 + \left (a^2 + b^2 - (a - b)^2 \right )x + b^2
\end{equation}

Upon closer inspection this equation only requires the calculation of three half-sized squares: $a^2$, $b^2$ and $(a - b)^2$.  As in 
Karatsuba multiplication, this algorithm can be applied recursively on the input and will achieve an asymptotic running time of 
$O \left ( n^{lg(3)} \right )$.

If the asymptotic times of Karatsuba squaring and multiplication are the same, why not simply use the multiplication algorithm 
instead?  The answer to this arises from the cutoff point for squaring.  As in multiplication there exists a cutoff point, at which the 
time required for a Comba based squaring and a Karatsuba based squaring meet.  Due to the overhead inherent in the Karatsuba method, the cutoff 
point is fairly high.  For example, on an AMD Athlon XP processor with $\beta = 2^{28}$, the cutoff point is around 127 digits.  







|


|







3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258

\subsection{Karatsuba Squaring}
Let $f(x) = ax + b$ represent the polynomial basis representation of a number to square.  
Let $h(x) = \left ( f(x) \right )^2$ represent the square of the polynomial.  The Karatsuba equation can be modified to square a 
number with the following equation.

\begin{equation}
h(x) = a^2x^2 + \left ((a + b)^2 - (a^2 + b^2) \right )x + b^2
\end{equation}

Upon closer inspection this equation only requires the calculation of three half-sized squares: $a^2$, $b^2$ and $(a + b)^2$.  As in 
Karatsuba multiplication, this algorithm can be applied recursively on the input and will achieve an asymptotic running time of 
$O \left ( n^{lg(3)} \right )$.

If the asymptotic times of Karatsuba squaring and multiplication are the same, why not simply use the multiplication algorithm 
instead?  The answer to this arises from the cutoff point for squaring.  As in multiplication there exists a cutoff point, at which the 
time required for a Comba based squaring and a Karatsuba based squaring meet.  Due to the overhead inherent in the Karatsuba method, the cutoff 
point is fairly high.  For example, on an AMD Athlon XP processor with $\beta = 2^{28}$, the cutoff point is around 127 digits.  
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3.  $B \leftarrow \lfloor a.used / 2 \rfloor$ \\
4.  $x0 \leftarrow a \mbox{ (mod }\beta^B\mbox{)}$ (\textit{mp\_mod\_2d}) \\
5.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_lshd}) \\
\\
Calculate the three squares. \\
6.  $x0x0 \leftarrow x0^2$ (\textit{mp\_sqr}) \\
7.  $x1x1 \leftarrow x1^2$ \\
8.  $t1 \leftarrow x1 - x0$ (\textit{mp\_sub}) \\
9.  $t1 \leftarrow t1^2$ \\
\\
Compute the middle term. \\
10.  $t2 \leftarrow x0x0 + x1x1$ (\textit{s\_mp\_add}) \\
11.  $t1 \leftarrow t2 - t1$ \\
\\
Compute final product. \\
12.  $t1 \leftarrow t1\beta^B$ (\textit{mp\_lshd}) \\
13.  $x1x1 \leftarrow x1x1\beta^{2B}$ \\
14.  $t1 \leftarrow t1 + x0x0$ \\
15.  $b \leftarrow t1 + x1x1$ \\
16.  Return(\textit{MP\_OKAY}). \\







|




|







3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3.  $B \leftarrow \lfloor a.used / 2 \rfloor$ \\
4.  $x0 \leftarrow a \mbox{ (mod }\beta^B\mbox{)}$ (\textit{mp\_mod\_2d}) \\
5.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_lshd}) \\
\\
Calculate the three squares. \\
6.  $x0x0 \leftarrow x0^2$ (\textit{mp\_sqr}) \\
7.  $x1x1 \leftarrow x1^2$ \\
8.  $t1 \leftarrow x1 + x0$ (\textit{s\_mp\_add}) \\
9.  $t1 \leftarrow t1^2$ \\
\\
Compute the middle term. \\
10.  $t2 \leftarrow x0x0 + x1x1$ (\textit{s\_mp\_add}) \\
11.  $t1 \leftarrow t1 - t2$ \\
\\
Compute final product. \\
12.  $t1 \leftarrow t1\beta^B$ (\textit{mp\_lshd}) \\
13.  $x1x1 \leftarrow x1x1\beta^{2B}$ \\
14.  $t1 \leftarrow t1 + x0x0$ \\
15.  $b \leftarrow t1 + x1x1$ \\
16.  Return(\textit{MP\_OKAY}). \\
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
This algorithm computes the square of an input $a$ using the Karatsuba technique.  This algorithm is very similar to the Karatsuba based
multiplication algorithm with the exception that the three half-size multiplications have been replaced with three half-size squarings.

The radix point for squaring is simply placed exactly in the middle of the digits when the input has an odd number of digits, otherwise it is
placed just below the middle.  Step 3, 4 and 5 compute the two halves required using $B$
as the radix point.  The first two squares in steps 6 and 7 are rather straightforward while the last square is of a more compact form.

By expanding $\left (x1 - x0 \right )^2$, the $x1^2$ and $x0^2$ terms in the middle disappear, that is $x1^2 + x0^2 - (x1 - x0)^2 = 2 \cdot x0 \cdot x1$.
Now if $5n$ single precision additions and a squaring of $n$-digits is faster than multiplying two $n$-digit numbers and doubling then
this method is faster.  Assuming no further recursions occur, the difference can be estimated with the following inequality.

Let $p$ represent the cost of a single precision addition and $q$ the cost of a single precision multiplication both in terms of time\footnote{Or
machine clock cycles.}. 

\begin{equation}







|







3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
This algorithm computes the square of an input $a$ using the Karatsuba technique.  This algorithm is very similar to the Karatsuba based
multiplication algorithm with the exception that the three half-size multiplications have been replaced with three half-size squarings.

The radix point for squaring is simply placed exactly in the middle of the digits when the input has an odd number of digits, otherwise it is
placed just below the middle.  Step 3, 4 and 5 compute the two halves required using $B$
as the radix point.  The first two squares in steps 6 and 7 are rather straightforward while the last square is of a more compact form.

By expanding $\left (x1 + x0 \right )^2$, the $x1^2$ and $x0^2$ terms in the middle disappear, that is $(x0 - x1)^2 - (x1^2 + x0^2)  = 2 \cdot x0 \cdot x1$.
Now if $5n$ single precision additions and a squaring of $n$-digits is faster than multiplying two $n$-digit numbers and doubling then
this method is faster.  Assuming no further recursions occur, the difference can be estimated with the following inequality.

Let $p$ represent the cost of a single precision addition and $q$ the cost of a single precision multiplication both in terms of time\footnote{Or
machine clock cycles.}. 

\begin{equation}
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_montgomery\_setup}. \\
\textbf{Input}.   mp\_int $n$ ($n > 1$ and $(n, 2) = 1$) \\
\textbf{Output}.  $\rho \equiv -1/n_0 \mbox{ (mod }\beta\mbox{)}$ \\
\hline \\
1.  $b \leftarrow n_0$ \\
2.  If $b$ is even return(\textit{MP\_VAL}) \\
3.  $x \leftarrow ((b + 2) \mbox{ AND } 4) << 1) + b$ \\
4.  for $k$ from 0 to $\lceil lg(lg(\beta)) \rceil - 2$ do \\
\hspace{3mm}4.1  $x \leftarrow x \cdot (2 - bx)$ \\
5.  $\rho \leftarrow \beta - x \mbox{ (mod }\beta\mbox{)}$ \\
6.  Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}







|







4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_montgomery\_setup}. \\
\textbf{Input}.   mp\_int $n$ ($n > 1$ and $(n, 2) = 1$) \\
\textbf{Output}.  $\rho \equiv -1/n_0 \mbox{ (mod }\beta\mbox{)}$ \\
\hline \\
1.  $b \leftarrow n_0$ \\
2.  If $b$ is even return(\textit{MP\_VAL}) \\
3.  $x \leftarrow (((b + 2) \mbox{ AND } 4) << 1) + b$ \\
4.  for $k$ from 0 to $\lceil lg(lg(\beta)) \rceil - 2$ do \\
\hspace{3mm}4.1  $x \leftarrow x \cdot (2 - bx)$ \\
5.  $\rho \leftarrow \beta - x \mbox{ (mod }\beta\mbox{)}$ \\
6.  Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}
Changes to libtommath/tommath.tex.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Greg Rose \\
QUALCOMM Australia \\
\end{tabular}
%\end{small}
}
}
\maketitle
This text has been placed in the public domain.  This text corresponds to the v0.35 release of the 
LibTomMath project.

\begin{alltt}
Tom St Denis
111 Banning Rd
Ottawa, Ontario
K2L 1C3







|







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Greg Rose \\
QUALCOMM Australia \\
\end{tabular}
%\end{small}
}
}
\maketitle
This text has been placed in the public domain.  This text corresponds to the v0.36 release of the 
LibTomMath project.

\begin{alltt}
Tom St Denis
111 Banning Rd
Ottawa, Ontario
K2L 1C3
810
811
812
813
814
815
816

817
818
819
820
821
822
823
035     a->used  = 0;
036     a->alloc = MP_PREC;
037     a->sign  = MP_ZPOS;
038   
039     return MP_OKAY;
040   \}
041   #endif

\end{alltt}
\end{small}

One immediate observation of this initializtion function is that it does not return a pointer to a mp\_int structure.  It 
is assumed that the caller has already allocated memory for the mp\_int structure, typically on the application stack.  The 
call to mp\_init() is used only to initialize the members of the structure to a known default state.  








>







810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
035     a->used  = 0;
036     a->alloc = MP_PREC;
037     a->sign  = MP_ZPOS;
038   
039     return MP_OKAY;
040   \}
041   #endif
042   
\end{alltt}
\end{small}

One immediate observation of this initializtion function is that it does not return a pointer to a mp\_int structure.  It 
is assumed that the caller has already allocated memory for the mp\_int structure, typically on the application stack.  The 
call to mp\_init() is used only to initialize the members of the structure to a known default state.  

898
899
900
901
902
903
904

905
906
907
908
909
910
911
033       /* reset members to make debugging easier */
034       a->dp    = NULL;
035       a->alloc = a->used = 0;
036       a->sign  = MP_ZPOS;
037     \}
038   \}
039   #endif

\end{alltt}
\end{small}

The algorithm only operates on the mp\_int if it hasn't been previously cleared.  The if statement (line 24)
checks to see if the \textbf{dp} member is not \textbf{NULL}.  If the mp\_int is a valid mp\_int then \textbf{dp} cannot be
\textbf{NULL} in which case the if statement will evaluate to true.








>







899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
033       /* reset members to make debugging easier */
034       a->dp    = NULL;
035       a->alloc = a->used = 0;
036       a->sign  = MP_ZPOS;
037     \}
038   \}
039   #endif
040   
\end{alltt}
\end{small}

The algorithm only operates on the mp\_int if it hasn't been previously cleared.  The if statement (line 24)
checks to see if the \textbf{dp} member is not \textbf{NULL}.  If the mp\_int is a valid mp\_int then \textbf{dp} cannot be
\textbf{NULL} in which case the if statement will evaluate to true.

1004
1005
1006
1007
1008
1009
1010

1011
1012
1013
1014
1015
1016
1017
046       for (; i < a->alloc; i++) \{
047         a->dp[i] = 0;
048       \}
049     \}
050     return MP_OKAY;
051   \}
052   #endif

\end{alltt}
\end{small}

A quick optimization is to first determine if a memory re-allocation is required at all.  The if statement (line 24) checks
if the \textbf{alloc} member of the mp\_int is smaller than the requested digit count.  If the count is not larger than \textbf{alloc}
the function skips the re-allocation part thus saving time.








>







1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
046       for (; i < a->alloc; i++) \{
047         a->dp[i] = 0;
048       \}
049     \}
050     return MP_OKAY;
051   \}
052   #endif
053   
\end{alltt}
\end{small}

A quick optimization is to first determine if a memory re-allocation is required at all.  The if statement (line 24) checks
if the \textbf{alloc} member of the mp\_int is smaller than the requested digit count.  If the count is not larger than \textbf{alloc}
the function skips the re-allocation part thus saving time.

1092
1093
1094
1095
1096
1097
1098

1099
1100
1101
1102
1103
1104
1105
037     for (x = 0; x < size; x++) \{
038         a->dp[x] = 0;
039     \}
040   
041     return MP_OKAY;
042   \}
043   #endif

\end{alltt}
\end{small}

The number of digits $b$ requested is padded (line 23) by first augmenting it to the next multiple of 
\textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result.  If the memory can be successfully allocated the 
mp\_int is placed in a default state representing the integer zero.  Otherwise, the error code \textbf{MP\_MEM} will be 
returned (line 28).  







>







1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
037     for (x = 0; x < size; x++) \{
038         a->dp[x] = 0;
039     \}
040   
041     return MP_OKAY;
042   \}
043   #endif
044   
\end{alltt}
\end{small}

The number of digits $b$ requested is padded (line 23) by first augmenting it to the next multiple of 
\textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result.  If the memory can be successfully allocated the 
mp\_int is placed in a default state representing the integer zero.  Otherwise, the error code \textbf{MP\_MEM} will be 
returned (line 28).  
1179
1180
1181
1182
1183
1184
1185

1186
1187
1188
1189
1190
1191
1192
048           cur_arg = va_arg(args, mp_int*);
049       \}
050       va_end(args);
051       return res;                /* Assumed ok, if error flagged above. */
052   \}
053   
054   #endif

\end{alltt}
\end{small}

This function intializes a variable length list of mp\_int structure pointers.  However, instead of having the mp\_int
structures in an actual C array they are simply passed as arguments to the function.  This function makes use of the 
``...'' argument syntax of the C programming language.  The list is terminated with a final \textbf{NULL} argument 
appended on the right.  







>







1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
048           cur_arg = va_arg(args, mp_int*);
049       \}
050       va_end(args);
051       return res;                /* Assumed ok, if error flagged above. */
052   \}
053   
054   #endif
055   
\end{alltt}
\end{small}

This function intializes a variable length list of mp\_int structure pointers.  However, instead of having the mp\_int
structures in an actual C array they are simply passed as arguments to the function.  This function makes use of the 
``...'' argument syntax of the C programming language.  The list is terminated with a final \textbf{NULL} argument 
appended on the right.  
1264
1265
1266
1267
1268
1269
1270

1271
1272
1273
1274
1275
1276
1277
033   
034     /* reset the sign flag if used == 0 */
035     if (a->used == 0) \{
036       a->sign = MP_ZPOS;
037     \}
038   \}
039   #endif

\end{alltt}
\end{small}

Note on line 27 how to test for the \textbf{used} count is made on the left of the \&\& operator.  In the C programming
language the terms to \&\& are evaluated left to right with a boolean short-circuit if any condition fails.  This is 
important since if the \textbf{used} is zero the test on the right would fetch below the array.  That is obviously 
undesirable.  The parenthesis on line 30 is used to make sure the \textbf{used} count is decremented and not







>







1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
033   
034     /* reset the sign flag if used == 0 */
035     if (a->used == 0) \{
036       a->sign = MP_ZPOS;
037     \}
038   \}
039   #endif
040   
\end{alltt}
\end{small}

Note on line 27 how to test for the \textbf{used} count is made on the left of the \&\& operator.  In the C programming
language the terms to \&\& are evaluated left to right with a boolean short-circuit if any condition fails.  This is 
important since if the \textbf{used} is zero the test on the right would fetch below the array.  That is obviously 
undesirable.  The parenthesis on line 30 is used to make sure the \textbf{used} count is decremented and not
1401
1402
1403
1404
1405
1406
1407

1408
1409
1410
1411
1412
1413
1414
057   
058     /* copy used count and sign */
059     b->used = a->used;
060     b->sign = a->sign;
061     return MP_OKAY;
062   \}
063   #endif

\end{alltt}
\end{small}

Occasionally a dependent algorithm may copy an mp\_int effectively into itself such as when the input and output
mp\_int structures passed to a function are one and the same.  For this case it is optimal to return immediately without 
copying digits (line 24).  








>







1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
057   
058     /* copy used count and sign */
059     b->used = a->used;
060     b->sign = a->sign;
061     return MP_OKAY;
062   \}
063   #endif
064   
\end{alltt}
\end{small}

Occasionally a dependent algorithm may copy an mp\_int effectively into itself such as when the input and output
mp\_int structures passed to a function are one and the same.  For this case it is optimal to return immediately without 
copying digits (line 24).  

1515
1516
1517
1518
1519
1520
1521

1522
1523
1524
1525
1526
1527
1528
021   
022     if ((res = mp_init (a)) != MP_OKAY) \{
023       return res;
024     \}
025     return mp_copy (b, a);
026   \}
027   #endif

\end{alltt}
\end{small}

This will initialize \textbf{a} and make it a verbatim copy of the contents of \textbf{b}.  Note that 
\textbf{a} will have its own memory allocated which means that \textbf{b} may be cleared after the call
and \textbf{a} will be left intact.  








>







1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
021   
022     if ((res = mp_init (a)) != MP_OKAY) \{
023       return res;
024     \}
025     return mp_copy (b, a);
026   \}
027   #endif
028   
\end{alltt}
\end{small}

This will initialize \textbf{a} and make it a verbatim copy of the contents of \textbf{b}.  Note that 
\textbf{a} will have its own memory allocated which means that \textbf{b} may be cleared after the call
and \textbf{a} will be left intact.  

1566
1567
1568
1569
1570
1571
1572

1573
1574
1575
1576
1577
1578
1579
025   
026     tmp = a->dp;
027     for (n = 0; n < a->alloc; n++) \{
028        *tmp++ = 0;
029     \}
030   \}
031   #endif

\end{alltt}
\end{small}

After the function is completed, all of the digits are zeroed, the \textbf{used} count is zeroed and the 
\textbf{sign} variable is set to \textbf{MP\_ZPOS}.

\section{Sign Manipulation}







>







1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
025   
026     tmp = a->dp;
027     for (n = 0; n < a->alloc; n++) \{
028        *tmp++ = 0;
029     \}
030   \}
031   #endif
032   
\end{alltt}
\end{small}

After the function is completed, all of the digits are zeroed, the \textbf{used} count is zeroed and the 
\textbf{sign} variable is set to \textbf{MP\_ZPOS}.

\section{Sign Manipulation}
1627
1628
1629
1630
1631
1632
1633

1634
1635
1636
1637
1638
1639
1640
032   
033     /* force the sign of b to positive */
034     b->sign = MP_ZPOS;
035   
036     return MP_OKAY;
037   \}
038   #endif

\end{alltt}
\end{small}

This fairly trivial algorithm first eliminates non--required duplications (line 27) and then sets the
\textbf{sign} flag to \textbf{MP\_ZPOS}.

\subsection{Integer Negation}







>







1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
032   
033     /* force the sign of b to positive */
034     b->sign = MP_ZPOS;
035   
036     return MP_OKAY;
037   \}
038   #endif
039   
\end{alltt}
\end{small}

This fairly trivial algorithm first eliminates non--required duplications (line 27) and then sets the
\textbf{sign} flag to \textbf{MP\_ZPOS}.

\subsection{Integer Negation}
1688
1689
1690
1691
1692
1693
1694

1695
1696
1697
1698
1699
1700
1701
029     \} else \{
030        b->sign = MP_ZPOS;
031     \}
032   
033     return MP_OKAY;
034   \}
035   #endif

\end{alltt}
\end{small}

Like mp\_abs() this function avoids non--required duplications (line 21) and then sets the sign.  We
have to make sure that only non--zero values get a \textbf{sign} of \textbf{MP\_NEG}.  If the mp\_int is zero
than the \textbf{sign} is hard--coded to \textbf{MP\_ZPOS}.








>







1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
029     \} else \{
030        b->sign = MP_ZPOS;
031     \}
032   
033     return MP_OKAY;
034   \}
035   #endif
036   
\end{alltt}
\end{small}

Like mp\_abs() this function avoids non--required duplications (line 21) and then sets the sign.  We
have to make sure that only non--zero values get a \textbf{sign} of \textbf{MP\_NEG}.  If the mp\_int is zero
than the \textbf{sign} is hard--coded to \textbf{MP\_ZPOS}.

1735
1736
1737
1738
1739
1740
1741

1742
1743
1744
1745
1746
1747
1748
018   void mp_set (mp_int * a, mp_digit b)
019   \{
020     mp_zero (a);
021     a->dp[0] = b & MP_MASK;
022     a->used  = (a->dp[0] != 0) ? 1 : 0;
023   \}
024   #endif

\end{alltt}
\end{small}

First we zero (line 20) the mp\_int to make sure that the other members are initialized for a 
small positive constant.  mp\_zero() ensures that the \textbf{sign} is positive and the \textbf{used} count
is zero.  Next we set the digit and reduce it modulo $\beta$ (line 21).  After this step we have to 
check if the resulting digit is zero or not.  If it is not then we set the \textbf{used} count to one, otherwise







>







1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
018   void mp_set (mp_int * a, mp_digit b)
019   \{
020     mp_zero (a);
021     a->dp[0] = b & MP_MASK;
022     a->used  = (a->dp[0] != 0) ? 1 : 0;
023   \}
024   #endif
025   
\end{alltt}
\end{small}

First we zero (line 20) the mp\_int to make sure that the other members are initialized for a 
small positive constant.  mp\_zero() ensures that the \textbf{sign} is positive and the \textbf{used} count
is zero.  Next we set the digit and reduce it modulo $\beta$ (line 21).  After this step we have to 
check if the resulting digit is zero or not.  If it is not then we set the \textbf{used} count to one, otherwise
1815
1816
1817
1818
1819
1820
1821

1822
1823
1824
1825
1826
1827
1828
037       /* ensure that digits are not clamped off */
038       a->used += 1;
039     \}
040     mp_clamp (a);
041     return MP_OKAY;
042   \}
043   #endif

\end{alltt}
\end{small}

This function sets four bits of the number at a time to handle all practical \textbf{DIGIT\_BIT} sizes.  The weird
addition on line 38 ensures that the newly added in bits are added to the number of digits.  While it may not 
seem obvious as to why the digit counter does not grow exceedingly large it is because of the shift on line 27 
as well as the  call to mp\_clamp() on line 40.  Both functions will clamp excess leading digits which keeps 







>







1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
037       /* ensure that digits are not clamped off */
038       a->used += 1;
039     \}
040     mp_clamp (a);
041     return MP_OKAY;
042   \}
043   #endif
044   
\end{alltt}
\end{small}

This function sets four bits of the number at a time to handle all practical \textbf{DIGIT\_BIT} sizes.  The weird
addition on line 38 ensures that the newly added in bits are added to the number of digits.  While it may not 
seem obvious as to why the digit counter does not grow exceedingly large it is because of the shift on line 27 
as well as the  call to mp\_clamp() on line 40.  Both functions will clamp excess leading digits which keeps 
1917
1918
1919
1920
1921
1922
1923

1924
1925
1926
1927
1928
1929
1930
044       if (*tmpa < *tmpb) \{
045         return MP_LT;
046       \}
047     \}
048     return MP_EQ;
049   \}
050   #endif

\end{alltt}
\end{small}

The two if statements (lines 24 and 28) compare the number of digits in the two inputs.  These two are 
performed before all of the digits are compared since it is a very cheap test to perform and can potentially save 
considerable time.  The implementation given is also not valid without those two statements.  $b.alloc$ may be 
smaller than $a.used$, meaning that undefined values will be read from $b$ past the end of the array of digits.







>







1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
044       if (*tmpa < *tmpb) \{
045         return MP_LT;
046       \}
047     \}
048     return MP_EQ;
049   \}
050   #endif
051   
\end{alltt}
\end{small}

The two if statements (lines 24 and 28) compare the number of digits in the two inputs.  These two are 
performed before all of the digits are compared since it is a very cheap test to perform and can potentially save 
considerable time.  The implementation given is also not valid without those two statements.  $b.alloc$ may be 
smaller than $a.used$, meaning that undefined values will be read from $b$ past the end of the array of digits.
1983
1984
1985
1986
1987
1988
1989

1990
1991
1992
1993
1994
1995
1996
032        /* if negative compare opposite direction */
033        return mp_cmp_mag(b, a);
034     \} else \{
035        return mp_cmp_mag(a, b);
036     \}
037   \}
038   #endif

\end{alltt}
\end{small}

The two if statements (lines 22 and 23) perform the initial sign comparison.  If the signs are not the equal then which ever
has the positive sign is larger.   The inputs are compared (line 31) based on magnitudes.  If the signs were both 
negative then the unsigned comparison is performed in the opposite direction (line 33).  Otherwise, the signs are assumed to 
be both positive and a forward direction unsigned comparison is performed.







>







1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
032        /* if negative compare opposite direction */
033        return mp_cmp_mag(b, a);
034     \} else \{
035        return mp_cmp_mag(a, b);
036     \}
037   \}
038   #endif
039   
\end{alltt}
\end{small}

The two if statements (lines 22 and 23) perform the initial sign comparison.  If the signs are not the equal then which ever
has the positive sign is larger.   The inputs are compared (line 31) based on magnitudes.  If the signs were both 
negative then the unsigned comparison is performed in the opposite direction (line 33).  Otherwise, the signs are assumed to 
be both positive and a forward direction unsigned comparison is performed.
2201
2202
2203
2204
2205
2206
2207

2208
2209
2210
2211
2212
2213
2214
098       \}
099     \}
100   
101     mp_clamp (c);
102     return MP_OKAY;
103   \}
104   #endif

\end{alltt}
\end{small}

We first sort (lines 27 to 35) the inputs based on magnitude and determine the $min$ and $max$ variables.
Note that $x$ is a pointer to an mp\_int assigned to the largest input, in effect it is a local alias.  Next we
grow the destination (37 to 42) ensure that it can accomodate the result of the addition. 








>







2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
098       \}
099     \}
100   
101     mp_clamp (c);
102     return MP_OKAY;
103   \}
104   #endif
105   
\end{alltt}
\end{small}

We first sort (lines 27 to 35) the inputs based on magnitude and determine the $min$ and $max$ variables.
Note that $x$ is a pointer to an mp\_int assigned to the largest input, in effect it is a local alias.  Next we
grow the destination (37 to 42) ensure that it can accomodate the result of the addition. 

2372
2373
2374
2375
2376
2377
2378

2379
2380
2381
2382
2383
2384
2385
078     \}
079   
080     mp_clamp (c);
081     return MP_OKAY;
082   \}
083   
084   #endif

\end{alltt}
\end{small}

Like low level addition we ``sort'' the inputs.  Except in this case the sorting is hardcoded 
(lines 24 and 25).  In reality the $min$ and $max$ variables are only aliases and are only 
used to make the source code easier to read.  Again the pointer alias optimization is used 
within this algorithm.  The aliases $tmpa$, $tmpb$ and $tmpc$ are initialized







>







2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
078     \}
079   
080     mp_clamp (c);
081     return MP_OKAY;
082   \}
083   
084   #endif
085   
\end{alltt}
\end{small}

Like low level addition we ``sort'' the inputs.  Except in this case the sorting is hardcoded 
(lines 24 and 25).  In reality the $min$ and $max$ variables are only aliases and are only 
used to make the source code easier to read.  Again the pointer alias optimization is used 
within this algorithm.  The aliases $tmpa$, $tmpb$ and $tmpc$ are initialized
2507
2508
2509
2510
2511
2512
2513

2514
2515
2516
2517
2518
2519
2520
042         res = s_mp_sub (a, b, c);
043       \}
044     \}
045     return res;
046   \}
047   
048   #endif

\end{alltt}
\end{small}

The source code follows the algorithm fairly closely.  The most notable new source code addition is the usage of the $res$ integer variable which
is used to pass result of the unsigned operations forward.  Unlike in the algorithm, the variable $res$ is merely returned as is without
explicitly checking it and returning the constant \textbf{MP\_OKAY}.  The observation is this algorithm will succeed or fail only if the lower
level functions do so.  Returning their return code is sufficient.







>







2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
042         res = s_mp_sub (a, b, c);
043       \}
044     \}
045     return res;
046   \}
047   
048   #endif
049   
\end{alltt}
\end{small}

The source code follows the algorithm fairly closely.  The most notable new source code addition is the usage of the $res$ integer variable which
is used to pass result of the unsigned operations forward.  Unlike in the algorithm, the variable $res$ is merely returned as is without
explicitly checking it and returning the constant \textbf{MP\_OKAY}.  The observation is this algorithm will succeed or fail only if the lower
level functions do so.  Returning their return code is sufficient.
2619
2620
2621
2622
2623
2624
2625

2626
2627
2628
2629
2630
2631
2632
048         res = s_mp_sub (b, a, c);
049       \}
050     \}
051     return res;
052   \}
053   
054   #endif

\end{alltt}
\end{small}

Much like the implementation of algorithm mp\_add the variable $res$ is used to catch the return code of the unsigned addition or subtraction operations
and forward it to the end of the function.  On line 38 the ``not equal to'' \textbf{MP\_LT} expression is used to emulate a 
``greater than or equal to'' comparison.  








>







2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
048         res = s_mp_sub (b, a, c);
049       \}
050     \}
051     return res;
052   \}
053   
054   #endif
055   
\end{alltt}
\end{small}

Much like the implementation of algorithm mp\_add the variable $res$ is used to catch the return code of the unsigned addition or subtraction operations
and forward it to the end of the function.  On line 38 the ``not equal to'' \textbf{MP\_LT} expression is used to emulate a 
``greater than or equal to'' comparison.  

2753
2754
2755
2756
2757
2758
2759

2760
2761
2762
2763
2764
2765
2766
071         *tmpb++ = 0;
072       \}
073     \}
074     b->sign = a->sign;
075     return MP_OKAY;
076   \}
077   #endif

\end{alltt}
\end{small}

This implementation is essentially an optimized implementation of s\_mp\_add for the case of doubling an input.  The only noteworthy difference
is the use of the logical shift operator on line 51 to perform a single precision doubling.  

\subsection{Division by Two}







>







2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
071         *tmpb++ = 0;
072       \}
073     \}
074     b->sign = a->sign;
075     return MP_OKAY;
076   \}
077   #endif
078   
\end{alltt}
\end{small}

This implementation is essentially an optimized implementation of s\_mp\_add for the case of doubling an input.  The only noteworthy difference
is the use of the logical shift operator on line 51 to perform a single precision doubling.  

\subsection{Division by Two}
2853
2854
2855
2856
2857
2858
2859

2860
2861
2862
2863
2864
2865
2866
057       \}
058     \}
059     b->sign = a->sign;
060     mp_clamp (b);
061     return MP_OKAY;
062   \}
063   #endif

\end{alltt}
\end{small}

\section{Polynomial Basis Operations}
Recall from section 4.3 that any integer can be represented as a polynomial in $x$ as $y = f(\beta)$.  Such a representation is also known as
the polynomial basis \cite[pp. 48]{ROSE}. Given such a notation a multiplication or division by $x$ amounts to shifting whole digits a single 
place.  The need for such operations arises in several other higher level algorithms such as Barrett and Montgomery reduction, integer







>







2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
057       \}
058     \}
059     b->sign = a->sign;
060     mp_clamp (b);
061     return MP_OKAY;
062   \}
063   #endif
064   
\end{alltt}
\end{small}

\section{Polynomial Basis Operations}
Recall from section 4.3 that any integer can be represented as a polynomial in $x$ as $y = f(\beta)$.  Such a representation is also known as
the polynomial basis \cite[pp. 48]{ROSE}. Given such a notation a multiplication or division by $x$ amounts to shifting whole digits a single 
place.  The need for such operations arises in several other higher level algorithms such as Barrett and Montgomery reduction, integer
2973
2974
2975
2976
2977
2978
2979

2980
2981
2982
2983
2984
2985
2986
056       for (x = 0; x < b; x++) \{
057         *top++ = 0;
058       \}
059     \}
060     return MP_OKAY;
061   \}
062   #endif

\end{alltt}
\end{small}

The if statement (line 23) ensures that the $b$ variable is greater than zero since we do not interpret negative
shift counts properly.  The \textbf{used} count is incremented by $b$ before the copy loop begins.  This elminates 
the need for an additional variable in the for loop.  The variable $top$ (line 41) is an alias
for the leading digit while $bottom$ (line 44) is an alias for the trailing edge.  The aliases form a 







>







2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
056       for (x = 0; x < b; x++) \{
057         *top++ = 0;
058       \}
059     \}
060     return MP_OKAY;
061   \}
062   #endif
063   
\end{alltt}
\end{small}

The if statement (line 23) ensures that the $b$ variable is greater than zero since we do not interpret negative
shift counts properly.  The \textbf{used} count is incremented by $b$ before the copy loop begins.  This elminates 
the need for an additional variable in the for loop.  The variable $top$ (line 41) is an alias
for the leading digit while $bottom$ (line 44) is an alias for the trailing edge.  The aliases form a 
3084
3085
3086
3087
3088
3089
3090

3091
3092
3093
3094
3095
3096
3097
061       \}
062     \}
063     
064     /* remove excess digits */
065     a->used -= b;
066   \}
067   #endif

\end{alltt}
\end{small}

The only noteworthy element of this routine is the lack of a return type since it cannot fail.  Like mp\_lshd() we
form a sliding window except we copy in the other direction.  After the window (line 59) we then zero
the upper digits of the input to make sure the result is correct.








>







3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
061       \}
062     \}
063     
064     /* remove excess digits */
065     a->used -= b;
066   \}
067   #endif
068   
\end{alltt}
\end{small}

The only noteworthy element of this routine is the lack of a return type since it cannot fail.  Like mp\_lshd() we
form a sliding window except we copy in the other direction.  After the window (line 59) we then zero
the upper digits of the input to make sure the result is correct.

3217
3218
3219
3220
3221
3222
3223

3224
3225
3226
3227
3228
3229
3230
074          c->dp[(c->used)++] = r;
075       \}
076     \}
077     mp_clamp (c);
078     return MP_OKAY;
079   \}
080   #endif

\end{alltt}
\end{small}

The shifting is performed in--place which means the first step (line 24) is to copy the input to the 
destination.  We avoid calling mp\_copy() by making sure the mp\_ints are different.  The destination then
has to be grown (line 31) to accomodate the result.








>







3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
074          c->dp[(c->used)++] = r;
075       \}
076     \}
077     mp_clamp (c);
078     return MP_OKAY;
079   \}
080   #endif
081   
\end{alltt}
\end{small}

The shifting is performed in--place which means the first step (line 24) is to copy the input to the 
destination.  We avoid calling mp\_copy() by making sure the mp\_ints are different.  The destination then
has to be grown (line 31) to accomodate the result.

3353
3354
3355
3356
3357
3358
3359

3360
3361
3362
3363
3364
3365
3366
086     if (d != NULL) \{
087       mp_exch (&t, d);
088     \}
089     mp_clear (&t);
090     return MP_OKAY;
091   \}
092   #endif

\end{alltt}
\end{small}

The implementation of algorithm mp\_div\_2d is slightly different than the algorithm specifies.  The remainder $d$ may be optionally 
ignored by passing \textbf{NULL} as the pointer to the mp\_int variable.    The temporary mp\_int variable $t$ is used to hold the 
result of the remainder operation until the end.  This allows $d$ and $a$ to represent the same mp\_int without modifying $a$ before
the quotient is obtained.







>







3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
086     if (d != NULL) \{
087       mp_exch (&t, d);
088     \}
089     mp_clear (&t);
090     return MP_OKAY;
091   \}
092   #endif
093   
\end{alltt}
\end{small}

The implementation of algorithm mp\_div\_2d is slightly different than the algorithm specifies.  The remainder $d$ may be optionally 
ignored by passing \textbf{NULL} as the pointer to the mp\_int variable.    The temporary mp\_int variable $t$ is used to hold the 
result of the remainder operation until the end.  This allows $d$ and $a$ to represent the same mp\_int without modifying $a$ before
the quotient is obtained.
3444
3445
3446
3447
3448
3449
3450

3451
3452
3453
3454
3455
3456
3457
045     c->dp[b / DIGIT_BIT] &=
046       (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digi
      t) 1));
047     mp_clamp (c);
048     return MP_OKAY;
049   \}
050   #endif

\end{alltt}
\end{small}

We first avoid cases of $b \le 0$ by simply mp\_zero()'ing the destination in such cases.  Next if $2^b$ is larger
than the input we just mp\_copy() the input and return right away.  After this point we know we must actually
perform some work to produce the remainder.








>







3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
045     c->dp[b / DIGIT_BIT] &=
046       (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digi
      t) 1));
047     mp_clamp (c);
048     return MP_OKAY;
049   \}
050   #endif
051   
\end{alltt}
\end{small}

We first avoid cases of $b \le 0$ by simply mp\_zero()'ing the destination in such cases.  Next if $2^b$ is larger
than the input we just mp\_copy() the input and return right away.  After this point we know we must actually
perform some work to produce the remainder.

3683
3684
3685
3686
3687
3688
3689

3690
3691
3692
3693
3694
3695
3696
079     mp_clamp (&t);
080     mp_exch (&t, c);
081   
082     mp_clear (&t);
083     return MP_OKAY;
084   \}
085   #endif

\end{alltt}
\end{small}

First we determine (line 30) if the Comba method can be used first since it's faster.  The conditions for 
sing the Comba routine are that min$(a.used, b.used) < \delta$ and the number of digits of output is less than 
\textbf{MP\_WARRAY}.  This new constant is used to control the stack usage in the Comba routines.  By default it is 
set to $\delta$ but can be reduced when memory is at a premium.







>







3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
079     mp_clamp (&t);
080     mp_exch (&t, c);
081   
082     mp_clear (&t);
083     return MP_OKAY;
084   \}
085   #endif
086   
\end{alltt}
\end{small}

First we determine (line 30) if the Comba method can be used first since it's faster.  The conditions for 
sing the Comba routine are that min$(a.used, b.used) < \delta$ and the number of digits of output is less than 
\textbf{MP\_WARRAY}.  This new constant is used to control the stack usage in the Comba routines.  By default it is 
set to $\delta$ but can be reduced when memory is at a premium.
3938
3939
3940
3941
3942
3943
3944

3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977

3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
065            while (tx++ < a->used && ty-- >= 0) \{ ... \}
066          */
067         iy = MIN(a->used-tx, ty+1);
068   
069         /* execute loop */
070         for (iz = 0; iz < iy; ++iz) \{
071            _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);

072         \}
073   
074         /* store term */
075         W[ix] = ((mp_digit)_W) & MP_MASK;
076   
077         /* make next carry */
078         _W = _W >> ((mp_word)DIGIT_BIT);
079     \}
080   
081     /* store final carry */
082     W[ix] = (mp_digit)(_W & MP_MASK);
083   
084     /* setup dest */
085     olduse  = c->used;
086     c->used = pa;
087   
088     \{
089       register mp_digit *tmpc;
090       tmpc = c->dp;
091       for (ix = 0; ix < pa+1; ix++) \{
092         /* now extract the previous digit [below the carry] */
093         *tmpc++ = W[ix];
094       \}
095   
096       /* clear unused digits [that existed in the old copy of c] */
097       for (; ix < olduse; ix++) \{
098         *tmpc++ = 0;
099       \}
100     \}
101     mp_clamp (c);
102     return MP_OKAY;
103   \}
104   #endif

\end{alltt}
\end{small}

As per the pseudo--code we first calculate $pa$ (line 47) as the number of digits to output.  Next we begin the outer loop
to produce the individual columns of the product.  We use the two aliases $tmpx$ and $tmpy$ (lines 61, 62) to point
inside the two multiplicands quickly.  

The inner loop (lines 70 to 72) of this implementation is where the tradeoff come into play.  Originally this comba 
implementation was ``row--major'' which means it adds to each of the columns in each pass.  After the outer loop it would then fix 
the carries.  This was very fast except it had an annoying drawback.  You had to read a mp\_word and two mp\_digits and write 
one mp\_word per iteration.  On processors such as the Athlon XP and P4 this did not matter much since the cache bandwidth 
is very high and it can keep the ALU fed with data.  It did, however, matter on older and embedded cpus where cache is often 
slower and also often doesn't exist.  This new algorithm only performs two reads per iteration under the assumption that the 
compiler has aliased $\_ \hat W$ to a CPU register.

After the inner loop we store the current accumulator in $W$ and shift $\_ \hat W$ (lines 75, 78) to forward it as 
a carry for the next pass.  After the outer loop we use the final carry (line 82) as the last digit of the product.  

\subsection{Polynomial Basis Multiplication}
To break the $O(n^2)$ barrier in multiplication requires a completely different look at integer multiplication.  In the following algorithms
the use of polynomial basis representation for two integers $a$ and $b$ as $f(x) = \sum_{i=0}^{n} a_i x^i$ and  
$g(x) = \sum_{i=0}^{n} b_i x^i$ respectively, is required.  In this system both $f(x)$ and $g(x)$ have $n + 1$ terms and are of the $n$'th degree.
 
The product $a \cdot b \equiv f(x)g(x)$ is the polynomial $W(x) = \sum_{i=0}^{2n} w_i x^i$.  The coefficients $w_i$ will







>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>







|







|
|







3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
065            while (tx++ < a->used && ty-- >= 0) \{ ... \}
066          */
067         iy = MIN(a->used-tx, ty+1);
068   
069         /* execute loop */
070         for (iz = 0; iz < iy; ++iz) \{
071            _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
072   
073         \}
074   
075         /* store term */
076         W[ix] = ((mp_digit)_W) & MP_MASK;
077   
078         /* make next carry */
079         _W = _W >> ((mp_word)DIGIT_BIT);
080     \}
081   
082     /* store final carry */
083     W[ix] = (mp_digit)(_W & MP_MASK);
084   
085     /* setup dest */
086     olduse  = c->used;
087     c->used = pa;
088   
089     \{
090       register mp_digit *tmpc;
091       tmpc = c->dp;
092       for (ix = 0; ix < pa+1; ix++) \{
093         /* now extract the previous digit [below the carry] */
094         *tmpc++ = W[ix];
095       \}
096   
097       /* clear unused digits [that existed in the old copy of c] */
098       for (; ix < olduse; ix++) \{
099         *tmpc++ = 0;
100       \}
101     \}
102     mp_clamp (c);
103     return MP_OKAY;
104   \}
105   #endif
106   
\end{alltt}
\end{small}

As per the pseudo--code we first calculate $pa$ (line 47) as the number of digits to output.  Next we begin the outer loop
to produce the individual columns of the product.  We use the two aliases $tmpx$ and $tmpy$ (lines 61, 62) to point
inside the two multiplicands quickly.  

The inner loop (lines 70 to 73) of this implementation is where the tradeoff come into play.  Originally this comba 
implementation was ``row--major'' which means it adds to each of the columns in each pass.  After the outer loop it would then fix 
the carries.  This was very fast except it had an annoying drawback.  You had to read a mp\_word and two mp\_digits and write 
one mp\_word per iteration.  On processors such as the Athlon XP and P4 this did not matter much since the cache bandwidth 
is very high and it can keep the ALU fed with data.  It did, however, matter on older and embedded cpus where cache is often 
slower and also often doesn't exist.  This new algorithm only performs two reads per iteration under the assumption that the 
compiler has aliased $\_ \hat W$ to a CPU register.

After the inner loop we store the current accumulator in $W$ and shift $\_ \hat W$ (lines 76, 79) to forward it as 
a carry for the next pass.  After the outer loop we use the final carry (line 83) as the last digit of the product.  

\subsection{Polynomial Basis Multiplication}
To break the $O(n^2)$ barrier in multiplication requires a completely different look at integer multiplication.  In the following algorithms
the use of polynomial basis representation for two integers $a$ and $b$ as $f(x) = \sum_{i=0}^{n} a_i x^i$ and  
$g(x) = \sum_{i=0}^{n} b_i x^i$ respectively, is required.  In this system both $f(x)$ and $g(x)$ have $n + 1$ terms and are of the $n$'th degree.
 
The product $a \cdot b \equiv f(x)g(x)$ is the polynomial $W(x) = \sum_{i=0}^{2n} w_i x^i$.  The coefficients $w_i$ will
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124

\subsection{Karatsuba Multiplication}
Karatsuba \cite{KARA} multiplication when originally proposed in 1962 was among the first set of algorithms to break the $O(n^2)$ barrier for
general purpose multiplication.  Given two polynomial basis representations $f(x) = ax + b$ and $g(x) = cx + d$, Karatsuba proved with 
light algebra \cite{KARAP} that the following polynomial is equivalent to multiplication of the two integers the polynomials represent.

\begin{equation}
f(x) \cdot g(x) = acx^2 + ((a - b)(c - d) - (ac + bd))x + bd
\end{equation}

Using the observation that $ac$ and $bd$ could be re-used only three half sized multiplications would be required to produce the product.  Applying
this algorithm recursively, the work factor becomes $O(n^{lg(3)})$ which is substantially better than the work factor $O(n^2)$ of the Comba technique.  It turns 
out what Karatsuba did not know or at least did not publish was that this is simply polynomial basis multiplication with the points 
$\zeta_0$, $\zeta_{\infty}$ and $-\zeta_{-1}$.  Consider the resultant system of equations.

\begin{center}
\begin{tabular}{rcrcrcrc}
$\zeta_{0}$ &      $=$ &  &  &  & & $w_0$ \\
$-\zeta_{-1}$ &    $=$ & $-w_2$ & $+$ & $w_1$ & $-$ & $w_0$ \\
$\zeta_{\infty}$ & $=$ & $w_2$ &  & &  & \\
\end{tabular}
\end{center}

By adding the first and last equation to the equation in the middle the term $w_1$ can be isolated and all three coefficients solved for.  The simplicity
of this system of equations has made Karatsuba fairly popular.  In fact the cutoff point is often fairly low\footnote{With LibTomMath 0.18 it is 70 and 109 digits for the Intel P4 and AMD Athlon respectively.}
making it an ideal algorithm to speed up certain public key cryptosystems such as RSA and Diffie-Hellman.  It is worth noting that the point 
$\zeta_1$ could be substituted for $-\zeta_{-1}$.  In this case the first and third row are subtracted instead of added to the second row.  

\newpage\begin{figure}[!here]
\begin{small}
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_karatsuba\_mul}. \\
\textbf{Input}.   mp\_int $a$ and mp\_int $b$ \\







|





|




|






|
<







4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145

4146
4147
4148
4149
4150
4151
4152

\subsection{Karatsuba Multiplication}
Karatsuba \cite{KARA} multiplication when originally proposed in 1962 was among the first set of algorithms to break the $O(n^2)$ barrier for
general purpose multiplication.  Given two polynomial basis representations $f(x) = ax + b$ and $g(x) = cx + d$, Karatsuba proved with 
light algebra \cite{KARAP} that the following polynomial is equivalent to multiplication of the two integers the polynomials represent.

\begin{equation}
f(x) \cdot g(x) = acx^2 + ((a + b)(c + d) - (ac + bd))x + bd
\end{equation}

Using the observation that $ac$ and $bd$ could be re-used only three half sized multiplications would be required to produce the product.  Applying
this algorithm recursively, the work factor becomes $O(n^{lg(3)})$ which is substantially better than the work factor $O(n^2)$ of the Comba technique.  It turns 
out what Karatsuba did not know or at least did not publish was that this is simply polynomial basis multiplication with the points 
$\zeta_0$, $\zeta_{\infty}$ and $\zeta_{1}$.  Consider the resultant system of equations.

\begin{center}
\begin{tabular}{rcrcrcrc}
$\zeta_{0}$ &      $=$ &  &  &  & & $w_0$ \\
$\zeta_{1}$ &      $=$ & $w_2$ & $+$ & $w_1$ & $+$ & $w_0$ \\
$\zeta_{\infty}$ & $=$ & $w_2$ &  & &  & \\
\end{tabular}
\end{center}

By adding the first and last equation to the equation in the middle the term $w_1$ can be isolated and all three coefficients solved for.  The simplicity
of this system of equations has made Karatsuba fairly popular.  In fact the cutoff point is often fairly low\footnote{With LibTomMath 0.18 it is 70 and 109 digits for the Intel P4 and AMD Athlon respectively.}
making it an ideal algorithm to speed up certain public key cryptosystems such as RSA and Diffie-Hellman.  


\newpage\begin{figure}[!here]
\begin{small}
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_karatsuba\_mul}. \\
\textbf{Input}.   mp\_int $a$ and mp\_int $b$ \\
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
5.  $y0 \leftarrow b \mbox{ (mod }\beta^B\mbox{)}$ \\
6.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_rshd}) \\
7.  $y1 \leftarrow \lfloor b / \beta^B \rfloor$ \\
\\
Calculate the three products. \\
8.  $x0y0 \leftarrow x0 \cdot y0$ (\textit{mp\_mul}) \\
9.  $x1y1 \leftarrow x1 \cdot y1$ \\
10.  $t1 \leftarrow x1 - x0$ (\textit{mp\_sub}) \\
11.  $x0 \leftarrow y1 - y0$ \\
12.  $t1 \leftarrow t1 \cdot x0$ \\
\\
Calculate the middle term. \\
13.  $x0 \leftarrow x0y0 + x1y1$ \\
14.  $t1 \leftarrow x0 - t1$ \\
\\
Calculate the final product. \\
15.  $t1 \leftarrow t1 \cdot \beta^B$ (\textit{mp\_lshd}) \\
16.  $x1y1 \leftarrow x1y1 \cdot \beta^{2B}$ \\
17.  $t1 \leftarrow x0y0 + t1$ \\
18.  $c \leftarrow t1 + x1y1$ \\
19.  Clear all of the temporary variables. \\







|
|




|







4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
5.  $y0 \leftarrow b \mbox{ (mod }\beta^B\mbox{)}$ \\
6.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_rshd}) \\
7.  $y1 \leftarrow \lfloor b / \beta^B \rfloor$ \\
\\
Calculate the three products. \\
8.  $x0y0 \leftarrow x0 \cdot y0$ (\textit{mp\_mul}) \\
9.  $x1y1 \leftarrow x1 \cdot y1$ \\
10.  $t1 \leftarrow x1 + x0$ (\textit{mp\_add}) \\
11.  $x0 \leftarrow y1 + y0$ \\
12.  $t1 \leftarrow t1 \cdot x0$ \\
\\
Calculate the middle term. \\
13.  $x0 \leftarrow x0y0 + x1y1$ \\
14.  $t1 \leftarrow t1 - x0$ (\textit{s\_mp\_sub}) \\
\\
Calculate the final product. \\
15.  $t1 \leftarrow t1 \cdot \beta^B$ (\textit{mp\_lshd}) \\
16.  $x1y1 \leftarrow x1y1 \cdot \beta^{2B}$ \\
17.  $t1 \leftarrow x0y0 + t1$ \\
18.  $c \leftarrow t1 + x1y1$ \\
19.  Clear all of the temporary variables. \\
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
\index{radix point}
In order to split the two inputs into their respective halves, a suitable \textit{radix point} must be chosen.  The radix point chosen must
be used for both of the inputs meaning that it must be smaller than the smallest input.  Step 3 chooses the radix point $B$ as half of the 
smallest input \textbf{used} count.  After the radix point is chosen the inputs are split into lower and upper halves.  Step 4 and 5 
compute the lower halves.  Step 6 and 7 computer the upper halves.  

After the halves have been computed the three intermediate half-size products must be computed.  Step 8 and 9 compute the trivial products
$x0 \cdot y0$ and $x1 \cdot y1$.  The mp\_int $x0$ is used as a temporary variable after $x1 - x0$ has been computed.  By using $x0$ instead
of an additional temporary variable, the algorithm can avoid an addition memory allocation operation.

The remaining steps 13 through 18 compute the Karatsuba polynomial through a variety of digit shifting and addition operations.

\vspace{+3mm}\begin{small}
\hspace{-5.1mm}{\bf File}: bn\_mp\_karatsuba\_mul.c
\vspace{-3mm}
\begin{alltt}
016   
017   /* c = |a| * |b| using Karatsuba Multiplication using 
018    * three half size multiplications
019    *
020    * Let B represent the radix [e.g. 2**DIGIT_BIT] and 
021    * let n represent half of the number of digits in 
022    * the min(a,b)
023    *
024    * a = a1 * B**n + a0
025    * b = b1 * B**n + b0
026    *
027    * Then, a * b => 
028      a1b1 * B**2n + ((a1 - a0)(b1 - b0) + a0b0 + a1b1) * B + a0b0
029    *
030    * Note that a1b1 and a0b0 are used twice and only need to be 
031    * computed once.  So in total three half size (half # of 
032    * digit) multiplications are performed, a0b0, a1b1 and 
033    * (a1-b1)(a0-b0)
034    *
035    * Note that a multiplication of half the digits requires
036    * 1/4th the number of single precision multiplications so in 
037    * total after one call 25% of the single precision multiplications 
038    * are saved.  Note also that the call to mp_mul can end up back 
039    * in this function if the a0, a1, b0, or b1 are above the threshold.  
040    * This is known as divide-and-conquer and leads to the famous 







|




















|




|







4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
\index{radix point}
In order to split the two inputs into their respective halves, a suitable \textit{radix point} must be chosen.  The radix point chosen must
be used for both of the inputs meaning that it must be smaller than the smallest input.  Step 3 chooses the radix point $B$ as half of the 
smallest input \textbf{used} count.  After the radix point is chosen the inputs are split into lower and upper halves.  Step 4 and 5 
compute the lower halves.  Step 6 and 7 computer the upper halves.  

After the halves have been computed the three intermediate half-size products must be computed.  Step 8 and 9 compute the trivial products
$x0 \cdot y0$ and $x1 \cdot y1$.  The mp\_int $x0$ is used as a temporary variable after $x1 + x0$ has been computed.  By using $x0$ instead
of an additional temporary variable, the algorithm can avoid an addition memory allocation operation.

The remaining steps 13 through 18 compute the Karatsuba polynomial through a variety of digit shifting and addition operations.

\vspace{+3mm}\begin{small}
\hspace{-5.1mm}{\bf File}: bn\_mp\_karatsuba\_mul.c
\vspace{-3mm}
\begin{alltt}
016   
017   /* c = |a| * |b| using Karatsuba Multiplication using 
018    * three half size multiplications
019    *
020    * Let B represent the radix [e.g. 2**DIGIT_BIT] and 
021    * let n represent half of the number of digits in 
022    * the min(a,b)
023    *
024    * a = a1 * B**n + a0
025    * b = b1 * B**n + b0
026    *
027    * Then, a * b => 
028      a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
029    *
030    * Note that a1b1 and a0b0 are used twice and only need to be 
031    * computed once.  So in total three half size (half # of 
032    * digit) multiplications are performed, a0b0, a1b1 and 
033    * (a1+b1)(a0+b0)
034    *
035    * Note that a multiplication of half the digits requires
036    * 1/4th the number of single precision multiplications so in 
037    * total after one call 25% of the single precision multiplications 
038    * are saved.  Note also that the call to mp_mul can end up back 
039    * in this function if the a0, a1, b0, or b1 are above the threshold.  
040    * This is known as divide-and-conquer and leads to the famous 
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
117     /* now calc the products x0y0 and x1y1 */
118     /* after this x0 is no longer required, free temp [x0==t2]! */
119     if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
120       goto X1Y1;          /* x0y0 = x0*y0 */
121     if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
122       goto X1Y1;          /* x1y1 = x1*y1 */
123   
124     /* now calc x1-x0 and y1-y0 */
125     if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
126       goto X1Y1;          /* t1 = x1 - x0 */
127     if (mp_sub (&y1, &y0, &x0) != MP_OKAY)
128       goto X1Y1;          /* t2 = y1 - y0 */
129     if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
130       goto X1Y1;          /* t1 = (x1 - x0) * (y1 - y0) */
131   
132     /* add x0y0 */
133     if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
134       goto X1Y1;          /* t2 = x0y0 + x1y1 */
135     if (mp_sub (&x0, &t1, &t1) != MP_OKAY)
136       goto X1Y1;          /* t1 = x0y0 + x1y1 - (x1-x0)*(y1-y0) */
137   
138     /* shift by B */
139     if (mp_lshd (&t1, B) != MP_OKAY)
140       goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
141     if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
142       goto X1Y1;          /* x1y1 = x1y1 << 2*B */
143   







|
|

|


|




|
|







4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
117     /* now calc the products x0y0 and x1y1 */
118     /* after this x0 is no longer required, free temp [x0==t2]! */
119     if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)  
120       goto X1Y1;          /* x0y0 = x0*y0 */
121     if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
122       goto X1Y1;          /* x1y1 = x1*y1 */
123   
124     /* now calc x1+x0 and y1+y0 */
125     if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
126       goto X1Y1;          /* t1 = x1 - x0 */
127     if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
128       goto X1Y1;          /* t2 = y1 - y0 */
129     if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
130       goto X1Y1;          /* t1 = (x1 + x0) * (y1 + y0) */
131   
132     /* add x0y0 */
133     if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
134       goto X1Y1;          /* t2 = x0y0 + x1y1 */
135     if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
136       goto X1Y1;          /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
137   
138     /* shift by B */
139     if (mp_lshd (&t1, B) != MP_OKAY)
140       goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
141     if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
142       goto X1Y1;          /* x1y1 = x1y1 << 2*B */
143   
4322
4323
4324
4325
4326
4327
4328

4329
4330
4331
4332
4333
4334
4335
156   Y0:mp_clear (&y0);
157   X1:mp_clear (&x1);
158   X0:mp_clear (&x0);
159   ERR:
160     return err;
161   \}
162   #endif

\end{alltt}
\end{small}

The new coding element in this routine, not  seen in previous routines, is the usage of goto statements.  The conventional
wisdom is that goto statements should be avoided.  This is generally true, however when every single function call can fail, it makes sense
to handle error recovery with a single piece of code.  Lines 61 to 75 handle initializing all of the temporary variables 
required.  Note how each of the if statements goes to a different label in case of failure.  This allows the routine to correctly free only







>







4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
156   Y0:mp_clear (&y0);
157   X1:mp_clear (&x1);
158   X0:mp_clear (&x0);
159   ERR:
160     return err;
161   \}
162   #endif
163   
\end{alltt}
\end{small}

The new coding element in this routine, not  seen in previous routines, is the usage of goto statements.  The conventional
wisdom is that goto statements should be avoided.  This is generally true, however when every single function call can fail, it makes sense
to handle error recovery with a single piece of code.  Lines 61 to 75 handle initializing all of the temporary variables 
required.  Note how each of the if statements goes to a different label in case of failure.  This allows the routine to correctly free only
4725
4726
4727
4728
4729
4730
4731

4732
4733
4734
4735
4736
4737
4738
273        mp_clear_multi(&w0, &w1, &w2, &w3, &w4, 
274                       &a0, &a1, &a2, &b0, &b1, 
275                       &b2, &tmp1, &tmp2, NULL);
276        return res;
277   \}     
278        
279   #endif

\end{alltt}
\end{small}

The first obvious thing to note is that this algorithm is complicated.  The complexity is worth it if you are multiplying very 
large numbers.  For example, a 10,000 digit multiplication takes approximaly 99,282,205 fewer single precision multiplications with
Toom--Cook than a Comba or baseline approach (this is a savings of more than 99$\%$).  For most ``crypto'' sized numbers this
algorithm is not practical as Karatsuba has a much lower cutoff point.







>







4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
273        mp_clear_multi(&w0, &w1, &w2, &w3, &w4, 
274                       &a0, &a1, &a2, &b0, &b1, 
275                       &b2, &tmp1, &tmp2, NULL);
276        return res;
277   \}     
278        
279   #endif
280   
\end{alltt}
\end{small}

The first obvious thing to note is that this algorithm is complicated.  The complexity is worth it if you are multiplying very 
large numbers.  For example, a 10,000 digit multiplication takes approximaly 99,282,205 fewer single precision multiplications with
Toom--Cook than a Comba or baseline approach (this is a savings of more than 99$\%$).  For most ``crypto'' sized numbers this
algorithm is not practical as Karatsuba has a much lower cutoff point.
4833
4834
4835
4836
4837
4838
4839

4840
4841
4842
4843
4844
4845
4846
055   #endif
056   
057     \}
058     c->sign = (c->used > 0) ? neg : MP_ZPOS;
059     return res;
060   \}
061   #endif

\end{alltt}
\end{small}

The implementation is rather simplistic and is not particularly noteworthy.  Line 23 computes the sign of the result using the ``?'' 
operator from the C programming language.  Line 47 computes $\delta$ using the fact that $1 << k$ is equal to $2^k$.  

\section{Squaring}







>







4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
055   #endif
056   
057     \}
058     c->sign = (c->used > 0) ? neg : MP_ZPOS;
059     return res;
060   \}
061   #endif
062   
\end{alltt}
\end{small}

The implementation is rather simplistic and is not particularly noteworthy.  Line 23 computes the sign of the result using the ``?'' 
operator from the C programming language.  Line 47 computes $\delta$ using the fact that $1 << k$ is equal to $2^k$.  

\section{Squaring}
5002
5003
5004
5005
5006
5007
5008

5009
5010
5011
5012
5013
5014
5015
073   
074     mp_clamp (&t);
075     mp_exch (&t, b);
076     mp_clear (&t);
077     return MP_OKAY;
078   \}
079   #endif

\end{alltt}
\end{small}

Inside the outer loop (line 33) the square term is calculated on line 36.  The carry (line 43) has been
extracted from the mp\_word accumulator using a right shift.  Aliases for $a_{ix}$ and $t_{ix+iy}$ are initialized 
(lines 46 and 49) to simplify the inner loop.  The doubling is performed using two
additions (line 58) since it is usually faster than shifting, if not at least as fast.  







>







5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
073   
074     mp_clamp (&t);
075     mp_exch (&t, b);
076     mp_clear (&t);
077     return MP_OKAY;
078   \}
079   #endif
080   
\end{alltt}
\end{small}

Inside the outer loop (line 33) the square term is calculated on line 36.  The carry (line 43) has been
extracted from the mp\_word accumulator using a right shift.  Aliases for $a_{ix}$ and $t_{ix+iy}$ are initialized 
(lines 46 and 49) to simplify the inner loop.  The doubling is performed using two
additions (line 58) since it is usually faster than shifting, if not at least as fast.  
5184
5185
5186
5187
5188
5189
5190

5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
103         *tmpb++ = 0;
104       \}
105     \}
106     mp_clamp (b);
107     return MP_OKAY;
108   \}
109   #endif

\end{alltt}
\end{small}

This implementation is essentially a copy of Comba multiplication with the appropriate changes added to make it faster for 
the special case of squaring.  

\subsection{Polynomial Basis Squaring}
The same algorithm that performs optimal polynomial basis multiplication can be used to perform polynomial basis squaring.  The minor exception
is that $\zeta_y = f(y)g(y)$ is actually equivalent to $\zeta_y = f(y)^2$ since $f(y) = g(y)$.  Instead of performing $2n + 1$
multiplications to find the $\zeta$ relations, squaring operations are performed instead.  

\subsection{Karatsuba Squaring}
Let $f(x) = ax + b$ represent the polynomial basis representation of a number to square.  
Let $h(x) = \left ( f(x) \right )^2$ represent the square of the polynomial.  The Karatsuba equation can be modified to square a 
number with the following equation.

\begin{equation}
h(x) = a^2x^2 + \left (a^2 + b^2 - (a - b)^2 \right )x + b^2
\end{equation}

Upon closer inspection this equation only requires the calculation of three half-sized squares: $a^2$, $b^2$ and $(a - b)^2$.  As in 
Karatsuba multiplication, this algorithm can be applied recursively on the input and will achieve an asymptotic running time of 
$O \left ( n^{lg(3)} \right )$.

If the asymptotic times of Karatsuba squaring and multiplication are the same, why not simply use the multiplication algorithm 
instead?  The answer to this arises from the cutoff point for squaring.  As in multiplication there exists a cutoff point, at which the 
time required for a Comba based squaring and a Karatsuba based squaring meet.  Due to the overhead inherent in the Karatsuba method, the cutoff 
point is fairly high.  For example, on an AMD Athlon XP processor with $\beta = 2^{28}$, the cutoff point is around 127 digits.  







>

















|


|







5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
103         *tmpb++ = 0;
104       \}
105     \}
106     mp_clamp (b);
107     return MP_OKAY;
108   \}
109   #endif
110   
\end{alltt}
\end{small}

This implementation is essentially a copy of Comba multiplication with the appropriate changes added to make it faster for 
the special case of squaring.  

\subsection{Polynomial Basis Squaring}
The same algorithm that performs optimal polynomial basis multiplication can be used to perform polynomial basis squaring.  The minor exception
is that $\zeta_y = f(y)g(y)$ is actually equivalent to $\zeta_y = f(y)^2$ since $f(y) = g(y)$.  Instead of performing $2n + 1$
multiplications to find the $\zeta$ relations, squaring operations are performed instead.  

\subsection{Karatsuba Squaring}
Let $f(x) = ax + b$ represent the polynomial basis representation of a number to square.  
Let $h(x) = \left ( f(x) \right )^2$ represent the square of the polynomial.  The Karatsuba equation can be modified to square a 
number with the following equation.

\begin{equation}
h(x) = a^2x^2 + \left ((a + b)^2 - (a^2 + b^2) \right )x + b^2
\end{equation}

Upon closer inspection this equation only requires the calculation of three half-sized squares: $a^2$, $b^2$ and $(a + b)^2$.  As in 
Karatsuba multiplication, this algorithm can be applied recursively on the input and will achieve an asymptotic running time of 
$O \left ( n^{lg(3)} \right )$.

If the asymptotic times of Karatsuba squaring and multiplication are the same, why not simply use the multiplication algorithm 
instead?  The answer to this arises from the cutoff point for squaring.  As in multiplication there exists a cutoff point, at which the 
time required for a Comba based squaring and a Karatsuba based squaring meet.  Due to the overhead inherent in the Karatsuba method, the cutoff 
point is fairly high.  For example, on an AMD Athlon XP processor with $\beta = 2^{28}$, the cutoff point is around 127 digits.  
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
3.  $B \leftarrow \lfloor a.used / 2 \rfloor$ \\
4.  $x0 \leftarrow a \mbox{ (mod }\beta^B\mbox{)}$ (\textit{mp\_mod\_2d}) \\
5.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_lshd}) \\
\\
Calculate the three squares. \\
6.  $x0x0 \leftarrow x0^2$ (\textit{mp\_sqr}) \\
7.  $x1x1 \leftarrow x1^2$ \\
8.  $t1 \leftarrow x1 - x0$ (\textit{mp\_sub}) \\
9.  $t1 \leftarrow t1^2$ \\
\\
Compute the middle term. \\
10.  $t2 \leftarrow x0x0 + x1x1$ (\textit{s\_mp\_add}) \\
11.  $t1 \leftarrow t2 - t1$ \\
\\
Compute final product. \\
12.  $t1 \leftarrow t1\beta^B$ (\textit{mp\_lshd}) \\
13.  $x1x1 \leftarrow x1x1\beta^{2B}$ \\
14.  $t1 \leftarrow t1 + x0x0$ \\
15.  $b \leftarrow t1 + x1x1$ \\
16.  Return(\textit{MP\_OKAY}). \\







|




|







5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
3.  $B \leftarrow \lfloor a.used / 2 \rfloor$ \\
4.  $x0 \leftarrow a \mbox{ (mod }\beta^B\mbox{)}$ (\textit{mp\_mod\_2d}) \\
5.  $x1 \leftarrow \lfloor a / \beta^B \rfloor$ (\textit{mp\_lshd}) \\
\\
Calculate the three squares. \\
6.  $x0x0 \leftarrow x0^2$ (\textit{mp\_sqr}) \\
7.  $x1x1 \leftarrow x1^2$ \\
8.  $t1 \leftarrow x1 + x0$ (\textit{s\_mp\_add}) \\
9.  $t1 \leftarrow t1^2$ \\
\\
Compute the middle term. \\
10.  $t2 \leftarrow x0x0 + x1x1$ (\textit{s\_mp\_add}) \\
11.  $t1 \leftarrow t1 - t2$ \\
\\
Compute final product. \\
12.  $t1 \leftarrow t1\beta^B$ (\textit{mp\_lshd}) \\
13.  $x1x1 \leftarrow x1x1\beta^{2B}$ \\
14.  $t1 \leftarrow t1 + x0x0$ \\
15.  $b \leftarrow t1 + x1x1$ \\
16.  Return(\textit{MP\_OKAY}). \\
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
This algorithm computes the square of an input $a$ using the Karatsuba technique.  This algorithm is very similar to the Karatsuba based
multiplication algorithm with the exception that the three half-size multiplications have been replaced with three half-size squarings.

The radix point for squaring is simply placed exactly in the middle of the digits when the input has an odd number of digits, otherwise it is
placed just below the middle.  Step 3, 4 and 5 compute the two halves required using $B$
as the radix point.  The first two squares in steps 6 and 7 are rather straightforward while the last square is of a more compact form.

By expanding $\left (x1 - x0 \right )^2$, the $x1^2$ and $x0^2$ terms in the middle disappear, that is $x1^2 + x0^2 - (x1 - x0)^2 = 2 \cdot x0 \cdot x1$.
Now if $5n$ single precision additions and a squaring of $n$-digits is faster than multiplying two $n$-digit numbers and doubling then
this method is faster.  Assuming no further recursions occur, the difference can be estimated with the following inequality.

Let $p$ represent the cost of a single precision addition and $q$ the cost of a single precision multiplication both in terms of time\footnote{Or
machine clock cycles.}. 

\begin{equation}







|







5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
This algorithm computes the square of an input $a$ using the Karatsuba technique.  This algorithm is very similar to the Karatsuba based
multiplication algorithm with the exception that the three half-size multiplications have been replaced with three half-size squarings.

The radix point for squaring is simply placed exactly in the middle of the digits when the input has an odd number of digits, otherwise it is
placed just below the middle.  Step 3, 4 and 5 compute the two halves required using $B$
as the radix point.  The first two squares in steps 6 and 7 are rather straightforward while the last square is of a more compact form.

By expanding $\left (x1 + x0 \right )^2$, the $x1^2$ and $x0^2$ terms in the middle disappear, that is $(x0 - x1)^2 - (x1^2 + x0^2)  = 2 \cdot x0 \cdot x1$.
Now if $5n$ single precision additions and a squaring of $n$-digits is faster than multiplying two $n$-digit numbers and doubling then
this method is faster.  Assuming no further recursions occur, the difference can be estimated with the following inequality.

Let $p$ represent the cost of a single precision addition and $q$ the cost of a single precision multiplication both in terms of time\footnote{Or
machine clock cycles.}. 

\begin{equation}
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
075   
076     /* now calc the products x0*x0 and x1*x1 */
077     if (mp_sqr (&x0, &x0x0) != MP_OKAY)
078       goto X1X1;           /* x0x0 = x0*x0 */
079     if (mp_sqr (&x1, &x1x1) != MP_OKAY)
080       goto X1X1;           /* x1x1 = x1*x1 */
081   
082     /* now calc (x1-x0)**2 */
083     if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
084       goto X1X1;           /* t1 = x1 - x0 */
085     if (mp_sqr (&t1, &t1) != MP_OKAY)
086       goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */
087   
088     /* add x0y0 */
089     if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
090       goto X1X1;           /* t2 = x0x0 + x1x1 */
091     if (mp_sub (&t2, &t1, &t1) != MP_OKAY)
092       goto X1X1;           /* t1 = x0x0 + x1x1 - (x1-x0)*(x1-x0) */
093   
094     /* shift by B */
095     if (mp_lshd (&t1, B) != MP_OKAY)
096       goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
097     if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
098       goto X1X1;           /* x1x1 = x1x1 << 2*B */
099   







|
|







|
|







5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
075   
076     /* now calc the products x0*x0 and x1*x1 */
077     if (mp_sqr (&x0, &x0x0) != MP_OKAY)
078       goto X1X1;           /* x0x0 = x0*x0 */
079     if (mp_sqr (&x1, &x1x1) != MP_OKAY)
080       goto X1X1;           /* x1x1 = x1*x1 */
081   
082     /* now calc (x1+x0)**2 */
083     if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
084       goto X1X1;           /* t1 = x1 - x0 */
085     if (mp_sqr (&t1, &t1) != MP_OKAY)
086       goto X1X1;           /* t1 = (x1 - x0) * (x1 - x0) */
087   
088     /* add x0y0 */
089     if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
090       goto X1X1;           /* t2 = x0x0 + x1x1 */
091     if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
092       goto X1X1;           /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
093   
094     /* shift by B */
095     if (mp_lshd (&t1, B) != MP_OKAY)
096       goto X1X1;           /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
097     if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
098       goto X1X1;           /* x1x1 = x1x1 << 2*B */
099   
5394
5395
5396
5397
5398
5399
5400

5401
5402
5403
5404
5405
5406
5407
110   T1:mp_clear (&t1);
111   X1:mp_clear (&x1);
112   X0:mp_clear (&x0);
113   ERR:
114     return err;
115   \}
116   #endif

\end{alltt}
\end{small}

This implementation is largely based on the implementation of algorithm mp\_karatsuba\_mul.  It uses the same inline style to copy and 
shift the input into the two halves.  The loop from line 53 to line 69 has been modified since only one input exists.  The \textbf{used}
count of both $x0$ and $x1$ is fixed up and $x0$ is clamped before the calculations begin.  At this point $x1$ and $x0$ are valid equivalents
to the respective halves as if mp\_rshd and mp\_mod\_2d had been used.  







>







5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
110   T1:mp_clear (&t1);
111   X1:mp_clear (&x1);
112   X0:mp_clear (&x0);
113   ERR:
114     return err;
115   \}
116   #endif
117   
\end{alltt}
\end{small}

This implementation is largely based on the implementation of algorithm mp\_karatsuba\_mul.  It uses the same inline style to copy and 
shift the input into the two halves.  The loop from line 53 to line 69 has been modified since only one input exists.  The \textbf{used}
count of both $x0$ and $x1$ is fixed up and $x0$ is clamped before the calculations begin.  At this point $x1$ and $x0$ are valid equivalents
to the respective halves as if mp\_rshd and mp\_mod\_2d had been used.  
5490
5491
5492
5493
5494
5495
5496

5497
5498
5499
5500
5501
5502
5503
047         res = MP_VAL;
048   #endif
049     \}
050     b->sign = MP_ZPOS;
051     return res;
052   \}
053   #endif

\end{alltt}
\end{small}

\section*{Exercises}
\begin{tabular}{cl}
$\left [ 3 \right ] $ & Devise an efficient algorithm for selection of the radix point to handle inputs \\
                      & that have different number of digits in Karatsuba multiplication. \\







>







5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
047         res = MP_VAL;
048   #endif
049     \}
050     b->sign = MP_ZPOS;
051     return res;
052   \}
053   #endif
054   
\end{alltt}
\end{small}

\section*{Exercises}
\begin{tabular}{cl}
$\left [ 3 \right ] $ & Devise an efficient algorithm for selection of the radix point to handle inputs \\
                      & that have different number of digits in Karatsuba multiplication. \\
5823
5824
5825
5826
5827
5828
5829

5830
5831
5832
5833
5834
5835
5836
089     
090   CLEANUP:
091     mp_clear (&q);
092   
093     return res;
094   \}
095   #endif

\end{alltt}
\end{small}

The first multiplication that determines the quotient can be performed by only producing the digits from $m - 1$ and up.  This essentially halves
the number of single precision multiplications required.  However, the optimization is only safe if $\beta$ is much larger than the number of digits
in the modulus.  In the source code this is evaluated on lines 36 to 43 where algorithm s\_mp\_mul\_high\_digs is used when it is
safe to do so.  







>







5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
089     
090   CLEANUP:
091     mp_clear (&q);
092   
093     return res;
094   \}
095   #endif
096   
\end{alltt}
\end{small}

The first multiplication that determines the quotient can be performed by only producing the digits from $m - 1$ and up.  This essentially halves
the number of single precision multiplications required.  However, the optimization is only safe if $\beta$ is much larger than the number of digits
in the modulus.  In the source code this is evaluated on lines 36 to 43 where algorithm s\_mp\_mul\_high\_digs is used when it is
safe to do so.  
5875
5876
5877
5878
5879
5880
5881

5882
5883
5884
5885
5886
5887
5888
023     
024     if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) \{
025       return res;
026     \}
027     return mp_div (a, b, a, NULL);
028   \}
029   #endif

\end{alltt}
\end{small}

This simple routine calculates the reciprocal $\mu$ required by Barrett reduction.  Note the extended usage of algorithm mp\_div where the variable
which would received the remainder is passed as NULL.  As will be discussed in~\ref{sec:division} the division routine allows both the quotient and the 
remainder to be passed as NULL meaning to ignore the value.  








>







5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
023     
024     if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) \{
025       return res;
026     \}
027     return mp_div (a, b, a, NULL);
028   \}
029   #endif
030   
\end{alltt}
\end{small}

This simple routine calculates the reciprocal $\mu$ required by Barrett reduction.  Note the extended usage of algorithm mp\_div where the variable
which would received the remainder is passed as NULL.  As will be discussed in~\ref{sec:division} the division routine allows both the quotient and the 
remainder to be passed as NULL meaning to ignore the value.  

6230
6231
6232
6233
6234
6235
6236

6237
6238
6239
6240
6241
6242
6243
107     if (mp_cmp_mag (x, n) != MP_LT) \{
108       return s_mp_sub (x, n, x);
109     \}
110   
111     return MP_OKAY;
112   \}
113   #endif

\end{alltt}
\end{small}

This is the baseline implementation of the Montgomery reduction algorithm.  Lines 30 to 35 determine if the Comba based
routine can be used instead.  Line 48 computes the value of $\mu$ for that particular iteration of the outer loop.  

The multiplication $\mu n \beta^{ix}$ is performed in one step in the inner loop.  The alias $tmpx$ refers to the $ix$'th digit of $x$ and







>







6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
107     if (mp_cmp_mag (x, n) != MP_LT) \{
108       return s_mp_sub (x, n, x);
109     \}
110   
111     return MP_OKAY;
112   \}
113   #endif
114   
\end{alltt}
\end{small}

This is the baseline implementation of the Montgomery reduction algorithm.  Lines 30 to 35 determine if the Comba based
routine can be used instead.  Line 48 computes the value of $\mu$ for that particular iteration of the outer loop.  

The multiplication $\mu n \beta^{ix}$ is performed in one step in the inner loop.  The alias $tmpx$ refers to the $ix$'th digit of $x$ and
6474
6475
6476
6477
6478
6479
6480

6481
6482
6483
6484
6485
6486
6487
161     /* if A >= m then A = A - m */
162     if (mp_cmp_mag (x, n) != MP_LT) \{
163       return s_mp_sub (x, n, x);
164     \}
165     return MP_OKAY;
166   \}
167   #endif

\end{alltt}
\end{small}

The $\hat W$ array is first filled with digits of $x$ on line 50 then the rest of the digits are zeroed on line 54.  Both loops share
the same alias variables to make the code easier to read.  

The value of $\mu$ is calculated in an interesting fashion.  First the value $\hat W_{ix}$ is reduced modulo $\beta$ and cast to a mp\_digit.  This







>







6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
161     /* if A >= m then A = A - m */
162     if (mp_cmp_mag (x, n) != MP_LT) \{
163       return s_mp_sub (x, n, x);
164     \}
165     return MP_OKAY;
166   \}
167   #endif
168   
\end{alltt}
\end{small}

The $\hat W$ array is first filled with digits of $x$ on line 50 then the rest of the digits are zeroed on line 54.  Both loops share
the same alias variables to make the code easier to read.  

The value of $\mu$ is calculated in an interesting fashion.  First the value $\hat W_{ix}$ is reduced modulo $\beta$ and cast to a mp\_digit.  This
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_montgomery\_setup}. \\
\textbf{Input}.   mp\_int $n$ ($n > 1$ and $(n, 2) = 1$) \\
\textbf{Output}.  $\rho \equiv -1/n_0 \mbox{ (mod }\beta\mbox{)}$ \\
\hline \\
1.  $b \leftarrow n_0$ \\
2.  If $b$ is even return(\textit{MP\_VAL}) \\
3.  $x \leftarrow ((b + 2) \mbox{ AND } 4) << 1) + b$ \\
4.  for $k$ from 0 to $\lceil lg(lg(\beta)) \rceil - 2$ do \\
\hspace{3mm}4.1  $x \leftarrow x \cdot (2 - bx)$ \\
5.  $\rho \leftarrow \beta - x \mbox{ (mod }\beta\mbox{)}$ \\
6.  Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}







|







6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
\begin{tabular}{l}
\hline Algorithm \textbf{mp\_montgomery\_setup}. \\
\textbf{Input}.   mp\_int $n$ ($n > 1$ and $(n, 2) = 1$) \\
\textbf{Output}.  $\rho \equiv -1/n_0 \mbox{ (mod }\beta\mbox{)}$ \\
\hline \\
1.  $b \leftarrow n_0$ \\
2.  If $b$ is even return(\textit{MP\_VAL}) \\
3.  $x \leftarrow (((b + 2) \mbox{ AND } 4) << 1) + b$ \\
4.  for $k$ from 0 to $\lceil lg(lg(\beta)) \rceil - 2$ do \\
\hspace{3mm}4.1  $x \leftarrow x \cdot (2 - bx)$ \\
5.  $\rho \leftarrow \beta - x \mbox{ (mod }\beta\mbox{)}$ \\
6.  Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}
6560
6561
6562
6563
6564
6565
6566

6567
6568
6569
6570
6571
6572
6573
048   
049     /* rho = -1/m mod b */
050     *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
051   
052     return MP_OKAY;
053   \}
054   #endif

\end{alltt}
\end{small}

This source code computes the value of $\rho$ required to perform Montgomery reduction.  It has been modified to avoid performing excess
multiplications when $\beta$ is not the default 28-bits.  

\section{The Diminished Radix Algorithm}







>







6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
048   
049     /* rho = -1/m mod b */
050     *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
051   
052     return MP_OKAY;
053   \}
054   #endif
055   
\end{alltt}
\end{small}

This source code computes the value of $\rho$ required to perform Montgomery reduction.  It has been modified to avoid performing excess
multiplications when $\beta$ is not the default 28-bits.  

\section{The Diminished Radix Algorithm}
6826
6827
6828
6829
6830
6831
6832

6833
6834
6835
6836
6837
6838
6839
083     if (mp_cmp_mag (x, n) != MP_LT) \{
084       s_mp_sub(x, n, x);
085       goto top;
086     \}
087     return MP_OKAY;
088   \}
089   #endif

\end{alltt}
\end{small}

The first step is to grow $x$ as required to $2m$ digits since the reduction is performed in place on $x$.  The label on line 51 is where
the algorithm will resume if further reduction passes are required.  In theory it could be placed at the top of the function however, the size of
the modulus and question of whether $x$ is large enough are invariant after the first pass meaning that it would be a waste of time.  








>







6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
083     if (mp_cmp_mag (x, n) != MP_LT) \{
084       s_mp_sub(x, n, x);
085       goto top;
086     \}
087     return MP_OKAY;
088   \}
089   #endif
090   
\end{alltt}
\end{small}

The first step is to grow $x$ as required to $2m$ digits since the reduction is performed in place on $x$.  The label on line 51 is where
the algorithm will resume if further reduction passes are required.  In theory it could be placed at the top of the function however, the size of
the modulus and question of whether $x$ is large enough are invariant after the first pass meaning that it would be a waste of time.  

6881
6882
6883
6884
6885
6886
6887

6888
6889
6890
6891
6892
6893
6894
021       * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
022       */
023      *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
024           ((mp_word)a->dp[0]));
025   \}
026   
027   #endif

\end{alltt}
\end{small}

\subsubsection{Modulus Detection}
Another algorithm which will be useful is the ability to detect a restricted Diminished Radix modulus.  An integer is said to be
of restricted Diminished Radix form if all of the digits are equal to $\beta - 1$ except the trailing digit which may be any value.








>







6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
021       * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
022       */
023      *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
024           ((mp_word)a->dp[0]));
025   \}
026   
027   #endif
028   
\end{alltt}
\end{small}

\subsubsection{Modulus Detection}
Another algorithm which will be useful is the ability to detect a restricted Diminished Radix modulus.  An integer is said to be
of restricted Diminished Radix form if all of the digits are equal to $\beta - 1$ except the trailing digit which may be any value.

6939
6940
6941
6942
6943
6944
6945

6946
6947
6948
6949
6950
6951
6952
032             return 0;
033          \}
034      \}
035      return 1;
036   \}
037   
038   #endif

\end{alltt}
\end{small}

\subsection{Unrestricted Diminished Radix Reduction}
The unrestricted Diminished Radix algorithm allows modular reductions to be performed when the modulus is of the form $2^p - k$.  This algorithm
is a straightforward adaptation of algorithm~\ref{fig:DR}.








>







6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
032             return 0;
033          \}
034      \}
035      return 1;
036   \}
037   
038   #endif
039   
\end{alltt}
\end{small}

\subsection{Unrestricted Diminished Radix Reduction}
The unrestricted Diminished Radix algorithm allows modular reductions to be performed when the modulus is of the form $2^p - k$.  This algorithm
is a straightforward adaptation of algorithm~\ref{fig:DR}.

7023
7024
7025
7026
7027
7028
7029

7030
7031
7032
7033
7034
7035
7036
050      
051   ERR:
052      mp_clear(&q);
053      return res;
054   \}
055   
056   #endif

\end{alltt}
\end{small}

The algorithm mp\_count\_bits calculates the number of bits in an mp\_int which is used to find the initial value of $p$.  The call to mp\_div\_2d
on line 30 calculates both the quotient $q$ and the remainder $a$ required.  By doing both in a single function call the code size
is kept fairly small.  The multiplication by $k$ is only performed if $k > 1$. This allows reductions modulo $2^p - 1$ to be performed without
any multiplications.  







>







7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
050      
051   ERR:
052      mp_clear(&q);
053      return res;
054   \}
055   
056   #endif
057   
\end{alltt}
\end{small}

The algorithm mp\_count\_bits calculates the number of bits in an mp\_int which is used to find the initial value of $p$.  The call to mp\_div\_2d
on line 30 calculates both the quotient $q$ and the remainder $a$ required.  By doing both in a single function call the code size
is kept fairly small.  The multiplication by $k$ is only performed if $k > 1$. This allows reductions modulo $2^p - 1$ to be performed without
any multiplications.  
7092
7093
7094
7095
7096
7097
7098

7099
7100
7101
7102
7103
7104
7105
036      \}
037      
038      *d = tmp.dp[0];
039      mp_clear(&tmp);
040      return MP_OKAY;
041   \}
042   #endif

\end{alltt}
\end{small}

\subsubsection{Unrestricted Detection}
An integer $n$ is a valid unrestricted Diminished Radix modulus if either of the following are true.

\begin{enumerate}







>







7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
036      \}
037      
038      *d = tmp.dp[0];
039      mp_clear(&tmp);
040      return MP_OKAY;
041   \}
042   #endif
043   
\end{alltt}
\end{small}

\subsubsection{Unrestricted Detection}
An integer $n$ is a valid unrestricted Diminished Radix modulus if either of the following are true.

\begin{enumerate}
7168
7169
7170
7171
7172
7173
7174

7175
7176
7177
7178
7179
7180
7181
041             \}
042         \}
043      \}
044      return MP_YES;
045   \}
046   
047   #endif

\end{alltt}
\end{small}



\section{Algorithm Comparison}
So far three very different algorithms for modular reduction have been discussed.  Each of the algorithms have their own strengths and weaknesses







>







7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
041             \}
042         \}
043      \}
044      return MP_YES;
045   \}
046   
047   #endif
048   
\end{alltt}
\end{small}



\section{Algorithm Comparison}
So far three very different algorithms for modular reduction have been discussed.  Each of the algorithms have their own strengths and weaknesses
7377
7378
7379
7380
7381
7382
7383

7384
7385
7386
7387
7388
7389
7390
046       b <<= 1;
047     \}
048   
049     mp_clear (&g);
050     return MP_OKAY;
051   \}
052   #endif

\end{alltt}
\end{small}

Line 28 sets the initial value of the result to $1$.  Next the loop on line 30 steps through each bit of the exponent starting from
the most significant down towards the least significant. The invariant squaring operation placed on line 32 is performed first.  After 
the squaring the result $c$ is multiplied by the base $g$ if and only if the most significant bit of the exponent is set.  The shift on line
46 moves all of the bits of the exponent upwards towards the most significant location.  







>







7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
046       b <<= 1;
047     \}
048   
049     mp_clear (&g);
050     return MP_OKAY;
051   \}
052   #endif
053   
\end{alltt}
\end{small}

Line 28 sets the initial value of the result to $1$.  Next the loop on line 30 steps through each bit of the exponent starting from
the most significant down towards the least significant. The invariant squaring operation placed on line 32 is performed first.  After 
the squaring the result $c$ is multiplied by the base $g$ if and only if the most significant bit of the exponent is set.  The shift on line
46 moves all of the bits of the exponent upwards towards the most significant location.  
7616
7617
7618
7619
7620
7621
7622
7623

7624
7625
7626
7627
7628
7629
7630
061   #else 
062        /* no invmod */
063        return MP_VAL;
064   #endif
065     \}
066   
067   /* modified diminished radix reduction */
068   #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C)

069     if (mp_reduce_is_2k_l(P) == MP_YES) \{
070        return s_mp_exptmod(G, X, P, Y, 1);
071     \}
072   #endif
073   
074   #ifdef BN_MP_DR_IS_MODULUS_C
075     /* is it a DR modulus? */







|
>







7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
061   #else 
062        /* no invmod */
063        return MP_VAL;
064   #endif
065     \}
066   
067   /* modified diminished radix reduction */
068   #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defin
      ed(BN_S_MP_EXPTMOD_C)
069     if (mp_reduce_is_2k_l(P) == MP_YES) \{
070        return s_mp_exptmod(G, X, P, Y, 1);
071     \}
072   #endif
073   
074   #ifdef BN_MP_DR_IS_MODULUS_C
075     /* is it a DR modulus? */
7656
7657
7658
7659
7660
7661
7662

7663
7664
7665
7666
7667
7668
7669
101   #endif
102   #ifdef BN_MP_EXPTMOD_FAST_C
103     \}
104   #endif
105   \}
106   
107   #endif

\end{alltt}
\end{small}

In order to keep the algorithms in a known state the first step on line 28 is to reject any negative modulus as input.  If the exponent is
negative the algorithm tries to perform a modular exponentiation with the modular inverse of the base $G$.  The temporary variable $tmpG$ is assigned
the modular inverse of $G$ and $tmpX$ is assigned the absolute value of $X$.  The algorithm will recuse with these new values with a positive
exponent.







>







7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
101   #endif
102   #ifdef BN_MP_EXPTMOD_FAST_C
103     \}
104   #endif
105   \}
106   
107   #endif
108   
\end{alltt}
\end{small}

In order to keep the algorithms in a known state the first step on line 28 is to reject any negative modulus as input.  If the exponent is
negative the algorithm tries to perform a modular exponentiation with the modular inverse of the base $G$.  The temporary variable $tmpG$ is assigned
the modular inverse of $G$ and $tmpX$ is assigned the absolute value of $X$.  The algorithm will recuse with these new values with a positive
exponent.
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075

8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
By step 13 there are no more digits left in the exponent.  However, there may be partial bits in the window left.  If $mode = 2$ then 
a Left-to-Right algorithm is used to process the remaining few bits.  

\vspace{+3mm}\begin{small}
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_exptmod.c
\vspace{-3mm}
\begin{alltt}
016   
017   #ifdef MP_LOW_MEM
018      #define TAB_SIZE 32
019   #else
020      #define TAB_SIZE 256
021   #endif
022   
023   int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmod
      e)
024   \{
025     mp_int  M[TAB_SIZE], res, mu;
026     mp_digit buf;
027     int     err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
028     int (*redux)(mp_int*,mp_int*,mp_int*);
029   
030     /* find window size */
031     x = mp_count_bits (X);
032     if (x <= 7) \{
033       winsize = 2;
034     \} else if (x <= 36) \{
035       winsize = 3;
036     \} else if (x <= 140) \{
037       winsize = 4;
038     \} else if (x <= 450) \{
039       winsize = 5;
040     \} else if (x <= 1303) \{
041       winsize = 6;
042     \} else if (x <= 3529) \{
043       winsize = 7;
044     \} else \{
045       winsize = 8;
046     \}
047   
048   #ifdef MP_LOW_MEM
049       if (winsize > 5) \{
050          winsize = 5;
051       \}
052   #endif
053   
054     /* init M array */
055     /* init first cell */
056     if ((err = mp_init(&M[1])) != MP_OKAY) \{
057        return err; 
058     \}
059   
060     /* now init the second half of the array */
061     for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
062       if ((err = mp_init(&M[x])) != MP_OKAY) \{
063         for (y = 1<<(winsize-1); y < x; y++) \{
064           mp_clear (&M[y]);
065         \}
066         mp_clear(&M[1]);
067         return err;
068       \}
069     \}
070   
071     /* create mu, used for Barrett reduction */
072     if ((err = mp_init (&mu)) != MP_OKAY) \{
073       goto LBL_M;
074     \}
075     
076     if (redmode == 0) \{
077        if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{
078           goto LBL_MU;
079        \}
080        redux = mp_reduce;
081     \} else \{
082        if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) \{
083           goto LBL_MU;
084        \}
085        redux = mp_reduce_2k_l;
086     \}    
087   
088     /* create M table
089      *
090      * The M table contains powers of the base, 
091      * e.g. M[x] = G**x mod P
092      *
093      * The first half of the table is not 
094      * computed though accept for M[0] and M[1]
095      */
096     if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{
097       goto LBL_MU;
098     \}
099   
100     /* compute the value at M[1<<(winsize-1)] by squaring 
101      * M[1] (winsize-1) times 
102      */
103     if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{
104       goto LBL_MU;
105     \}
106   
107     for (x = 0; x < (winsize - 1); x++) \{
108       /* square it */
109       if ((err = mp_sqr (&M[1 << (winsize - 1)], 
110                          &M[1 << (winsize - 1)])) != MP_OKAY) \{
111         goto LBL_MU;
112       \}
113   
114       /* reduce modulo P */
115       if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{
116         goto LBL_MU;
117       \}
118     \}
119   
120     /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
121      * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
122      */
123     for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{
124       if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{
125         goto LBL_MU;
126       \}
127       if ((err = redux (&M[x], P, &mu)) != MP_OKAY) \{
128         goto LBL_MU;
129       \}
130     \}
131   
132     /* setup result */
133     if ((err = mp_init (&res)) != MP_OKAY) \{
134       goto LBL_MU;
135     \}
136     mp_set (&res, 1);
137   
138     /* set initial mode and bit cnt */
139     mode   = 0;
140     bitcnt = 1;
141     buf    = 0;
142     digidx = X->used - 1;
143     bitcpy = 0;
144     bitbuf = 0;
145   
146     for (;;) \{
147       /* grab next digit as required */
148       if (--bitcnt == 0) \{
149         /* if digidx == -1 we are out of digits */
150         if (digidx == -1) \{
151           break;
152         \}
153         /* read next digit and reset the bitcnt */
154         buf    = X->dp[digidx--];
155         bitcnt = (int) DIGIT_BIT;
156       \}
157   
158       /* grab the next msb from the exponent */
159       y     = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
160       buf <<= (mp_digit)1;
161   
162       /* if the bit is zero and mode == 0 then we ignore it
163        * These represent the leading zero bits before the first 1 bit
164        * in the exponent.  Technically this opt is not required but it
165        * does lower the # of trivial squaring/reductions used
166        */
167       if (mode == 0 && y == 0) \{
168         continue;
169       \}
170   
171       /* if the bit is zero and mode == 1 then we square */
172       if (mode == 1 && y == 0) \{
173         if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
174           goto LBL_RES;
175         \}
176         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
177           goto LBL_RES;
178         \}
179         continue;
180       \}
181   
182       /* else we add it to the window */
183       bitbuf |= (y << (winsize - ++bitcpy));
184       mode    = 2;
185   
186       if (bitcpy == winsize) \{
187         /* ok window is filled so square as required and multiply  */
188         /* square first */
189         for (x = 0; x < winsize; x++) \{
190           if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
191             goto LBL_RES;
192           \}
193           if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
194             goto LBL_RES;
195           \}
196         \}
197   
198         /* then multiply */
199         if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{
200           goto LBL_RES;
201         \}
202         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
203           goto LBL_RES;
204         \}
205   
206         /* empty window and reset */
207         bitcpy = 0;
208         bitbuf = 0;
209         mode   = 1;
210       \}
211     \}
212   
213     /* if bits remain then square/multiply */
214     if (mode == 2 && bitcpy > 0) \{
215       /* square then multiply if the bit is set */
216       for (x = 0; x < bitcpy; x++) \{
217         if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
218           goto LBL_RES;
219         \}
220         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
221           goto LBL_RES;
222         \}
223   
224         bitbuf <<= 1;
225         if ((bitbuf & (1 << winsize)) != 0) \{
226           /* then multiply */
227           if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{
228             goto LBL_RES;
229           \}
230           if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
231             goto LBL_RES;
232           \}
233         \}
234       \}
235     \}
236   
237     mp_exch (&res, Y);
238     err = MP_OKAY;
239   LBL_RES:mp_clear (&res);
240   LBL_MU:mp_clear (&mu);
241   LBL_M:
242     mp_clear(&M[1]);
243     for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
244       mp_clear (&M[x]);
245     \}
246     return err;
247   \}
248   #endif

\end{alltt}
\end{small}

Lines 21 through 40 determine the optimal window size based on the length of the exponent in bits.  The window divisions are sorted
from smallest to greatest so that in each \textbf{if} statement only one condition must be tested.  For example, by the \textbf{if} statement 
on line 32 the value of $x$ is already known to be greater than $140$.  

The conditional piece of code beginning on line 48 allows the window size to be restricted to five bits.  This logic is used to ensure
the table of precomputed powers of $G$ remains relatively small.  

The for loop on line 61 initializes the $M$ array while lines 62 and 77 compute the value of $\mu$ required for
Barrett reduction.  

-- More later.

\section{Quick Power of Two}
Calculating $b = 2^a$ can be performed much quicker than with any of the previous algorithms.  Recall that a logical shift left $m << k$ is
equivalent to $m \cdot 2^k$.  By this logic when $m = 1$ a quick power of two can be achieved.







<
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>



|

|

|


|







7884
7885
7886
7887
7888
7889
7890

7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
By step 13 there are no more digits left in the exponent.  However, there may be partial bits in the window left.  If $mode = 2$ then 
a Left-to-Right algorithm is used to process the remaining few bits.  

\vspace{+3mm}\begin{small}
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_exptmod.c
\vspace{-3mm}
\begin{alltt}

016   #ifdef MP_LOW_MEM
017      #define TAB_SIZE 32
018   #else
019      #define TAB_SIZE 256
020   #endif
021   
022   int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmod
      e)
023   \{
024     mp_int  M[TAB_SIZE], res, mu;
025     mp_digit buf;
026     int     err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
027     int (*redux)(mp_int*,mp_int*,mp_int*);
028   
029     /* find window size */
030     x = mp_count_bits (X);
031     if (x <= 7) \{
032       winsize = 2;
033     \} else if (x <= 36) \{
034       winsize = 3;
035     \} else if (x <= 140) \{
036       winsize = 4;
037     \} else if (x <= 450) \{
038       winsize = 5;
039     \} else if (x <= 1303) \{
040       winsize = 6;
041     \} else if (x <= 3529) \{
042       winsize = 7;
043     \} else \{
044       winsize = 8;
045     \}
046   
047   #ifdef MP_LOW_MEM
048       if (winsize > 5) \{
049          winsize = 5;
050       \}
051   #endif
052   
053     /* init M array */
054     /* init first cell */
055     if ((err = mp_init(&M[1])) != MP_OKAY) \{
056        return err; 
057     \}
058   
059     /* now init the second half of the array */
060     for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
061       if ((err = mp_init(&M[x])) != MP_OKAY) \{
062         for (y = 1<<(winsize-1); y < x; y++) \{
063           mp_clear (&M[y]);
064         \}
065         mp_clear(&M[1]);
066         return err;
067       \}
068     \}
069   
070     /* create mu, used for Barrett reduction */
071     if ((err = mp_init (&mu)) != MP_OKAY) \{
072       goto LBL_M;
073     \}
074     
075     if (redmode == 0) \{
076        if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{
077           goto LBL_MU;
078        \}
079        redux = mp_reduce;
080     \} else \{
081        if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) \{
082           goto LBL_MU;
083        \}
084        redux = mp_reduce_2k_l;
085     \}    
086   
087     /* create M table
088      *
089      * The M table contains powers of the base, 
090      * e.g. M[x] = G**x mod P
091      *
092      * The first half of the table is not 
093      * computed though accept for M[0] and M[1]
094      */
095     if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{
096       goto LBL_MU;
097     \}
098   
099     /* compute the value at M[1<<(winsize-1)] by squaring 
100      * M[1] (winsize-1) times 
101      */
102     if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{
103       goto LBL_MU;
104     \}
105   
106     for (x = 0; x < (winsize - 1); x++) \{
107       /* square it */
108       if ((err = mp_sqr (&M[1 << (winsize - 1)], 
109                          &M[1 << (winsize - 1)])) != MP_OKAY) \{
110         goto LBL_MU;
111       \}
112   
113       /* reduce modulo P */
114       if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{
115         goto LBL_MU;
116       \}
117     \}
118   
119     /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
120      * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
121      */
122     for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{
123       if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{
124         goto LBL_MU;
125       \}
126       if ((err = redux (&M[x], P, &mu)) != MP_OKAY) \{
127         goto LBL_MU;
128       \}
129     \}
130   
131     /* setup result */
132     if ((err = mp_init (&res)) != MP_OKAY) \{
133       goto LBL_MU;
134     \}
135     mp_set (&res, 1);
136   
137     /* set initial mode and bit cnt */
138     mode   = 0;
139     bitcnt = 1;
140     buf    = 0;
141     digidx = X->used - 1;
142     bitcpy = 0;
143     bitbuf = 0;
144   
145     for (;;) \{
146       /* grab next digit as required */
147       if (--bitcnt == 0) \{
148         /* if digidx == -1 we are out of digits */
149         if (digidx == -1) \{
150           break;
151         \}
152         /* read next digit and reset the bitcnt */
153         buf    = X->dp[digidx--];
154         bitcnt = (int) DIGIT_BIT;
155       \}
156   
157       /* grab the next msb from the exponent */
158       y     = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
159       buf <<= (mp_digit)1;
160   
161       /* if the bit is zero and mode == 0 then we ignore it
162        * These represent the leading zero bits before the first 1 bit
163        * in the exponent.  Technically this opt is not required but it
164        * does lower the # of trivial squaring/reductions used
165        */
166       if (mode == 0 && y == 0) \{
167         continue;
168       \}
169   
170       /* if the bit is zero and mode == 1 then we square */
171       if (mode == 1 && y == 0) \{
172         if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
173           goto LBL_RES;
174         \}
175         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
176           goto LBL_RES;
177         \}
178         continue;
179       \}
180   
181       /* else we add it to the window */
182       bitbuf |= (y << (winsize - ++bitcpy));
183       mode    = 2;
184   
185       if (bitcpy == winsize) \{
186         /* ok window is filled so square as required and multiply  */
187         /* square first */
188         for (x = 0; x < winsize; x++) \{
189           if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
190             goto LBL_RES;
191           \}
192           if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
193             goto LBL_RES;
194           \}
195         \}
196   
197         /* then multiply */
198         if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{
199           goto LBL_RES;
200         \}
201         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
202           goto LBL_RES;
203         \}
204   
205         /* empty window and reset */
206         bitcpy = 0;
207         bitbuf = 0;
208         mode   = 1;
209       \}
210     \}
211   
212     /* if bits remain then square/multiply */
213     if (mode == 2 && bitcpy > 0) \{
214       /* square then multiply if the bit is set */
215       for (x = 0; x < bitcpy; x++) \{
216         if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
217           goto LBL_RES;
218         \}
219         if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
220           goto LBL_RES;
221         \}
222   
223         bitbuf <<= 1;
224         if ((bitbuf & (1 << winsize)) != 0) \{
225           /* then multiply */
226           if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{
227             goto LBL_RES;
228           \}
229           if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
230             goto LBL_RES;
231           \}
232         \}
233       \}
234     \}
235   
236     mp_exch (&res, Y);
237     err = MP_OKAY;
238   LBL_RES:mp_clear (&res);
239   LBL_MU:mp_clear (&mu);
240   LBL_M:
241     mp_clear(&M[1]);
242     for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
243       mp_clear (&M[x]);
244     \}
245     return err;
246   \}
247   #endif
248   
\end{alltt}
\end{small}

Lines 31 through 41 determine the optimal window size based on the length of the exponent in bits.  The window divisions are sorted
from smallest to greatest so that in each \textbf{if} statement only one condition must be tested.  For example, by the \textbf{if} statement 
on line 33 the value of $x$ is already known to be greater than $140$.  

The conditional piece of code beginning on line 47 allows the window size to be restricted to five bits.  This logic is used to ensure
the table of precomputed powers of $G$ remains relatively small.  

The for loop on line 60 initializes the $M$ array while lines 61 and 76 compute the value of $\mu$ required for
Barrett reduction.  

-- More later.

\section{Quick Power of Two}
Calculating $b = 2^a$ can be performed much quicker than with any of the previous algorithms.  Recall that a logical shift left $m << k$ is
equivalent to $m \cdot 2^k$.  By this logic when $m = 1$ a quick power of two can be achieved.
8142
8143
8144
8145
8146
8147
8148

8149
8150
8151
8152
8153
8154
8155
037   
038     /* put the single bit in its place */
039     a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
040   
041     return MP_OKAY;
042   \}
043   #endif

\end{alltt}
\end{small}

\chapter{Higher Level Algorithms}

This chapter discusses the various higher level algorithms that are required to complete a well rounded multiple precision integer package.  These
routines are less performance oriented than the algorithms of chapters five, six and seven but are no less important.  







>







8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
037   
038     /* put the single bit in its place */
039     a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
040   
041     return MP_OKAY;
042   \}
043   #endif
044   
\end{alltt}
\end{small}

\chapter{Higher Level Algorithms}

This chapter discusses the various higher level algorithms that are required to complete a well rounded multiple precision integer package.  These
routines are less performance oriented than the algorithms of chapters five, six and seven but are no less important.  
8662
8663
8664
8665
8666
8667
8668

8669
8670
8671
8672
8673
8674
8675
281   LBL_Q:mp_clear (&q);
282     return res;
283   \}
284   
285   #endif
286   
287   #endif

\end{alltt}
\end{small}

The implementation of this algorithm differs slightly from the pseudo code presented previously.  In this algorithm either of the quotient $c$ or
remainder $d$ may be passed as a \textbf{NULL} pointer which indicates their value is not desired.  For example, the C code to call the division
algorithm with only the quotient is 








>







8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
281   LBL_Q:mp_clear (&q);
282     return res;
283   \}
284   
285   #endif
286   
287   #endif
288   
\end{alltt}
\end{small}

The implementation of this algorithm differs slightly from the pseudo code presented previously.  In this algorithm either of the quotient $c$ or
remainder $d$ may be passed as a \textbf{NULL} pointer which indicates their value is not desired.  For example, the C code to call the division
algorithm with only the quotient is 

8816
8817
8818
8819
8820
8821
8822

8823
8824
8825
8826
8827
8828
8829
098     \}
099     mp_clamp(c);
100   
101     return MP_OKAY;
102   \}
103   
104   #endif

\end{alltt}
\end{small}

Clever use of the letter 't'.

\subsubsection{Subtraction}
The single digit subtraction algorithm mp\_sub\_d is essentially the same except it uses mp\_sub to subtract the digit from the mp\_int.







>







8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
098     \}
099     mp_clamp(c);
100   
101     return MP_OKAY;
102   \}
103   
104   #endif
105   
\end{alltt}
\end{small}

Clever use of the letter 't'.

\subsubsection{Subtraction}
The single digit subtraction algorithm mp\_sub\_d is essentially the same except it uses mp\_sub to subtract the digit from the mp\_int.
8925
8926
8927
8928
8929
8930
8931

8932
8933
8934
8935
8936
8937
8938
068     /* set used count */
069     c->used = a->used + 1;
070     mp_clamp(c);
071   
072     return MP_OKAY;
073   \}
074   #endif

\end{alltt}
\end{small}

In this implementation the destination $c$ may point to the same mp\_int as the source $a$ since the result is written after the digit is 
read from the source.  This function uses pointer aliases $tmpa$ and $tmpc$ for the digits of $a$ and $c$ respectively.  

\subsection{Single Digit Division}







>







8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
068     /* set used count */
069     c->used = a->used + 1;
070     mp_clamp(c);
071   
072     return MP_OKAY;
073   \}
074   #endif
075   
\end{alltt}
\end{small}

In this implementation the destination $c$ may point to the same mp\_int as the source $a$ since the result is written after the digit is 
read from the source.  This function uses pointer aliases $tmpa$ and $tmpc$ for the digits of $a$ and $c$ respectively.  

\subsection{Single Digit Division}
9070
9071
9072
9073
9074
9075
9076

9077
9078
9079
9080
9081
9082
9083
099     \}
100     mp_clear(&q);
101     
102     return res;
103   \}
104   
105   #endif

\end{alltt}
\end{small}

Like the implementation of algorithm mp\_div this algorithm allows either of the quotient or remainder to be passed as a \textbf{NULL} pointer to
indicate the respective value is not required.  This allows a trivial single digit modular reduction algorithm, mp\_mod\_d to be created.

The division and remainder on lines 43 and @45,%@ can be replaced often by a single division on most processors.  For example, the 32-bit x86 based 







>







9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
099     \}
100     mp_clear(&q);
101     
102     return res;
103   \}
104   
105   #endif
106   
\end{alltt}
\end{small}

Like the implementation of algorithm mp\_div this algorithm allows either of the quotient or remainder to be passed as a \textbf{NULL} pointer to
indicate the respective value is not required.  This allows a trivial single digit modular reduction algorithm, mp\_mod\_d to be created.

The division and remainder on lines 43 and @45,%@ can be replaced often by a single division on most processors.  For example, the 32-bit x86 based 
9256
9257
9258
9259
9260
9261
9262

9263
9264
9265
9266
9267
9268
9269
121   
122   LBL_T3:mp_clear (&t3);
123   LBL_T2:mp_clear (&t2);
124   LBL_T1:mp_clear (&t1);
125     return res;
126   \}
127   #endif

\end{alltt}
\end{small}

\section{Random Number Generation}

Random numbers come up in a variety of activities from public key cryptography to simple simulations and various randomized algorithms.  Pollard-Rho 
factoring for example, can make use of random values as starting points to find factors of a composite integer.  In this case the algorithm presented







>







9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
121   
122   LBL_T3:mp_clear (&t3);
123   LBL_T2:mp_clear (&t2);
124   LBL_T1:mp_clear (&t1);
125     return res;
126   \}
127   #endif
128   
\end{alltt}
\end{small}

\section{Random Number Generation}

Random numbers come up in a variety of activities from public key cryptography to simple simulations and various randomized algorithms.  Pollard-Rho 
factoring for example, can make use of random values as starting points to find factors of a composite integer.  In this case the algorithm presented
9332
9333
9334
9335
9336
9337
9338

9339
9340
9341
9342
9343
9344
9345
044         return res;
045       \}
046     \}
047   
048     return MP_OKAY;
049   \}
050   #endif

\end{alltt}
\end{small}

\section{Formatted Representations}
The ability to emit a radix-$n$ textual representation of an integer is useful for interacting with human parties.  For example, the ability to
be given a string of characters such as ``114585'' and turn it into the radix-$\beta$ equivalent would make it easier to enter numbers
into a program.







>







9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
044         return res;
045       \}
046     \}
047   
048     return MP_OKAY;
049   \}
050   #endif
051   
\end{alltt}
\end{small}

\section{Formatted Representations}
The ability to emit a radix-$n$ textual representation of an integer is useful for interacting with human parties.  For example, the ability to
be given a string of characters such as ``114585'' and turn it into the radix-$\beta$ equivalent would make it easier to enter numbers
into a program.
9476
9477
9478
9479
9480
9481
9482

9483
9484
9485
9486
9487
9488
9489
071     /* set the sign only if a != 0 */
072     if (mp_iszero(a) != 1) \{
073        a->sign = neg;
074     \}
075     return MP_OKAY;
076   \}
077   #endif

\end{alltt}
\end{small}

\subsection{Generating Radix-$n$ Output}
Generating radix-$n$ output is fairly trivial with a division and remainder algorithm.  

\newpage\begin{figure}[!here]







>







9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
071     /* set the sign only if a != 0 */
072     if (mp_iszero(a) != 1) \{
073        a->sign = neg;
074     \}
075     return MP_OKAY;
076   \}
077   #endif
078   
\end{alltt}
\end{small}

\subsection{Generating Radix-$n$ Output}
Generating radix-$n$ output is fairly trivial with a division and remainder algorithm.  

\newpage\begin{figure}[!here]
9595
9596
9597
9598
9599
9600
9601

9602
9603
9604
9605
9606
9607
9608
064     *str = '\symbol{92}0';
065   
066     mp_clear (&t);
067     return MP_OKAY;
068   \}
069   
070   #endif

\end{alltt}
\end{small}

\chapter{Number Theoretic Algorithms}
This chapter discusses several fundamental number theoretic algorithms such as the greatest common divisor, least common multiple and Jacobi 
symbol computation.  These algorithms arise as essential components in several key cryptographic algorithms such as the RSA public key algorithm and
various Sieve based factoring algorithms.







>







9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
064     *str = '\symbol{92}0';
065   
066     mp_clear (&t);
067     return MP_OKAY;
068   \}
069   
070   #endif
071   
\end{alltt}
\end{small}

\chapter{Number Theoretic Algorithms}
This chapter discusses several fundamental number theoretic algorithms such as the greatest common divisor, least common multiple and Jacobi 
symbol computation.  These algorithms arise as essential components in several key cryptographic algorithms such as the RSA public key algorithm and
various Sieve based factoring algorithms.
9875
9876
9877
9878
9879
9880
9881

9882
9883
9884
9885
9886
9887
9888
102     c->sign = MP_ZPOS;
103     res = MP_OKAY;
104   LBL_V:mp_clear (&u);
105   LBL_U:mp_clear (&v);
106     return res;
107   \}
108   #endif

\end{alltt}
\end{small}

This function makes use of the macros mp\_iszero and mp\_iseven.  The former evaluates to $1$ if the input mp\_int is equivalent to the 
integer zero otherwise it evaluates to $0$.  The latter evaluates to $1$ if the input mp\_int represents a non-zero even integer otherwise
it evaluates to $0$.  Note that just because mp\_iseven may evaluate to $0$ does not mean the input is odd, it could also be zero.  The three 
trivial cases of inputs are handled on lines 24 through 37.  After those lines the inputs are assumed to be non-zero.







>







9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
102     c->sign = MP_ZPOS;
103     res = MP_OKAY;
104   LBL_V:mp_clear (&u);
105   LBL_U:mp_clear (&v);
106     return res;
107   \}
108   #endif
109   
\end{alltt}
\end{small}

This function makes use of the macros mp\_iszero and mp\_iseven.  The former evaluates to $1$ if the input mp\_int is equivalent to the 
integer zero otherwise it evaluates to $0$.  The latter evaluates to $1$ if the input mp\_int represents a non-zero even integer otherwise
it evaluates to $0$.  Note that just because mp\_iseven may evaluate to $0$ does not mean the input is odd, it could also be zero.  The three 
trivial cases of inputs are handled on lines 24 through 37.  After those lines the inputs are assumed to be non-zero.
9970
9971
9972
9973
9974
9975
9976

9977
9978
9979
9980
9981
9982
9983
049     c->sign = MP_ZPOS;
050   
051   LBL_T:
052     mp_clear_multi (&t1, &t2, NULL);
053     return res;
054   \}
055   #endif

\end{alltt}
\end{small}

\section{Jacobi Symbol Computation}
To explain the Jacobi Symbol we shall first discuss the Legendre function\footnote{Arrg.  What is the name of this?} off which the Jacobi symbol is 
defined.  The Legendre function computes whether or not an integer $a$ is a quadratic residue modulo an odd prime $p$.  Numerically it is
equivalent to equation \ref{eqn:legendre}.







>







10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
049     c->sign = MP_ZPOS;
050   
051   LBL_T:
052     mp_clear_multi (&t1, &t2, NULL);
053     return res;
054   \}
055   #endif
056   
\end{alltt}
\end{small}

\section{Jacobi Symbol Computation}
To explain the Jacobi Symbol we shall first discuss the Legendre function\footnote{Arrg.  What is the name of this?} off which the Jacobi symbol is 
defined.  The Legendre function computes whether or not an integer $a$ is a quadratic residue modulo an odd prime $p$.  Numerically it is
equivalent to equation \ref{eqn:legendre}.
10214
10215
10216
10217
10218
10219
10220

10221
10222
10223
10224
10225
10226
10227
094     /* done */
095     res = MP_OKAY;
096   LBL_P1:mp_clear (&p1);
097   LBL_A1:mp_clear (&a1);
098     return res;
099   \}
100   #endif

\end{alltt}
\end{small}

As a matter of practicality the variable $a'$ as per the pseudo-code is reprensented by the variable $a1$ since the $'$ symbol is not valid for a C 
variable name character. 

The two simple cases of $a = 0$ and $a = 1$ are handled at the very beginning to simplify the algorithm.  If the input is non-trivial the algorithm







>







10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
094     /* done */
095     res = MP_OKAY;
096   LBL_P1:mp_clear (&p1);
097   LBL_A1:mp_clear (&a1);
098     return res;
099   \}
100   #endif
101   
\end{alltt}
\end{small}

As a matter of practicality the variable $a'$ as per the pseudo-code is reprensented by the variable $a1$ since the $'$ symbol is not valid for a C 
variable name character. 

The two simple cases of $a = 0$ and $a = 1$ are handled at the very beginning to simplify the algorithm.  If the input is non-trivial the algorithm
10362
10363
10364
10365
10366
10367
10368

10369
10370
10371
10372
10373
10374
10375
032   #ifdef BN_MP_INVMOD_SLOW_C
033     return mp_invmod_slow(a, b, c);
034   #endif
035   
036     return MP_VAL;
037   \}
038   #endif

\end{alltt}
\end{small}

\subsubsection{Odd Moduli}

When the modulus $b$ is odd the variables $A$ and $C$ are fixed and are not required to compute the inverse.  In particular by attempting to solve
the Diophantine $Cb + Da = 1$ only $B$ and $D$ are required to find the inverse of $a$.  







>







10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
032   #ifdef BN_MP_INVMOD_SLOW_C
033     return mp_invmod_slow(a, b, c);
034   #endif
035   
036     return MP_VAL;
037   \}
038   #endif
039   
\end{alltt}
\end{small}

\subsubsection{Odd Moduli}

When the modulus $b$ is odd the variables $A$ and $C$ are fixed and are not required to compute the inverse.  In particular by attempting to solve
the Diophantine $Cb + Da = 1$ only $B$ and $D$ are required to find the inverse of $a$.  
10463
10464
10465
10466
10467
10468
10469

10470
10471
10472
10473
10474
10475
10476
039         return MP_OKAY;
040       \}
041     \}
042   
043     return MP_OKAY;
044   \}
045   #endif

\end{alltt}
\end{small}

The algorithm defaults to a return of $0$ in case an error occurs.  The values in the prime table are all specified to be in the range of a 
mp\_digit.  The table \_\_prime\_tab is defined in the following file.

\vspace{+3mm}\begin{small}







>







10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
039         return MP_OKAY;
040       \}
041     \}
042   
043     return MP_OKAY;
044   \}
045   #endif
046   
\end{alltt}
\end{small}

The algorithm defaults to a return of $0$ in case an error occurs.  The values in the prime table are all specified to be in the range of a 
mp\_digit.  The table \_\_prime\_tab is defined in the following file.

\vspace{+3mm}\begin{small}
10514
10515
10516
10517
10518
10519
10520

10521
10522
10523
10524
10525
10526
10527
050     0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
051     0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
052     0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
053     0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
054   #endif
055   \};
056   #endif

\end{alltt}
\end{small}

Note that there are two possible tables.  When an mp\_digit is 7-bits long only the primes upto $127$ may be included, otherwise the primes
upto $1619$ are used.  Note that the value of \textbf{PRIME\_SIZE} is a constant dependent on the size of a mp\_digit. 

\subsection{The Fermat Test}







>







10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
050     0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
051     0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
052     0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
053     0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
054   #endif
055   \};
056   #endif
057   
\end{alltt}
\end{small}

Note that there are two possible tables.  When an mp\_digit is 7-bits long only the primes upto $127$ may be included, otherwise the primes
upto $1619$ are used.  Note that the value of \textbf{PRIME\_SIZE} is a constant dependent on the size of a mp\_digit. 

\subsection{The Fermat Test}
10602
10603
10604
10605
10606
10607
10608

10609
10610
10611
10612
10613
10614
10615
051     \}
052   
053     err = MP_OKAY;
054   LBL_T:mp_clear (&t);
055     return err;
056   \}
057   #endif

\end{alltt}
\end{small}

\subsection{The Miller-Rabin Test}
The Miller-Rabin (citation) test is another primality test which has tighter error bounds than the Fermat test specifically with sequentially chosen 
candidate  integers.  The algorithm is based on the observation that if $n - 1 = 2^kr$ and if $b^r \nequiv \pm 1$ then after upto $k - 1$ squarings the 
value must be equal to $-1$.  The squarings are stopped as soon as $-1$ is observed.  If the value of $1$ is observed first it means that







>







10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
051     \}
052   
053     err = MP_OKAY;
054   LBL_T:mp_clear (&t);
055     return err;
056   \}
057   #endif
058   
\end{alltt}
\end{small}

\subsection{The Miller-Rabin Test}
The Miller-Rabin (citation) test is another primality test which has tighter error bounds than the Fermat test specifically with sequentially chosen 
candidate  integers.  The algorithm is based on the observation that if $n - 1 = 2^kr$ and if $b^r \nequiv \pm 1$ then after upto $k - 1$ squarings the 
value must be equal to $-1$.  The squarings are stopped as soon as $-1$ is observed.  If the value of $1$ is observed first it means that
10737
10738
10739
10740
10741
10742
10743

10744
10745
10746
10747
10748
10749
10750
092     *result = MP_YES;
093   LBL_Y:mp_clear (&y);
094   LBL_R:mp_clear (&r);
095   LBL_N1:mp_clear (&n1);
096     return err;
097   \}
098   #endif

\end{alltt}
\end{small}




\backmatter







>







10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
092     *result = MP_YES;
093   LBL_Y:mp_clear (&y);
094   LBL_R:mp_clear (&r);
095   LBL_N1:mp_clear (&n1);
096     return err;
097   \}
098   #endif
099   
\end{alltt}
\end{small}




\backmatter
Changes to libtommath/tommath_class.h.
683
684
685
686
687
688
689

690
691
692
693
694
695
696
   #define BN_MP_ADD_D_C
   #define BN_MP_LSHD_C
#endif

#if defined(BN_MP_READ_RADIX_C)
   #define BN_MP_ZERO_C
   #define BN_MP_S_RMAP_C

   #define BN_MP_MUL_D_C
   #define BN_MP_ADD_D_C
   #define BN_MP_ISZERO_C
#endif

#if defined(BN_MP_READ_SIGNED_BIN_C)
   #define BN_MP_READ_UNSIGNED_BIN_C







>







683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
   #define BN_MP_ADD_D_C
   #define BN_MP_LSHD_C
#endif

#if defined(BN_MP_READ_RADIX_C)
   #define BN_MP_ZERO_C
   #define BN_MP_S_RMAP_C
   #define BN_MP_RADIX_SMAP_C
   #define BN_MP_MUL_D_C
   #define BN_MP_ADD_D_C
   #define BN_MP_ISZERO_C
#endif

#if defined(BN_MP_READ_SIGNED_BIN_C)
   #define BN_MP_READ_UNSIGNED_BIN_C
988
989
990
991
992
993
994




#define LTM_LAST
#endif
#include <tommath_superclass.h>
#include <tommath_class.h>
#else
#define LTM_LAST
#endif











>
>
>
>
989
990
991
992
993
994
995
996
997
998
999
#define LTM_LAST
#endif
#include <tommath_superclass.h>
#include <tommath_class.h>
#else
#define LTM_LAST
#endif

/* $Source: /root/tcl/repos-to-convert/tcl/libtommath/tommath_class.h,v $ */
/* $Revision: 1.1.1.3 $ */
/* $Date: 2005/09/26 16:32:16 $ */