xref: /btstack/3rd-party/micro-ecc/uECC.c (revision e6c9673776c7f85e24c76da7f4b1b83031102d2b)
1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2 
3 #include "uECC.h"
4 
5 #ifndef uECC_PLATFORM
6     #if __AVR__
7         #define uECC_PLATFORM uECC_avr
8     #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */
9         #define uECC_PLATFORM uECC_arm_thumb2
10     #elif defined(__thumb__)
11         #define uECC_PLATFORM uECC_arm_thumb
12     #elif defined(__arm__) || defined(_M_ARM)
13         #define uECC_PLATFORM uECC_arm
14     #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__)
15         #define uECC_PLATFORM uECC_x86
16     #elif defined(__amd64__) || defined(_M_X64)
17         #define uECC_PLATFORM uECC_x86_64
18     #else
19         #define uECC_PLATFORM uECC_arch_other
20     #endif
21 #endif
22 
23 #ifndef uECC_WORD_SIZE
24     #if uECC_PLATFORM == uECC_avr
25         #define uECC_WORD_SIZE 1
26     #elif (uECC_PLATFORM == uECC_x86_64)
27         #define uECC_WORD_SIZE 8
28     #else
29         #define uECC_WORD_SIZE 4
30     #endif
31 #endif
32 
33 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp224r1) && (uECC_WORD_SIZE == 8)
34     #undef uECC_WORD_SIZE
35     #define uECC_WORD_SIZE 4
36     #if (uECC_PLATFORM == uECC_x86_64)
37         #undef uECC_PLATFORM
38         #define uECC_PLATFORM uECC_x86
39     #endif
40 #endif
41 
42 #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8)
43     #error "Unsupported value for uECC_WORD_SIZE"
44 #endif
45 
46 #if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1))
47     #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm")
48     #undef uECC_WORD_SIZE
49     #define uECC_WORD_SIZE 1
50 #endif
51 
52 #if (uECC_ASM && \
53      (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \
54      (uECC_WORD_SIZE != 4))
55     #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm")
56     #undef uECC_WORD_SIZE
57     #define uECC_WORD_SIZE 4
58 #endif
59 
60 #if __STDC_VERSION__ >= 199901L
61     #define RESTRICT restrict
62 #else
63     #define RESTRICT
64 #endif
65 
66 #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302)
67     #define SUPPORTS_INT128 1
68 #else
69     #define SUPPORTS_INT128 0
70 #endif
71 
72 #define MAX_TRIES 64
73 
74 #if (uECC_WORD_SIZE == 1)
75 
76 typedef uint8_t uECC_word_t;
77 typedef uint16_t uECC_dword_t;
78 typedef uint8_t wordcount_t;
79 typedef int8_t swordcount_t;
80 typedef int16_t bitcount_t;
81 typedef int8_t cmpresult_t;
82 
83 #define HIGH_BIT_SET 0x80
84 #define uECC_WORD_BITS 8
85 #define uECC_WORD_BITS_SHIFT 3
86 #define uECC_WORD_BITS_MASK 0x07
87 
88 #define uECC_WORDS_1 20
89 #define uECC_WORDS_2 24
90 #define uECC_WORDS_3 32
91 #define uECC_WORDS_4 32
92 #define uECC_WORDS_5 28
93 
94 #define uECC_N_WORDS_1 21
95 #define uECC_N_WORDS_2 24
96 #define uECC_N_WORDS_3 32
97 #define uECC_N_WORDS_4 32
98 #define uECC_N_WORDS_5 28
99 
100 #define Curve_P_1 {0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, \
101                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
102                    0xFF, 0xFF, 0xFF, 0xFF}
103 #define Curve_P_2 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
104                    0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
105                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
106 #define Curve_P_3 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
107                    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, \
108                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
109                    0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
110 #define Curve_P_4 {0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, \
111                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
112                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
113                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
114 #define Curve_P_5 {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
115                    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \
116                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
117                    0xFF, 0xFF, 0xFF, 0xFF}
118 
119 #define Curve_B_1 {0x45, 0xFA, 0x65, 0xC5, 0xAD, 0xD4, 0xD4, 0x81, \
120                    0x9F, 0xF8, 0xAC, 0x65, 0x8B, 0x7A, 0xBD, 0x54, \
121                    0xFC, 0xBE, 0x97, 0x1C}
122 #define Curve_B_2 {0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE, \
123                    0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F, \
124                    0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64}
125 #define Curve_B_3 {0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B, \
126                    0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65, \
127                    0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3, \
128                    0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A}
129 #define Curve_B_4 {0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
130                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
131                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
132                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
133 #define Curve_B_5 {0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27, \
134                    0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50, \
135                    0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C, \
136                    0x85, 0x0A, 0x05, 0xB4}
137 
138 #define Curve_G_1 { \
139     {0x82, 0xFC, 0xCB, 0x13, 0xB9, 0x8B, 0xC3, 0x68, \
140         0x89, 0x69, 0x64, 0x46, 0x28, 0x73, 0xF5, 0x8E, \
141         0x68, 0xB5, 0x96, 0x4A}, \
142     {0x32, 0xFB, 0xC5, 0x7A, 0x37, 0x51, 0x23, 0x04, \
143         0x12, 0xC9, 0xDC, 0x59, 0x7D, 0x94, 0x68, 0x31, \
144         0x55, 0x28, 0xA6, 0x23}}
145 
146 #define Curve_G_2 { \
147     {0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4, \
148         0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C, \
149         0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18}, \
150     {0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73, \
151         0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63, \
152         0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07}}
153 
154 #define Curve_G_3 { \
155     {0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4, \
156         0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77, \
157         0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8, \
158         0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B}, \
159     {0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB, \
160         0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B, \
161         0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E, \
162         0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F}}
163 
164 #define Curve_G_4 { \
165     {0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59, \
166         0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02, \
167         0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55, \
168         0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79}, \
169     {0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C, \
170         0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD, \
171         0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D, \
172         0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48}}
173 
174 #define Curve_G_5 { \
175     {0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34, \
176         0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A, \
177         0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B, \
178         0xBD, 0x0C, 0x0E, 0xB7}, \
179     {0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44, \
180         0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD, \
181         0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5, \
182         0x88, 0x63, 0x37, 0xBD}}
183 
184 #define Curve_N_1 {0x57, 0x22, 0x75, 0xCA, 0xD3, 0xAE, 0x27, 0xF9, \
185                    0xC8, 0xF4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
186                    0x00, 0x00, 0x00, 0x00, 0x01}
187 #define Curve_N_2 {0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14, \
188                    0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF, \
189                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
190 #define Curve_N_3 {0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, \
191                    0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC, \
192                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
193                    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
194 #define Curve_N_4 {0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, \
195                    0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA, \
196                    0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
197                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
198 #define Curve_N_5 {0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13, \
199                    0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF, \
200                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
201                    0xFF, 0xFF, 0xFF, 0xFF}
202 
203 #elif (uECC_WORD_SIZE == 4)
204 
205 typedef uint32_t uECC_word_t;
206 typedef uint64_t uECC_dword_t;
207 typedef unsigned wordcount_t;
208 typedef int swordcount_t;
209 typedef int bitcount_t;
210 typedef int cmpresult_t;
211 
212 #define HIGH_BIT_SET 0x80000000
213 #define uECC_WORD_BITS 32
214 #define uECC_WORD_BITS_SHIFT 5
215 #define uECC_WORD_BITS_MASK 0x01F
216 
217 #define uECC_WORDS_1 5
218 #define uECC_WORDS_2 6
219 #define uECC_WORDS_3 8
220 #define uECC_WORDS_4 8
221 #define uECC_WORDS_5 7
222 
223 #define uECC_N_WORDS_1 6
224 #define uECC_N_WORDS_2 6
225 #define uECC_N_WORDS_3 8
226 #define uECC_N_WORDS_4 8
227 #define uECC_N_WORDS_5 7
228 
229 #define Curve_P_1 {0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
230 #define Curve_P_2 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
231 #define Curve_P_3 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, \
232                    0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF}
233 #define Curve_P_4 {0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, \
234                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
235 #define Curve_P_5 {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, \
236                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
237 
238 #define Curve_B_1 {0xC565FA45, 0x81D4D4AD, 0x65ACF89F, 0x54BD7A8B, 0x1C97BEFC}
239 #define Curve_B_2 {0xC146B9B1, 0xFEB8DEEC, 0x72243049, 0x0FA7E9AB, 0xE59C80E7, 0x64210519}
240 #define Curve_B_3 {0x27D2604B, 0x3BCE3C3E, 0xCC53B0F6, 0x651D06B0, \
241                    0x769886BC, 0xB3EBBD55, 0xAA3A93E7, 0x5AC635D8}
242 #define Curve_B_4 {0x00000007, 0x00000000, 0x00000000, 0x00000000, \
243                    0x00000000, 0x00000000, 0x00000000, 0x00000000}
244 #define Curve_B_5 {0x2355FFB4, 0x270B3943, 0xD7BFD8BA, 0x5044B0B7, \
245                    0xF5413256, 0x0C04B3AB, 0xB4050A85}
246 
247 #define Curve_G_1 { \
248     {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \
249     {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}}
250 
251 #define Curve_G_2 { \
252     {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \
253     {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}}
254 
255 #define Curve_G_3 { \
256     {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81,  \
257      0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \
258     {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357,  \
259      0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}}
260 
261 #define Curve_G_4 { \
262     {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB,  \
263      0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \
264     {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448,  \
265      0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}}
266 
267 #define Curve_G_5 { \
268     {0x115C1D21, 0x343280D6, 0x56C21122, 0x4A03C1D3, \
269      0x321390B9, 0x6BB4BF7F, 0xB70E0CBD}, \
270     {0x85007E34, 0x44D58199, 0x5A074764, 0xCD4375A0, \
271      0x4C22DFE6, 0xB5F723FB, 0xBD376388}}
272 
273 #define Curve_N_1 {0xCA752257, 0xF927AED3, 0x0001F4C8, 0x00000000, 0x00000000, 0x00000001}
274 #define Curve_N_2 {0xB4D22831, 0x146BC9B1, 0x99DEF836, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
275 #define Curve_N_3 {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, \
276                    0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF}
277 #define Curve_N_4 {0xD0364141, 0xBFD25E8C, 0xAF48A03B, 0xBAAEDCE6, \
278                    0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
279 #define Curve_N_5 {0x5C5C2A3D, 0x13DD2945, 0xE0B8F03E, 0xFFFF16A2, \
280                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
281 
282 #elif (uECC_WORD_SIZE == 8)
283 
284 typedef uint64_t uECC_word_t;
285 #if SUPPORTS_INT128
286 typedef unsigned __int128 uECC_dword_t;
287 #endif
288 typedef unsigned wordcount_t;
289 typedef int swordcount_t;
290 typedef int bitcount_t;
291 typedef int cmpresult_t;
292 
293 #define HIGH_BIT_SET 0x8000000000000000ull
294 #define uECC_WORD_BITS 64
295 #define uECC_WORD_BITS_SHIFT 6
296 #define uECC_WORD_BITS_MASK 0x03F
297 
298 #define uECC_WORDS_1 3
299 #define uECC_WORDS_2 3
300 #define uECC_WORDS_3 4
301 #define uECC_WORDS_4 4
302 #define uECC_WORDS_5 4
303 
304 #define uECC_N_WORDS_1 3
305 #define uECC_N_WORDS_2 3
306 #define uECC_N_WORDS_3 4
307 #define uECC_N_WORDS_4 4
308 #define uECC_N_WORDS_5 4
309 
310 #define Curve_P_1 {0xFFFFFFFF7FFFFFFFull, 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
311 #define Curve_P_2 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
312 #define Curve_P_3 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \
313                    0x0000000000000000ull, 0xFFFFFFFF00000001ull}
314 #define Curve_P_4 {0xFFFFFFFEFFFFFC2Full, 0xFFFFFFFFFFFFFFFFull, \
315                    0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull}
316 #define Curve_P_5 {0x0000000000000001ull, 0xFFFFFFFF00000000ull, \
317                    0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
318 
319 #define Curve_B_1 {0x81D4D4ADC565FA45ull, 0x54BD7A8B65ACF89Full, 0x000000001C97BEFCull}
320 #define Curve_B_2 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull}
321 #define Curve_B_3 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \
322                    0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull}
323 #define Curve_B_4 {0x0000000000000007ull, 0x0000000000000000ull, \
324                    0x0000000000000000ull, 0x0000000000000000ull}
325 #define Curve_B_5 {0x270B39432355FFB4ull, 0x5044B0B7D7BFD8BAull, \
326                    0x0C04B3ABF5413256ull, 0x00000000B4050A85ull}
327 
328 #define Curve_G_1 { \
329     {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \
330     {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}}
331 
332 #define Curve_G_2 { \
333     {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \
334     {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}}
335 
336 #define Curve_G_3 { \
337     {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \
338     {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}}
339 
340 #define Curve_G_4 { \
341     {0x59F2815B16F81798ull, 0x029BFCDB2DCE28D9ull, 0x55A06295CE870B07ull, 0x79BE667EF9DCBBACull}, \
342     {0x9C47D08FFB10D4B8ull, 0xFD17B448A6855419ull, 0x5DA4FBFC0E1108A8ull, 0x483ADA7726A3C465ull}}
343 
344 #define Curve_G_5 { \
345     {0x343280D6115C1D21ull, 0x4A03C1D356C21122ull, 0x6BB4BF7F321390B9ull, 0x00000000B70E0CBDull}, \
346     {0x44D5819985007E34ull, 0xCD4375A05A074764ull, 0xB5F723FB4C22DFE6ull, 0x00000000BD376388ull}}
347 
348 #define Curve_N_1 {0xF927AED3CA752257ull, 0x000000000001F4C8ull, 0x0000000100000000ull}
349 #define Curve_N_2 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull}
350 #define Curve_N_3 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \
351                    0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull}
352 #define Curve_N_4 {0xBFD25E8CD0364141ull, 0xBAAEDCE6AF48A03Bull, \
353                    0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
354 #define Curve_N_5 {0x13DD29455C5C2A3Dull, 0xFFFF16A2E0B8F03Eull, \
355                    0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
356 
357 #endif /* (uECC_WORD_SIZE == 8) */
358 
359 #define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE)
360 #define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE)
361 
362 typedef struct EccPoint {
363     uECC_word_t x[uECC_WORDS];
364     uECC_word_t y[uECC_WORDS];
365 } EccPoint;
366 
367 static const uECC_word_t curve_p[uECC_WORDS] = uECC_CONCAT(Curve_P_, uECC_CURVE);
368 static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE);
369 static const EccPoint curve_G = uECC_CONCAT(Curve_G_, uECC_CURVE);
370 static const uECC_word_t curve_n[uECC_N_WORDS] = uECC_CONCAT(Curve_N_, uECC_CURVE);
371 
372 static void vli_clear(uECC_word_t *vli);
373 static uECC_word_t vli_isZero(const uECC_word_t *vli);
374 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit);
375 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words);
376 static void vli_set(uECC_word_t *dest, const uECC_word_t *src);
377 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right);
378 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right);
379 static void vli_rshift1(uECC_word_t *vli);
380 static uECC_word_t vli_add(uECC_word_t *result,
381                            const uECC_word_t *left,
382                            const uECC_word_t *right);
383 static uECC_word_t vli_sub(uECC_word_t *result,
384                            const uECC_word_t *left,
385                            const uECC_word_t *right);
386 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right);
387 static void vli_modAdd(uECC_word_t *result,
388                        const uECC_word_t *left,
389                        const uECC_word_t *right,
390                        const uECC_word_t *mod);
391 static void vli_modSub(uECC_word_t *result,
392                        const uECC_word_t *left,
393                        const uECC_word_t *right,
394                        const uECC_word_t *mod);
395 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product);
396 static void vli_modMult_fast(uECC_word_t *result,
397                              const uECC_word_t *left,
398                              const uECC_word_t *right);
399 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod);
400 #if uECC_SQUARE_FUNC
401 static void vli_square(uECC_word_t *result, const uECC_word_t *left);
402 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left);
403 #endif
404 
405 #if (defined(_WIN32) || defined(_WIN64))
406 /* Windows */
407 
408 #define WIN32_LEAN_AND_MEAN
409 #include <windows.h>
410 #include <wincrypt.h>
411 
412 static int default_RNG(uint8_t *dest, unsigned size) {
413     HCRYPTPROV prov;
414     if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
415         return 0;
416     }
417 
418     CryptGenRandom(prov, size, (BYTE *)dest);
419     CryptReleaseContext(prov, 0);
420     return 1;
421 }
422 
423 #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \
424     (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)
425 
426 /* Some POSIX-like system with /dev/urandom or /dev/random. */
427 #include <sys/types.h>
428 #include <fcntl.h>
429 #include <unistd.h>
430 
431 #ifndef O_CLOEXEC
432     #define O_CLOEXEC 0
433 #endif
434 
435 static int default_RNG(uint8_t *dest, unsigned size) {
436     int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
437     if (fd == -1) {
438         fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
439         if (fd == -1) {
440             return 0;
441         }
442     }
443 
444     char *ptr = (char *)dest;
445     size_t left = size;
446     while (left > 0) {
447         ssize_t bytes_read = read(fd, ptr, left);
448         if (bytes_read <= 0) { // read failed
449             close(fd);
450             return 0;
451         }
452         left -= bytes_read;
453         ptr += bytes_read;
454     }
455 
456     close(fd);
457     return 1;
458 }
459 
460 #else /* Some other platform */
461 
462 static int default_RNG(uint8_t *dest, unsigned size) {
463     return 0;
464 }
465 
466 #endif
467 
468 static uECC_RNG_Function g_rng_function = &default_RNG;
469 
470 void uECC_set_rng(uECC_RNG_Function rng_function) {
471     g_rng_function = rng_function;
472 }
473 
474 #ifdef __GNUC__ /* Only support GCC inline asm for now */
475     #if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
476         #include "asm_avr.inc"
477     #endif
478 
479     #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
480                       uECC_PLATFORM == uECC_arm_thumb2))
481         #include "asm_arm.inc"
482     #endif
483 #endif
484 
485 #if !asm_clear
486 static void vli_clear(uECC_word_t *vli) {
487     wordcount_t i;
488     for (i = 0; i < uECC_WORDS; ++i) {
489         vli[i] = 0;
490     }
491 }
492 #endif
493 
494 /* Returns 1 if vli == 0, 0 otherwise. */
495 #if !asm_isZero
496 static uECC_word_t vli_isZero(const uECC_word_t *vli) {
497     wordcount_t i;
498     for (i = 0; i < uECC_WORDS; ++i) {
499         if (vli[i]) {
500             return 0;
501         }
502     }
503     return 1;
504 }
505 #endif
506 
507 /* Returns nonzero if bit 'bit' of vli is set. */
508 #if !asm_testBit
509 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) {
510     return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
511 }
512 #endif
513 
514 /* Counts the number of words in vli. */
515 #if !asm_numBits
516 static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) {
517     swordcount_t i;
518     /* Search from the end until we find a non-zero digit.
519        We do it in reverse because we expect that most digits will be nonzero. */
520     for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
521     }
522 
523     return (i + 1);
524 }
525 
526 /* Counts the number of bits required to represent vli. */
527 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) {
528     uECC_word_t i;
529     uECC_word_t digit;
530 
531     wordcount_t num_digits = vli_numDigits(vli, max_words);
532     if (num_digits == 0) {
533         return 0;
534     }
535 
536     digit = vli[num_digits - 1];
537     for (i = 0; digit; ++i) {
538         digit >>= 1;
539     }
540 
541     return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
542 }
543 #endif /* !asm_numBits */
544 
545 /* Sets dest = src. */
546 #if !asm_set
547 static void vli_set(uECC_word_t *dest, const uECC_word_t *src) {
548     wordcount_t i;
549     for (i = 0; i < uECC_WORDS; ++i) {
550         dest[i] = src[i];
551     }
552 }
553 #endif
554 
555 /* Returns sign of left - right. */
556 #if !asm_cmp
557 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) {
558     swordcount_t i;
559     for (i = uECC_WORDS - 1; i >= 0; --i) {
560         if (left[i] > right[i]) {
561             return 1;
562         } else if (left[i] < right[i]) {
563             return -1;
564         }
565     }
566     return 0;
567 }
568 #endif
569 
570 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) {
571     uECC_word_t result = 0;
572     swordcount_t i;
573     for (i = uECC_WORDS - 1; i >= 0; --i) {
574         result |= (left[i] ^ right[i]);
575     }
576     return (result == 0);
577 }
578 
579 /* Computes vli = vli >> 1. */
580 #if !asm_rshift1
581 static void vli_rshift1(uECC_word_t *vli) {
582     uECC_word_t *end = vli;
583     uECC_word_t carry = 0;
584 
585     vli += uECC_WORDS;
586     while (vli-- > end) {
587         uECC_word_t temp = *vli;
588         *vli = (temp >> 1) | carry;
589         carry = temp << (uECC_WORD_BITS - 1);
590     }
591 }
592 #endif
593 
594 /* Computes result = left + right, returning carry. Can modify in place. */
595 #if !asm_add
596 static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
597     uECC_word_t carry = 0;
598     wordcount_t i;
599     for (i = 0; i < uECC_WORDS; ++i) {
600         uECC_word_t sum = left[i] + right[i] + carry;
601         if (sum != left[i]) {
602             carry = (sum < left[i]);
603         }
604         result[i] = sum;
605     }
606     return carry;
607 }
608 #endif
609 
610 /* Computes result = left - right, returning borrow. Can modify in place. */
611 #if !asm_sub
612 static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
613     uECC_word_t borrow = 0;
614     wordcount_t i;
615     for (i = 0; i < uECC_WORDS; ++i) {
616         uECC_word_t diff = left[i] - right[i] - borrow;
617         if (diff != left[i]) {
618             borrow = (diff > left[i]);
619         }
620         result[i] = diff;
621     }
622     return borrow;
623 }
624 #endif
625 
626 #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1)
627 static void muladd(uECC_word_t a,
628                    uECC_word_t b,
629                    uECC_word_t *r0,
630                    uECC_word_t *r1,
631                    uECC_word_t *r2) {
632 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
633     uint64_t a0 = a & 0xffffffffull;
634     uint64_t a1 = a >> 32;
635     uint64_t b0 = b & 0xffffffffull;
636     uint64_t b1 = b >> 32;
637 
638     uint64_t i0 = a0 * b0;
639     uint64_t i1 = a0 * b1;
640     uint64_t i2 = a1 * b0;
641     uint64_t i3 = a1 * b1;
642 
643     uint64_t p0, p1;
644 
645     i2 += (i0 >> 32);
646     i2 += i1;
647     if (i2 < i1) { // overflow
648         i3 += 0x100000000ull;
649     }
650 
651     p0 = (i0 & 0xffffffffull) | (i2 << 32);
652     p1 = i3 + (i2 >> 32);
653 
654     *r0 += p0;
655     *r1 += (p1 + (*r0 < p0));
656     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
657 #else
658     uECC_dword_t p = (uECC_dword_t)a * b;
659     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
660     r01 += p;
661     *r2 += (r01 < p);
662     *r1 = r01 >> uECC_WORD_BITS;
663     *r0 = (uECC_word_t)r01;
664 #endif
665 }
666 #define muladd_exists 1
667 #endif
668 
669 #if !asm_mult
670 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
671     uECC_word_t r0 = 0;
672     uECC_word_t r1 = 0;
673     uECC_word_t r2 = 0;
674     wordcount_t i, k;
675 
676     /* Compute each digit of result in sequence, maintaining the carries. */
677     for (k = 0; k < uECC_WORDS; ++k) {
678         for (i = 0; i <= k; ++i) {
679             muladd(left[i], right[k - i], &r0, &r1, &r2);
680         }
681         result[k] = r0;
682         r0 = r1;
683         r1 = r2;
684         r2 = 0;
685     }
686     for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) {
687         for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) {
688             muladd(left[i], right[k - i], &r0, &r1, &r2);
689         }
690         result[k] = r0;
691         r0 = r1;
692         r1 = r2;
693         r2 = 0;
694     }
695     result[uECC_WORDS * 2 - 1] = r0;
696 }
697 #endif
698 
699 #if uECC_SQUARE_FUNC
700 
701 #if !asm_square
702 static void mul2add(uECC_word_t a,
703                     uECC_word_t b,
704                     uECC_word_t *r0,
705                     uECC_word_t *r1,
706                     uECC_word_t *r2) {
707 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
708     uint64_t a0 = a & 0xffffffffull;
709     uint64_t a1 = a >> 32;
710     uint64_t b0 = b & 0xffffffffull;
711     uint64_t b1 = b >> 32;
712 
713     uint64_t i0 = a0 * b0;
714     uint64_t i1 = a0 * b1;
715     uint64_t i2 = a1 * b0;
716     uint64_t i3 = a1 * b1;
717 
718     uint64_t p0, p1;
719 
720     i2 += (i0 >> 32);
721     i2 += i1;
722     if (i2 < i1)
723     { // overflow
724         i3 += 0x100000000ull;
725     }
726 
727     p0 = (i0 & 0xffffffffull) | (i2 << 32);
728     p1 = i3 + (i2 >> 32);
729 
730     *r2 += (p1 >> 63);
731     p1 = (p1 << 1) | (p0 >> 63);
732     p0 <<= 1;
733 
734     *r0 += p0;
735     *r1 += (p1 + (*r0 < p0));
736     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
737 #else
738     uECC_dword_t p = (uECC_dword_t)a * b;
739     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
740     *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
741     p *= 2;
742     r01 += p;
743     *r2 += (r01 < p);
744     *r1 = r01 >> uECC_WORD_BITS;
745     *r0 = (uECC_word_t)r01;
746 #endif
747 }
748 
749 static void vli_square(uECC_word_t *result, const uECC_word_t *left) {
750     uECC_word_t r0 = 0;
751     uECC_word_t r1 = 0;
752     uECC_word_t r2 = 0;
753 
754     wordcount_t i, k;
755 
756     for (k = 0; k < uECC_WORDS * 2 - 1; ++k) {
757         uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS);
758         for (i = min; i <= k && i <= k - i; ++i) {
759             if (i < k-i) {
760                 mul2add(left[i], left[k - i], &r0, &r1, &r2);
761             } else {
762                 muladd(left[i], left[k - i], &r0, &r1, &r2);
763             }
764         }
765         result[k] = r0;
766         r0 = r1;
767         r1 = r2;
768         r2 = 0;
769     }
770 
771     result[uECC_WORDS * 2 - 1] = r0;
772 }
773 #endif
774 
775 #else /* uECC_SQUARE_FUNC */
776 
777 #define vli_square(result, left, size) vli_mult((result), (left), (left), (size))
778 
779 #endif /* uECC_SQUARE_FUNC */
780 
781 
782 /* Computes result = (left + right) % mod.
783    Assumes that left < mod and right < mod, and that result does not overlap mod. */
784 #if !asm_modAdd
785 static void vli_modAdd(uECC_word_t *result,
786                        const uECC_word_t *left,
787                        const uECC_word_t *right,
788                        const uECC_word_t *mod) {
789     uECC_word_t carry = vli_add(result, left, right);
790     if (carry || vli_cmp(result, mod) >= 0) {
791         /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
792         vli_sub(result, result, mod);
793     }
794 }
795 #endif
796 
797 /* Computes result = (left - right) % mod.
798    Assumes that left < mod and right < mod, and that result does not overlap mod. */
799 #if !asm_modSub
800 static void vli_modSub(uECC_word_t *result,
801                        const uECC_word_t *left,
802                        const uECC_word_t *right,
803                        const uECC_word_t *mod) {
804     uECC_word_t l_borrow = vli_sub(result, left, right);
805     if (l_borrow) {
806         /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
807            we can get the correct result from result + mod (with overflow). */
808         vli_add(result, result, mod);
809     }
810 }
811 #endif
812 
813 #if !asm_modSub_fast
814     #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p)
815 #endif
816 
817 #if !asm_mmod_fast
818 
819 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1)
820 /* omega_mult() is defined farther below for the different curves / word sizes */
821 static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right);
822 
823 /* Computes result = product % curve_p
824     see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354
825 
826     Note that this only works if log2(omega) < log2(p) / 2 */
827 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) {
828     uECC_word_t tmp[2 * uECC_WORDS];
829     uECC_word_t carry;
830 
831     vli_clear(tmp);
832     vli_clear(tmp + uECC_WORDS);
833 
834     omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */
835 
836     carry = vli_add(result, product, tmp); /* (C, r) = r + q       */
837     vli_clear(product);
838     omega_mult(product, tmp + uECC_WORDS); /* Rq*c */
839     carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */
840 
841     while (carry > 0) {
842         --carry;
843         vli_sub(result, result, curve_p);
844     }
845     if (vli_cmp(result, curve_p) > 0) {
846         vli_sub(result, result, curve_p);
847     }
848 }
849 
850 #endif
851 
852 #if uECC_CURVE == uECC_secp160r1
853 
854 #if uECC_WORD_SIZE == 1
855 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
856     uint8_t carry;
857     uint8_t i;
858 
859     /* Multiply by (2^31 + 1). */
860     vli_set(result + 4, right); /* 2^32 */
861     vli_rshift1(result + 4); /* 2^31 */
862     result[3] = right[0] << 7; /* get last bit from shift */
863 
864     carry = vli_add(result, result, right); /* 2^31 + 1 */
865     for (i = uECC_WORDS; carry; ++i) {
866         uint16_t sum = (uint16_t)result[i] + carry;
867         result[i] = (uint8_t)sum;
868         carry = sum >> 8;
869     }
870 }
871 #elif uECC_WORD_SIZE == 4
872 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
873     uint32_t carry;
874     unsigned i;
875 
876     /* Multiply by (2^31 + 1). */
877     vli_set(result + 1, right); /* 2^32 */
878     vli_rshift1(result + 1); /* 2^31 */
879     result[0] = right[0] << 31; /* get last bit from shift */
880 
881     carry = vli_add(result, result, right); /* 2^31 + 1 */
882     for (i = uECC_WORDS; carry; ++i) {
883         uint64_t sum = (uint64_t)result[i] + carry;
884         result[i] = (uint32_t)sum;
885         carry = sum >> 32;
886     }
887 }
888 #endif /* uECC_WORD_SIZE */
889 
890 #elif uECC_CURVE == uECC_secp192r1
891 
892 /* Computes result = product % curve_p.
893    See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
894 #if uECC_WORD_SIZE == 1
895 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
896     uint8_t tmp[uECC_WORDS];
897     uint8_t carry;
898 
899     vli_set(result, product);
900 
901     vli_set(tmp, &product[24]);
902     carry = vli_add(result, result, tmp);
903 
904     tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
905     tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27];
906     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
907     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
908     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
909     carry += vli_add(result, result, tmp);
910 
911     tmp[0] = tmp[8] = product[40];
912     tmp[1] = tmp[9] = product[41];
913     tmp[2] = tmp[10] = product[42];
914     tmp[3] = tmp[11] = product[43];
915     tmp[4] = tmp[12] = product[44];
916     tmp[5] = tmp[13] = product[45];
917     tmp[6] = tmp[14] = product[46];
918     tmp[7] = tmp[15] = product[47];
919     tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
920     carry += vli_add(result, result, tmp);
921 
922     while (carry || vli_cmp(curve_p, result) != 1) {
923         carry -= vli_sub(result, result, curve_p);
924     }
925 }
926 #elif uECC_WORD_SIZE == 4
927 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
928     uint32_t tmp[uECC_WORDS];
929     int carry;
930 
931     vli_set(result, product);
932 
933     vli_set(tmp, &product[6]);
934     carry = vli_add(result, result, tmp);
935 
936     tmp[0] = tmp[1] = 0;
937     tmp[2] = product[6];
938     tmp[3] = product[7];
939     tmp[4] = product[8];
940     tmp[5] = product[9];
941     carry += vli_add(result, result, tmp);
942 
943     tmp[0] = tmp[2] = product[10];
944     tmp[1] = tmp[3] = product[11];
945     tmp[4] = tmp[5] = 0;
946     carry += vli_add(result, result, tmp);
947 
948     while (carry || vli_cmp(curve_p, result) != 1) {
949         carry -= vli_sub(result, result, curve_p);
950     }
951 }
952 #else
953 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
954     uint64_t tmp[uECC_WORDS];
955     int carry;
956 
957     vli_set(result, product);
958 
959     vli_set(tmp, &product[3]);
960     carry = vli_add(result, result, tmp);
961 
962     tmp[0] = 0;
963     tmp[1] = product[3];
964     tmp[2] = product[4];
965     carry += vli_add(result, result, tmp);
966 
967     tmp[0] = tmp[1] = product[5];
968     tmp[2] = 0;
969     carry += vli_add(result, result, tmp);
970 
971     while (carry || vli_cmp(curve_p, result) != 1) {
972         carry -= vli_sub(result, result, curve_p);
973     }
974 }
975 #endif /* uECC_WORD_SIZE */
976 
977 #elif uECC_CURVE == uECC_secp256r1
978 
979 /* Computes result = product % curve_p
980    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
981 #if uECC_WORD_SIZE == 1
982 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
983     uint8_t tmp[uECC_BYTES];
984     int8_t carry;
985 
986     /* t */
987     vli_set(result, product);
988 
989     /* s1 */
990     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
991     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
992     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
993     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
994     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
995     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
996     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
997     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
998     carry = vli_add(tmp, tmp, tmp);
999     carry += vli_add(result, result, tmp);
1000 
1001     /* s2 */
1002     tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51];
1003     tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55];
1004     tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59];
1005     tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63];
1006     tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0;
1007     carry += vli_add(tmp, tmp, tmp);
1008     carry += vli_add(result, result, tmp);
1009 
1010     /* s3 */
1011     tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35];
1012     tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39];
1013     tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43];
1014     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1015     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1016     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1017     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
1018     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
1019     carry += vli_add(result, result, tmp);
1020 
1021     /* s4 */
1022     tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39];
1023     tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43];
1024     tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47];
1025     tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55];
1026     tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59];
1027     tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63];
1028     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1029     tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35];
1030     carry += vli_add(result, result, tmp);
1031 
1032     /* d1 */
1033     tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47];
1034     tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51];
1035     tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1036     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1037     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1038     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1039     tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35];
1040     tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43];
1041     carry -= vli_sub(result, result, tmp);
1042 
1043     /* d2 */
1044     tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51];
1045     tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55];
1046     tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59];
1047     tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63];
1048     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1049     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1050     tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39];
1051     tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47];
1052     carry -= vli_sub(result, result, tmp);
1053 
1054     /* d3 */
1055     tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55];
1056     tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59];
1057     tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63];
1058     tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35];
1059     tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39];
1060     tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43];
1061     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1062     tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51];
1063     carry -= vli_sub(result, result, tmp);
1064 
1065     /* d4 */
1066     tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59];
1067     tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63];
1068     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1069     tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39];
1070     tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43];
1071     tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47];
1072     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1073     tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55];
1074     carry -= vli_sub(result, result, tmp);
1075 
1076     if (carry < 0) {
1077         do {
1078             carry += vli_add(result, result, curve_p);
1079         } while (carry < 0);
1080     } else {
1081         while (carry || vli_cmp(curve_p, result) != 1) {
1082             carry -= vli_sub(result, result, curve_p);
1083         }
1084     }
1085 }
1086 #elif uECC_WORD_SIZE == 4
1087 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
1088     uint32_t tmp[uECC_WORDS];
1089     int carry;
1090 
1091     /* t */
1092     vli_set(result, product);
1093 
1094     /* s1 */
1095     tmp[0] = tmp[1] = tmp[2] = 0;
1096     tmp[3] = product[11];
1097     tmp[4] = product[12];
1098     tmp[5] = product[13];
1099     tmp[6] = product[14];
1100     tmp[7] = product[15];
1101     carry = vli_add(tmp, tmp, tmp);
1102     carry += vli_add(result, result, tmp);
1103 
1104     /* s2 */
1105     tmp[3] = product[12];
1106     tmp[4] = product[13];
1107     tmp[5] = product[14];
1108     tmp[6] = product[15];
1109     tmp[7] = 0;
1110     carry += vli_add(tmp, tmp, tmp);
1111     carry += vli_add(result, result, tmp);
1112 
1113     /* s3 */
1114     tmp[0] = product[8];
1115     tmp[1] = product[9];
1116     tmp[2] = product[10];
1117     tmp[3] = tmp[4] = tmp[5] = 0;
1118     tmp[6] = product[14];
1119     tmp[7] = product[15];
1120     carry += vli_add(result, result, tmp);
1121 
1122     /* s4 */
1123     tmp[0] = product[9];
1124     tmp[1] = product[10];
1125     tmp[2] = product[11];
1126     tmp[3] = product[13];
1127     tmp[4] = product[14];
1128     tmp[5] = product[15];
1129     tmp[6] = product[13];
1130     tmp[7] = product[8];
1131     carry += vli_add(result, result, tmp);
1132 
1133     /* d1 */
1134     tmp[0] = product[11];
1135     tmp[1] = product[12];
1136     tmp[2] = product[13];
1137     tmp[3] = tmp[4] = tmp[5] = 0;
1138     tmp[6] = product[8];
1139     tmp[7] = product[10];
1140     carry -= vli_sub(result, result, tmp);
1141 
1142     /* d2 */
1143     tmp[0] = product[12];
1144     tmp[1] = product[13];
1145     tmp[2] = product[14];
1146     tmp[3] = product[15];
1147     tmp[4] = tmp[5] = 0;
1148     tmp[6] = product[9];
1149     tmp[7] = product[11];
1150     carry -= vli_sub(result, result, tmp);
1151 
1152     /* d3 */
1153     tmp[0] = product[13];
1154     tmp[1] = product[14];
1155     tmp[2] = product[15];
1156     tmp[3] = product[8];
1157     tmp[4] = product[9];
1158     tmp[5] = product[10];
1159     tmp[6] = 0;
1160     tmp[7] = product[12];
1161     carry -= vli_sub(result, result, tmp);
1162 
1163     /* d4 */
1164     tmp[0] = product[14];
1165     tmp[1] = product[15];
1166     tmp[2] = 0;
1167     tmp[3] = product[9];
1168     tmp[4] = product[10];
1169     tmp[5] = product[11];
1170     tmp[6] = 0;
1171     tmp[7] = product[13];
1172     carry -= vli_sub(result, result, tmp);
1173 
1174     if (carry < 0) {
1175         do {
1176             carry += vli_add(result, result, curve_p);
1177         } while (carry < 0);
1178     } else {
1179         while (carry || vli_cmp(curve_p, result) != 1) {
1180             carry -= vli_sub(result, result, curve_p);
1181         }
1182     }
1183 }
1184 #else
1185 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
1186     uint64_t tmp[uECC_WORDS];
1187     int carry;
1188 
1189     /* t */
1190     vli_set(result, product);
1191 
1192     /* s1 */
1193     tmp[0] = 0;
1194     tmp[1] = product[5] & 0xffffffff00000000ull;
1195     tmp[2] = product[6];
1196     tmp[3] = product[7];
1197     carry = vli_add(tmp, tmp, tmp);
1198     carry += vli_add(result, result, tmp);
1199 
1200     /* s2 */
1201     tmp[1] = product[6] << 32;
1202     tmp[2] = (product[6] >> 32) | (product[7] << 32);
1203     tmp[3] = product[7] >> 32;
1204     carry += vli_add(tmp, tmp, tmp);
1205     carry += vli_add(result, result, tmp);
1206 
1207     /* s3 */
1208     tmp[0] = product[4];
1209     tmp[1] = product[5] & 0xffffffff;
1210     tmp[2] = 0;
1211     tmp[3] = product[7];
1212     carry += vli_add(result, result, tmp);
1213 
1214     /* s4 */
1215     tmp[0] = (product[4] >> 32) | (product[5] << 32);
1216     tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
1217     tmp[2] = product[7];
1218     tmp[3] = (product[6] >> 32) | (product[4] << 32);
1219     carry += vli_add(result, result, tmp);
1220 
1221     /* d1 */
1222     tmp[0] = (product[5] >> 32) | (product[6] << 32);
1223     tmp[1] = (product[6] >> 32);
1224     tmp[2] = 0;
1225     tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
1226     carry -= vli_sub(result, result, tmp);
1227 
1228     /* d2 */
1229     tmp[0] = product[6];
1230     tmp[1] = product[7];
1231     tmp[2] = 0;
1232     tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
1233     carry -= vli_sub(result, result, tmp);
1234 
1235     /* d3 */
1236     tmp[0] = (product[6] >> 32) | (product[7] << 32);
1237     tmp[1] = (product[7] >> 32) | (product[4] << 32);
1238     tmp[2] = (product[4] >> 32) | (product[5] << 32);
1239     tmp[3] = (product[6] << 32);
1240     carry -= vli_sub(result, result, tmp);
1241 
1242     /* d4 */
1243     tmp[0] = product[7];
1244     tmp[1] = product[4] & 0xffffffff00000000ull;
1245     tmp[2] = product[5];
1246     tmp[3] = product[6] & 0xffffffff00000000ull;
1247     carry -= vli_sub(result, result, tmp);
1248 
1249     if (carry < 0) {
1250         do {
1251             carry += vli_add(result, result, curve_p);
1252         } while (carry < 0);
1253     } else {
1254         while (carry || vli_cmp(curve_p, result) != 1) {
1255             carry -= vli_sub(result, result, curve_p);
1256         }
1257     }
1258 }
1259 #endif /* uECC_WORD_SIZE */
1260 
1261 #elif uECC_CURVE == uECC_secp256k1
1262 
1263 #if uECC_WORD_SIZE == 1
1264 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
1265     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1266     uECC_word_t r0 = 0;
1267     uECC_word_t r1 = 0;
1268     uECC_word_t r2 = 0;
1269     wordcount_t k;
1270 
1271     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1272     muladd(0xD1, right[0], &r0, &r1, &r2);
1273     result[0] = r0;
1274     r0 = r1;
1275     r1 = r2;
1276     /* r2 is still 0 */
1277 
1278     for (k = 1; k < uECC_WORDS; ++k) {
1279         muladd(0x03, right[k - 1], &r0, &r1, &r2);
1280         muladd(0xD1, right[k], &r0, &r1, &r2);
1281         result[k] = r0;
1282         r0 = r1;
1283         r1 = r2;
1284         r2 = 0;
1285     }
1286     muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2);
1287     result[uECC_WORDS] = r0;
1288     result[uECC_WORDS + 1] = r1;
1289 
1290     result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */
1291 }
1292 #elif uECC_WORD_SIZE == 4
1293 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
1294     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1295     uint32_t carry = 0;
1296     wordcount_t k;
1297 
1298     for (k = 0; k < uECC_WORDS; ++k) {
1299         uint64_t p = (uint64_t)0x3D1 * right[k] + carry;
1300         result[k] = (p & 0xffffffff);
1301         carry = p >> 32;
1302     }
1303     result[uECC_WORDS] = carry;
1304 
1305     result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */
1306 }
1307 #else
1308 static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) {
1309     uECC_word_t r0 = 0;
1310     uECC_word_t r1 = 0;
1311     uECC_word_t r2 = 0;
1312     wordcount_t k;
1313 
1314     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1315     for (k = 0; k < uECC_WORDS; ++k) {
1316         muladd(0x1000003D1ull, right[k], &r0, &r1, &r2);
1317         result[k] = r0;
1318         r0 = r1;
1319         r1 = r2;
1320         r2 = 0;
1321     }
1322     result[uECC_WORDS] = r0;
1323 }
1324 #endif /* uECC_WORD_SIZE */
1325 
1326 #elif uECC_CURVE == uECC_secp224r1
1327 
1328 /* Computes result = product % curve_p
1329    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1330 #if uECC_WORD_SIZE == 1
1331 // TODO it may be faster to use the omega_mult method when fully asm optimized.
1332 void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
1333     uint8_t tmp[uECC_WORDS];
1334     int8_t carry;
1335 
1336     /* t */
1337     vli_set(result, product);
1338 
1339     /* s1 */
1340     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
1341     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
1342     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1343     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
1344     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
1345     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
1346     tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43];
1347     carry = vli_add(result, result, tmp);
1348 
1349     /* s2 */
1350     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
1351     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
1352     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
1353     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1354     carry += vli_add(result, result, tmp);
1355 
1356     /* d1 */
1357     tmp[0]  = product[28]; tmp[1]  = product[29]; tmp[2]  = product[30]; tmp[3]  = product[31];
1358     tmp[4]  = product[32]; tmp[5]  = product[33]; tmp[6]  = product[34]; tmp[7]  = product[35];
1359     tmp[8]  = product[36]; tmp[9]  = product[37]; tmp[10] = product[38]; tmp[11] = product[39];
1360     tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43];
1361     tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47];
1362     tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51];
1363     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1364     carry -= vli_sub(result, result, tmp);
1365 
1366     /* d2 */
1367     tmp[0]  = product[44]; tmp[1]  = product[45]; tmp[2]  = product[46]; tmp[3]  = product[47];
1368     tmp[4]  = product[48]; tmp[5]  = product[49]; tmp[6]  = product[50]; tmp[7]  = product[51];
1369     tmp[8]  = product[52]; tmp[9]  = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1370     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1371     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1372     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1373     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1374     carry -= vli_sub(result, result, tmp);
1375 
1376     if (carry < 0) {
1377         do {
1378             carry += vli_add(result, result, curve_p);
1379         } while (carry < 0);
1380     } else {
1381         while (carry || vli_cmp(curve_p, result) != 1) {
1382             carry -= vli_sub(result, result, curve_p);
1383         }
1384     }
1385 }
1386 #elif uECC_WORD_SIZE == 4
1387 void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product)
1388 {
1389     uint32_t tmp[uECC_WORDS];
1390     int carry;
1391 
1392     /* t */
1393     vli_set(result, product);
1394 
1395     /* s1 */
1396     tmp[0] = tmp[1] = tmp[2] = 0;
1397     tmp[3] = product[7];
1398     tmp[4] = product[8];
1399     tmp[5] = product[9];
1400     tmp[6] = product[10];
1401     carry = vli_add(result, result, tmp);
1402 
1403     /* s2 */
1404     tmp[3] = product[11];
1405     tmp[4] = product[12];
1406     tmp[5] = product[13];
1407     tmp[6] = 0;
1408     carry += vli_add(result, result, tmp);
1409 
1410     /* d1 */
1411     tmp[0] = product[7];
1412     tmp[1] = product[8];
1413     tmp[2] = product[9];
1414     tmp[3] = product[10];
1415     tmp[4] = product[11];
1416     tmp[5] = product[12];
1417     tmp[6] = product[13];
1418     carry -= vli_sub(result, result, tmp);
1419 
1420     /* d2 */
1421     tmp[0] = product[11];
1422     tmp[1] = product[12];
1423     tmp[2] = product[13];
1424     tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0;
1425     carry -= vli_sub(result, result, tmp);
1426 
1427     if (carry < 0) {
1428         do {
1429             carry += vli_add(result, result, curve_p);
1430         } while (carry < 0);
1431     } else {
1432         while (carry || vli_cmp(curve_p, result) != 1) {
1433             carry -= vli_sub(result, result, curve_p);
1434         }
1435     }
1436 }
1437 #endif /* uECC_WORD_SIZE */
1438 
1439 #endif /* uECC_CURVE */
1440 #endif /* !asm_mmod_fast */
1441 
1442 /* Computes result = (left * right) % curve_p. */
1443 static void vli_modMult_fast(uECC_word_t *result,
1444                              const uECC_word_t *left,
1445                              const uECC_word_t *right) {
1446     uECC_word_t product[2 * uECC_WORDS];
1447     vli_mult(product, left, right);
1448     vli_mmod_fast(result, product);
1449 }
1450 
1451 #if uECC_SQUARE_FUNC
1452 
1453 /* Computes result = left^2 % curve_p. */
1454 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) {
1455     uECC_word_t product[2 * uECC_WORDS];
1456     vli_square(product, left);
1457     vli_mmod_fast(result, product);
1458 }
1459 
1460 #else /* uECC_SQUARE_FUNC */
1461 
1462 #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left))
1463 
1464 #endif /* uECC_SQUARE_FUNC */
1465 
1466 
1467 #define EVEN(vli) (!(vli[0] & 1))
1468 /* Computes result = (1 / input) % mod. All VLIs are the same size.
1469    See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
1470    https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
1471 #if !asm_modInv
1472 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
1473     uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS];
1474     uECC_word_t carry;
1475     cmpresult_t cmpResult;
1476 
1477     if (vli_isZero(input)) {
1478         vli_clear(result);
1479         return;
1480     }
1481 
1482     vli_set(a, input);
1483     vli_set(b, mod);
1484     vli_clear(u);
1485     u[0] = 1;
1486     vli_clear(v);
1487     while ((cmpResult = vli_cmp(a, b)) != 0) {
1488         carry = 0;
1489         if (EVEN(a)) {
1490             vli_rshift1(a);
1491             if (!EVEN(u)) {
1492                 carry = vli_add(u, u, mod);
1493             }
1494             vli_rshift1(u);
1495             if (carry) {
1496                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1497             }
1498         } else if (EVEN(b)) {
1499             vli_rshift1(b);
1500             if (!EVEN(v)) {
1501                 carry = vli_add(v, v, mod);
1502             }
1503             vli_rshift1(v);
1504             if (carry) {
1505                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1506             }
1507         } else if (cmpResult > 0) {
1508             vli_sub(a, a, b);
1509             vli_rshift1(a);
1510             if (vli_cmp(u, v) < 0) {
1511                 vli_add(u, u, mod);
1512             }
1513             vli_sub(u, u, v);
1514             if (!EVEN(u)) {
1515                 carry = vli_add(u, u, mod);
1516             }
1517             vli_rshift1(u);
1518             if (carry) {
1519                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1520             }
1521         } else {
1522             vli_sub(b, b, a);
1523             vli_rshift1(b);
1524             if (vli_cmp(v, u) < 0) {
1525                 vli_add(v, v, mod);
1526             }
1527             vli_sub(v, v, u);
1528             if (!EVEN(v)) {
1529                 carry = vli_add(v, v, mod);
1530             }
1531             vli_rshift1(v);
1532             if (carry) {
1533                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1534             }
1535         }
1536     }
1537     vli_set(result, u);
1538 }
1539 #endif /* !asm_modInv */
1540 
1541 /* ------ Point operations ------ */
1542 
1543 /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */
1544 static cmpresult_t EccPoint_isZero(const EccPoint *point) {
1545     return (vli_isZero(point->x) && vli_isZero(point->y));
1546 }
1547 
1548 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
1549 From http://eprint.iacr.org/2011/338.pdf
1550 */
1551 
1552 /* Double in place */
1553 #if (uECC_CURVE == uECC_secp256k1)
1554 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1555                                      uECC_word_t * RESTRICT Y1,
1556                                      uECC_word_t * RESTRICT Z1) {
1557     /* t1 = X, t2 = Y, t3 = Z */
1558     uECC_word_t t4[uECC_WORDS];
1559     uECC_word_t t5[uECC_WORDS];
1560 
1561     if (vli_isZero(Z1)) {
1562         return;
1563     }
1564 
1565     vli_modSquare_fast(t5, Y1);   /* t5 = y1^2 */
1566     vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */
1567     vli_modSquare_fast(X1, X1);   /* t1 = x1^2 */
1568     vli_modSquare_fast(t5, t5);   /* t5 = y1^4 */
1569     vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */
1570 
1571     vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */
1572     vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */
1573     if (vli_testBit(Y1, 0)) {
1574         uECC_word_t carry = vli_add(Y1, Y1, curve_p);
1575         vli_rshift1(Y1);
1576         Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1);
1577     } else {
1578         vli_rshift1(Y1);
1579     }
1580     /* t2 = 3/2*(x1^2) = B */
1581 
1582     vli_modSquare_fast(X1, Y1);      /* t1 = B^2 */
1583     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */
1584     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */
1585 
1586     vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */
1587     vli_modMult_fast(Y1, Y1, t4);    /* t2 = B * (A - x3) */
1588     vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */
1589 }
1590 #else
1591 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1592                                      uECC_word_t * RESTRICT Y1,
1593                                      uECC_word_t * RESTRICT Z1) {
1594     /* t1 = X, t2 = Y, t3 = Z */
1595     uECC_word_t t4[uECC_WORDS];
1596     uECC_word_t t5[uECC_WORDS];
1597 
1598     if (vli_isZero(Z1)) {
1599         return;
1600     }
1601 
1602     vli_modSquare_fast(t4, Y1);   /* t4 = y1^2 */
1603     vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
1604     vli_modSquare_fast(t4, t4);   /* t4 = y1^4 */
1605     vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
1606     vli_modSquare_fast(Z1, Z1);   /* t3 = z1^2 */
1607 
1608     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
1609     vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
1610     vli_modSub_fast(Z1, X1, Z1);     /* t3 = x1 - z1^2 */
1611     vli_modMult_fast(X1, X1, Z1);    /* t1 = x1^2 - z1^4 */
1612 
1613     vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
1614     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
1615     if (vli_testBit(X1, 0)) {
1616         uECC_word_t l_carry = vli_add(X1, X1, curve_p);
1617         vli_rshift1(X1);
1618         X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1);
1619     } else {
1620         vli_rshift1(X1);
1621     }
1622     /* t1 = 3/2*(x1^2 - z1^4) = B */
1623 
1624     vli_modSquare_fast(Z1, X1);   /* t3 = B^2 */
1625     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - A */
1626     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - 2A = x3 */
1627     vli_modSub_fast(t5, t5, Z1);  /* t5 = A - x3 */
1628     vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
1629     vli_modSub_fast(t4, X1, t4);  /* t4 = B * (A - x3) - y1^4 = y3 */
1630 
1631     vli_set(X1, Z1);
1632     vli_set(Z1, Y1);
1633     vli_set(Y1, t4);
1634 }
1635 #endif
1636 
1637 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1638 static void apply_z(uECC_word_t * RESTRICT X1,
1639                     uECC_word_t * RESTRICT Y1,
1640                     const uECC_word_t * RESTRICT Z) {
1641     uECC_word_t t1[uECC_WORDS];
1642 
1643     vli_modSquare_fast(t1, Z);    /* z^2 */
1644     vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
1645     vli_modMult_fast(t1, t1, Z);  /* z^3 */
1646     vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
1647 }
1648 
1649 /* P = (x1, y1) => 2P, (x2, y2) => P' */
1650 static void XYcZ_initial_double(uECC_word_t * RESTRICT X1,
1651                                 uECC_word_t * RESTRICT Y1,
1652                                 uECC_word_t * RESTRICT X2,
1653                                 uECC_word_t * RESTRICT Y2,
1654                                 const uECC_word_t * RESTRICT initial_Z) {
1655     uECC_word_t z[uECC_WORDS];
1656     if (initial_Z) {
1657         vli_set(z, initial_Z);
1658     } else {
1659         vli_clear(z);
1660         z[0] = 1;
1661     }
1662 
1663     vli_set(X2, X1);
1664     vli_set(Y2, Y1);
1665 
1666     apply_z(X1, Y1, z);
1667     EccPoint_double_jacobian(X1, Y1, z);
1668     apply_z(X2, Y2, z);
1669 }
1670 
1671 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1672    Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1673    or P => P', Q => P + Q
1674 */
1675 static void XYcZ_add(uECC_word_t * RESTRICT X1,
1676                      uECC_word_t * RESTRICT Y1,
1677                      uECC_word_t * RESTRICT X2,
1678                      uECC_word_t * RESTRICT Y2) {
1679     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1680     uECC_word_t t5[uECC_WORDS];
1681 
1682     vli_modSub_fast(t5, X2, X1);  /* t5 = x2 - x1 */
1683     vli_modSquare_fast(t5, t5);   /* t5 = (x2 - x1)^2 = A */
1684     vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
1685     vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
1686     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y2 - y1 */
1687     vli_modSquare_fast(t5, Y2);   /* t5 = (y2 - y1)^2 = D */
1688 
1689     vli_modSub_fast(t5, t5, X1);  /* t5 = D - B */
1690     vli_modSub_fast(t5, t5, X2);  /* t5 = D - B - C = x3 */
1691     vli_modSub_fast(X2, X2, X1);  /* t3 = C - B */
1692     vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
1693     vli_modSub_fast(X2, X1, t5);  /* t3 = B - x3 */
1694     vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
1695     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y3 */
1696 
1697     vli_set(X2, t5);
1698 }
1699 
1700 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1701    Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1702    or P => P - Q, Q => P + Q
1703 */
1704 static void XYcZ_addC(uECC_word_t * RESTRICT X1,
1705                       uECC_word_t * RESTRICT Y1,
1706                       uECC_word_t * RESTRICT X2,
1707                       uECC_word_t * RESTRICT Y2) {
1708     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1709     uECC_word_t t5[uECC_WORDS];
1710     uECC_word_t t6[uECC_WORDS];
1711     uECC_word_t t7[uECC_WORDS];
1712 
1713     vli_modSub_fast(t5, X2, X1);     /* t5 = x2 - x1 */
1714     vli_modSquare_fast(t5, t5);      /* t5 = (x2 - x1)^2 = A */
1715     vli_modMult_fast(X1, X1, t5);    /* t1 = x1*A = B */
1716     vli_modMult_fast(X2, X2, t5);    /* t3 = x2*A = C */
1717     vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
1718     vli_modSub_fast(Y2, Y2, Y1);     /* t4 = y2 - y1 */
1719 
1720     vli_modSub_fast(t6, X2, X1);     /* t6 = C - B */
1721     vli_modMult_fast(Y1, Y1, t6);    /* t2 = y1 * (C - B) = E */
1722     vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
1723     vli_modSquare_fast(X2, Y2);      /* t3 = (y2 - y1)^2 = D */
1724     vli_modSub_fast(X2, X2, t6);     /* t3 = D - (B + C) = x3 */
1725 
1726     vli_modSub_fast(t7, X1, X2);  /* t7 = B - x3 */
1727     vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
1728     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = (y2 - y1)*(B - x3) - E = y3 */
1729 
1730     vli_modSquare_fast(t7, t5);   /* t7 = (y2 + y1)^2 = F */
1731     vli_modSub_fast(t7, t7, t6);  /* t7 = F - (B + C) = x3' */
1732     vli_modSub_fast(t6, t7, X1);  /* t6 = x3' - B */
1733     vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
1734     vli_modSub_fast(Y1, t6, Y1);  /* t2 = (y2 + y1)*(x3' - B) - E = y3' */
1735 
1736     vli_set(X1, t7);
1737 }
1738 
1739 static void EccPoint_mult(EccPoint * RESTRICT result,
1740                           const EccPoint * RESTRICT point,
1741                           const uECC_word_t * RESTRICT scalar,
1742                           const uECC_word_t * RESTRICT initialZ,
1743                           bitcount_t numBits) {
1744     /* R0 and R1 */
1745     uECC_word_t Rx[2][uECC_WORDS];
1746     uECC_word_t Ry[2][uECC_WORDS];
1747     uECC_word_t z[uECC_WORDS];
1748     bitcount_t i;
1749     uECC_word_t nb;
1750 
1751     vli_set(Rx[1], point->x);
1752     vli_set(Ry[1], point->y);
1753 
1754     XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ);
1755 
1756     for (i = numBits - 2; i > 0; --i) {
1757         nb = !vli_testBit(scalar, i);
1758         XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1759         XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1760     }
1761 
1762     nb = !vli_testBit(scalar, 0);
1763     XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1764 
1765     /* Find final 1/Z value. */
1766     vli_modSub_fast(z, Rx[1], Rx[0]);   /* X1 - X0 */
1767     vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
1768     vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */
1769     vli_modInv(z, z, curve_p);          /* 1 / (xP * Yb * (X1 - X0)) */
1770     vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */
1771     vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
1772     /* End 1/Z calculation */
1773 
1774     XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1775     apply_z(Rx[0], Ry[0], z);
1776 
1777     vli_set(result->x, Rx[0]);
1778     vli_set(result->y, Ry[0]);
1779 }
1780 
1781 static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) {
1782     uECC_word_t tmp1[uECC_WORDS];
1783     uECC_word_t tmp2[uECC_WORDS];
1784     uECC_word_t *p2[2] = {tmp1, tmp2};
1785     uECC_word_t carry;
1786 
1787     /* Make sure the private key is in the range [1, n-1]. */
1788     if (vli_isZero(private)) {
1789         return 0;
1790     }
1791 
1792 #if (uECC_CURVE == uECC_secp160r1)
1793     // Don't regularize the bitcount for secp160r1, since it would have a larger performance
1794     // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to
1795     // a significant increase in code size.
1796 
1797     EccPoint_mult(result, &curve_G, private, 0, vli_numBits(private, uECC_WORDS));
1798 #else
1799     if (vli_cmp(curve_n, private) != 1) {
1800         return 0;
1801     }
1802 
1803     // Regularize the bitcount for the private key so that attackers cannot use a side channel
1804     // attack to learn the number of leading zeros.
1805     carry = vli_add(tmp1, private, curve_n);
1806     vli_add(tmp2, tmp1, curve_n);
1807     EccPoint_mult(result, &curve_G, p2[!carry], 0, (uECC_BYTES * 8) + 1);
1808 #endif
1809 
1810     if (EccPoint_isZero(result)) {
1811         return 0;
1812     }
1813     return 1;
1814 }
1815 
1816 #if uECC_CURVE == uECC_secp224r1
1817 
1818 /* Routine 3.2.4 RS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1819 static void mod_sqrt_secp224r1_rs(uECC_word_t *d1,
1820                                   uECC_word_t *e1,
1821                                   uECC_word_t *f1,
1822                                   const uECC_word_t *d0,
1823                                   const uECC_word_t *e0,
1824                                   const uECC_word_t *f0) {
1825     uECC_word_t t[uECC_WORDS];
1826 
1827     vli_modSquare_fast(t, d0);                 /* t <-- d0 ^ 2 */
1828     vli_modMult_fast(e1, d0, e0);              /* e1 <-- d0 * e0 */
1829     vli_modAdd(d1, t, f0, curve_p);            /* d1 <-- t  + f0 */
1830     vli_modAdd(e1, e1, e1, curve_p);           /* e1 <-- e1 + e1 */
1831     vli_modMult_fast(f1, t, f0);               /* f1 <-- t  * f0 */
1832     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1833     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1834 }
1835 
1836 /* Routine 3.2.5 RSS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1837 static void mod_sqrt_secp224r1_rss(uECC_word_t *d1,
1838                                    uECC_word_t *e1,
1839                                    uECC_word_t *f1,
1840                                    const uECC_word_t *d0,
1841                                    const uECC_word_t *e0,
1842                                    const uECC_word_t *f0,
1843                                    const bitcount_t j) {
1844     bitcount_t i;
1845 
1846     vli_set(d1, d0);                           /* d1 <-- d0 */
1847     vli_set(e1, e0);                           /* e1 <-- e0 */
1848     vli_set(f1, f0);                           /* f1 <-- f0 */
1849     for (i = 1; i <= j; i++) {
1850         mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */
1851     }
1852 }
1853 
1854 /* Routine 3.2.6 RM;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1855 static void mod_sqrt_secp224r1_rm(uECC_word_t *d2,
1856                                   uECC_word_t *e2,
1857                                   uECC_word_t *f2,
1858                                   const uECC_word_t *c,
1859                                   const uECC_word_t *d0,
1860                                   const uECC_word_t *e0,
1861                                   const uECC_word_t *d1,
1862                                   const uECC_word_t *e1) {
1863     uECC_word_t t1[uECC_WORDS];
1864     uECC_word_t t2[uECC_WORDS];
1865 
1866     vli_modMult_fast(t1, e0, e1);              /* t1 <-- e0 * e1 */
1867     vli_modMult_fast(t1, t1, c);               /* t1 <-- t1 * c */
1868     vli_modSub_fast(t1, curve_p, t1);          /* t1 <-- p  - t1 */
1869     vli_modMult_fast(t2, d0, d1);              /* t2 <-- d0 * d1 */
1870     vli_modAdd(t2, t2, t1, curve_p);           /* t2 <-- t2 + t1 */
1871     vli_modMult_fast(t1, d0, e1);              /* t1 <-- d0 * e1 */
1872     vli_modMult_fast(e2, d1, e0);              /* e2 <-- d1 * e0 */
1873     vli_modAdd(e2, e2, t1, curve_p);           /* e2 <-- e2 + t1 */
1874     vli_modSquare_fast(f2, e2);                /* f2 <-- e2^2 */
1875     vli_modMult_fast(f2, f2, c);               /* f2 <-- f2 * c */
1876     vli_modSub_fast(f2, curve_p, f2);          /* f2 <-- p  - f2 */
1877     vli_set(d2, t2);                           /* d2 <-- t2 */
1878 }
1879 
1880 /* Routine 3.2.7 RP;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1881 static void mod_sqrt_secp224r1_rp(uECC_word_t *d1,
1882                                   uECC_word_t *e1,
1883                                   uECC_word_t *f1,
1884                                   const uECC_word_t *c,
1885                                   const uECC_word_t *r) {
1886     wordcount_t i;
1887     wordcount_t pow2i = 1;
1888     uECC_word_t d0[uECC_WORDS];
1889     uECC_word_t e0[uECC_WORDS] = {1};          /* e0 <-- 1 */
1890     uECC_word_t f0[uECC_WORDS];
1891 
1892     vli_set(d0, r);                            /* d0 <-- r */
1893     vli_modSub_fast(f0, curve_p, c);           /* f0 <-- p  - c */
1894     for (i = 0; i <= 6; i++) {
1895         mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */
1896         mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0);  /* RM (d1,e1,f1,c,d1,e1,d0,e0) */
1897         vli_set(d0, d1);                       /* d0 <-- d1 */
1898         vli_set(e0, e1);                       /* e0 <-- e1 */
1899         vli_set(f0, f1);                       /* f0 <-- f1 */
1900         pow2i *= 2;
1901     }
1902 }
1903 
1904 /* Compute a = sqrt(a) (mod curve_p). */
1905 /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1906 static void mod_sqrt(uECC_word_t *a) {
1907     bitcount_t i;
1908     uECC_word_t e1[uECC_WORDS];
1909     uECC_word_t f1[uECC_WORDS];
1910     uECC_word_t d0[uECC_WORDS];
1911     uECC_word_t e0[uECC_WORDS];
1912     uECC_word_t f0[uECC_WORDS];
1913     uECC_word_t d1[uECC_WORDS];
1914 
1915     // s = a; using constant instead of random value
1916     mod_sqrt_secp224r1_rp(d0, e0, f0, a, a);           /* RP (d0, e0, f0, c, s) */
1917     mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0);     /* RS (d1, e1, f1, d0, e0, f0) */
1918     for (i = 1; i <= 95; i++) {
1919         vli_set(d0, d1);                               /* d0 <-- d1 */
1920         vli_set(e0, e1);                               /* e0 <-- e1 */
1921         vli_set(f0, f1);                               /* f0 <-- f1 */
1922         mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */
1923         if (vli_isZero(d1)) {                          /* if d1 == 0 */
1924 	        break;
1925         }
1926     }
1927     vli_modInv(f1, e0, curve_p);                       /* f1 <-- 1 / e0 */
1928     vli_modMult_fast(a, d0, f1);                       /* a  <-- d0 / e0 */
1929 }
1930 
1931 #else /* uECC_CURVE */
1932 
1933 /* Compute a = sqrt(a) (mod curve_p). */
1934 static void mod_sqrt(uECC_word_t *a) {
1935     bitcount_t i;
1936     uECC_word_t p1[uECC_WORDS] = {1};
1937     uECC_word_t l_result[uECC_WORDS] = {1};
1938 
1939     /* Since curve_p == 3 (mod 4) for all supported curves, we can
1940        compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
1941     vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
1942     for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) {
1943         vli_modSquare_fast(l_result, l_result);
1944         if (vli_testBit(p1, i)) {
1945             vli_modMult_fast(l_result, l_result, a);
1946         }
1947     }
1948     vli_set(a, l_result);
1949 }
1950 #endif /* uECC_CURVE */
1951 
1952 #if uECC_WORD_SIZE == 1
1953 
1954 static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) {
1955     uint8_t i;
1956     for (i = 0; i < uECC_BYTES; ++i) {
1957         dest[i] = src[(uECC_BYTES - 1) - i];
1958     }
1959 }
1960 
1961 #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src))
1962 
1963 #elif uECC_WORD_SIZE == 4
1964 
1965 static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) {
1966     unsigned i;
1967     for (i = 0; i < uECC_WORDS; ++i) {
1968         uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
1969         digit[0] = native[i] >> 24;
1970         digit[1] = native[i] >> 16;
1971         digit[2] = native[i] >> 8;
1972         digit[3] = native[i];
1973     }
1974 }
1975 
1976 static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) {
1977     unsigned i;
1978     for (i = 0; i < uECC_WORDS; ++i) {
1979         const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
1980         native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) |
1981                     ((uint32_t)digit[2] << 8) | (uint32_t)digit[3];
1982     }
1983 }
1984 
1985 #else
1986 
1987 static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) {
1988     unsigned i;
1989     for (i = 0; i < uECC_WORDS; ++i) {
1990         uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
1991         digit[0] = native[i] >> 56;
1992         digit[1] = native[i] >> 48;
1993         digit[2] = native[i] >> 40;
1994         digit[3] = native[i] >> 32;
1995         digit[4] = native[i] >> 24;
1996         digit[5] = native[i] >> 16;
1997         digit[6] = native[i] >> 8;
1998         digit[7] = native[i];
1999     }
2000 }
2001 
2002 static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) {
2003     unsigned i;
2004     for (i = 0; i < uECC_WORDS; ++i) {
2005         const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
2006         native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) |
2007                     ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) |
2008                     ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) |
2009                     ((uint64_t)digit[6] << 8) | (uint64_t)digit[7];
2010     }
2011 }
2012 
2013 #endif /* uECC_WORD_SIZE */
2014 
2015 int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) {
2016     uECC_word_t private[uECC_WORDS];
2017     EccPoint public;
2018     uECC_word_t tries;
2019     for (tries = 0; tries < MAX_TRIES; ++tries) {
2020         if (g_rng_function((uint8_t *)private, sizeof(private)) &&
2021                 EccPoint_compute_public_key(&public, private)) {
2022             vli_nativeToBytes(private_key, private);
2023             vli_nativeToBytes(public_key, public.x);
2024             vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2025             return 1;
2026         }
2027     }
2028     return 0;
2029 }
2030 
2031 int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2],
2032                        const uint8_t private_key[uECC_BYTES],
2033                        uint8_t secret[uECC_BYTES]) {
2034     EccPoint public;
2035     EccPoint product;
2036     uECC_word_t private[uECC_WORDS];
2037     uECC_word_t tmp[uECC_WORDS];
2038     uECC_word_t *p2[2] = {private, tmp};
2039     uECC_word_t random[uECC_WORDS];
2040     uECC_word_t *initial_Z = 0;
2041     uECC_word_t tries;
2042     uECC_word_t carry;
2043 
2044     // Try to get a random initial Z value to improve protection against side-channel
2045     // attacks. If the RNG fails every time (eg it was not defined), we continue so that
2046     // uECC_shared_secret() can still work without an RNG defined.
2047     for (tries = 0; tries < MAX_TRIES; ++tries) {
2048         if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) {
2049             initial_Z = random;
2050             break;
2051         }
2052     }
2053 
2054     vli_bytesToNative(private, private_key);
2055     vli_bytesToNative(public.x, public_key);
2056     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2057 
2058 #if (uECC_CURVE == uECC_secp160r1)
2059     // Don't regularize the bitcount for secp160r1.
2060     EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS));
2061 #else
2062     // Regularize the bitcount for the private key so that attackers cannot use a side channel
2063     // attack to learn the number of leading zeros.
2064     carry = vli_add(private, private, curve_n);
2065     vli_add(tmp, private, curve_n);
2066     EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1);
2067 #endif
2068 
2069     vli_nativeToBytes(secret, product.x);
2070     return !EccPoint_isZero(&product);
2071 }
2072 
2073 void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) {
2074     wordcount_t i;
2075     for (i = 0; i < uECC_BYTES; ++i) {
2076         compressed[i+1] = public_key[i];
2077     }
2078     compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01);
2079 }
2080 
2081 /* Computes result = x^3 + ax + b. result must not overlap x. */
2082 static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) {
2083 #if (uECC_CURVE == uECC_secp256k1)
2084     vli_modSquare_fast(result, x); /* r = x^2 */
2085     vli_modMult_fast(result, result, x); /* r = x^3 */
2086     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */
2087 #else
2088     uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */
2089 
2090     vli_modSquare_fast(result, x); /* r = x^2 */
2091     vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */
2092     vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
2093     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */
2094 #endif
2095 }
2096 
2097 void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) {
2098     EccPoint point;
2099     vli_bytesToNative(point.x, compressed + 1);
2100     curve_x_side(point.y, point.x);
2101     mod_sqrt(point.y);
2102 
2103     if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) {
2104         vli_sub(point.y, curve_p, point.y);
2105     }
2106 
2107     vli_nativeToBytes(public_key, point.x);
2108     vli_nativeToBytes(public_key + uECC_BYTES, point.y);
2109 }
2110 
2111 int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) {
2112     uECC_word_t tmp1[uECC_WORDS];
2113     uECC_word_t tmp2[uECC_WORDS];
2114     EccPoint public;
2115 
2116     vli_bytesToNative(public.x, public_key);
2117     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2118 
2119     // The point at infinity is invalid.
2120     if (EccPoint_isZero(&public)) {
2121         return 0;
2122     }
2123 
2124     // x and y must be smaller than p.
2125     if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) {
2126         return 0;
2127     }
2128 
2129     vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */
2130     curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */
2131 
2132     /* Make sure that y^2 == x^3 + ax + b */
2133     return (vli_cmp(tmp1, tmp2) == 0);
2134 }
2135 
2136 int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES],
2137                             uint8_t public_key[uECC_BYTES * 2]) {
2138     uECC_word_t private[uECC_WORDS];
2139     EccPoint public;
2140 
2141     vli_bytesToNative(private, private_key);
2142 
2143     if (!EccPoint_compute_public_key(&public, private)) {
2144         return 0;
2145     }
2146 
2147     vli_nativeToBytes(public_key, public.x);
2148     vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2149     return 1;
2150 }
2151 
2152 int uECC_bytes(void) {
2153     return uECC_BYTES;
2154 }
2155 
2156 int uECC_curve(void) {
2157     return uECC_CURVE;
2158 }
2159 
2160 /* -------- ECDSA code -------- */
2161 
2162 #if (uECC_CURVE == uECC_secp160r1)
2163 static void vli_clear_n(uECC_word_t *vli) {
2164     vli_clear(vli);
2165     vli[uECC_N_WORDS - 1] = 0;
2166 }
2167 
2168 static uECC_word_t vli_isZero_n(const uECC_word_t *vli) {
2169     if (vli[uECC_N_WORDS - 1]) {
2170         return 0;
2171     }
2172     return vli_isZero(vli);
2173 }
2174 
2175 static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) {
2176     vli_set(dest, src);
2177     dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1];
2178 }
2179 
2180 static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) {
2181     if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) {
2182         return 1;
2183     } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) {
2184         return -1;
2185     }
2186     return vli_cmp(left, right);
2187 }
2188 
2189 static void vli_rshift1_n(uECC_word_t *vli) {
2190     vli_rshift1(vli);
2191     vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1);
2192     vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1;
2193 }
2194 
2195 static uECC_word_t vli_add_n(uECC_word_t *result,
2196                              const uECC_word_t *left,
2197                              const uECC_word_t *right) {
2198     uECC_word_t carry = vli_add(result, left, right);
2199     uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry;
2200     if (sum != left[uECC_N_WORDS - 1]) {
2201         carry = (sum < left[uECC_N_WORDS - 1]);
2202     }
2203     result[uECC_N_WORDS - 1] = sum;
2204     return carry;
2205 }
2206 
2207 static uECC_word_t vli_sub_n(uECC_word_t *result,
2208                              const uECC_word_t *left,
2209                              const uECC_word_t *right) {
2210     uECC_word_t borrow = vli_sub(result, left, right);
2211     uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow;
2212     if (diff != left[uECC_N_WORDS - 1]) {
2213         borrow = (diff > left[uECC_N_WORDS - 1]);
2214     }
2215     result[uECC_N_WORDS - 1] = diff;
2216     return borrow;
2217 }
2218 
2219 #if !muladd_exists
2220 static void muladd(uECC_word_t a,
2221                    uECC_word_t b,
2222                    uECC_word_t *r0,
2223                    uECC_word_t *r1,
2224                    uECC_word_t *r2) {
2225     uECC_dword_t p = (uECC_dword_t)a * b;
2226     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
2227     r01 += p;
2228     *r2 += (r01 < p);
2229     *r1 = r01 >> uECC_WORD_BITS;
2230     *r0 = (uECC_word_t)r01;
2231 }
2232 #define muladd_exists 1
2233 #endif
2234 
2235 static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2236     uECC_word_t r0 = 0;
2237     uECC_word_t r1 = 0;
2238     uECC_word_t r2 = 0;
2239     wordcount_t i, k;
2240 
2241     for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) {
2242         wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS);
2243         wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1);
2244         for (i = min; i <= max; ++i) {
2245             muladd(left[i], right[k - i], &r0, &r1, &r2);
2246         }
2247         result[k] = r0;
2248         r0 = r1;
2249         r1 = r2;
2250         r2 = 0;
2251     }
2252     result[uECC_N_WORDS * 2 - 1] = r0;
2253 }
2254 
2255 static void vli_modAdd_n(uECC_word_t *result,
2256                          const uECC_word_t *left,
2257                          const uECC_word_t *right,
2258                          const uECC_word_t *mod) {
2259     uECC_word_t carry = vli_add_n(result, left, right);
2260     if (carry || vli_cmp_n(result, mod) >= 0) {
2261         vli_sub_n(result, result, mod);
2262     }
2263 }
2264 
2265 static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
2266     uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS];
2267     uECC_word_t carry;
2268     cmpresult_t cmpResult;
2269 
2270     if (vli_isZero_n(input)) {
2271         vli_clear_n(result);
2272         return;
2273     }
2274 
2275     vli_set_n(a, input);
2276     vli_set_n(b, mod);
2277     vli_clear_n(u);
2278     u[0] = 1;
2279     vli_clear_n(v);
2280     while ((cmpResult = vli_cmp_n(a, b)) != 0) {
2281         carry = 0;
2282         if (EVEN(a)) {
2283             vli_rshift1_n(a);
2284             if (!EVEN(u)) {
2285                 carry = vli_add_n(u, u, mod);
2286             }
2287             vli_rshift1_n(u);
2288             if (carry) {
2289                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2290             }
2291         } else if (EVEN(b)) {
2292             vli_rshift1_n(b);
2293             if (!EVEN(v)) {
2294                 carry = vli_add_n(v, v, mod);
2295             }
2296             vli_rshift1_n(v);
2297             if (carry) {
2298                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2299             }
2300         } else if (cmpResult > 0) {
2301             vli_sub_n(a, a, b);
2302             vli_rshift1_n(a);
2303             if (vli_cmp_n(u, v) < 0) {
2304                 vli_add_n(u, u, mod);
2305             }
2306             vli_sub_n(u, u, v);
2307             if (!EVEN(u)) {
2308                 carry = vli_add_n(u, u, mod);
2309             }
2310             vli_rshift1_n(u);
2311             if (carry) {
2312                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2313             }
2314         } else {
2315             vli_sub_n(b, b, a);
2316             vli_rshift1_n(b);
2317             if (vli_cmp_n(v, u) < 0) {
2318                 vli_add_n(v, v, mod);
2319             }
2320             vli_sub_n(v, v, u);
2321             if (!EVEN(v)) {
2322                 carry = vli_add_n(v, v, mod);
2323             }
2324             vli_rshift1_n(v);
2325             if (carry) {
2326                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2327             }
2328         }
2329     }
2330     vli_set_n(result, u);
2331 }
2332 
2333 static void vli2_rshift1_n(uECC_word_t *vli) {
2334     vli_rshift1_n(vli);
2335     vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1);
2336     vli_rshift1_n(vli + uECC_N_WORDS);
2337 }
2338 
2339 static uECC_word_t vli2_sub_n(uECC_word_t *result,
2340                               const uECC_word_t *left,
2341                               const uECC_word_t *right) {
2342     uECC_word_t borrow = 0;
2343     wordcount_t i;
2344     for (i = 0; i < uECC_N_WORDS * 2; ++i) {
2345         uECC_word_t diff = left[i] - right[i] - borrow;
2346         if (diff != left[i]) {
2347             borrow = (diff > left[i]);
2348         }
2349         result[i] = diff;
2350     }
2351     return borrow;
2352 }
2353 
2354 /* Computes result = (left * right) % curve_n. */
2355 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2356     bitcount_t i;
2357     uECC_word_t product[2 * uECC_N_WORDS];
2358     uECC_word_t modMultiple[2 * uECC_N_WORDS];
2359     uECC_word_t tmp[2 * uECC_N_WORDS];
2360     uECC_word_t *v[2] = {tmp, product};
2361     uECC_word_t index = 1;
2362 
2363     vli_mult_n(product, left, right);
2364     vli_clear_n(modMultiple);
2365     vli_set(modMultiple + uECC_N_WORDS + 1, curve_n);
2366     vli_rshift1(modMultiple + uECC_N_WORDS + 1);
2367     modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2368     modMultiple[uECC_N_WORDS] = HIGH_BIT_SET;
2369 
2370     for (i = 0;
2371          i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1));
2372          ++i) {
2373         uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple);
2374         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2375         vli2_rshift1_n(modMultiple);
2376     }
2377     vli_set_n(result, v[index]);
2378 }
2379 
2380 #else
2381 
2382 #define vli_cmp_n vli_cmp
2383 #define vli_modInv_n vli_modInv
2384 #define vli_modAdd_n vli_modAdd
2385 
2386 static void vli2_rshift1(uECC_word_t *vli) {
2387     vli_rshift1(vli);
2388     vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1);
2389     vli_rshift1(vli + uECC_WORDS);
2390 }
2391 
2392 static uECC_word_t vli2_sub(uECC_word_t *result,
2393                             const uECC_word_t *left,
2394                             const uECC_word_t *right) {
2395     uECC_word_t borrow = 0;
2396     wordcount_t i;
2397     for (i = 0; i < uECC_WORDS * 2; ++i) {
2398         uECC_word_t diff = left[i] - right[i] - borrow;
2399         if (diff != left[i]) {
2400             borrow = (diff > left[i]);
2401         }
2402         result[i] = diff;
2403     }
2404     return borrow;
2405 }
2406 
2407 /* Computes result = (left * right) % curve_n. */
2408 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2409     uECC_word_t product[2 * uECC_WORDS];
2410     uECC_word_t modMultiple[2 * uECC_WORDS];
2411     uECC_word_t tmp[2 * uECC_WORDS];
2412     uECC_word_t *v[2] = {tmp, product};
2413     bitcount_t i;
2414     uECC_word_t index = 1;
2415 
2416     vli_mult(product, left, right);
2417     vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */
2418     vli_clear(modMultiple);
2419 
2420     for (i = 0; i <= uECC_BYTES * 8; ++i) {
2421         uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple);
2422         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2423         vli2_rshift1(modMultiple);
2424     }
2425     vli_set(result, v[index]);
2426 }
2427 #endif /* (uECC_CURVE != uECC_secp160r1) */
2428 
2429 static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES],
2430                             const uint8_t message_hash[uECC_BYTES],
2431                             uECC_word_t k[uECC_N_WORDS],
2432                             uint8_t signature[uECC_BYTES*2]) {
2433     uECC_word_t tmp[uECC_N_WORDS];
2434     uECC_word_t s[uECC_N_WORDS];
2435     uECC_word_t *k2[2] = {tmp, s};
2436     EccPoint p;
2437     uECC_word_t carry;
2438     uECC_word_t tries;
2439 
2440     /* Make sure 0 < k < curve_n */
2441     if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) {
2442         return 0;
2443     }
2444 
2445 #if (uECC_CURVE == uECC_secp160r1)
2446     /* Make sure that we don't leak timing information about k.
2447        See http://eprint.iacr.org/2011/232.pdf */
2448     vli_add_n(tmp, k, curve_n);
2449     carry = (tmp[uECC_WORDS] & 0x02);
2450     vli_add_n(s, tmp, curve_n);
2451 
2452     /* p = k * G */
2453     EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 2);
2454 #else
2455     /* Make sure that we don't leak timing information about k.
2456        See http://eprint.iacr.org/2011/232.pdf */
2457     carry = vli_add(tmp, k, curve_n);
2458     vli_add(s, tmp, curve_n);
2459 
2460     /* p = k * G */
2461     EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 1);
2462 
2463     /* r = x1 (mod n) */
2464     if (vli_cmp(curve_n, p.x) != 1) {
2465         vli_sub(p.x, p.x, curve_n);
2466     }
2467 #endif
2468     if (vli_isZero(p.x)) {
2469         return 0;
2470     }
2471 
2472     // Attempt to get a random number to prevent side channel analysis of k.
2473     // If the RNG fails every time (eg it was not defined), we continue so that
2474     // deterministic signing can still work (with reduced security) without
2475     // an RNG defined.
2476     carry = 0; // use to signal that the RNG succeeded at least once.
2477     for (tries = 0; tries < MAX_TRIES; ++tries) {
2478         if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) {
2479             continue;
2480         }
2481         carry = 1;
2482         if (!vli_isZero(tmp)) {
2483             break;
2484         }
2485     }
2486     if (!carry) {
2487         vli_clear(tmp);
2488         tmp[0] = 1;
2489     }
2490 
2491     /* Prevent side channel analysis of vli_modInv() to determine
2492        bits of k / the private key by premultiplying by a random number */
2493     vli_modMult_n(k, k, tmp); /* k' = rand * k */
2494     vli_modInv_n(k, k, curve_n); /* k = 1 / k' */
2495     vli_modMult_n(k, k, tmp); /* k = 1 / k */
2496 
2497     vli_nativeToBytes(signature, p.x); /* store r */
2498 
2499     tmp[uECC_N_WORDS - 1] = 0;
2500     vli_bytesToNative(tmp, private_key); /* tmp = d */
2501     s[uECC_N_WORDS - 1] = 0;
2502     vli_set(s, p.x);
2503     vli_modMult_n(s, tmp, s); /* s = r*d */
2504 
2505     vli_bytesToNative(tmp, message_hash);
2506     vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */
2507     vli_modMult_n(s, s, k); /* s = (e + r*d) / k */
2508 #if (uECC_CURVE == uECC_secp160r1)
2509     if (s[uECC_N_WORDS - 1]) {
2510         return 0;
2511     }
2512 #endif
2513     vli_nativeToBytes(signature + uECC_BYTES, s);
2514     return 1;
2515 }
2516 
2517 int uECC_sign(const uint8_t private_key[uECC_BYTES],
2518               const uint8_t message_hash[uECC_BYTES],
2519               uint8_t signature[uECC_BYTES*2]) {
2520     uECC_word_t k[uECC_N_WORDS];
2521     uECC_word_t tries;
2522 
2523     for (tries = 0; tries < MAX_TRIES; ++tries) {
2524         if(g_rng_function((uint8_t *)k, sizeof(k))) {
2525         #if (uECC_CURVE == uECC_secp160r1)
2526             k[uECC_WORDS] &= 0x01;
2527         #endif
2528             if (uECC_sign_with_k(private_key, message_hash, k, signature)) {
2529                 return 1;
2530             }
2531         }
2532     }
2533     return 0;
2534 }
2535 
2536 /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
2537    the same size as the hash result size. */
2538 static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) {
2539     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2540     unsigned i;
2541     for (i = 0; i < hash_context->result_size; ++i)
2542         pad[i] = K[i] ^ 0x36;
2543     for (; i < hash_context->block_size; ++i)
2544         pad[i] = 0x36;
2545 
2546     hash_context->init_hash(hash_context);
2547     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2548 }
2549 
2550 static void HMAC_update(uECC_HashContext *hash_context,
2551                         const uint8_t *message,
2552                         unsigned message_size) {
2553     hash_context->update_hash(hash_context, message, message_size);
2554 }
2555 
2556 static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) {
2557     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2558     unsigned i;
2559     for (i = 0; i < hash_context->result_size; ++i)
2560         pad[i] = K[i] ^ 0x5c;
2561     for (; i < hash_context->block_size; ++i)
2562         pad[i] = 0x5c;
2563 
2564     hash_context->finish_hash(hash_context, result);
2565 
2566     hash_context->init_hash(hash_context);
2567     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2568     hash_context->update_hash(hash_context, result, hash_context->result_size);
2569     hash_context->finish_hash(hash_context, result);
2570 }
2571 
2572 /* V = HMAC_K(V) */
2573 static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) {
2574     HMAC_init(hash_context, K);
2575     HMAC_update(hash_context, V, hash_context->result_size);
2576     HMAC_finish(hash_context, K, V);
2577 }
2578 
2579 /* Deterministic signing, similar to RFC 6979. Differences are:
2580     * We just use (truncated) H(m) directly rather than bits2octets(H(m))
2581       (it is not reduced modulo curve_n).
2582     * We generate a value for k (aka T) directly rather than converting endianness.
2583 
2584    Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */
2585 int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES],
2586                             const uint8_t message_hash[uECC_BYTES],
2587                             uECC_HashContext *hash_context,
2588                             uint8_t signature[uECC_BYTES*2]) {
2589     uint8_t *K = hash_context->tmp;
2590     uint8_t *V = K + hash_context->result_size;
2591     uECC_word_t tries;
2592     unsigned i;
2593     for (i = 0; i < hash_context->result_size; ++i) {
2594         V[i] = 0x01;
2595         K[i] = 0;
2596     }
2597 
2598     // K = HMAC_K(V || 0x00 || int2octets(x) || h(m))
2599     HMAC_init(hash_context, K);
2600     V[hash_context->result_size] = 0x00;
2601     HMAC_update(hash_context, V, hash_context->result_size + 1);
2602     HMAC_update(hash_context, private_key, uECC_BYTES);
2603     HMAC_update(hash_context, message_hash, uECC_BYTES);
2604     HMAC_finish(hash_context, K, K);
2605 
2606     update_V(hash_context, K, V);
2607 
2608     // K = HMAC_K(V || 0x01 || int2octets(x) || h(m))
2609     HMAC_init(hash_context, K);
2610     V[hash_context->result_size] = 0x01;
2611     HMAC_update(hash_context, V, hash_context->result_size + 1);
2612     HMAC_update(hash_context, private_key, uECC_BYTES);
2613     HMAC_update(hash_context, message_hash, uECC_BYTES);
2614     HMAC_finish(hash_context, K, K);
2615 
2616     update_V(hash_context, K, V);
2617 
2618     for (tries = 0; tries < MAX_TRIES; ++tries) {
2619         uECC_word_t T[uECC_N_WORDS];
2620         uint8_t *T_ptr = (uint8_t *)T;
2621         unsigned T_bytes = 0;
2622         while (T_bytes < sizeof(T)) {
2623             update_V(hash_context, K, V);
2624             for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) {
2625                 T_ptr[T_bytes] = V[i];
2626             }
2627         }
2628     #if (uECC_CURVE == uECC_secp160r1)
2629         T[uECC_WORDS] &= 0x01;
2630     #endif
2631 
2632         if (uECC_sign_with_k(private_key, message_hash, T, signature)) {
2633             return 1;
2634         }
2635 
2636         // K = HMAC_K(V || 0x00)
2637         HMAC_init(hash_context, K);
2638         V[hash_context->result_size] = 0x00;
2639         HMAC_update(hash_context, V, hash_context->result_size + 1);
2640         HMAC_finish(hash_context, K, K);
2641 
2642         update_V(hash_context, K, V);
2643     }
2644     return 0;
2645 }
2646 
2647 static bitcount_t smax(bitcount_t a, bitcount_t b) {
2648     return (a > b ? a : b);
2649 }
2650 
2651 int uECC_verify(const uint8_t public_key[uECC_BYTES*2],
2652                 const uint8_t hash[uECC_BYTES],
2653                 const uint8_t signature[uECC_BYTES*2]) {
2654     uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS];
2655     uECC_word_t z[uECC_N_WORDS];
2656     EccPoint public, sum;
2657     uECC_word_t rx[uECC_WORDS];
2658     uECC_word_t ry[uECC_WORDS];
2659     uECC_word_t tx[uECC_WORDS];
2660     uECC_word_t ty[uECC_WORDS];
2661     uECC_word_t tz[uECC_WORDS];
2662     const EccPoint *points[4];
2663     const EccPoint *point;
2664     bitcount_t numBits;
2665     bitcount_t i;
2666     uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS];
2667     r[uECC_N_WORDS - 1] = 0;
2668     s[uECC_N_WORDS - 1] = 0;
2669 
2670     vli_bytesToNative(public.x, public_key);
2671     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2672     vli_bytesToNative(r, signature);
2673     vli_bytesToNative(s, signature + uECC_BYTES);
2674 
2675     if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */
2676         return 0;
2677     }
2678 
2679 #if (uECC_CURVE != uECC_secp160r1)
2680     if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */
2681         return 0;
2682     }
2683 #endif
2684 
2685     /* Calculate u1 and u2. */
2686     vli_modInv_n(z, s, curve_n); /* Z = s^-1 */
2687     u1[uECC_N_WORDS - 1] = 0;
2688     vli_bytesToNative(u1, hash);
2689     vli_modMult_n(u1, u1, z); /* u1 = e/s */
2690     vli_modMult_n(u2, r, z); /* u2 = r/s */
2691 
2692     /* Calculate sum = G + Q. */
2693     vli_set(sum.x, public.x);
2694     vli_set(sum.y, public.y);
2695     vli_set(tx, curve_G.x);
2696     vli_set(ty, curve_G.y);
2697     vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */
2698     XYcZ_add(tx, ty, sum.x, sum.y);
2699     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2700     apply_z(sum.x, sum.y, z);
2701 
2702     /* Use Shamir's trick to calculate u1*G + u2*Q */
2703     points[0] = 0;
2704     points[1] = &curve_G;
2705     points[2] = &public;
2706     points[3] = &sum;
2707     numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS));
2708 
2709     point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)];
2710     vli_set(rx, point->x);
2711     vli_set(ry, point->y);
2712     vli_clear(z);
2713     z[0] = 1;
2714 
2715     for (i = numBits - 2; i >= 0; --i) {
2716         uECC_word_t index;
2717         EccPoint_double_jacobian(rx, ry, z);
2718 
2719         index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
2720         point = points[index];
2721         if (point) {
2722             vli_set(tx, point->x);
2723             vli_set(ty, point->y);
2724             apply_z(tx, ty, z);
2725             vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */
2726             XYcZ_add(tx, ty, rx, ry);
2727             vli_modMult_fast(z, z, tz);
2728         }
2729     }
2730 
2731     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2732     apply_z(rx, ry, z);
2733 
2734     /* v = x1 (mod n) */
2735 #if (uECC_CURVE != uECC_secp160r1)
2736     if (vli_cmp(curve_n, rx) != 1) {
2737         vli_sub(rx, rx, curve_n);
2738     }
2739 #endif
2740 
2741     /* Accept only if v == r. */
2742     return vli_equal(rx, r);
2743 }
2744