3
/* nettle, low-level cryptographics library
5
* Copyright (C) 2013 Niels Möller
7
* The nettle library is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU Lesser General Public License as published by
9
* the Free Software Foundation; either version 2.1 of the License, or (at your
10
* option) any later version.
12
* The nettle library is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
* License for more details.
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with the nettle library; see the file COPYING.LIB. If not, write to
19
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
#ifndef NETTLE_GMP_GLUE_H_INCLUDED
24
#define NETTLE_GMP_GLUE_H_INCLUDED
28
#include "nettle-stdint.h"
31
#define GMP_HAVE_mpz_limbs_read 1
33
#define GMP_HAVE_mpz_limbs_read 0
37
#define GMP_HAVE_mpn_copyd 1
39
#define GMP_HAVE_mpn_copyd 0
43
#if !GMP_HAVE_mpz_limbs_read
44
#define mpz_limbs_read _nettle_mpz_limbs_read
45
#define mpz_limbs_write _nettle_mpz_limbs_write
46
#define mpz_limbs_modify _nettle_mpz_limbs_modify
47
#define mpz_limbs_finish _nettle_mpz_limbs_finish
48
#define mpz_roinit_n _nettle_mpz_roinit_n
51
#if !GMP_HAVE_mpn_copyd
52
#define mpn_copyd _nettle_mpn_copyd
53
#define mpn_copyi _nettle_mpn_copyi
54
#define mpn_zero _nettle_mpn_zero
58
#define mpn_sqr(rp, ap, n) mpn_mul_n((rp), (ap), (ap), (n))
61
#define mpz_limbs_cmp _nettle_mpz_limbs_cmp
62
#define mpz_limbs_read_n _nettle_mpz_limbs_read_n
63
#define mpz_limbs_copy _nettle_mpz_limbs_copy
64
#define mpz_set_n _nettle_mpz_set_n
65
#define mpn_set_base256 _nettle_mpn_set_base256
66
#define gmp_alloc_limbs _nettle_gmp_alloc_limbs
67
#define gmp_free_limbs _nettle_gmp_free_limbs
69
/* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
71
# define cnd_add_n(cnd, rp, ap, n) mpn_cnd_add_n ((cnd), (rp), (rp), (ap), (n))
72
# define cnd_sub_n(cnd, rp, ap, n) mpn_cnd_sub_n ((cnd), (rp), (rp), (ap), (n))
74
# define cnd_add_n(cnd, rp, ap, n) mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
75
# define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
78
/* Some functions for interfacing between mpz and mpn code. Signs of
79
the mpz numbers are generally ignored. */
81
#if !GMP_HAVE_mpz_limbs_read
82
/* Read access to mpz numbers. */
84
/* Return limb pointer, for read-only operations. Use mpz_size to get
85
the number of limbs. */
87
mpz_limbs_read (const mpz_srcptr x);
89
/* Write access to mpz numbers. */
91
/* Get a limb pointer for writing, previous contents may be
94
mpz_limbs_write (mpz_ptr x, mp_size_t n);
96
/* Get a limb pointer for writing, previous contents is intact. */
98
mpz_limbs_modify (mpz_ptr x, mp_size_t n);
102
mpz_limbs_finish (mpz_ptr x, mp_size_t n);
104
/* Using an mpn number as an mpz. Can be used for read-only access
105
only. x must not be cleared or reallocated. */
107
mpz_roinit_n (mpz_ptr x, const mp_limb_t *xp, mp_size_t xs);
109
#endif /* !GMP_HAVE_mpz_limbs_read */
111
#if !GMP_HAVE_mpn_copyd
112
/* Copy elements, backwards */
114
mpn_copyd (mp_ptr dst, mp_srcptr src, mp_size_t n);
116
/* Copy elements, forwards */
118
mpn_copyi (mp_ptr dst, mp_srcptr src, mp_size_t n);
122
mpn_zero (mp_ptr ptr, mp_size_t n);
123
#endif /* !GMP_HAVE_mpn_copyd */
125
/* Convenience functions */
127
mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
129
/* Get a pointer to an n limb area, for read-only operation. n must be
130
greater or equal to the current size, and the mpz is zero-padded if
133
mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
135
/* Copy limbs, with zero-padding. */
136
/* FIXME: Reorder arguments, on the theory that the first argument of
137
an _mpz_* fucntion should be an mpz_t? Or rename to _mpz_get_limbs,
138
with argument order consistent with mpz_get_*. */
140
mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
143
mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
145
/* Like mpn_set_str, but always writes rn limbs. If input is larger,
146
higher bits are ignored. */
148
mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
149
const uint8_t *xp, size_t xn);
153
gmp_alloc_limbs (mp_size_t n);
156
gmp_free_limbs (mp_limb_t *p, mp_size_t n);
159
#endif /* NETTLE_GMP_GLUE_H_INCLUDED */