[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / bignum.h
1 /* bignum.h
2  *
3  * bignum operations that are missing from gmp.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25  
26 #ifndef NETTLE_BIGNUM_H_INCLUDED
27 #define NETTLE_BIGNUM_H_INCLUDED
28
29 #include "nettle-meta.h"
30
31 #include <gmp.h>
32 #include "nettle-types.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* Size needed for signed encoding, including extra sign byte if
39  * necessary. */
40 unsigned
41 nettle_mpz_sizeinbase_256_s(const mpz_t x);
42
43 /* Size needed for unsigned encoding */
44 unsigned
45 nettle_mpz_sizeinbase_256_u(const mpz_t x);
46
47 /* Writes an integer as length octets, using big endian byte order,
48  * and two's complement for negative numbers. */
49 void
50 nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x);
51
52 /* Reads a big endian, two's complement, integer. */
53 void
54 nettle_mpz_set_str_256_s(mpz_t x,
55                          unsigned length, const uint8_t *s);
56
57 void
58 nettle_mpz_init_set_str_256_s(mpz_t x,
59                               unsigned length, const uint8_t *s);
60
61 /* Similar, but for unsigned format. These function don't interpret
62  * the most significant bit as the sign. */
63 void
64 nettle_mpz_set_str_256_u(mpz_t x,
65                          unsigned length, const uint8_t *s);
66
67 void
68 nettle_mpz_init_set_str_256_u(mpz_t x,
69                               unsigned length, const uint8_t *s);
70
71 /* Returns a uniformly distributed random number 0 <= x < 2^n */
72 void
73 nettle_mpz_random_size(mpz_t x,
74                        void *ctx, nettle_random_func random,
75                        unsigned bits);
76
77 /* Returns a number x, almost uniformly random in the range
78  * 0 <= x < n. */
79 void
80 nettle_mpz_random(mpz_t x, 
81                   void *ctx, nettle_random_func random,
82                   const mpz_t n);
83
84 void
85 nettle_next_prime(mpz_t p, mpz_t n, unsigned count, unsigned prime_limit,
86                   void *progress_ctx, nettle_progress_func progress);
87
88 void
89 nettle_random_prime(mpz_t p, unsigned bits, int top_bits_set,
90                     void *ctx, nettle_random_func random,
91                     void *progress_ctx, nettle_progress_func progress);
92
93 void
94 _nettle_generate_pocklington_prime (mpz_t p, mpz_t r,
95                                     unsigned bits, int top_bits_set, 
96                                     void *ctx, nettle_random_func random, 
97                                     const mpz_t p0,
98                                     const mpz_t q,
99                                     const mpz_t p0q);
100   
101 /* sexp parsing */
102 struct sexp_iterator;
103
104 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
105  * Implies sexp_iterator_next. */
106 int
107 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
108
109
110 /* der parsing */
111 struct asn1_der_iterator;
112
113 int
114 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
115                            mpz_t x, unsigned max_bits);
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* NETTLE_BIGNUM_H_INCLUDED */