Added silc_mp_format and silc_mp_unformat MP integer encoder/decoder
[crypto.git] / lib / silcmath / silcmp.h
index e611554fcfa15b43565c75dc3aa5f7c78a0565ba..5ff3d34772b70e15b409a0a4b0b400f2e9fc817e 100644 (file)
@@ -1,46 +1,49 @@
-/****h* silcmath/silcmp.h
- *
- * NAME
- *
- * silcmp.h
- *
- * COPYRIGHT
- *
- * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
- *
- * Copyright (C) 1997 - 2000 Pekka Riikonen
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+/*
+
+  silcmp.h
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 1997 - 2008 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+
+/****h* silcmath/MP Integer Interface
  *
  * DESCRIPTION
  *
  * SILC MP Library Interface. This interface defines the arbitrary
- * precision arithmetic routines for SILC. Currently the actual routines
- * are implemented separately, usually by some other MP library. The
- * interface is generic but is mainly intended for crypto usage. This
- * interface is used by SILC routines that needs big numbers, such as
- * RSA implementation, Diffie-Hellman implementation etc.
+ * precision arithmetic routines for SILC. The interface is generic but
+ * is mainly intended for crypto usage. This interface is used by SILC
+ * routines that needs big numbers, such as RSA implementation,
+ * Diffie-Hellman implementation etc.
  *
  ***/
 
 #ifndef SILCMP_H
 #define SILCMP_H
 
-#ifdef SILC_MP_GMP
+#if defined(SILC_MP_GMP)
 #include "mp_gmp.h"            /* SILC_MP_GMP */
 #else
-#include "mp_mpi.h"            /* SILC_MP_NSS_MPI */
+#ifdef SILC_DIST_TMA
+#include "mp_tma.h"
+#endif /* SILC_DIST_TMA */
+#ifdef SILC_DIST_TFM
+#include "mp_tfm.h"
+#endif /* SILC_DIST_TFM */
 #endif
 
-/****d* silcmath/SilcMPAPI/SilcMPInt
+/****d* silcmath/SilcMPInt
  *
  * NAME
  *
 typedef SILC_MP_INT SilcMPInt;
 /***/
 
-/****f* silcmath/SilcMPAPI/silc_mp_alloc
+/****f* silcmath/silc_mp_init
  *
  * SYNOPSIS
  *
- *    void silc_mp_init(SilcMPInt mp);
+ *    SilcBool silc_mp_init(SilcMPInt mp);
  *
  * DESCRIPTION
  *
- *    Initializes the SilcMPInt *that is the actual MP Integer.
- *    This must be called before any of the silc_mp_ routines can be
- *    used. The integer is uninitialized with the silc_mp_uninit function.
+ *    Initializes the MP integer.  This must be called before calling any
+ *    other routine in SILC MP API.  The silc_mp_uninit must be called
+ *    to uninitialize the integer.  Returns FALSE on error, TRUE otherwise.
  *
  ***/
