3 * $Id: mp-io.c,v 1.5 2002/10/06 22:52:50 mdw Exp $
5 * Loading and storing of multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2002/10/06 22:52:50 mdw
34 * Pile of changes for supporting two's complement properly.
36 * Revision 1.4 2000/06/17 11:45:09 mdw
37 * Major memory management overhaul. Added arena support. Use the secure
38 * arena for secret integers. Replace and improve the MP management macros
39 * (e.g., replace MP_MODIFY by MP_DEST).
41 * Revision 1.3 1999/11/21 22:13:02 mdw
42 * Add mp version of MPX_BITS.
44 * Revision 1.2 1999/11/19 13:19:06 mdw
45 * Set flags on results correctly.
47 * Revision 1.1 1999/11/17 18:02:16 mdw
48 * New multiprecision integer arithmetic suite.
52 /*----- Header files ------------------------------------------------------*/
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @mp_octets@ --- *
60 * Arguments: @const mp *m@ = a multiprecision integer
62 * Returns: The number of octets required to represent @m@.
64 * Use: Calculates the external storage required for a multiprecision
68 size_t mp_octets(const mp
*m
)
71 MPX_OCTETS(sz
, m
->v
, m
->vl
);
75 /* --- @mp_octets2c@ --- *
77 * Arguments: @const mp *m@ = a multiprecision integer
79 * Returns: The number of octets required to represent @m@.
81 * Use: Calculates the external storage required for a multiprecision
82 * integer represented as two's complement.
85 size_t mp_octets2c(const mp
*m
)
88 MPX_OCTETS2C(sz
, m
->v
, m
->vl
);
92 /* --- @mp_bits@ --- *
94 * Arguments: @const mp *m@ = a multiprecision integer
96 * Returns: The number of bits required to represent @m@.
98 * Use: Calculates the external storage required for a multiprecision
102 unsigned long mp_bits(const mp
*m
)
105 MPX_BITS(bits
, m
->v
, m
->vl
);
109 /* --- @mp_loadl@ --- *
111 * Arguments: @mp *d@ = destination
112 * @const void *pv@ = pointer to source data
113 * @size_t sz@ = size of the source data
115 * Returns: Resulting multiprecision number.
117 * Use: Loads a multiprecision number from an array of octets. The
118 * first byte in the array is the least significant. More
119 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
120 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
123 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
125 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
126 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
127 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
132 /* --- @mp_storel@ --- *
134 * Arguments: @const mp *m@ = source
135 * @void *pv@ = pointer to output array
136 * @size_t sz@ = size of the output array
140 * Use: Stores a multiprecision number in an array of octets. The
141 * first byte in the array is the least significant. If the
142 * array is too small to represent the number, high-order bits
143 * are truncated; if the array is too large, high order bytes
144 * are filled with zeros. More formally, if the number is
145 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
146 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
149 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
151 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
154 /* --- @mp_loadb@ --- *
156 * Arguments: @mp *d@ = destination
157 * @const void *pv@ = pointer to source data
158 * @size_t sz@ = size of the source data
160 * Returns: Resulting multiprecision number.
162 * Use: Loads a multiprecision number from an array of octets. The
163 * last byte in the array is the least significant. More
164 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
165 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
168 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
170 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
171 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
172 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
177 /* --- @mp_storeb@ --- *
179 * Arguments: @const mp *m@ = source
180 * @void *pv@ = pointer to output array
181 * @size_t sz@ = size of the output array
185 * Use: Stores a multiprecision number in an array of octets. The
186 * last byte in the array is the least significant. If the
187 * array is too small to represent the number, high-order bits
188 * are truncated; if the array is too large, high order bytes
189 * are filled with zeros. More formally, if the number is
190 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
191 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
194 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
196 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
199 /* --- @mp_loadl2c@ --- *
201 * Arguments: @mp *d@ = destination
202 * @const void *pv@ = pointer to source data
203 * @size_t sz@ = size of the source data
205 * Returns: Resulting multiprecision number.
207 * Use: Loads a multiprecision number from an array of octets as
208 * two's complement. The first byte in the array is the least
212 mp
*mp_loadl2c(mp
*d
, const void *pv
, size_t sz
)
214 const octet
*ov
= pv
;
215 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
216 if (!sz
|| !(ov
[sz
- 1] & 0x80)) {
217 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
220 mpx_loadl2cn(d
->v
, d
->vl
, pv
, sz
);
228 /* --- @mp_storel2c@ --- *
230 * Arguments: @const mp *m@ = source
231 * @void *pv@ = pointer to output array
232 * @size_t sz@ = size of the output array
236 * Use: Stores a multiprecision number in an array of octets as two's
237 * complement. The first byte in the array is the least
238 * significant. If the array is too small to represent the
239 * number, high-order bits are truncated; if the array is too
240 * large, high order bytes are sign-extended.
243 void mp_storel2c(const mp
*m
, void *pv
, size_t sz
)
246 mpx_storel2cn(m
->v
, m
->vl
, pv
, sz
);
248 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
251 /* --- @mp_loadb2c@ --- *
253 * Arguments: @mp *d@ = destination
254 * @const void *pv@ = pointer to source data
255 * @size_t sz@ = size of the source data
257 * Returns: Resulting multiprecision number.
259 * Use: Loads a multiprecision number from an array of octets as
260 * two's complement. The last byte in the array is the least
264 mp
*mp_loadb2c(mp
*d
, const void *pv
, size_t sz
)
266 const octet
*ov
= pv
;
267 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
268 if (!sz
|| !(ov
[0] & 0x80)) {
269 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
272 mpx_loadl2cn(d
->v
, d
->vl
, pv
, sz
);
280 /* --- @mp_storeb2c@ --- *
282 * Arguments: @const mp *m@ = source
283 * @void *pv@ = pointer to output array
284 * @size_t sz@ = size of the output array
288 * Use: Stores a multiprecision number in an array of octets, as
289 * two's complement. The last byte in the array is the least
290 * significant. If the array is too small to represent the
291 * number, high-order bits are truncated; if the array is too
292 * large, high order bytes are sign-extended.
295 void mp_storeb2c(const mp
*m
, void *pv
, size_t sz
)
298 mpx_storeb2cn(m
->v
, m
->vl
, pv
, sz
);
300 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
303 /*----- That's all, folks -------------------------------------------------*/