1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

Fix special case when mp_modsub returns zero.

If it had to negate x-y to make it positive for mp_mod, but the answer
comes out as zero after that, then after re-negating it this is the
one case where we _shouldn't_ add the modulus afterwards. Result was
that, for example, mp_modsub(0, 0, 5) would return 5 instead of the
obvious 0.
This commit is contained in:
Simon Tatham 2019-01-03 11:53:38 +00:00
parent df1ed3ba6e
commit 425a119ae8

12
mpint.c
View File

@ -2076,11 +2076,15 @@ mp_int *mp_modsub(mp_int *x, mp_int *y, mp_int *modulus)
mp_sub_into(diff, x, y);
unsigned negate = mp_cmp_hs(y, x);
mp_cond_negate(diff, diff, negate);
mp_int *reduced = mp_mod(diff, modulus);
mp_cond_negate(reduced, reduced, negate);
mp_cond_add_into(reduced, reduced, modulus, negate);
mp_int *residue = mp_mod(diff, modulus);
mp_cond_negate(residue, residue, negate);
/* If we've just negated the residue, then it will be < 0 and need
* the modulus adding to it to make it positive - *except* if the
* residue was zero when we negated it. */
unsigned make_positive = negate & ~mp_eq_integer(residue, 0);
mp_cond_add_into(residue, residue, modulus, make_positive);
mp_free(diff);
return reduced;
return residue;
}
static mp_int *mp_modadd_in_range(mp_int *x, mp_int *y, mp_int *modulus)