~ubuntu-branches/ubuntu/utopic/nettle/utopic

« back to all changes in this revision

Viewing changes to ecc-j-to-a.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-05-07 22:57:14 UTC
  • mfrom: (8.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130507225714-s331yr8ov53dtt17
Tags: 2.7-2
Tag some (ECC related) symbols that only exist on some architectures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ecc-j-to-a.c */
 
2
 
 
3
/* nettle, low-level cryptographics library
 
4
 *
 
5
 * Copyright (C) 2013 Niels Möller
 
6
 *  
 
7
 * The nettle library is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU Lesser General Public License as published by
 
9
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
 * option) any later version.
 
11
 * 
 
12
 * The nettle library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
 * License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public License
 
18
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
19
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
 * MA 02111-1301, USA.
 
21
 */
 
22
 
 
23
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
 
24
 
 
25
#if HAVE_CONFIG_H
 
26
# include "config.h"
 
27
#endif
 
28
 
 
29
#include "ecc.h"
 
30
#include "ecc-internal.h"
 
31
 
 
32
mp_size_t
 
33
ecc_j_to_a_itch (const struct ecc_curve *ecc)
 
34
{
 
35
  /* Needs 2*ecc->size + scratch for ecc_modq_inv */
 
36
  return ECC_J_TO_A_ITCH (ecc->size);
 
37
}
 
38
 
 
39
void
 
40
ecc_j_to_a (const struct ecc_curve *ecc,
 
41
            int flags,
 
42
            mp_limb_t *r, const mp_limb_t *p,
 
43
            mp_limb_t *scratch)
 
44
{
 
45
#define izp   scratch
 
46
#define up   (scratch + ecc->size)
 
47
#define iz2p (scratch + ecc->size)
 
48
#define iz3p (scratch + 2*ecc->size)
 
49
#define tp    scratch
 
50
 
 
51
  mp_limb_t cy;
 
52
 
 
53
  if (ecc->use_redc)
 
54
    {
 
55
      /* Set v = (r_z / B^2)^-1,
 
56
 
 
57
         r_x = p_x v^2 / B^3 =  ((v/B * v)/B * p_x)/B
 
58
         r_y = p_y v^3 / B^4 = (((v/B * v)/B * v)/B * p_x)/B
 
59
 
 
60
         Skip the first redc, if we want to stay in Montgomery
 
61
         representation.
 
62
      */
 
63
 
 
64
      mpn_copyi (up, p + 2*ecc->size, ecc->size);
 
65
      mpn_zero (up + ecc->size, ecc->size);
 
66
      ecc->redc (ecc, up);
 
67
      mpn_zero (up + ecc->size, ecc->size);
 
68
      ecc->redc (ecc, up);
 
69
 
 
70
      ecc_modp_inv (ecc, izp, up, up + ecc->size);
 
71
 
 
72
      if (flags & 1)
 
73
        {
 
74
          /* Divide this common factor by B */
 
75
          mpn_copyi (iz3p, izp, ecc->size);
 
76
          mpn_zero (iz3p + ecc->size, ecc->size);
 
77
          ecc->redc (ecc, iz3p);
 
78
      
 
79
          ecc_modp_mul (ecc, iz2p, izp, iz3p);
 
80
        }
 
81
      else
 
82
        ecc_modp_sqr (ecc, iz2p, izp);  
 
83
    }
 
84
  else
 
85
    {
 
86
      /* Set s = p_z^{-1}, r_x = p_x s^2, r_y = p_y s^3 */
 
87
 
 
88
      mpn_copyi (up, p+2*ecc->size, ecc->size); /* p_z */
 
89
      ecc_modp_inv (ecc, izp, up, up + ecc->size);
 
90
 
 
91
      ecc_modp_sqr (ecc, iz2p, izp);
 
92
    }
 
93
 
 
94
  ecc_modp_mul (ecc, iz3p, iz2p, p);
 
95
  /* ecc_modp (and ecc_modp_mul) may return a value up to 2p - 1, so
 
96
     do a conditional subtraction. */
 
97
  cy = mpn_sub_n (r, iz3p, ecc->p, ecc->size);
 
98
  cnd_copy (cy, r, iz3p, ecc->size);
 
99
 
 
100
  if (flags & 2)
 
101
    /* Skip y coordinate */
 
102
    return;
 
103
 
 
104
  ecc_modp_mul (ecc, iz3p, iz2p, izp);
 
105
  ecc_modp_mul (ecc, tp, iz3p, p + ecc->size);
 
106
  /* And a similar subtraction. */
 
107
  cy = mpn_sub_n (r + ecc->size, tp, ecc->p, ecc->size);
 
108
  cnd_copy (cy, r + ecc->size, tp, ecc->size);
 
109
 
 
110
#undef izp
 
111
#undef up
 
112
#undef iz2p
 
113
#undef iz3p
 
114
#undef tp
 
115
}