7713757c4dbace397b9b9a33748227d3a647793f
[platform/upstream/nettle.git] / gmp-glue.h
1 /* gmp-glue.h
2
3    Copyright (C) 2013 Niels Möller
4    Copyright (C) 2013 Red Hat
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
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.
14
15    or
16
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.
20
21    or both in parallel, as here.
22
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.
27
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/.
31 */
32
33 #ifndef NETTLE_GMP_GLUE_H_INCLUDED
34 #define NETTLE_GMP_GLUE_H_INCLUDED
35
36 #include "bignum.h"
37
38 #ifdef mpz_limbs_read
39 #define GMP_HAVE_mpz_limbs_read 1
40 #else
41 #define GMP_HAVE_mpz_limbs_read 0
42 #endif
43
44 #ifdef mpn_copyd
45 #define GMP_HAVE_mpn_copyd 1
46 #else
47 #define GMP_HAVE_mpn_copyd 0
48 #endif
49
50 /* Name mangling. */
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
57 #endif
58
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
63 #endif
64
65 #ifndef mpn_sqr
66 #define mpn_sqr(rp, ap, n) mpn_mul_n((rp), (ap), (ap), (n))
67 #endif
68
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
81
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));        \
87   } while (0)
88 #define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
89
90
91 /* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
92 #ifdef mpn_cnd_add_n
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))
95 #else
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)
98 #endif
99
100 /* Some functions for interfacing between mpz and mpn code. Signs of
101    the mpz numbers are generally ignored. */
102
103 #if !GMP_HAVE_mpz_limbs_read
104 /* Read access to mpz numbers. */
105
106 /* Return limb pointer, for read-only operations. Use mpz_size to get
107    the number of limbs. */
108 const mp_limb_t *
109 mpz_limbs_read (const mpz_srcptr x);
110
111 /* Write access to mpz numbers. */
112
113 /* Get a limb pointer for writing, previous contents may be
114    destroyed. */
115 mp_limb_t *
116 mpz_limbs_write (mpz_ptr x, mp_size_t n);
117
118 /* Get a limb pointer for writing, previous contents is intact. */
119 mp_limb_t *
120 mpz_limbs_modify (mpz_ptr x, mp_size_t n);
121
122 /* Update size. */
123 void
124 mpz_limbs_finish (mpz_ptr x, mp_size_t n);
125
126 /* Using an mpn number as an mpz. Can be used for read-only access
127    only. x must not be cleared or reallocated. */
128 mpz_srcptr
129 mpz_roinit_n (mpz_ptr x, const mp_limb_t *xp, mp_size_t xs);
130
131 #endif /* !GMP_HAVE_mpz_limbs_read */
132
133 #if !GMP_HAVE_mpn_copyd
134 /* Copy elements, backwards */
135 void
136 mpn_copyd (mp_ptr dst, mp_srcptr src, mp_size_t n);
137
138 /* Copy elements, forwards */
139 void
140 mpn_copyi (mp_ptr dst, mp_srcptr src, mp_size_t n);
141
142 /* Zero elements */
143 void
144 mpn_zero (mp_ptr ptr, mp_size_t n);
145 #endif /* !GMP_HAVE_mpn_copyd */
146
147 void
148 cnd_swap (mp_limb_t cnd, mp_limb_t *ap, mp_limb_t *bp, mp_size_t n);
149
150 /* Convenience functions */
151 int
152 mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
153
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
156    needed. */
157 const mp_limb_t *
158 mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
159
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_*. */
164 void
165 mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
166
167 void
168 mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
169
170 /* Like mpn_set_str, but always writes rn limbs. If input is larger,
171    higher bits are ignored. */
172 void
173 mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
174                  const uint8_t *xp, size_t xn);
175
176 void
177 mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
178                     const uint8_t *xp, size_t xn);
179
180 void
181 mpn_get_base256_le (uint8_t *rp, size_t rn,
182                     const mp_limb_t *xp, mp_size_t xn);
183
184
185 mp_limb_t *
186 gmp_alloc_limbs (mp_size_t n);
187
188 void
189 gmp_free_limbs (mp_limb_t *p, mp_size_t n);
190
191 void *gmp_alloc(size_t n);
192 void gmp_free(void *p, size_t n);
193
194 #endif /* NETTLE_GMP_GLUE_H_INCLUDED */