ecc-eh-to-a.c 3.45 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 44 45 46 47 48 49 50 51
#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,
52
	     int op,
53 54 55 56 57 58 59 60 61 62 63 64
	     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)
Niels Möller's avatar
Niels Möller committed
65
  /* x = (1+v)/(1-v), y = t x / u (with t = sqrt(b+2))
66 67 68 69 70 71 72 73 74 75 76

     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
77
  /* NOTE: For the infinity point, this subtraction gives zero (mod
Niels Möller's avatar
Niels Möller committed
78
     p), which isn't invertible. For curve25519, the desired output is
Niels Möller's avatar
Niels Möller committed
79 80 81
     x = 0, and we should be fine, since ecc_modp_inv returns 0
     in this case. */
  ecc_modp_sub (ecc, izp, wp, vp);
82 83 84 85 86 87 88 89 90 91 92
  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);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  if (op)
    {
      /* Skip y coordinate */
      if (op > 1)
	{
	  /* Reduce modulo q. FIXME: Hardcoded for curve25519,
	     duplicates end of ecc_25519_modq. */
	  mp_limb_t cy;
	  unsigned shift;
	  assert (ecc->bit_size == 255);
	  shift = 252 - GMP_NUMB_BITS * (ecc->size - 1);
	  cy = mpn_submul_1 (xp, ecc->q, ecc->size,
			     xp[ecc->size-1] >> shift);
	  assert (cy < 2);
	  cnd_add_n (cy, xp, ecc->q, ecc->size);
	}
      return;
    }
Niels Möller's avatar
Niels Möller committed
111 112 113
  ecc_modp_add (ecc, sp, wp, vp); /* FIXME: Redundant. Also the (W +
				     V) Z^-1 multiplication is
				     redundant. */
114 115 116 117 118 119 120 121
  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);
}