int aws_mp_karatsuba_mul()

in AWSCognitoIdentityProvider/Internal/JKBigInteger/LibTomMath/tommath.c [1689:1811]


int aws_mp_karatsuba_mul(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
{
  aws_mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
  int     B, err;

  /* default the return code to an error */
  err = AWS_MP_MEM;

  /* min # of digits */
  B = AWS_MIN (a->used, b->used);

  /* now divide in two */
  B = B >> 1;

  /* init copy all the temps */
  if (aws_mp_init_size(&x0, B) != AWS_MP_OKAY)
    goto ERR;
  if (aws_mp_init_size(&x1, a->used - B) != AWS_MP_OKAY)
    goto X0;
  if (aws_mp_init_size(&y0, B) != AWS_MP_OKAY)
    goto X1;
  if (aws_mp_init_size(&y1, b->used - B) != AWS_MP_OKAY)
    goto Y0;

  /* init temps */
  if (aws_mp_init_size(&t1, B * 2) != AWS_MP_OKAY)
    goto Y1;
  if (aws_mp_init_size(&x0y0, B * 2) != AWS_MP_OKAY)
    goto T1;
  if (aws_mp_init_size(&x1y1, B * 2) != AWS_MP_OKAY)
    goto X0Y0;

  /* now shift the digits */
  x0.used = y0.used = B;
  x1.used = a->used - B;
  y1.used = b->used - B;

  {
    register int x;
    register aws_mp_digit *tmpa, *tmpb, *tmpx, *tmpy;

    /* we copy the digits directly instead of using higher level functions
     * since we also need to shift the digits
     */
    tmpa = a->dp;
    tmpb = b->dp;

    tmpx = x0.dp;
    tmpy = y0.dp;
    for (x = 0; x < B; x++) {
      *tmpx++ = *tmpa++;
      *tmpy++ = *tmpb++;
    }

    tmpx = x1.dp;
    for (x = B; x < a->used; x++) {
      *tmpx++ = *tmpa++;
    }

    tmpy = y1.dp;
    for (x = B; x < b->used; x++) {
      *tmpy++ = *tmpb++;
    }
  }

  /* only need to clamp the lower words since by definition the 
   * upper words x1/y1 must have a known number of digits
   */
    aws_mp_clamp(&x0);
    aws_mp_clamp(&y0);

  /* now calc the products x0y0 and x1y1 */
  /* after this x0 is no longer required, free temp [x0==t2]! */
  if (aws_mp_mul(&x0, &y0, &x0y0) != AWS_MP_OKAY)
    goto X1Y1;          /* x0y0 = x0*y0 */
  if (aws_mp_mul(&x1, &y1, &x1y1) != AWS_MP_OKAY)
    goto X1Y1;          /* x1y1 = x1*y1 */

  /* now calc x1+x0 and y1+y0 */
  if (aws_s_mp_add(&x1, &x0, &t1) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = x1 - x0 */
  if (aws_s_mp_add(&y1, &y0, &x0) != AWS_MP_OKAY)
    goto X1Y1;          /* t2 = y1 - y0 */
  if (aws_mp_mul(&t1, &x0, &t1) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = (x1 + x0) * (y1 + y0) */

  /* add x0y0 */
  if (aws_mp_add(&x0y0, &x1y1, &x0) != AWS_MP_OKAY)
    goto X1Y1;          /* t2 = x0y0 + x1y1 */
  if (aws_s_mp_sub(&t1, &x0, &t1) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */

  /* shift by B */
  if (aws_mp_lshd(&t1, B) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  if (aws_mp_lshd(&x1y1, B * 2) != AWS_MP_OKAY)
    goto X1Y1;          /* x1y1 = x1y1 << 2*B */

  if (aws_mp_add(&x0y0, &t1, &t1) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = x0y0 + t1 */
  if (aws_mp_add(&t1, &x1y1, c) != AWS_MP_OKAY)
    goto X1Y1;          /* t1 = x0y0 + t1 + x1y1 */

  /* Algorithm succeeded set the return code to AWS_MP_OKAY */
  err = AWS_MP_OKAY;

X1Y1:
aws_mp_clear(&x1y1);
X0Y0:
aws_mp_clear(&x0y0);
T1:
aws_mp_clear(&t1);
Y1:
aws_mp_clear(&y1);
Y0:
aws_mp_clear(&y0);
X1:
aws_mp_clear(&x1);
X0:
aws_mp_clear(&x0);
ERR:
  return err;
}