Blame lib/nettle/ecc/gmp-glue.h

Packit Service 991b93
/* gmp-glue.h
Packit Service 991b93
Packit Service 991b93
   Copyright (C) 2013 Niels Möller
Packit Service 991b93
   Copyright (C) 2013 Red Hat
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
#ifndef GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED
Packit Service 991b93
#define GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED
Packit Service 991b93
Packit Service 991b93
#include <nettle/bignum.h>
Packit Service 991b93
Packit Service 991b93
#define cnd_swap _gnutls_nettle_ecc_cnd_swap
Packit Service 991b93
#define mpz_limbs_cmp _gnutls_nettle_ecc_mpz_limbs_cmp
Packit Service 991b93
#define mpz_limbs_read_n _gnutls_nettle_ecc_mpz_limbs_read_n
Packit Service 991b93
#define mpz_limbs_copy _gnutls_nettle_ecc_mpz_limbs_copy
Packit Service 991b93
#define mpz_set_n _gnutls_nettle_ecc_mpz_set_n
Packit Service 991b93
#define mpn_set_base256 _gnutls_nettle_ecc_mpn_set_base256
Packit Service 991b93
#define mpn_set_base256_le _gnutls_nettle_ecc_mpn_set_base256_le
Packit Service 991b93
#define mpn_get_base256 _gnutls_nettle_ecc_mpn_get_base256
Packit Service 991b93
#define mpn_get_base256_le _gnutls_nettle_ecc_mpn_get_base256_le
Packit Service 991b93
#define gmp_alloc_limbs _gnutls_nettle_ecc_gmp_alloc_limbs
Packit Service 991b93
#define gmp_free_limbs _gnutls_nettle_ecc_gmp_free_limbs
Packit Service 991b93
#define gmp_free _gnutls_nettle_ecc_gmp_free
Packit Service 991b93
#define gmp_alloc _gnutls_nettle_ecc_gmp_alloc
Packit Service 991b93
Packit Service 991b93
#define TMP_GMP_DECL(name, type) type *name;	\
Packit Service 991b93
  size_t tmp_##name##_size
Packit Service 991b93
#define TMP_GMP_ALLOC(name, size) do {					\
Packit Service 991b93
    tmp_##name##_size = (size);						\
Packit Service 991b93
    (name) = gmp_alloc(sizeof (*name) * (size));	\
Packit Service 991b93
  } while (0)
Packit Service 991b93
#define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
Packit Service 991b93
Packit Service 991b93
Packit Service 991b93
/* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
Packit Service 991b93
#ifdef mpn_cnd_add_n
Packit Service 991b93
# define cnd_add_n(cnd, rp, ap, n) mpn_cnd_add_n ((cnd), (rp), (rp), (ap), (n))
Packit Service 991b93
# define cnd_sub_n(cnd, rp, ap, n) mpn_cnd_sub_n ((cnd), (rp), (rp), (ap), (n))
Packit Service 991b93
#else
Packit Service 991b93
# define cnd_add_n(cnd, rp, ap, n) mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
Packit Service 991b93
# define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
Packit Service 991b93
#endif
Packit Service 991b93
Packit Service 991b93
#define NETTLE_OCTET_SIZE_TO_LIMB_SIZE(n) \
Packit Service 991b93
  (((n) * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
cnd_swap (mp_limb_t cnd, mp_limb_t *ap, mp_limb_t *bp, mp_size_t n);
Packit Service 991b93
Packit Service 991b93
/* Convenience functions */
Packit Service 991b93
int
Packit Service 991b93
mpz_limbs_cmp (mpz_srcptr a, const mp_limb_t *bp, mp_size_t bn);
Packit Service 991b93
Packit Service 991b93
/* Get a pointer to an n limb area, for read-only operation. n must be
Packit Service 991b93
   greater or equal to the current size, and the mpz is zero-padded if
Packit Service 991b93
   needed. */
Packit Service 991b93
const mp_limb_t *
Packit Service 991b93
mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
Packit Service 991b93
Packit Service 991b93
/* Copy limbs, with zero-padding. */
Packit Service 991b93
/* FIXME: Reorder arguments, on the theory that the first argument of
Packit Service 991b93
   an _mpz_* function should be an mpz_t? Or rename to _mpz_get_limbs,
Packit Service 991b93
   with argument order consistent with mpz_get_*. */
Packit Service 991b93
void
Packit Service 991b93
mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
mpz_set_n (mpz_t r, const mp_limb_t *xp, mp_size_t xn);
Packit Service 991b93
Packit Service 991b93
/* Like mpn_set_str, but always writes rn limbs. If input is larger,
Packit Service 991b93
   higher bits are ignored. */
Packit Service 991b93
void
Packit Service 991b93
mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
Packit Service 991b93
		 const uint8_t *xp, size_t xn);
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
Packit Service 991b93
		    const uint8_t *xp, size_t xn);
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
mpn_get_base256 (uint8_t *rp, size_t rn,
Packit Service 991b93
	         const mp_limb_t *xp, mp_size_t xn);
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
mpn_get_base256_le (uint8_t *rp, size_t rn,
Packit Service 991b93
		    const mp_limb_t *xp, mp_size_t xn);
Packit Service 991b93
Packit Service 991b93
Packit Service 991b93
mp_limb_t *
Packit Service 991b93
gmp_alloc_limbs (mp_size_t n);
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
gmp_free_limbs (mp_limb_t *p, mp_size_t n);
Packit Service 991b93
Packit Service 991b93
void *gmp_alloc(size_t n);
Packit Service 991b93
void gmp_free(void *p, size_t n);
Packit Service 991b93
Packit Service 991b93
#endif /* GNUTLS_LIB_NETTLE_ECC_NETTLE_GMP_GLUE_H_INCLUDED */