Lines Matching +full:- +full:x
4 * http://libtom.org/files/ltm-0.41.tar.bz2
64 #define MIN(x,y) ((x)<(y)?(x):(y)) argument
68 #define MAX(x,y) ((x)>(y)?(x):(y)) argument
71 #define OPT_CAST(x) (x *) argument
85 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
87 #define MP_LT -1 /* less than */
95 #define MP_MEM -2 /* out of mem */
96 #define MP_VAL -3 /* invalid input */
115 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
116 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
125 /* ---> Basic Manipulations <--- */
126 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
127 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
128 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
132 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
133 static int s_mp_exptmod(mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
175 static int mp_reduce(mp_int * x, mp_int * m, mp_int * mu);
178 static int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
200 iy = len - 1; in bn_reverse()
206 --iy; in bn_reverse()
215 mp_int *x; in s_mp_add() local
219 * them. "x" will point to the input with the most digits in s_mp_add()
221 if (a->used > b->used) { in s_mp_add()
222 min = b->used; in s_mp_add()
223 max = a->used; in s_mp_add()
224 x = a; in s_mp_add()
226 min = a->used; in s_mp_add()
227 max = b->used; in s_mp_add()
228 x = b; in s_mp_add()
232 if (c->alloc < max + 1) { in s_mp_add()
239 olduse = c->used; in s_mp_add()
240 c->used = max + 1; in s_mp_add()
249 tmpa = a->dp; in s_mp_add()
252 tmpb = b->dp; in s_mp_add()
255 tmpc = c->dp; in s_mp_add()
275 /* T[i] = X[i] + U */ in s_mp_add()
276 *tmpc = x->dp[i] + u; in s_mp_add()
290 for (i = c->used; i < olduse; i++) { in s_mp_add()
307 min = b->used; in s_mp_sub()
308 max = a->used; in s_mp_sub()
311 if (c->alloc < max) { in s_mp_sub()
316 olduse = c->used; in s_mp_sub()
317 c->used = max; in s_mp_sub()
324 tmpa = a->dp; in s_mp_sub()
325 tmpb = b->dp; in s_mp_sub()
326 tmpc = c->dp; in s_mp_sub()
331 /* T[i] = A[i] - B[i] - U */ in s_mp_sub()
332 *tmpc = *tmpa++ - *tmpb++ - u; in s_mp_sub()
339 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); in s_mp_sub()
347 /* T[i] = A[i] - U */ in s_mp_sub()
348 *tmpc = *tmpa++ - u; in s_mp_sub()
351 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); in s_mp_sub()
358 for (i = c->used; i < olduse; i++) { in s_mp_sub()
375 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC); in mp_init()
376 if (a->dp == NULL) { in mp_init()
382 a->dp[i] = 0; in mp_init()
387 a->used = 0; in mp_init()
388 a->alloc = MP_PREC; in mp_init()
389 a->sign = MP_ZPOS; in mp_init()
402 if (a->dp != NULL) { in mp_clear()
404 for (i = 0; i < a->used; i++) { in mp_clear()
405 a->dp[i] = 0; in mp_clear()
409 XFREE(a->dp); in mp_clear()
412 a->dp = NULL; in mp_clear()
413 a->alloc = a->used = 0; in mp_clear()
414 a->sign = MP_ZPOS; in mp_clear()
426 sa = a->sign; in mp_add()
427 sb = b->sign; in mp_add()
433 c->sign = sa; in mp_add()
441 c->sign = sb; in mp_add()
444 c->sign = sa; in mp_add()
458 sa = a->sign; in mp_sub()
459 sb = b->sign; in mp_sub()
466 c->sign = sa; in mp_sub()
475 c->sign = sa; in mp_sub()
481 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; in mp_sub()
495 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; in mp_mul()
497 /* use Toom-Cook? */ in mp_mul()
499 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { in mp_mul()
505 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { in mp_mul()
517 int digs = a->used + b->used + 1; in mp_mul()
520 MIN(a->used, b->used) <= in mp_mul()
521 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in mp_mul()
533 c->sign = (c->used > 0) ? neg : MP_ZPOS; in mp_mul()
575 if (t.sign != b->sign) { in mp_mod()
593 mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) in mp_exptmod() argument
598 if (P->sign == MP_NEG) { in mp_exptmod()
602 /* if exponent X is negative we have to recurse */ in mp_exptmod()
603 if (X->sign == MP_NEG) { in mp_exptmod()
620 /* now get |X| */ in mp_exptmod()
625 if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { in mp_exptmod()
630 /* and now compute (1/G)**|X| instead of G**X [X < 0] */ in mp_exptmod()
645 return s_mp_exptmod(G, X, P, Y, 1); in mp_exptmod()
667 return mp_exptmod_fast (G, X, P, Y, dr); in mp_exptmod()
673 return s_mp_exptmod (G, X, P, Y, 0); in mp_exptmod()
690 if (a->sign != b->sign) { in mp_cmp()
691 if (a->sign == MP_NEG) { in mp_cmp()
699 if (a->sign == MP_NEG) { in mp_cmp()
713 if (a->sign == MP_NEG) { in mp_cmp_d()
718 if (a->used > 1) { in mp_cmp_d()
723 if (a->dp[0] > b) { in mp_cmp_d()
725 } else if (a->dp[0] < b) { in mp_cmp_d()
739 if (b->sign == MP_NEG || mp_iszero(b) == 1) { in mp_invmod()
778 mp_int x, y, u, v, A, B, C, D; in mp_invmod_slow() local
782 if (b->sign == MP_NEG || mp_iszero(b) == 1) { in mp_invmod_slow()
787 if ((res = mp_init_multi(&x, &y, &u, &v, in mp_invmod_slow()
792 /* x = a, y = b */ in mp_invmod_slow()
793 if ((res = mp_mod(a, b, &x)) != MP_OKAY) { in mp_invmod_slow()
800 /* 2. [modified] if x,y are both even then return an error! */ in mp_invmod_slow()
801 if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { in mp_invmod_slow()
806 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ in mp_invmod_slow()
807 if ((res = mp_copy (&x, &u)) != MP_OKAY) { in mp_invmod_slow()
825 /* A = (A+y)/2, B = (B-x)/2 */ in mp_invmod_slow()
829 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { in mp_invmod_slow()
850 /* C = (C+y)/2, D = (D-x)/2 */ in mp_invmod_slow()
854 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { in mp_invmod_slow()
869 /* u = u - v, A = A - C, B = B - D */ in mp_invmod_slow()
882 /* v - v - u, C = C - A, D = D - B */ in mp_invmod_slow()
925 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); in mp_invmod_slow()
938 /* compare based on # of non-zero digits */ in mp_cmp_mag()
939 if (a->used > b->used) { in mp_cmp_mag()
943 if (a->used < b->used) { in mp_cmp_mag()
948 tmpa = a->dp + (a->used - 1); in mp_cmp_mag()
951 tmpb = b->dp + (a->used - 1); in mp_cmp_mag()
954 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { in mp_cmp_mag()
974 if (a->alloc < 2) { in mp_read_unsigned_bin()
984 while (c-- > 0) { in mp_read_unsigned_bin()
990 a->dp[0] |= *b++; in mp_read_unsigned_bin()
991 a->used += 1; in mp_read_unsigned_bin()
993 a->dp[0] = (*b & MP_MASK); in mp_read_unsigned_bin()
994 a->dp[1] |= ((*b++ >> 7U) & 1); in mp_read_unsigned_bin()
995 a->used += 2; in mp_read_unsigned_bin()
1007 int x, res; in mp_to_unsigned_bin() local
1014 x = 0; in mp_to_unsigned_bin()
1017 b[x++] = (unsigned char) (t.dp[0] & 255); in mp_to_unsigned_bin()
1019 b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7)); in mp_to_unsigned_bin()
1026 bn_reverse (b, x); in mp_to_unsigned_bin()
1037 int x, res; in mp_div_2d() local
1079 mask = (((mp_digit)1) << D) - 1; in mp_div_2d()
1082 shift = DIGIT_BIT - D; in mp_div_2d()
1085 tmpc = c->dp + (c->used - 1); in mp_div_2d()
1089 for (x = c->used - 1; x >= 0; x--) { in mp_div_2d()
1095 --tmpc; in mp_div_2d()
1129 a->sign = MP_ZPOS; in mp_zero()
1130 a->used = 0; in mp_zero()
1132 tmp = a->dp; in mp_zero()
1133 for (n = 0; n < a->alloc; n++) { in mp_zero()
1151 if (b->alloc < a->used) { in mp_copy()
1152 if ((res = mp_grow (b, a->used)) != MP_OKAY) { in mp_copy()
1164 tmpa = a->dp; in mp_copy()
1167 tmpb = b->dp; in mp_copy()
1170 for (n = 0; n < a->used; n++) { in mp_copy()
1175 for (; n < b->used; n++) { in mp_copy()
1181 b->used = a->used; in mp_copy()
1182 b->sign = a->sign; in mp_copy()
1191 int x; in mp_rshd() local
1199 if (a->used <= b) { in mp_rshd()
1210 bottom = a->dp; in mp_rshd()
1213 top = a->dp + b; in mp_rshd()
1216 * the window is b-digits long and digits from in mp_rshd()
1221 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> in mp_rshd()
1222 /\ | ----> in mp_rshd()
1223 \-------------------/ ----> in mp_rshd()
1225 for (x = 0; x < (a->used - b); x++) { in mp_rshd()
1230 for (; x < a->used; x++) { in mp_rshd()
1236 a->used -= b; in mp_rshd()
1257 * trimed and the leading "used" digit will be non-zero
1267 while (a->used > 0 && a->dp[a->used - 1] == 0) { in mp_clamp()
1268 --(a->used); in mp_clamp()
1272 if (a->used == 0) { in mp_clamp()
1273 a->sign = MP_ZPOS; in mp_clamp()
1286 if (a->alloc < size) { in mp_grow()
1288 size += (MP_PREC * 2) - (size % MP_PREC); in mp_grow()
1290 /* reallocate the array a->dp in mp_grow()
1296 tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size); in mp_grow()
1302 /* reallocation succeeded so set a->dp */ in mp_grow()
1303 a->dp = tmp; in mp_grow()
1306 i = a->alloc; in mp_grow()
1307 a->alloc = size; in mp_grow()
1308 for (; i < a->alloc; i++) { in mp_grow()
1309 a->dp[i] = 0; in mp_grow()
1334 b->sign = MP_ZPOS; in mp_abs()
1346 a->dp[0] = b & MP_MASK; in mp_set()
1347 a->used = (a->dp[0] != 0) ? 1 : 0; in mp_set()
1356 int x, res, oldused; in mp_div_2() local
1359 if (b->alloc < a->used) { in mp_div_2()
1360 if ((res = mp_grow (b, a->used)) != MP_OKAY) { in mp_div_2()
1365 oldused = b->used; in mp_div_2()
1366 b->used = a->used; in mp_div_2()
1371 tmpa = a->dp + b->used - 1; in mp_div_2()
1374 tmpb = b->dp + b->used - 1; in mp_div_2()
1378 for (x = b->used - 1; x >= 0; x--) { in mp_div_2()
1383 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); in mp_div_2()
1390 tmpb = b->dp + b->used; in mp_div_2()
1391 for (x = b->used; x < oldused; x++) { in mp_div_2()
1395 b->sign = a->sign; in mp_div_2()
1416 if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) { in mp_mul_2d()
1417 if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) { in mp_mul_2d()
1433 register int x; in mp_mul_2d() local
1436 mask = (((mp_digit)1) << d) - 1; in mp_mul_2d()
1439 shift = DIGIT_BIT - d; in mp_mul_2d()
1442 tmpc = c->dp; in mp_mul_2d()
1446 for (x = 0; x < c->used; x++) { in mp_mul_2d()
1460 c->dp[(c->used)++] = r; in mp_mul_2d()
1480 /* Oops - error! Back-track and mp_clear what we already in mp_init_multi()
1481 succeeded in init-ing, then return error. in mp_init_multi()
1491 while (n--) { in mp_init_multi()
1528 int x, res; in mp_lshd() local
1536 if (a->alloc < a->used + b) { in mp_lshd()
1537 if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { in mp_lshd()
1546 a->used += b; in mp_lshd()
1549 top = a->dp + a->used - 1; in mp_lshd()
1552 bottom = a->dp + a->used - 1 - b; in mp_lshd()
1558 for (x = a->used - 1; x >= b; x--) { in mp_lshd()
1559 *top-- = *bottom--; in mp_lshd()
1563 top = a->dp; in mp_lshd()
1564 for (x = 0; x < b; x++) { in mp_lshd()
1580 if (a->used == 0) { in mp_count_bits()
1585 r = (a->used - 1) * DIGIT_BIT; in mp_count_bits()
1588 q = a->dp[a->used - 1]; in mp_count_bits()
1601 int x, res; in mp_mod_2d() local
1610 if (b >= (int) (a->used * DIGIT_BIT)) { in mp_mod_2d()
1621 for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) { in mp_mod_2d()
1622 c->dp[x] = 0; in mp_mod_2d()
1625 c->dp[b / DIGIT_BIT] &= in mp_mod_2d()
1626 (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); in mp_mod_2d()
1634 /* slower bit-bang division... also smaller */
1666 n = mp_count_bits(a) - mp_count_bits(b); in mp_div()
1674 while (n-- >= 0) { in mp_div()
1688 n = a->sign; in mp_div()
1689 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); in mp_div()
1692 c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; in mp_div()
1696 d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; in mp_div()
1711 * the case where digits are removed from 'x' in
1721 mp_int q, x, y, t1, t2; in mp_div() local
1742 if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) { in mp_div()
1745 q.used = a->used + 2; in mp_div()
1755 if ((res = mp_init_copy (&x, a)) != MP_OKAY) { in mp_div()
1764 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; in mp_div()
1765 x.sign = y.sign = MP_ZPOS; in mp_div()
1767 /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ in mp_div()
1769 if (norm < (int)(DIGIT_BIT-1)) { in mp_div()
1770 norm = (DIGIT_BIT-1) - norm; in mp_div()
1771 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) { in mp_div()
1782 n = x.used - 1; in mp_div()
1783 t = y.used - 1; in mp_div()
1785 /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ in mp_div()
1786 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ in mp_div()
1790 while (mp_cmp (&x, &y) != MP_LT) { in mp_div()
1791 ++(q.dp[n - t]); in mp_div()
1792 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) { in mp_div()
1798 mp_rshd (&y, n - t); in mp_div()
1801 for (i = n; i >= (t + 1); i--) { in mp_div()
1802 if (i > x.used) { in mp_div()
1806 /* step 3.1 if xi == yt then set q{i-t-1} to b-1, in mp_div()
1807 * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ in mp_div()
1808 if (x.dp[i] == y.dp[t]) { in mp_div()
1809 q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); in mp_div()
1812 tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); in mp_div()
1813 tmp |= ((mp_word) x.dp[i - 1]); in mp_div()
1817 q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); in mp_div()
1820 /* while (q{i-t-1} * (yt * b + y{t-1})) > in mp_div()
1821 xi * b**2 + xi-1 * b + xi-2 in mp_div()
1823 do q{i-t-1} -= 1; in mp_div()
1825 q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; in mp_div()
1827 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK; in mp_div()
1831 t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1]; in mp_div()
1834 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { in mp_div()
1839 t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2]; in mp_div()
1840 t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1]; in mp_div()
1841 t2.dp[2] = x.dp[i]; in mp_div()
1845 /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ in mp_div()
1846 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { in mp_div()
1850 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { in mp_div()
1854 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) { in mp_div()
1858 /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ in mp_div()
1859 if (x.sign == MP_NEG) { in mp_div()
1863 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { in mp_div()
1866 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { in mp_div()
1870 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; in mp_div()
1874 /* now q is the quotient and x is the remainder in mp_div()
1879 x.sign = x.used == 0 ? MP_ZPOS : a->sign; in mp_div()
1884 c->sign = neg; in mp_div()
1888 mp_div_2d (&x, norm, &x, NULL); in mp_div()
1889 mp_exch (&x, d); in mp_div()
1895 LBL_X:mp_clear (&x); in mp_div()
1912 s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) in s_mp_exptmod() argument
1916 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; in s_mp_exptmod() local
1920 x = mp_count_bits (X); in s_mp_exptmod()
1921 if (x <= 7) { in s_mp_exptmod()
1923 } else if (x <= 36) { in s_mp_exptmod()
1925 } else if (x <= 140) { in s_mp_exptmod()
1927 } else if (x <= 450) { in s_mp_exptmod()
1929 } else if (x <= 1303) { in s_mp_exptmod()
1931 } else if (x <= 3529) { in s_mp_exptmod()
1950 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in s_mp_exptmod()
1951 if ((err = mp_init(&M[x])) != MP_OKAY) { in s_mp_exptmod()
1952 for (y = 1<<(winsize-1); y < x; y++) { in s_mp_exptmod()
1980 * e.g. M[x] = G**x mod P in s_mp_exptmod()
1989 /* compute the value at M[1<<(winsize-1)] by squaring in s_mp_exptmod()
1990 * M[1] (winsize-1) times in s_mp_exptmod()
1992 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { in s_mp_exptmod()
1996 for (x = 0; x < (winsize - 1); x++) { in s_mp_exptmod()
1998 if ((err = mp_sqr (&M[1 << (winsize - 1)], in s_mp_exptmod()
1999 &M[1 << (winsize - 1)])) != MP_OKAY) { in s_mp_exptmod()
2004 if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { in s_mp_exptmod()
2009 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) in s_mp_exptmod()
2010 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) in s_mp_exptmod()
2012 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { in s_mp_exptmod()
2013 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { in s_mp_exptmod()
2016 if ((err = redux (&M[x], P, &mu)) != MP_OKAY) { in s_mp_exptmod()
2031 digidx = X->used - 1; in s_mp_exptmod()
2037 if (--bitcnt == 0) { in s_mp_exptmod()
2038 /* if digidx == -1 we are out of digits */ in s_mp_exptmod()
2039 if (digidx == -1) { in s_mp_exptmod()
2043 buf = X->dp[digidx--]; in s_mp_exptmod()
2048 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1; in s_mp_exptmod()
2072 bitbuf |= (y << (winsize - ++bitcpy)); in s_mp_exptmod()
2078 for (x = 0; x < winsize; x++) { in s_mp_exptmod()
2105 for (x = 0; x < bitcpy; x++) { in s_mp_exptmod()
2132 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in s_mp_exptmod()
2133 mp_clear (&M[x]); in s_mp_exptmod()
2146 /* use Toom-Cook? */ in mp_sqr()
2147 if (a->used >= TOOM_SQR_CUTOFF) { in mp_sqr()
2153 if (a->used >= KARATSUBA_SQR_CUTOFF) { in mp_sqr()
2160 if ((a->used * 2 + 1) < MP_WARRAY && in mp_sqr()
2161 a->used < in mp_sqr()
2162 (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { in mp_sqr()
2173 b->sign = MP_ZPOS; in mp_sqr()
2178 /* reduces a modulo n where n is of the form 2**p - d
2264 a->used = b / DIGIT_BIT + 1; in mp_2expt()
2267 a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); in mp_2expt()
2273 /* pre-calculate the value required for Barrett reduction
2281 if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { in mp_reduce_setup()
2288 /* reduces x mod m, assumes 0 < x < m**2, mu is
2293 mp_reduce (mp_int * x, mp_int * m, mp_int * mu) in mp_reduce() argument
2296 int res, um = m->used; in mp_reduce()
2298 /* q = x */ in mp_reduce()
2299 if ((res = mp_init_copy (&q, x)) != MP_OKAY) { in mp_reduce()
2303 /* q1 = x / b**(k-1) */ in mp_reduce()
2304 mp_rshd (&q, um - 1); in mp_reduce()
2307 if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { in mp_reduce()
2332 /* x = x mod b**(k+1), quick (no division) */ in mp_reduce()
2333 if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) { in mp_reduce()
2342 /* x = x - q */ in mp_reduce()
2343 if ((res = mp_sub (x, &q, x)) != MP_OKAY) { in mp_reduce()
2347 /* If x < 0, add b**(k+1) to it */ in mp_reduce()
2348 if (mp_cmp_d (x, 0) == MP_LT) { in mp_reduce()
2353 if ((res = mp_add (x, &q, x)) != MP_OKAY) { in mp_reduce()
2359 while (mp_cmp (x, m) != MP_LT) { in mp_reduce()
2360 if ((res = s_mp_sub (x, m, x)) != MP_OKAY) { in mp_reduce()
2387 MIN (a->used, b->used) < in s_mp_mul_digs()
2388 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in s_mp_mul_digs()
2398 pa = a->used; in s_mp_mul_digs()
2404 pb = MIN (b->used, digs - ix); in s_mp_mul_digs()
2408 tmpx = a->dp[ix]; in s_mp_mul_digs()
2414 tmpy = b->dp; in s_mp_mul_digs()
2445 * This is the fast column-array [comba] multiplier. It is
2449 * simple and schedulable on super-scalar processors.
2452 * digits of output so if say only a half-product is required
2467 if (c->alloc < digs) { in fast_s_mp_mul_digs()
2474 pa = MIN(digs, a->used + b->used); in fast_s_mp_mul_digs()
2484 ty = MIN(b->used-1, ix); in fast_s_mp_mul_digs()
2485 tx = ix - ty; in fast_s_mp_mul_digs()
2488 tmpx = a->dp + tx; in fast_s_mp_mul_digs()
2489 tmpy = b->dp + ty; in fast_s_mp_mul_digs()
2492 while (tx++ < a->used && ty-- >= 0) { ... } in fast_s_mp_mul_digs()
2494 iy = MIN(a->used-tx, ty+1); in fast_s_mp_mul_digs()
2498 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); in fast_s_mp_mul_digs()
2510 olduse = c->used; in fast_s_mp_mul_digs()
2511 c->used = pa; in fast_s_mp_mul_digs()
2515 tmpc = c->dp; in fast_s_mp_mul_digs()
2535 int x; in mp_init_size() local
2538 size += (MP_PREC * 2) - (size % MP_PREC); in mp_init_size()
2541 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); in mp_init_size()
2542 if (a->dp == NULL) { in mp_init_size()
2547 a->used = 0; in mp_init_size()
2548 a->alloc = size; in mp_init_size()
2549 a->sign = MP_ZPOS; in mp_init_size()
2552 for (x = 0; x < size; x++) { in mp_init_size()
2553 a->dp[x] = 0; in mp_init_size()
2560 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
2569 pa = a->used; in s_mp_sqr()
2581 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); in s_mp_sqr()
2590 tmpx = a->dp[ix]; in s_mp_sqr()
2597 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); in s_mp_sqr()
2639 if (((a->used + b->used + 1) < MP_WARRAY) in s_mp_mul_high_digs()
2640 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in s_mp_mul_high_digs()
2645 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { in s_mp_mul_high_digs()
2648 t.used = a->used + b->used + 1; in s_mp_mul_high_digs()
2650 pa = a->used; in s_mp_mul_high_digs()
2651 pb = b->used; in s_mp_mul_high_digs()
2657 tmpx = a->dp[ix]; in s_mp_mul_high_digs()
2663 tmpy = b->dp + (digs - ix); in s_mp_mul_high_digs()
2665 for (iy = digs - ix; iy < pb; iy++) { in s_mp_mul_high_digs()
2691 mp_digit x, b; in mp_montgomery_setup() local
2697 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) in mp_montgomery_setup()
2698 * => 2*X*A - X*X*A*A = 1 in mp_montgomery_setup()
2699 * => 2*(1) - (1) = 1 in mp_montgomery_setup()
2701 b = n->dp[0]; in mp_montgomery_setup()
2707 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ in mp_montgomery_setup()
2708 x *= 2 - b * x; /* here x*a==1 mod 2**8 */ in mp_montgomery_setup()
2710 x *= 2 - b * x; /* here x*a==1 mod 2**16 */ in mp_montgomery_setup()
2713 x *= 2 - b * x; /* here x*a==1 mod 2**32 */ in mp_montgomery_setup()
2716 x *= 2 - b * x; /* here x*a==1 mod 2**64 */ in mp_montgomery_setup()
2719 /* rho = -1/m mod b */ in mp_montgomery_setup()
2720 *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; in mp_montgomery_setup()
2728 /* computes xR**-1 == x (mod N) via Montgomery Reduction
2737 fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) in fast_mp_montgomery_reduce() argument
2743 olduse = x->used; in fast_mp_montgomery_reduce()
2746 if (x->alloc < n->used + 1) { in fast_mp_montgomery_reduce()
2747 if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) { in fast_mp_montgomery_reduce()
2762 /* alias for the digits of x*/ in fast_mp_montgomery_reduce()
2763 tmpx = x->dp; in fast_mp_montgomery_reduce()
2765 /* copy the digits of a into W[0..a->used-1] */ in fast_mp_montgomery_reduce()
2766 for (ix = 0; ix < x->used; ix++) { in fast_mp_montgomery_reduce()
2770 /* zero the high words of W[a->used..m->used*2] */ in fast_mp_montgomery_reduce()
2771 for (; ix < n->used * 2 + 1; ix++) { in fast_mp_montgomery_reduce()
2779 for (ix = 0; ix < n->used; ix++) { in fast_mp_montgomery_reduce()
2784 * that W[ix-1] have the carry cleared (see after the inner loop) in fast_mp_montgomery_reduce()
2801 * first m->used words of W[] have the carries fixed in fast_mp_montgomery_reduce()
2809 tmpn = n->dp; in fast_mp_montgomery_reduce()
2815 for (iy = 0; iy < n->used; iy++) { in fast_mp_montgomery_reduce()
2840 for (; ix <= n->used * 2 + 1; ix++) { in fast_mp_montgomery_reduce()
2852 tmpx = x->dp; in fast_mp_montgomery_reduce()
2855 _W = W + n->used; in fast_mp_montgomery_reduce()
2857 for (ix = 0; ix < n->used + 1; ix++) { in fast_mp_montgomery_reduce()
2862 * m->used+1 we'll have to clear the digits in fast_mp_montgomery_reduce()
2870 x->used = n->used + 1; in fast_mp_montgomery_reduce()
2871 mp_clamp (x); in fast_mp_montgomery_reduce()
2873 /* if A >= m then A = A - m */ in fast_mp_montgomery_reduce()
2874 if (mp_cmp_mag (x, n) != MP_LT) { in fast_mp_montgomery_reduce()
2875 return s_mp_sub (x, n, x); in fast_mp_montgomery_reduce()
2887 int x, res, oldused; in mp_mul_2() local
2890 if (b->alloc < a->used + 1) { in mp_mul_2()
2891 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { in mp_mul_2()
2896 oldused = b->used; in mp_mul_2()
2897 b->used = a->used; in mp_mul_2()
2903 tmpa = a->dp; in mp_mul_2()
2906 tmpb = b->dp; in mp_mul_2()
2910 for (x = 0; x < a->used; x++) { in mp_mul_2()
2915 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); in mp_mul_2()
2930 ++(b->used); in mp_mul_2()
2936 tmpb = b->dp + b->used; in mp_mul_2()
2937 for (x = b->used; x < oldused; x++) { in mp_mul_2()
2941 b->sign = a->sign; in mp_mul_2()
2957 int x, bits, res; in mp_montgomery_calc_normalization() local
2962 if (b->used > 1) { in mp_montgomery_calc_normalization()
2963 if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { in mp_montgomery_calc_normalization()
2973 for (x = bits - 1; x < (int)DIGIT_BIT; x++) { in mp_montgomery_calc_normalization()
2990 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
2992 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
2999 mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) in mp_exptmod_fast() argument
3003 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; in mp_exptmod_fast() local
3012 x = mp_count_bits (X); in mp_exptmod_fast()
3013 if (x <= 7) { in mp_exptmod_fast()
3015 } else if (x <= 36) { in mp_exptmod_fast()
3017 } else if (x <= 140) { in mp_exptmod_fast()
3019 } else if (x <= 450) { in mp_exptmod_fast()
3021 } else if (x <= 1303) { in mp_exptmod_fast()
3023 } else if (x <= 3529) { in mp_exptmod_fast()
3042 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in mp_exptmod_fast()
3043 if ((err = mp_init(&M[x])) != MP_OKAY) { in mp_exptmod_fast()
3044 for (y = 1<<(winsize-1); y < x; y++) { in mp_exptmod_fast()
3066 if (((P->used * 2 + 1) < MP_WARRAY) && in mp_exptmod_fast()
3067 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in mp_exptmod_fast()
3082 /* setup DR reduction for moduli of the form B**k - b */ in mp_exptmod_fast()
3091 /* setup DR reduction for moduli of the form 2**k - b */ in mp_exptmod_fast()
3136 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ in mp_exptmod_fast()
3137 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { in mp_exptmod_fast()
3141 for (x = 0; x < (winsize - 1); x++) { in mp_exptmod_fast()
3142 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { in mp_exptmod_fast()
3145 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { in mp_exptmod_fast()
3151 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { in mp_exptmod_fast()
3152 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { in mp_exptmod_fast()
3155 if ((err = redux (&M[x], P, mp)) != MP_OKAY) { in mp_exptmod_fast()
3164 digidx = X->used - 1; in mp_exptmod_fast()
3170 if (--bitcnt == 0) { in mp_exptmod_fast()
3171 /* if digidx == -1 we are out of digits so break */ in mp_exptmod_fast()
3172 if (digidx == -1) { in mp_exptmod_fast()
3176 buf = X->dp[digidx--]; in mp_exptmod_fast()
3181 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1; in mp_exptmod_fast()
3205 bitbuf |= (y << (winsize - ++bitcpy)); in mp_exptmod_fast()
3211 for (x = 0; x < winsize; x++) { in mp_exptmod_fast()
3238 for (x = 0; x < bitcpy; x++) { in mp_exptmod_fast()
3278 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in mp_exptmod_fast()
3279 mp_clear (&M[x]); in mp_exptmod_fast()
3291 * (ty-tx) so that it never happens. You double all those
3305 pa = a->used + a->used; in fast_s_mp_sqr()
3306 if (b->alloc < pa) { in fast_s_mp_sqr()
3323 ty = MIN(a->used-1, ix); in fast_s_mp_sqr()
3324 tx = ix - ty; in fast_s_mp_sqr()
3327 tmpx = a->dp + tx; in fast_s_mp_sqr()
3328 tmpy = a->dp + ty; in fast_s_mp_sqr()
3331 while (tx++ < a->used && ty-- >= 0) { ... } in fast_s_mp_sqr()
3333 iy = MIN(a->used-tx, ty+1); in fast_s_mp_sqr()
3336 * we halve the distance since they approach at a rate of 2x in fast_s_mp_sqr()
3339 iy = MIN(iy, (ty-tx+1)>>1); in fast_s_mp_sqr()
3343 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); in fast_s_mp_sqr()
3351 _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]); in fast_s_mp_sqr()
3362 olduse = b->used; in fast_s_mp_sqr()
3363 b->used = a->used+a->used; in fast_s_mp_sqr()
3367 tmpb = b->dp; in fast_s_mp_sqr()
3393 if (c->alloc < a->used + 1) { in mp_mul_d()
3394 if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) { in mp_mul_d()
3400 olduse = c->used; in mp_mul_d()
3403 c->sign = a->sign; in mp_mul_d()
3405 /* alias for a->dp [source] */ in mp_mul_d()
3406 tmpa = a->dp; in mp_mul_d()
3408 /* alias for c->dp [dest] */ in mp_mul_d()
3409 tmpc = c->dp; in mp_mul_d()
3415 for (ix = 0; ix < a->used; ix++) { in mp_mul_d()
3436 c->used = a->used + 1; in mp_mul_d()