updates.
[silc.git] / lib / silcmath / silcmp.h
1 /****h* silcmath/silcmp.h
2  *
3  * NAME
4  *
5  * silcmp.h
6  *
7  * COPYRIGHT
8  *
9  * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
10  *
11  * Copyright (C) 1997 - 2000 Pekka Riikonen
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  ***/
24
25 #ifndef SILCMP_H
26 #define SILCMP_H
27
28 #include "gmp.h"
29
30 #if 1
31
32 /* SILC MP library definitions. We use GNU MP library as default
33    MP library. However, to make possible future changes easier (SILC 
34    might have its own MP library in the future) we implement our own 
35    MP API with simple macros. */ 
36
37 typedef MP_INT SilcInt;
38 #define silc_mp_abs(a, b) mpz_abs((a), (b))
39 #define silc_mp_add(a, b, c) mpz_add((a), (b), (c))
40 #define silc_mp_add_ui(a, b, c) mpz_add_ui((a), (b), (c))
41 #define silc_mp_and(a, b, c) mpz_and((a), (b), (c))
42 #define silc_mp_cdiv_q(a, b, c) mpz_cdiv_q((a), (b), (c))
43 #define silc_mp_cdiv_q_ui(a, b, c) mpz_cdiv_q_ui((a), (b), (c))
44 #define silc_mp_cdiv_r(a, b, c) mpz_cdiv_r((a), (b), (c))
45 #define silc_mp_cdiv_r_ui(a, b, c) mpz_cdiv_r_ui((a), (b), (c))
46 #define silc_mp_cdiv_ui(a, b) mpz_cdiv_ui((a), (b))
47 #define silc_mp_clear(a) mpz_clear((a))
48 #define silc_mp_clrbit(a, b) mpz_clrbit((a), (b))
49 #define silc_mp_cmp(a, b) mpz_cmp((a), (b))
50 #define silc_mp_cmp_si(a, b) mpz_cmp_si((a), (b))
51 #define silc_mp_cmp_ui(a, b) mpz_cmp_ui((a), (b))
52 #define silc_mp_com(a, b) mpz_com((a), (b))
53 #define silc_mp_divexact(a, b, c) mpz_divexact((a), (b), (c))
54 #define silc_mp_div(a, b, c) mpz_div((a), (b), (c))
55 #define silc_mp_div_ui(a, b, c) mpz_div_ui((a), (b), (c))
56 #define silc_mp_fdiv_ui(a, b) mpz_fdiv_ui((a), (b))
57 #define silc_mp_fdiv_q(a, b, c) mpz_fdiv_q((a), (b), (c))
58 #define silc_mp_fdiv_q_2exp(a, b, c) mpz_fdiv_q_2exp((a), (b), (c))
59 #define silc_mp_fdiv_q_ui(a, b, c) mpz_fdiv_q_ui((a), (b), (c))
60 #define silc_mp_fdiv_qr(a, b, c, d) mpz_fdiv_qr((a), (b), (c), (d))
61 #define silc_mp_fdiv_qr_ui(a, b, c, d) mpz_fdiv_qr_ui((a), (b), (c), (d))
62 #define silc_mp_fdiv_r(a, b, c) mpz_fdiv_r((a), (b), (c))
63 #define silc_mp_fdiv_r_2exp(a, b, c) mpz_fdiv_r_2exp((a), (b), (c))
64 #define silc_mp_fdiv_r_ui(a, b, c) mpz_fdiv_r_ui((a), (b), (c))
65 #define silc_mp_fdiv_ui(a, b) mpz_fdiv_ui((a), (b))
66 #define silc_mp_gcd(a, b, c) mpz_gcd((a), (b), (c))
67 #define silc_mp_gcd_ui(a, b, c) mpz_gcd_ui((a), (b), (c))
68 #define silc_mp_gcdext(a, b, c, d, e) mpz_gcdext((a), (b), (c), (d), (e))
69 #define silc_mp_get_ui(a) mpz_get_ui((a))
70 #define silc_mp_init(a) mpz_init((a))
71 #define silc_mp_init_set(a, b) mpz_init_set((a), (b))
72 #define silc_mp_init_set_d(a, b) mpz_init_set_d((a), (b))
73 #define silc_mp_init_set_si(a, b) mpz_init_set_si((a), (b))
74 #define silc_mp_init_set_str(a, b, c) mpz_init_set_str((a), (b), (c))
75 #define silc_mp_init_set_ui(a, b) mpz_init_set_ui((a), (b))
76 #define silc_mp_invert(a, b, c) mpz_invert((a), (b), (c))
77 #define silc_mp_ior(a, b, c) mpz_ior((a), (b), (c))
78 #define silc_mp_mod(a, b, c) mpz_mod((a), (b), (c))
79 #define silc_mp_mod_2exp(a, b, c) mpz_mod_2exp((a), (b), (c))
80 #define silc_mp_mod_ui(a, b, c) mpz_mod_ui((a), (b), (c))
81 #define silc_mp_mul(a, b, c) mpz_mul((a), (b), (c))
82 #define silc_mp_mul_2exp(a, b, c) mpz_mul_2exp((a), (b), (c))
83 #define silc_mp_mul_ui(a, b, c) mpz_mul_ui((a), (b), (c))
84 #define silc_mp_neg(a, b) mpz_neg((a), (b))
85 #define silc_mp_pow_ui(a, b, c) mpz_pow_ui((a), (b), (c))
86 #define silc_mp_powm(a, b, c, d) mpz_powm((a), (b), (c), (d))
87 #define silc_mp_powm_ui(a, b, c, d) mpz_powm_ui((a), (b), (c), (d))
88 #define silc_mp_probab_prime_p(a, b) mpz_probab_prime_p((a), (b))
89 #define silc_mp_set(a, b) mpz_set((a), (b))
90 #define silc_mp_set_d(a, b) mpz_set_d((a), (b))
91 #define silc_mp_set_f(a, b) mpz_set_f((a), (b))
92 #define silc_mp_set_q(a, b) mpz_set_q((a), (b))
93 #define silc_mp_set_si(a, b) mpz_set_si((a), (b))
94 #define silc_mp_set_str(a, b, c) mpz_set_str((a), (b), (c))
95 #define silc_mp_set_ui(a, b) mpz_set_ui((a), (b))
96 #define silc_mp_setbit(a, b) mpz_setbit((a), (b))
97 #define silc_mp_size(a) mpz_size((a))
98 #define silc_mp_sizeinbase(a, b) mpz_sizeinbase((a), (b))
99 #define silc_mp_sqrt(a, b) mpz_sqrt((a), (b))
100 #define silc_mp_sqrtrem(a, b, c) mpz_sqrtrem((a), (b), (c))
101 #define silc_mp_sub(a, b, c) mpz_sub((a), (b), (c))
102 #define silc_mp_sub_ui(a, b, c) mpz_sub_ui((a), (b), (c))
103 #define silc_mp_tdiv_ui(a, b) mpz_tdiv_ui((a), (b))
104 #define silc_mp_tdiv_q(a, b, c) mpz_tdiv_q((a), (b), (c))
105 #define silc_mp_tdiv_q_2exp(a, b, c) mpz_tdiv_q_2exp((a), (b), (c))
106 #define silc_mp_tdiv_q_ui(a, b, c) mpz_tdiv_q_ui((a), (b), (c))
107 #define silc_mp_tdiv_qr(a, b, c, d) mpz_tdiv_qr((a), (b), (c), (d))
108 #define silc_mp_tdiv_qr_ui(a, b, c, d) mpz_tdiv_qr_ui((a), (b), (c), (d))
109 #define silc_mp_tdiv_r(a, b, c) mpz_tdiv_r((a), (b), (c))
110 #define silc_mp_tdiv_r_2exp(a, b, c) mpz_tdiv_r_2exp((a), (b), (c))
111 #define silc_mp_tdiv_r_ui(a, b, c) mpz_tdiv_r_ui((a), (b), (c))
112 #define silc_mp_tdiv_ui(a, b) mpz_tdiv_ui((a), (b))
113 #define silc_mp_ui_pow_ui(a, b, c) mpz_ui_pow_ui((a), (b), (c))
114 #define silc_mp_get_str(a, b, c) mpz_get_str((a), (b), (c))
115 #define silc_mp_out_str(a, b, c) mpz_out_str((a), (b), (c))
116
117 #else
118
119 /* SILC MP Library Interface. This interface defines the arbitrary
120    precision arithmetic routines for SILC. Currently the actual routines
121    are implemented separately, usually by some other MP library. The
122    interface is generic but is mainly intended for crypto usage. This
123    interface is used by SILC routines that needs big numbers, such as
124    RSA implementation, Diffie-Hellman implementation etc. */
125
126 /* XXX Move this to implementation specific files */
127 #define SILC_MP_INT MP_INT
128
129 /****d* silcmath/SilcMPAPI/SilcMPInt
130  *
131  * NAME
132  *
133  *    typedef SILC_MP_INT SilcMPInt;
134  *
135  * DESCRIPTION
136  *
137  *    The SILC MP Integer definition. This is the actual MP integer.
138  *    The type is defined as SILC_MP_INT as it is implementation specific
139  *    and is unknown to the application.
140  *
141  * SOURCE
142  */
143 typedef SILC_MP_INT SilcMPInt;
144 /***/
145
146 /****f* silcmath/SilcMPAPI/silc_mp_alloc
147  *
148  * SYNOPSIS
149  *
150  *    void silc_mp_init(SilcMPInt mp);
151  *
152  * DESCRIPTION
153  *
154  *    Initializes the SilcMPInt *that is the actual MP Integer.
155  *    This must be called before any of the silc_mp_ routines can be
156  *    used. The integer is uninitialized with the silc_mp_uninit function.
157  *
158  ***/
159 void silc_mp_init(SilcMPInt *mp);
160
161 /****f* silcmath/SilcMPAPI/silc_mp_free
162  *
163  * SYNOPSIS
164  *
165  *    void silc_mp_uninit(SilcMPInt *mp);
166  *
167  * DESCRIPTION
168  *
169  *    Uninitializes the MP Integer.
170  *
171  ***/
172 void silc_mp_uninit(SilcMPInt *mp);
173
174 /****f* silcmath/SilcMPAPI/silc_mp_abs
175  *
176  * SYNOPSIS
177  *
178  *    void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
179  *
180  * DESCRIPTION
181  *
182  *    Assign the absolute value of `src' to `dst'.
183  *
184  ***/
185 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
186
187 /****f* silcmath/SilcMPAPI/silc_mp_add
188  *
189  * SYNOPSIS
190  *
191  *    void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
192  *
193  * DESCRIPTION
194  *
195  *    Add two integers `mp1' and `mp2' and save the result to `dst'.
196  *
197  ***/
198 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
199
200 /****f* silcmath/SilcMPAPI/silc_mp_add_ui
201  *
202  * SYNOPSIS
203  *
204  *    void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
205  *
206  * DESCRIPTION
207  *
208  *    Add two integers `mp1' and unsigned word `ui' and save the result
209  *    to `dst'.
210  *
211  ***/
212 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
213
214 /****f* silcmath/SilcMPAPI/silc_mp_and
215  *
216  * SYNOPSIS
217  *
218  *    void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
219  *
220  * DESCRIPTION
221  *
222  *    Logical and operator. The result is saved to `dst'.
223  *
224  ***/
225 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
226
227 /****f* silcmath/SilcMPAPI/silc_mp_cmp
228  *
229  * SYNOPSIS
230  *
231  *    int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
232  *
233  * DESCRIPTION
234  *
235  *    Compare `mp1' and `mp2'. Returns posivite, zero, or negative
236  *    if `mp1' > `mp2', `mp1' == `mp2', or `mp1' < `mp2', respectively.
237  *
238  ***/
239 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
240
241 /****f* silcmath/SilcMPAPI/silc_mp_cmp_si
242  *
243  * SYNOPSIS
244  *
245  *    int silc_mp_cmp_si(SilcMPInt *mp1, int32 si);
246  *
247  * DESCRIPTION
248  *
249  *    Compare `mp1' and single word `si'. Returns posivite, zero, or negative
250  *    if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
251  *
252  ***/
253 int silc_mp_cmp_si(SilcMPInt *mp1, int32 si);
254
255 /****f* silcmath/SilcMPAPI/silc_mp_cmp_ui
256  *
257  * SYNOPSIS
258  *
259  *    int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui);
260  *
261  * DESCRIPTION
262  *
263  *    Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or 
264  *    negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui', 
265  *    respectively.
266  *
267  ***/
268 int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui);
269
270 /****f* silcmath/SilcMPAPI/silc_mp_div
271  *
272  * SYNOPSIS
273  *
274  *    void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
275  *
276  * DESCRIPTION
277  *
278  *    Divide the `mp1' and `mp2' and save the result to the `dst'. This
279  *    is equivalent to dst = mp1 / mp2;
280  *
281  ***/
282 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
283
284 /****f* silcmath/SilcMPAPI/silc_mp_div_ui
285  *
286  * SYNOPSIS
287  *
288  *    void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
289  *
290  * DESCRIPTION
291  *
292  *    Divide the `mp1' and unsigned word `ui' and save the result to the
293  *    `dst'. This is equivalent to dst = mp1 / ui;
294  *
295  ***/
296 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
297
298 /****f* silcmath/SilcMPAPI/silc_mp_div_qr
299  *
300  * SYNOPSIS
301  *
302  *    void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
303  *                        SilcMPInt *mp2);
304  *
305  * DESCRIPTION
306  *
307  *    Divide the `mp1' and `mp2' and save the quotient to the `q' and
308  *    the remainder to the `r'.  This is equivalent to the q = mp1 / mp2, 
309  *    r = mp1 mod mp2 (or mp1 = mp2 * q + r). If the `q' or `r' is NULL
310  *    then the operation is omitted.
311  *
312  ***/
313 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, SilcMPInt *mp2);
314
315 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp
316  *
317  * SYNOPSIS
318  *
319  *    void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
320  *
321  * DESCRIPTION
322  *
323  *    Divide the `mp1' with 2 ** `exp' and save the result to `dst'.
324  *    This is equivalent to dst = mp1 / (2 ^ exp).
325  *
326  ***/
327 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
328
329 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp
330  *
331  * SYNOPSIS
332  *
333  *    void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
334  *
335  * DESCRIPTION
336  *
337  *    Divide the `mp1' with 2 ** `exp' and save the quotient to `q' and
338  *    the remainder to `r'. This is equivalent to q = mp1 / (2 ^ exp),
339  *    r = mp1 mod (2 ^ exp). If the `q' or `r' is NULL then the operation
340  *    is omitted.
341  *
342  ***/
343 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, uint32 exp);
344
345 /****f* silcmath/SilcMPAPI/silc_mp_gcd
346  *
347  * SYNOPSIS
348  *
349  *    void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
350  *
351  * DESCRIPTION
352  *
353  *    Calculate the greatest common divisor of the integers `mp1' and `mp2'
354  *    and save the result to `dst'.
355  *
356  ***/
357 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
358
359 /****f* silcmath/SilcMPAPI/silc_mp_gcdext
360  *
361  * SYNOPSIS
362  *
363  *    void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
364  *                        SilcMPInt *mp2);
365  *
366  * DESCRIPTION
367  *
368  *    Calculate the extended greatest common divisor `g', `s' and `t' such
369  *    that g = mp1 * s + mp2 * + t.
370  *
371  ***/
372 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
373                     SilcMPInt *mp2);
374
375 /****f* silcmath/SilcMPAPI/silc_mp_get_ui
376  *
377  * SYNOPSIS
378  *
379  *    uint32 silc_mp_get_ui(SilcMPInt *mp);
380  *
381  * DESCRIPTION
382  *
383  *    Returns the least significant unsigned word from `mp'.
384  *
385  ***/
386 uint32 silc_mp_get_ui(SilcMPInt *mp);
387
388 /****f* silcmath/SilcMPAPI/silc_mp_get_str
389  *
390  * SYNOPSIS
391  *
392  *    void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
393  *
394  * DESCRIPTION
395  *
396  *    Converts integer `mp' into a string of base `base'. The `str'
397  *    must already have space allocated. The function returns the same
398  *    as `str' or NULL on error.
399  *
400  ***/
401 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
402
403 /****f* silcmath/SilcMPAPI/silc_mp_or
404  *
405  * SYNOPSIS
406  *
407  *    void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
408  *
409  * DESCRIPTION
410  *
411  *    Logical inclusive OR operator. The result is saved to `dst'.
412  *
413  ***/
414 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
415
416 /****f* silcmath/SilcMPAPI/silc_mp_mod
417  *
418  * SYNOPSIS
419  *
420  *    void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
421  *
422  * DESCRIPTION
423  *
424  *    Mathematical MOD function. Produces the remainder of `mp1' and `mp2'
425  *    and saves the result to `dst'. This is equivalent to dst = mp1 mod mp2.
426  *    The same result can also be get with silc_mp_div_qr as that function
427  *    returns the remainder as well.
428  *
429  ***/
430 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
431
432 /****f* silcmath/SilcMPAPI/silc_mp_mod_ui
433  *
434  * SYNOPSIS
435  *
436  *    void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
437  *
438  * DESCRIPTION
439  *
440  *    Mathematical MOD function. Produces the remainder of `mp1' and 
441  *    unsigned word `ui' and saves the result to `dst'. This is equivalent
442  *    to dst = mp1 mod ui.
443  *
444  ***/
445 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
446
447 /****f* silcmath/SilcMPAPI/silc_mp_mod_2exp
448  *
449  * SYNOPSIS
450  *
451  *    void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
452  *
453  * DESCRIPTION
454  *
455  *    Computes the remainder of `mp1' with 2 ** `exp' and saves the
456  *    result to `dst'. This is equivalent to dst = mp1 mod (2 ^ exp).
457  *    The same result can also be get with silc_mp_div_2exp_qr as that
458  *    function returns the remainder as well.
459  *
460  ***/
461 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
462
463 /****f* silcmath/SilcMPAPI/silc_mp_mul
464  *
465  * SYNOPSIS
466  *
467  *    void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
468  *
469  * DESCRIPTION
470  *
471  *    Multiply two integers `mp1' and `mp2' and save the result to `dst'.
472  *
473  ***/
474 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
475
476 /****f* silcmath/SilcMPAPI/silc_mp_mul_ui
477  *
478  * SYNOPSIS
479  *
480  *    void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
481  *
482  * DESCRIPTION
483  *
484  *    Multiply integer `mp1' and unsigned word `ui' and save the result
485  *    to `dst'.
486  *
487  ***/
488 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
489
490 /****f* silcmath/SilcMPAPI/silc_mp_mul_2exp
491  *
492  * SYNOPSIS
493  *
494  *    void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
495  *
496  * DESCRIPTION
497  *
498  *    Multiply integers `mp1' with 2 ** `exp' and save the result to 
499  *    `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
500  *
501  ***/
502 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
503
504 /****f* silcmath/SilcMPAPI/silc_mp_neg
505  *
506  * SYNOPSIS
507  *
508  *    void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
509  *
510  * DESCRIPTION
511  *
512  *    Negate `src' and save the result to `dst'.
513  *
514  ***/
515 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
516
517 /****f* silcmath/SilcMPAPI/silc_mp_pow
518  *
519  * SYNOPSIS
520  *
521  *    void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
522  *
523  * DESCRIPTION
524  *
525  *    Compute `mp1' ** `exp' and save the result to `dst'. This is
526  *    equivalent to dst = mp1 ^ exp.
527  *
528  ***/
529 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
530
531 /****f* silcmath/SilcMPAPI/silc_mp_pow_ui
532  *
533  * SYNOPSIS
534  *
535  *    void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
536  *
537  * DESCRIPTION
538  *
539  *    Compute `mp1' ** `exp' and save the result to `dst'. This is
540  *    equivalent to dst = mp1 ^ exp.
541  *
542  ***/
543 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
544
545 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod
546  *
547  * SYNOPSIS
548  *
549  *    void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp, 
550  *                         SilcMPInt *mod);
551  *
552  * DESCRIPTION
553  *
554  *    Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
555  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
556  *
557  ***/
558 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp, 
559                      SilcMPInt *mod);
560
561 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod_ui
562  *
563  * SYNOPSIS
564  *
565  *    void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp, 
566  *                            SilcMPInt *mod);
567  *
568  * DESCRIPTION
569  *
570  *    Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
571  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
572  *
573  ***/
574 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp, 
575                         SilcMPInt *mod);
576
577 /****f* silcmath/SilcMPAPI/silc_mp_modinv
578  *
579  * SYNOPSIS
580  *
581  *    void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
582  *
583  * DESCRIPTION
584  *
585  *    Find multiplicative inverse using Euclid's extended algorithm. 
586  *    Computes inverse such that a * inv mod n = 1, where 0 < a < n. 
587  *    Algorithm goes like this:
588  *  
589  *    g(0) = n    v(0) = 0
590  *    g(1) = a    v(1) = 1
591  * 
592  *    y = g(i-1) / g(i)
593  *    g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
594  *    v(i+1) = v(i-1) - y * v(i)
595  * 
596  *    do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n, 
597  *    is added to inverse making it positive again. (Sometimes the algorithm 
598  *    has a variable u defined too and it behaves just like v, except that 
599  *    initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is 
600  *    not needed by the algorithm so it does not have to be included.)
601  *
602  ***/
603 void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
604
605 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin
606  *
607  * SYNOPSIS
608  *
609  *    unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len, 
610  *                                  uint32 *ret_len);
611  *
612  * DESCRIPTION
613  *
614  *    Encodes MP integer into binary data. Returns allocated data that
615  *    must be free'd by the caller. If `len' is provided the destination
616  *    buffer is allocated that large. If zero then the size is approximated.
617  *
618  ***/
619 unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len, uint32 *ret_len);
620
621 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin_noalloc
622  *
623  * SYNOPSIS
624  *
625  *    void silc_mp_mp2bin_noalloc(SilcMPInt val, unsigned char *dst,
626  *                                uint32 dst_len);
627  *
628  * DESCRIPTION
629  *
630  *    Same as silc_mp_mp2bin but does not allocate any memory.  The
631  *    encoded data is returned into `dst' and it's length to the `ret_len'.
632  *
633  ***/
634 void silc_mp_mp2bin_noalloc(SilcMPInt val, unsigned char *dst,
635                             uint32 dst_len);
636
637 /****f* silcmath/SilcMPAPI/silc_mp_bin2mp
638  *
639  * SYNOPSIS
640  *
641  *    void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcMPInt *ret);
642  *
643  * DESCRIPTION
644  *
645  *    Decodes binary data into MP integer. The integer sent as argument
646  *    must be initialized.
647  *
648  ***/
649 void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcMPInt *ret);
650
651 /****f* silcmath/SilcMPAPI/silc_mp_set
652  *
653  * SYNOPSIS
654  *
655  *    void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
656  *
657  * DESCRIPTION
658  *
659  *    Set `dst' integer from `src' integer.
660  *
661  ***/
662 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
663
664 void silc_mp_set_ui(SilcMPInt *dst, uint32 ui);
665 void silc_mp_set_ui64(SilcMPInt *dst, uint64 ui);
666 void silc_mp_set_si(SilcMPInt *dst, uint32 ui);
667 void silc_mp_set_si64(SilcMPInt *dst, uint64 ui);
668 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
669 size_t silc_mp_size(SilcMPInt *mp);
670 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
671 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
672 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
673 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
674
675 #endif /* 1 */
676
677 #endif