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/.
42 #if !GMP_HAVE_mpz_limbs_read
44 /* This implementation tries to make a minimal use of GMP internals.
45 We access and _mp_size and _mp_d, but not _mp_alloc. */
47 /* Use macros compatible with gmp-impl.h. */
48 #define ABS(x) ((x) >= 0 ? (x) : -(x))
49 #define PTR(x) ((x)->_mp_d)
50 #define SIZ(x) ((x)->_mp_size)
51 #define ABSIZ(x) ABS (SIZ (x))
53 #define MPN_NORMALIZE(xp, xn) do { \
54 while ( (xn) > 0 && (xp)[xn-1] == 0) \
58 /* NOTE: Makes an unnecessary realloc if allocation is already large
59 enough, but looking at _mp_alloc may break in future GMP
61 #define MPZ_REALLOC(x, n) \
62 (ABSIZ(x) >= (n) ? PTR(x) : (_mpz_realloc ((x),(n)), PTR (x)))
64 #define MPZ_NEWALLOC MPZ_REALLOC
66 /* Read access to mpz numbers. */
68 /* Return limb pointer, for read-only operations. Use mpz_size to get
69 the number of limbs. */
71 mpz_limbs_read (mpz_srcptr x)
76 /* Write access to mpz numbers. */
78 /* Get a limb pointer for writing, previous contents may be
81 mpz_limbs_write (mpz_ptr x, mp_size_t n)
84 return MPZ_NEWALLOC (x, n);
87 /* Get a limb pointer for writing, previous contents is intact. */
89 mpz_limbs_modify (mpz_ptr x, mp_size_t n)
92 return MPZ_REALLOC (x, n);
96 mpz_limbs_finish (mpz_ptr x, mp_size_t n)
99 MPN_NORMALIZE (PTR(x), n);
104 /* Needs some ugly casts. */
106 mpz_roinit_n (mpz_ptr x, const mp_limb_t *xp, mp_size_t xs)
108 mp_size_t xn = ABS (xs);
110 MPN_NORMALIZE (xp, xn);
112 x->_mp_size = xs < 0 ? -xn : xn;
114 x->_mp_d = (mp_limb_t *) xp;
117 #endif /* !GMP_HAVE_mpz_limbs_read */
120 cnd_swap (mp_limb_t cnd, mp_limb_t *ap, mp_limb_t *bp, mp_size_t n)
122 mp_limb_t mask = - (mp_limb_t) (cnd != 0);
124 for (i = 0; i < n; i++)
135 /* Additional convenience functions. */
138 mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn)
140 mp_size_t an = mpz_size (a);
141 assert (mpz_sgn (a) >= 0);
151 return mpn_cmp (mpz_limbs_read(a), bp, an);
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 mp_size_t xn = mpz_size (x);
165 xp = mpz_limbs_modify (x, n);
168 mpn_zero (xp + xn, n - xn);
174 mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n)
176 mp_size_t xn = mpz_size (x);
179 mpn_copyi (xp, mpz_limbs_read (x), xn);
181 mpn_zero (xp + xn, n - xn);
185 mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn)
187 mpn_copyi (mpz_limbs_write (r, xn), xp, xn);
188 mpz_limbs_finish (r, xn);
192 mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
193 const uint8_t *xp, size_t xn)
198 for (xi = xn, out = bits = 0; xi > 0 && rn > 0; )
200 mp_limb_t in = xp[--xi];
201 out |= (in << bits) & GMP_NUMB_MASK;
203 if (bits >= GMP_NUMB_BITS)
208 bits -= GMP_NUMB_BITS;
209 out = in >> (8 - bits);
221 mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
222 const uint8_t *xp, size_t xn)
227 for (xi = 0, out = bits = 0; xi < xn && rn > 0; )
229 mp_limb_t in = xp[xi++];
230 out |= (in << bits) & GMP_NUMB_MASK;
232 if (bits >= GMP_NUMB_BITS)
237 bits -= GMP_NUMB_BITS;
238 out = in >> (8 - bits);
250 mpn_get_base256 (uint8_t *rp, size_t rn,
251 const mp_limb_t *xp, mp_size_t xn)
255 for (bits = in = 0; xn > 0 && rn > 0; )
268 rp[--rn] = old | (in << bits);
270 bits += GMP_NUMB_BITS - 8;
281 mpn_get_base256_le (uint8_t *rp, size_t rn,
282 const mp_limb_t *xp, mp_size_t xn)
286 for (bits = in = 0; xn > 0 && rn > 0; )
300 *rp++ = old | (in << bits);
303 bits += GMP_NUMB_BITS - 8;
315 gmp_alloc_limbs (mp_size_t n)
318 void *(*alloc_func)(size_t);
322 mp_get_memory_functions (&alloc_func, NULL, NULL);
323 return (mp_limb_t *) alloc_func ( (size_t) n * sizeof(mp_limb_t));
327 gmp_free_limbs (mp_limb_t *p, mp_size_t n)
329 void (*free_func)(void *, size_t);
332 mp_get_memory_functions (NULL, NULL, &free_func);
334 free_func (p, (size_t) n * sizeof(mp_limb_t));
340 void *(*alloc_func)(size_t);
343 mp_get_memory_functions(&alloc_func, NULL, NULL);
345 return alloc_func (n);
349 gmp_free(void *p, size_t n)
351 void (*free_func)(void *, size_t);
354 mp_get_memory_functions (NULL, NULL, &free_func);
356 free_func (p, (size_t) n);