Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:14

0001 /* bignum.h
0002 
0003    Bignum operations that are missing from gmp.
0004 
0005    Copyright (C) 2001 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033  
0034 #ifndef NETTLE_BIGNUM_H_INCLUDED
0035 #define NETTLE_BIGNUM_H_INCLUDED
0036 
0037 #include "nettle-types.h"
0038 
0039 /* For NETTLE_USE_MINI_GMP */
0040 #include "version.h"
0041 
0042 #if NETTLE_USE_MINI_GMP
0043 # include "mini-gmp.h"
0044 
0045 # define GMP_NUMB_MASK (~(mp_limb_t) 0)
0046 
0047 /* Side-channel silent powm not available in mini-gmp. */
0048 # define mpz_powm_sec mpz_powm
0049 #else
0050 # include <gmp.h>
0051 #endif
0052 
0053 #ifdef __cplusplus
0054 extern "C" {
0055 #endif
0056 
0057 /* Size needed for signed encoding, including extra sign byte if
0058  * necessary. */
0059 size_t
0060 nettle_mpz_sizeinbase_256_s(const mpz_t x);
0061 
0062 /* Size needed for unsigned encoding */
0063 size_t
0064 nettle_mpz_sizeinbase_256_u(const mpz_t x);
0065 
0066 /* Writes an integer as length octets, using big endian byte order,
0067  * and two's complement for negative numbers. */
0068 void
0069 nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
0070 
0071 /* Reads a big endian, two's complement, integer. */
0072 void
0073 nettle_mpz_set_str_256_s(mpz_t x,
0074              size_t length, const uint8_t *s);
0075 
0076 void
0077 nettle_mpz_init_set_str_256_s(mpz_t x,
0078                   size_t length, const uint8_t *s);
0079 
0080 /* Similar, but for unsigned format. These function don't interpret
0081  * the most significant bit as the sign. */
0082 void
0083 nettle_mpz_set_str_256_u(mpz_t x,
0084              size_t length, const uint8_t *s);
0085 
0086 void
0087 nettle_mpz_init_set_str_256_u(mpz_t x,
0088                   size_t length, const uint8_t *s);
0089 
0090 /* Returns a uniformly distributed random number 0 <= x < 2^n */
0091 void
0092 nettle_mpz_random_size(mpz_t x,
0093                void *ctx, nettle_random_func *random,
0094                unsigned bits);
0095 
0096 /* Returns a number x, almost uniformly random in the range
0097  * 0 <= x < n. */
0098 void
0099 nettle_mpz_random(mpz_t x, 
0100           void *ctx, nettle_random_func *random,
0101           const mpz_t n);
0102 
0103 void
0104 nettle_random_prime(mpz_t p, unsigned bits, int top_bits_set,
0105             void *ctx, nettle_random_func *random,
0106             void *progress_ctx, nettle_progress_func *progress);
0107 
0108   
0109 /* sexp parsing */
0110 struct sexp_iterator;
0111 
0112 /* If LIMIT is non-zero, the number must be at most LIMIT bits.
0113  * Implies sexp_iterator_next. */
0114 int
0115 nettle_mpz_set_sexp(mpz_t x, unsigned limit, struct sexp_iterator *i);
0116 
0117 
0118 /* der parsing */
0119 struct asn1_der_iterator;
0120 
0121 int
0122 nettle_asn1_der_get_bignum(struct asn1_der_iterator *iterator,
0123                mpz_t x, unsigned max_bits);
0124 
0125 #ifdef __cplusplus
0126 }
0127 #endif
0128 
0129 #endif /* NETTLE_BIGNUM_H_INCLUDED */