-void silc_mp_init(SilcMPInt *mp);
+SilcBool silc_mp_init(SilcMPInt *mp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_free
+/****f* silcmath/silc_mp_sinit
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
+ *
+ * DESCRIPTION
+ *
+ *    Initializes the MP integer.  This must be called before calling any
+ *    other routine in SILC MP API.  The silc_mp_uninit must be called
+ *    to uninitialize the integer.  Returns FALSE on error, TRUE otherwise.
+ *
+ *    If `stack' is non-NULL all memory is allocated from `stack'.  If it
+ *    is NULL this is equivalent to silc_mp_init.  The silc_mp_uninit must
+ *    be called to uninitialize the integer even when `stack' is non-NULL.
+ *
+ ***/
+SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp);
+
+/****f* silcmath/silc_mp_uninit
  *
  * SYNOPSIS
  *
@@ -85,7 +107,7 @@ void silc_mp_init(SilcMPInt *mp);
  ***/
 void silc_mp_uninit(SilcMPInt *mp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_size
+/****f* silcmath/silc_mp_size
  *
  * SYNOPSIS
  *
@@ -98,7 +120,7 @@ void silc_mp_uninit(SilcMPInt *mp);
  ***/
 size_t silc_mp_size(SilcMPInt *mp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_sizeinbase
+/****f* silcmath/silc_mp_sizeinbase
  *
  * SYNOPSIS
  *
@@ -106,33 +128,39 @@ size_t silc_mp_size(SilcMPInt *mp);
  *
  * DESCRIPTION
  *
- *    Return the size of the integer in base `base'. Note that this size
- *    is probably only an approximation.  However, it is guaranteed that
- *    the returned size is always at least the size of the integer, however,
- *    it may be larger.
+ *    Return the size of the integer in base `base'.
+ *
+ * NOTES
+ *
+ *    For any other base but 2 this function usually returns only an
+ *    approximated size in the base.  It is however guaranteed that the
+ *    the returned size is always at least the size of the integer or
+ *    larger.
+ *
+ *    For base 2 this returns the exact bit-size of the integer.
  *
  ***/
 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base);
 
-/****f* silcmath/SilcMPAPI/silc_mp_set
+/****f* silcmath/silc_mp_set
  *
  * SYNOPSIS
  *
- *    void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
+ *    SilcBool silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
  *
  * DESCRIPTION
  *
  *    Set `dst' integer from `src' integer. The `dst' must already be
- *    initialized.
+ *    initialized.  This in effect copies the integer from `src' to `dst'.
  *
  ***/
-void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
+SilcBool silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
 
-/****f* silcmath/SilcMPAPI/silc_mp_set_ui
+/****f* silcmath/silc_mp_set_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_set_ui(SilcMPInt *dst, uint32 ui);
+ *    SilcBool silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
@@ -140,13 +168,13 @@ void silc_mp_set(SilcMPInt *dst, SilcMPInt *src);
  *    initialized.
  *
  ***/
-void silc_mp_set_ui(SilcMPInt *dst, uint32 ui);
+SilcBool silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_set_si
+/****f* silcmath/silc_mp_set_si
  *
  * SYNOPSIS
  *
- *    void silc_mp_set_si(SilcMPInt *dst, int32 si);
+ *    SilcBool silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
  *
  * DESCRIPTION
  *
@@ -154,40 +182,45 @@ void silc_mp_set_ui(SilcMPInt *dst, uint32 ui);
  *    already be initialized.
  *
  ***/
-void silc_mp_set_si(SilcMPInt *dst, int32 si);
+SilcBool silc_mp_set_si(SilcMPInt *dst, SilcInt32 si);
 
-/****f* silcmath/SilcMPAPI/silc_mp_set_str
+/****f* silcmath/silc_mp_set_str
  *
  * SYNOPSIS
  *
- *    void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
+ *    SilcBool silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
  *
  * DESCRIPTION
  *
  *    Set `dst' integer from string `str' of base `base'. The `dst' must
  *    already be initialized.
  *
+ * NOTES
+ *
+ *    For base 2 the string must be in ASCII bit presentation, not in
+ *    binary.  Use the silc_mp_bin2mp to decode binary into integer.
+ *
  ***/
-void silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
+SilcBool silc_mp_set_str(SilcMPInt *dst, const char *str, int base);
 
-/****f* silcmath/SilcMPAPI/silc_mp_get_ui
+/****f* silcmath/silc_mp_get_ui
  *
  * SYNOPSIS
  *
- *    uint32 silc_mp_get_ui(SilcMPInt *mp);
+ *    SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
  *
  * DESCRIPTION
  *
  *    Returns the least significant unsigned word from `mp'.
  *
  ***/
-uint32 silc_mp_get_ui(SilcMPInt *mp);
+SilcUInt32 silc_mp_get_ui(SilcMPInt *mp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_get_str
+/****f* silcmath/silc_mp_get_str
  *
  * SYNOPSIS
  *
- *    void silc_mp_get_str(char *str, SilcMPInt *mp, int base);
+ *    char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
  *
  * DESCRIPTION
  *
@@ -195,27 +228,32 @@ uint32 silc_mp_get_ui(SilcMPInt *mp);
  *    must already have space allocated. The function returns the same
  *    as `str' or NULL on error.
  *
+ * NOTES
+ *
+ *    For base 2 the returned string is in ASCII bit presentation, not
+ *    in binary.  Use the silc_mp_mp2bin to encode integer into binary.
+ *
  ***/
 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base);
 
-/****f* silcmath/SilcMPAPI/silc_mp_add
+/****f* silcmath/silc_mp_add
  *
  * SYNOPSIS
  *
- *    void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
  *    Add two integers `mp1' and `mp2' and save the result to `dst'.
  *
  ***/
-void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_add_ui
+/****f* silcmath/silc_mp_add_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+ *    SilcBool silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
@@ -223,26 +261,26 @@ void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *    to `dst'.
  *
  ***/
-void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, mp_digit ui);
+SilcBool silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_sub
+/****f* silcmath/silc_mp_sub
  *
  * SYNOPSIS
  *
- *    void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
  *    Subtract two integers `mp1' and `mp2' and save the result to `dst'.
  *
  ***/
