24158e069561514c0c221fc3309c38a5a3dedb6b
[platform/upstream/nettle.git] / bignum.h
1 /* bignum.h
2
3    Bignum operations that are missing from gmp.
4
5    Copyright (C) 2001 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33  
34 #ifndef NETTLE_BIGNUM_H_INCLUDED
35 #define NETTLE_BIGNUM_H_INCLUDED
36
37 #include "nettle-meta.h"
38
39 #include "nettle-types.h"
40
41 /* For NETTLE_USE_MINI_GMP */
42 #include "version.h"
43
44 #if NETTLE_USE_MINI_GMP
45 # include "mini-gmp.h"
46
47 # define GMP_NUMB_MASK (~(mp_limb_t) 0)
48
49 /* Functions missing in older gmp versions, and checked for with ifdef */
50 # define mpz_limbs_read mpz_limbs_read
51 # define mpn_copyd mpn_copyd
52 # define mpn_sqr mpn_sqr
53 # define mpz_combit mpz_combit
54 # define mpz_import mpz_import
55 # define mpz_export mpz_export
56 #else
57 # include <gmp.h>
58 #endif
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 /* Size needed for signed encoding, including extra sign byte if
65  * necessary. */
66 size_t
67 nettle_mpz_sizeinbase_256_s(const mpz_t x);
68
69 /* Size needed for unsigned encoding */
70 size_t
71 nettle_mpz_sizeinbase_256_u(const mpz_t x);
72
73 /* Writes an integer as length octets, using big endian byte order,
74  * and two's complement for negative numbers. */
75 void
76 nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
77
78 /* Reads a big endian, two's complement, integer. */
79 void
80 nettle_mpz_set_str_256_s(mpz_t x,
81                          size_t length, const uint8_t *s);
82
83 void
84 nettle_mpz_init_set_str_256_s(mpz_t x,
85                               size_t length, const uint8_t *s);
86
87 /* Similar, but for unsigned format. These function don't interpret
88  * the most significant bit as the sign. */
89 void
90 nettle_mpz_set_str_256_u(mpz_t x,
91                          size_t length, const uint8_t *s);
92
93 void
94 nettle_mpz_init_set_str_256_u(mpz_t x,
95                               size_t length, const uint8_t *s);
96
97 /* Returns a uniformly distributed random number 0 <= x < 2^n */
98 void
99 nettle_mpz_random_size(mpz_t x,
100                        void *ctx, nettle_random_func *random,
101                        unsigned bits);
102
103 /* Returns a number x, almost uniformly random in the range
104  * 0 <= x < n. */
105 void
106 nettle_mpz_random(mpz_t x, 
107                   void *ctx, nettle_random_func *random,
108                   const mpz_t n);
109
110 void
111 nettle_random_prime(mpz_t p, unsigned bits, int top_bits_set,
112                     void *ctx, nettle_random_func *random,
113                     void *progress_ctx, nettle_progress_func *progress);
114
115 void
116 _nettle_generate_pocklington_prime (mpz_t p, mpz_t r,
117                                     unsigned bits, int top_bits_set, 
118                                     void *ctx, nettle_random_func *random, 
119                                     const mpz_t p0,
120                                     const mpz_t q,
121                                     const mpz_t p0q);
122   
123 /* sexp parsing */
124 struct sexp_iterator;
125
126 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
127  * Implies sexp_iterator_next. */
128 int
129 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
130
131
132 /* der parsing */
133 struct asn1_der_iterator;
134
135 int
136 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
137                            mpz_t x, unsigned max_bits);
138
139 #ifdef __cplusplus
140 }
141 #endif
142
143 #endif /* NETTLE_BIGNUM_H_INCLUDED */