Lines Matching +full:- +full:a +full:-
1 /* mpiutil.ac - Utility functions for MPI
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * You should have received a copy of the GNU General Public License
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "mpi-internal.h"
56 return -EFAULT; in mpi_init()
59 constants[idx]->flags = (16|32); in mpi_init()
66 /* Return a constant MPI descripbed by NO which is one of the
81 * Note: It was a bad idea to use the number of limbs to allocate
82 * because on a alpha the limbs are large but we normally need
83 * integers of n bits - So we should change this to bits (or bytes).
85 * But mpi_alloc is used in a lot of places :-)
89 MPI a; in mpi_alloc() local
91 a = kmalloc(sizeof *a, GFP_KERNEL); in mpi_alloc()
92 if (!a) in mpi_alloc()
93 return a; in mpi_alloc()
96 a->d = mpi_alloc_limb_space(nlimbs); in mpi_alloc()
97 if (!a->d) { in mpi_alloc()
98 kfree(a); in mpi_alloc()
102 a->d = NULL; in mpi_alloc()
105 a->alloced = nlimbs; in mpi_alloc()
106 a->nlimbs = 0; in mpi_alloc()
107 a->sign = 0; in mpi_alloc()
108 a->flags = 0; in mpi_alloc()
109 a->nbits = 0; in mpi_alloc()
110 return a; in mpi_alloc()
124 void mpi_free_limb_space(mpi_ptr_t a) in mpi_free_limb_space() argument
126 if (!a) in mpi_free_limb_space()
129 kfree_sensitive(a); in mpi_free_limb_space()
132 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs) in mpi_assign_limb_space() argument
134 mpi_free_limb_space(a->d); in mpi_assign_limb_space()
135 a->d = ap; in mpi_assign_limb_space()
136 a->alloced = nlimbs; in mpi_assign_limb_space()
140 * Resize the array of A to NLIMBS. the additional space is cleared
143 int mpi_resize(MPI a, unsigned nlimbs) in mpi_resize() argument
147 if (nlimbs <= a->alloced) in mpi_resize()
150 if (a->d) { in mpi_resize()
153 return -ENOMEM; in mpi_resize()
154 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t)); in mpi_resize()
155 kfree_sensitive(a->d); in mpi_resize()
156 a->d = p; in mpi_resize()
158 a->d = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL); in mpi_resize()
159 if (!a->d) in mpi_resize()
160 return -ENOMEM; in mpi_resize()
162 a->alloced = nlimbs; in mpi_resize()
166 void mpi_clear(MPI a) in mpi_clear() argument
168 if (!a) in mpi_clear()
170 a->nlimbs = 0; in mpi_clear()
171 a->flags = 0; in mpi_clear()
175 void mpi_free(MPI a) in mpi_free() argument
177 if (!a) in mpi_free()
180 if (a->flags & 4) in mpi_free()
181 kfree_sensitive(a->d); in mpi_free()
183 mpi_free_limb_space(a->d); in mpi_free()
185 if (a->flags & ~7) in mpi_free()
187 kfree(a); in mpi_free()
195 MPI mpi_copy(MPI a) in mpi_copy() argument
200 if (a) { in mpi_copy()
201 b = mpi_alloc(a->nlimbs); in mpi_copy()
202 b->nlimbs = a->nlimbs; in mpi_copy()
203 b->sign = a->sign; in mpi_copy()
204 b->flags = a->flags; in mpi_copy()
205 b->flags &= ~(16|32); /* Reset the immutable and constant flags. */ in mpi_copy()
206 for (i = 0; i < b->nlimbs; i++) in mpi_copy()
207 b->d[i] = a->d[i]; in mpi_copy()
215 * a value as large as the one given in the argument and allocates it
216 * with the same flags as A.
218 MPI mpi_alloc_like(MPI a) in mpi_alloc_like() argument
222 if (a) { in mpi_alloc_like()
223 b = mpi_alloc(a->nlimbs); in mpi_alloc_like()
224 b->nlimbs = 0; in mpi_alloc_like()
225 b->sign = 0; in mpi_alloc_like()
226 b->flags = a->flags; in mpi_alloc_like()
238 mpi_assign_limb_space(w, u->d, u->alloced); in mpi_snatch()
239 w->nlimbs = u->nlimbs; in mpi_snatch()
240 w->sign = u->sign; in mpi_snatch()
241 w->flags = u->flags; in mpi_snatch()
242 u->alloced = 0; in mpi_snatch()
243 u->nlimbs = 0; in mpi_snatch()
244 u->d = NULL; in mpi_snatch()
253 mpi_size_t usize = u->nlimbs; in mpi_set()
254 int usign = u->sign; in mpi_set()
259 wp = w->d; in mpi_set()
260 up = u->d; in mpi_set()
262 w->nlimbs = usize; in mpi_set()
263 w->flags = u->flags; in mpi_set()
264 w->flags &= ~(16|32); /* Reset the immutable and constant flags. */ in mpi_set()
265 w->sign = usign; in mpi_set()
278 w->d[0] = u; in mpi_set_ui()
279 w->nlimbs = u ? 1 : 0; in mpi_set_ui()
280 w->sign = 0; in mpi_set_ui()
281 w->flags = 0; in mpi_set_ui()
289 w->d[0] = u; in mpi_alloc_set_ui()
290 w->nlimbs = u ? 1 : 0; in mpi_alloc_set_ui()
291 w->sign = 0; in mpi_alloc_set_ui()
296 * Swap the value of A and B, when SWAP is 1.
298 * This implementation should be constant-time regardless of SWAP.
300 void mpi_swap_cond(MPI a, MPI b, unsigned long swap) in mpi_swap_cond() argument
304 mpi_limb_t mask = ((mpi_limb_t)0) - swap; in mpi_swap_cond()
307 if (a->alloced > b->alloced) in mpi_swap_cond()
308 nlimbs = b->alloced; in mpi_swap_cond()
310 nlimbs = a->alloced; in mpi_swap_cond()
311 if (a->nlimbs > nlimbs || b->nlimbs > nlimbs) in mpi_swap_cond()
315 x = mask & (a->d[i] ^ b->d[i]); in mpi_swap_cond()
316 a->d[i] = a->d[i] ^ x; in mpi_swap_cond()
317 b->d[i] = b->d[i] ^ x; in mpi_swap_cond()
320 x = mask & (a->nlimbs ^ b->nlimbs); in mpi_swap_cond()
321 a->nlimbs = a->nlimbs ^ x; in mpi_swap_cond()
322 b->nlimbs = b->nlimbs ^ x; in mpi_swap_cond()
324 x = mask & (a->sign ^ b->sign); in mpi_swap_cond()
325 a->sign = a->sign ^ x; in mpi_swap_cond()
326 b->sign = b->sign ^ x; in mpi_swap_cond()