18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* mpihelp-add_2.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_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 218c2ecf20Sopenharmony_ci mpi_ptr_t s2_ptr, mpi_size_t size) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci mpi_limb_t x, y, cy; 248c2ecf20Sopenharmony_ci mpi_size_t j; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci /* The loop counter and index J goes from -SIZE to -1. This way 278c2ecf20Sopenharmony_ci the loop becomes faster. */ 288c2ecf20Sopenharmony_ci j = -size; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci /* Offset the base pointers to compensate for the negative indices. */ 318c2ecf20Sopenharmony_ci s1_ptr -= j; 328c2ecf20Sopenharmony_ci s2_ptr -= j; 338c2ecf20Sopenharmony_ci res_ptr -= j; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci cy = 0; 368c2ecf20Sopenharmony_ci do { 378c2ecf20Sopenharmony_ci y = s2_ptr[j]; 388c2ecf20Sopenharmony_ci x = s1_ptr[j]; 398c2ecf20Sopenharmony_ci y += cy; /* add previous carry to subtrahend */ 408c2ecf20Sopenharmony_ci cy = y < cy; /* get out carry from that addition */ 418c2ecf20Sopenharmony_ci y = x - y; /* main subtract */ 428c2ecf20Sopenharmony_ci cy += y > x; /* get out carry from the subtract, combine */ 438c2ecf20Sopenharmony_ci res_ptr[j] = y; 448c2ecf20Sopenharmony_ci } while (++j); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci return cy; 478c2ecf20Sopenharmony_ci} 48