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 /* 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
61 /* Size needed for signed encoding, including extra sign byte if
64 nettle_mpz_sizeinbase_256_s(const mpz_t x);
66 /* Size needed for unsigned encoding */
68 nettle_mpz_sizeinbase_256_u(const mpz_t x);
70 /* Writes an integer as length octets, using big endian byte order,
71 * and two's complement for negative numbers. */
73 nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
75 /* Reads a big endian, two's complement, integer. */
77 nettle_mpz_set_str_256_s(mpz_t x,
78 size_t length, const uint8_t *s);
81 nettle_mpz_init_set_str_256_s(mpz_t x,
82 size_t length, const uint8_t *s);
84 /* Similar, but for unsigned format. These function don't interpret
85 * the most significant bit as the sign. */
87 nettle_mpz_set_str_256_u(mpz_t x,
88 size_t length, const uint8_t *s);
91 nettle_mpz_init_set_str_256_u(mpz_t x,
92 size_t length, const uint8_t *s);
94 /* Returns a uniformly distributed random number 0 <= x < 2^n */
96 nettle_mpz_random_size(mpz_t x,
97 void *ctx, nettle_random_func *random,
100 /* Returns a number x, almost uniformly random in the range
103 nettle_mpz_random(mpz_t x,
104 void *ctx, nettle_random_func *random,
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);
114 struct sexp_iterator;
116 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
117 * Implies sexp_iterator_next. */
119 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
123 struct asn1_der_iterator;
126 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
127 mpz_t x, unsigned max_bits);
133 #endif /* NETTLE_BIGNUM_H_INCLUDED */