Fixed package group
[platform/upstream/nettle.git] / ecc-192.c
1 /* ecc-192.c */
2
3 /* Compile time constant (but machine dependent) tables. */
4
5 /* nettle, low-level cryptographics library
6  *
7  * Copyright (C) 2013 Niels Möller
8  *  
9  * The nettle library is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or (at your
12  * option) any later version.
13  * 
14  * The nettle library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17  * License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with the nettle library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22  * MA 02111-1301, USA.
23  */
24
25 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include "ecc-internal.h"
34
35 #define USE_REDC 0
36
37 #include "ecc-192.h"
38
39 #if HAVE_NATIVE_ecc_192_modp
40
41 #define ecc_192_modp nettle_ecc_192_modp
42 void
43 ecc_192_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
44
45 /* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */
46
47 #elif GMP_NUMB_BITS == 32
48 /* p is 6 limbs, p = B^6 - B^2 - 1 */
49 static void
50 ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
51 {
52   mp_limb_t cy;
53
54   /* Reduce from 12 to 9 limbs (top limb small)*/
55   cy = mpn_add_n (rp + 2, rp + 2, rp + 8, 4);
56   cy = sec_add_1 (rp + 6, rp + 6, 2, cy);
57   cy += mpn_add_n (rp + 4, rp + 4, rp + 8, 4);
58   assert (cy <= 2);
59
60   rp[8] = cy;
61
62   /* Reduce from 9 to 6 limbs */
63   cy = mpn_add_n (rp, rp, rp + 6, 3);
64   cy = sec_add_1 (rp + 3, rp + 3, 2, cy);
65   cy += mpn_add_n (rp + 2, rp + 2, rp + 6, 3);
66   cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
67   
68   assert (cy <= 1);
69   cy = cnd_add_n (cy, rp, ecc_Bmodp, 6);
70   assert (cy == 0);  
71 }
72 #elif GMP_NUMB_BITS == 64
73 /* p is 3 limbs, p = B^3 - B - 1 */
74 static void
75 ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
76 {
77   mp_limb_t cy;
78
79   /* Reduce from 6 to 5 limbs (top limb small)*/
80   cy = mpn_add_n (rp + 1, rp + 1, rp + 4, 2);
81   cy = sec_add_1 (rp + 3, rp + 3, 1, cy);
82   cy += mpn_add_n (rp + 2, rp + 2, rp + 4, 2);
83   assert (cy <= 2);
84
85   rp[4] = cy;
86
87   /* Reduce from 5 to 4 limbs (high limb small) */
88   cy = mpn_add_n (rp, rp, rp + 3, 2);
89   cy = sec_add_1 (rp + 2, rp + 2, 1, cy);
90   cy += mpn_add_n (rp + 1, rp + 1, rp + 3, 2);
91
92   assert (cy <= 1);
93   cy = cnd_add_n (cy, rp, ecc_Bmodp, 3);
94   assert (cy == 0);  
95 }
96   
97 #else
98 #define ecc_192_modp ecc_generic_modp
99 #endif
100
101 const struct ecc_curve nettle_secp_192r1 =
102 {
103   192,
104   ECC_LIMB_SIZE,
105   ECC_BMODP_SIZE,
106   ECC_BMODQ_SIZE,
107   USE_REDC,
108   ECC_REDC_SIZE,
109   ECC_PIPPENGER_K,
110   ECC_PIPPENGER_C,
111   ecc_p,
112   ecc_b,
113   ecc_q,
114   ecc_g,
115   ecc_redc_g,
116   ecc_192_modp,
117   ecc_generic_redc,
118   ecc_192_modp,
119   ecc_generic_modq,
120   ecc_Bmodp,
121   ecc_Bmodp_shifted,
122   ecc_pp1h,
123   ecc_redc_ppm1,
124   ecc_unit,
125   ecc_Bmodq,
126   ecc_Bmodq_shifted,
127   ecc_qp1h,
128   ecc_table
129 };
130