Lines Matching +full:- +full:x
4 * http://libtom.org/files/ltm-0.41.tar.bz2
62 #define MIN(x,y) ((x)<(y)?(x):(y)) argument
66 #define MAX(x,y) ((x)>(y)?(x):(y)) argument
69 #define OPT_CAST(x) (x *) argument
83 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
85 #define MP_LT -1 /* less than */
93 #define MP_MEM -2 /* out of mem */
94 #define MP_VAL -3 /* invalid input */
113 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
114 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
123 /* ---> Basic Manipulations <--- */
124 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
125 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
126 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
130 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
131 static int s_mp_exptmod(mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
173 static int mp_reduce(mp_int * x, mp_int * m, mp_int * mu);
176 static int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
198 iy = len - 1; in bn_reverse()
204 --iy; in bn_reverse()
213 mp_int *x; in s_mp_add() local
217 * them. "x" will point to the input with the most digits in s_mp_add()
219 if (a->used > b->used) { in s_mp_add()
220 min = b->used; in s_mp_add()
221 max = a->used; in s_mp_add()
222 x = a; in s_mp_add()
224 min = a->used; in s_mp_add()
225 max = b->used; in s_mp_add()
226 x = b; in s_mp_add()
230 if (c->alloc < max + 1) { in s_mp_add()
237 olduse = c->used; in s_mp_add()
238 c->used = max + 1; in s_mp_add()
247 tmpa = a->dp; in s_mp_add()
250 tmpb = b->dp; in s_mp_add()
253 tmpc = c->dp; in s_mp_add()
273 /* T[i] = X[i] + U */ in s_mp_add()
274 *tmpc = x->dp[i] + u; in s_mp_add()
288 for (i = c->used; i < olduse; i++) { in s_mp_add()
305 min = b->used; in s_mp_sub()
306 max = a->used; in s_mp_sub()
309 if (c->alloc < max) { in s_mp_sub()
314 olduse = c->used; in s_mp_sub()
315 c->used = max; in s_mp_sub()
322 tmpa = a->dp; in s_mp_sub()
323 tmpb = b->dp; in s_mp_sub()
324 tmpc = c->dp; in s_mp_sub()
329 /* T[i] = A[i] - B[i] - U */ in s_mp_sub()
330 *tmpc = *tmpa++ - *tmpb++ - u; in s_mp_sub()
337 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); in s_mp_sub()
345 /* T[i] = A[i] - U */ in s_mp_sub()
346 *tmpc = *tmpa++ - u; in s_mp_sub()
349 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); in s_mp_sub()
356 for (i = c->used; i < olduse; i++) { in s_mp_sub()
373 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC); in mp_init()
374 if (a->dp == NULL) { in mp_init()
380 a->dp[i] = 0; in mp_init()
385 a->used = 0; in mp_init()
386 a->alloc = MP_PREC; in mp_init()
387 a->sign = MP_ZPOS; in mp_init()
400 if (a->dp != NULL) { in mp_clear()
402 for (i = 0; i < a->used; i++) { in mp_clear()
403 a->dp[i] = 0; in mp_clear()
407 XFREE(a->dp); in mp_clear()
410 a->dp = NULL; in mp_clear()
411 a->alloc = a->used = 0; in mp_clear()
412 a->sign = MP_ZPOS; in mp_clear()
424 sa = a->sign; in mp_add()
425 sb = b->sign; in mp_add()
431 c->sign = sa; in mp_add()
439 c->sign = sb; in mp_add()
442 c->sign = sa; in mp_add()
456 sa = a->sign; in mp_sub()
457 sb = b->sign; in mp_sub()
464 c->sign = sa; in mp_sub()
473 c->sign = sa; in mp_sub()
479 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; in mp_sub()
493 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; in mp_mul()
495 /* use Toom-Cook? */ in mp_mul()
497 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { in mp_mul()
503 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { in mp_mul()
515 int digs = a->used + b->used + 1; in mp_mul()
518 MIN(a->used, b->used) <= in mp_mul()
519 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in mp_mul()
531 c->sign = (c->used > 0) ? neg : MP_ZPOS; in mp_mul()
573 if (t.sign != b->sign) { in mp_mod()
591 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()
664 return mp_exptmod_fast (G, X, P, Y, dr); in mp_exptmod()
669 return s_mp_exptmod (G, X, P, Y, 0); in mp_exptmod()
686 if (a->sign != b->sign) { in mp_cmp()
687 if (a->sign == MP_NEG) { in mp_cmp()
695 if (a->sign == MP_NEG) { in mp_cmp()
709 if (a->sign == MP_NEG) { in mp_cmp_d()
714 if (a->used > 1) { in mp_cmp_d()
719 if (a->dp[0] > b) { in mp_cmp_d()
721 } else if (a->dp[0] < b) { in mp_cmp_d()
735 if (b->sign == MP_NEG || mp_iszero(b) == 1) { in mp_invmod()
774 mp_int x, y, u, v, A, B, C, D; in mp_invmod_slow() local
778 if (b->sign == MP_NEG || mp_iszero(b) == 1) { in mp_invmod_slow()
783 if ((res = mp_init_multi(&x, &y, &u, &v, in mp_invmod_slow()
788 /* x = a, y = b */ in mp_invmod_slow()
789 if ((res = mp_mod(a, b, &x)) != MP_OKAY) { in mp_invmod_slow()
796 /* 2. [modified] if x,y are both even then return an error! */ in mp_invmod_slow()
797 if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { in mp_invmod_slow()
802 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ in mp_invmod_slow()
803 if ((res = mp_copy (&x, &u)) != MP_OKAY) { in mp_invmod_slow()
821 /* A = (A+y)/2, B = (B-x)/2 */ in mp_invmod_slow()
825 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { in mp_invmod_slow()
846 /* C = (C+y)/2, D = (D-x)/2 */ in mp_invmod_slow()
850 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { in mp_invmod_slow()
865 /* u = u - v, A = A - C, B = B - D */ in mp_invmod_slow()
878 /* v - v - u, C = C - A, D = D - B */ in mp_invmod_slow()
921 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); in mp_invmod_slow()
934 /* compare based on # of non-zero digits */ in mp_cmp_mag()
935 if (a->used > b->used) { in mp_cmp_mag()
939 if (a->used < b->used) { in mp_cmp_mag()
944 tmpa = a->dp + (a->used - 1); in mp_cmp_mag()
947 tmpb = b->dp + (a->used - 1); in mp_cmp_mag()
950 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { in mp_cmp_mag()
970 if (a->alloc < 2) { in mp_read_unsigned_bin()
980 while (c-- > 0) { in mp_read_unsigned_bin()
986 a->dp[0] |= *b++; in mp_read_unsigned_bin()
987 a->used += 1; in mp_read_unsigned_bin()
989 a->dp[0] = (*b & MP_MASK); in mp_read_unsigned_bin()
990 a->dp[1] |= ((*b++ >> 7U) & 1); in mp_read_unsigned_bin()
991 a->used += 2; in mp_read_unsigned_bin()
1003 int x, res; in mp_to_unsigned_bin() local
1010 x = 0; in mp_to_unsigned_bin()
1013 b[x++] = (unsigned char) (t.dp[0] & 255); in mp_to_unsigned_bin()
1015 b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7)); in mp_to_unsigned_bin()
1022 bn_reverse (b, x); in mp_to_unsigned_bin()
1033 int x, res; in mp_div_2d() local
1075 mask = (((mp_digit)1) << D) - 1; in mp_div_2d()
1078 shift = DIGIT_BIT - D; in mp_div_2d()
1081 tmpc = c->dp + (c->used - 1); in mp_div_2d()
1085 for (x = c->used - 1; x >= 0; x--) { in mp_div_2d()
1091 --tmpc; in mp_div_2d()
1125 a->sign = MP_ZPOS; in mp_zero()
1126 a->used = 0; in mp_zero()
1128 tmp = a->dp; in mp_zero()
1129 for (n = 0; n < a->alloc; n++) { in mp_zero()
1147 if (b->alloc < a->used) { in mp_copy()
1148 if ((res = mp_grow (b, a->used)) != MP_OKAY) { in mp_copy()
1160 tmpa = a->dp; in mp_copy()
1163 tmpb = b->dp; in mp_copy()
1166 for (n = 0; n < a->used; n++) { in mp_copy()
1171 for (; n < b->used; n++) { in mp_copy()
1177 b->used = a->used; in mp_copy()
1178 b->sign = a->sign; in mp_copy()
1187 int x; in mp_rshd() local
1195 if (a->used <= b) { in mp_rshd()
1206 bottom = a->dp; in mp_rshd()
1209 top = a->dp + b; in mp_rshd()
1212 * the window is b-digits long and digits from in mp_rshd()
1217 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> in mp_rshd()
1218 /\ | ----> in mp_rshd()
1219 \-------------------/ ----> in mp_rshd()
1221 for (x = 0; x < (a->used - b); x++) { in mp_rshd()
1226 for (; x < a->used; x++) { in mp_rshd()
1232 a->used -= b; in mp_rshd()
1253 * trimed and the leading "used" digit will be non-zero
1263 while (a->used > 0 && a->dp[a->used - 1] == 0) { in mp_clamp()
1264 --(a->used); in mp_clamp()
1268 if (a->used == 0) { in mp_clamp()
1269 a->sign = MP_ZPOS; in mp_clamp()
1282 if (a->alloc < size) { in mp_grow()
1284 size += (MP_PREC * 2) - (size % MP_PREC); in mp_grow()
1286 /* reallocate the array a->dp in mp_grow()
1292 tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size); in mp_grow()
1298 /* reallocation succeeded so set a->dp */ in mp_grow()
1299 a->dp = tmp; in mp_grow()
1302 i = a->alloc; in mp_grow()
1303 a->alloc = size; in mp_grow()
1304 for (; i < a->alloc; i++) { in mp_grow()
1305 a->dp[i] = 0; in mp_grow()
1330 b->sign = MP_ZPOS; in mp_abs()
1342 a->dp[0] = b & MP_MASK; in mp_set()
1343 a->used = (a->dp[0] != 0) ? 1 : 0; in mp_set()
1352 int x, res, oldused; in mp_div_2() local
1355 if (b->alloc < a->used) { in mp_div_2()
1356 if ((res = mp_grow (b, a->used)) != MP_OKAY) { in mp_div_2()
1361 oldused = b->used; in mp_div_2()
1362 b->used = a->used; in mp_div_2()
1367 tmpa = a->dp + b->used - 1; in mp_div_2()
1370 tmpb = b->dp + b->used - 1; in mp_div_2()
1374 for (x = b->used - 1; x >= 0; x--) { in mp_div_2()
1379 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); in mp_div_2()
1386 tmpb = b->dp + b->used; in mp_div_2()
1387 for (x = b->used; x < oldused; x++) { in mp_div_2()
1391 b->sign = a->sign; in mp_div_2()
1412 if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) { in mp_mul_2d()
1413 if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) { in mp_mul_2d()
1429 register int x; in mp_mul_2d() local
1432 mask = (((mp_digit)1) << d) - 1; in mp_mul_2d()
1435 shift = DIGIT_BIT - d; in mp_mul_2d()
1438 tmpc = c->dp; in mp_mul_2d()
1442 for (x = 0; x < c->used; x++) { in mp_mul_2d()
1456 c->dp[(c->used)++] = r; in mp_mul_2d()
1476 /* Oops - error! Back-track and mp_clear what we already in mp_init_multi()
1477 succeeded in init-ing, then return error. in mp_init_multi()
1487 while (n--) { in mp_init_multi()
1524 int x, res; in mp_lshd() local
1532 if (a->alloc < a->used + b) { in mp_lshd()
1533 if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { in mp_lshd()
1542 a->used += b; in mp_lshd()
1545 top = a->dp + a->used - 1; in mp_lshd()
1548 bottom = a->dp + a->used - 1 - b; in mp_lshd()
1554 for (x = a->used - 1; x >= b; x--) { in mp_lshd()
1555 *top-- = *bottom--; in mp_lshd()
1559 top = a->dp; in mp_lshd()
1560 for (x = 0; x < b; x++) { in mp_lshd()
1576 if (a->used == 0) { in mp_count_bits()
1581 r = (a->used - 1) * DIGIT_BIT; in mp_count_bits()
1584 q = a->dp[a->used - 1]; in mp_count_bits()
1597 int x, res; in mp_mod_2d() local
1606 if (b >= (int) (a->used * DIGIT_BIT)) { in mp_mod_2d()
1617 for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) { in mp_mod_2d()
1618 c->dp[x] = 0; in mp_mod_2d()
1621 c->dp[b / DIGIT_BIT] &= in mp_mod_2d()
1622 (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); in mp_mod_2d()
1630 /* slower bit-bang division... also smaller */
1662 n = mp_count_bits(a) - mp_count_bits(b); in mp_div()
1670 while (n-- >= 0) { in mp_div()
1684 n = a->sign; in mp_div()
1685 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); in mp_div()
1688 c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; in mp_div()
1692 d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; in mp_div()
1707 * the case where digits are removed from 'x' in
1717 mp_int q, x, y, t1, t2; in mp_div() local
1738 if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) { in mp_div()
1741 q.used = a->used + 2; in mp_div()
1751 if ((res = mp_init_copy (&x, a)) != MP_OKAY) { in mp_div()
1760 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; in mp_div()
1761 x.sign = y.sign = MP_ZPOS; in mp_div()
1763 /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ in mp_div()
1765 if (norm < (int)(DIGIT_BIT-1)) { in mp_div()
1766 norm = (DIGIT_BIT-1) - norm; in mp_div()
1767 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) { in mp_div()
1778 n = x.used - 1; in mp_div()
1779 t = y.used - 1; in mp_div()
1781 /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ in mp_div()
1782 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ in mp_div()
1786 while (mp_cmp (&x, &y) != MP_LT) { in mp_div()
1787 ++(q.dp[n - t]); in mp_div()
1788 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) { in mp_div()
1794 mp_rshd (&y, n - t); in mp_div()
1797 for (i = n; i >= (t + 1); i--) { in mp_div()
1798 if (i > x.used) { in mp_div()
1802 /* step 3.1 if xi == yt then set q{i-t-1} to b-1, in mp_div()
1803 * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ in mp_div()
1804 if (x.dp[i] == y.dp[t]) { in mp_div()
1805 q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); in mp_div()
1808 tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); in mp_div()
1809 tmp |= ((mp_word) x.dp[i - 1]); in mp_div()
1813 q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); in mp_div()
1816 /* while (q{i-t-1} * (yt * b + y{t-1})) > in mp_div()
1817 xi * b**2 + xi-1 * b + xi-2 in mp_div()
1819 do q{i-t-1} -= 1; in mp_div()
1821 q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; in mp_div()
1823 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK; in mp_div()
1827 t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1]; in mp_div()
1830 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { in mp_div()
1835 t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2]; in mp_div()
1836 t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1]; in mp_div()
1837 t2.dp[2] = x.dp[i]; in mp_div()
1841 /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ in mp_div()
1842 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { in mp_div()
1846 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { in mp_div()
1850 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) { in mp_div()
1854 /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ in mp_div()
1855 if (x.sign == MP_NEG) { in mp_div()
1859 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { in mp_div()
1862 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { in mp_div()
1866 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; in mp_div()
1870 /* now q is the quotient and x is the remainder in mp_div()
1875 x.sign = x.used == 0 ? MP_ZPOS : a->sign; in mp_div()
1880 c->sign = neg; in mp_div()
1884 mp_div_2d (&x, norm, &x, NULL); in mp_div()
1885 mp_exch (&x, d); in mp_div()
1891 LBL_X:mp_clear (&x); in mp_div()
1908 s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) in s_mp_exptmod() argument
1912 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; in s_mp_exptmod() local
1916 x = mp_count_bits (X); in s_mp_exptmod()
1917 if (x <= 7) { in s_mp_exptmod()
1919 } else if (x <= 36) { in s_mp_exptmod()
1921 } else if (x <= 140) { in s_mp_exptmod()
1923 } else if (x <= 450) { in s_mp_exptmod()
1925 } else if (x <= 1303) { in s_mp_exptmod()
1927 } else if (x <= 3529) { in s_mp_exptmod()
1946 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in s_mp_exptmod()
1947 if ((err = mp_init(&M[x])) != MP_OKAY) { in s_mp_exptmod()
1948 for (y = 1<<(winsize-1); y < x; y++) { in s_mp_exptmod()
1976 * e.g. M[x] = G**x mod P in s_mp_exptmod()
1985 /* compute the value at M[1<<(winsize-1)] by squaring in s_mp_exptmod()
1986 * M[1] (winsize-1) times in s_mp_exptmod()
1988 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { in s_mp_exptmod()
1992 for (x = 0; x < (winsize - 1); x++) { in s_mp_exptmod()
1994 if ((err = mp_sqr (&M[1 << (winsize - 1)], in s_mp_exptmod()
1995 &M[1 << (winsize - 1)])) != MP_OKAY) { in s_mp_exptmod()
2000 if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { in s_mp_exptmod()
2005 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) in s_mp_exptmod()
2006 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) in s_mp_exptmod()
2008 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { in s_mp_exptmod()
2009 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { in s_mp_exptmod()
2012 if ((err = redux (&M[x], P, &mu)) != MP_OKAY) { in s_mp_exptmod()
2027 digidx = X->used - 1; in s_mp_exptmod()
2033 if (--bitcnt == 0) { in s_mp_exptmod()
2034 /* if digidx == -1 we are out of digits */ in s_mp_exptmod()
2035 if (digidx == -1) { in s_mp_exptmod()
2039 buf = X->dp[digidx--]; in s_mp_exptmod()
2044 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1; in s_mp_exptmod()
2068 bitbuf |= (y << (winsize - ++bitcpy)); in s_mp_exptmod()
2074 for (x = 0; x < winsize; x++) { in s_mp_exptmod()
2101 for (x = 0; x < bitcpy; x++) { in s_mp_exptmod()
2128 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in s_mp_exptmod()
2129 mp_clear (&M[x]); in s_mp_exptmod()
2142 /* use Toom-Cook? */ in mp_sqr()
2143 if (a->used >= TOOM_SQR_CUTOFF) { in mp_sqr()
2149 if (a->used >= KARATSUBA_SQR_CUTOFF) { in mp_sqr()
2156 if ((a->used * 2 + 1) < MP_WARRAY && in mp_sqr()
2157 a->used < in mp_sqr()
2158 (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { in mp_sqr()
2169 b->sign = MP_ZPOS; in mp_sqr()
2174 /* reduces a modulo n where n is of the form 2**p - d
2260 a->used = b / DIGIT_BIT + 1; in mp_2expt()
2263 a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); in mp_2expt()
2269 /* pre-calculate the value required for Barrett reduction
2277 if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { in mp_reduce_setup()
2284 /* reduces x mod m, assumes 0 < x < m**2, mu is
2289 mp_reduce (mp_int * x, mp_int * m, mp_int * mu) in mp_reduce() argument
2292 int res, um = m->used; in mp_reduce()
2294 /* q = x */ in mp_reduce()
2295 if ((res = mp_init_copy (&q, x)) != MP_OKAY) { in mp_reduce()
2299 /* q1 = x / b**(k-1) */ in mp_reduce()
2300 mp_rshd (&q, um - 1); in mp_reduce()
2303 if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { in mp_reduce()
2328 /* x = x mod b**(k+1), quick (no division) */ in mp_reduce()
2329 if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) { in mp_reduce()
2338 /* x = x - q */ in mp_reduce()
2339 if ((res = mp_sub (x, &q, x)) != MP_OKAY) { in mp_reduce()
2343 /* If x < 0, add b**(k+1) to it */ in mp_reduce()
2344 if (mp_cmp_d (x, 0) == MP_LT) { in mp_reduce()
2349 if ((res = mp_add (x, &q, x)) != MP_OKAY) { in mp_reduce()
2355 while (mp_cmp (x, m) != MP_LT) { in mp_reduce()
2356 if ((res = s_mp_sub (x, m, x)) != MP_OKAY) { in mp_reduce()
2383 MIN (a->used, b->used) < in s_mp_mul_digs()
2384 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in s_mp_mul_digs()
2394 pa = a->used; in s_mp_mul_digs()
2400 pb = MIN (b->used, digs - ix); in s_mp_mul_digs()
2404 tmpx = a->dp[ix]; in s_mp_mul_digs()
2410 tmpy = b->dp; in s_mp_mul_digs()
2441 * This is the fast column-array [comba] multiplier. It is
2445 * simple and schedulable on super-scalar processors.
2448 * digits of output so if say only a half-product is required
2463 if (c->alloc < digs) { in fast_s_mp_mul_digs()
2470 pa = MIN(digs, a->used + b->used); in fast_s_mp_mul_digs()
2480 ty = MIN(b->used-1, ix); in fast_s_mp_mul_digs()
2481 tx = ix - ty; in fast_s_mp_mul_digs()
2484 tmpx = a->dp + tx; in fast_s_mp_mul_digs()
2485 tmpy = b->dp + ty; in fast_s_mp_mul_digs()
2488 while (tx++ < a->used && ty-- >= 0) { ... } in fast_s_mp_mul_digs()
2490 iy = MIN(a->used-tx, ty+1); in fast_s_mp_mul_digs()
2494 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); in fast_s_mp_mul_digs()
2506 olduse = c->used; in fast_s_mp_mul_digs()
2507 c->used = pa; in fast_s_mp_mul_digs()
2511 tmpc = c->dp; in fast_s_mp_mul_digs()
2531 int x; in mp_init_size() local
2534 size += (MP_PREC * 2) - (size % MP_PREC); in mp_init_size()
2537 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); in mp_init_size()
2538 if (a->dp == NULL) { in mp_init_size()
2543 a->used = 0; in mp_init_size()
2544 a->alloc = size; in mp_init_size()
2545 a->sign = MP_ZPOS; in mp_init_size()
2548 for (x = 0; x < size; x++) { in mp_init_size()
2549 a->dp[x] = 0; in mp_init_size()
2556 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
2565 pa = a->used; in s_mp_sqr()
2577 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); in s_mp_sqr()
2586 tmpx = a->dp[ix]; in s_mp_sqr()
2593 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); in s_mp_sqr()
2635 if (((a->used + b->used + 1) < MP_WARRAY) in s_mp_mul_high_digs()
2636 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in s_mp_mul_high_digs()
2641 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { in s_mp_mul_high_digs()
2644 t.used = a->used + b->used + 1; in s_mp_mul_high_digs()
2646 pa = a->used; in s_mp_mul_high_digs()
2647 pb = b->used; in s_mp_mul_high_digs()
2653 tmpx = a->dp[ix]; in s_mp_mul_high_digs()
2659 tmpy = b->dp + (digs - ix); in s_mp_mul_high_digs()
2661 for (iy = digs - ix; iy < pb; iy++) { in s_mp_mul_high_digs()
2687 mp_digit x, b; in mp_montgomery_setup() local
2693 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) in mp_montgomery_setup()
2694 * => 2*X*A - X*X*A*A = 1 in mp_montgomery_setup()
2695 * => 2*(1) - (1) = 1 in mp_montgomery_setup()
2697 b = n->dp[0]; in mp_montgomery_setup()
2703 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ in mp_montgomery_setup()
2704 x *= 2 - b * x; /* here x*a==1 mod 2**8 */ in mp_montgomery_setup()
2706 x *= 2 - b * x; /* here x*a==1 mod 2**16 */ in mp_montgomery_setup()
2709 x *= 2 - b * x; /* here x*a==1 mod 2**32 */ in mp_montgomery_setup()
2712 x *= 2 - b * x; /* here x*a==1 mod 2**64 */ in mp_montgomery_setup()
2715 /* rho = -1/m mod b */ in mp_montgomery_setup()
2716 *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; in mp_montgomery_setup()
2724 /* computes xR**-1 == x (mod N) via Montgomery Reduction
2733 fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) in fast_mp_montgomery_reduce() argument
2739 olduse = x->used; in fast_mp_montgomery_reduce()
2742 if (x->alloc < n->used + 1) { in fast_mp_montgomery_reduce()
2743 if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) { in fast_mp_montgomery_reduce()
2758 /* alias for the digits of x*/ in fast_mp_montgomery_reduce()
2759 tmpx = x->dp; in fast_mp_montgomery_reduce()
2761 /* copy the digits of a into W[0..a->used-1] */ in fast_mp_montgomery_reduce()
2762 for (ix = 0; ix < x->used; ix++) { in fast_mp_montgomery_reduce()
2766 /* zero the high words of W[a->used..m->used*2] */ in fast_mp_montgomery_reduce()
2767 for (; ix < n->used * 2 + 1; ix++) { in fast_mp_montgomery_reduce()
2775 for (ix = 0; ix < n->used; ix++) { in fast_mp_montgomery_reduce()
2780 * that W[ix-1] have the carry cleared (see after the inner loop) in fast_mp_montgomery_reduce()
2797 * first m->used words of W[] have the carries fixed in fast_mp_montgomery_reduce()
2805 tmpn = n->dp; in fast_mp_montgomery_reduce()
2811 for (iy = 0; iy < n->used; iy++) { in fast_mp_montgomery_reduce()
2836 for (; ix <= n->used * 2 + 1; ix++) { in fast_mp_montgomery_reduce()
2848 tmpx = x->dp; in fast_mp_montgomery_reduce()
2851 _W = W + n->used; in fast_mp_montgomery_reduce()
2853 for (ix = 0; ix < n->used + 1; ix++) { in fast_mp_montgomery_reduce()
2858 * m->used+1 we'll have to clear the digits in fast_mp_montgomery_reduce()
2866 x->used = n->used + 1; in fast_mp_montgomery_reduce()
2867 mp_clamp (x); in fast_mp_montgomery_reduce()
2869 /* if A >= m then A = A - m */ in fast_mp_montgomery_reduce()
2870 if (mp_cmp_mag (x, n) != MP_LT) { in fast_mp_montgomery_reduce()
2871 return s_mp_sub (x, n, x); in fast_mp_montgomery_reduce()
2883 int x, res, oldused; in mp_mul_2() local
2886 if (b->alloc < a->used + 1) { in mp_mul_2()
2887 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { in mp_mul_2()
2892 oldused = b->used; in mp_mul_2()
2893 b->used = a->used; in mp_mul_2()
2899 tmpa = a->dp; in mp_mul_2()
2902 tmpb = b->dp; in mp_mul_2()
2906 for (x = 0; x < a->used; x++) { in mp_mul_2()
2911 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); in mp_mul_2()
2926 ++(b->used); in mp_mul_2()
2932 tmpb = b->dp + b->used; in mp_mul_2()
2933 for (x = b->used; x < oldused; x++) { in mp_mul_2()
2937 b->sign = a->sign; in mp_mul_2()
2953 int x, bits, res; in mp_montgomery_calc_normalization() local
2958 if (b->used > 1) { in mp_montgomery_calc_normalization()
2959 if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { in mp_montgomery_calc_normalization()
2969 for (x = bits - 1; x < (int)DIGIT_BIT; x++) { in mp_montgomery_calc_normalization()
2986 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
2988 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
2995 mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) in mp_exptmod_fast() argument
2999 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; in mp_exptmod_fast() local
3008 x = mp_count_bits (X); in mp_exptmod_fast()
3009 if (x <= 7) { in mp_exptmod_fast()
3011 } else if (x <= 36) { in mp_exptmod_fast()
3013 } else if (x <= 140) { in mp_exptmod_fast()
3015 } else if (x <= 450) { in mp_exptmod_fast()
3017 } else if (x <= 1303) { in mp_exptmod_fast()
3019 } else if (x <= 3529) { in mp_exptmod_fast()
3038 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in mp_exptmod_fast()
3039 if ((err = mp_init(&M[x])) != MP_OKAY) { in mp_exptmod_fast()
3040 for (y = 1<<(winsize-1); y < x; y++) { in mp_exptmod_fast()
3062 if (((P->used * 2 + 1) < MP_WARRAY) && in mp_exptmod_fast()
3063 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { in mp_exptmod_fast()
3078 /* setup DR reduction for moduli of the form B**k - b */ in mp_exptmod_fast()
3087 /* setup DR reduction for moduli of the form 2**k - b */ in mp_exptmod_fast()
3132 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ in mp_exptmod_fast()
3133 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { in mp_exptmod_fast()
3137 for (x = 0; x < (winsize - 1); x++) { in mp_exptmod_fast()
3138 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { in mp_exptmod_fast()
3141 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { in mp_exptmod_fast()
3147 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { in mp_exptmod_fast()
3148 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { in mp_exptmod_fast()
3151 if ((err = redux (&M[x], P, mp)) != MP_OKAY) { in mp_exptmod_fast()
3160 digidx = X->used - 1; in mp_exptmod_fast()
3166 if (--bitcnt == 0) { in mp_exptmod_fast()
3167 /* if digidx == -1 we are out of digits so break */ in mp_exptmod_fast()
3168 if (digidx == -1) { in mp_exptmod_fast()
3172 buf = X->dp[digidx--]; in mp_exptmod_fast()
3177 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1; in mp_exptmod_fast()
3201 bitbuf |= (y << (winsize - ++bitcpy)); in mp_exptmod_fast()
3207 for (x = 0; x < winsize; x++) { in mp_exptmod_fast()
3234 for (x = 0; x < bitcpy; x++) { in mp_exptmod_fast()
3274 for (x = 1<<(winsize-1); x < (1 << winsize); x++) { in mp_exptmod_fast()
3275 mp_clear (&M[x]); in mp_exptmod_fast()
3287 * (ty-tx) so that it never happens. You double all those
3301 pa = a->used + a->used; in fast_s_mp_sqr()
3302 if (b->alloc < pa) { in fast_s_mp_sqr()
3319 ty = MIN(a->used-1, ix); in fast_s_mp_sqr()
3320 tx = ix - ty; in fast_s_mp_sqr()
3323 tmpx = a->dp + tx; in fast_s_mp_sqr()
3324 tmpy = a->dp + ty; in fast_s_mp_sqr()
3327 while (tx++ < a->used && ty-- >= 0) { ... } in fast_s_mp_sqr()
3329 iy = MIN(a->used-tx, ty+1); in fast_s_mp_sqr()
3332 * we halve the distance since they approach at a rate of 2x in fast_s_mp_sqr()
3335 iy = MIN(iy, (ty-tx+1)>>1); in fast_s_mp_sqr()
3339 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); in fast_s_mp_sqr()
3347 _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]); in fast_s_mp_sqr()
3358 olduse = b->used; in fast_s_mp_sqr()
3359 b->used = a->used+a->used; in fast_s_mp_sqr()
3363 tmpb = b->dp; in fast_s_mp_sqr()
3389 if (c->alloc < a->used + 1) { in mp_mul_d()
3390 if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) { in mp_mul_d()
3396 olduse = c->used; in mp_mul_d()
3399 c->sign = a->sign; in mp_mul_d()
3401 /* alias for a->dp [source] */ in mp_mul_d()
3402 tmpa = a->dp; in mp_mul_d()
3404 /* alias for c->dp [dest] */ in mp_mul_d()
3405 tmpc = c->dp; in mp_mul_d()
3411 for (ix = 0; ix < a->used; ix++) { in mp_mul_d()
3432 c->used = a->used + 1; in mp_mul_d()