ecc-j-to-a.c 3.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
/* ecc-j-to-a.c

   Copyright (C) 2013 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/.
*/
Niels Möller's avatar
Niels Möller committed
31

32
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
Niels Möller's avatar
Niels Möller committed
33 34 35 36 37 38 39 40 41 42 43 44

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

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

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

void
ecc_j_to_a (const struct ecc_curve *ecc,
50
	    int op,
Niels Möller's avatar
Niels Möller committed
51 52 53 54
	    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
55 56 57 58
#define up   (scratch + ecc->p.size)
#define iz2p (scratch + ecc->p.size)
#define iz3p (scratch + 2*ecc->p.size)
#define izBp (scratch + 3*ecc->p.size)
Niels Möller's avatar
Niels Möller committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define tp    scratch

  mp_limb_t cy;

  if (ecc->use_redc)
    {
      /* Set v = (r_z / B^2)^-1,

	 r_x = p_x v^2 / B^3 =  ((v/B * v)/B * p_x)/B
	 r_y = p_y v^3 / B^4 = (((v/B * v)/B * v)/B * p_x)/B

	 Skip the first redc, if we want to stay in Montgomery
	 representation.
      */

Niels Möller's avatar
Niels Möller committed
74 75
      mpn_copyi (up, p + 2*ecc->p.size, ecc->p.size);
      mpn_zero (up + ecc->p.size, ecc->p.size);
76
      ecc->p.reduce (&ecc->p, up);
Niels Möller's avatar
Niels Möller committed
77
      mpn_zero (up + ecc->p.size, ecc->p.size);
78
      ecc->p.reduce (&ecc->p, up);
Niels Möller's avatar
Niels Möller committed
79

Niels Möller's avatar
Niels Möller committed
80
      ecc_modp_inv (ecc, izp, up, up + ecc->p.size);
Niels Möller's avatar
Niels Möller committed
81

82
      /* Divide this common factor by B */
Niels Möller's avatar
Niels Möller committed
83 84
      mpn_copyi (izBp, izp, ecc->p.size);
      mpn_zero (izBp + ecc->p.size, ecc->p.size);
85
      ecc->p.reduce (&ecc->p, izBp);
86

87
      ecc_modp_mul (ecc, iz2p, izp, izBp);
Niels Möller's avatar
Niels Möller committed
88 89 90 91 92
    }
  else
    {
      /* Set s = p_z^{-1}, r_x = p_x s^2, r_y = p_y s^3 */

Niels Möller's avatar
Niels Möller committed
93 94
      mpn_copyi (up, p+2*ecc->p.size, ecc->p.size); /* p_z */
      ecc_modp_inv (ecc, izp, up, up + ecc->p.size);
Niels Möller's avatar
Niels Möller committed
95 96 97 98 99 100 101

      ecc_modp_sqr (ecc, iz2p, izp);
    }

  ecc_modp_mul (ecc, iz3p, iz2p, p);
  /* ecc_modp (and ecc_modp_mul) may return a value up to 2p - 1, so
     do a conditional subtraction. */
Niels Möller's avatar
Niels Möller committed
102 103
  cy = mpn_sub_n (r, iz3p, ecc->p.m, ecc->p.size);
  cnd_copy (cy, r, iz3p, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
104

105 106 107 108 109 110 111 112
  if (op)
    {
      /* Skip y coordinate */
      if (op > 1)
	{
	  /* Also reduce the x coordinate mod ecc->q. It should
	     already be < 2*ecc->q, so one subtraction should
	     suffice. */
Niels Möller's avatar
Niels Möller committed
113 114
	  cy = mpn_sub_n (scratch, r, ecc->q.m, ecc->p.size);
	  cnd_copy (cy == 0, r, scratch, ecc->p.size);
115 116 117
	}
      return;
    }
Niels Möller's avatar
Niels Möller committed
118
  ecc_modp_mul (ecc, iz3p, iz2p, izp);
Niels Möller's avatar
Niels Möller committed
119
  ecc_modp_mul (ecc, tp, iz3p, p + ecc->p.size);
Niels Möller's avatar
Niels Möller committed
120
  /* And a similar subtraction. */
Niels Möller's avatar
Niels Möller committed
121 122
  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);
Niels Möller's avatar
Niels Möller committed
123 124 125 126 127 128 129

#undef izp
#undef up
#undef iz2p
#undef iz3p
#undef tp
}