3 Bignum operations that are missing from gmp.
5 Copyright (C) 2001 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
34 #ifndef NETTLE_BIGNUM_H_INCLUDED
35 #define NETTLE_BIGNUM_H_INCLUDED
37 #include "nettle-meta.h"
39 #include "nettle-types.h"
41 /* For NETTLE_USE_MINI_GMP */
44 #if NETTLE_USE_MINI_GMP
45 # include "mini-gmp.h"
47 # define GMP_NUMB_MASK (~(mp_limb_t) 0)
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
64 /* Size needed for signed encoding, including extra sign byte if
67 nettle_mpz_sizeinbase_256_s(const mpz_t x);
69 /* Size needed for unsigned encoding */
71 nettle_mpz_sizeinbase_256_u(const mpz_t x);
73 /* Writes an integer as length octets, using big endian byte order,
74 * and two's complement for negative numbers. */
76 nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
78 /* Reads a big endian, two's complement, integer. */
80 nettle_mpz_set_str_256_s(mpz_t x,
81 size_t length, const uint8_t *s);
84 nettle_mpz_init_set_str_256_s(mpz_t x,
85 size_t length, const uint8_t *s);
87 /* Similar, but for unsigned format. These function don't interpret
88 * the most significant bit as the sign. */
90 nettle_mpz_set_str_256_u(mpz_t x,
91 size_t length, const uint8_t *s);
94 nettle_mpz_init_set_str_256_u(mpz_t x,
95 size_t length, const uint8_t *s);
97 /* Returns a uniformly distributed random number 0 <= x < 2^n */
99 nettle_mpz_random_size(mpz_t x,
100 void *ctx, nettle_random_func *random,
103 /* Returns a number x, almost uniformly random in the range
106 nettle_mpz_random(mpz_t x,
107 void *ctx, nettle_random_func *random,
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);
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,
124 struct sexp_iterator;
126 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
127 * Implies sexp_iterator_next. */
129 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
133 struct asn1_der_iterator;
136 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
137 mpz_t x, unsigned max_bits);
143 #endif /* NETTLE_BIGNUM_H_INCLUDED */