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