ecc-192.c 3.93 KB
Newer Older
1 2 3 4
/* ecc-192.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 40 41

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

#include <assert.h>

42 43 44
/* FIXME: Remove ecc.h include, once prototypes of more internal
   functions are moved to ecc-internal.h */
#include "ecc.h"
Niels Möller's avatar
Niels Möller committed
45 46 47 48 49 50
#include "ecc-internal.h"

#define USE_REDC 0

#include "ecc-192.h"

51 52 53 54
#if HAVE_NATIVE_ecc_192_modp

#define ecc_192_modp nettle_ecc_192_modp
void
55
ecc_192_modp (const struct ecc_modulo *m, mp_limb_t *rp);
56

Niels Möller's avatar
Niels Möller committed
57 58
/* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */

59
#elif GMP_NUMB_BITS == 32
Niels Möller's avatar
Niels Möller committed
60 61
/* p is 6 limbs, p = B^6 - B^2 - 1 */
static void
62
ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
Niels Möller's avatar
Niels Möller committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
  mp_limb_t cy;

  /* Reduce from 12 to 9 limbs (top limb small)*/
  cy = mpn_add_n (rp + 2, rp + 2, rp + 8, 4);
  cy = sec_add_1 (rp + 6, rp + 6, 2, cy);
  cy += mpn_add_n (rp + 4, rp + 4, rp + 8, 4);
  assert (cy <= 2);

  rp[8] = cy;

  /* Reduce from 9 to 6 limbs */
  cy = mpn_add_n (rp, rp, rp + 6, 3);
  cy = sec_add_1 (rp + 3, rp + 3, 2, cy);
  cy += mpn_add_n (rp + 2, rp + 2, rp + 6, 3);
  cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
  
  assert (cy <= 1);
Niels Möller's avatar
Niels Möller committed
81
  cy = cnd_add_n (cy, rp, ecc_Bmodp, 6);
Niels Möller's avatar
Niels Möller committed
82 83 84 85 86
  assert (cy == 0);  
}
#elif GMP_NUMB_BITS == 64
/* p is 3 limbs, p = B^3 - B - 1 */
static void
87
ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
Niels Möller's avatar
Niels Möller committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
  mp_limb_t cy;

  /* Reduce from 6 to 5 limbs (top limb small)*/
  cy = mpn_add_n (rp + 1, rp + 1, rp + 4, 2);
  cy = sec_add_1 (rp + 3, rp + 3, 1, cy);
  cy += mpn_add_n (rp + 2, rp + 2, rp + 4, 2);
  assert (cy <= 2);

  rp[4] = cy;

  /* Reduce from 5 to 4 limbs (high limb small) */
  cy = mpn_add_n (rp, rp, rp + 3, 2);
  cy = sec_add_1 (rp + 2, rp + 2, 1, cy);
  cy += mpn_add_n (rp + 1, rp + 1, rp + 3, 2);

  assert (cy <= 1);
  cy = cnd_add_n (cy, rp, ecc_Bmodp, 3);
  assert (cy == 0);  
}
  
#else
110
#define ecc_192_modp ecc_mod
Niels Möller's avatar
Niels Möller committed
111 112
#endif

113
const struct ecc_curve _nettle_secp_192r1 =
Niels Möller's avatar
Niels Möller committed
114
{
Niels Möller's avatar
Niels Möller committed
115 116 117 118 119
  {
    192,
    ECC_LIMB_SIZE,
    ECC_BMODP_SIZE,
    ECC_REDC_SIZE,
120
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
121
    0,
122

Niels Möller's avatar
Niels Möller committed
123 124 125 126
    ecc_p,
    ecc_Bmodp,
    ecc_Bmodp_shifted,    
    ecc_redc_ppm1,
127 128
    ecc_pp1h,

129 130
    ecc_192_modp,
    ecc_192_modp,
131
    ecc_mod_inv,
132
    NULL,
Niels Möller's avatar
Niels Möller committed
133 134 135 136 137 138
  },
  {
    192,
    ECC_LIMB_SIZE,
    ECC_BMODQ_SIZE,
    0,
139
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
140
    0,
141

Niels Möller's avatar
Niels Möller committed
142 143 144 145
    ecc_q,
    ecc_Bmodq,
    ecc_Bmodq_shifted,
    NULL,
146 147
    ecc_qp1h,

148 149
    ecc_mod,
    ecc_mod,
150
    ecc_mod_inv,
151
    NULL,
Niels Möller's avatar
Niels Möller committed
152 153
  },
  
Niels Möller's avatar
Niels Möller committed
154 155 156
  USE_REDC,
  ECC_PIPPENGER_K,
  ECC_PIPPENGER_C,
157

158
  ECC_ADD_JJA_ITCH (ECC_LIMB_SIZE),
159
  ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
160
  ECC_DUP_JJ_ITCH (ECC_LIMB_SIZE),
161 162 163 164
  ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
  ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
  ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),

165
  ecc_add_jja,
166
  ecc_add_jjj,
167
  ecc_dup_jj,
168 169 170 171
  ecc_mul_a,
  ecc_mul_g,
  ecc_j_to_a,

Niels Möller's avatar
Niels Möller committed
172 173 174 175 176 177
  ecc_b,
  ecc_g,
  ecc_unit,
  ecc_table
};

178 179
const struct ecc_curve *nettle_get_secp_192r1(void)
{
180
  return &_nettle_secp_192r1;
181
}