18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* mpihelp-mul_1.c  -  MPI helper functions
38c2ecf20Sopenharmony_ci * Copyright (C) 1994, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file is part of GnuPG.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Note: This code is heavily based on the GNU MP Library.
88c2ecf20Sopenharmony_ci *	 Actually it's the same code with only minor changes in the
98c2ecf20Sopenharmony_ci *	 way the data is stored; this is to support the abstraction
108c2ecf20Sopenharmony_ci *	 of an optional secure memory allocation which may be used
118c2ecf20Sopenharmony_ci *	 to avoid revealing of sensitive data due to paging etc.
128c2ecf20Sopenharmony_ci *	 The GNU MP Library itself is published under the LGPL;
138c2ecf20Sopenharmony_ci *	 however I decided to publish this code under the plain GPL.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "mpi-internal.h"
178c2ecf20Sopenharmony_ci#include "longlong.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cimpi_limb_t
208c2ecf20Sopenharmony_cimpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
218c2ecf20Sopenharmony_ci	      mpi_limb_t s2_limb)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	mpi_limb_t cy_limb;
248c2ecf20Sopenharmony_ci	mpi_size_t j;
258c2ecf20Sopenharmony_ci	mpi_limb_t prod_high, prod_low;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	/* The loop counter and index J goes from -S1_SIZE to -1.  This way
288c2ecf20Sopenharmony_ci	 * the loop becomes faster.  */
298c2ecf20Sopenharmony_ci	j = -s1_size;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	/* Offset the base pointers to compensate for the negative indices.  */
328c2ecf20Sopenharmony_ci	s1_ptr -= j;
338c2ecf20Sopenharmony_ci	res_ptr -= j;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	cy_limb = 0;
368c2ecf20Sopenharmony_ci	do {
378c2ecf20Sopenharmony_ci		umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
388c2ecf20Sopenharmony_ci		prod_low += cy_limb;
398c2ecf20Sopenharmony_ci		cy_limb = (prod_low < cy_limb ? 1 : 0) + prod_high;
408c2ecf20Sopenharmony_ci		res_ptr[j] = prod_low;
418c2ecf20Sopenharmony_ci	} while (++j);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return cy_limb;
448c2ecf20Sopenharmony_ci}
45