-void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_sub_ui
+/****f* silcmath/silc_mp_sub_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+ *    SilcBool silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
@@ -250,26 +288,26 @@ void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *    to `dst'.
  *
  ***/
-void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+SilcBool silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mul
+/****f* silcmath/silc_mp_mul
  *
  * SYNOPSIS
  *
- *    void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
  *    Multiply two integers `mp1' and `mp2' and save the result to `dst'.
  *
  ***/
-void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mul_ui
+/****f* silcmath/silc_mp_mul_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+ *    SilcBool silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
@@ -277,40 +315,40 @@ void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *    to `dst'.
  *
  ***/
-void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+SilcBool silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mul_2exp
+/****f* silcmath/silc_mp_mul_2exp
  *
  * SYNOPSIS
  *
- *    void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
+ *    SilcBool silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
  *
  * DESCRIPTION
  *
- *    Multiply integers `mp1' with 2 ** `exp' and save the result to 
+ *    Multiply integers `mp1' with 2 ** `exp' and save the result to
  *    `dst'. This is equivalent to dst = mp1 * (2 ^ exp).
  *
  ***/
-void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
+SilcBool silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_sqrt
+/****f* silcmath/silc_mp_sqrt
  *
  * SYNOPSIS
  *
- *    void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
+ *    SilcBool silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
  *
  * DESCRIPTION
  *
  *    Compute square root of floor(sqrt(src)) and save the result to `dst'.
  *
  ***/
-void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
+SilcBool silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
 
-/****f* silcmath/SilcMPAPI/silc_mp_div
+/****f* silcmath/silc_mp_div
  *
  * SYNOPSIS
  *
- *    void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
@@ -318,13 +356,13 @@ void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src);
  *    is equivalent to dst = mp1 / mp2;
  *
  ***/
-void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_div_ui
+/****f* silcmath/silc_mp_div_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+ *    SilcBool silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
@@ -332,31 +370,31 @@ void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *    `dst'. This is equivalent to dst = mp1 / ui;
  *
  ***/
-void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+SilcBool silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_div_qr
+/****f* silcmath/silc_mp_div_qr
  *
  * SYNOPSIS
  *
- *    void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
- *                        SilcMPInt *mp2);
+ *    SilcBool silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
+ *                            SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
  *    Divide the `mp1' and `mp2' and save the quotient to the `q' and
- *    the remainder to the `r'.  This is equivalent to the q = mp1 / mp2, 
+ *    the remainder to the `r'.  This is equivalent to the q = mp1 / mp2,
  *    r = mp1 mod mp2 (or mp1 = mp2 * q + r). If the `q' or `r' is NULL
  *    then the operation is omitted.
  *
  ***/
-void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
-                   SilcMPInt *mp2);
+SilcBool silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
+                       SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_div_2exp
+/****f* silcmath/silc_mp_div_2exp
  *
  * SYNOPSIS
  *
- *    void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
@@ -364,14 +402,14 @@ void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
  *    This is equivalent to dst = mp1 / (2 ^ exp).
  *
  ***/
