ecc-eh-to-a.c 2.43 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
#include "ecc.h"
#include "ecc-internal.h"

/* Convert from homogeneous coordinates on the Edwards curve to affine
42
   coordinates. */
43 44
void
ecc_eh_to_a (const struct ecc_curve *ecc,
45
	     int op,
46 47 48 49
	     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
50
#define tp (scratch + ecc->p.size)
51 52


53
#define xp p
Niels Möller's avatar
Niels Möller committed
54 55
#define yp (p + ecc->p.size)
#define zp (p + 2*ecc->p.size)
56 57 58

  mp_limb_t cy;

59
  /* Needs 2*size + scratch for the invert call. */
60
  ecc->p.invert (&ecc->p, izp, zp, tp + ecc->p.size);
61

62
  ecc_modp_mul (ecc, tp, xp, izp);
Niels Möller's avatar
Niels Möller committed
63 64
  cy = mpn_sub_n (r, tp, ecc->p.m, ecc->p.size);
  cnd_copy (cy, r, tp, ecc->p.size);
65

66 67 68 69 70
  if (op)
    {
      /* Skip y coordinate */
      if (op > 1)
	{
Niels Möller's avatar
Niels Möller committed
71 72 73 74
	  /* Reduce modulo q. Hardcoded for curve25519, duplicates end
	     of ecc_25519_modq. FIXME: Is this needed at all? op > 0
	     is only used by ecdsa code, and ecdsa on Edwards curves
	     makes little sense and is is only used by tests. */
75
	  unsigned shift;
Niels Möller's avatar
Niels Möller committed
76
	  assert (ecc->p.bit_size == 255);
77
	  shift = ecc->q.bit_size - 1 - GMP_NUMB_BITS * (ecc->p.size - 1);
Niels Möller's avatar
Niels Möller committed
78 79
	  cy = mpn_submul_1 (r, ecc->q.m, ecc->p.size,
			     r[ecc->p.size-1] >> shift);
80
	  assert (cy < 2);
Niels Möller's avatar
Niels Möller committed
81
	  cnd_add_n (cy, r, ecc->q.m, ecc->p.size);
82 83 84
	}
      return;
    }
85
  ecc_modp_mul (ecc, tp, yp, izp);
Niels Möller's avatar
Niels Möller committed
86 87
  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);
88
}