Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bn_mp_reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
/* If x < 0, add b**(k+1) to it */
if (mp_cmp_d(x, 0uL) == MP_LT) {
mp_set(&q, 1uL);
if ((err = mp_lshd(&q, um + 1)) != MP_OKAY)
if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) {
goto CLEANUP;
if ((err = mp_add(x, &q, x)) != MP_OKAY)
}
if ((err = mp_add(x, &q, x)) != MP_OKAY) {
goto CLEANUP;
}
}

/* Back off if it's too big */
Expand Down
3 changes: 2 additions & 1 deletion bn_s_mp_invmod_slow.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
}

/* if not zero goto step 4 */
if (!MP_IS_ZERO(&u))
if (!MP_IS_ZERO(&u)) {
goto top;
}

/* now a = C, b = D, gcd == g*v */

Expand Down
54 changes: 36 additions & 18 deletions bn_s_mp_karatsuba_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,29 @@ mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
B = B >> 1;

/* init copy all the temps */
if (mp_init_size(&x0, B) != MP_OKAY)
if (mp_init_size(&x0, B) != MP_OKAY) {
goto LBL_ERR;
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
}
if (mp_init_size(&x1, a->used - B) != MP_OKAY) {
goto X0;
if (mp_init_size(&y0, B) != MP_OKAY)
}
if (mp_init_size(&y0, B) != MP_OKAY) {
goto X1;
if (mp_init_size(&y1, b->used - B) != MP_OKAY)
}
if (mp_init_size(&y1, b->used - B) != MP_OKAY) {
goto Y0;
}

/* init temps */
if (mp_init_size(&t1, B * 2) != MP_OKAY)
if (mp_init_size(&t1, B * 2) != MP_OKAY) {
goto Y1;
if (mp_init_size(&x0y0, B * 2) != MP_OKAY)
}
if (mp_init_size(&x0y0, B * 2) != MP_OKAY) {
goto T1;
if (mp_init_size(&x1y1, B * 2) != MP_OKAY)
}
if (mp_init_size(&x1y1, B * 2) != MP_OKAY) {
goto X0Y0;
}

/* now shift the digits */
x0.used = y0.used = B;
Expand Down Expand Up @@ -103,35 +110,46 @@ mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)

/* now calc the products x0y0 and x1y1 */
/* after this x0 is no longer required, free temp [x0==t2]! */
if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY)
if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY) {
goto X1Y1; /* x0y0 = x0*y0 */
if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY)
}
if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY) {
goto X1Y1; /* x1y1 = x1*y1 */
}

/* now calc x1+x0 and y1+y0 */
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY) {
goto X1Y1; /* t1 = x1 - x0 */
if (s_mp_add(&y1, &y0, &x0) != MP_OKAY)
}
if (s_mp_add(&y1, &y0, &x0) != MP_OKAY) {
goto X1Y1; /* t2 = y1 - y0 */
if (mp_mul(&t1, &x0, &t1) != MP_OKAY)
}
if (mp_mul(&t1, &x0, &t1) != MP_OKAY) {
goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
}

/* add x0y0 */
if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY)
if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY) {
goto X1Y1; /* t2 = x0y0 + x1y1 */
if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY)
}
if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY) {
goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
}

/* shift by B */
if (mp_lshd(&t1, B) != MP_OKAY)
if (mp_lshd(&t1, B) != MP_OKAY) {
goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
if (mp_lshd(&x1y1, B * 2) != MP_OKAY)
}
if (mp_lshd(&x1y1, B * 2) != MP_OKAY) {
goto X1Y1; /* x1y1 = x1y1 << 2*B */
}

if (mp_add(&x0y0, &t1, &t1) != MP_OKAY)
if (mp_add(&x0y0, &t1, &t1) != MP_OKAY) {
goto X1Y1; /* t1 = x0y0 + t1 */
if (mp_add(&t1, &x1y1, c) != MP_OKAY)
}
if (mp_add(&t1, &x1y1, c) != MP_OKAY) {
goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
}

/* Algorithm succeeded set the return code to MP_OKAY */
err = MP_OKAY;
Expand Down