5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
80 /****f* silcmath/SilcMPAPI/silc_mp_uninit
84 * void silc_mp_uninit(SilcMPInt *mp);
88 * Uninitializes the MP Integer.
91 void silc_mp_uninit(SilcMPInt *mp);
93 /****f* silcmath/SilcMPAPI/silc_mp_size
97 * size_t silc_mp_size(SilcMPInt *mp);
101 * Return the precision size of the integer `mp'.
104 size_t silc_mp_size(SilcMPInt *mp);
106 /****f* silcmath/SilcMPAPI/silc_mp_sizeinbase
110 * size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
114 * Return the size of the integer in base `base'.
118 * For any other base but 2 this function usually returns only an
119 * approximated size in the base. It is however guaranteed that the
120 * the returned size is always at least the size of the integer or
123 * For base 2 this returns the exact bit-size of the integer.
126 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
128 /****f* silcmath/SilcMPAPI/silc_mp_set
132 * void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
136 * Set `dst' integer from `src' integer. The `dst' must already be
140 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
142 /****f* silcmath/SilcMPAPI/silc_mp_set_ui
146 * void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
150 * Set `dst' integer from unsigned word `ui'. The `dst' must already be
154 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
156 /****f* silcmath/SilcMPAPI/silc_mp_set_si
160 * void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
164 * Set `dst' integer from single word `si'. The `dst' must
165 * already be initialized.
168 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
170 /****f* silcmath/SilcMPAPI/silc_mp_set_str
174 * void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
178 * Set `dst' integer from string `str' of base `base'. The `dst' must
179 * already be initialized.
183 * For base 2 the string must be in ASCII bit presentation, not in
184 * binary. Use the silc_mp_bin2mp to decode binary into integer.
187 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
189 /****f* silcmath/SilcMPAPI/silc_mp_get_ui
193 * SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
197 * Returns the least significant unsigned word from `mp'.
200 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
202 /****f* silcmath/SilcMPAPI/silc_mp_get_str
206 * void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
210 * Converts integer `mp' into a string of base `base'. The `str'
211 * must already have space allocated. The function returns the same
212 * as `str' or NULL on error.
216 * For base 2 the returned string is in ASCII bit presentation, not
217 * in binary. Use the silc_mp_mp2bin to encode integer into binary.
220 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
222 /****f* silcmath/SilcMPAPI/silc_mp_add
226 * void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
230 * Add two integers `mp1' and `mp2' and save the result to `dst'.
233 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
235 /****f* silcmath/SilcMPAPI/silc_mp_add_ui
239 * void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
243 * Add two integers `mp1' and unsigned word `ui' and save the result
247 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
249 /****f* silcmath/SilcMPAPI/silc_mp_sub
253 * void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
257 * Subtract two integers `mp1' and `mp2' and save the result to `dst'.
260 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
262 /****f* silcmath/SilcMPAPI/silc_mp_sub_ui
266 * void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
270 * Subtract integers `mp1' and unsigned word `ui' and save the result
274 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
276 /****f* silcmath/SilcMPAPI/silc_mp_mul
280 * void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
284 * Multiply two integers `mp1' and `mp2' and save the result to `dst'.
287 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
289 /****f* silcmath/SilcMPAPI/silc_mp_mul_ui
293 * void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
297 * Multiply integer `mp1' and unsigned word `ui' and save the result
301 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
303 /****f* silcmath/SilcMPAPI/silc_mp_mul_2exp
307 * void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
311 * Multiply integers `mp1' with 2 ** `exp' and save the result to
312 * `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
315 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
317 /****f* silcmath/SilcMPAPI/silc_mp_sqrt
321 * void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
325 * Compute square root of floor(sqrt(src)) and save the result to `dst'.
328 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
330 /****f* silcmath/SilcMPAPI/silc_mp_div
334 * void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
338 * Divide the `mp1' and `mp2' and save the result to the `dst'. This
339 * is equivalent to dst = mp1 / mp2;
342 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
344 /****f* silcmath/SilcMPAPI/silc_mp_div_ui
348 * void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
352 * Divide the `mp1' and unsigned word `ui' and save the result to the
353 * `dst'. This is equivalent to dst = mp1 / ui;
356 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
358 /****f* silcmath/SilcMPAPI/silc_mp_div_qr
362 * void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
367 * Divide the `mp1' and `mp2' and save the quotient to the `q' and
368 * the remainder to the `r'. This is equivalent to the q = mp1 / mp2,
369 * r = mp1 mod mp2 (or mp1 = mp2 * q + r). If the `q' or `r' is NULL
370 * then the operation is omitted.
373 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
376 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp
380 * void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
384 * Divide the `mp1' with 2 ** `exp' and save the result to `dst'.
385 * This is equivalent to dst = mp1 / (2 ^ exp).
388 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
390 /****f* silcmath/SilcMPAPI/silc_mp_div_2exp_qr
394 * void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
399 * Divide the `mp1' with 2 ** `exp' and save the quotient to `q' and
400 * the remainder to `r'. This is equivalent to q = mp1 / (2 ^ exp),
401 * r = mp1 mod (2 ^ exp). If the `q' or `r' is NULL then the operation
405 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
408 /****f* silcmath/SilcMPAPI/silc_mp_mod
412 * void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
416 * Mathematical MOD function. Produces the remainder of `mp1' and `mp2'
417 * and saves the result to `dst'. This is equivalent to dst = mp1 mod mp2.
418 * The same result can also be get with silc_mp_div_qr as that function
419 * returns the remainder as well.
422 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
424 /****f* silcmath/SilcMPAPI/silc_mp_mod_ui
428 * void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
432 * Mathematical MOD function. Produces the remainder of `mp1' and
433 * unsigned word `ui' and saves the result to `dst'. This is equivalent
434 * to dst = mp1 mod ui.
437 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
439 /****f* silcmath/SilcMPAPI/silc_mp_mod_2exp
443 * void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
447 * Computes the remainder of `mp1' with 2 ** `exp' and saves the
448 * result to `dst'. This is equivalent to dst = mp1 mod (2 ^ exp).
449 * The same result can also be get with silc_mp_div_2exp_qr as that
450 * function returns the remainder as well.
453 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
455 /****f* silcmath/SilcMPAPI/silc_mp_pow
459 * void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
463 * Compute `mp1' ** `exp' and save the result to `dst'. This is
464 * equivalent to dst = mp1 ^ exp.
467 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
469 /****f* silcmath/SilcMPAPI/silc_mp_pow_ui
473 * void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
477 * Compute `mp1' ** `exp' and save the result to `dst'. This is
478 * equivalent to dst = mp1 ^ exp.
481 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
483 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod
487 * void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
492 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
493 * This is equivalent to dst = (mp1 ^ exp) mod mod.
496 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
499 /****f* silcmath/SilcMPAPI/silc_mp_pow_mod_ui
503 * void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
508 * Compute (`mp1' ** `exp') mod `mod' and save the result to `dst'.
509 * This is equivalent to dst = (mp1 ^ exp) mod mod.
512 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
515 /****f* silcmath/SilcMPAPI/silc_mp_modinv
519 * void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
523 * Find multiplicative inverse using Euclid's extended algorithm.
524 * Computes inverse such that a * inv mod n = 1, where 0 < a < n.
525 * Algorithm goes like this:
531 * g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
532 * v(i+1) = v(i-1) - y * v(i)
534 * do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n,
535 * is added to inverse making it positive again. (Sometimes the algorithm
536 * has a variable u defined too and it behaves just like v, except that
537 * initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is
538 * not needed by the algorithm so it does not have to be included.)
541 void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
543 /****f* silcmath/SilcMPAPI/silc_mp_gcd
547 * void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
551 * Calculate the greatest common divisor of the integers `mp1' and `mp2'
552 * and save the result to `dst'.
555 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
557 /****f* silcmath/SilcMPAPI/silc_mp_gcdext
561 * void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t,
562 * SilcMPInt *mp1, SilcMPInt *mp2);
566 * Calculate the extended greatest common divisor `g', `s' and `t' such
567 * that g = mp1 * s + mp2 * + t.
570 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
573 /****f* silcmath/SilcMPAPI/silc_mp_cmp
577 * int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
581 * Compare `mp1' and `mp2'. Returns posivite, zero, or negative
582 * if `mp1' > `mp2', `mp1' == `mp2', or `mp1' < `mp2', respectively.
585 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
587 /****f* silcmath/SilcMPAPI/silc_mp_cmp_si
591 * int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
595 * Compare `mp1' and single word `si'. Returns posivite, zero, or negative
596 * if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
599 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
601 /****f* silcmath/SilcMPAPI/silc_mp_cmp_ui
605 * int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
609 * Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or
610 * negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui',
614 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
616 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin
620 * unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
621 * SilcUInt32 *ret_len);
625 * Encodes MP integer into binary data. Returns allocated data that
626 * must be free'd by the caller. If `len' is provided the destination
627 * buffer is allocated that large. If zero then the size is approximated.
630 unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
631 SilcUInt32 *ret_len);
633 /****f* silcmath/SilcMPAPI/silc_mp_mp2bin_noalloc
637 * void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
638 * SilcUInt32 dst_len);
642 * Same as silc_mp_mp2bin but does not allocate any memory. The
643 * encoded data is returned into `dst' of size of `dst_len'.
646 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
649 /****f* silcmath/SilcMPAPI/silc_mp_bin2mp
653 * void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len,
658 * Decodes binary data into MP integer. The integer sent as argument
659 * must be initialized.
662 void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret);
664 /****f* silcmath/SilcMPAPI/silc_mp_abs
668 * void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
672 * Assign the absolute value of `src' to `dst'.
675 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
677 /****f* silcmath/SilcMPAPI/silc_mp_neg
681 * void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
685 * Negate `src' and save the result to `dst'.
688 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
690 /****f* silcmath/SilcMPAPI/silc_mp_and
694 * void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
698 * Logical and operator. The result is saved to `dst'.
701 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
703 /****f* silcmath/SilcMPAPI/silc_mp_or
707 * void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
711 * Logical inclusive OR operator. The result is saved to `dst'.
714 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
716 /****f* silcmath/SilcMPAPI/silc_mp_xor
720 * void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
724 * Logical exclusive OR operator. The result is saved to `dst'.
727 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);