Blame lib/nettle/ecc/eddsa-compress.c

Packit Service 991b93
/* eddsa-compress.c
Packit Service 991b93
Packit Service 991b93
   Copyright (C) 2014 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
#if HAVE_CONFIG_H
Packit Service 991b93
# include "config.h"
Packit Service 991b93
#endif
Packit Service 991b93
Packit Service 991b93
#include "eddsa.h"
Packit Service 991b93
#include "eddsa-internal.h"
Packit Service 991b93
Packit Service 991b93
#include "ecc-internal.h"
Packit Service 991b93
#include "gmp-glue.h"
Packit Service 991b93
Packit Service 991b93
mp_size_t
Packit Service 991b93
_eddsa_compress_itch (const struct ecc_curve *ecc)
Packit Service 991b93
{
Packit Service 991b93
  return 2*ecc->p.size + ecc->h_to_a_itch;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
void
Packit Service 991b93
_eddsa_compress (const struct ecc_curve *ecc, uint8_t *r, mp_limb_t *p,
Packit Service 991b93
		 mp_limb_t *scratch)
Packit Service 991b93
{
Packit Service 991b93
#define xp scratch
Packit Service 991b93
#define yp (scratch + ecc->p.size)
Packit Service 991b93
#define scratch_out (scratch + 2*ecc->p.size)
Packit Service 991b93
Packit Service 991b93
  size_t nbytes = 1 + ecc->p.bit_size / 8;
Packit Service 991b93
  ecc->h_to_a (ecc, 0, xp, p, scratch_out);
Packit Service 991b93
  /* Encoding is the y coordinate and an appended "sign" bit, which is
Packit Service 991b93
     the low bit of x. The sign bit is stored as the most significant
Packit Service 991b93
     bit of the last byte. */
Packit Service 991b93
  mpn_get_base256_le (r, nbytes, yp, ecc->p.size);
Packit Service 991b93
  r[nbytes - 1] += (xp[0] & 1) << 7;
Packit Service 991b93
}