Merge pull request #3814 from SparkiDev/gcd_lcm_zero

Math: GCD(0,0) is undefined and LCM(0,*) is undefined.
This commit is contained in:
toddouska
2021-02-25 16:00:04 -08:00
committed by GitHub
3 changed files with 25 additions and 0 deletions

View File

@ -5039,6 +5039,11 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
int res;
mp_int t1, t2;
/* LCM of 0 and any number is undefined as 0 is not in the set of values
* being used. */
if (mp_iszero (a) == MP_YES || mp_iszero (b) == MP_YES) {
return MP_VAL;
}
if ((res = mp_init_multi (&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
return res;
@ -5083,6 +5088,10 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
/* either zero than gcd is the largest */
if (mp_iszero (a) == MP_YES) {
/* GCD of 0 and 0 is undefined as all integers divide 0. */
if (mp_iszero (b) == MP_YES) {
return MP_VAL;
}
return mp_abs (b, c);
}
if (mp_iszero (b) == MP_YES) {

View File

@ -13250,6 +13250,7 @@ int sp_gcd(sp_int* a, sp_int* b, sp_int* r)
err = MP_VAL;
}
else if (sp_iszero(a)) {
/* GCD of 0 and 0 is undefined as all integers divide 0. */
if (sp_iszero(b)) {
err = MP_VAL;
}
@ -13360,6 +13361,10 @@ int sp_lcm(sp_int* a, sp_int* b, sp_int* r)
if ((a == NULL) || (b == NULL) || (r == NULL)) {
err = MP_VAL;
}
/* LCM of 0 and any number is undefined as 0 is not in the set of values
* being used.
*/
if ((err == MP_OKAY) && (mp_iszero(a) || mp_iszero(b))) {
err = MP_VAL;
}

View File

@ -5210,6 +5210,12 @@ int fp_lcm(fp_int *a, fp_int *b, fp_int *c)
fp_int *t;
#endif
/* LCM of 0 and any number is undefined as 0 is not in the set of values
* being used. */
if (fp_iszero(a) == FP_YES || fp_iszero(b) == FP_YES) {
return FP_VAL;
}
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL) {
@ -5249,6 +5255,11 @@ int fp_gcd(fp_int *a, fp_int *b, fp_int *c)
fp_int *u, *v, *r;
#endif
/* GCD of 0 and 0 is undefined as all integers divide 0. */
if (fp_iszero(a) == FP_YES && fp_iszero(b) == FP_YES) {
return FP_VAL;
}
/* either zero than gcd is the largest */
if (fp_iszero (a) == FP_YES && fp_iszero (b) == FP_NO) {
fp_abs (b, c);