5c52b043f66d905e8ee348c97ecc884622f99cc8
[platform/upstream/nettle.git] / ecc-192.c
1 /* ecc-192.c
2
3    Compile time constant (but machine dependent) tables.
4
5    Copyright (C) 2013, 2014 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
35
36 #if HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <assert.h>
41
42 /* FIXME: Remove ecc.h include, once prototypes of more internal
43    functions are moved to ecc-internal.h */
44 #include "ecc.h"
45 #include "ecc-internal.h"
46
47 #define USE_REDC 0
48
49 #include "ecc-192.h"
50
51 #if HAVE_NATIVE_ecc_192_modp
52
53 #define ecc_192_modp nettle_ecc_192_modp
54 void
55 ecc_192_modp (const struct ecc_modulo *m, mp_limb_t *rp);
56
57 /* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */
58
59 #elif GMP_NUMB_BITS == 32
60 /* p is 6 limbs, p = B^6 - B^2 - 1 */
61 static void
62 ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
63 {
64   mp_limb_t cy;
65
66   /* Reduce from 12 to 9 limbs (top limb small)*/
67   cy = mpn_add_n (rp + 2, rp + 2, rp + 8, 4);
68   cy = sec_add_1 (rp + 6, rp + 6, 2, cy);
69   cy += mpn_add_n (rp + 4, rp + 4, rp + 8, 4);
70   assert (cy <= 2);
71
72   rp[8] = cy;
73
74   /* Reduce from 9 to 6 limbs */
75   cy = mpn_add_n (rp, rp, rp + 6, 3);
76   cy = sec_add_1 (rp + 3, rp + 3, 2, cy);
77   cy += mpn_add_n (rp + 2, rp + 2, rp + 6, 3);
78   cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
79   
80   assert (cy <= 1);
81   cy = cnd_add_n (cy, rp, ecc_Bmodp, 6);
82   assert (cy == 0);  
83 }
84 #elif GMP_NUMB_BITS == 64
85 /* p is 3 limbs, p = B^3 - B - 1 */
86 static void
87 ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
88 {
89   mp_limb_t cy;
90
91   /* Reduce from 6 to 5 limbs (top limb small)*/
92   cy = mpn_add_n (rp + 1, rp + 1, rp + 4, 2);
93   cy = sec_add_1 (rp + 3, rp + 3, 1, cy);
94   cy += mpn_add_n (rp + 2, rp + 2, rp + 4, 2);
95   assert (cy <= 2);
96
97   rp[4] = cy;
98
99   /* Reduce from 5 to 4 limbs (high limb small) */
100   cy = mpn_add_n (rp, rp, rp + 3, 2);
101   cy = sec_add_1 (rp + 2, rp + 2, 1, cy);
102   cy += mpn_add_n (rp + 1, rp + 1, rp + 3, 2);
103
104   assert (cy <= 1);
105   cy = cnd_add_n (cy, rp, ecc_Bmodp, 3);
106   assert (cy == 0);  
107 }
108   
109 #else
110 #define ecc_192_modp ecc_mod
111 #endif
112
113 const struct ecc_curve nettle_secp_192r1 =
114 {
115   {
116     192,
117     ECC_LIMB_SIZE,
118     ECC_BMODP_SIZE,
119     ECC_REDC_SIZE,
120     ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
121     0,
122
123     ecc_p,
124     ecc_Bmodp,
125     ecc_Bmodp_shifted,    
126     ecc_redc_ppm1,
127     ecc_pp1h,
128
129     ecc_192_modp,
130     ecc_192_modp,
131     ecc_mod_inv,
132     NULL,
133   },
134   {
135     192,
136     ECC_LIMB_SIZE,
137     ECC_BMODQ_SIZE,
138     0,
139     ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
140     0,
141
142     ecc_q,
143     ecc_Bmodq,
144     ecc_Bmodq_shifted,
145     NULL,
146     ecc_qp1h,
147
148     ecc_mod,
149     ecc_mod,
150     ecc_mod_inv,
151     NULL,
152   },
153   
154   USE_REDC,
155   ECC_PIPPENGER_K,
156   ECC_PIPPENGER_C,
157
158   ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
159   ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
160   ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
161   ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),
162
163   ecc_add_jjj,
164   ecc_mul_a,
165   ecc_mul_g,
166   ecc_j_to_a,
167
168   ecc_b,
169   ecc_g,
170   NULL,
171   ecc_unit,
172   ecc_table
173 };
174