ecc-eh-to-a.c 2.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* ecc-eh-to-a.c

   Copyright (C) 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

36 37
#include <assert.h>

38 39 40 41 42 43
#include "ecc.h"
#include "ecc-internal.h"

mp_size_t
ecc_eh_to_a_itch (const struct ecc_curve *ecc)
{
Niels Möller's avatar
Niels Möller committed
44 45
  /* Needs ecc->p.size + scratch for ecc_modq_inv */
  return ECC_EH_TO_A_ITCH (ecc->p.size);
46 47 48
}

/* Convert from homogeneous coordinates on the Edwards curve to affine
49
   coordinates. */
50 51
void
ecc_eh_to_a (const struct ecc_curve *ecc,
52
	     int op,
53 54 55 56
	     mp_limb_t *r, const mp_limb_t *p,
	     mp_limb_t *scratch)
{
#define izp scratch
Niels Möller's avatar
Niels Möller committed
57
#define tp (scratch + ecc->p.size)
58 59


60
#define xp p
Niels Möller's avatar
Niels Möller committed
61 62
#define yp (p + ecc->p.size)
#define zp (p + 2*ecc->p.size)
63 64 65

  mp_limb_t cy;

Niels Möller's avatar
Niels Möller committed
66
  mpn_copyi (tp, zp, ecc->p.size);
67
  /* Needs 3*size scratch */
Niels Möller's avatar
Niels Möller committed
68
  ecc_modp_inv (ecc, izp, tp, tp + ecc->p.size);
69

70
  ecc_modp_mul (ecc, tp, xp, izp);
Niels Möller's avatar
Niels Möller committed
71 72
  cy = mpn_sub_n (r, tp, ecc->p.m, ecc->p.size);
  cnd_copy (cy, r, tp, ecc->p.size);
73

74 75 76 77 78 79
  if (op)
    {
      /* Skip y coordinate */
      if (op > 1)
	{
	  /* Reduce modulo q. FIXME: Hardcoded for curve25519,
80 81
	     duplicates end of ecc_25519_modq. FIXME: Is this needed
	     at all? Full reduction mod p is maybe sufficient. */
82 83
	  mp_limb_t cy;
	  unsigned shift;
Niels Möller's avatar
Niels Möller committed
84 85 86 87
	  assert (ecc->p.bit_size == 255);
	  shift = 252 - GMP_NUMB_BITS * (ecc->p.size - 1);
	  cy = mpn_submul_1 (r, ecc->q.m, ecc->p.size,
			     r[ecc->p.size-1] >> shift);
88
	  assert (cy < 2);
Niels Möller's avatar
Niels Möller committed
89
	  cnd_add_n (cy, r, ecc->q.m, ecc->p.size);
90 91 92
	}
      return;
    }
93
  ecc_modp_mul (ecc, tp, yp, izp);
Niels Möller's avatar
Niels Möller committed
94 95
  cy = mpn_sub_n (r + ecc->p.size, tp, ecc->p.m, ecc->p.size);
  cnd_copy (cy, r + ecc->p.size, tp, ecc->p.size);
96
}