int aws_mp_exteuclid()

in AWSCognitoIdentityProvider/Internal/JKBigInteger/LibTomMath/tommath.c [2277:2334]


int aws_mp_exteuclid(aws_mp_int *a, aws_mp_int *b, aws_mp_int *U1, aws_mp_int *U2, aws_mp_int *U3)
{
   aws_mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp;
   int err;

   if ((err = aws_mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != AWS_MP_OKAY) {
      return err;
   }

   /* initialize, (u1,u2,u3) = (1,0,a) */
    aws_mp_set(&u1, 1);
   if ((err = aws_mp_copy(a, &u3)) != AWS_MP_OKAY)                                        { goto _ERR; }

   /* initialize, (v1,v2,v3) = (0,1,b) */
    aws_mp_set(&v2, 1);
   if ((err = aws_mp_copy(b, &v3)) != AWS_MP_OKAY)                                        { goto _ERR; }

   /* loop while v3 != 0 */
   while (aws_mp_iszero(&v3) == AWS_MP_NO) {
       /* q = u3/v3 */
       if ((err = aws_mp_div(&u3, &v3, &q, NULL)) != AWS_MP_OKAY)                         { goto _ERR; }

       /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
       if ((err = aws_mp_mul(&v1, &q, &tmp)) != AWS_MP_OKAY)                              { goto _ERR; }
       if ((err = aws_mp_sub(&u1, &tmp, &t1)) != AWS_MP_OKAY)                             { goto _ERR; }
       if ((err = aws_mp_mul(&v2, &q, &tmp)) != AWS_MP_OKAY)                              { goto _ERR; }
       if ((err = aws_mp_sub(&u2, &tmp, &t2)) != AWS_MP_OKAY)                             { goto _ERR; }
       if ((err = aws_mp_mul(&v3, &q, &tmp)) != AWS_MP_OKAY)                              { goto _ERR; }
       if ((err = aws_mp_sub(&u3, &tmp, &t3)) != AWS_MP_OKAY)                             { goto _ERR; }

       /* (u1,u2,u3) = (v1,v2,v3) */
       if ((err = aws_mp_copy(&v1, &u1)) != AWS_MP_OKAY)                                  { goto _ERR; }
       if ((err = aws_mp_copy(&v2, &u2)) != AWS_MP_OKAY)                                  { goto _ERR; }
       if ((err = aws_mp_copy(&v3, &u3)) != AWS_MP_OKAY)                                  { goto _ERR; }

       /* (v1,v2,v3) = (t1,t2,t3) */
       if ((err = aws_mp_copy(&t1, &v1)) != AWS_MP_OKAY)                                  { goto _ERR; }
       if ((err = aws_mp_copy(&t2, &v2)) != AWS_MP_OKAY)                                  { goto _ERR; }
       if ((err = aws_mp_copy(&t3, &v3)) != AWS_MP_OKAY)                                  { goto _ERR; }
   }

   /* make sure U3 >= 0 */
   if (u3.sign == AWS_MP_NEG) {
       aws_mp_neg(&u1, &u1);
       aws_mp_neg(&u2, &u2);
       aws_mp_neg(&u3, &u3);
   }

   /* copy result out */
   if (U1 != NULL) {aws_mp_exch(U1, &u1); }
   if (U2 != NULL) {aws_mp_exch(U2, &u2); }
   if (U3 != NULL) {aws_mp_exch(U3, &u3); }

   err = AWS_MP_OKAY;
_ERR:
aws_mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
   return err;
}