Added SILC Thread Queue API
[silc.git] / lib / silcmath / silcmp.h
1 /*
2
3   silcmp.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcmath/SILC MP Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC MP Library Interface. This interface defines the arbitrary
25  * precision arithmetic routines for SILC. The interface is generic but
26  * is mainly intended for crypto usage. This interface is used by SILC
27  * routines that needs big numbers, such as RSA implementation,
28  * Diffie-Hellman implementation etc.
29  *
30  ***/
31
32 #ifndef SILCMP_H
33 #define SILCMP_H
34
35 #if defined(SILC_MP_GMP)
36 #include "mp_gmp.h"             /* SILC_MP_GMP */
37 #else
38 #ifdef SILC_DIST_TMA
39 #include "mp_tma.h"
40 #endif /* SILC_DIST_TMA */
41 #ifdef SILC_DIST_TFM
42 #include "mp_tfm.h"
43 #endif /* SILC_DIST_TFM */
44 #endif
45
46 /****d* silcmath/SilcMPAPI/SilcMPInt
47  *
48  * NAME
49  *
50  *    typedef SILC_MP_INT SilcMPInt;
51  *
52  * DESCRIPTION
53  *
54  *    The SILC MP Integer definition. This is the actual MP integer.
55  *    The type is defined as SILC_MP_INT as it is implementation specific
56  *    and is unknown to the application.
57  *
58  * SOURCE
59  */
60 typedef SILC_MP_INT SilcMPInt;
61 /***/
62
63 /****f* silcmath/SilcMPAPI/silc_mp_init
64  *
65  * SYNOPSIS
66  *
67  *    void silc_mp_init(SilcMPInt mp);
68  *
69  * DESCRIPTION
70  *
71  *    Initializes the SilcMPInt that is the actual MP Integer.
72  *    This must be called before any of the silc_mp_ routines can be
73  *    used. The integer is uninitialized with the silc_mp_uninit function.
74  *
75  ***/
76 void silc_mp_init(SilcMPInt *mp);
77
78 /****f* silcmath/SilcMPAPI/silc_mp_sinit
79  *
80  * SYNOPSIS
81  *
82  *    SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
83  *
84  * DESCRIPTION
85  *
86  *    Initializes the SilcMPInt that is the actual MP Integer.
87  *    This must be called before any of the silc_mp_ routines can be
88  *    used. The integer is uninitialized with the silc_mp_suninit function.
89  *    If `stack' is non-NULL it will be used as the memory source.  If it
90  *    is NULL, this call is equivalent to silc_mp_init.
91  *
92  ***/
93 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
94
95 /****f* silcmath/SilcMPAPI/silc_mp_uninit
96  *
97  * SYNOPSIS
98  *
99  *    void silc_mp_uninit(SilcMPInt *mp);
100  *
101  * DESCRIPTION
102  *
103  *    Uninitializes the MP Integer.
104  *
105  ***/
106 void silc_mp_uninit(SilcMPInt *mp);
107
108 /****f* silcmath/SilcMPAPI/silc_mp_suninit
109  *
110  * SYNOPSIS
111  *
112  *    void silc_mp_suninit(SilcStack stack, SilcMPInt *mp);
113  *
114  * DESCRIPTION
115  *
116  *    Uninitializes the MP Integer.
117  *
118  ***/
119 void silc_mp_suninit(SilcStack stack, SilcMPInt *mp);
120
121 /****f* silcmath/SilcMPAPI/silc_mp_size
122  *
123  * SYNOPSIS
124  *
125  *    size_t silc_mp_size(SilcMPInt *mp);
126  *
127  * DESCRIPTION
128  *
129  *    Return the precision size of the integer `mp'.
130  *
131  ***/
132 size_t silc_mp_size(SilcMPInt *mp);
133
134 /****f* silcmath/SilcMPAPI/silc_mp_sizeinbase
135  *
136  * SYNOPSIS
137  *
138  *    size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
139  *
140  * DESCRIPTION
141  *
142  *    Return the size of the integer in base `base'.
143  *
144  * NOTES
145  *
146  *    For any other base but 2 this function usually returns only an
147  *    approximated size in the base.  It is however guaranteed that the
148  *    the returned size is always at least the size of the integer or
149  *    larger.
150  *
151  *    For base 2 this returns the exact bit-size of the integer.
152  *
153  ***/
154 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
155
156 /****f* silcmath/SilcMPAPI/silc_mp_set
157  *
158  * SYNOPSIS
159  *
160  *    void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
161  *
162  * DESCRIPTION
163  *
164  *    Set `dst' integer from `src' integer. The `dst' must already be
165  *    initialized.
166  *
167  ***/
168 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
169
170 /****f* silcmath/SilcMPAPI/silc_mp_set_ui
171  *
172  * SYNOPSIS
173  *
174  *    void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
175  *
176  * DESCRIPTION
177  *
178  *    Set `dst' integer from unsigned word `ui'. The `dst' must already be
179  *    initialized.
180  *
181  ***/
182 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
183
184 /****f* silcmath/SilcMPAPI/silc_mp_set_si
185  *
186  * SYNOPSIS
187  *
188  *    void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
189  *
190  * DESCRIPTION
191  *
192  *    Set `dst' integer from single word `si'. The `dst' must
193  *    already be initialized.
194  *
195  ***/
196 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
197
198 /****f* silcmath/SilcMPAPI/silc_mp_set_str
199  *
200  * SYNOPSIS
201  *
202  *    void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
203  *
204  * DESCRIPTION
205  *
206  *    Set `dst' integer from string `str' of base `base'. The `dst' must
207  *    already be initialized.
208  *
209  * NOTES
210  *
211  *    For base 2 the string must be in ASCII bit presentation, not in
212  *    binary.  Use the silc_mp_bin2mp to decode binary into integer.
213  *
214  ***/
215 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
216
217 /****f* silcmath/SilcMPAPI/silc_mp_get_ui
218  *
219  * SYNOPSIS
220  *
221  *    SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
222  *
223  * DESCRIPTION
224  *
225  *    Returns the least significant unsigned word from `mp'.
226  *
227  ***/
228 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
229
230 /****f* silcmath/SilcMPAPI/silc_mp_get_str
231  *
232  * SYNOPSIS
233  *
234  *    void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
235  *
236  * DESCRIPTION
237  *
238  *    Converts integer `mp' into a string of base `base'. The `str'
239  *    must already have space allocated. The function returns the same
240  *    as `str' or NULL on error.
241  *
242  * NOTES
243  *
244  *    For base 2 the returned string is in ASCII bit presentation, not
245  *    in binary.  Use the silc_mp_mp2bin to encode integer into binary.
246  *
247  ***/
248 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
249
250 /****f* silcmath/SilcMPAPI/silc_mp_add
251  *
252  * SYNOPSIS
253  *
254  *    void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
255  *
256  * DESCRIPTION
257  *
258  *    Add two integers `mp1' and `mp2' and save the result to `dst'.
259  *
260  ***/
261 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
262
263 /****f* silcmath/SilcMPAPI/silc_mp_add_ui
264  *
265  * SYNOPSIS
266  *
267  *    void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
268  *
269  * DESCRIPTION
270  *
271  *    Add two integers `mp1' and unsigned word `ui' and save the result
272  *    to `dst'.
273  *
274  ***/
275 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
276
277 /****f* silcmath/SilcMPAPI/silc_mp_sub
278  *
279  * SYNOPSIS
280  *
281  *    void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
282  *
283  * DESCRIPTION
284  *
285  *    Subtract two integers `mp1' and `mp2' and save the result to `dst'.
286  *
287  ***/
288 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
289
290 /****f* silcmath/SilcMPAPI/silc_mp_sub_ui
291  *
292  * SYNOPSIS
293  *
294  *    void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
295  *
296  * DESCRIPTION
297  *
298  *    Subtract integers `mp1' and unsigned word `ui' and save the result
299  *    to `dst'.
300  *
301  ***/
302 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
303
304 /****f* silcmath/SilcMPAPI/silc_mp_mul
305  *
306  * SYNOPSIS
307  *
308  *    void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
309  *
310  * DESCRIPTION
311  *
312  *    Multiply two integers `mp1' and `mp2' and save the result to `dst'.
313  *
314  ***/
315 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
316
317 /****f* silcmath/SilcMPAPI/silc_mp_mul_ui
318  *
319  * SYNOPSIS
320  *
321  *    void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
322  *
323  * DESCRIPTION
324  *
325  *    Multiply integer `mp1' and unsigned word `ui' and save the result
326  *    to `dst'.
327  *
328  ***/
329 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
330
331 /****f* silcmath/SilcMPAPI/silc_mp_mul_2exp
332  *
333  * SYNOPSIS
334  *
335  *    void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
336  *
337  * DESCRIPTION
338  *
339  *    Multiply integers `mp1' with 2 ** `exp' and save the result to
340  *    `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
341  *
342  ***/
343 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
344
345 /****f* silcmath/SilcMPAPI/silc_mp_sqrt
346  *
347  * SYNOPSIS
348  *
349  *    void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
350  *
351  * DESCRIPTION
352  *
353  *    Compute square root of floor(sqrt(src)) and save the result to `dst'.
354  *
355  ***/
356 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
357
358 /****f* silcmath/SilcMPAPI/silc_mp_div
359  *
360  * SYNOPSIS
361  *
362  *    void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
363  *
364  * DESCRIPTION
365  *
366  *    Divide the `mp1' and `mp2' and save the result to the `dst'. This
367  *    is equivalent to dst = mp1 / mp2;
368  *
369  ***/
370 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
371
372 /****f* silcmath/SilcMPAPI/silc_mp_div_ui
373  *
374  * SYNOPSIS
375  *
376  *    void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
377  *
378  * DESCRIPTION
379  *
380  *    Divide the `mp1' and unsigned word `ui' and save the result to the
381  *    `dst'. This is equivalent to dst = mp1 / ui;
382  *
383  ***/
384 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
385
386 /****f* silcmath/SilcMPAPI/silc_mp_div_qr
387  *
388  * SYNOPSIS
389  *
390  *    void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
391  *                        SilcMPInt *mp2);
392  *
393  * DESCRIPTION
394  *
395  *    Divide the `mp1' and `mp2' and save the quotient to the `q' and
396  *    the remainder to the `r'.  This is equivalent to the q = mp1 / mp2,
397  *    r = mp1 mod mp2 (or mp1 = mp2 * q + r). If the `q' or `r' is NULL
398  *    then the operation is omitted.
399  *
400  ***/
401 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
402                     SilcMPInt *mp2);
403
404 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp
405  *
406  * SYNOPSIS
407  *
408  *    void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
409  *
410  * DESCRIPTION
411  *
412  *    Divide the `mp1' with 2 ** `exp' and save the result to `dst'.
413  *    This is equivalent to dst = mp1 / (2 ^ exp).
414  *
415  ***/
416 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
417
418 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp_qr
419  *
420  * SYNOPSIS
421  *
422  *    void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
423  *                             SilcUInt32 exp);
424  *
425  * DESCRIPTION
426  *
427  *    Divide the `mp1' with 2 ** `exp' and save the quotient to `q' and
428  *    the remainder to `r'. This is equivalent to q = mp1 / (2 ^ exp),
429  *    r = mp1 mod (2 ^ exp). If the `q' or `r' is NULL then the operation
430  *    is omitted.
431  *
432  ***/
433 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
434                          SilcUInt32 exp);
435
436 /****f* silcmath/SilcMPAPI/silc_mp_mod
437  *
438  * SYNOPSIS
439  *
440  *    void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
441  *
442  * DESCRIPTION
443  *
444  *    Mathematical MOD function. Produces the remainder of `mp1' and `mp2'
445  *    and saves the result to `dst'. This is equivalent to dst = mp1 mod mp2.
446  *    The same result can also be get with silc_mp_div_qr as that function
447  *    returns the remainder as well.
448  *
449  ***/
450 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
451
452 /****f* silcmath/SilcMPAPI/silc_mp_mod_ui
453  *
454  * SYNOPSIS
455  *
456  *    void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
457  *
458  * DESCRIPTION
459  *
460  *    Mathematical MOD function. Produces the remainder of `mp1' and
461  *    unsigned word `ui' and saves the result to `dst'. This is equivalent
462  *    to dst = mp1 mod ui.
463  *
464  ***/
465 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
466
467 /****f* silcmath/SilcMPAPI/silc_mp_mod_2exp
468  *
469  * SYNOPSIS
470  *
471  *    void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
472  *
473  * DESCRIPTION
474  *
475  *    Computes the remainder of `mp1' with 2 ** `exp' and saves the
476  *    result to `dst'. This is equivalent to dst = mp1 mod (2 ^ exp).
477  *    The same result can also be get with silc_mp_div_2exp_qr as that
478  *    function returns the remainder as well.
479  *
480  ***/
481 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
482
483 /****f* silcmath/SilcMPAPI/silc_mp_pow
484  *
485  * SYNOPSIS
486  *
487  *    void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
488  *
489  * DESCRIPTION
490  *
491  *    Compute `mp1' ** `exp' and save the result to `dst'. This is
492  *    equivalent to dst = mp1 ^ exp.
493  *
494  ***/
495 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
496
497 /****f* silcmath/SilcMPAPI/silc_mp_pow_ui
498  *
499  * SYNOPSIS
500  *
501  *    void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
502  *
503  * DESCRIPTION
504  *
505  *    Compute `mp1' ** `exp' and save the result to `dst'. This is
506  *    equivalent to dst = mp1 ^ exp.
507  *
508  ***/
509 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
510
511 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod
512  *
513  * SYNOPSIS
514  *
515  *    void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
516  *                         SilcMPInt *mod);
517  *
518  * DESCRIPTION
519  *
520  *    Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
521  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
522  *
523  ***/
524 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
525                      SilcMPInt *mod);
526
527 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod_ui
528  *
529  * SYNOPSIS
530  *
531  *    void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
532  *                            SilcMPInt *mod);
533  *
534  * DESCRIPTION
535  *
536  *    Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
537  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
538  *
539  ***/
540 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
541                         SilcMPInt *mod);
542
543 /****f* silcmath/SilcMPAPI/silc_mp_modinv
544  *
545  * SYNOPSIS
546  *
547  *    void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
548  *
549  * DESCRIPTION
550  *
551  *    Find multiplicative inverse using Euclid's extended algorithm.
552  *    Computes inverse such that a * inv mod n = 1, where 0 < a < n.
553  *    Algorithm goes like this:
554  *
555  *    g(0) = n    v(0) = 0
556  *    g(1) = a    v(1) = 1
557  *
558  *    y = g(i-1) / g(i)
559  *    g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
560  *    v(i+1) = v(i-1) - y * v(i)
561  *
562  *    do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n,
563  *    is added to inverse making it positive again. (Sometimes the algorithm
564  *    has a variable u defined too and it behaves just like v, except that
565  *    initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is
566  *    not needed by the algorithm so it does not have to be included.)
567  *
568  ***/
569 void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
570
571 /****f* silcmath/SilcMPAPI/silc_mp_gcd
572  *
573  * SYNOPSIS
574  *
575  *    void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
576  *
577  * DESCRIPTION
578  *
579  *    Calculate the greatest common divisor of the integers `mp1' and `mp2'
580  *    and save the result to `dst'.
581  *
582  ***/
583 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
584
585 /****f* silcmath/SilcMPAPI/silc_mp_gcdext
586  *
587  * SYNOPSIS
588  *
589  *    void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t,
590  *                        SilcMPInt *mp1, SilcMPInt *mp2);
591  *
592  * DESCRIPTION
593  *
594  *    Calculate the extended greatest common divisor `g', `s' and `t' such
595  *    that g = mp1 * s + mp2 * + t.
596  *
597  ***/
598 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
599                     SilcMPInt *mp2);
600
601 /****f* silcmath/SilcMPAPI/silc_mp_cmp
602  *
603  * SYNOPSIS
604  *
605  *    int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
606  *
607  * DESCRIPTION
608  *
609  *    Compare `mp1' and `mp2'. Returns posivite, zero, or negative
610  *    if `mp1' > `mp2', `mp1' == `mp2', or `mp1' < `mp2', respectively.
611  *
612  ***/
613 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
614
615 /****f* silcmath/SilcMPAPI/silc_mp_cmp_si
616  *
617  * SYNOPSIS
618  *
619  *    int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
620  *
621  * DESCRIPTION
622  *
623  *    Compare `mp1' and single word `si'. Returns posivite, zero, or negative
624  *    if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
625  *
626  ***/
627 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
628
629 /****f* silcmath/SilcMPAPI/silc_mp_cmp_ui
630  *
631  * SYNOPSIS
632  *
633  *    int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
634  *
635  * DESCRIPTION
636  *
637  *    Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or
638  *    negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui',
639  *    respectively.
640  *
641  ***/
642 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
643
644 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin
645  *
646  * SYNOPSIS
647  *
648  *    unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
649  *                                  SilcUInt32 *ret_len);
650  *
651  * DESCRIPTION
652  *
653  *    Encodes MP integer into binary data. Returns allocated data that
654  *    must be free'd by the caller. If `len' is provided the destination
655  *    buffer is allocated that large. If zero then the size is approximated.
656  *
657  ***/
658 unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
659                               SilcUInt32 *ret_len);
660
661 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin_noalloc
662  *
663  * SYNOPSIS
664  *
665  *    void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
666  *                                SilcUInt32 dst_len);
667  *
668  * DESCRIPTION
669  *
670  *    Same as silc_mp_mp2bin but does not allocate any memory.  The
671  *    encoded data is returned into `dst' of size of `dst_len'.
672  *
673  ***/
674 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
675                             SilcUInt32 dst_len);
676
677 /****f* silcmath/SilcMPAPI/silc_mp_bin2mp
678  *
679  * SYNOPSIS
680  *
681  *    void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len,
682  *                        SilcMPInt *ret);
683  *
684  * DESCRIPTION
685  *
686  *    Decodes binary data into MP integer. The integer sent as argument
687  *    must be initialized.
688  *
689  ***/
690 void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret);
691
692 /****f* silcmath/SilcMPAPI/silc_mp_abs
693  *
694  * SYNOPSIS
695  *
696  *    void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
697  *
698  * DESCRIPTION
699  *
700  *    Assign the absolute value of `src' to `dst'.
701  *
702  ***/
703 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
704
705 /****f* silcmath/SilcMPAPI/silc_mp_neg
706  *
707  * SYNOPSIS
708  *
709  *    void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
710  *
711  * DESCRIPTION
712  *
713  *    Negate `src' and save the result to `dst'.
714  *
715  ***/
716 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
717
718 /****f* silcmath/SilcMPAPI/silc_mp_and
719  *
720  * SYNOPSIS
721  *
722  *    void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
723  *
724  * DESCRIPTION
725  *
726  *    Logical and operator. The result is saved to `dst'.
727  *
728  ***/
729 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
730
731 /****f* silcmath/SilcMPAPI/silc_mp_or
732  *
733  * SYNOPSIS
734  *
735  *    void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
736  *
737  * DESCRIPTION
738  *
739  *    Logical inclusive OR operator. The result is saved to `dst'.
740  *
741  ***/
742 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
743
744 /****f* silcmath/SilcMPAPI/silc_mp_xor
745  *
746  * SYNOPSIS
747  *
748  *    void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
749  *
750  * DESCRIPTION
751  *
752  *    Logical exclusive OR operator. The result is saved to `dst'.
753  *
754  ***/
755 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
756
757 #endif