3 Copyright (C) 2013 Niels Möller
4 Copyright (C) 2013 Red Hat
6 This file is part of GNU Nettle.
8 GNU Nettle is free software: you can redistribute it and/or
9 modify it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free
18 Software Foundation; either version 2 of the License, or (at your
19 option) any later version.
21 or both in parallel, as here.
23 GNU Nettle is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received copies of the GNU General Public License and
29 the GNU Lesser General Public License along with this program. If
30 not, see http://www.gnu.org/licenses/.
33 #ifndef NETTLE_GMP_GLUE_H_INCLUDED
34 #define NETTLE_GMP_GLUE_H_INCLUDED
39 #define GMP_HAVE_mpz_limbs_read 1
41 #define GMP_HAVE_mpz_limbs_read 0
45 #define GMP_HAVE_mpn_copyd 1
47 #define GMP_HAVE_mpn_copyd 0
51 #if !GMP_HAVE_mpz_limbs_read
52 #define mpz_limbs_read _nettle_mpz_limbs_read
53 #define mpz_limbs_write _nettle_mpz_limbs_write
54 #define mpz_limbs_modify _nettle_mpz_limbs_modify
55 #define mpz_limbs_finish _nettle_mpz_limbs_finish
56 #define mpz_roinit_n _nettle_mpz_roinit_n
59 #if !GMP_HAVE_mpn_copyd
60 #define mpn_copyd _nettle_mpn_copyd
61 #define mpn_copyi _nettle_mpn_copyi
62 #define mpn_zero _nettle_mpn_zero
66 #define mpn_sqr(rp, ap, n) mpn_mul_n((rp), (ap), (ap), (n))
69 #define cnd_swap _nettle_cnd_swap
70 #define mpz_limbs_cmp _nettle_mpz_limbs_cmp
71 #define mpz_limbs_read_n _nettle_mpz_limbs_read_n
72 #define mpz_limbs_copy _nettle_mpz_limbs_copy
73 #define mpz_set_n _nettle_mpz_set_n
74 #define mpn_set_base256 _nettle_mpn_set_base256
75 #define mpn_set_base256_le _nettle_mpn_set_base256_le
76 #define mpn_get_base256_le _nettle_mpn_get_base256_le
77 #define gmp_alloc_limbs _nettle_gmp_alloc_limbs
78 #define gmp_free_limbs _nettle_gmp_free_limbs
79 #define gmp_free _nettle_gmp_free
80 #define gmp_alloc _nettle_gmp_alloc
82 #define TMP_GMP_DECL(name, type) type *name; \
83 size_t tmp_##name##_size
84 #define TMP_GMP_ALLOC(name, size) do { \
85 tmp_##name##_size = (size); \
86 (name) = gmp_alloc(sizeof (*name) * (size)); \
88 #define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
91 /* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
93 # define cnd_add_n(cnd, rp, ap, n) mpn_cnd_add_n ((cnd), (rp), (rp), (ap), (n))
94 # define cnd_sub_n(cnd, rp, ap, n) mpn_cnd_sub_n ((cnd), (rp), (rp), (ap), (n))
96 # define cnd_add_n(cnd, rp, ap, n) mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
97 # define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
100 /* Some functions for interfacing between mpz and mpn code. Signs of
101 the mpz numbers are generally ignored. */
103 #if !GMP_HAVE_mpz_limbs_read
104 /* Read access to mpz numbers. */
106 /* Return limb pointer, for read-only operations. Use mpz_size to get
107 the number of limbs. */
109 mpz_limbs_read (const mpz_srcptr x);
111 /* Write access to mpz numbers. */
113 /* Get a limb pointer for writing, previous contents may be
116 mpz_limbs_write (mpz_ptr x, mp_size_t n);
118 /* Get a limb pointer for writing, previous contents is intact. */
120 mpz_limbs_modify (mpz_ptr x, mp_size_t n);
124 mpz_limbs_finish (mpz_ptr x, mp_size_t n);
126 /* Using an mpn number as an mpz. Can be used for read-only access
127 only. x must not be cleared or reallocated. */
129 mpz_roinit_n (mpz_ptr x, const mp_limb_t *xp, mp_size_t xs);
131 #endif /* !GMP_HAVE_mpz_limbs_read */
133 #if !GMP_HAVE_mpn_copyd
134 /* Copy elements, backwards */
136 mpn_copyd (mp_ptr dst, mp_srcptr src, mp_size_t n);
138 /* Copy elements, forwards */
140 mpn_copyi (mp_ptr dst, mp_srcptr src, mp_size_t n);
144 mpn_zero (mp_ptr ptr, mp_size_t n);
145 #endif /* !GMP_HAVE_mpn_copyd */
148 cnd_swap (mp_limb_t cnd, mp_limb_t *ap, mp_limb_t *bp, mp_size_t n);
150 /* Convenience functions */
152 mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
154 /* Get a pointer to an n limb area, for read-only operation. n must be
155 greater or equal to the current size, and the mpz is zero-padded if
158 mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
160 /* Copy limbs, with zero-padding. */
161 /* FIXME: Reorder arguments, on the theory that the first argument of
162 an _mpz_* function should be an mpz_t? Or rename to _mpz_get_limbs,
163 with argument order consistent with mpz_get_*. */
165 mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
168 mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
170 /* Like mpn_set_str, but always writes rn limbs. If input is larger,
171 higher bits are ignored. */
173 mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
174 const uint8_t *xp, size_t xn);
177 mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
178 const uint8_t *xp, size_t xn);
181 mpn_get_base256_le (uint8_t *rp, size_t rn,
182 const mp_limb_t *xp, mp_size_t xn);
186 gmp_alloc_limbs (mp_size_t n);
189 gmp_free_limbs (mp_limb_t *p, mp_size_t n);
191 void *gmp_alloc(size_t n);
192 void gmp_free(void *p, size_t n);
194 #endif /* NETTLE_GMP_GLUE_H_INCLUDED */