ecc-eh-to-a.c 3.05 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/* 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

#include "ecc.h"
#include "ecc-internal.h"

mp_size_t
ecc_eh_to_a_itch (const struct ecc_curve *ecc)
{
  /* Needs 2*ecc->size + scratch for ecc_modq_inv */
  return ECC_EH_TO_A_ITCH (ecc->size);
}

/* Convert from homogeneous coordinates on the Edwards curve to affine
   coordinates on the corresponding Montgomery curve. */
void
ecc_eh_to_a (const struct ecc_curve *ecc,
	     int flags,
	     mp_limb_t *r, const mp_limb_t *p,
	     mp_limb_t *scratch)
{
#define izp scratch
#define sp (scratch + ecc->size)
#define tp (scratch + 2*ecc->size)

#define xp r
#define yp (r + ecc->size)
#define up p
#define vp (p + ecc->size)
#define wp (p + 2*ecc->size)
  /* x = (v+1)/(v-1), y = t x / u (with t = sqrt(b+2))

     In homogeneous coordinates,

     X = (W + V) U
     Y = t (W + V) W
     Z = (W - V) U
  */
  /* FIXME: Simplify for common case that only x-coordinate is wanted. */

  mp_limb_t cy;

Niels Möller's avatar
Niels Möller committed
75
  /* NOTE: For the infinity point, this subtraction gives zero (mod
Niels Möller's avatar
Niels Möller committed
76
     p), which isn't invertible. For curve25519, the desired output is
Niels Möller's avatar
Niels Möller committed
77 78 79
     x = 0, and we should be fine, since ecc_modp_inv returns 0
     in this case. */
  ecc_modp_sub (ecc, izp, wp, vp);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  ecc_modp_mul (ecc, izp + ecc->size, izp, up);
  /* Needs 3*size scratch */
  ecc_modp_inv (ecc, izp, izp + ecc->size, izp + 2*ecc->size);

  ecc_modp_add (ecc, sp, wp, vp);
  ecc_modp_mul (ecc, tp, sp, up);
  mpn_copyi (sp, tp, ecc->size); /* FIXME: Eliminate copy */
  ecc_modp_mul (ecc, tp, sp, izp);
  cy = mpn_sub_n (xp, tp, ecc->p, ecc->size);
  cnd_copy (cy, xp, tp, ecc->size);

  if (flags & 2)
    /* Skip y coordinate */
    return;
  
Niels Möller's avatar
Niels Möller committed
95 96 97
  ecc_modp_add (ecc, sp, wp, vp); /* FIXME: Redundant. Also the (W +
				     V) Z^-1 multiplication is
				     redundant. */
98 99 100 101 102 103 104 105
  ecc_modp_mul (ecc, tp, sp, wp);
  mpn_copyi (sp, tp, ecc->size); /* FIXME: Eliminate copy */
  ecc_modp_mul (ecc, tp, sp, ecc->edwards_root);
  mpn_copyi (sp, tp, ecc->size); /* FIXME: Eliminate copy */
  ecc_modp_mul (ecc, tp, sp, izp);
  cy = mpn_sub_n (yp, tp, ecc->p, ecc->size);
  cnd_copy (cy, yp, tp, ecc->size);
}