trim trailing spaces

This commit is contained in:
Steffen Jaeckel 2015-11-12 01:18:15 +01:00
parent 1c1baaa755
commit 00ff6da1cc
5 changed files with 69 additions and 69 deletions

View File

@ -40,7 +40,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
} }
return res; return res;
} }
/* init our temps */ /* init our temps */
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) { if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) {
return res; return res;
@ -50,7 +50,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
mp_set(&tq, 1); mp_set(&tq, 1);
n = mp_count_bits(a) - mp_count_bits(b); n = mp_count_bits(a) - mp_count_bits(b);
if (((res = mp_abs(a, &ta)) != MP_OKAY) || if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
((res = mp_abs(b, &tb)) != MP_OKAY) || ((res = mp_abs(b, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
goto LBL_ERR; goto LBL_ERR;
@ -87,17 +87,17 @@ LBL_ERR:
#else #else
/* integer signed division. /* integer signed division.
* c*b + d == a [e.g. a/b, c=quotient, d=remainder] * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
* HAC pp.598 Algorithm 14.20 * HAC pp.598 Algorithm 14.20
* *
* Note that the description in HAC is horribly * Note that the description in HAC is horribly
* incomplete. For example, it doesn't consider * incomplete. For example, it doesn't consider
* the case where digits are removed from 'x' in * the case where digits are removed from 'x' in
* the inner loop. It also doesn't consider the * the inner loop. It also doesn't consider the
* case that y has fewer than three digits, etc.. * case that y has fewer than three digits, etc..
* *
* The overall algorithm is as described as * The overall algorithm is as described as
* 14.20 from HAC but fixed to treat these cases. * 14.20 from HAC but fixed to treat these cases.
*/ */
int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
@ -187,7 +187,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
continue; continue;
} }
/* step 3.1 if xi == yt then set q{i-t-1} to b-1, /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
if (x.dp[i] == y.dp[t]) { if (x.dp[i] == y.dp[t]) {
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
@ -202,10 +202,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
} }
/* while (q{i-t-1} * (yt * b + y{t-1})) > /* while (q{i-t-1} * (yt * b + y{t-1})) >
xi * b**2 + xi-1 * b + xi-2 xi * b**2 + xi-1 * b + xi-2
do q{i-t-1} -= 1; do q{i-t-1} -= 1;
*/ */
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
do { do {
@ -256,10 +256,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
} }
} }
/* now q is the quotient and x is the remainder /* now q is the quotient and x is the remainder
* [which we have to normalize] * [which we have to normalize]
*/ */
/* get sign before writing to c */ /* get sign before writing to c */
x.sign = x.used == 0 ? MP_ZPOS : a->sign; x.sign = x.used == 0 ? MP_ZPOS : a->sign;

View File

@ -15,7 +15,7 @@
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/ */
/* Extended euclidean algorithm of (a, b) produces /* Extended euclidean algorithm of (a, b) produces
a*u1 + b*u2 = u3 a*u1 + b*u2 = u3
*/ */
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)

View File