-void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
+SilcBool silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_div_2exp
+/****f* silcmath/silc_mp_div_2exp_qr
  *
  * SYNOPSIS
  *
- *    void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
- *                             uint32 exp);
+ *    SilcBool silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
+ *                                 SilcUInt32 exp);
  *
  * DESCRIPTION
  *
@@ -381,14 +419,14 @@ void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
  *    is omitted.
  *
  ***/
-void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
-                        uint32 exp);
+SilcBool silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
+                            SilcUInt32 exp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mod
+/****f* silcmath/silc_mp_mod
  *
  * SYNOPSIS
  *
- *    void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
@@ -398,28 +436,28 @@ void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
  *    returns the remainder as well.
  *
  ***/
-void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mod_ui
+/****f* silcmath/silc_mp_mod_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+ *    SilcBool silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
- *    Mathematical MOD function. Produces the remainder of `mp1' and 
+ *    Mathematical MOD function. Produces the remainder of `mp1' and
  *    unsigned word `ui' and saves the result to `dst'. This is equivalent
  *    to dst = mp1 mod ui.
  *
  ***/
-void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
+SilcBool silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mod_2exp
+/****f* silcmath/silc_mp_mod_2exp
  *
  * SYNOPSIS
  *
- *    void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
@@ -429,27 +467,13 @@ void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
  *    function returns the remainder as well.
  *
  ***/
-void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui);
-
-/****f* silcmath/SilcMPAPI/silc_mp_pow
- *
- * SYNOPSIS
- *
- *    void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
- *
- * DESCRIPTION
- *
- *    Compute `mp1' ** `exp' and save the result to `dst'. This is
- *    equivalent to dst = mp1 ^ exp.
- *
- ***/
-void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
+SilcBool silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_pow_ui
+/****f* silcmath/silc_mp_pow_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
+ *    SilcBool silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
  *
  * DESCRIPTION
  *
@@ -457,14 +481,14 @@ void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp);
  *    equivalent to dst = mp1 ^ exp.
  *
  ***/
-void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
+SilcBool silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp);
 
-/****f* silcmath/SilcMPAPI/silc_mp_pow_mod
+/****f* silcmath/silc_mp_pow_mod
  *
  * SYNOPSIS
  *
- *    void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp, 
- *                         SilcMPInt *mod);
+ *    SilcBool silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
+ *                             SilcMPInt *mod);
  *
  * DESCRIPTION
  *
@@ -472,15 +496,15 @@ void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp);
  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
  *
  ***/
-void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp, 
-                    SilcMPInt *mod);
+SilcBool silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
+                        SilcMPInt *mod);
 
-/****f* silcmath/SilcMPAPI/silc_mp_pow_mod_ui
+/****f* silcmath/silc_mp_pow_mod_ui
  *
  * SYNOPSIS
  *
- *    void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp, 
- *                            SilcMPInt *mod);
+ *    SilcBool silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1,
+ *                                SilcUInt32 exp, SilcMPInt *mod);
  *
  * DESCRIPTION
  *
@@ -488,42 +512,42 @@ void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
  *    This is equivalent to dst = (mp1 ^ exp) mod mod.
  *
  ***/
-void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp, 
-                       SilcMPInt *mod);
+SilcBool silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
+                           SilcMPInt *mod);
 
-/****f* silcmath/SilcMPAPI/silc_mp_modinv
+/****f* silcmath/silc_mp_modinv
  *
  * SYNOPSIS
  *
- *    void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
+ *    SilcBool silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
  *
  * DESCRIPTION
  *
- *    Find multiplicative inverse using Euclid's extended algorithm. 
- *    Computes inverse such that a * inv mod n = 1, where 0 < a < n. 
+ *    Find multiplicative inverse using Euclid's extended algorithm.
+ *    Computes inverse such that a * inv mod n = 1, where 0 < a < n.
  *    Algorithm goes like this:
- *  
+ *
  *    g(0) = n    v(0) = 0
  *    g(1) = a    v(1) = 1
- * 
+ *
  *    y = g(i-1) / g(i)
  *    g(i+1) = g(i-1) - y * g(i) = g(i)-1 mod g(i)
  *    v(i+1) = v(i-1) - y * v(i)
- * 
- *    do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n, 
- *    is added to inverse making it positive again. (Sometimes the algorithm 
- *    has a variable u defined too and it behaves just like v, except that 
- *    initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is 
+ *
+ *    do until g(i) = 0, then inverse = v(i-1). If inverse is negative then n,
+ *    is added to inverse making it positive again. (Sometimes the algorithm
+ *    has a variable u defined too and it behaves just like v, except that
+ *    initalize values are swapped (i.e. u(0) = 1, u(1) = 0). However, u is
  *    not needed by the algorithm so it does not have to be included.)
  *
  ***/
