2006-07-13 12:15:25 +08:00
|
|
|
/* imath version 1.3 */
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
Name: imath.c
|
|
|
|
Purpose: Arbitrary precision integer arithmetic routines.
|
|
|
|
Author: M. J. Fromberger <http://www.dartmouth.edu/~sting/>
|
|
|
|
Info: Id: imath.c 21 2006-04-02 18:58:36Z sting
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
Copyright (C) 2002 Michael J. Fromberger, All Rights Reserved.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation files
|
|
|
|
(the "Software"), to deal in the Software without restriction,
|
|
|
|
including without limitation the rights to use, copy, modify, merge,
|
|
|
|
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
2006-10-04 08:30:14 +08:00
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
2006-07-13 12:15:25 +08:00
|
|
|
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
2007-07-16 06:43:40 +08:00
|
|
|
/* $PostgreSQL: pgsql/contrib/pgcrypto/imath.c,v 1.7 2007/07/15 22:43:40 tgl Exp $ */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "px.h"
|
|
|
|
#include "imath.h"
|
|
|
|
|
|
|
|
#undef assert
|
2006-07-15 23:27:14 +08:00
|
|
|
#define assert(TEST) Assert(TEST)
|
2006-07-13 12:15:25 +08:00
|
|
|
#define TRACEABLE_CLAMP 0
|
|
|
|
#define TRACEABLE_FREE 0
|
|
|
|
|
|
|
|
/* {{{ Constants */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
const mp_result MP_OK = 0; /* no error, all is well */
|
|
|
|
const mp_result MP_FALSE = 0; /* boolean false */
|
|
|
|
const mp_result MP_TRUE = -1; /* boolean true */
|
|
|
|
const mp_result MP_MEMORY = -2; /* out of memory */
|
|
|
|
const mp_result MP_RANGE = -3; /* argument out of range */
|
|
|
|
const mp_result MP_UNDEF = -4; /* result undefined */
|
|
|
|
const mp_result MP_TRUNC = -5; /* output truncated */
|
2006-07-13 12:15:25 +08:00
|
|
|
const mp_result MP_BADARG = -6; /* invalid null argument */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
const mp_sign MP_NEG = 1; /* value is strictly negative */
|
|
|
|
const mp_sign MP_ZPOS = 0; /* value is non-negative */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
static const char *s_unknown_err = "unknown result code";
|
|
|
|
static const char *s_error_msg[] = {
|
2006-10-04 08:30:14 +08:00
|
|
|
"error code 0",
|
|
|
|
"boolean true",
|
|
|
|
"out of memory",
|
|
|
|
"argument out of range",
|
|
|
|
"result undefined",
|
|
|
|
"output truncated",
|
|
|
|
"invalid null argument",
|
|
|
|
NULL
|
2006-07-13 12:15:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* Optional library flags */
|
2006-10-04 08:30:14 +08:00
|
|
|
#define MP_CAP_DIGITS 1 /* flag bit to capitalize letter digits */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Argument checking macros
|
2006-07-13 12:15:25 +08:00
|
|
|
Use CHECK() where a return value is required; NRCHECK() elsewhere */
|
|
|
|
#define CHECK(TEST) assert(TEST)
|
|
|
|
#define NRCHECK(TEST) assert(TEST)
|
|
|
|
|
|
|
|
/* {{{ Logarithm table for computing output sizes */
|
|
|
|
|
|
|
|
/* The ith entry of this table gives the value of log_i(2).
|
|
|
|
|
|
|
|
An integer value n requires ceil(log_i(n)) digits to be represented
|
|
|
|
in base i. Since it is easy to compute lg(n), by counting bits, we
|
|
|
|
can compute log_i(n) = lg(n) * log_i(2).
|
|
|
|
*/
|
|
|
|
static const double s_log2[] = {
|
2006-10-04 08:30:14 +08:00
|
|
|
0.000000000, 0.000000000, 1.000000000, 0.630929754, /* 0 1 2 3 */
|
|
|
|
0.500000000, 0.430676558, 0.386852807, 0.356207187, /* 4 5 6 7 */
|
|
|
|
0.333333333, 0.315464877, 0.301029996, 0.289064826, /* 8 9 10 11 */
|
|
|
|
0.278942946, 0.270238154, 0.262649535, 0.255958025, /* 12 13 14 15 */
|
|
|
|
0.250000000, 0.244650542, 0.239812467, 0.235408913, /* 16 17 18 19 */
|
|
|
|
0.231378213, 0.227670249, 0.224243824, 0.221064729, /* 20 21 22 23 */
|
|
|
|
0.218104292, 0.215338279, 0.212746054, 0.210309918, /* 24 25 26 27 */
|
|
|
|
0.208014598, 0.205846832, 0.203795047, 0.201849087, /* 28 29 30 31 */
|
|
|
|
0.200000000, 0.198239863, 0.196561632, 0.194959022, /* 32 33 34 35 */
|
|
|
|
0.193426404, 0.191958720, 0.190551412, 0.189200360, /* 36 37 38 39 */
|
|
|
|
0.187901825, 0.186652411, 0.185449023, 0.184288833, /* 40 41 42 43 */
|
|
|
|
0.183169251, 0.182087900, 0.181042597, 0.180031327, /* 44 45 46 47 */
|
|
|
|
0.179052232, 0.178103594, 0.177183820, 0.176291434, /* 48 49 50 51 */
|
|
|
|
0.175425064, 0.174583430, 0.173765343, 0.172969690, /* 52 53 54 55 */
|
|
|
|
0.172195434, 0.171441601, 0.170707280, 0.169991616, /* 56 57 58 59 */
|
|
|
|
0.169293808, 0.168613099, 0.167948779, 0.167300179, /* 60 61 62 63 */
|
|
|
|
0.166666667
|
2006-07-13 12:15:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ Various macros */
|
|
|
|
|
|
|
|
/* Return the number of digits needed to represent a static value */
|
|
|
|
#define MP_VALUE_DIGITS(V) \
|
|
|
|
((sizeof(V)+(sizeof(mp_digit)-1))/sizeof(mp_digit))
|
|
|
|
|
|
|
|
/* Round precision P to nearest word boundary */
|
|
|
|
#define ROUND_PREC(P) ((mp_size)(2*(((P)+1)/2)))
|
|
|
|
|
|
|
|
/* Set array P of S digits to zero */
|
|
|
|
#define ZERO(P, S) \
|
|
|
|
do{mp_size i__=(S)*sizeof(mp_digit);mp_digit *p__=(P);memset(p__,0,i__);}while(0)
|
|
|
|
|
|
|
|
/* Copy S digits from array P to array Q */
|
|
|
|
#define COPY(P, Q, S) \
|
|
|
|
do{mp_size i__=(S)*sizeof(mp_digit);mp_digit *p__=(P),*q__=(Q);\
|
|
|
|
memcpy(q__,p__,i__);}while(0)
|
|
|
|
|
|
|
|
/* Reverse N elements of type T in array A */
|
|
|
|
#define REV(T, A, N) \
|
|
|
|
do{T *u_=(A),*v_=u_+(N)-1;while(u_<v_){T xch=*u_;*u_++=*v_;*v_--=xch;}}while(0)
|
|
|
|
|
|
|
|
#if TRACEABLE_CLAMP
|
|
|
|
#define CLAMP(Z) s_clamp(Z)
|
|
|
|
#else
|
|
|
|
#define CLAMP(Z) \
|
|
|
|
do{mp_int z_=(Z);mp_size uz_=MP_USED(z_);mp_digit *dz_=MP_DIGITS(z_)+uz_-1;\
|
|
|
|
while(uz_ > 1 && (*dz_-- == 0)) --uz_;MP_USED(z_)=uz_;}while(0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
|
|
|
#define MIN(A, B) ((B)<(A)?(B):(A))
|
|
|
|
#define MAX(A, B) ((B)>(A)?(B):(A))
|
|
|
|
#define SWAP(T, A, B) do{T t_=(A);A=(B);B=t_;}while(0)
|
|
|
|
|
|
|
|
#define TEMP(K) (temp + (K))
|
|
|
|
#define SETUP(E, C) \
|
|
|
|
do{if((res = (E)) != MP_OK) goto CLEANUP; ++(C);}while(0)
|
|
|
|
|
|
|
|
#define CMPZ(Z) \
|
|
|
|
(((Z)->used==1&&(Z)->digits[0]==0)?0:((Z)->sign==MP_NEG)?-1:1)
|
|
|
|
|
|
|
|
#define UMUL(X, Y, Z) \
|
|
|
|
do{mp_size ua_=MP_USED(X),ub_=MP_USED(Y);mp_size o_=ua_+ub_;\
|
|
|
|
ZERO(MP_DIGITS(Z),o_);\
|
|
|
|
(void) s_kmul(MP_DIGITS(X),MP_DIGITS(Y),MP_DIGITS(Z),ua_,ub_);\
|
|
|
|
MP_USED(Z)=o_;CLAMP(Z);}while(0)
|
|
|
|
|
|
|
|
#define USQR(X, Z) \
|
|
|
|
do{mp_size ua_=MP_USED(X),o_=ua_+ua_;ZERO(MP_DIGITS(Z),o_);\
|
|
|
|
(void) s_ksqr(MP_DIGITS(X),MP_DIGITS(Z),ua_);MP_USED(Z)=o_;CLAMP(Z);}while(0)
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
#define UPPER_HALF(W) ((mp_word)((W) >> MP_DIGIT_BIT))
|
|
|
|
#define LOWER_HALF(W) ((mp_digit)(W))
|
|
|
|
#define HIGH_BIT_SET(W) ((W) >> (MP_WORD_BIT - 1))
|
2006-07-13 12:15:25 +08:00
|
|
|
#define ADD_WILL_OVERFLOW(W, V) ((MP_WORD_MAX - (V)) < (W))
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* Default number of digits allocated to a new mp_int */
|
|
|
|
static mp_size default_precision = 64;
|
|
|
|
|
|
|
|
/* Minimum number of digits to invoke recursive multiply */
|
|
|
|
static mp_size multiply_threshold = 32;
|
|
|
|
|
|
|
|
/* Default library configuration flags */
|
|
|
|
static mp_word mp_flags = MP_CAP_DIGITS;
|
|
|
|
|
|
|
|
/* Allocate a buffer of (at least) num digits, or return
|
|
|
|
NULL if that couldn't be done. */
|
|
|
|
static mp_digit *s_alloc(mp_size num);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-07-13 12:15:25 +08:00
|
|
|
#if TRACEABLE_FREE
|
|
|
|
static void s_free(void *ptr);
|
|
|
|
#else
|
|
|
|
#define s_free(P) px_free(P)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Insure that z has at least min digits allocated, resizing if
|
|
|
|
necessary. Returns true if successful, false if out of memory. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_pad(mp_int z, mp_size min);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Normalize by removing leading zeroes (except when z = 0) */
|
|
|
|
#if TRACEABLE_CLAMP
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_clamp(mp_int z);
|
2006-07-13 12:15:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Fill in a "fake" mp_int on the stack with a given value */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_fake(mp_int z, int value, mp_digit vbuf[]);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Compare two runs of digits of given length, returns <0, 0, >0 */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_cdig(mp_digit * da, mp_digit * db, mp_size len);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Pack the unsigned digits of v into array t */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_vpack(int v, mp_digit t[]);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Compare magnitudes of a and b, returns <0, 0, >0 */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_ucmp(mp_int a, mp_int b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Compare magnitudes of a and v, returns <0, 0, >0 */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_vcmp(mp_int a, int v);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Unsigned magnitude addition; assumes dc is big enough.
|
|
|
|
Carry out is returned (no memory allocated). */
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit s_uadd(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Unsigned magnitude subtraction. Assumes dc is big enough. */
|
|
|
|
static void s_usub(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Unsigned recursive multiplication. Assumes dc is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_kmul(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Unsigned magnitude multiplication. Assumes dc is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_umul(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Unsigned recursive squaring. Assumes dc is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_ksqr(mp_digit * da, mp_digit * dc, mp_size size_a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Unsigned magnitude squaring. Assumes dc is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_usqr(mp_digit * da, mp_digit * dc, mp_size size_a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Single digit addition. Assumes a is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_dadd(mp_int a, mp_digit b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Single digit multiplication. Assumes a is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_dmul(mp_int a, mp_digit b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Single digit multiplication on buffers; assumes dc is big enough. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_dbmul(mp_digit * da, mp_digit b, mp_digit * dc,
|
|
|
|
mp_size size_a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Single digit division. Replaces a with the quotient,
|
2006-07-13 12:15:25 +08:00
|
|
|
returns the remainder. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit s_ddiv(mp_int a, mp_digit b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Quick division by a power of 2, replaces z (no allocation) */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_qdiv(mp_int z, mp_size p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Quick remainder by a power of 2, replaces z (no allocation) */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_qmod(mp_int z, mp_size p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Quick multiplication by a power of 2, replaces z.
|
2006-07-13 12:15:25 +08:00
|
|
|
Allocates if necessary; returns false in case this fails. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_qmul(mp_int z, mp_size p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Quick subtraction from a power of 2, replaces z.
|
|
|
|
Allocates if necessary; returns false in case this fails. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_qsub(mp_int z, mp_size p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Return maximum k such that 2^k divides z. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_dp2k(mp_int z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Return k >= 0 such that z = 2^k, or -1 if there is no such k. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_isp2(mp_int z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Set z to 2^k. May allocate; returns false in case this fails. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_2expt(mp_int z, int k);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Normalize a and b for division, returns normalization constant */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_norm(mp_int a, mp_int b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Compute constant mu for Barrett reduction, given modulus m, result
|
|
|
|
replaces z, m is untouched. */
|
|
|
|
static mp_result s_brmu(mp_int z, mp_int m);
|
|
|
|
|
|
|
|
/* Reduce a modulo m, using Barrett's algorithm. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_reduce(mp_int x, mp_int m, mp_int mu, mp_int q1, mp_int q2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Modular exponentiation, using Barrett reduction */
|
|
|
|
static mp_result s_embar(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c);
|
|
|
|
|
|
|
|
/* Unsigned magnitude division. Assumes |a| > |b|. Allocates
|
|
|
|
temporaries; overwrites a with quotient, b with remainder. */
|
|
|
|
static mp_result s_udiv(mp_int a, mp_int b);
|
|
|
|
|
|
|
|
/* Compute the number of digits in radix r required to represent the
|
|
|
|
given value. Does not account for sign flags, terminators, etc. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_outlen(mp_int z, mp_size r);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Guess how many digits of precision will be needed to represent a
|
|
|
|
radix r value of the specified number of digits. Returns a value
|
|
|
|
guaranteed to be no smaller than the actual number required. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_size s_inlen(int len, mp_size r);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Convert a character to a digit value in radix r, or
|
2006-07-13 12:15:25 +08:00
|
|
|
-1 if out of range */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int s_ch2val(char c, int r);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Convert a digit value to a character */
|
2006-10-04 08:30:14 +08:00
|
|
|
static char s_val2ch(int v, int caps);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Take 2's complement of a buffer in place */
|
2006-10-04 08:30:14 +08:00
|
|
|
static void s_2comp(unsigned char *buf, int len);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* Convert a value to binary, ignoring sign. On input, *limpos is the
|
|
|
|
bound on how many bytes should be written to buf; on output, *limpos
|
|
|
|
is set to the number of bytes actually written. */
|
|
|
|
static mp_result s_tobin(mp_int z, unsigned char *buf, int *limpos, int pad);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Dump a representation of the mp_int to standard output */
|
2006-10-04 08:30:14 +08:00
|
|
|
void s_print(char *tag, mp_int z);
|
|
|
|
void s_print_buf(char *tag, mp_digit * buf, mp_size num);
|
2006-07-13 12:15:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* {{{ get_default_precision() */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size
|
|
|
|
mp_get_default_precision(void)
|
|
|
|
{
|
|
|
|
return default_precision;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_set_default_precision(s) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_set_default_precision(mp_size s)
|
|
|
|
{
|
|
|
|
NRCHECK(s > 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
default_precision = (mp_size) ROUND_PREC(s);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_get_multiply_threshold() */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size
|
|
|
|
mp_get_multiply_threshold(void)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
return multiply_threshold;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_set_multiply_threshold(s) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_set_multiply_threshold(mp_size s)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
multiply_threshold = s;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_init(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_init(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_init_size(z, default_precision);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_alloc() */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_int
|
|
|
|
mp_int_alloc(void)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_int out = px_alloc(sizeof(mpz_t));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
assert(out != NULL);
|
|
|
|
out->digits = NULL;
|
|
|
|
out->used = 0;
|
|
|
|
out->alloc = 0;
|
|
|
|
out->sign = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return out;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_init_size(z, prec) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_init_size(mp_int z, mp_size prec)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
|
|
|
|
|
|
|
prec = (mp_size) ROUND_PREC(prec);
|
|
|
|
prec = MAX(prec, default_precision);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((MP_DIGITS(z) = s_alloc(prec)) == NULL)
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
z->digits[0] = 0;
|
|
|
|
MP_USED(z) = 1;
|
|
|
|
MP_ALLOC(z) = prec;
|
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_init_copy(z, old) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_init_copy(mp_int z, mp_int old)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
|
|
|
mp_size uold,
|
|
|
|
target;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && old != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
uold = MP_USED(old);
|
|
|
|
target = MAX(uold, default_precision);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_init_size(z, target)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = uold;
|
|
|
|
MP_SIGN(z) = MP_SIGN(old);
|
|
|
|
COPY(MP_DIGITS(old), MP_DIGITS(z), uold);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_init_value(z, value) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_init_value(mp_int z, int value)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_init(z)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_set_value(z, value);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_set_value(z, value) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_set_value(mp_int z, int value)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size ndig;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* How many digits to copy */
|
|
|
|
ndig = (mp_size) MP_VALUE_DIGITS(value);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(z, ndig))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = (mp_size) s_vpack(value, MP_DIGITS(z));
|
|
|
|
MP_SIGN(z) = (value < 0) ? MP_NEG : MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_clear(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_int_clear(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
if (z == NULL)
|
|
|
|
return;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_DIGITS(z) != NULL)
|
|
|
|
{
|
|
|
|
s_free(MP_DIGITS(z));
|
|
|
|
MP_DIGITS(z) = NULL;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_free(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_int_free(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
NRCHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (z->digits != NULL)
|
|
|
|
mp_int_clear(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
px_free(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_copy(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_copy(mp_int a, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (a != c)
|
|
|
|
{
|
|
|
|
mp_size ua = MP_USED(a);
|
|
|
|
mp_digit *da,
|
|
|
|
*dc;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(c, ua))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
da = MP_DIGITS(a);
|
|
|
|
dc = MP_DIGITS(c);
|
|
|
|
COPY(da, dc, ua);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(c) = ua;
|
|
|
|
MP_SIGN(c) = MP_SIGN(a);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_swap(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_int_swap(mp_int a, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
if (a != c)
|
|
|
|
{
|
|
|
|
mpz_t tmp = *a;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*a = *c;
|
|
|
|
*c = tmp;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_zero(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
mp_int_zero(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
NRCHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
z->digits[0] = 0;
|
|
|
|
MP_USED(z) = 1;
|
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_abs(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_abs(mp_int a, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_copy(a, c)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_SIGN(c) = MP_ZPOS;
|
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_neg(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_neg(mp_int a, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_copy(a, c)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (CMPZ(c) != 0)
|
|
|
|
MP_SIGN(c) = 1 - MP_SIGN(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_add(a, b, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_add(mp_int a, mp_int b, mp_int c)
|
|
|
|
{
|
|
|
|
mp_size ua,
|
|
|
|
ub,
|
|
|
|
uc,
|
|
|
|
max;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && b != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ua = MP_USED(a);
|
|
|
|
ub = MP_USED(b);
|
|
|
|
uc = MP_USED(c);
|
|
|
|
max = MAX(ua, ub);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_SIGN(a) == MP_SIGN(b))
|
|
|
|
{
|
|
|
|
/* Same sign -- add magnitudes, preserve sign of addends */
|
|
|
|
mp_digit carry;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(c, max))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
carry = s_uadd(MP_DIGITS(a), MP_DIGITS(b), MP_DIGITS(c), ua, ub);
|
|
|
|
uc = max;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (carry)
|
|
|
|
{
|
|
|
|
if (!s_pad(c, max + 1))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
c->digits[max] = carry;
|
|
|
|
++uc;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(c) = uc;
|
|
|
|
MP_SIGN(c) = MP_SIGN(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Different signs -- subtract magnitudes, preserve sign of greater */
|
|
|
|
mp_int x,
|
|
|
|
y;
|
|
|
|
int cmp = s_ucmp(a, b); /* magnitude comparision, sign ignored */
|
|
|
|
|
|
|
|
/* Set x to max(a, b), y to min(a, b) to simplify later code */
|
|
|
|
if (cmp >= 0)
|
|
|
|
{
|
|
|
|
x = a;
|
|
|
|
y = b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = b;
|
|
|
|
y = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s_pad(c, MP_USED(x)))
|
|
|
|
return MP_MEMORY;
|
|
|
|
|
|
|
|
/* Subtract smaller from larger */
|
|
|
|
s_usub(MP_DIGITS(x), MP_DIGITS(y), MP_DIGITS(c), MP_USED(x), MP_USED(y));
|
|
|
|
MP_USED(c) = MP_USED(x);
|
|
|
|
CLAMP(c);
|
|
|
|
|
|
|
|
/* Give result the sign of the larger */
|
|
|
|
MP_SIGN(c) = MP_SIGN(x);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_add_value(a, value, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_add_value(mp_int a, int value, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t vtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_add(a, &vtmp, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_sub(a, b, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_sub(mp_int a, mp_int b, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size ua,
|
|
|
|
ub,
|
|
|
|
uc,
|
|
|
|
max;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && b != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ua = MP_USED(a);
|
|
|
|
ub = MP_USED(b);
|
|
|
|
uc = MP_USED(c);
|
|
|
|
max = MAX(ua, ub);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_SIGN(a) != MP_SIGN(b))
|
|
|
|
{
|
|
|
|
/* Different signs -- add magnitudes and keep sign of a */
|
|
|
|
mp_digit carry;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(c, max))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
carry = s_uadd(MP_DIGITS(a), MP_DIGITS(b), MP_DIGITS(c), ua, ub);
|
|
|
|
uc = max;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (carry)
|
|
|
|
{
|
|
|
|
if (!s_pad(c, max + 1))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
c->digits[max] = carry;
|
|
|
|
++uc;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(c) = uc;
|
|
|
|
MP_SIGN(c) = MP_SIGN(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Same signs -- subtract magnitudes */
|
|
|
|
mp_int x,
|
|
|
|
y;
|
|
|
|
mp_sign osign;
|
|
|
|
int cmp = s_ucmp(a, b);
|
|
|
|
|
|
|
|
if (!s_pad(c, max))
|
|
|
|
return MP_MEMORY;
|
|
|
|
|
|
|
|
if (cmp >= 0)
|
|
|
|
{
|
|
|
|
x = a;
|
|
|
|
y = b;
|
|
|
|
osign = MP_ZPOS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = b;
|
|
|
|
y = a;
|
|
|
|
osign = MP_NEG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MP_SIGN(a) == MP_NEG && cmp != 0)
|
|
|
|
osign = 1 - osign;
|
|
|
|
|
|
|
|
s_usub(MP_DIGITS(x), MP_DIGITS(y), MP_DIGITS(c), MP_USED(x), MP_USED(y));
|
|
|
|
MP_USED(c) = MP_USED(x);
|
|
|
|
CLAMP(c);
|
|
|
|
|
|
|
|
MP_SIGN(c) = osign;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* }}} */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* {{{ mp_int_sub_value(a, value, c) */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_sub_value(mp_int a, int value, mp_int c)
|
|
|
|
{
|
|
|
|
mpz_t vtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_sub(a, &vtmp, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* {{{ mp_int_mul(a, b, c) */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_mul(mp_int a, mp_int b, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit *out;
|
|
|
|
mp_size osize,
|
|
|
|
ua,
|
|
|
|
ub,
|
|
|
|
p = 0;
|
|
|
|
mp_sign osign;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && b != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* If either input is zero, we can shortcut multiplication */
|
|
|
|
if (mp_int_compare_zero(a) == 0 || mp_int_compare_zero(b) == 0)
|
|
|
|
{
|
|
|
|
mp_int_zero(c);
|
|
|
|
return MP_OK;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Output is positive if inputs have same sign, otherwise negative */
|
|
|
|
osign = (MP_SIGN(a) == MP_SIGN(b)) ? MP_ZPOS : MP_NEG;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If the output is not equal to any of the inputs, we'll write the
|
|
|
|
* results there directly; otherwise, allocate a temporary space.
|
|
|
|
*/
|
|
|
|
ua = MP_USED(a);
|
|
|
|
ub = MP_USED(b);
|
|
|
|
osize = ua + ub;
|
|
|
|
|
|
|
|
if (c == a || c == b)
|
|
|
|
{
|
|
|
|
p = ROUND_PREC(osize);
|
|
|
|
p = MAX(p, default_precision);
|
|
|
|
|
|
|
|
if ((out = s_alloc(p)) == NULL)
|
|
|
|
return MP_MEMORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!s_pad(c, osize))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
out = MP_DIGITS(c);
|
|
|
|
}
|
|
|
|
ZERO(out, osize);
|
|
|
|
|
|
|
|
if (!s_kmul(MP_DIGITS(a), MP_DIGITS(b), out, ua, ub))
|
|
|
|
return MP_MEMORY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we allocated a new buffer, get rid of whatever memory c was already
|
|
|
|
* using, and fix up its fields to reflect that.
|
|
|
|
*/
|
|
|
|
if (out != MP_DIGITS(c))
|
|
|
|
{
|
|
|
|
s_free(MP_DIGITS(c));
|
|
|
|
MP_DIGITS(c) = out;
|
|
|
|
MP_ALLOC(c) = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_USED(c) = osize; /* might not be true, but we'll fix it ... */
|
|
|
|
CLAMP(c); /* ... right here */
|
|
|
|
MP_SIGN(c) = osign;
|
|
|
|
|
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_mul_value(a, value, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_mul_value(mp_int a, int value, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t vtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_mul(a, &vtmp, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_mul_pow2(a, p2, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_mul_pow2(mp_int a, int p2, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL && p2 >= 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_copy(a, c)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (s_qmul(c, (mp_size) p2))
|
|
|
|
return MP_OK;
|
|
|
|
else
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_sqr(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_sqr(mp_int a, mp_int c)
|
|
|
|
{
|
|
|
|
mp_digit *out;
|
|
|
|
mp_size osize,
|
|
|
|
p = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Get a temporary buffer big enough to hold the result */
|
|
|
|
osize = (mp_size) 2 *MP_USED(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (a == c)
|
|
|
|
{
|
|
|
|
p = ROUND_PREC(osize);
|
|
|
|
p = MAX(p, default_precision);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((out = s_alloc(p)) == NULL)
|
|
|
|
return MP_MEMORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!s_pad(c, osize))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
out = MP_DIGITS(c);
|
|
|
|
}
|
|
|
|
ZERO(out, osize);
|
|
|
|
|
|
|
|
s_ksqr(MP_DIGITS(a), out, MP_USED(a));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get rid of whatever memory c was already using, and fix up its fields
|
|
|
|
* to reflect the new digit array it's using
|
|
|
|
*/
|
|
|
|
if (out != MP_DIGITS(c))
|
|
|
|
{
|
|
|
|
s_free(MP_DIGITS(c));
|
|
|
|
MP_DIGITS(c) = out;
|
|
|
|
MP_ALLOC(c) = p;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(c) = osize; /* might not be true, but we'll fix it ... */
|
|
|
|
CLAMP(c); /* ... right here */
|
|
|
|
MP_SIGN(c) = MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_div(a, b, q, r) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_div(mp_int a, mp_int b, mp_int q, mp_int r)
|
|
|
|
{
|
|
|
|
int cmp,
|
|
|
|
last = 0,
|
|
|
|
lg;
|
|
|
|
mp_result res = MP_OK;
|
|
|
|
mpz_t temp[2];
|
|
|
|
mp_int qout,
|
|
|
|
rout;
|
|
|
|
mp_sign sa = MP_SIGN(a),
|
|
|
|
sb = MP_SIGN(b);
|
|
|
|
|
|
|
|
CHECK(a != NULL && b != NULL && q != r);
|
|
|
|
|
|
|
|
if (CMPZ(b) == 0)
|
|
|
|
return MP_UNDEF;
|
|
|
|
else if ((cmp = s_ucmp(a, b)) < 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If |a| < |b|, no division is required: q = 0, r = a
|
|
|
|
*/
|
|
|
|
if (r && (res = mp_int_copy(a, r)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (q)
|
|
|
|
mp_int_zero(q);
|
|
|
|
|
|
|
|
return MP_OK;
|
|
|
|
}
|
|
|
|
else if (cmp == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If |a| = |b|, no division is required: q = 1 or -1, r = 0
|
|
|
|
*/
|
|
|
|
if (r)
|
|
|
|
mp_int_zero(r);
|
|
|
|
|
|
|
|
if (q)
|
|
|
|
{
|
|
|
|
mp_int_zero(q);
|
|
|
|
q->digits[0] = 1;
|
|
|
|
|
|
|
|
if (sa != sb)
|
|
|
|
MP_SIGN(q) = MP_NEG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When |a| > |b|, real division is required. We need someplace to store
|
|
|
|
* quotient and remainder, but q and r are allowed to be NULL or to
|
|
|
|
* overlap with the inputs.
|
|
|
|
*/
|
|
|
|
if ((lg = s_isp2(b)) < 0)
|
|
|
|
{
|
|
|
|
if (q && b != q && (res = mp_int_copy(a, q)) == MP_OK)
|
|
|
|
{
|
|
|
|
qout = q;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qout = TEMP(last);
|
|
|
|
SETUP(mp_int_init_copy(TEMP(last), a), last);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r && a != r && (res = mp_int_copy(b, r)) == MP_OK)
|
|
|
|
{
|
|
|
|
rout = r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rout = TEMP(last);
|
|
|
|
SETUP(mp_int_init_copy(TEMP(last), b), last);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = s_udiv(qout, rout)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (q && (res = mp_int_copy(a, q)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (r && (res = mp_int_copy(a, r)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
|
|
|
|
if (q)
|
|
|
|
s_qdiv(q, (mp_size) lg);
|
|
|
|
qout = q;
|
|
|
|
if (r)
|
|
|
|
s_qmod(r, (mp_size) lg);
|
|
|
|
rout = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recompute signs for output */
|
|
|
|
if (rout)
|
|
|
|
{
|
|
|
|
MP_SIGN(rout) = sa;
|
|
|
|
if (CMPZ(rout) == 0)
|
|
|
|
MP_SIGN(rout) = MP_ZPOS;
|
|
|
|
}
|
|
|
|
if (qout)
|
|
|
|
{
|
|
|
|
MP_SIGN(qout) = (sa == sb) ? MP_ZPOS : MP_NEG;
|
|
|
|
if (CMPZ(qout) == 0)
|
|
|
|
MP_SIGN(qout) = MP_ZPOS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (q && (res = mp_int_copy(qout, q)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (r && (res = mp_int_copy(rout, r)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
|
|
|
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_mod(a, m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_mod(mp_int a, mp_int m, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
|
|
|
mpz_t tmp;
|
|
|
|
mp_int out;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (m == c)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_init(&tmp)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
out = &tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out = c;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_div(a, m, NULL, out)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (CMPZ(out) < 0)
|
|
|
|
res = mp_int_add(out, m, c);
|
|
|
|
else
|
|
|
|
res = mp_int_copy(out, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
if (out != c)
|
|
|
|
mp_int_clear(&tmp);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
/* {{{ mp_int_div_value(a, value, q, r) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_div_value(mp_int a, int value, mp_int q, int *r)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t vtmp,
|
|
|
|
rtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
|
|
|
mp_result res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_init(&rtmp)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_div(a, &vtmp, q, &rtmp)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (r)
|
|
|
|
(void) mp_int_to_int(&rtmp, r); /* can't fail */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
mp_int_clear(&rtmp);
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_div_pow2(a, p2, q, r) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_div_pow2(mp_int a, int p2, mp_int q, mp_int r)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res = MP_OK;
|
|
|
|
|
|
|
|
CHECK(a != NULL && p2 >= 0 && q != r);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (q != NULL && (res = mp_int_copy(a, q)) == MP_OK)
|
|
|
|
s_qdiv(q, (mp_size) p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (res == MP_OK && r != NULL && (res = mp_int_copy(a, r)) == MP_OK)
|
|
|
|
s_qmod(r, (mp_size) p2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_expt(a, b, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_expt(mp_int a, int b, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t t;
|
|
|
|
mp_result res;
|
|
|
|
unsigned int v = abs(b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(b >= 0 && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_init_copy(&t, a)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
(void) mp_int_set_value(c, 1);
|
|
|
|
while (v != 0)
|
|
|
|
{
|
|
|
|
if (v & 1)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_mul(c, &t, c)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
v >>= 1;
|
|
|
|
if (v == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((res = mp_int_sqr(&t, &t)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
mp_int_clear(&t);
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_expt_value(a, b, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_expt_value(int a, int b, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t t;
|
|
|
|
mp_result res;
|
|
|
|
unsigned int v = abs(b);
|
|
|
|
|
|
|
|
CHECK(b >= 0 && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_init_value(&t, a)) != MP_OK)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
(void) mp_int_set_value(c, 1);
|
|
|
|
while (v != 0)
|
|
|
|
{
|
|
|
|
if (v & 1)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_mul(c, &t, c)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
v >>= 1;
|
|
|
|
if (v == 0)
|
|
|
|
break;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_sqr(&t, &t)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
mp_int_clear(&t);
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_compare(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_compare(mp_int a, mp_int b)
|
|
|
|
{
|
|
|
|
mp_sign sa;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && b != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
sa = MP_SIGN(a);
|
|
|
|
if (sa == MP_SIGN(b))
|
|
|
|
{
|
|
|
|
int cmp = s_ucmp(a, b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If they're both zero or positive, the normal comparison applies; if
|
|
|
|
* both negative, the sense is reversed.
|
|
|
|
*/
|
|
|
|
if (sa == MP_ZPOS)
|
|
|
|
return cmp;
|
|
|
|
else
|
|
|
|
return -cmp;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sa == MP_ZPOS)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_compare_unsigned(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_compare_unsigned(mp_int a, mp_int b)
|
|
|
|
{
|
|
|
|
NRCHECK(a != NULL && b != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return s_ucmp(a, b);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_compare_zero(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_compare_zero(mp_int z)
|
|
|
|
{
|
|
|
|
NRCHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_USED(z) == 1 && z->digits[0] == 0)
|
|
|
|
return 0;
|
|
|
|
else if (MP_SIGN(z) == MP_ZPOS)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_compare_value(z, value) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_compare_value(mp_int z, int value)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_sign vsign = (value < 0) ? MP_NEG : MP_ZPOS;
|
|
|
|
int cmp;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (vsign == MP_SIGN(z))
|
|
|
|
{
|
|
|
|
cmp = s_vcmp(z, value);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (vsign == MP_ZPOS)
|
|
|
|
return cmp;
|
|
|
|
else
|
|
|
|
return -cmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value < 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_exptmod(a, b, m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c)
|
|
|
|
{
|
|
|
|
mp_result res;
|
|
|
|
mp_size um;
|
|
|
|
mpz_t temp[3];
|
|
|
|
mp_int s;
|
|
|
|
int last = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && b != NULL && c != NULL && m != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Zero moduli and negative exponents are not considered. */
|
|
|
|
if (CMPZ(m) == 0)
|
|
|
|
return MP_UNDEF;
|
|
|
|
if (CMPZ(b) < 0)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
um = MP_USED(m);
|
|
|
|
SETUP(mp_int_init_size(TEMP(0), 2 * um), last);
|
|
|
|
SETUP(mp_int_init_size(TEMP(1), 2 * um), last);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (c == b || c == m)
|
|
|
|
{
|
|
|
|
SETUP(mp_int_init_size(TEMP(2), 2 * um), last);
|
|
|
|
s = TEMP(2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = c;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_mod(a, m, TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = s_brmu(TEMP(1), m)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = s_embar(TEMP(0), b, m, TEMP(1), s)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = mp_int_copy(s, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
|
|
|
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_exptmod_evalue(a, value, m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_exptmod_evalue(mp_int a, int value, mp_int m, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t vtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_exptmod(a, &vtmp, m, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_exptmod_bvalue(v, b, m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_exptmod_bvalue(int value, mp_int b,
|
|
|
|
mp_int m, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mpz_t vtmp;
|
|
|
|
mp_digit vbuf[MP_VALUE_DIGITS(value)];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_fake(&vtmp, value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_exptmod(&vtmp, b, m, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_exptmod_known(a, b, m, mu, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_exptmod_known(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
|
|
|
mp_size um;
|
|
|
|
mpz_t temp[2];
|
|
|
|
mp_int s;
|
|
|
|
int last = 0;
|
|
|
|
|
|
|
|
CHECK(a && b && m && c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Zero moduli and negative exponents are not considered. */
|
|
|
|
if (CMPZ(m) == 0)
|
|
|
|
return MP_UNDEF;
|
|
|
|
if (CMPZ(b) < 0)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
um = MP_USED(m);
|
|
|
|
SETUP(mp_int_init_size(TEMP(0), 2 * um), last);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (c == b || c == m)
|
|
|
|
{
|
|
|
|
SETUP(mp_int_init_size(TEMP(1), 2 * um), last);
|
|
|
|
s = TEMP(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = c;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_mod(a, m, TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = s_embar(TEMP(0), b, m, mu, s)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = mp_int_copy(s, c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_redux_const(m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_redux_const(mp_int m, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(m != NULL && c != NULL && m != c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return s_brmu(c, m);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_invmod(a, m, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_invmod(mp_int a, mp_int m, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
|
|
|
mp_sign sa;
|
|
|
|
int last = 0;
|
|
|
|
mpz_t temp[2];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && m != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (CMPZ(a) == 0 || CMPZ(m) <= 0)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
sa = MP_SIGN(a); /* need this for the result later */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (last = 0; last < 2; ++last)
|
|
|
|
if ((res = mp_int_init(TEMP(last))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_egcd(a, m, TEMP(0), TEMP(1), NULL)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (mp_int_compare_value(TEMP(0), 1) != 0)
|
|
|
|
{
|
|
|
|
res = MP_UNDEF;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* It is first necessary to constrain the value to the proper range */
|
|
|
|
if ((res = mp_int_mod(TEMP(1), m, TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* Now, if 'a' was originally negative, the value we have is actually the
|
|
|
|
* magnitude of the negative representative; to get the positive value we
|
|
|
|
* have to subtract from the modulus. Otherwise, the value is okay as it
|
|
|
|
* stands.
|
|
|
|
*/
|
|
|
|
if (sa == MP_NEG)
|
|
|
|
res = mp_int_sub(m, TEMP(1), c);
|
|
|
|
else
|
|
|
|
res = mp_int_copy(TEMP(1), c);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_gcd(a, b, c) */
|
|
|
|
|
|
|
|
/* Binary GCD algorithm due to Josef Stein, 1961 */
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_gcd(mp_int a, mp_int b, mp_int c)
|
|
|
|
{
|
|
|
|
int ca,
|
|
|
|
cb,
|
|
|
|
k = 0;
|
|
|
|
mpz_t u,
|
|
|
|
v,
|
|
|
|
t;
|
|
|
|
mp_result res;
|
|
|
|
|
|
|
|
CHECK(a != NULL && b != NULL && c != NULL);
|
|
|
|
|
|
|
|
ca = CMPZ(a);
|
|
|
|
cb = CMPZ(b);
|
|
|
|
if (ca == 0 && cb == 0)
|
|
|
|
return MP_UNDEF;
|
|
|
|
else if (ca == 0)
|
|
|
|
return mp_int_abs(b, c);
|
|
|
|
else if (cb == 0)
|
|
|
|
return mp_int_abs(a, c);
|
|
|
|
|
|
|
|
if ((res = mp_int_init(&t)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
if ((res = mp_int_init_copy(&u, a)) != MP_OK)
|
|
|
|
goto U;
|
|
|
|
if ((res = mp_int_init_copy(&v, b)) != MP_OK)
|
|
|
|
goto V;
|
|
|
|
|
|
|
|
MP_SIGN(&u) = MP_ZPOS;
|
|
|
|
MP_SIGN(&v) = MP_ZPOS;
|
|
|
|
|
|
|
|
{ /* Divide out common factors of 2 from u and v */
|
|
|
|
int div2_u = s_dp2k(&u),
|
|
|
|
div2_v = s_dp2k(&v);
|
|
|
|
|
|
|
|
k = MIN(div2_u, div2_v);
|
|
|
|
s_qdiv(&u, (mp_size) k);
|
|
|
|
s_qdiv(&v, (mp_size) k);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_int_is_odd(&u))
|
|
|
|
{
|
|
|
|
if ((res = mp_int_neg(&v, &t)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((res = mp_int_copy(&u, &t)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
s_qdiv(&t, s_dp2k(&t));
|
|
|
|
|
|
|
|
if (CMPZ(&t) > 0)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_copy(&t, &u)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((res = mp_int_neg(&t, &v)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_int_sub(&u, &v, &t)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
|
|
|
|
if (CMPZ(&t) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_int_abs(&u, c)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (!s_qmul(c, (mp_size) k))
|
|
|
|
res = MP_MEMORY;
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
mp_int_clear(&v);
|
|
|
|
V: mp_int_clear(&u);
|
|
|
|
U: mp_int_clear(&t);
|
|
|
|
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_egcd(a, b, c, x, y) */
|
|
|
|
|
|
|
|
/* This is the binary GCD algorithm again, but this time we keep track
|
|
|
|
of the elementary matrix operations as we go, so we can get values
|
|
|
|
x and y satisfying c = ax + by.
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_egcd(mp_int a, mp_int b, mp_int c,
|
|
|
|
mp_int x, mp_int y)
|
|
|
|
{
|
|
|
|
int k,
|
|
|
|
last = 0,
|
|
|
|
ca,
|
|
|
|
cb;
|
|
|
|
mpz_t temp[8];
|
|
|
|
mp_result res;
|
|
|
|
|
|
|
|
CHECK(a != NULL && b != NULL && c != NULL &&
|
|
|
|
(x != NULL || y != NULL));
|
|
|
|
|
|
|
|
ca = CMPZ(a);
|
|
|
|
cb = CMPZ(b);
|
|
|
|
if (ca == 0 && cb == 0)
|
|
|
|
return MP_UNDEF;
|
|
|
|
else if (ca == 0)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_abs(b, c)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
mp_int_zero(x);
|
|
|
|
(void) mp_int_set_value(y, 1);
|
|
|
|
return MP_OK;
|
|
|
|
}
|
|
|
|
else if (cb == 0)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_abs(a, c)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
(void) mp_int_set_value(x, 1);
|
|
|
|
mp_int_zero(y);
|
|
|
|
return MP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize temporaries: A:0, B:1, C:2, D:3, u:4, v:5, ou:6, ov:7
|
|
|
|
*/
|
|
|
|
for (last = 0; last < 4; ++last)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_init(TEMP(last))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
TEMP(0)->digits[0] = 1;
|
|
|
|
TEMP(3)->digits[0] = 1;
|
|
|
|
|
|
|
|
SETUP(mp_int_init_copy(TEMP(4), a), last);
|
|
|
|
SETUP(mp_int_init_copy(TEMP(5), b), last);
|
|
|
|
|
|
|
|
/* We will work with absolute values here */
|
|
|
|
MP_SIGN(TEMP(4)) = MP_ZPOS;
|
|
|
|
MP_SIGN(TEMP(5)) = MP_ZPOS;
|
|
|
|
|
|
|
|
{ /* Divide out common factors of 2 from u and v */
|
|
|
|
int div2_u = s_dp2k(TEMP(4)),
|
|
|
|
div2_v = s_dp2k(TEMP(5));
|
|
|
|
|
|
|
|
k = MIN(div2_u, div2_v);
|
|
|
|
s_qdiv(TEMP(4), k);
|
|
|
|
s_qdiv(TEMP(5), k);
|
|
|
|
}
|
|
|
|
|
|
|
|
SETUP(mp_int_init_copy(TEMP(6), TEMP(4)), last);
|
|
|
|
SETUP(mp_int_init_copy(TEMP(7), TEMP(5)), last);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while (mp_int_is_even(TEMP(4)))
|
|
|
|
{
|
|
|
|
s_qdiv(TEMP(4), 1);
|
|
|
|
|
|
|
|
if (mp_int_is_odd(TEMP(0)) || mp_int_is_odd(TEMP(1)))
|
|
|
|
{
|
|
|
|
if ((res = mp_int_add(TEMP(0), TEMP(7), TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(1), TEMP(6), TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_qdiv(TEMP(0), 1);
|
|
|
|
s_qdiv(TEMP(1), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mp_int_is_even(TEMP(5)))
|
|
|
|
{
|
|
|
|
s_qdiv(TEMP(5), 1);
|
|
|
|
|
|
|
|
if (mp_int_is_odd(TEMP(2)) || mp_int_is_odd(TEMP(3)))
|
|
|
|
{
|
|
|
|
if ((res = mp_int_add(TEMP(2), TEMP(7), TEMP(2))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(3), TEMP(6), TEMP(3))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_qdiv(TEMP(2), 1);
|
|
|
|
s_qdiv(TEMP(3), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_int_compare(TEMP(4), TEMP(5)) >= 0)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_sub(TEMP(4), TEMP(5), TEMP(4))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(0), TEMP(2), TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(1), TEMP(3), TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((res = mp_int_sub(TEMP(5), TEMP(4), TEMP(5))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(2), TEMP(0), TEMP(2))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_sub(TEMP(3), TEMP(1), TEMP(3))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CMPZ(TEMP(4)) == 0)
|
|
|
|
{
|
|
|
|
if (x && (res = mp_int_copy(TEMP(2), x)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (y && (res = mp_int_copy(TEMP(3), y)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
if (!s_qmul(TEMP(5), k))
|
|
|
|
{
|
|
|
|
res = MP_MEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = mp_int_copy(TEMP(5), c);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
|
|
|
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_divisible_value(a, v) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_divisible_value(mp_int a, int v)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int rem = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (mp_int_div_value(a, v, NULL, &rem) != MP_OK)
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return rem == 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_is_pow2(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
int
|
|
|
|
mp_int_is_pow2(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return s_isp2(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_sqrt(a, c) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_sqrt(mp_int a, mp_int c)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res = MP_OK;
|
|
|
|
mpz_t temp[2];
|
|
|
|
int last = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(a != NULL && c != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* The square root of a negative value does not exist in the integers. */
|
|
|
|
if (MP_SIGN(a) == MP_NEG)
|
|
|
|
return MP_UNDEF;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
SETUP(mp_int_init_copy(TEMP(last), a), last);
|
|
|
|
SETUP(mp_int_init(TEMP(last)), last);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if ((res = mp_int_sqr(TEMP(0), TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (mp_int_compare_unsigned(a, TEMP(1)) == 0)
|
|
|
|
break;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_copy(a, TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_div(TEMP(1), TEMP(0), TEMP(1), NULL)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_add(TEMP(0), TEMP(1), TEMP(1))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if ((res = mp_int_div_pow2(TEMP(1), 1, TEMP(1), NULL)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (mp_int_compare_unsigned(TEMP(0), TEMP(1)) == 0)
|
|
|
|
break;
|
|
|
|
if ((res = mp_int_sub_value(TEMP(0), 1, TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
if (mp_int_compare_unsigned(TEMP(0), TEMP(1)) == 0)
|
|
|
|
break;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = mp_int_copy(TEMP(1), TEMP(0))) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = mp_int_copy(TEMP(0), c);
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
|
|
|
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_to_int(z, out) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_to_int(mp_int z, int *out)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned int uv = 0;
|
|
|
|
mp_size uz;
|
|
|
|
mp_digit *dz;
|
|
|
|
mp_sign sz;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Make sure the value is representable as an int */
|
|
|
|
sz = MP_SIGN(z);
|
|
|
|
if ((sz == MP_ZPOS && mp_int_compare_value(z, INT_MAX) > 0) ||
|
|
|
|
mp_int_compare_value(z, INT_MIN) < 0)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
uz = MP_USED(z);
|
|
|
|
dz = MP_DIGITS(z) + uz - 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (uz > 0)
|
|
|
|
{
|
|
|
|
uv <<= MP_DIGIT_BIT / 2;
|
|
|
|
uv = (uv << (MP_DIGIT_BIT / 2)) | *dz--;
|
|
|
|
--uz;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out)
|
|
|
|
*out = (sz == MP_NEG) ? -(int) uv : (int) uv;
|
|
|
|
|
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_to_string(z, radix, str, limit) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_to_string(mp_int z, mp_size radix,
|
|
|
|
char *str, int limit)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res;
|
|
|
|
int cmp = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && str != NULL && limit >= 2);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (radix < MP_MIN_RADIX || radix > MP_MAX_RADIX)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (CMPZ(z) == 0)
|
|
|
|
{
|
|
|
|
*str++ = s_val2ch(0, mp_flags & MP_CAP_DIGITS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mpz_t tmp;
|
|
|
|
char *h,
|
|
|
|
*t;
|
|
|
|
|
|
|
|
if ((res = mp_int_init_copy(&tmp, z)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (MP_SIGN(z) == MP_NEG)
|
|
|
|
{
|
|
|
|
*str++ = '-';
|
|
|
|
--limit;
|
|
|
|
}
|
|
|
|
h = str;
|
|
|
|
|
|
|
|
/* Generate digits in reverse order until finished or limit reached */
|
|
|
|
for ( /* */ ; limit > 0; --limit)
|
|
|
|
{
|
|
|
|
mp_digit d;
|
|
|
|
|
|
|
|
if ((cmp = CMPZ(&tmp)) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
d = s_ddiv(&tmp, (mp_digit) radix);
|
|
|
|
*str++ = s_val2ch(d, mp_flags & MP_CAP_DIGITS);
|
|
|
|
}
|
|
|
|
t = str - 1;
|
|
|
|
|
|
|
|
/* Put digits back in correct output order */
|
|
|
|
while (h < t)
|
|
|
|
{
|
|
|
|
char tc = *h;
|
|
|
|
|
|
|
|
*h++ = *t;
|
|
|
|
*t-- = tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_int_clear(&tmp);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*str = '\0';
|
|
|
|
if (cmp == 0)
|
|
|
|
return MP_OK;
|
|
|
|
else
|
|
|
|
return MP_TRUNC;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_string_len(z, radix) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_string_len(mp_int z, mp_size radix)
|
|
|
|
{
|
|
|
|
int len;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (radix < MP_MIN_RADIX || radix > MP_MAX_RADIX)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
len = s_outlen(z, radix) + 1; /* for terminator */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Allow for sign marker on negatives */
|
|
|
|
if (MP_SIGN(z) == MP_NEG)
|
|
|
|
len += 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return len;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_read_string(z, radix, *str) */
|
|
|
|
|
|
|
|
/* Read zero-terminated string into z */
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_read_string(mp_int z, mp_size radix, const char *str)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
return mp_int_read_cstring(z, radix, str, NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_read_cstring(z, radix, *str, **end) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_read_cstring(mp_int z, mp_size radix, const char *str, char **end)
|
|
|
|
{
|
|
|
|
int ch;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && str != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (radix < MP_MIN_RADIX || radix > MP_MAX_RADIX)
|
|
|
|
return MP_RANGE;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Skip leading whitespace */
|
|
|
|
while (isspace((unsigned char) *str))
|
|
|
|
++str;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Handle leading sign tag (+/-, positive default) */
|
|
|
|
switch (*str)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
MP_SIGN(z) = MP_NEG;
|
|
|
|
++str;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
++str; /* fallthrough */
|
|
|
|
default:
|
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip leading zeroes */
|
|
|
|
while ((ch = s_ch2val(*str, radix)) == 0)
|
|
|
|
++str;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Make sure there is enough space for the value */
|
|
|
|
if (!s_pad(z, s_inlen(strlen(str), radix)))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = 1;
|
|
|
|
z->digits[0] = 0;
|
|
|
|
|
|
|
|
while (*str != '\0' && ((ch = s_ch2val(*str, radix)) >= 0))
|
|
|
|
{
|
|
|
|
s_dmul(z, (mp_digit) radix);
|
|
|
|
s_dadd(z, (mp_digit) ch);
|
|
|
|
++str;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLAMP(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Override sign for zero, even if negative specified. */
|
|
|
|
if (CMPZ(z) == 0)
|
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (end != NULL)
|
|
|
|
*end = (char *) str;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* Return a truncation error if the string has unprocessed characters
|
|
|
|
* remaining, so the caller can tell if the whole string was done
|
|
|
|
*/
|
|
|
|
if (*str != '\0')
|
|
|
|
return MP_TRUNC;
|
|
|
|
else
|
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_count_bits(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_count_bits(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size nbits = 0,
|
|
|
|
uz;
|
|
|
|
mp_digit d;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
uz = MP_USED(z);
|
|
|
|
if (uz == 1 && z->digits[0] == 0)
|
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
--uz;
|
|
|
|
nbits = uz * MP_DIGIT_BIT;
|
|
|
|
d = z->digits[uz];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (d != 0)
|
|
|
|
{
|
|
|
|
d >>= 1;
|
|
|
|
++nbits;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return nbits;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_to_binary(z, buf, limit) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_to_binary(mp_int z, unsigned char *buf, int limit)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
static const int PAD_FOR_2C = 1;
|
|
|
|
|
|
|
|
mp_result res;
|
|
|
|
int limpos = limit;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && buf != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = s_tobin(z, buf, &limpos, PAD_FOR_2C);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_SIGN(z) == MP_NEG)
|
|
|
|
s_2comp(buf, limpos);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_read_binary(z, buf, len) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_read_binary(mp_int z, unsigned char *buf, int len)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size need,
|
|
|
|
i;
|
|
|
|
unsigned char *tmp;
|
|
|
|
mp_digit *dz;
|
|
|
|
|
|
|
|
CHECK(z != NULL && buf != NULL && len > 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Figure out how many digits are needed to represent this value */
|
|
|
|
need = ((len * CHAR_BIT) + (MP_DIGIT_BIT - 1)) / MP_DIGIT_BIT;
|
|
|
|
if (!s_pad(z, need))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_int_zero(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If the high-order bit is set, take the 2's complement before reading
|
|
|
|
* the value (it will be restored afterward)
|
|
|
|
*/
|
|
|
|
if (buf[0] >> (CHAR_BIT - 1))
|
|
|
|
{
|
|
|
|
MP_SIGN(z) = MP_NEG;
|
|
|
|
s_2comp(buf, len);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
dz = MP_DIGITS(z);
|
|
|
|
for (tmp = buf, i = len; i > 0; --i, ++tmp)
|
|
|
|
{
|
|
|
|
s_qmul(z, (mp_size) CHAR_BIT);
|
|
|
|
*dz |= *tmp;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Restore 2's complement if we took it before */
|
|
|
|
if (MP_SIGN(z) == MP_NEG)
|
|
|
|
s_2comp(buf, len);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_binary_len(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_binary_len(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res = mp_int_count_bits(z);
|
|
|
|
int bytes = mp_int_unsigned_len(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
bytes = (res + (CHAR_BIT - 1)) / CHAR_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If the highest-order bit falls exactly on a byte boundary, we need to
|
|
|
|
* pad with an extra byte so that the sign will be read correctly when
|
|
|
|
* reading it back in.
|
|
|
|
*/
|
|
|
|
if (bytes * CHAR_BIT == res)
|
|
|
|
++bytes;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return bytes;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_to_unsigned(z, buf, limit) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_to_unsigned(mp_int z, unsigned char *buf, int limit)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
static const int NO_PADDING = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && buf != NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return s_tobin(z, buf, &limit, NO_PADDING);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_read_unsigned(z, buf, len) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_read_unsigned(mp_int z, unsigned char *buf, int len)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size need,
|
|
|
|
i;
|
|
|
|
unsigned char *tmp;
|
|
|
|
mp_digit *dz;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CHECK(z != NULL && buf != NULL && len > 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Figure out how many digits are needed to represent this value */
|
|
|
|
need = ((len * CHAR_BIT) + (MP_DIGIT_BIT - 1)) / MP_DIGIT_BIT;
|
|
|
|
if (!s_pad(z, need))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_int_zero(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
dz = MP_DIGITS(z);
|
|
|
|
for (tmp = buf, i = len; i > 0; --i, ++tmp)
|
|
|
|
{
|
|
|
|
(void) s_qmul(z, CHAR_BIT);
|
|
|
|
*dz |= *tmp;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return MP_OK;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_int_unsigned_len(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result
|
|
|
|
mp_int_unsigned_len(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result res = mp_int_count_bits(z);
|
|
|
|
int bytes;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
bytes = (res + (CHAR_BIT - 1)) / CHAR_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return bytes;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ mp_error_string(res) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *
|
|
|
|
mp_error_string(mp_result res)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int ix;
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
return s_unknown_err;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = -res;
|
|
|
|
for (ix = 0; ix < res && s_error_msg[ix] != NULL; ++ix)
|
|
|
|
;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (s_error_msg[ix] != NULL)
|
|
|
|
return s_error_msg[ix];
|
|
|
|
else
|
|
|
|
return s_unknown_err;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Private functions for internal use. These make assumptions. */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
/* {{{ s_alloc(num) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit *
|
|
|
|
s_alloc(mp_size num)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit *out = px_alloc(num * sizeof(mp_digit));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
assert(out != NULL); /* for debugging */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return out;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_realloc(old, num) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit *
|
|
|
|
s_realloc(mp_digit * old, mp_size num)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit *new = px_realloc(old, num * sizeof(mp_digit));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
assert(new != NULL); /* for debugging */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return new;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_free(ptr) */
|
|
|
|
|
|
|
|
#if TRACEABLE_FREE
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_free(void *ptr)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
px_free(ptr);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_pad(z, min) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_pad(mp_int z, mp_size min)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_ALLOC(z) < min)
|
|
|
|
{
|
|
|
|
mp_size nsize = ROUND_PREC(min);
|
|
|
|
mp_digit *tmp = s_realloc(MP_DIGITS(z), nsize);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (tmp == NULL)
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_DIGITS(z) = tmp;
|
|
|
|
MP_ALLOC(z) = nsize;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_clamp(z) */
|
|
|
|
|
|
|
|
#if TRACEABLE_CLAMP
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_clamp(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size uz = MP_USED(z);
|
|
|
|
mp_digit *zd = MP_DIGITS(z) + uz - 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (uz > 1 && (*zd-- == 0))
|
|
|
|
--uz;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = uz;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_fake(z, value, vbuf) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_fake(mp_int z, int value, mp_digit vbuf[])
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size uv = (mp_size) s_vpack(value, vbuf);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
z->used = uv;
|
|
|
|
z->alloc = MP_VALUE_DIGITS(value);
|
|
|
|
z->sign = (value < 0) ? MP_NEG : MP_ZPOS;
|
|
|
|
z->digits = vbuf;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_cdig(da, db, len) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_cdig(mp_digit * da, mp_digit * db, mp_size len)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit *dat = da + len - 1,
|
|
|
|
*dbt = db + len - 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for ( /* */ ; len != 0; --len, --dat, --dbt)
|
|
|
|
{
|
|
|
|
if (*dat > *dbt)
|
|
|
|
return 1;
|
|
|
|
else if (*dat < *dbt)
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_vpack(v, t[]) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_vpack(int v, mp_digit t[])
|
|
|
|
{
|
|
|
|
unsigned int uv = (unsigned int) ((v < 0) ? -v : v);
|
|
|
|
int ndig = 0;
|
|
|
|
|
|
|
|
if (uv == 0)
|
|
|
|
t[ndig++] = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (uv != 0)
|
|
|
|
{
|
|
|
|
t[ndig++] = (mp_digit) uv;
|
|
|
|
uv >>= MP_DIGIT_BIT / 2;
|
|
|
|
uv >>= MP_DIGIT_BIT / 2;
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return ndig;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_ucmp(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_ucmp(mp_int a, mp_int b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size ua = MP_USED(a),
|
|
|
|
ub = MP_USED(b);
|
|
|
|
|
|
|
|
if (ua > ub)
|
|
|
|
return 1;
|
|
|
|
else if (ub > ua)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return s_cdig(MP_DIGITS(a), MP_DIGITS(b), ua);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_vcmp(a, v) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_vcmp(mp_int a, int v)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit vdig[MP_VALUE_DIGITS(v)];
|
|
|
|
int ndig = 0;
|
|
|
|
mp_size ua = MP_USED(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ndig = s_vpack(v, vdig);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ua > ndig)
|
|
|
|
return 1;
|
|
|
|
else if (ua < ndig)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return s_cdig(MP_DIGITS(a), vdig, ndig);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_uadd(da, db, dc, size_a, size_b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit
|
|
|
|
s_uadd(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size pos;
|
|
|
|
mp_word w = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Insure that da is the longer of the two to simplify later code */
|
|
|
|
if (size_b > size_a)
|
|
|
|
{
|
|
|
|
SWAP(mp_digit *, da, db);
|
|
|
|
SWAP(mp_size, size_a, size_b);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Add corresponding digits until the shorter number runs out */
|
|
|
|
for (pos = 0; pos < size_b; ++pos, ++da, ++db, ++dc)
|
|
|
|
{
|
|
|
|
w = w + (mp_word) * da + (mp_word) * db;
|
|
|
|
*dc = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Propagate carries as far as necessary */
|
|
|
|
for ( /* */ ; pos < size_a; ++pos, ++da, ++dc)
|
|
|
|
{
|
|
|
|
w = w + *da;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dc = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Return carry out */
|
|
|
|
return (mp_digit) w;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_usub(da, db, dc, size_a, size_b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_usub(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size pos;
|
|
|
|
mp_word w = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* We assume that |a| >= |b| so this should definitely hold */
|
|
|
|
assert(size_a >= size_b);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Subtract corresponding digits and propagate borrow */
|
|
|
|
for (pos = 0; pos < size_b; ++pos, ++da, ++db, ++dc)
|
|
|
|
{
|
|
|
|
w = ((mp_word) MP_DIGIT_MAX + 1 + /* MP_RADIX */
|
|
|
|
(mp_word) * da) - w - (mp_word) * db;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dc = LOWER_HALF(w);
|
|
|
|
w = (UPPER_HALF(w) == 0);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Finish the subtraction for remaining upper digits of da */
|
|
|
|
for ( /* */ ; pos < size_a; ++pos, ++da, ++dc)
|
|
|
|
{
|
|
|
|
w = ((mp_word) MP_DIGIT_MAX + 1 + /* MP_RADIX */
|
|
|
|
(mp_word) * da) - w;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dc = LOWER_HALF(w);
|
|
|
|
w = (UPPER_HALF(w) == 0);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* If there is a borrow out at the end, it violates the precondition */
|
|
|
|
assert(w == 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_kmul(da, db, dc, size_a, size_b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_kmul(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size bot_size;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Make sure b is the smaller of the two input values */
|
|
|
|
if (size_b > size_a)
|
|
|
|
{
|
|
|
|
SWAP(mp_digit *, da, db);
|
|
|
|
SWAP(mp_size, size_a, size_b);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* Insure that the bottom is the larger half in an odd-length split; the
|
|
|
|
* code below relies on this being true.
|
|
|
|
*/
|
|
|
|
bot_size = (size_a + 1) / 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the values are big enough to bother with recursion, use the
|
|
|
|
* Karatsuba algorithm to compute the product; otherwise use the normal
|
|
|
|
* multiplication algorithm
|
|
|
|
*/
|
|
|
|
if (multiply_threshold &&
|
|
|
|
size_a >= multiply_threshold &&
|
|
|
|
size_b > bot_size)
|
|
|
|
{
|
|
|
|
|
|
|
|
mp_digit *t1,
|
|
|
|
*t2,
|
|
|
|
*t3,
|
|
|
|
carry;
|
|
|
|
|
|
|
|
mp_digit *a_top = da + bot_size;
|
|
|
|
mp_digit *b_top = db + bot_size;
|
|
|
|
|
|
|
|
mp_size at_size = size_a - bot_size;
|
|
|
|
mp_size bt_size = size_b - bot_size;
|
|
|
|
mp_size buf_size = 2 * bot_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a single allocation for all three temporary buffers needed; each
|
|
|
|
* buffer must be big enough to hold the product of two bottom halves,
|
|
|
|
* and one buffer needs space for the completed product; twice the
|
|
|
|
* space is plenty.
|
|
|
|
*/
|
|
|
|
if ((t1 = s_alloc(4 * buf_size)) == NULL)
|
|
|
|
return 0;
|
|
|
|
t2 = t1 + buf_size;
|
|
|
|
t3 = t2 + buf_size;
|
|
|
|
ZERO(t1, 4 * buf_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* t1 and t2 are initially used as temporaries to compute the inner
|
|
|
|
* product (a1 + a0)(b1 + b0) = a1b1 + a1b0 + a0b1 + a0b0
|
|
|
|
*/
|
|
|
|
carry = s_uadd(da, a_top, t1, bot_size, at_size); /* t1 = a1 + a0 */
|
|
|
|
t1[bot_size] = carry;
|
|
|
|
|
|
|
|
carry = s_uadd(db, b_top, t2, bot_size, bt_size); /* t2 = b1 + b0 */
|
|
|
|
t2[bot_size] = carry;
|
|
|
|
|
|
|
|
(void) s_kmul(t1, t2, t3, bot_size + 1, bot_size + 1); /* t3 = t1 * t2 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we'll get t1 = a0b0 and t2 = a1b1, and subtract them out so
|
|
|
|
* that we're left with only the pieces we want: t3 = a1b0 + a0b1
|
|
|
|
*/
|
|
|
|
ZERO(t1, bot_size + 1);
|
|
|
|
ZERO(t2, bot_size + 1);
|
|
|
|
(void) s_kmul(da, db, t1, bot_size, bot_size); /* t1 = a0 * b0 */
|
|
|
|
(void) s_kmul(a_top, b_top, t2, at_size, bt_size); /* t2 = a1 * b1 */
|
|
|
|
|
|
|
|
/* Subtract out t1 and t2 to get the inner product */
|
|
|
|
s_usub(t3, t1, t3, buf_size + 2, buf_size);
|
|
|
|
s_usub(t3, t2, t3, buf_size + 2, buf_size);
|
|
|
|
|
|
|
|
/* Assemble the output value */
|
|
|
|
COPY(t1, dc, buf_size);
|
|
|
|
(void) s_uadd(t3, dc + bot_size, dc + bot_size,
|
|
|
|
buf_size + 1, buf_size + 1);
|
|
|
|
|
|
|
|
(void) s_uadd(t2, dc + 2 * bot_size, dc + 2 * bot_size,
|
|
|
|
buf_size, buf_size);
|
|
|
|
|
|
|
|
s_free(t1); /* note t2 and t3 are just internal pointers
|
|
|
|
* to t1 */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_umul(da, db, dc, size_a, size_b);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_umul(da, db, dc, size_a, size_b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_umul(mp_digit * da, mp_digit * db, mp_digit * dc,
|
|
|
|
mp_size size_a, mp_size size_b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size a,
|
|
|
|
b;
|
|
|
|
mp_word w;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (a = 0; a < size_a; ++a, ++dc, ++da)
|
|
|
|
{
|
|
|
|
mp_digit *dct = dc;
|
|
|
|
mp_digit *dbt = db;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (*da == 0)
|
|
|
|
continue;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
w = 0;
|
|
|
|
for (b = 0; b < size_b; ++b, ++dbt, ++dct)
|
|
|
|
{
|
|
|
|
w = (mp_word) * da * (mp_word) * dbt + w + (mp_word) * dct;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dct = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dct = (mp_digit) w;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_ksqr(da, dc, size_a) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_ksqr(mp_digit * da, mp_digit * dc, mp_size size_a)
|
|
|
|
{
|
|
|
|
if (multiply_threshold && size_a > multiply_threshold)
|
|
|
|
{
|
|
|
|
mp_size bot_size = (size_a + 1) / 2;
|
|
|
|
mp_digit *a_top = da + bot_size;
|
|
|
|
mp_digit *t1,
|
|
|
|
*t2,
|
|
|
|
*t3;
|
|
|
|
mp_size at_size = size_a - bot_size;
|
|
|
|
mp_size buf_size = 2 * bot_size;
|
|
|
|
|
|
|
|
if ((t1 = s_alloc(4 * buf_size)) == NULL)
|
|
|
|
return 0;
|
|
|
|
t2 = t1 + buf_size;
|
|
|
|
t3 = t2 + buf_size;
|
|
|
|
ZERO(t1, 4 * buf_size);
|
|
|
|
|
|
|
|
(void) s_ksqr(da, t1, bot_size); /* t1 = a0 ^ 2 */
|
|
|
|
(void) s_ksqr(a_top, t2, at_size); /* t2 = a1 ^ 2 */
|
|
|
|
|
|
|
|
(void) s_kmul(da, a_top, t3, bot_size, at_size); /* t3 = a0 * a1 */
|
|
|
|
|
|
|
|
/* Quick multiply t3 by 2, shifting left (can't overflow) */
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
top = bot_size + at_size;
|
|
|
|
mp_word w,
|
|
|
|
save = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < top; ++i)
|
|
|
|
{
|
|
|
|
w = t3[i];
|
|
|
|
w = (w << 1) | save;
|
|
|
|
t3[i] = LOWER_HALF(w);
|
|
|
|
save = UPPER_HALF(w);
|
|
|
|
}
|
|
|
|
t3[i] = LOWER_HALF(save);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble the output value */
|
|
|
|
COPY(t1, dc, 2 * bot_size);
|
|
|
|
(void) s_uadd(t3, dc + bot_size, dc + bot_size,
|
|
|
|
buf_size + 1, buf_size + 1);
|
|
|
|
|
|
|
|
(void) s_uadd(t2, dc + 2 * bot_size, dc + 2 * bot_size,
|
|
|
|
buf_size, buf_size);
|
|
|
|
|
|
|
|
px_free(t1); /* note that t2 and t2 are internal pointers
|
|
|
|
* only */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_usqr(da, dc, size_a);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_usqr(da, dc, size_a) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_usqr(mp_digit * da, mp_digit * dc, mp_size size_a)
|
|
|
|
{
|
|
|
|
mp_size i,
|
|
|
|
j;
|
|
|
|
mp_word w;
|
|
|
|
|
|
|
|
for (i = 0; i < size_a; ++i, dc += 2, ++da)
|
|
|
|
{
|
|
|
|
mp_digit *dct = dc,
|
|
|
|
*dat = da;
|
|
|
|
|
|
|
|
if (*da == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Take care of the first digit, no rollover */
|
|
|
|
w = (mp_word) * dat * (mp_word) * dat + (mp_word) * dct;
|
|
|
|
*dct = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
++dat;
|
|
|
|
++dct;
|
|
|
|
|
|
|
|
for (j = i + 1; j < size_a; ++j, ++dat, ++dct)
|
|
|
|
{
|
|
|
|
mp_word t = (mp_word) * da * (mp_word) * dat;
|
|
|
|
mp_word u = w + (mp_word) * dct,
|
|
|
|
ov = 0;
|
|
|
|
|
|
|
|
/* Check if doubling t will overflow a word */
|
|
|
|
if (HIGH_BIT_SET(t))
|
|
|
|
ov = 1;
|
|
|
|
|
|
|
|
w = t + t;
|
|
|
|
|
|
|
|
/* Check if adding u to w will overflow a word */
|
|
|
|
if (ADD_WILL_OVERFLOW(w, u))
|
|
|
|
ov = 1;
|
|
|
|
|
|
|
|
w += u;
|
|
|
|
|
|
|
|
*dct = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
if (ov)
|
|
|
|
{
|
|
|
|
w += MP_DIGIT_MAX; /* MP_RADIX */
|
|
|
|
++w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w = w + *dct;
|
|
|
|
*dct = (mp_digit) w;
|
|
|
|
while ((w = UPPER_HALF(w)) != 0)
|
|
|
|
{
|
|
|
|
++dct;
|
|
|
|
w = w + *dct;
|
|
|
|
*dct = LOWER_HALF(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(w == 0);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_dadd(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_dadd(mp_int a, mp_digit b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_word w = 0;
|
|
|
|
mp_digit *da = MP_DIGITS(a);
|
|
|
|
mp_size ua = MP_USED(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
w = (mp_word) * da + b;
|
|
|
|
*da++ = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (ua -= 1; ua > 0; --ua, ++da)
|
|
|
|
{
|
|
|
|
w = (mp_word) * da + w;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*da = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (w)
|
|
|
|
{
|
|
|
|
*da = (mp_digit) w;
|
|
|
|
MP_USED(a) += 1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_dmul(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_dmul(mp_int a, mp_digit b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_word w = 0;
|
|
|
|
mp_digit *da = MP_DIGITS(a);
|
|
|
|
mp_size ua = MP_USED(a);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (ua > 0)
|
|
|
|
{
|
|
|
|
w = (mp_word) * da * b + w;
|
|
|
|
*da++ = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
--ua;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (w)
|
|
|
|
{
|
|
|
|
*da = (mp_digit) w;
|
|
|
|
MP_USED(a) += 1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_dbmul(da, b, dc, size_a) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_dbmul(mp_digit * da, mp_digit b, mp_digit * dc, mp_size size_a)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_word w = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (size_a > 0)
|
|
|
|
{
|
|
|
|
w = (mp_word) * da++ * (mp_word) b + w;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dc++ = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w);
|
|
|
|
--size_a;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (w)
|
|
|
|
*dc = LOWER_HALF(w);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_ddiv(da, d, dc, size_a) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_digit
|
|
|
|
s_ddiv(mp_int a, mp_digit b)
|
|
|
|
{
|
|
|
|
mp_word w = 0,
|
|
|
|
qdigit;
|
|
|
|
mp_size ua = MP_USED(a);
|
|
|
|
mp_digit *da = MP_DIGITS(a) + ua - 1;
|
|
|
|
|
|
|
|
for ( /* */ ; ua > 0; --ua, --da)
|
|
|
|
{
|
|
|
|
w = (w << MP_DIGIT_BIT) | *da;
|
|
|
|
|
|
|
|
if (w >= b)
|
|
|
|
{
|
|
|
|
qdigit = w / b;
|
|
|
|
w = w % b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qdigit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*da = (mp_digit) qdigit;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLAMP(a);
|
|
|
|
return (mp_digit) w;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_qdiv(z, p2) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_qdiv(mp_int z, mp_size p2)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size ndig = p2 / MP_DIGIT_BIT,
|
|
|
|
nbits = p2 % MP_DIGIT_BIT;
|
|
|
|
mp_size uz = MP_USED(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ndig)
|
|
|
|
{
|
|
|
|
mp_size mark;
|
|
|
|
mp_digit *to,
|
|
|
|
*from;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ndig >= uz)
|
|
|
|
{
|
|
|
|
mp_int_zero(z);
|
|
|
|
return;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
to = MP_DIGITS(z);
|
|
|
|
from = to + ndig;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (mark = ndig; mark < uz; ++mark)
|
|
|
|
*to++ = *from++;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = uz - ndig;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (nbits)
|
|
|
|
{
|
|
|
|
mp_digit d = 0,
|
|
|
|
*dz,
|
|
|
|
save;
|
|
|
|
mp_size up = MP_DIGIT_BIT - nbits;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
uz = MP_USED(z);
|
|
|
|
dz = MP_DIGITS(z) + uz - 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for ( /* */ ; uz > 0; --uz, --dz)
|
|
|
|
{
|
|
|
|
save = *dz;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*dz = (*dz >> nbits) | (d << up);
|
|
|
|
d = save;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLAMP(z);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_USED(z) == 1 && z->digits[0] == 0)
|
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_qmod(z, p2) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_qmod(mp_int z, mp_size p2)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size start = p2 / MP_DIGIT_BIT + 1,
|
|
|
|
rest = p2 % MP_DIGIT_BIT;
|
|
|
|
mp_size uz = MP_USED(z);
|
|
|
|
mp_digit mask = (1 << rest) - 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (start <= uz)
|
|
|
|
{
|
|
|
|
MP_USED(z) = start;
|
|
|
|
z->digits[start - 1] &= mask;
|
|
|
|
CLAMP(z);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_qmul(z, p2) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_qmul(mp_int z, mp_size p2)
|
|
|
|
{
|
|
|
|
mp_size uz,
|
|
|
|
need,
|
|
|
|
rest,
|
|
|
|
extra,
|
|
|
|
i;
|
|
|
|
mp_digit *from,
|
|
|
|
*to,
|
|
|
|
d;
|
|
|
|
|
|
|
|
if (p2 == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
uz = MP_USED(z);
|
|
|
|
need = p2 / MP_DIGIT_BIT;
|
|
|
|
rest = p2 % MP_DIGIT_BIT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out if we need an extra digit at the top end; this occurs if the
|
|
|
|
* topmost `rest' bits of the high-order digit of z are not zero, meaning
|
|
|
|
* they will be shifted off the end if not preserved
|
|
|
|
*/
|
|
|
|
extra = 0;
|
|
|
|
if (rest != 0)
|
|
|
|
{
|
|
|
|
mp_digit *dz = MP_DIGITS(z) + uz - 1;
|
|
|
|
|
|
|
|
if ((*dz >> (MP_DIGIT_BIT - rest)) != 0)
|
|
|
|
extra = 1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(z, uz + need + extra))
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If we need to shift by whole digits, do that in one pass, then to back
|
|
|
|
* and shift by partial digits.
|
|
|
|
*/
|
|
|
|
if (need > 0)
|
|
|
|
{
|
|
|
|
from = MP_DIGITS(z) + uz - 1;
|
|
|
|
to = from + need;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < uz; ++i)
|
|
|
|
*to-- = *from--;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ZERO(MP_DIGITS(z), need);
|
|
|
|
uz += need;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (rest)
|
|
|
|
{
|
|
|
|
d = 0;
|
|
|
|
for (i = need, from = MP_DIGITS(z) + need; i < uz; ++i, ++from)
|
|
|
|
{
|
|
|
|
mp_digit save = *from;
|
|
|
|
|
|
|
|
*from = (*from << rest) | (d >> (MP_DIGIT_BIT - rest));
|
|
|
|
d = save;
|
|
|
|
}
|
|
|
|
|
|
|
|
d >>= (MP_DIGIT_BIT - rest);
|
|
|
|
if (d != 0)
|
|
|
|
{
|
|
|
|
*from = d;
|
|
|
|
uz += extra;
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_USED(z) = uz;
|
|
|
|
CLAMP(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_qsub(z, p2) */
|
|
|
|
|
|
|
|
/* Subtract |z| from 2^p2, assuming 2^p2 > |z|, and set z to be positive */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_qsub(mp_int z, mp_size p2)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit hi = (1 << (p2 % MP_DIGIT_BIT)),
|
|
|
|
*zp;
|
|
|
|
mp_size tdig = (p2 / MP_DIGIT_BIT),
|
|
|
|
pos;
|
|
|
|
mp_word w = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(z, tdig + 1))
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (pos = 0, zp = MP_DIGITS(z); pos < tdig; ++pos, ++zp)
|
|
|
|
{
|
|
|
|
w = ((mp_word) MP_DIGIT_MAX + 1) - w - (mp_word) * zp;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*zp = LOWER_HALF(w);
|
|
|
|
w = UPPER_HALF(w) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = ((mp_word) MP_DIGIT_MAX + 1 + hi) - w - (mp_word) * zp;
|
|
|
|
*zp = LOWER_HALF(w);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
assert(UPPER_HALF(w) != 0); /* no borrow out should be possible */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
MP_SIGN(z) = MP_ZPOS;
|
|
|
|
CLAMP(z);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_dp2k(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_dp2k(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int k = 0;
|
|
|
|
mp_digit *dp = MP_DIGITS(z),
|
|
|
|
d;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (MP_USED(z) == 1 && *dp == 0)
|
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*dp == 0)
|
|
|
|
{
|
|
|
|
k += MP_DIGIT_BIT;
|
|
|
|
++dp;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
d = *dp;
|
|
|
|
while ((d & 1) == 0)
|
|
|
|
{
|
|
|
|
d >>= 1;
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return k;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_isp2(z) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_isp2(mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size uz = MP_USED(z),
|
|
|
|
k = 0;
|
|
|
|
mp_digit *dz = MP_DIGITS(z),
|
|
|
|
d;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (uz > 1)
|
|
|
|
{
|
|
|
|
if (*dz++ != 0)
|
|
|
|
return -1;
|
|
|
|
k += MP_DIGIT_BIT;
|
|
|
|
--uz;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
d = *dz;
|
|
|
|
while (d > 1)
|
|
|
|
{
|
|
|
|
if (d & 1)
|
|
|
|
return -1;
|
|
|
|
++k;
|
|
|
|
d >>= 1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (int) k;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_2expt(z, k) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_2expt(mp_int z, int k)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size ndig,
|
|
|
|
rest;
|
|
|
|
mp_digit *dz;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ndig = (k + MP_DIGIT_BIT) / MP_DIGIT_BIT;
|
|
|
|
rest = k % MP_DIGIT_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(z, ndig))
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
dz = MP_DIGITS(z);
|
|
|
|
ZERO(dz, ndig);
|
|
|
|
*(dz + ndig - 1) = (1 << rest);
|
|
|
|
MP_USED(z) = ndig;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_norm(a, b) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_norm(mp_int a, mp_int b)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_digit d = b->digits[MP_USED(b) - 1];
|
|
|
|
int k = 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2007-07-16 06:43:40 +08:00
|
|
|
while (d < (mp_digit) ((mp_digit) 1 << (MP_DIGIT_BIT - 1)))
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* d < (MP_RADIX / 2) */
|
|
|
|
d <<= 1;
|
|
|
|
++k;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* These multiplications can't fail */
|
|
|
|
if (k != 0)
|
|
|
|
{
|
|
|
|
(void) s_qmul(a, (mp_size) k);
|
|
|
|
(void) s_qmul(b, (mp_size) k);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return k;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_brmu(z, m) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_result
|
|
|
|
s_brmu(mp_int z, mp_int m)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size um = MP_USED(m) * 2;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!s_pad(z, um))
|
|
|
|
return MP_MEMORY;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s_2expt(z, MP_DIGIT_BIT * um);
|
|
|
|
return mp_int_div(z, m, z, NULL);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_reduce(x, m, mu, q1, q2) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_reduce(mp_int x, mp_int m, mp_int mu, mp_int q1, mp_int q2)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size um = MP_USED(m),
|
|
|
|
umb_p1,
|
|
|
|
umb_m1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
umb_p1 = (um + 1) * MP_DIGIT_BIT;
|
|
|
|
umb_m1 = (um - 1) * MP_DIGIT_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (mp_int_copy(x, q1) != MP_OK)
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Compute q2 = floor((floor(x / b^(k-1)) * mu) / b^(k+1)) */
|
|
|
|
s_qdiv(q1, umb_m1);
|
|
|
|
UMUL(q1, mu, q2);
|
|
|
|
s_qdiv(q2, umb_p1);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Set x = x mod b^(k+1) */
|
|
|
|
s_qmod(x, umb_p1);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* Now, q is a guess for the quotient a / m. Compute x - q * m mod
|
|
|
|
* b^(k+1), replacing x. This may be off by a factor of 2m, but no more
|
|
|
|
* than that.
|
|
|
|
*/
|
|
|
|
UMUL(q2, m, q1);
|
|
|
|
s_qmod(q1, umb_p1);
|
|
|
|
(void) mp_int_sub(x, q1, x); /* can't fail */
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* The result may be < 0; if it is, add b^(k+1) to pin it in the proper
|
|
|
|
* range.
|
|
|
|
*/
|
|
|
|
if ((CMPZ(x) < 0) && !s_qsub(x, umb_p1))
|
|
|
|
return 0;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* If x > m, we need to back it off until it is in range. This will be
|
|
|
|
* required at most twice.
|
|
|
|
*/
|
|
|
|
if (mp_int_compare(x, m) >= 0)
|
|
|
|
(void) mp_int_sub(x, m, x);
|
|
|
|
if (mp_int_compare(x, m) >= 0)
|
|
|
|
(void) mp_int_sub(x, m, x);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* At this point, x has been properly reduced. */
|
|
|
|
return 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_embar(a, b, m, mu, c) */
|
|
|
|
|
|
|
|
/* Perform modular exponentiation using Barrett's method, where mu is
|
|
|
|
the reduction constant for m. Assumes a < m, b > 0. */
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_result
|
|
|
|
s_embar(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c)
|
|
|
|
{
|
|
|
|
mp_digit *db,
|
|
|
|
*dbt,
|
|
|
|
umu,
|
|
|
|
d;
|
|
|
|
mpz_t temp[3];
|
|
|
|
mp_result res;
|
|
|
|
int last = 0;
|
|
|
|
|
|
|
|
umu = MP_USED(mu);
|
|
|
|
db = MP_DIGITS(b);
|
|
|
|
dbt = db + MP_USED(b) - 1;
|
|
|
|
|
|
|
|
while (last < 3)
|
|
|
|
SETUP(mp_int_init_size(TEMP(last), 2 * umu), last);
|
|
|
|
|
|
|
|
(void) mp_int_set_value(c, 1);
|
|
|
|
|
|
|
|
/* Take care of low-order digits */
|
|
|
|
while (db < dbt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (d = *db, i = MP_DIGIT_BIT; i > 0; --i, d >>= 1)
|
|
|
|
{
|
|
|
|
if (d & 1)
|
|
|
|
{
|
|
|
|
/* The use of a second temporary avoids allocation */
|
|
|
|
UMUL(c, a, TEMP(0));
|
|
|
|
if (!s_reduce(TEMP(0), m, mu, TEMP(1), TEMP(2)))
|
|
|
|
{
|
|
|
|
res = MP_MEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
mp_int_copy(TEMP(0), c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
USQR(a, TEMP(0));
|
|
|
|
assert(MP_SIGN(TEMP(0)) == MP_ZPOS);
|
|
|
|
if (!s_reduce(TEMP(0), m, mu, TEMP(1), TEMP(2)))
|
|
|
|
{
|
|
|
|
res = MP_MEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
assert(MP_SIGN(TEMP(0)) == MP_ZPOS);
|
|
|
|
mp_int_copy(TEMP(0), a);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
++db;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Take care of highest-order digit */
|
|
|
|
d = *dbt;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (d & 1)
|
|
|
|
{
|
|
|
|
UMUL(c, a, TEMP(0));
|
|
|
|
if (!s_reduce(TEMP(0), m, mu, TEMP(1), TEMP(2)))
|
|
|
|
{
|
|
|
|
res = MP_MEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
mp_int_copy(TEMP(0), c);
|
|
|
|
}
|
|
|
|
|
|
|
|
d >>= 1;
|
|
|
|
if (!d)
|
|
|
|
break;
|
|
|
|
|
|
|
|
USQR(a, TEMP(0));
|
|
|
|
if (!s_reduce(TEMP(0), m, mu, TEMP(1), TEMP(2)))
|
|
|
|
{
|
|
|
|
res = MP_MEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
(void) mp_int_copy(TEMP(0), a);
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
CLEANUP:
|
|
|
|
while (--last >= 0)
|
|
|
|
mp_int_clear(TEMP(last));
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_udiv(a, b) */
|
|
|
|
|
|
|
|
/* Precondition: a >= b and b > 0
|
|
|
|
Postcondition: a' = a / b, b' = a % b
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_result
|
|
|
|
s_udiv(mp_int a, mp_int b)
|
|
|
|
{
|
|
|
|
mpz_t q,
|
|
|
|
r,
|
|
|
|
t;
|
|
|
|
mp_size ua,
|
|
|
|
ub,
|
|
|
|
qpos = 0;
|
|
|
|
mp_digit *da,
|
|
|
|
btop;
|
|
|
|
mp_result res = MP_OK;
|
|
|
|
int k,
|
|
|
|
skip = 0;
|
|
|
|
|
|
|
|
/* Force signs to positive */
|
|
|
|
MP_SIGN(a) = MP_ZPOS;
|
|
|
|
MP_SIGN(b) = MP_ZPOS;
|
|
|
|
|
|
|
|
/* Normalize, per Knuth */
|
|
|
|
k = s_norm(a, b);
|
|
|
|
|
|
|
|
ua = MP_USED(a);
|
|
|
|
ub = MP_USED(b);
|
|
|
|
btop = b->digits[ub - 1];
|
|
|
|
if ((res = mp_int_init_size(&q, ua)) != MP_OK)
|
|
|
|
return res;
|
|
|
|
if ((res = mp_int_init_size(&t, ua + 1)) != MP_OK)
|
|
|
|
goto CLEANUP;
|
|
|
|
|
|
|
|
da = MP_DIGITS(a);
|
|
|
|
r.digits = da + ua - 1; /* The contents of r are shared with a */
|
|
|
|
r.used = 1;
|
|
|
|
r.sign = MP_ZPOS;
|
|
|
|
r.alloc = MP_ALLOC(a);
|
|
|
|
ZERO(t.digits, t.alloc);
|
|
|
|
|
|
|
|
/* Solve for quotient digits, store in q.digits in reverse order */
|
|
|
|
while (r.digits >= da)
|
|
|
|
{
|
|
|
|
assert(qpos <= q.alloc);
|
|
|
|
|
|
|
|
if (s_ucmp(b, &r) > 0)
|
|
|
|
{
|
|
|
|
r.digits -= 1;
|
|
|
|
r.used += 1;
|
|
|
|
|
|
|
|
if (++skip > 1)
|
|
|
|
q.digits[qpos++] = 0;
|
|
|
|
|
|
|
|
CLAMP(&r);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mp_word pfx = r.digits[r.used - 1];
|
|
|
|
mp_word qdigit;
|
|
|
|
|
|
|
|
if (r.used > 1 && (pfx < btop || r.digits[r.used - 2] == 0))
|
|
|
|
{
|
|
|
|
pfx <<= MP_DIGIT_BIT / 2;
|
|
|
|
pfx <<= MP_DIGIT_BIT / 2;
|
|
|
|
pfx |= r.digits[r.used - 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
qdigit = pfx / btop;
|
|
|
|
if (qdigit > MP_DIGIT_MAX)
|
|
|
|
qdigit = 1;
|
|
|
|
|
|
|
|
s_dbmul(MP_DIGITS(b), (mp_digit) qdigit, t.digits, ub);
|
|
|
|
t.used = ub + 1;
|
|
|
|
CLAMP(&t);
|
|
|
|
while (s_ucmp(&t, &r) > 0)
|
|
|
|
{
|
|
|
|
--qdigit;
|
|
|
|
(void) mp_int_sub(&t, b, &t); /* cannot fail */
|
|
|
|
}
|
|
|
|
|
|
|
|
s_usub(r.digits, t.digits, r.digits, r.used, t.used);
|
|
|
|
CLAMP(&r);
|
|
|
|
|
|
|
|
q.digits[qpos++] = (mp_digit) qdigit;
|
|
|
|
ZERO(t.digits, t.used);
|
|
|
|
skip = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put quotient digits in the correct order, and discard extra zeroes */
|
|
|
|
q.used = qpos;
|
|
|
|
REV(mp_digit, q.digits, qpos);
|
|
|
|
CLAMP(&q);
|
|
|
|
|
|
|
|
/* Denormalize the remainder */
|
|
|
|
CLAMP(a);
|
|
|
|
if (k != 0)
|
|
|
|
s_qdiv(a, k);
|
|
|
|
|
|
|
|
mp_int_copy(a, b); /* ok: 0 <= r < b */
|
|
|
|
mp_int_copy(&q, a); /* ok: q <= a */
|
|
|
|
|
|
|
|
mp_int_clear(&t);
|
|
|
|
CLEANUP:
|
|
|
|
mp_int_clear(&q);
|
|
|
|
return res;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_outlen(z, r) */
|
|
|
|
|
|
|
|
/* Precondition: 2 <= r < 64 */
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_outlen(mp_int z, mp_size r)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_result bits;
|
|
|
|
double raw;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
bits = mp_int_count_bits(z);
|
|
|
|
raw = (double) bits *s_log2[r];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (int) (raw + 0.999999);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_inlen(len, r) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_size
|
|
|
|
s_inlen(int len, mp_size r)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
double raw = (double) len / s_log2[r];
|
|
|
|
mp_size bits = (mp_size) (raw + 0.5);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (mp_size) ((bits + (MP_DIGIT_BIT - 1)) / MP_DIGIT_BIT);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_ch2val(c, r) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
s_ch2val(char c, int r)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int out;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (isdigit((unsigned char) c))
|
|
|
|
out = c - '0';
|
|
|
|
else if (r > 10 && isalpha((unsigned char) c))
|
|
|
|
out = toupper((unsigned char) c) - 'A' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (out >= r) ? -1 : out;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_val2ch(v, caps) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static char
|
|
|
|
s_val2ch(int v, int caps)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
assert(v >= 0);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (v < 10)
|
|
|
|
return v + '0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char out = (v - 10) + 'a';
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (caps)
|
|
|
|
return toupper((unsigned char) out);
|
|
|
|
else
|
|
|
|
return out;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_2comp(buf, len) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static void
|
|
|
|
s_2comp(unsigned char *buf, int len)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int i;
|
|
|
|
unsigned short s = 1;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = len - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
unsigned char c = ~buf[i];
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
s = c + s;
|
|
|
|
c = s & UCHAR_MAX;
|
|
|
|
s >>= CHAR_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
buf[i] = c;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* last carry out is ignored */
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_tobin(z, buf, *limpos) */
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static mp_result
|
|
|
|
s_tobin(mp_int z, unsigned char *buf, int *limpos, int pad)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
mp_size uz;
|
|
|
|
mp_digit *dz;
|
|
|
|
int pos = 0,
|
|
|
|
limit = *limpos;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
uz = MP_USED(z);
|
|
|
|
dz = MP_DIGITS(z);
|
|
|
|
while (uz > 0 && pos < limit)
|
|
|
|
{
|
|
|
|
mp_digit d = *dz++;
|
|
|
|
int i;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = sizeof(mp_digit); i > 0 && pos < limit; --i)
|
|
|
|
{
|
|
|
|
buf[pos++] = (unsigned char) d;
|
|
|
|
d >>= CHAR_BIT;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Don't write leading zeroes */
|
|
|
|
if (d == 0 && uz == 1)
|
|
|
|
i = 0; /* exit loop without signaling truncation */
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Detect truncation (loop exited with pos >= limit) */
|
|
|
|
if (i > 0)
|
|
|
|
break;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
--uz;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (pad != 0 && (buf[pos - 1] >> (CHAR_BIT - 1)))
|
|
|
|
{
|
|
|
|
if (pos < limit)
|
|
|
|
buf[pos++] = 0;
|
|
|
|
else
|
|
|
|
uz = 1;
|
|
|
|
}
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Digits are in reverse order, fix that */
|
|
|
|
REV(unsigned char, buf, pos);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* Return the number of bytes actually written */
|
|
|
|
*limpos = pos;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (uz == 0) ? MP_OK : MP_TRUNC;
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ s_print(tag, z) */
|
|
|
|
|
|
|
|
#if 0
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
s_print(char *tag, mp_int z)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int i;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
fprintf(stderr, "%s: %c ", tag,
|
|
|
|
(MP_SIGN(z) == MP_NEG) ? '-' : '+');
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = MP_USED(z) - 1; i >= 0; --i)
|
|
|
|
fprintf(stderr, "%0*X", (int) (MP_DIGIT_BIT / 4), z->digits[i]);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
fputc('\n', stderr);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
s_print_buf(char *tag, mp_digit * buf, mp_size num)
|
2006-07-13 12:15:25 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int i;
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
fprintf(stderr, "%s: ", tag);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = num - 1; i >= 0; --i)
|
|
|
|
fprintf(stderr, "%0*X", (int) (MP_DIGIT_BIT / 4), buf[i]);
|
2006-07-13 12:15:25 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
fputc('\n', stderr);
|
2006-07-13 12:15:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* HERE THERE BE DRAGONS */
|