Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / gmp-glue.h
1 /* gmp-glue.h */
2
3 /* nettle, low-level cryptographics library
4  *
5  * Copyright (C) 2013 Niels Möller
6  *  
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.
11  * 
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.
16  * 
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,
20  * MA 02111-1301, USA.
21  */
22
23 #ifndef NETTLE_GMP_GLUE_H_INCLUDED
24 #define NETTLE_GMP_GLUE_H_INCLUDED
25
26 #include <gmp.h>
27
28 #include "nettle-stdint.h"
29
30 #ifdef mpz_limbs_read
31 #define GMP_HAVE_mpz_limbs_read 1
32 #else
33 #define GMP_HAVE_mpz_limbs_read 0
34 #endif
35
36 #ifdef mpn_copyd
37 #define GMP_HAVE_mpn_copyd 1
38 #else
39 #define GMP_HAVE_mpn_copyd 0
40 #endif
41
42 /* Name mangling. */
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
49 #endif
50
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
55 #endif
56
57 #ifndef mpn_sqr
58 #define mpn_sqr(rp, ap, n) mpn_mul_n((rp), (ap), (ap), (n))
59 #endif
60
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
68
69 /* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
70 #ifdef mpn_cnd_add_n
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))
73 #else
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)
76 #endif
77
78 /* Some functions for interfacing between mpz and mpn code. Signs of
79    the mpz numbers are generally ignored. */
80
81 #if !GMP_HAVE_mpz_limbs_read
82 /* Read access to mpz numbers. */
83
84 /* Return limb pointer, for read-only operations. Use mpz_size to get
85    the number of limbs. */
86 const mp_limb_t *
87 mpz_limbs_read (const mpz_srcptr x);
88
89 /* Write access to mpz numbers. */
90
91 /* Get a limb pointer for writing, previous contents may be
92    destroyed. */
93 mp_limb_t *
94 mpz_limbs_write (mpz_ptr x, mp_size_t n);
95
96 /* Get a limb pointer for writing, previous contents is intact. */
97 mp_limb_t *
98 mpz_limbs_modify (mpz_ptr x, mp_size_t n);
99
100 /* Update size. */
101 void
102 mpz_limbs_finish (mpz_ptr x, mp_size_t n);
103
104 /* Using an mpn number as an mpz. Can be used for read-only access
105    only. x must not be cleared or reallocated. */
106 mpz_srcptr
107 mpz_roinit_n (mpz_ptr x, const mp_limb_t *xp, mp_size_t xs);
108
109 #endif /* !GMP_HAVE_mpz_limbs_read */
110
111 #if !GMP_HAVE_mpn_copyd
112 /* Copy elements, backwards */
113 void
114 mpn_copyd (mp_ptr dst, mp_srcptr src, mp_size_t n);
115
116 /* Copy elements, forwards */
117 void
118 mpn_copyi (mp_ptr dst, mp_srcptr src, mp_size_t n);
119
120 /* Zero elements */
121 void
122 mpn_zero (mp_ptr ptr, mp_size_t n);
123 #endif /* !GMP_HAVE_mpn_copyd */
124
125 /* Convenience functions */
126 int
127 mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
128
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
131    needed. */
132 const mp_limb_t *
133 mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
134
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_*. */
139 void
140 mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
141
142 void
143 mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
144
145 /* Like mpn_set_str, but always writes rn limbs. If input is larger,
146    higher bits are ignored. */
147 void
148 mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
149                  const uint8_t *xp, size_t xn);
150
151
152 mp_limb_t *
153 gmp_alloc_limbs (mp_size_t n);
154
155 void
156 gmp_free_limbs (mp_limb_t *p, mp_size_t n);
157
158
159 #endif /* NETTLE_GMP_GLUE_H_INCLUDED */