ecc-521.c 3.13 KB
Newer Older
1
2
3
4
/* ecc-521.c

   Compile time constant (but machine dependent) tables.

5
   Copyright (C) 2013, 2014 Niels Möller
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

   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
33

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

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

40
#include "ecc.h"
Niels Möller's avatar
Niels Möller committed
41
42
43
44
45
46
#include "ecc-internal.h"

#define USE_REDC 0

#include "ecc-521.h"

Niels Möller's avatar
Niels Möller committed
47
48
49
#if HAVE_NATIVE_ecc_521_modp
#define ecc_521_modp nettle_ecc_521_modp
void
50
ecc_521_modp (const struct ecc_modulo *m, mp_limb_t *rp);
Niels Möller's avatar
Niels Möller committed
51
52
53

#else

Niels Möller's avatar
Niels Möller committed
54
55
56
57
58
59
#define B_SHIFT (521 % GMP_NUMB_BITS)
#define BMODP_SHIFT (GMP_NUMB_BITS - B_SHIFT)
#define BMODP ((mp_limb_t) 1 << BMODP_SHIFT)

/* Result may be *slightly* larger than 2^521 */
static void
60
ecc_521_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
Niels Möller's avatar
Niels Möller committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
  /* FIXME: Should use mpn_addlsh_n_ip1 */
  mp_limb_t hi;
  /* Reduce from 2*ECC_LIMB_SIZE to ECC_LIMB_SIZE + 1 */
  rp[ECC_LIMB_SIZE]
    = mpn_addmul_1 (rp, rp + ECC_LIMB_SIZE, ECC_LIMB_SIZE, BMODP);
  hi = mpn_addmul_1 (rp, rp + ECC_LIMB_SIZE, 1, BMODP);
  hi = sec_add_1 (rp + 1, rp + 1, ECC_LIMB_SIZE - 1, hi);

  /* Combine hi with top bits, and add in. */
  hi = (hi << BMODP_SHIFT) | (rp[ECC_LIMB_SIZE-1] >> B_SHIFT);
  rp[ECC_LIMB_SIZE-1] = (rp[ECC_LIMB_SIZE-1]
			 & (((mp_limb_t) 1 << B_SHIFT)-1))
    + sec_add_1 (rp, rp, ECC_LIMB_SIZE - 1, hi);
}
Niels Möller's avatar
Niels Möller committed
76
#endif
Niels Möller's avatar
Niels Möller committed
77

78
const struct ecc_curve _nettle_secp_521r1 =
Niels Möller's avatar
Niels Möller committed
79
{
Niels Möller's avatar
Niels Möller committed
80
81
82
83
84
  {
    521,
    ECC_LIMB_SIZE,    
    ECC_BMODP_SIZE,
    ECC_REDC_SIZE,
85
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
86
    0,
87

Niels Möller's avatar
Niels Möller committed
88
89
90
91
    ecc_p,
    ecc_Bmodp,
    ecc_Bmodp_shifted,
    ecc_redc_ppm1,
92
93
    ecc_pp1h,

94
95
    ecc_521_modp,
    ecc_521_modp,
96
    ecc_mod_inv,
97
    NULL,
Niels Möller's avatar
Niels Möller committed
98
99
100
101
102
103
  },
  {
    521,
    ECC_LIMB_SIZE,    
    ECC_BMODQ_SIZE,
    0,
104
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
105
    0,
106

Niels Möller's avatar
Niels Möller committed
107
108
109
110
    ecc_q,
    ecc_Bmodq,
    ecc_Bmodq_shifted,
    NULL,
111
112
    ecc_qp1h,

113
114
    ecc_mod,
    ecc_mod,
115
    ecc_mod_inv,
116
    NULL,
Niels Möller's avatar
Niels Möller committed
117
118
  },
  
Niels Möller's avatar
Niels Möller committed
119
120
121
  USE_REDC,
  ECC_PIPPENGER_K,
  ECC_PIPPENGER_C,
122

123
  ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
124
125
126
127
  ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
  ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
  ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),

128
  ecc_add_jjj,
129
130
131
132
  ecc_mul_a,
  ecc_mul_g,
  ecc_j_to_a,

Niels Möller's avatar
Niels Möller committed
133
134
  ecc_b,
  ecc_g,
135
  NULL,
Niels Möller's avatar
Niels Möller committed
136
137
138
139
  ecc_unit,
  ecc_table
};

140
141
const struct ecc_curve *nettle_get_secp_521r1(void)
{
142
  return &_nettle_secp_521r1;
143
}