-void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
+SilcBool silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
 
-/****f* silcmath/SilcMPAPI/silc_mp_gcd
+/****f* silcmath/silc_mp_gcd
  *
  * SYNOPSIS
  *
- *    void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
@@ -531,25 +555,9 @@ void silc_mp_modinv(SilcMPInt *inv, SilcMPInt *a, SilcMPInt *n);
  *    and save the result to `dst'.
  *
  ***/
-void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_gcdext
- *
- * SYNOPSIS
- *
- *    void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, 
- *                        SilcMPInt *mp1, SilcMPInt *mp2);
- *
- * DESCRIPTION
- *
- *    Calculate the extended greatest common divisor `g', `s' and `t' such
- *    that g = mp1 * s + mp2 * + t.
- *
- ***/
-void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
-                   SilcMPInt *mp2);
-
-/****f* silcmath/SilcMPAPI/silc_mp_cmp
+/****f* silcmath/silc_mp_cmp
  *
  * SYNOPSIS
  *
@@ -563,11 +571,11 @@ void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
  ***/
 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_cmp_si
+/****f* silcmath/silc_mp_cmp_si
  *
  * SYNOPSIS
  *
- *    int silc_mp_cmp_si(SilcMPInt *mp1, int32 si);
+ *    int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
  *
  * DESCRIPTION
  *
@@ -575,29 +583,29 @@ int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2);
  *    if `mp1' > `si', `mp1' == `si', or `mp1' < `si', respectively.
  *
  ***/
-int silc_mp_cmp_si(SilcMPInt *mp1, int32 si);
+int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si);
 
-/****f* silcmath/SilcMPAPI/silc_mp_cmp_ui
+/****f* silcmath/silc_mp_cmp_ui
  *
  * SYNOPSIS
  *
- *    int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui);
+ *    int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
  *
  * DESCRIPTION
  *
- *    Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or 
- *    negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui', 
+ *    Compare `mp1' and unsigned word `ui'. Returns posivite, zero, or
+ *    negative if `mp1' > `ui', `mp1' == `ui', or `mp1' < `ui',
  *    respectively.
  *
  ***/
-int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui);
+int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mp2bin
+/****f* silcmath/silc_mp_mp2bin
  *
  * SYNOPSIS
  *
- *    unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len, 
- *                                  uint32 *ret_len);
+ *    unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
+ *                                  SilcUInt32 *ret_len);
  *
  * DESCRIPTION
  *
@@ -606,30 +614,30 @@ int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui);
  *    buffer is allocated that large. If zero then the size is approximated.
  *
  ***/
-unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len, 
-                             uint32 *ret_len);
+unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
+                             SilcUInt32 *ret_len);
 
-/****f* silcmath/SilcMPAPI/silc_mp_mp2bin_noalloc
+/****f* silcmath/silc_mp_mp2bin_noalloc
  *
  * SYNOPSIS
  *
  *    void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
- *                                uint32 dst_len);
+ *                                SilcUInt32 dst_len);
  *
  * DESCRIPTION
  *
  *    Same as silc_mp_mp2bin but does not allocate any memory.  The
- *    encoded data is returned into `dst' and it's length to the `ret_len'.
+ *    encoded data is returned into `dst' of size of `dst_len'.
  *
  ***/
 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
-                           uint32 dst_len);
+                           SilcUInt32 dst_len);
 
-/****f* silcmath/SilcMPAPI/silc_mp_bin2mp
+/****f* silcmath/silc_mp_bin2mp
  *
  * SYNOPSIS
  *
- *    void silc_mp_bin2mp(unsigned char *data, uint32 len, 
+ *    void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len,
  *                        SilcMPInt *ret);
  *
  * DESCRIPTION
@@ -638,71 +646,127 @@ void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
  *    must be initialized.
  *
  ***/
