(root)/
gmp-6.3.0/
mpn/
generic/
cnd_sub_n.c
       1  /* mpn_cnd_sub_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-impl.h"
      36  
      37  mp_limb_t
      38  mpn_cnd_sub_n (mp_limb_t cnd, mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
      39  {
      40    mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask;
      41  
      42    ASSERT (n >= 1);
      43    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
      44    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
      45  
      46    mask = -(mp_limb_t) (cnd != 0);
      47    cy = 0;
      48    do
      49      {
      50        ul = *up++;
      51        vl = *vp++ & mask;
      52  #if GMP_NAIL_BITS == 0
      53        sl = ul - vl;
      54        cy1 = sl > ul;
      55        rl = sl - cy;
      56        cy2 = rl > sl;
      57        cy = cy1 | cy2;
      58        *rp++ = rl;
      59  #else
      60        rl = ul - vl;
      61        rl -= cy;
      62        cy = rl >> (GMP_LIMB_BITS - 1);
      63        *rp++ = rl & GMP_NUMB_MASK;
      64  #endif
      65      }
      66    while (--n != 0);
      67  
      68    return cy;
      69  }