1 1.1 mrg /* 2 1.1 mrg Name: imath.h 3 1.1 mrg Purpose: Arbitrary precision integer arithmetic routines. 4 1.1 mrg Author: M. J. Fromberger 5 1.1 mrg 6 1.1 mrg Copyright (C) 2002-2007 Michael J. Fromberger, All Rights Reserved. 7 1.1 mrg 8 1.1 mrg Permission is hereby granted, free of charge, to any person obtaining a copy 9 1.1 mrg of this software and associated documentation files (the "Software"), to deal 10 1.1 mrg in the Software without restriction, including without limitation the rights 11 1.1 mrg to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 1.1 mrg copies of the Software, and to permit persons to whom the Software is 13 1.1 mrg furnished to do so, subject to the following conditions: 14 1.1 mrg 15 1.1 mrg The above copyright notice and this permission notice shall be included in 16 1.1 mrg all copies or substantial portions of the Software. 17 1.1 mrg 18 1.1 mrg THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 1.1 mrg IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 1.1 mrg AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 1.1 mrg LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 1.1 mrg OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 1.1 mrg SOFTWARE. 25 1.1 mrg */ 26 1.1 mrg 27 1.1 mrg #ifndef IMATH_H_ 28 1.1 mrg #define IMATH_H_ 29 1.1 mrg 30 1.1 mrg #include <limits.h> 31 1.1 mrg #include <stdbool.h> 32 1.1 mrg #include <stdint.h> 33 1.1 mrg 34 1.1 mrg #ifdef __cplusplus 35 1.1 mrg extern "C" { 36 1.1 mrg #endif 37 1.1 mrg 38 1.1 mrg typedef unsigned char mp_sign; 39 1.1 mrg typedef unsigned int mp_size; 40 1.1 mrg typedef int mp_result; 41 1.1 mrg typedef long mp_small; /* must be a signed type */ 42 1.1 mrg typedef unsigned long mp_usmall; /* must be an unsigned type */ 43 1.1 mrg 44 1.1 mrg 45 1.1 mrg /* Build with words as uint64_t by default. */ 46 1.1 mrg #ifdef USE_32BIT_WORDS 47 1.1 mrg typedef uint16_t mp_digit; 48 1.1 mrg typedef uint32_t mp_word; 49 1.1 mrg # define MP_DIGIT_MAX (UINT16_MAX * 1UL) 50 1.1 mrg # define MP_WORD_MAX (UINT32_MAX * 1UL) 51 1.1 mrg #else 52 1.1 mrg typedef uint32_t mp_digit; 53 1.1 mrg typedef uint64_t mp_word; 54 1.1 mrg # define MP_DIGIT_MAX (UINT32_MAX * UINT64_C(1)) 55 1.1 mrg # define MP_WORD_MAX (UINT64_MAX) 56 1.1 mrg #endif 57 1.1 mrg 58 1.1 mrg typedef struct { 59 1.1 mrg mp_digit single; 60 1.1 mrg mp_digit* digits; 61 1.1 mrg mp_size alloc; 62 1.1 mrg mp_size used; 63 1.1 mrg mp_sign sign; 64 1.1 mrg } mpz_t, *mp_int; 65 1.1 mrg 66 1.1 mrg static inline mp_digit* MP_DIGITS(mp_int Z) { return Z->digits; } 67 1.1 mrg static inline mp_size MP_ALLOC(mp_int Z) { return Z->alloc; } 68 1.1 mrg static inline mp_size MP_USED(mp_int Z) { return Z->used; } 69 1.1 mrg static inline mp_sign MP_SIGN(mp_int Z) { return Z->sign; } 70 1.1 mrg 71 1.1 mrg extern const mp_result MP_OK; 72 1.1 mrg extern const mp_result MP_FALSE; 73 1.1 mrg extern const mp_result MP_TRUE; 74 1.1 mrg extern const mp_result MP_MEMORY; 75 1.1 mrg extern const mp_result MP_RANGE; 76 1.1 mrg extern const mp_result MP_UNDEF; 77 1.1 mrg extern const mp_result MP_TRUNC; 78 1.1 mrg extern const mp_result MP_BADARG; 79 1.1 mrg extern const mp_result MP_MINERR; 80 1.1 mrg 81 1.1 mrg #define MP_DIGIT_BIT (sizeof(mp_digit) * CHAR_BIT) 82 1.1 mrg #define MP_WORD_BIT (sizeof(mp_word) * CHAR_BIT) 83 1.1 mrg #define MP_SMALL_MIN LONG_MIN 84 1.1 mrg #define MP_SMALL_MAX LONG_MAX 85 1.1 mrg #define MP_USMALL_MAX ULONG_MAX 86 1.1 mrg 87 1.1 mrg #define MP_MIN_RADIX 2 88 1.1 mrg #define MP_MAX_RADIX 36 89 1.1 mrg 90 1.1 mrg /** Sets the default number of digits allocated to an `mp_int` constructed by 91 1.1 mrg `mp_int_init_size()` with `prec == 0`. Allocations are rounded up to 92 1.1 mrg multiples of this value. `MP_DEFAULT_PREC` is the default value. Requires 93 1.1 mrg `ndigits > 0`. */ 94 1.1 mrg void mp_int_default_precision(mp_size ndigits); 95 1.1 mrg 96 1.1 mrg /** Sets the number of digits below which multiplication will use the standard 97 1.1 mrg quadratic "schoolbook" multiplication algorithm rather than Karatsuba-Ofman. 98 1.1 mrg Requires `ndigits >= sizeof(mp_word)`. */ 99 1.1 mrg void mp_int_multiply_threshold(mp_size ndigits); 100 1.1 mrg 101 1.1 mrg /** A sign indicating a (strictly) negative value. */ 102 1.1 mrg extern const mp_sign MP_NEG; 103 1.1 mrg 104 1.1 mrg /** A sign indicating a zero or positive value. */ 105 1.1 mrg extern const mp_sign MP_ZPOS; 106 1.1 mrg 107 1.1 mrg /** Reports whether `z` is odd, having remainder 1 when divided by 2. */ 108 1.1 mrg static inline bool mp_int_is_odd(mp_int z) { return (z->digits[0] & 1) != 0; } 109 1.1 mrg 110 1.1 mrg /** Reports whether `z` is even, having remainder 0 when divided by 2. */ 111 1.1 mrg static inline bool mp_int_is_even(mp_int z) { return (z->digits[0] & 1) == 0; } 112 1.1 mrg 113 1.1 mrg /** Initializes `z` with 1-digit precision and sets it to zero. This function 114 1.1 mrg cannot fail unless `z == NULL`. */ 115 1.1 mrg mp_result mp_int_init(mp_int z); 116 1.1 mrg 117 1.1 mrg /** Allocates a fresh zero-valued `mpz_t` on the heap, returning NULL in case 118 1.1 mrg of error. The only possible error is out-of-memory. */ 119 1.1 mrg mp_int mp_int_alloc(void); 120 1.1 mrg 121 1.1 mrg /** Initializes `z` with at least `prec` digits of storage, and sets it to 122 1.1 mrg zero. If `prec` is zero, the default precision is used. In either case the 123 1.1 mrg size is rounded up to the nearest multiple of the word size. */ 124 1.1 mrg mp_result mp_int_init_size(mp_int z, mp_size prec); 125 1.1 mrg 126 1.1 mrg /** Initializes `z` to be a copy of an already-initialized value in `old`. The 127 1.1 mrg new copy does not share storage with the original. */ 128 1.1 mrg mp_result mp_int_init_copy(mp_int z, mp_int old); 129 1.1 mrg 130 1.1 mrg /** Initializes `z` to the specified signed `value` at default precision. */ 131 1.1 mrg mp_result mp_int_init_value(mp_int z, mp_small value); 132 1.1 mrg 133 1.1 mrg /** Initializes `z` to the specified unsigned `value` at default precision. */ 134 1.1 mrg mp_result mp_int_init_uvalue(mp_int z, mp_usmall uvalue); 135 1.1 mrg 136 1.1 mrg /** Sets `z` to the value of the specified signed `value`. */ 137 1.1 mrg mp_result mp_int_set_value(mp_int z, mp_small value); 138 1.1 mrg 139 1.1 mrg /** Sets `z` to the value of the specified unsigned `value`. */ 140 1.1 mrg mp_result mp_int_set_uvalue(mp_int z, mp_usmall uvalue); 141 1.1 mrg 142 1.1 mrg /** Releases the storage used by `z`. */ 143 1.1 mrg void mp_int_clear(mp_int z); 144 1.1 mrg 145 1.1 mrg /** Releases the storage used by `z` and also `z` itself. 146 1.1 mrg This should only be used for `z` allocated by `mp_int_alloc()`. */ 147 1.1 mrg void mp_int_free(mp_int z); 148 1.1 mrg 149 1.1 mrg /** Replaces the value of `c` with a copy of the value of `a`. No new memory is 150 1.1 mrg allocated unless `a` has more significant digits than `c` has allocated. */ 151 1.1 mrg mp_result mp_int_copy(mp_int a, mp_int c); 152 1.1 mrg 153 1.1 mrg /** Swaps the values and storage between `a` and `c`. */ 154 1.1 mrg void mp_int_swap(mp_int a, mp_int c); 155 1.1 mrg 156 1.1 mrg /** Sets `z` to zero. The allocated storage of `z` is not changed. */ 157 1.1 mrg void mp_int_zero(mp_int z); 158 1.1 mrg 159 1.1 mrg /** Sets `c` to the absolute value of `a`. */ 160 1.1 mrg mp_result mp_int_abs(mp_int a, mp_int c); 161 1.1 mrg 162 1.1 mrg /** Sets `c` to the additive inverse (negation) of `a`. */ 163 1.1 mrg mp_result mp_int_neg(mp_int a, mp_int c); 164 1.1 mrg 165 1.1 mrg /** Sets `c` to the sum of `a` and `b`. */ 166 1.1 mrg mp_result mp_int_add(mp_int a, mp_int b, mp_int c); 167 1.1 mrg 168 1.1 mrg /** Sets `c` to the sum of `a` and `value`. */ 169 1.1 mrg mp_result mp_int_add_value(mp_int a, mp_small value, mp_int c); 170 1.1 mrg 171 1.1 mrg /** Sets `c` to the difference of `a` less `b`. */ 172 1.1 mrg mp_result mp_int_sub(mp_int a, mp_int b, mp_int c); 173 1.1 mrg 174 1.1 mrg /** Sets `c` to the difference of `a` less `value`. */ 175 1.1 mrg mp_result mp_int_sub_value(mp_int a, mp_small value, mp_int c); 176 1.1 mrg 177 1.1 mrg /** Sets `c` to the product of `a` and `b`. */ 178 1.1 mrg mp_result mp_int_mul(mp_int a, mp_int b, mp_int c); 179 1.1 mrg 180 1.1 mrg /** Sets `c` to the product of `a` and `value`. */ 181 1.1 mrg mp_result mp_int_mul_value(mp_int a, mp_small value, mp_int c); 182 1.1 mrg 183 1.1 mrg /** Sets `c` to the product of `a` and `2^p2`. Requires `p2 >= 0`. */ 184 1.1 mrg mp_result mp_int_mul_pow2(mp_int a, mp_small p2, mp_int c); 185 1.1 mrg 186 1.1 mrg /** Sets `c` to the square of `a`. */ 187 1.1 mrg mp_result mp_int_sqr(mp_int a, mp_int c); 188 1.1 mrg 189 1.1 mrg /** Sets `q` and `r` to the quotent and remainder of `a / b`. Division by 190 1.1 mrg powers of 2 is detected and handled efficiently. The remainder is pinned 191 1.1 mrg to `0 <= r < b`. 192 1.1 mrg 193 1.1 mrg Either of `q` or `r` may be NULL, but not both, and `q` and `r` may not 194 1.1 mrg point to the same value. */ 195 1.1 mrg mp_result mp_int_div(mp_int a, mp_int b, mp_int q, mp_int r); 196 1.1 mrg 197 1.1 mrg /** Sets `q` and `*r` to the quotent and remainder of `a / value`. Division by 198 1.1 mrg powers of 2 is detected and handled efficiently. The remainder is pinned to 199 1.1 mrg `0 <= *r < b`. Either of `q` or `r` may be NULL. */ 200 1.1 mrg mp_result mp_int_div_value(mp_int a, mp_small value, mp_int q, mp_small *r); 201 1.1 mrg 202 1.1 mrg /** Sets `q` and `r` to the quotient and remainder of `a / 2^p2`. This is a 203 1.1 mrg special case for division by powers of two that is more efficient than 204 1.1 mrg using ordinary division. Note that `mp_int_div()` will automatically handle 205 1.1 mrg this case, this function is for cases where you have only the exponent. */ 206 1.1 mrg mp_result mp_int_div_pow2(mp_int a, mp_small p2, mp_int q, mp_int r); 207 1.1 mrg 208 1.1 mrg /** Sets `c` to the remainder of `a / m`. 209 1.1 mrg The remainder is pinned to `0 <= c < m`. */ 210 1.1 mrg mp_result mp_int_mod(mp_int a, mp_int m, mp_int c); 211 1.1 mrg 212 1.1 mrg /** Sets `c` to the value of `a` raised to the `b` power. 213 1.1 mrg It returns `MP_RANGE` if `b < 0`. */ 214 1.1 mrg mp_result mp_int_expt(mp_int a, mp_small b, mp_int c); 215 1.1 mrg 216 1.1 mrg /** Sets `c` to the value of `a` raised to the `b` power. 217 1.1 mrg It returns `MP_RANGE` if `b < 0`. */ 218 1.1 mrg mp_result mp_int_expt_value(mp_small a, mp_small b, mp_int c); 219 1.1 mrg 220 1.1 mrg /** Sets `c` to the value of `a` raised to the `b` power. 221 1.1 mrg It returns `MP_RANGE`) if `b < 0`. */ 222 1.1 mrg mp_result mp_int_expt_full(mp_int a, mp_int b, mp_int c); 223 1.1 mrg 224 1.1 mrg /** Sets `*r` to the remainder of `a / value`. 225 1.1 mrg The remainder is pinned to `0 <= r < value`. */ 226 1.1 mrg static inline 227 1.1 mrg mp_result mp_int_mod_value(mp_int a, mp_small value, mp_small* r) { 228 1.1 mrg return mp_int_div_value(a, value, 0, r); 229 1.1 mrg } 230 1.1 mrg 231 1.1 mrg /** Returns the comparator of `a` and `b`. */ 232 1.1 mrg int mp_int_compare(mp_int a, mp_int b); 233 1.1 mrg 234 1.1 mrg /** Returns the comparator of the magnitudes of `a` and `b`, disregarding their 235 1.1 mrg signs. Neither `a` nor `b` is modified by the comparison. */ 236 1.1 mrg int mp_int_compare_unsigned(mp_int a, mp_int b); 237 1.1 mrg 238 1.1 mrg /** Returns the comparator of `z` and zero. */ 239 1.1 mrg int mp_int_compare_zero(mp_int z); 240 1.1 mrg 241 1.1 mrg /** Returns the comparator of `z` and the signed value `v`. */ 242 1.1 mrg int mp_int_compare_value(mp_int z, mp_small v); 243 1.1 mrg 244 1.1 mrg /** Returns the comparator of `z` and the unsigned value `uv`. */ 245 1.1 mrg int mp_int_compare_uvalue(mp_int z, mp_usmall uv); 246 1.1 mrg 247 1.1 mrg /** Reports whether `a` is divisible by `v`. */ 248 1.1 mrg bool mp_int_divisible_value(mp_int a, mp_small v); 249 1.1 mrg 250 1.1 mrg /** Returns `k >= 0` such that `z` is `2^k`, if such a `k` exists. If no such 251 1.1 mrg `k` exists, the function returns -1. */ 252 1.1 mrg int mp_int_is_pow2(mp_int z); 253 1.1 mrg 254 1.1 mrg /** Sets `c` to the value of `a` raised to the `b` power, reduced modulo `m`. 255 1.1 mrg It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`. */ 256 1.1 mrg mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c); 257 1.1 mrg 258 1.1 mrg /** Sets `c` to the value of `a` raised to the `value` power, modulo `m`. 259 1.1 mrg It returns `MP_RANGE` if `value < 0` or `MP_UNDEF` if `m == 0`. */ 260 1.1 mrg mp_result mp_int_exptmod_evalue(mp_int a, mp_small value, mp_int m, mp_int c); 261 1.1 mrg 262 1.1 mrg /** Sets `c` to the value of `value` raised to the `b` power, modulo `m`. 263 1.1 mrg It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`. */ 264 1.1 mrg mp_result mp_int_exptmod_bvalue(mp_small value, mp_int b, mp_int m, mp_int c); 265 1.1 mrg 266 1.1 mrg /** Sets `c` to the value of `a` raised to the `b` power, reduced modulo `m`, 267 1.1 mrg given a precomputed reduction constant `mu` defined for Barrett's modular 268 1.1 mrg reduction algorithm. 269 1.1 mrg 270 1.1 mrg It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`. */ 271 1.1 mrg mp_result mp_int_exptmod_known(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c); 272 1.1 mrg 273 1.1 mrg /** Sets `c` to the reduction constant for Barrett reduction by modulus `m`. 274 1.1 mrg Requires that `c` and `m` point to distinct locations. */ 275 1.1 mrg mp_result mp_int_redux_const(mp_int m, mp_int c); 276 1.1 mrg 277 1.1 mrg /** Sets `c` to the multiplicative inverse of `a` modulo `m`, if it exists. 278 1.1 mrg The least non-negative representative of the congruence class is computed. 279 1.1 mrg 280 1.1 mrg It returns `MP_UNDEF` if the inverse does not exist, or `MP_RANGE` if `a == 281 1.1 mrg 0` or `m <= 0`. */ 282 1.1 mrg mp_result mp_int_invmod(mp_int a, mp_int m, mp_int c); 283 1.1 mrg 284 1.1 mrg /** Sets `c` to the greatest common divisor of `a` and `b`. 285 1.1 mrg 286 1.1 mrg It returns `MP_UNDEF` if the GCD is undefined, such as for example if `a` 287 1.1 mrg and `b` are both zero. */ 288 1.1 mrg mp_result mp_int_gcd(mp_int a, mp_int b, mp_int c); 289 1.1 mrg 290 1.1 mrg /** Sets `c` to the greatest common divisor of `a` and `b`, and sets `x` and 291 1.1 mrg `y` to values satisfying Bezout's identity `gcd(a, b) = ax + by`. 292 1.1 mrg 293 1.1 mrg It returns `MP_UNDEF` if the GCD is undefined, such as for example if `a` 294 1.1 mrg and `b` are both zero. */ 295 1.1 mrg mp_result mp_int_egcd(mp_int a, mp_int b, mp_int c, mp_int x, mp_int y); 296 1.1 mrg 297 1.1 mrg /** Sets `c` to the least common multiple of `a` and `b`. 298 1.1 mrg 299 1.1 mrg It returns `MP_UNDEF` if the LCM is undefined, such as for example if `a` 300 1.1 mrg and `b` are both zero. */ 301 1.1 mrg mp_result mp_int_lcm(mp_int a, mp_int b, mp_int c); 302 1.1 mrg 303 1.1 mrg /** Sets `c` to the greatest integer not less than the `b`th root of `a`, 304 1.1 mrg using Newton's root-finding algorithm. 305 1.1 mrg It returns `MP_UNDEF` if `a < 0` and `b` is even. */ 306 1.1 mrg mp_result mp_int_root(mp_int a, mp_small b, mp_int c); 307 1.1 mrg 308 1.1 mrg /** Sets `c` to the greatest integer not less than the square root of `a`. 309 1.1 mrg This is a special case of `mp_int_root()`. */ 310 1.1 mrg static inline 311 1.1 mrg mp_result mp_int_sqrt(mp_int a, mp_int c) { return mp_int_root(a, 2, c); } 312 1.1 mrg 313 1.1 mrg /** Returns `MP_OK` if `z` is representable as `mp_small`, else `MP_RANGE`. 314 1.1 mrg If `out` is not NULL, `*out` is set to the value of `z` when `MP_OK`. */ 315 1.1 mrg mp_result mp_int_to_int(mp_int z, mp_small *out); 316 1.1 mrg 317 1.1 mrg /** Returns `MP_OK` if `z` is representable as `mp_usmall`, or `MP_RANGE`. 318 1.1 mrg If `out` is not NULL, `*out` is set to the value of `z` when `MP_OK`. */ 319 1.1 mrg mp_result mp_int_to_uint(mp_int z, mp_usmall *out); 320 1.1 mrg 321 1.1 mrg /** Converts `z` to a zero-terminated string of characters in the specified 322 1.1 mrg `radix`, writing at most `limit` characters to `str` including the 323 1.1 mrg terminating NUL value. A leading `-` is used to indicate a negative value. 324 1.1 mrg 325 1.1 mrg Returns `MP_TRUNC` if `limit` was to small to write all of `z`. 326 1.1 mrg Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`. */ 327 1.1 mrg mp_result mp_int_to_string(mp_int z, mp_size radix, char *str, int limit); 328 1.1 mrg 329 1.1 mrg /** Reports the minimum number of characters required to represent `z` as a 330 1.1 mrg zero-terminated string in the given `radix`. 331 1.1 mrg Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`. */ 332 1.1 mrg mp_result mp_int_string_len(mp_int z, mp_size radix); 333 1.1 mrg 334 1.1 mrg /** Reads a string of ASCII digits in the specified `radix` from the zero 335 1.1 mrg terminated `str` provided into `z`. For values of `radix > 10`, the letters 336 1.1 mrg `A`..`Z` or `a`..`z` are accepted. Letters are interpreted without respect 337 1.1 mrg to case. 338 1.1 mrg 339 1.1 mrg Leading whitespace is ignored, and a leading `+` or `-` is interpreted as a 340 1.1 mrg sign flag. Processing stops when a NUL or any other character out of range 341 1.1 mrg for a digit in the given radix is encountered. 342 1.1 mrg 343 1.1 mrg If the whole string was consumed, `MP_OK` is returned; otherwise 344 1.1 mrg `MP_TRUNC`. is returned. 345 1.1 mrg 346 1.1 mrg Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`. */ 347 1.1 mrg mp_result mp_int_read_string(mp_int z, mp_size radix, const char *str); 348 1.1 mrg 349 1.1 mrg /** Reads a string of ASCII digits in the specified `radix` from the zero 350 1.1 mrg terminated `str` provided into `z`. For values of `radix > 10`, the letters 351 1.1 mrg `A`..`Z` or `a`..`z` are accepted. Letters are interpreted without respect 352 1.1 mrg to case. 353 1.1 mrg 354 1.1 mrg Leading whitespace is ignored, and a leading `+` or `-` is interpreted as a 355 1.1 mrg sign flag. Processing stops when a NUL or any other character out of range 356 1.1 mrg for a digit in the given radix is encountered. 357 1.1 mrg 358 1.1 mrg If the whole string was consumed, `MP_OK` is returned; otherwise 359 1.1 mrg `MP_TRUNC`. is returned. If `end` is not NULL, `*end` is set to point to 360 1.1 mrg the first unconsumed byte of the input string (the NUL byte if the whole 361 1.1 mrg string was consumed). This emulates the behavior of the standard C 362 1.1 mrg `strtol()` function. 363 1.1 mrg 364 1.1 mrg Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`. */ 365 1.1 mrg mp_result mp_int_read_cstring(mp_int z, mp_size radix, const char *str, char **end); 366 1.1 mrg 367 1.1 mrg /** Returns the number of significant bits in `z`. */ 368 1.1 mrg mp_result mp_int_count_bits(mp_int z); 369 1.1 mrg 370 1.1 mrg /** Converts `z` to 2's complement binary, writing at most `limit` bytes into 371 1.1 mrg the given `buf`. Returns `MP_TRUNC` if the buffer limit was too small to 372 1.1 mrg contain the whole value. If this occurs, the contents of buf will be 373 1.1 mrg effectively garbage, as the function uses the buffer as scratch space. 374 1.1 mrg 375 1.1 mrg The binary representation of `z` is in base-256 with digits ordered from 376 1.1 mrg most significant to least significant (network byte ordering). The 377 1.1 mrg high-order bit of the first byte is set for negative values, clear for 378 1.1 mrg non-negative values. 379 1.1 mrg 380 1.1 mrg As a result, non-negative values will be padded with a leading zero byte if 381 1.1 mrg the high-order byte of the base-256 magnitude is set. This extra byte is 382 1.1 mrg accounted for by the `mp_int_binary_len()` function. */ 383 1.1 mrg mp_result mp_int_to_binary(mp_int z, unsigned char *buf, int limit); 384 1.1 mrg 385 1.1 mrg /** Reads a 2's complement binary value from `buf` into `z`, where `len` is the 386 1.1 mrg length of the buffer. The contents of `buf` may be overwritten during 387 1.1 mrg processing, although they will be restored when the function returns. */ 388 1.1 mrg mp_result mp_int_read_binary(mp_int z, unsigned char *buf, int len); 389 1.1 mrg 390 1.1 mrg /** Returns the number of bytes to represent `z` in 2's complement binary. */ 391 1.1 mrg mp_result mp_int_binary_len(mp_int z); 392 1.1 mrg 393 1.1 mrg /** Converts the magnitude of `z` to unsigned binary, writing at most `limit` 394 1.1 mrg bytes into the given `buf`. The sign of `z` is ignored, but `z` is not 395 1.1 mrg modified. Returns `MP_TRUNC` if the buffer limit was too small to contain 396 1.1 mrg the whole value. If this occurs, the contents of `buf` will be effectively 397 1.1 mrg garbage, as the function uses the buffer as scratch space during 398 1.1 mrg conversion. 399 1.1 mrg 400 1.1 mrg The binary representation of `z` is in base-256 with digits ordered from 401 1.1 mrg most significant to least significant (network byte ordering). */ 402 1.1 mrg mp_result mp_int_to_unsigned(mp_int z, unsigned char *buf, int limit); 403 1.1 mrg 404 1.1 mrg /** Reads an unsigned binary value from `buf` into `z`, where `len` is the 405 1.1 mrg length of the buffer. The contents of `buf` are not modified during 406 1.1 mrg processing. */ 407 1.1 mrg mp_result mp_int_read_unsigned(mp_int z, unsigned char *buf, int len); 408 1.1 mrg 409 1.1 mrg /** Returns the number of bytes required to represent `z` as an unsigned binary 410 1.1 mrg value in base 256. */ 411 1.1 mrg mp_result mp_int_unsigned_len(mp_int z); 412 1.1 mrg 413 1.1 mrg /** Returns a pointer to a brief, human-readable, zero-terminated string 414 1.1 mrg describing `res`. The returned string is statically allocated and must not 415 1.1 mrg be freed by the caller. */ 416 1.1 mrg const char *mp_error_string(mp_result res); 417 1.1 mrg 418 1.1 mrg #ifdef __cplusplus 419 1.1 mrg } 420 1.1 mrg #endif 421 1.1 mrg #endif /* end IMATH_H_ */ 422