5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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/SilcMPAPI/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/SilcMPAPI/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/SilcMPAPI/silc_mp_uninit
82 * void silc_mp_uninit(SilcMPInt *mp);
86 * Uninitializes the MP Integer.
89 void silc_mp_uninit(SilcMPInt *mp);
91 /****f* silcmath/SilcMPAPI/silc_mp_size
95 * size_t silc_mp_size(SilcMPInt *mp);
99 * Return the precision size of the integer `mp'.
102 size_t silc_mp_size(SilcMPInt *mp);
104 /****f* silcmath/SilcMPAPI/silc_mp_sizeinbase
108 * size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
112 * Return the size of the integer in base `base'.
116 * For any other base but 2 this function usually returns only an
117 * approximated size in the base. It is however guaranteed that the
118 * the returned size is always at least the size of the integer or
121 * For base 2 this returns the exact bit-size of the integer.
124 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
126 /****f* silcmath/SilcMPAPI/silc_mp_set
130 * void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
134 * Set `dst' integer from `src' integer. The `dst' must already be
138 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
140 /****f* silcmath/SilcMPAPI/silc_mp_set_ui
144 * void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
148 * Set `dst' integer from unsigned word `ui'. The `dst' must already be
152 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
154 /****f* silcmath/SilcMPAPI/silc_mp_set_si
158 * void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
162 * Set `dst' integer from single word `si'. The `dst' must
163 * already be initialized.
166 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
168 /****f* silcmath/SilcMPAPI/silc_mp_set_str
172 * void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
176 * Set `dst' integer from string `str' of base `base'. The `dst' must
177 * already be initialized.
181 * For base 2 the string must be in ASCII bit presentation, not in
182 * binary. Use the silc_mp_bin2mp to decode binary into integer.
185 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
187 /****f* silcmath/SilcMPAPI/silc_mp_get_ui
191 * SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
195 * Returns the least significant unsigned word from `mp'.
198 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
200 /****f* silcmath/SilcMPAPI/silc_mp_get_str
204 * void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
208 * Converts integer `mp' into a string of base `base'. The `str'
209 * must already have space allocated. The function returns the same
210 * as `str' or NULL on error.
214 * For base 2 the returned string is in ASCII bit presentation, not
215 * in binary. Use the silc_mp_mp2bin to encode integer into binary.
218 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
220 /****f* silcmath/SilcMPAPI/silc_mp_add
224 * void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
228 * Add two integers `mp1' and `mp2' and save the result to `dst'.
231 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
233 /****f* silcmath/SilcMPAPI/silc_mp_add_ui
237 * void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
241 * Add two integers `mp1' and unsigned word `ui' and save the result
245 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
247 /****f* silcmath/SilcMPAPI/silc_mp_sub
251 * void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
255 * Subtract two integers `mp1' and `mp2' and save the result to `dst'.
258 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
260 /****f* silcmath/SilcMPAPI/silc_mp_sub_ui
264 * void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
268 * Subtract integers `mp1' and unsigned word `ui' and save the result
272 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
274 /****f* silcmath/SilcMPAPI/silc_mp_mul
278 * void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
282 * Multiply two integers `mp1' and `mp2' and save the result to `dst'.
285 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
287 /****f* silcmath/SilcMPAPI/silc_mp_mul_ui
291 * void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
295 * Multiply integer `mp1' and unsigned word `ui' and save the result
299 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
301 /****f* silcmath/SilcMPAPI/silc_mp_mul_2exp
305 * void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
309 * Multiply integers `mp1' with 2 ** `exp' and save the result to
310 * `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
313 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
315 /****f* silcmath/SilcMPAPI/silc_mp_sqrt
319 * void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
323 * Compute square root of floor(sqrt(src)) and save the result to `dst'.
326 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
328 /****f* silcmath/SilcMPAPI/silc_mp_div
332 * void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
336 * Divide the `mp1' and `mp2' and save the result to the `dst'. This
337 * is equivalent to dst = mp1 / mp2;
340 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
342 /****f* silcmath/SilcMPAPI/silc_mp_div_ui
346 * void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
350 * Divide the `mp1' and unsigned word `ui' and save the result to the
351 * `dst'. This is equivalent to dst = mp1 / ui;
354 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
356 /****f* silcmath/SilcMPAPI/silc_mp_div_qr
360 * void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
365 * Divide the `mp1' and `mp2' and save the quotient to the `q' and
366 * the remainder to the `r'. This is equivalent to the q = mp1 / mp2,
367 * r = mp1 mod mp2 (or mp1 = mp2 * q + r). If the `q' or `r' is NULL
368 * then the operation is omitted.
371 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
374 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp
378 * void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
382 * Divide the `mp1' with 2 ** `exp' and save the result to `dst'.
383 * This is equivalent to dst = mp1 / (2 ^ exp).
386 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
388 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp_qr
392 * void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
397 * Divide the `mp1' with 2 ** `exp' and save the quotient to `q' and
398 * the remainder to `r'. This is equivalent to q = mp1 / (2 ^ exp),
399 * r = mp1 mod (2 ^ exp). If the `q' or `r' is NULL then the operation
403 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
406 /****f* silcmath/SilcMPAPI/silc_mp_mod
410 * void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
414 * Mathematical MOD function. Produces the remainder of `mp1' and `mp2'
415 * and saves the result to `dst'. This is equivalent to dst = mp1 mod mp2.
416 * The same result can also be get with silc_mp_div_qr as that function
417 * returns the remainder as well.
420 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
422 /****f* silcmath/SilcMPAPI/silc_mp_mod_ui
426 * void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
430 * Mathematical MOD function. Produces the remainder of `mp1' and
431 * unsigned word `ui' and saves the result to `dst'. This is equivalent
432 * to dst = mp1 mod ui.
435 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
437 /****f* silcmath/SilcMPAPI/silc_mp_mod_2exp
441 * void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
445 * Computes the remainder of `mp1' with 2 ** `exp' and saves the
446 * result to `dst'. This is equivalent to dst = mp1 mod (2 ^ exp).
447 * The same result can also be get with silc_mp_div_2exp_qr as that
448 * function returns the remainder as well.
451 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
453 /****f* silcmath/SilcMPAPI/silc_mp_pow
457 * void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
461 * Compute `mp1' ** `exp' and save the result to `dst'. This is
462 * equivalent to dst = mp1 ^ exp.
465 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
467 /****f* silcmath/SilcMPAPI/silc_mp_pow_ui
471 * void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
475 * Compute `mp1' ** `exp' and save the result to `dst'. This is
476 * equivalent to dst = mp1 ^ exp.
479 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
481 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod
485 * void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
490 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
491 * This is equivalent to dst = (mp1 ^ exp) mod mod.
494 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
497 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod_ui
501 * void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
506 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
507 * This is equivalent to dst = (mp1 ^ exp) mod mod.
510 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
513 /****f* silcmath/SilcMPAPI/silc_mp_modinv
517 * void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
521 * Find multiplicative inverse using Euclid's extended algorithm.
522 * Computes inverse such that a * inv mod n = 1, where 0 < a < n.
523 * Algorithm goes like this:
529 * g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
530 * v(i+1) = v(i-1) - y * v(i)
532 * do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n,
533 * is added to inverse making it positive again. (Sometimes the algorithm
534 * has a variable u defined too and it behaves just like v, except that
535 * initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is
536 * not needed by the algorithm so it does not have to be included.)
539 void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
541 /****f* silcmath/SilcMPAPI/silc_mp_gcd
545 * void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
549 * Calculate the greatest common divisor of the integers `mp1' and `mp2'
550 * and save the result to `dst'.
553 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
555 /****f* silcmath/SilcMPAPI/silc_mp_gcdext
559 * void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t,
560 * SilcMPInt *mp1, SilcMPInt *mp2);
564 * Calculate the extended greatest common divisor `g', `s' and `t' such
565 * that g = mp1 * s + mp2 * + t.
568 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
571 /****f* silcmath/SilcMPAPI/silc_mp_cmp
575 * int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
579 * Compare `mp1' and `mp2'. Returns posivite, zero, or negative
580 * if `mp1' > `mp2', `mp1' == `mp2', or `mp1' < `mp2', respectively.
583 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
585 /****f* silcmath/SilcMPAPI/silc_mp_cmp_si
589 * int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
593 * Compare `mp1' and single word `si'. Returns posivite, zero, or negative
594 * if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
597 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
599 /****f* silcmath/SilcMPAPI/silc_mp_cmp_ui
603 * int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
607 * Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or
608 * negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui',
612 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
614 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin
618 * unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
619 * SilcUInt32 *ret_len);
623 * Encodes MP integer into binary data. Returns allocated data that
624 * must be free'd by the caller. If `len' is provided the destination
625 * buffer is allocated that large. If zero then the size is approximated.
628 unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
629 SilcUInt32 *ret_len);
631 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin_noalloc
635 * void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
636 * SilcUInt32 dst_len);
640 * Same as silc_mp_mp2bin but does not allocate any memory. The
641 * encoded data is returned into `dst' and it's length to the `ret_len'.
644 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
647 /****f* silcmath/SilcMPAPI/silc_mp_bin2mp
651 * void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len,
656 * Decodes binary data into MP integer. The integer sent as argument
657 * must be initialized.
660 void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret);
662 /****f* silcmath/SilcMPAPI/silc_mp_abs
666 * void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
670 * Assign the absolute value of `src' to `dst'.
673 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
675 /****f* silcmath/SilcMPAPI/silc_mp_neg
679 * void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
683 * Negate `src' and save the result to `dst'.
686 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
688 /****f* silcmath/SilcMPAPI/silc_mp_and
692 * void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
696 * Logical and operator. The result is saved to `dst'.
699 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
701 /****f* silcmath/SilcMPAPI/silc_mp_or
705 * void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
709 * Logical inclusive OR operator. The result is saved to `dst'.
712 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
714 /****f* silcmath/SilcMPAPI/silc_mp_xor
718 * void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
722 * Logical exclusive OR operator. The result is saved to `dst'.
725 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);