Update copyright headers for dual licensing.
[gd/nettle] / 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 <gmp.h>
40 #include "nettle-types.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 /* Size needed for signed encoding, including extra sign byte if
47  * necessary. */
48 size_t
49 nettle_mpz_sizeinbase_256_s(const mpz_t x);
50
51 /* Size needed for unsigned encoding */
52 size_t
53 nettle_mpz_sizeinbase_256_u(const mpz_t x);
54
55 /* Writes an integer as length octets, using big endian byte order,
56  * and two's complement for negative numbers. */
57 void
58 nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
59
60 /* Reads a big endian, two's complement, integer. */
61 void
62 nettle_mpz_set_str_256_s(mpz_t x,
63                          size_t length, const uint8_t *s);
64
65 void
66 nettle_mpz_init_set_str_256_s(mpz_t x,
67                               size_t length, const uint8_t *s);
68
69 /* Similar, but for unsigned format. These function don't interpret
70  * the most significant bit as the sign. */
71 void
72 nettle_mpz_set_str_256_u(mpz_t x,
73                          size_t length, const uint8_t *s);
74
75 void
76 nettle_mpz_init_set_str_256_u(mpz_t x,
77                               size_t length, const uint8_t *s);
78
79 /* Returns a uniformly distributed random number 0 <= x < 2^n */
80 void
81 nettle_mpz_random_size(mpz_t x,
82                        void *ctx, nettle_random_func *random,
83                        unsigned bits);
84
85 /* Returns a number x, almost uniformly random in the range
86  * 0 <= x < n. */
87 void
88 nettle_mpz_random(mpz_t x, 
89                   void *ctx, nettle_random_func *random,
90                   const mpz_t n);
91
92 void
93 nettle_next_prime(mpz_t p, mpz_t n, unsigned count, unsigned prime_limit,
94                   void *progress_ctx, nettle_progress_func *progress);
95
96 void
97 nettle_random_prime(mpz_t p, unsigned bits, int top_bits_set,
98                     void *ctx, nettle_random_func *random,
99                     void *progress_ctx, nettle_progress_func *progress);
100
101 void
102 _nettle_generate_pocklington_prime (mpz_t p, mpz_t r,
103                                     unsigned bits, int top_bits_set, 
104                                     void *ctx, nettle_random_func *random, 
105                                     const mpz_t p0,
106                                     const mpz_t q,
107                                     const mpz_t p0q);
108   
109 /* sexp parsing */
110 struct sexp_iterator;
111
112 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
113  * Implies sexp_iterator_next. */
114 int
115 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
116
117
118 /* der parsing */
119 struct asn1_der_iterator;
120
121 int
122 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
123                            mpz_t x, unsigned max_bits);
124
125 #ifdef __cplusplus
126 }
127 #endif
128
129 #endif /* NETTLE_BIGNUM_H_INCLUDED */