-void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcMPInt *ret);
+void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret);
 
-/****f* silcmath/SilcMPAPI/silc_mp_abs
+/****f* silcmath/silc_mp_abs
  *
  * SYNOPSIS
  *
- *    void silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
+ *    SilcBool silc_mp_abs(SilcMPInt *src, SilcMPInt *dst);
  *
  * DESCRIPTION
  *
  *    Assign the absolute value of `src' to `dst'.
  *
  ***/
-void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
+SilcBool silc_mp_abs(SilcMPInt *dst, SilcMPInt *src);
 
-/****f* silcmath/SilcMPAPI/silc_mp_neg
+/****f* silcmath/silc_mp_neg
  *
  * SYNOPSIS
  *
- *    void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
+ *    SilcBool silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
  *
  * DESCRIPTION
  *
  *    Negate `src' and save the result to `dst'.
  *
  ***/
-void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
+SilcBool silc_mp_neg(SilcMPInt *dst, SilcMPInt *src);
 
-/****f* silcmath/SilcMPAPI/silc_mp_and
+/****f* silcmath/silc_mp_and
  *
  * SYNOPSIS
  *
- *    void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
- *    Logical and operator. The result is saved to `dst'.
+ *    Bitwise AND operator. The result is saved to `dst'.
  *
  ***/
-void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_or
+/****f* silcmath/silc_mp_or
  *
  * SYNOPSIS
  *
- *    void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
- *    Logical inclusive OR operator. The result is saved to `dst'.
+ *    Bitwise inclusive OR operator. The result is saved to `dst'.
  *
  ***/
-void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+SilcBool silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
-/****f* silcmath/SilcMPAPI/silc_mp_or
+/****f* silcmath/silc_mp_xor
  *
  * SYNOPSIS
  *
- *    void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+ *    SilcBool silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  *
  * DESCRIPTION
  *
- *    Logical exclusive OR operator. The result is saved to `dst'.
+ *    Bitwise exclusive OR operator. The result is saved to `dst'.
+ *
+ ***/
+SilcBool silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+
+/* Utility functions */
+
+/****f* silcmath/silc_mp_format
+ *
+ * SYNOPSIS
+ *
+ *    int silc_mp_format(SilcStack stack, SilcBuffer buffer,
+ *                       void *value, void *context)
+ *
+ * DESCRIPTION
+ *
+ *    MP integer encoding function to be used with silc_buffer_format and
+ *    SILC_STR_FUNC formatter.  The encoded data is of following format:
+ *
+ *      SILC_STR_UINT32, integer_len,
+ *      SILC_STR_DATA    integer_data
+ *
+ * EXAMPLE
+ *
+ *    silc_buffer_format(buf,
+ *                       SILC_STR_FUNC(silc_mp_format, mpint, NULL),
+ *                       SILC_STR_END);
+ *
+ ***/
+int silc_mp_format(SilcStack stack, SilcBuffer buffer,
+                  void *value, void *context);
+
+/****f* silcmath/silc_mp_unformat
+ *
+ * SYNOPSIS
+ *
+ *    int silc_mp_unformat(SilcStack stack, SilcBuffer buffer,
+ *                         void **value, void *context)
+ *
+ * DESCRIPTION
+ *
+ *    MP integer decoding function to be used with silc_buffer_unformat and
+ *    SILC_STR_FUNC unformatter.  This function expects that the length of
+ *    the integer is encoded as 32-bit integer and precedes the integer
+ *    data.
+ *
+ * EXAMPLE
+ *
+ *    SilcMPint mp_ptr;
+ *
+ *    silc_mp_init(&mpint);
+ *    mp_ptr = &mpint;
+ *
+ *    silc_buffer_unformat(buf,
+ *                         SILC_STR_FUNC(silc_mp_unformat, &mp_ptr, NULL),
+ *                         SILC_STR_END);
  *
  ***/
-void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
+int silc_mp_unformat(SilcStack stack, SilcBuffer buffer,
+                    void **value, void *context);
 
 #endif