Improve GTK-DOC coverage.
[gnutls.git] / tests / mpi.c
blob73c33810130195b1e99de9c7662a7d05d69a46b5
1 /*
2 * Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
6 * This file is part of GNUTLS.
8 * GNUTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GNUTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNUTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdio.h>
29 #include "utils.h"
30 #include "../lib/gnutls_int.h"
31 #include "../lib/gnutls_mpi.h"
32 #include "../lib/gnutls_errors.h"
33 #include "../lib/debug.h"
35 static void
36 tls_log_func (int level, const char *str)
38 fprintf (stderr, "|<%d>| %s", level, str);
41 #define RND_BITS 510 /* not multiple of 8 */
42 void
43 doit (void)
45 int rc;
46 bigint_t n1, n2, n3, n4;
48 gnutls_global_init ();
50 gnutls_global_set_log_function (tls_log_func);
51 gnutls_global_set_log_level (99);
53 n1 = _gnutls_mpi_new (1000);
54 if (n1 == NULL)
55 fail ("mpi_new failed\n");
57 n2 = _gnutls_mpi_set_ui (NULL, 2);
58 if (n2 == NULL)
59 fail ("mpi_set_ui failed\n");
61 n3 = _gnutls_mpi_set_ui (NULL, 5);
62 if (n3 == NULL)
63 fail ("mpi_set_ui failed\n");
65 _gnutls_mpi_randomize (n1, RND_BITS, GNUTLS_RND_NONCE);
67 _gnutls_mpi_log ("rand:", n1);
69 rc = _gnutls_mpi_get_nbits (n1);
70 if (rc > RND_BITS)
71 fail ("mpi_get_nbits failed... returned %d\n", rc);
73 n4 = _gnutls_mpi_addm (NULL, n1, n3, n2);
74 if (n4 == NULL)
75 fail ("mpi_set_ui failed\n");
77 if (_gnutls_mpi_cmp_ui (n4, 0) != 0 && _gnutls_mpi_cmp_ui (n4, 1) != 0)
78 fail ("mpi_cmp_ui failed\n");
80 _gnutls_mpi_release (&n1);
81 _gnutls_mpi_release (&n2);
82 _gnutls_mpi_release (&n3);
83 _gnutls_mpi_release (&n4);
85 success ("mpi ops ok\n");