5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 Pekka Riikonen
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.
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.
20 /****h* silcmath/SILC MP Interface
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.
35 #if defined(SILC_MP_GMP)
36 #include "mp_gmp.h" /* SILC_MP_GMP */
40 #endif /* SILC_DIST_TMA */
43 #endif /* SILC_DIST_TFM */
46 /****d* silcmath/SilcMPInt
50 * typedef SILC_MP_INT SilcMPInt;
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.
60 typedef SILC_MP_INT SilcMPInt;
63 /****f* silcmath/silc_mp_init
67 * void silc_mp_init(SilcMPInt mp);
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.
76 void silc_mp_init(SilcMPInt *mp);
78 /****f* silcmath/silc_mp_sinit
82 * SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
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.
93 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
95 /****f* silcmath/silc_mp_uninit
99 * void silc_mp_uninit(SilcMPInt *mp);
103 * Uninitializes the MP Integer.
106 void silc_mp_uninit(SilcMPInt *mp);
108 /****f* silcmath/silc_mp_suninit
112 * void silc_mp_suninit(SilcStack stack, SilcMPInt *mp);
116 * Uninitializes the MP Integer.
119 void silc_mp_suninit(SilcStack stack, SilcMPInt *mp);
121 /****f* silcmath/silc_mp_size
125 * size_t silc_mp_size(SilcMPInt *mp);
129 * Return the precision size of the integer `mp'.
132 size_t silc_mp_size(SilcMPInt *mp);
134 /****f* silcmath/silc_mp_sizeinbase
138 * size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
142 * Return the size of the integer in base `base'.
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
151 * For base 2 this returns the exact bit-size of the integer.
154 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
156 /****f* silcmath/silc_mp_set
160 * void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
164 * Set `dst' integer from `src' integer. The `dst' must already be
168 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
170 /****f* silcmath/silc_mp_set_ui
174 * void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
178 * Set `dst' integer from unsigned word `ui'. The `dst' must already be
182 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
184 /****f* silcmath/silc_mp_set_si
188 * void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
192 * Set `dst' integer from single word `si'. The `dst' must
193 * already be initialized.
196 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
198 /****f* silcmath/silc_mp_set_str
202 * void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
206 * Set `dst' integer from string `str' of base `base'. The `dst' must
207 * already be initialized.
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.
215 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
217 /****f* silcmath/silc_mp_get_ui
221 * SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
225 * Returns the least significant unsigned word from `mp'.
228 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
230 /****f* silcmath/silc_mp_get_str
234 * void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
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.
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.
248 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
250 /****f* silcmath/silc_mp_add
254 * void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
258 * Add two integers `mp1' and `mp2' and save the result to `dst'.
261 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
263 /****f* silcmath/silc_mp_add_ui
267 * void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
271 * Add two integers `mp1' and unsigned word `ui' and save the result
275 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
277 /****f* silcmath/silc_mp_sub
281 * void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
285 * Subtract two integers `mp1' and `mp2' and save the result to `dst'.
288 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
290 /****f* silcmath/silc_mp_sub_ui
294 * void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
298 * Subtract integers `mp1' and unsigned word `ui' and save the result
302 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
304 /****f* silcmath/silc_mp_mul
308 * void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
312 * Multiply two integers `mp1' and `mp2' and save the result to `dst'.
315 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
317 /****f* silcmath/silc_mp_mul_ui
321 * void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
325 * Multiply integer `mp1' and unsigned word `ui' and save the result
329 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
331 /****f* silcmath/silc_mp_mul_2exp
335 * void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
339 * Multiply integers `mp1' with 2 ** `exp' and save the result to
340 * `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
343 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
345 /****f* silcmath/silc_mp_sqrt
349 * void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
353 * Compute square root of floor(sqrt(src)) and save the result to `dst'.
356 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
358 /****f* silcmath/silc_mp_div
362 * void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
366 * Divide the `mp1' and `mp2' and save the result to the `dst'. This
367 * is equivalent to dst = mp1 / mp2;
370 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
372 /****f* silcmath/silc_mp_div_ui
376 * void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
380 * Divide the `mp1' and unsigned word `ui' and save the result to the
381 * `dst'. This is equivalent to dst = mp1 / ui;
384 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
386 /****f* silcmath/silc_mp_div_qr
390 * void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
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.
401 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
404 /****f* silcmath/silc_mp_div_2exp
408 * void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
412 * Divide the `mp1' with 2 ** `exp' and save the result to `dst'.
413 * This is equivalent to dst = mp1 / (2 ^ exp).
416 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
418 /****f* silcmath/silc_mp_div_2exp_qr
422 * void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
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
433 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
436 /****f* silcmath/silc_mp_mod
440 * void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
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.
450 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
452 /****f* silcmath/silc_mp_mod_ui
456 * void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
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.
465 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
467 /****f* silcmath/silc_mp_mod_2exp
471 * void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
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.
481 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
483 /****f* silcmath/silc_mp_pow
487 * void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
491 * Compute `mp1' ** `exp' and save the result to `dst'. This is
492 * equivalent to dst = mp1 ^ exp.
495 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
497 /****f* silcmath/silc_mp_pow_ui
501 * void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
505 * Compute `mp1' ** `exp' and save the result to `dst'. This is
506 * equivalent to dst = mp1 ^ exp.
509 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
511 /****f* silcmath/silc_mp_pow_mod
515 * void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
520 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
521 * This is equivalent to dst = (mp1 ^ exp) mod mod.
524 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
527 /****f* silcmath/silc_mp_pow_mod_ui
531 * void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
536 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
537 * This is equivalent to dst = (mp1 ^ exp) mod mod.
540 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
543 /****f* silcmath/silc_mp_modinv
547 * void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
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:
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)
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.)
569 void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
571 /****f* silcmath/silc_mp_gcd
575 * void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
579 * Calculate the greatest common divisor of the integers `mp1' and `mp2'
580 * and save the result to `dst'.
583 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
585 /****f* silcmath/silc_mp_gcdext
589 * void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t,
590 * SilcMPInt *mp1, SilcMPInt *mp2);
594 * Calculate the extended greatest common divisor `g', `s' and `t' such
595 * that g = mp1 * s + mp2 * + t.
598 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
601 /****f* silcmath/silc_mp_cmp
605 * int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
609 * Compare `mp1' and `mp2'. Returns posivite, zero, or negative
610 * if `mp1' > `mp2', `mp1' == `mp2', or `mp1' < `mp2', respectively.
613 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
615 /****f* silcmath/silc_mp_cmp_si
619 * int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
623 * Compare `mp1' and single word `si'. Returns posivite, zero, or negative
624 * if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
627 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
629 /****f* silcmath/silc_mp_cmp_ui
633 * int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
637 * Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or
638 * negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui',
642 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
644 /****f* silcmath/silc_mp_mp2bin
648 * unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
649 * SilcUInt32 *ret_len);
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.
658 unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
659 SilcUInt32 *ret_len);
661 /****f* silcmath/silc_mp_mp2bin_noalloc
665 * void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
666 * SilcUInt32 dst_len);
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'.
674 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
677 /****f* silcmath/silc_mp_bin2mp
681 * void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len,
686 * Decodes binary data into MP integer. The integer sent as argument
687 * must be initialized.
690 void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret);
692 /****f* silcmath/silc_mp_abs
696 * void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
700 * Assign the absolute value of `src' to `dst'.
703 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
705 /****f* silcmath/silc_mp_neg
709 * void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
713 * Negate `src' and save the result to `dst'.
716 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
718 /****f* silcmath/silc_mp_and
722 * void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
726 * Logical and operator. The result is saved to `dst'.
729 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
731 /****f* silcmath/silc_mp_or
735 * void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
739 * Logical inclusive OR operator. The result is saved to `dst'.
742 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
744 /****f* silcmath/silc_mp_xor
748 * void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
752 * Logical exclusive OR operator. The result is saved to `dst'.
755 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);