Blame lib/nettle/ecc/sec-tabselect.c

Packit Service 991b93
/* sec-tabselect.c
Packit Service 991b93
Packit Service 991b93
   Copyright (C) 2013 Niels Möller
Packit Service 991b93
Packit Service 991b93
   This file is part of GNU Nettle.
Packit Service 991b93
Packit Service 991b93
   GNU Nettle is free software: you can redistribute it and/or
Packit Service 991b93
   modify it under the terms of either:
Packit Service 991b93
Packit Service 991b93
     * the GNU Lesser General Public License as published by the Free
Packit Service 991b93
       Software Foundation; either version 3 of the License, or (at your
Packit Service 991b93
       option) any later version.
Packit Service 991b93
Packit Service 991b93
   or
Packit Service 991b93
Packit Service 991b93
     * the GNU General Public License as published by the Free
Packit Service 991b93
       Software Foundation; either version 2 of the License, or (at your
Packit Service 991b93
       option) any later version.
Packit Service 991b93
Packit Service 991b93
   or both in parallel, as here.
Packit Service 991b93
Packit Service 991b93
   GNU Nettle is distributed in the hope that it will be useful,
Packit Service 991b93
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 991b93
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 991b93
   General Public License for more details.
Packit Service 991b93
Packit Service 991b93
   You should have received copies of the GNU General Public License and
Packit Service 991b93
   the GNU Lesser General Public License along with this program.  If
Packit Service 991b93
   not, see http://www.gnu.org/licenses/.
Packit Service 991b93
*/
Packit Service 991b93
Packit Service 991b93
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
Packit Service 991b93
Packit Service 991b93
#if HAVE_CONFIG_H
Packit Service 991b93
# include "config.h"
Packit Service 991b93
#endif
Packit Service 991b93
Packit Service 991b93
#include <assert.h>
Packit Service 991b93
Packit Service 991b93
#include "ecc-internal.h"
Packit Service 991b93
Packit Service 991b93
/* Copy the k'th element of the table out tn elements, each of size
Packit Service 991b93
   rn. Always read complete table. Similar to gmp's mpn_tabselect. */
Packit Service 991b93
/* FIXME: Should we need to volatile declare anything? */
Packit Service 991b93
void
Packit Service 991b93
sec_tabselect (mp_limb_t *rp, mp_size_t rn,
Packit Service 991b93
	       const mp_limb_t *table, unsigned tn,
Packit Service 991b93
	       unsigned k)
Packit Service 991b93
{
Packit Service 991b93
  const mp_limb_t *end = table + tn * rn;
Packit Service 991b93
  const mp_limb_t *p;
Packit Service 991b93
  mp_size_t i;
Packit Service 991b93
  
Packit Service 991b93
  assert (k < tn);
Packit Service 991b93
  mpn_zero (rp, rn);
Packit Service 991b93
  for (p = table; p < end; p += rn, k--)
Packit Service 991b93
    {
Packit Service 991b93
      mp_limb_t mask = - (mp_limb_t) (k == 0);
Packit Service 991b93
      for (i = 0; i < rn; i++)
Packit Service 991b93
	rp[i] += mask & p[i];
Packit Service 991b93
    }
Packit Service 991b93
}