1 /* ecc-gost-gc256b.c
2 
3    Copyright (C) 2016-2020 Dmitry Eremin-Solenikov
4 
5    This file is part of GNU Nettle.
6 
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9 
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13 
14    or
15 
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19 
20    or both in parallel, as here.
21 
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26 
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31 
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <assert.h>
37 
38 #include <nettle/ecc.h>
39 #include "ecc-gost-curve.h"
40 #include "ecc-internal.h"
41 
42 #define USE_REDC 0
43 
44 #if defined __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
45 # pragma GCC diagnostic ignored "-Wunused-const-variable"
46 #endif
47 #if GMP_NUMB_BITS == 32
48 #include "ecc/ecc-gost-gc256b-32.h"
49 #elif GMP_NUMB_BITS == 64
50 #include "ecc/ecc-gost-gc256b-64.h"
51 #else
52 #error unsupported configuration
53 #endif
54 
55 static void
ecc_gost_gc256b_modp(const struct ecc_modulo * m,mp_limb_t * rp)56 ecc_gost_gc256b_modp (const struct ecc_modulo *m, mp_limb_t *rp)
57 {
58   mp_size_t mn = m->size;
59   mp_limb_t hi;
60 
61   hi = mpn_addmul_1(rp, rp + mn, mn, 0x269);
62   hi = sec_add_1 (rp, rp, mn, hi * 0x269);
63   hi = sec_add_1 (rp, rp, mn, hi * 0x269);
64   assert(hi == 0);
65 }
66 
67 #define ecc_gost_gc256b_modp ecc_gost_gc256b_modp
68 #define ecc_gost_gc256b_modq ecc_mod
69 
70 const struct ecc_curve _nettle_gost_gc256b =
71 {
72   {
73     256,
74     ECC_LIMB_SIZE,
75     ECC_BMODP_SIZE,
76     ECC_REDC_SIZE,
77     ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
78     0,
79 
80     ecc_p,
81     ecc_Bmodp,
82     ecc_Bmodp_shifted,
83     ecc_redc_ppm1,
84 
85     ecc_pp1h,
86     ecc_gost_gc256b_modp,
87     ecc_gost_gc256b_modp,
88     ecc_mod_inv,
89     NULL,
90   },
91   {
92     256,
93     ECC_LIMB_SIZE,
94     ECC_BMODQ_SIZE,
95     0,
96     ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
97     0,
98 
99     ecc_q,
100     ecc_Bmodq,
101     ecc_Bmodq_shifted,
102     NULL,
103     ecc_qp1h,
104 
105     ecc_gost_gc256b_modq,
106     ecc_gost_gc256b_modq,
107     ecc_mod_inv,
108     NULL,
109   },
110 
111   USE_REDC,
112   ECC_PIPPENGER_K,
113   ECC_PIPPENGER_C,
114 
115   ECC_ADD_JJA_ITCH (ECC_LIMB_SIZE),
116   ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
117   ECC_DUP_JJ_ITCH (ECC_LIMB_SIZE),
118   ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
119   ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
120   ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),
121 
122   ecc_add_jja,
123   ecc_add_jjj,
124   ecc_dup_jj,
125   ecc_mul_a,
126   ecc_mul_g,
127   ecc_j_to_a,
128 
129   ecc_b,
130   ecc_unit,
131   ecc_table
132 };
133 
nettle_get_gost_gc256b(void)134 const struct ecc_curve *nettle_get_gost_gc256b(void)
135 {
136   return &_nettle_gost_gc256b;
137 }
138