Skip to content

Commit

Permalink
n_factor_ecm_select_curve: fix rare arithmetic bug and fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Nov 2, 2023
1 parent 96863fb commit 016de13
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/ulong_extras/factor_ecm.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ n_factor_ecm_select_curve(mp_limb_t *f, mp_limb_t sig, mp_limb_t n, n_ecm_t n_ec
{
mp_limb_t u, v, w, t, hi, lo;
mp_ptr a;
int ret = 0;
TMP_INIT;

TMP_START;
Expand All @@ -351,7 +352,7 @@ n_factor_ecm_select_curve(mp_limb_t *f, mp_limb_t sig, mp_limb_t n, n_ecm_t n_ec
/* w = sig ^ 2 */
w = n_mulmod_preinv(u, u, n, n_ecm_inf->ninv, n_ecm_inf->normbits);

u = w - (UWORD(5) << n_ecm_inf->normbits); /* u = sig^2 - 5 */
u = n_submod(w, UWORD(5) << n_ecm_inf->normbits, n); /* u = sig^2 - 5 */

/* w = u * u */
w = n_mulmod_preinv(u, u, n, n_ecm_inf->ninv, n_ecm_inf->normbits);
Expand Down Expand Up @@ -382,9 +383,14 @@ n_factor_ecm_select_curve(mp_limb_t *f, mp_limb_t sig, mp_limb_t n, n_ecm_t n_ec
*f = n_gcdinv(&u, v, n);

if (*f == n)
return 0;
{
goto cleanup;
}
else if (*f > n_ecm_inf->one)
return 1;
{
ret = 1;
goto cleanup;
}

a[1] = UWORD(0);
a[0] = u;
Expand All @@ -405,9 +411,10 @@ n_factor_ecm_select_curve(mp_limb_t *f, mp_limb_t sig, mp_limb_t n, n_ecm_t n_ec
n_ecm_inf->a24 <<= n_ecm_inf->normbits;
n_ecm_inf->z = n_ecm_inf->one;

cleanup:
TMP_END;

return 0;
return ret;
}

int
Expand Down

0 comments on commit 016de13

Please sign in to comment.