f2e47f6747c11ead4ef8ce62a34517006aa6c172
[platform/upstream/nettle.git] / ecc-mod-arith.c
1 /* ecc-mod-arith.c
2
3    Copyright (C) 2013, 2014 Niels Möller
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 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39
40 #include "ecc-internal.h"
41
42 /* Routines for modp arithmetic. All values are ecc->size limbs, but
43    not necessarily < p. */
44
45 void
46 ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
47              const mp_limb_t *ap, const mp_limb_t *bp)
48 {
49   mp_limb_t cy;
50   cy = mpn_add_n (rp, ap, bp, m->size);
51   cy = cnd_add_n (cy, rp, m->B, m->size);
52   cy = cnd_add_n (cy, rp, m->B, m->size);
53   assert (cy == 0);  
54 }
55
56 void
57 ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
58              const mp_limb_t *ap, const mp_limb_t *bp)
59 {
60   mp_limb_t cy;
61   cy = mpn_sub_n (rp, ap, bp, m->size);
62   cy = cnd_sub_n (cy, rp, m->B, m->size);
63   cy = cnd_sub_n (cy, rp, m->B, m->size);
64   assert (cy == 0);  
65 }
66
67 void
68 ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
69                const mp_limb_t *ap, mp_limb_t b)
70 {
71   mp_limb_t hi;
72
73   assert (b <= 0xffffffff);
74   hi = mpn_mul_1 (rp, ap, m->size, b);
75   hi = mpn_addmul_1 (rp, m->B, m->size, hi);
76   assert (hi <= 1);
77   hi = cnd_add_n (hi, rp, m->B, m->size);
78   /* Sufficient if b < B^size / p */
79   assert (hi == 0);
80 }
81
82 void
83 ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
84                   const mp_limb_t *ap, mp_limb_t b)
85 {
86   mp_limb_t hi;
87
88   assert (b <= 0xffffffff);
89   hi = mpn_addmul_1 (rp, ap, m->size, b);
90   hi = mpn_addmul_1 (rp, m->B, m->size, hi);
91   assert (hi <= 1);
92   hi = cnd_add_n (hi, rp, m->B, m->size);
93   /* Sufficient roughly if b < B^size / p */
94   assert (hi == 0);
95 }
96   
97 void
98 ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
99                   const mp_limb_t *ap, mp_limb_t b)
100 {
101   mp_limb_t hi;
102
103   assert (b <= 0xffffffff);
104   hi = mpn_submul_1 (rp, ap, m->size, b);
105   hi = mpn_submul_1 (rp, m->B, m->size, hi);
106   assert (hi <= 1);
107   hi = cnd_sub_n (hi, rp, m->B, m->size);
108   /* Sufficient roughly if b < B^size / p */
109   assert (hi == 0);
110 }
111
112 /* NOTE: mul and sqr needs 2*m->size limbs at rp */
113 void
114 ecc_mod_mul (const struct ecc_modulo *m, mp_limb_t *rp,
115              const mp_limb_t *ap, const mp_limb_t *bp)
116 {
117   mpn_mul_n (rp, ap, bp, m->size);
118   m->reduce (m, rp);
119 }
120
121 void
122 ecc_mod_sqr (const struct ecc_modulo *m, mp_limb_t *rp,
123              const mp_limb_t *ap)
124 {
125   mpn_sqr (rp, ap, m->size);
126   m->reduce (m, rp);
127 }