162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/* mpihelp-mul_1.c  -  MPI helper functions
362306a36Sopenharmony_ci * Copyright (C) 1994, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This file is part of GnuPG.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Note: This code is heavily based on the GNU MP Library.
862306a36Sopenharmony_ci *	 Actually it's the same code with only minor changes in the
962306a36Sopenharmony_ci *	 way the data is stored; this is to support the abstraction
1062306a36Sopenharmony_ci *	 of an optional secure memory allocation which may be used
1162306a36Sopenharmony_ci *	 to avoid revealing of sensitive data due to paging etc.
1262306a36Sopenharmony_ci *	 The GNU MP Library itself is published under the LGPL;
1362306a36Sopenharmony_ci *	 however I decided to publish this code under the plain GPL.
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "mpi-internal.h"
1762306a36Sopenharmony_ci#include "longlong.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cimpi_limb_t
2062306a36Sopenharmony_cimpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
2162306a36Sopenharmony_ci	      mpi_limb_t s2_limb)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	mpi_limb_t cy_limb;
2462306a36Sopenharmony_ci	mpi_size_t j;
2562306a36Sopenharmony_ci	mpi_limb_t prod_high, prod_low;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	/* The loop counter and index J goes from -S1_SIZE to -1.  This way
2862306a36Sopenharmony_ci	 * the loop becomes faster.  */
2962306a36Sopenharmony_ci	j = -s1_size;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	/* Offset the base pointers to compensate for the negative indices.  */
3262306a36Sopenharmony_ci	s1_ptr -= j;
3362306a36Sopenharmony_ci	res_ptr -= j;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	cy_limb = 0;
3662306a36Sopenharmony_ci	do {
3762306a36Sopenharmony_ci		umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
3862306a36Sopenharmony_ci		prod_low += cy_limb;
3962306a36Sopenharmony_ci		cy_limb = (prod_low < cy_limb ? 1 : 0) + prod_high;
4062306a36Sopenharmony_ci		res_ptr[j] = prod_low;
4162306a36Sopenharmony_ci	} while (++j);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	return cy_limb;
4462306a36Sopenharmony_ci}
45