Fix fp_div_2d to return remainder correctly

If a == c are then a and c don't equal d:
calculate d before c
If a != c then a doesn't change in calculating c:
calculate d after c
This commit is contained in:
Sean Parkinson
2018-09-03 08:32:55 +10:00
parent 41ab3d91fd
commit 4d0478a287

View File

@@ -762,9 +762,8 @@ void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
return; return;
} }
/* get the remainder */ /* get the remainder before a is changed in calculating c */
if (d != NULL) { if (a == c && d != NULL) {
/* NOTE: d must not be the same pointer as a or b */
fp_mod_2d (a, b, d); fp_mod_2d (a, b, d);
} }
@@ -781,6 +780,12 @@ void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
if (D != 0) { if (D != 0) {
fp_rshb(c, D); fp_rshb(c, D);
} }
/* get the remainder if a is not changed in calculating c */
if (a != c && d != NULL) {
fp_mod_2d (a, b, d);
}
fp_clamp (c); fp_clamp (c);
} }