github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/cnd_add_n.c (about)

     1  /* mpn_cnd_add_n -- Compute R = U + V if CND != 0 or R = U if CND == 0.
     2     Both cases should take the same time and perform the exact same memory
     3     accesses, since this function is intended to be used where side-channel
     4     attack resilience is relevant.
     5  
     6  Copyright 1992-1994, 1996, 2000, 2002, 2008, 2009, 2011, 2013 Free Software
     7  Foundation, Inc.
     8  
     9  This file is part of the GNU MP Library.
    10  
    11  The GNU MP Library is free software; you can redistribute it and/or modify
    12  it under the terms of either:
    13  
    14    * the GNU Lesser General Public License as published by the Free
    15      Software Foundation; either version 3 of the License, or (at your
    16      option) any later version.
    17  
    18  or
    19  
    20    * the GNU General Public License as published by the Free Software
    21      Foundation; either version 2 of the License, or (at your option) any
    22      later version.
    23  
    24  or both in parallel, as here.
    25  
    26  The GNU MP Library is distributed in the hope that it will be useful, but
    27  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    28  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    29  for more details.
    30  
    31  You should have received copies of the GNU General Public License and the
    32  GNU Lesser General Public License along with the GNU MP Library.  If not,
    33  see https://www.gnu.org/licenses/.  */
    34  
    35  #include "gmp.h"
    36  #include "gmp-impl.h"
    37  
    38  mp_limb_t
    39  mpn_cnd_add_n (mp_limb_t cnd, mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
    40  {
    41    mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask;
    42  
    43    ASSERT (n >= 1);
    44    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
    45    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
    46  
    47    mask = -(mp_limb_t) (cnd != 0);
    48    cy = 0;
    49    do
    50      {
    51        ul = *up++;
    52        vl = *vp++ & mask;
    53  #if GMP_NAIL_BITS == 0
    54        sl = ul + vl;
    55        cy1 = sl < ul;
    56        rl = sl + cy;
    57        cy2 = rl < sl;
    58        cy = cy1 | cy2;
    59        *rp++ = rl;
    60  #else
    61        rl = ul + vl;
    62        rl += cy;
    63        cy = rl >> GMP_NUMB_BITS;
    64        *rp++ = rl & GMP_NUMB_MASK;
    65  #endif
    66      }
    67    while (--n != 0);
    68  
    69    return cy;
    70  }