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