1 /****h* silcmath/silcmath.h
9 * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
11 * Copyright (C) 1997 - 2000 Pekka Riikonen
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 * SILC Math interface includes various utility functions such as
26 * prime generation, and conversion routines. See the silcmp.h for the
34 /****f* silcmath/SilcMathAPI/silc_mp_modinv
38 * void silc_mp_modinv(SilcInt *inv, SilcInt *a, SilcInt *n);
42 * Find multiplicative inverse using Euclid's extended algorithm.
43 * Computes inverse such that a * inv mod n = 1, where 0 < a < n.
44 * Algorithm goes like this:
50 * g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
51 * v(i+1) = v(i-1) - y * v(i)
53 * do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n,
54 * is added to inverse making it positive again. (Sometimes the algorithm
55 * has a variable u defined too and it behaves just like v, except that
56 * initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is
57 * not needed by the algorithm so it does not have to be included.)
60 void silc_mp_modinv(SilcInt *inv, SilcInt *a, SilcInt *n);
62 /****f* silcmath/SilcMathAPI/silc_mp_mp2bin
66 * unsigned char *silc_mp_mp2bin(SilcInt *val, uint32 len,
71 * Encodes MP integer into binary data. Returns allocated data that
72 * must be free'd by the caller. If `len' is provided the destination
73 * buffer is allocated that large. If zero then the size is approximated.
76 unsigned char *silc_mp_mp2bin(SilcInt *val, uint32 len,
79 /****f* silcmath/SilcMathAPI/silc_mp_mp2bin_noalloc
83 * void silc_mp_mp2bin_noalloc(SilcInt *val, unsigned char *dst,
88 * Same as silc_mp_mp2bin but does not allocate any memory. The
89 * encoded data is returned into `dst' and it's length to the `ret_len'.
92 void silc_mp_mp2bin_noalloc(SilcInt *val, unsigned char *dst,
95 /****f* silcmath/SilcMathAPI/silc_mp_bin2mp
99 * void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcInt *ret);
103 * Decodes binary data into MP integer. The integer sent as argument
104 * must be initialized.
107 void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcInt *ret);
109 /****f* silcmath/SilcMathAPI/silc_math_gen_prime
113 * int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose);
117 * Find appropriate prime. It generates a number by taking random bytes.
118 * It then tests the number that it's not divisible by any of the small
119 * primes and then it performs Fermat's prime test. I thank Rieks Joosten
120 * (r.joosten@pijnenburg.nl) for such a good help with prime tests.
122 * If argument verbose is TRUE this will display some status information
123 * about the progress of generation.
126 int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose);
128 /****f* silcmath/SilcMathAPI/silc_math_prime_test
132 * int silc_math_prime_test(SilcInt *p);
136 * Performs primality testings for given number. Returns TRUE if the
137 * number is probably a prime.
140 int silc_math_prime_test(SilcInt *p);