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