@ -20,37 +20,37 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
{ {
mp_int q; mp_int q;
int p, res; int p, res;
if ((res = mp_init(&q)) != MP_OKAY) { if ((res = mp_init(&q)) != MP_OKAY) {
return res; return res;
} }
p = mp_count_bits(n); p = mp_count_bits(n);
top: top:
/* q = a/2**p, a = a mod 2**p */ /* q = a/2**p, a = a mod 2**p */
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if (d != 1) { if (d != 1) {
/* q = q * d */ /* q = q * d */
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
goto ERR; goto ERR;
} }
} }
/* a = a + q */ /* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if (mp_cmp_mag(a, n) != MP_LT) { if (mp_cmp_mag(a, n) != MP_LT) {
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
goto top; goto top;
} }
ERR: ERR:
mp_clear(&q); mp_clear(&q);
return res; return res;

View File

@ -15,7 +15,7 @@
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/ */
/* reduces a modulo n where n is of the form 2**p - d /* reduces a modulo n where n is of the form 2**p - d
This differs from reduce_2k since "d" can be larger This differs from reduce_2k since "d" can be larger
than a single digit. than a single digit.
*/ */
@ -23,35 +23,35 @@ int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
{ {
mp_int q; mp_int q;
int p, res; int p, res;
if ((res = mp_init(&q)) != MP_OKAY) { if ((res = mp_init(&q)) != MP_OKAY) {
return res; return res;
} }
p = mp_count_bits(n); p = mp_count_bits(n);
top: top:
/* q = a/2**p, a = a mod 2**p */ /* q = a/2**p, a = a mod 2**p */
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* q = q * d */ /* q = q * d */
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* a = a + q */ /* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if (mp_cmp_mag(a, n) != MP_LT) { if (mp_cmp_mag(a, n) != MP_LT) {
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
goto ERR; goto ERR;
} }
goto top; goto top;
} }
ERR: ERR:
mp_clear(&q); mp_clear(&q);
return res; return res;

View File

@ -15,28 +15,28 @@
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/ */
/* multiplication using the Toom-Cook 3-way algorithm /* multiplication using the Toom-Cook 3-way algorithm
* *
* Much more complicated than Karatsuba but has a lower * Much more complicated than Karatsuba but has a lower
* asymptotic running time of O(N**1.464). This algorithm is * asymptotic running time of O(N**1.464). This algorithm is
* only particularly useful on VERY large inputs * only particularly useful on VERY large inputs
* (we're talking 1000s of digits here...). * (we're talking 1000s of digits here...).
*/ */
int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
{ {
mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2; mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
int res, B; int res, B;
/* init temps */ /* init temps */
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
&a0, &a1, &a2, &b0, &b1, &a0, &a1, &a2, &b0, &b1,
&b2, &tmp1, &tmp2, NULL)) != MP_OKAY) { &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
return res; return res;
} }
/* B */ /* B */
B = MIN(a->used, b->used) / 3; B = MIN(a->used, b->used) / 3;
/* a = a2 * B**2 + a1 * B + a0 */ /* a = a2 * B**2 + a1 * B + a0 */
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) { if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
goto ERR; goto ERR;
@ -54,7 +54,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
goto ERR; goto ERR;
} }
mp_rshd(&a2, B*2); mp_rshd(&a2, B*2);
/* b = b2 * B**2 + b1 * B + b0 */ /* b = b2 * B**2 + b1 * B + b0 */
if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) { if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
goto ERR; goto ERR;
@ -70,17 +70,17 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
goto ERR; goto ERR;
} }
mp_rshd(&b2, B*2); mp_rshd(&b2, B*2);
/* w0 = a0*b0 */ /* w0 = a0*b0 */
if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) { if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* w4 = a2 * b2 */ /* w4 = a2 * b2 */
if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) { if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */ /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) { if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
goto ERR; goto ERR;
@ -94,7 +94,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) { if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) { if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
goto ERR; goto ERR;
} }
@ -107,11 +107,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) { if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) { if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */ /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) { if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
goto ERR; goto ERR;
@ -125,7 +125,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) { if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
goto ERR; goto ERR;
} }
@ -138,11 +138,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) { if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) { if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */ /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) { if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
@ -160,19 +160,19 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) { if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* now solve the matrix /* now solve the matrix
0 0 0 0 1 0 0 0 0 1
1 2 4 8 16 1 2 4 8 16
1 1 1 1 1 1 1 1 1 1
16 8 4 2 1 16 8 4 2 1
1 0 0 0 0 1 0 0 0 0
using 12 subtractions, 4 shifts, using 12 subtractions, 4 shifts,
2 small divisions and 1 small multiplication 2 small divisions and 1 small multiplication
*/ */
/* r1 - r4 */ /* r1 - r4 */
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
goto ERR; goto ERR;
@ -244,7 +244,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
goto ERR; goto ERR;
} }
/* at this point shift W[n] by B*n */ /* at this point shift W[n] by B*n */
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
goto ERR; goto ERR;
@ -257,8 +257,8 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
} }
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
goto ERR; goto ERR;
} }
if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) { if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
goto ERR; goto ERR;
} }
@ -270,15 +270,15 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
} }
if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) { if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
goto ERR; goto ERR;
} }
ERR: ERR:
mp_clear_multi(&w0, &w1, &w2, &w3, &w4, mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
&a0, &a1, &a2, &b0, &b1, &a0, &a1, &a2, &b0, &b1,
&b2, &tmp1, &tmp2, NULL); &b2, &tmp1, &tmp2, NULL);
return res; return res;
} }
#endif #endif
/* $Source$ */ /* $Source$ */