in AWSCognitoIdentityProvider/Internal/JKBigInteger/LibTomMath/tommath.c [3531:3683]
int aws_mp_invmod_slow(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
{
aws_mp_int x, y, u, v, A, B, C, D;
int res;
/* b cannot be negative */
if (b->sign == AWS_MP_NEG || aws_mp_iszero(b) == 1) {
return AWS_MP_VAL;
}
/* init temps */
if ((res = aws_mp_init_multi(&x, &y, &u, &v,
&A, &B, &C, &D, NULL)) != AWS_MP_OKAY) {
return res;
}
/* x = a, y = b */
if ((res = aws_mp_mod(a, b, &x)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_copy(b, &y)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
/* 2. [modified] if x,y are both even then return an error! */
if (aws_mp_iseven (&x) == 1 && aws_mp_iseven (&y) == 1) {
res = AWS_MP_VAL;
goto LBL_ERR;
}
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = aws_mp_copy(&x, &u)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_copy(&y, &v)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
aws_mp_set(&A, 1);
aws_mp_set(&D, 1);
top:
/* 4. while u is even do */
while (aws_mp_iseven (&u) == 1) {
/* 4.1 u = u/2 */
if ((res = aws_mp_div_2(&u, &u)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
/* 4.2 if A or B is odd then */
if (aws_mp_isodd (&A) == 1 || aws_mp_isodd (&B) == 1) {
/* A = (A+y)/2, B = (B-x)/2 */
if ((res = aws_mp_add(&A, &y, &A)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&B, &x, &B)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* A = A/2, B = B/2 */
if ((res = aws_mp_div_2(&A, &A)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_div_2(&B, &B)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* 5. while v is even do */
while (aws_mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
if ((res = aws_mp_div_2(&v, &v)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
/* 5.2 if C or D is odd then */
if (aws_mp_isodd (&C) == 1 || aws_mp_isodd (&D) == 1) {
/* C = (C+y)/2, D = (D-x)/2 */
if ((res = aws_mp_add(&C, &y, &C)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&D, &x, &D)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* C = C/2, D = D/2 */
if ((res = aws_mp_div_2(&C, &C)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_div_2(&D, &D)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* 6. if u >= v then */
if (aws_mp_cmp(&u, &v) != AWS_MP_LT) {
/* u = u - v, A = A - C, B = B - D */
if ((res = aws_mp_sub(&u, &v, &u)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&A, &C, &A)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&B, &D, &B)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
} else {
/* v - v - u, C = C - A, D = D - B */
if ((res = aws_mp_sub(&v, &u, &v)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&C, &A, &C)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
if ((res = aws_mp_sub(&D, &B, &D)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* if not zero goto step 4 */
if (aws_mp_iszero (&u) == 0)
goto top;
/* now a = C, b = D, gcd == g*v */
/* if v != 1 then there is no inverse */
if (aws_mp_cmp_d(&v, 1) != AWS_MP_EQ) {
res = AWS_MP_VAL;
goto LBL_ERR;
}
/* if its too low */
while (aws_mp_cmp_d(&C, 0) == AWS_MP_LT) {
if ((res = aws_mp_add(&C, b, &C)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* too big */
while (aws_mp_cmp_mag(&C, b) != AWS_MP_LT) {
if ((res = aws_mp_sub(&C, b, &C)) != AWS_MP_OKAY) {
goto LBL_ERR;
}
}
/* C is now the inverse */
aws_mp_exch(&C, c);
res = AWS_MP_OKAY;
LBL_ERR:
aws_mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
return res;
}