Tizen 2.0 Release
[external/libgnutls26.git] / tests / mpi.c
1 /*
2  * Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
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"
34
35 static void
36 tls_log_func (int level, const char *str)
37 {
38   fprintf (stderr, "|<%d>| %s", level, str);
39 }
40
41 #define RND_BITS 510            /* not multiple of 8 */
42 void
43 doit (void)
44 {
45   int rc;
46   bigint_t n1, n2, n3, n4;
47
48   gnutls_global_init ();
49
50   gnutls_global_set_log_function (tls_log_func);
51   if (debug)
52     gnutls_global_set_log_level (99);
53
54   n1 = _gnutls_mpi_new (1000);
55   if (n1 == NULL)
56     fail ("mpi_new failed\n");
57
58   n2 = _gnutls_mpi_set_ui (NULL, 2);
59   if (n2 == NULL)
60     fail ("mpi_set_ui failed\n");
61
62   n3 = _gnutls_mpi_set_ui (NULL, 5);
63   if (n3 == NULL)
64     fail ("mpi_set_ui failed\n");
65
66   _gnutls_mpi_randomize (n1, RND_BITS, GNUTLS_RND_NONCE);
67
68   _gnutls_mpi_log ("rand:", n1);
69
70   rc = _gnutls_mpi_get_nbits (n1);
71   if (rc > RND_BITS)
72     fail ("mpi_get_nbits failed... returned %d\n", rc);
73
74   n4 = _gnutls_mpi_addm (NULL, n1, n3, n2);
75   if (n4 == NULL)
76     fail ("mpi_set_ui failed\n");
77
78   if (_gnutls_mpi_cmp_ui (n4, 0) != 0 && _gnutls_mpi_cmp_ui (n4, 1) != 0)
79     fail ("mpi_cmp_ui failed\n");
80
81   _gnutls_mpi_release (&n1);
82   _gnutls_mpi_release (&n2);
83   _gnutls_mpi_release (&n3);
84   _gnutls_mpi_release (&n4);
85
86   success ("mpi ops ok\n");
87 }