xref: /btstack/3rd-party/micro-ecc/uECC.c (revision f0e9886bf1825249aaea7e15f4fe6fb8d065bffb)
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     (void) dest;
471     (void) size;
472     return 0;
473 }
474 
475 #endif
476 
477 static uECC_RNG_Function g_rng_function = &default_RNG;
478 
479 void uECC_set_rng(uECC_RNG_Function rng_function) {
480     g_rng_function = rng_function;
481 }
482 
483 #ifdef __GNUC__ /* Only support GCC inline asm for now */
484     #if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
485         #include "asm_avr.inc"
486     #endif
487 
488     #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
489                       uECC_PLATFORM == uECC_arm_thumb2))
490         #include "asm_arm.inc"
491     #endif
492 #endif
493 
494 #if !asm_clear
495 static void vli_clear(uECC_word_t *vli) {
496     wordcount_t i;
497     for (i = 0; i < uECC_WORDS; ++i) {
498         vli[i] = 0;
499     }
500 }
501 #endif
502 
503 /* Returns 1 if vli == 0, 0 otherwise. */
504 #if !asm_isZero
505 static uECC_word_t vli_isZero(const uECC_word_t *vli) {
506     wordcount_t i;
507     for (i = 0; i < uECC_WORDS; ++i) {
508         if (vli[i]) {
509             return 0;
510         }
511     }
512     return 1;
513 }
514 #endif
515 
516 /* Returns nonzero if bit 'bit' of vli is set. */
517 #if !asm_testBit
518 static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) {
519     return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
520 }
521 #endif
522 
523 #ifdef ENABLE_MICO_ECC_ECDSA
524 
525 /* Counts the number of words in vli. */
526 #if !asm_numBits
527 static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) {
528     swordcount_t i;
529     /* Search from the end until we find a non-zero digit.
530        We do it in reverse because we expect that most digits will be nonzero. */
531     for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
532     }
533 
534     return (i + 1);
535 }
536 
537 /* Counts the number of bits required to represent vli. */
538 static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) {
539     uECC_word_t i;
540     uECC_word_t digit;
541 
542     wordcount_t num_digits = vli_numDigits(vli, max_words);
543     if (num_digits == 0) {
544         return 0;
545     }
546 
547     digit = vli[num_digits - 1];
548     for (i = 0; digit; ++i) {
549         digit >>= 1;
550     }
551 
552     return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
553 }
554 
555 #endif /* ENABLE_MICO_ECC_ECDSA */
556 
557 #endif /* !asm_numBits */
558 
559 /* Sets dest = src. */
560 #if !asm_set
561 static void vli_set(uECC_word_t *dest, const uECC_word_t *src) {
562     wordcount_t i;
563     for (i = 0; i < uECC_WORDS; ++i) {
564         dest[i] = src[i];
565     }
566 }
567 #endif
568 
569 /* Returns sign of left - right. */
570 #if !asm_cmp
571 static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) {
572     swordcount_t i;
573     for (i = uECC_WORDS - 1; i >= 0; --i) {
574         if (left[i] > right[i]) {
575             return 1;
576         } else if (left[i] < right[i]) {
577             return -1;
578         }
579     }
580     return 0;
581 }
582 #endif
583 
584 #ifdef ENABLE_MICRO_ECC_ECDSA
585 
586 static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) {
587     uECC_word_t result = 0;
588     swordcount_t i;
589     for (i = uECC_WORDS - 1; i >= 0; --i) {
590         result |= (left[i] ^ right[i]);
591     }
592     return (result == 0);
593 }
594 
595 #endif
596 
597 /* Computes vli = vli >> 1. */
598 #if !asm_rshift1
599 static void vli_rshift1(uECC_word_t *vli) {
600     uECC_word_t *end = vli;
601     uECC_word_t carry = 0;
602     uECC_word_t *vli_ = vli;
603 
604     vli_ += uECC_WORDS;
605     while (vli_-- > end) {
606         uECC_word_t temp = *vli_;
607         *vli_ = (temp >> 1) | carry;
608         carry = temp << (uECC_WORD_BITS - 1);
609     }
610 }
611 #endif
612 
613 /* Computes result = left + right, returning carry. Can modify in place. */
614 #if !asm_add
615 static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
616     uECC_word_t carry = 0;
617     wordcount_t i;
618     for (i = 0; i < uECC_WORDS; ++i) {
619         uECC_word_t sum = left[i] + right[i] + carry;
620         if (sum != left[i]) {
621             carry = (sum < left[i]);
622         }
623         result[i] = sum;
624     }
625     return carry;
626 }
627 #endif
628 
629 /* Computes result = left - right, returning borrow. Can modify in place. */
630 #if !asm_sub
631 static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
632     uECC_word_t borrow = 0;
633     wordcount_t i;
634     for (i = 0; i < uECC_WORDS; ++i) {
635         uECC_word_t diff = left[i] - right[i] - borrow;
636         if (diff != left[i]) {
637             borrow = (diff > left[i]);
638         }
639         result[i] = diff;
640     }
641     return borrow;
642 }
643 #endif
644 
645 #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1)
646 static void muladd(uECC_word_t a,
647                    uECC_word_t b,
648                    uECC_word_t *r0,
649                    uECC_word_t *r1,
650                    uECC_word_t *r2) {
651 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
652     uint64_t a0 = a & 0xffffffffull;
653     uint64_t a1 = a >> 32;
654     uint64_t b0 = b & 0xffffffffull;
655     uint64_t b1 = b >> 32;
656 
657     uint64_t i0 = a0 * b0;
658     uint64_t i1 = a0 * b1;
659     uint64_t i2 = a1 * b0;
660     uint64_t i3 = a1 * b1;
661 
662     uint64_t p0, p1;
663 
664     i2 += (i0 >> 32);
665     i2 += i1;
666     if (i2 < i1) { // overflow
667         i3 += 0x100000000ull;
668     }
669 
670     p0 = (i0 & 0xffffffffull) | (i2 << 32);
671     p1 = i3 + (i2 >> 32);
672 
673     *r0 += p0;
674     *r1 += (p1 + (*r0 < p0));
675     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
676 #else
677     uECC_dword_t p = (uECC_dword_t)a * b;
678     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
679     r01 += p;
680     *r2 += (r01 < p);
681     *r1 = r01 >> uECC_WORD_BITS;
682     *r0 = (uECC_word_t)r01;
683 #endif
684 }
685 #define muladd_exists 1
686 #endif
687 
688 #if !asm_mult
689 static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
690     uECC_word_t r0 = 0;
691     uECC_word_t r1 = 0;
692     uECC_word_t r2 = 0;
693     wordcount_t i, k;
694 
695     /* Compute each digit of result in sequence, maintaining the carries. */
696     for (k = 0; k < uECC_WORDS; ++k) {
697         for (i = 0; i <= k; ++i) {
698             muladd(left[i], right[k - i], &r0, &r1, &r2);
699         }
700         result[k] = r0;
701         r0 = r1;
702         r1 = r2;
703         r2 = 0;
704     }
705     for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) {
706         for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) {
707             muladd(left[i], right[k - i], &r0, &r1, &r2);
708         }
709         result[k] = r0;
710         r0 = r1;
711         r1 = r2;
712         r2 = 0;
713     }
714     result[uECC_WORDS * 2 - 1] = r0;
715 }
716 #endif
717 
718 #if uECC_SQUARE_FUNC
719 
720 #if !asm_square
721 static void mul2add(uECC_word_t a,
722                     uECC_word_t b,
723                     uECC_word_t *r0,
724                     uECC_word_t *r1,
725                     uECC_word_t *r2) {
726 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
727     uint64_t a0 = a & 0xffffffffull;
728     uint64_t a1 = a >> 32;
729     uint64_t b0 = b & 0xffffffffull;
730     uint64_t b1 = b >> 32;
731 
732     uint64_t i0 = a0 * b0;
733     uint64_t i1 = a0 * b1;
734     uint64_t i2 = a1 * b0;
735     uint64_t i3 = a1 * b1;
736 
737     uint64_t p0, p1;
738 
739     i2 += (i0 >> 32);
740     i2 += i1;
741     if (i2 < i1)
742     { // overflow
743         i3 += 0x100000000ull;
744     }
745 
746     p0 = (i0 & 0xffffffffull) | (i2 << 32);
747     p1 = i3 + (i2 >> 32);
748 
749     *r2 += (p1 >> 63);
750     p1 = (p1 << 1) | (p0 >> 63);
751     p0 <<= 1;
752 
753     *r0 += p0;
754     *r1 += (p1 + (*r0 < p0));
755     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
756 #else
757     uECC_dword_t p = (uECC_dword_t)a * b;
758     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
759     *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
760     p *= 2;
761     r01 += p;
762     *r2 += (r01 < p);
763     *r1 = r01 >> uECC_WORD_BITS;
764     *r0 = (uECC_word_t)r01;
765 #endif
766 }
767 
768 static void vli_square(uECC_word_t *result, const uECC_word_t *left) {
769     uECC_word_t r0 = 0;
770     uECC_word_t r1 = 0;
771     uECC_word_t r2 = 0;
772 
773     wordcount_t i, k;
774 
775     for (k = 0; k < uECC_WORDS * 2 - 1; ++k) {
776         uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS);
777         for (i = min; i <= k && i <= k - i; ++i) {
778             if (i < k-i) {
779                 mul2add(left[i], left[k - i], &r0, &r1, &r2);
780             } else {
781                 muladd(left[i], left[k - i], &r0, &r1, &r2);
782             }
783         }
784         result[k] = r0;
785         r0 = r1;
786         r1 = r2;
787         r2 = 0;
788     }
789 
790     result[uECC_WORDS * 2 - 1] = r0;
791 }
792 #endif
793 
794 #else /* uECC_SQUARE_FUNC */
795 
796 #define vli_square(result, left, size) vli_mult((result), (left), (left), (size))
797 
798 #endif /* uECC_SQUARE_FUNC */
799 
800 
801 /* Computes result = (left + right) % mod.
802    Assumes that left < mod and right < mod, and that result does not overlap mod. */
803 #if !asm_modAdd
804 static void vli_modAdd(uECC_word_t *result,
805                        const uECC_word_t *left,
806                        const uECC_word_t *right,
807                        const uECC_word_t *mod) {
808     uECC_word_t carry = vli_add(result, left, right);
809     if (carry || vli_cmp(result, mod) >= 0) {
810         /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
811         vli_sub(result, result, mod);
812     }
813 }
814 #endif
815 
816 /* Computes result = (left - right) % mod.
817    Assumes that left < mod and right < mod, and that result does not overlap mod. */
818 #if !asm_modSub
819 static void vli_modSub(uECC_word_t *result,
820                        const uECC_word_t *left,
821                        const uECC_word_t *right,
822                        const uECC_word_t *mod) {
823     uECC_word_t l_borrow = vli_sub(result, left, right);
824     if (l_borrow) {
825         /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
826            we can get the correct result from result + mod (with overflow). */
827         vli_add(result, result, mod);
828     }
829 }
830 #endif
831 
832 #if !asm_modSub_fast
833     #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p)
834 #endif
835 
836 #if !asm_mmod_fast
837 
838 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1)
839 /* omega_mult() is defined farther below for the different curves / word sizes */
840 static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right);
841 
842 /* Computes result = product % curve_p
843     see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354
844 
845     Note that this only works if log2(omega) < log2(p) / 2 */
846 static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) {
847     uECC_word_t tmp[2 * uECC_WORDS];
848     uECC_word_t carry;
849 
850     vli_clear(tmp);
851     vli_clear(tmp + uECC_WORDS);
852 
853     omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */
854 
855     carry = vli_add(result, product, tmp); /* (C, r) = r + q       */
856     vli_clear(product);
857     omega_mult(product, tmp + uECC_WORDS); /* Rq*c */
858     carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */
859 
860     while (carry > 0) {
861         --carry;
862         vli_sub(result, result, curve_p);
863     }
864     if (vli_cmp(result, curve_p) > 0) {
865         vli_sub(result, result, curve_p);
866     }
867 }
868 
869 #endif
870 
871 #if uECC_CURVE == uECC_secp160r1
872 
873 #if uECC_WORD_SIZE == 1
874 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
875     uint8_t carry;
876     uint8_t i;
877 
878     /* Multiply by (2^31 + 1). */
879     vli_set(result + 4, right); /* 2^32 */
880     vli_rshift1(result + 4); /* 2^31 */
881     result[3] = right[0] << 7; /* get last bit from shift */
882 
883     carry = vli_add(result, result, right); /* 2^31 + 1 */
884     for (i = uECC_WORDS; carry; ++i) {
885         uint16_t sum = (uint16_t)result[i] + carry;
886         result[i] = (uint8_t)sum;
887         carry = sum >> 8;
888     }
889 }
890 #elif uECC_WORD_SIZE == 4
891 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
892     uint32_t carry;
893     unsigned i;
894 
895     /* Multiply by (2^31 + 1). */
896     vli_set(result + 1, right); /* 2^32 */
897     vli_rshift1(result + 1); /* 2^31 */
898     result[0] = right[0] << 31; /* get last bit from shift */
899 
900     carry = vli_add(result, result, right); /* 2^31 + 1 */
901     for (i = uECC_WORDS; carry; ++i) {
902         uint64_t sum = (uint64_t)result[i] + carry;
903         result[i] = (uint32_t)sum;
904         carry = sum >> 32;
905     }
906 }
907 #endif /* uECC_WORD_SIZE */
908 
909 #elif uECC_CURVE == uECC_secp192r1
910 
911 /* Computes result = product % curve_p.
912    See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
913 #if uECC_WORD_SIZE == 1
914 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
915     uint8_t tmp[uECC_WORDS];
916     uint8_t carry;
917 
918     vli_set(result, product);
919 
920     vli_set(tmp, &product[24]);
921     carry = vli_add(result, result, tmp);
922 
923     tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
924     tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27];
925     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
926     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
927     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
928     carry += vli_add(result, result, tmp);
929 
930     tmp[0] = tmp[8] = product[40];
931     tmp[1] = tmp[9] = product[41];
932     tmp[2] = tmp[10] = product[42];
933     tmp[3] = tmp[11] = product[43];
934     tmp[4] = tmp[12] = product[44];
935     tmp[5] = tmp[13] = product[45];
936     tmp[6] = tmp[14] = product[46];
937     tmp[7] = tmp[15] = product[47];
938     tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
939     carry += vli_add(result, result, tmp);
940 
941     while (carry || vli_cmp(curve_p, result) != 1) {
942         carry -= vli_sub(result, result, curve_p);
943     }
944 }
945 #elif uECC_WORD_SIZE == 4
946 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
947     uint32_t tmp[uECC_WORDS];
948     int carry;
949 
950     vli_set(result, product);
951 
952     vli_set(tmp, &product[6]);
953     carry = vli_add(result, result, tmp);
954 
955     tmp[0] = tmp[1] = 0;
956     tmp[2] = product[6];
957     tmp[3] = product[7];
958     tmp[4] = product[8];
959     tmp[5] = product[9];
960     carry += vli_add(result, result, tmp);
961 
962     tmp[0] = tmp[2] = product[10];
963     tmp[1] = tmp[3] = product[11];
964     tmp[4] = tmp[5] = 0;
965     carry += vli_add(result, result, tmp);
966 
967     while (carry || vli_cmp(curve_p, result) != 1) {
968         carry -= vli_sub(result, result, curve_p);
969     }
970 }
971 #else
972 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
973     uint64_t tmp[uECC_WORDS];
974     int carry;
975 
976     vli_set(result, product);
977 
978     vli_set(tmp, &product[3]);
979     carry = vli_add(result, result, tmp);
980 
981     tmp[0] = 0;
982     tmp[1] = product[3];
983     tmp[2] = product[4];
984     carry += vli_add(result, result, tmp);
985 
986     tmp[0] = tmp[1] = product[5];
987     tmp[2] = 0;
988     carry += vli_add(result, result, tmp);
989 
990     while (carry || vli_cmp(curve_p, result) != 1) {
991         carry -= vli_sub(result, result, curve_p);
992     }
993 }
994 #endif /* uECC_WORD_SIZE */
995 
996 #elif uECC_CURVE == uECC_secp256r1
997 
998 /* Computes result = product % curve_p
999    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1000 #if uECC_WORD_SIZE == 1
1001 static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
1002     uint8_t tmp[uECC_BYTES];
1003     int8_t carry;
1004 
1005     /* t */
1006     vli_set(result, product);
1007 
1008     /* s1 */
1009     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
1010     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
1011     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1012     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
1013     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
1014     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
1015     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
1016     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
1017     carry = vli_add(tmp, tmp, tmp);
1018     carry += vli_add(result, result, tmp);
1019 
1020     /* s2 */
1021     tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51];
1022     tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55];
1023     tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59];
1024     tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63];
1025     tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0;
1026     carry += vli_add(tmp, tmp, tmp);
1027     carry += vli_add(result, result, tmp);
1028 
1029     /* s3 */
1030     tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35];
1031     tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39];
1032     tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43];
1033     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1034     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1035     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1036     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
1037     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
1038     carry += vli_add(result, result, tmp);
1039 
1040     /* s4 */
1041     tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39];
1042     tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43];
1043     tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47];
1044     tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55];
1045     tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59];
1046     tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63];
1047     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1048     tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35];
1049     carry += vli_add(result, result, tmp);
1050 
1051     /* d1 */
1052     tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47];
1053     tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51];
1054     tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1055     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1056     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1057     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1058     tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35];
1059     tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43];
1060     carry -= vli_sub(result, result, tmp);
1061 
1062     /* d2 */
1063     tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51];
1064     tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55];
1065     tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59];
1066     tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63];
1067     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1068     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1069     tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39];
1070     tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47];
1071     carry -= vli_sub(result, result, tmp);
1072 
1073     /* d3 */
1074     tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55];
1075     tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59];
1076     tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63];
1077     tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35];
1078     tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39];
1079     tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43];
1080     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1081     tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51];
1082     carry -= vli_sub(result, result, tmp);
1083 
1084     /* d4 */
1085     tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59];
1086     tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63];
1087     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1088     tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39];
1089     tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43];
1090     tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47];
1091     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1092     tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55];
1093     carry -= vli_sub(result, result, tmp);
1094 
1095     if (carry < 0) {
1096         do {
1097             carry += vli_add(result, result, curve_p);
1098         } while (carry < 0);
1099     } else {
1100         while (carry || vli_cmp(curve_p, result) != 1) {
1101             carry -= vli_sub(result, result, curve_p);
1102         }
1103     }
1104 }
1105 #elif uECC_WORD_SIZE == 4
1106 static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
1107     uint32_t tmp[uECC_WORDS];
1108     int carry;
1109 
1110     /* t */
1111     vli_set(result, product);
1112 
1113     /* s1 */
1114     tmp[0] = tmp[1] = tmp[2] = 0;
1115     tmp[3] = product[11];
1116     tmp[4] = product[12];
1117     tmp[5] = product[13];
1118     tmp[6] = product[14];
1119     tmp[7] = product[15];
1120     carry = vli_add(tmp, tmp, tmp);
1121     carry += vli_add(result, result, tmp);
1122 
1123     /* s2 */
1124     tmp[3] = product[12];
1125     tmp[4] = product[13];
1126     tmp[5] = product[14];
1127     tmp[6] = product[15];
1128     tmp[7] = 0;
1129     carry += vli_add(tmp, tmp, tmp);
1130     carry += vli_add(result, result, tmp);
1131 
1132     /* s3 */
1133     tmp[0] = product[8];
1134     tmp[1] = product[9];
1135     tmp[2] = product[10];
1136     tmp[3] = tmp[4] = tmp[5] = 0;
1137     tmp[6] = product[14];
1138     tmp[7] = product[15];
1139     carry += vli_add(result, result, tmp);
1140 
1141     /* s4 */
1142     tmp[0] = product[9];
1143     tmp[1] = product[10];
1144     tmp[2] = product[11];
1145     tmp[3] = product[13];
1146     tmp[4] = product[14];
1147     tmp[5] = product[15];
1148     tmp[6] = product[13];
1149     tmp[7] = product[8];
1150     carry += vli_add(result, result, tmp);
1151 
1152     /* d1 */
1153     tmp[0] = product[11];
1154     tmp[1] = product[12];
1155     tmp[2] = product[13];
1156     tmp[3] = tmp[4] = tmp[5] = 0;
1157     tmp[6] = product[8];
1158     tmp[7] = product[10];
1159     carry -= vli_sub(result, result, tmp);
1160 
1161     /* d2 */
1162     tmp[0] = product[12];
1163     tmp[1] = product[13];
1164     tmp[2] = product[14];
1165     tmp[3] = product[15];
1166     tmp[4] = tmp[5] = 0;
1167     tmp[6] = product[9];
1168     tmp[7] = product[11];
1169     carry -= vli_sub(result, result, tmp);
1170 
1171     /* d3 */
1172     tmp[0] = product[13];
1173     tmp[1] = product[14];
1174     tmp[2] = product[15];
1175     tmp[3] = product[8];
1176     tmp[4] = product[9];
1177     tmp[5] = product[10];
1178     tmp[6] = 0;
1179     tmp[7] = product[12];
1180     carry -= vli_sub(result, result, tmp);
1181 
1182     /* d4 */
1183     tmp[0] = product[14];
1184     tmp[1] = product[15];
1185     tmp[2] = 0;
1186     tmp[3] = product[9];
1187     tmp[4] = product[10];
1188     tmp[5] = product[11];
1189     tmp[6] = 0;
1190     tmp[7] = product[13];
1191     carry -= vli_sub(result, result, tmp);
1192 
1193     if (carry < 0) {
1194         do {
1195             carry += vli_add(result, result, curve_p);
1196         } while (carry < 0);
1197     } else {
1198         while (carry || vli_cmp(curve_p, result) != 1) {
1199             carry -= vli_sub(result, result, curve_p);
1200         }
1201     }
1202 }
1203 #else
1204 static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
1205     uint64_t tmp[uECC_WORDS];
1206     int carry;
1207 
1208     /* t */
1209     vli_set(result, product);
1210 
1211     /* s1 */
1212     tmp[0] = 0;
1213     tmp[1] = product[5] & 0xffffffff00000000ull;
1214     tmp[2] = product[6];
1215     tmp[3] = product[7];
1216     carry = vli_add(tmp, tmp, tmp);
1217     carry += vli_add(result, result, tmp);
1218 
1219     /* s2 */
1220     tmp[1] = product[6] << 32;
1221     tmp[2] = (product[6] >> 32) | (product[7] << 32);
1222     tmp[3] = product[7] >> 32;
1223     carry += vli_add(tmp, tmp, tmp);
1224     carry += vli_add(result, result, tmp);
1225 
1226     /* s3 */
1227     tmp[0] = product[4];
1228     tmp[1] = product[5] & 0xffffffff;
1229     tmp[2] = 0;
1230     tmp[3] = product[7];
1231     carry += vli_add(result, result, tmp);
1232 
1233     /* s4 */
1234     tmp[0] = (product[4] >> 32) | (product[5] << 32);
1235     tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
1236     tmp[2] = product[7];
1237     tmp[3] = (product[6] >> 32) | (product[4] << 32);
1238     carry += vli_add(result, result, tmp);
1239 
1240     /* d1 */
1241     tmp[0] = (product[5] >> 32) | (product[6] << 32);
1242     tmp[1] = (product[6] >> 32);
1243     tmp[2] = 0;
1244     tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
1245     carry -= vli_sub(result, result, tmp);
1246 
1247     /* d2 */
1248     tmp[0] = product[6];
1249     tmp[1] = product[7];
1250     tmp[2] = 0;
1251     tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
1252     carry -= vli_sub(result, result, tmp);
1253 
1254     /* d3 */
1255     tmp[0] = (product[6] >> 32) | (product[7] << 32);
1256     tmp[1] = (product[7] >> 32) | (product[4] << 32);
1257     tmp[2] = (product[4] >> 32) | (product[5] << 32);
1258     tmp[3] = (product[6] << 32);
1259     carry -= vli_sub(result, result, tmp);
1260 
1261     /* d4 */
1262     tmp[0] = product[7];
1263     tmp[1] = product[4] & 0xffffffff00000000ull;
1264     tmp[2] = product[5];
1265     tmp[3] = product[6] & 0xffffffff00000000ull;
1266     carry -= vli_sub(result, result, tmp);
1267 
1268     if (carry < 0) {
1269         do {
1270             carry += vli_add(result, result, curve_p);
1271         } while (carry < 0);
1272     } else {
1273         while (carry || vli_cmp(curve_p, result) != 1) {
1274             carry -= vli_sub(result, result, curve_p);
1275         }
1276     }
1277 }
1278 #endif /* uECC_WORD_SIZE */
1279 
1280 #elif uECC_CURVE == uECC_secp256k1
1281 
1282 #if uECC_WORD_SIZE == 1
1283 static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
1284     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1285     uECC_word_t r0 = 0;
1286     uECC_word_t r1 = 0;
1287     uECC_word_t r2 = 0;
1288     wordcount_t k;
1289 
1290     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1291     muladd(0xD1, right[0], &r0, &r1, &r2);
1292     result[0] = r0;
1293     r0 = r1;
1294     r1 = r2;
1295     /* r2 is still 0 */
1296 
1297     for (k = 1; k < uECC_WORDS; ++k) {
1298         muladd(0x03, right[k - 1], &r0, &r1, &r2);
1299         muladd(0xD1, right[k], &r0, &r1, &r2);
1300         result[k] = r0;
1301         r0 = r1;
1302         r1 = r2;
1303         r2 = 0;
1304     }
1305     muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2);
1306     result[uECC_WORDS] = r0;
1307     result[uECC_WORDS + 1] = r1;
1308 
1309     result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */
1310 }
1311 #elif uECC_WORD_SIZE == 4
1312 static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
1313     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1314     uint32_t carry = 0;
1315     wordcount_t k;
1316 
1317     for (k = 0; k < uECC_WORDS; ++k) {
1318         uint64_t p = (uint64_t)0x3D1 * right[k] + carry;
1319         result[k] = (p & 0xffffffff);
1320         carry = p >> 32;
1321     }
1322     result[uECC_WORDS] = carry;
1323 
1324     result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */
1325 }
1326 #else
1327 static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) {
1328     uECC_word_t r0 = 0;
1329     uECC_word_t r1 = 0;
1330     uECC_word_t r2 = 0;
1331     wordcount_t k;
1332 
1333     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1334     for (k = 0; k < uECC_WORDS; ++k) {
1335         muladd(0x1000003D1ull, right[k], &r0, &r1, &r2);
1336         result[k] = r0;
1337         r0 = r1;
1338         r1 = r2;
1339         r2 = 0;
1340     }
1341     result[uECC_WORDS] = r0;
1342 }
1343 #endif /* uECC_WORD_SIZE */
1344 
1345 #elif uECC_CURVE == uECC_secp224r1
1346 
1347 /* Computes result = product % curve_p
1348    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1349 #if uECC_WORD_SIZE == 1
1350 // TODO it may be faster to use the omega_mult method when fully asm optimized.
1351 void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
1352     uint8_t tmp[uECC_WORDS];
1353     int8_t carry;
1354 
1355     /* t */
1356     vli_set(result, product);
1357 
1358     /* s1 */
1359     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
1360     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
1361     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1362     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
1363     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
1364     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
1365     tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43];
1366     carry = vli_add(result, result, tmp);
1367 
1368     /* s2 */
1369     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
1370     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
1371     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
1372     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1373     carry += vli_add(result, result, tmp);
1374 
1375     /* d1 */
1376     tmp[0]  = product[28]; tmp[1]  = product[29]; tmp[2]  = product[30]; tmp[3]  = product[31];
1377     tmp[4]  = product[32]; tmp[5]  = product[33]; tmp[6]  = product[34]; tmp[7]  = product[35];
1378     tmp[8]  = product[36]; tmp[9]  = product[37]; tmp[10] = product[38]; tmp[11] = product[39];
1379     tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43];
1380     tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47];
1381     tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51];
1382     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1383     carry -= vli_sub(result, result, tmp);
1384 
1385     /* d2 */
1386     tmp[0]  = product[44]; tmp[1]  = product[45]; tmp[2]  = product[46]; tmp[3]  = product[47];
1387     tmp[4]  = product[48]; tmp[5]  = product[49]; tmp[6]  = product[50]; tmp[7]  = product[51];
1388     tmp[8]  = product[52]; tmp[9]  = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1389     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1390     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1391     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1392     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1393     carry -= vli_sub(result, result, tmp);
1394 
1395     if (carry < 0) {
1396         do {
1397             carry += vli_add(result, result, curve_p);
1398         } while (carry < 0);
1399     } else {
1400         while (carry || vli_cmp(curve_p, result) != 1) {
1401             carry -= vli_sub(result, result, curve_p);
1402         }
1403     }
1404 }
1405 #elif uECC_WORD_SIZE == 4
1406 void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product)
1407 {
1408     uint32_t tmp[uECC_WORDS];
1409     int carry;
1410 
1411     /* t */
1412     vli_set(result, product);
1413 
1414     /* s1 */
1415     tmp[0] = tmp[1] = tmp[2] = 0;
1416     tmp[3] = product[7];
1417     tmp[4] = product[8];
1418     tmp[5] = product[9];
1419     tmp[6] = product[10];
1420     carry = vli_add(result, result, tmp);
1421 
1422     /* s2 */
1423     tmp[3] = product[11];
1424     tmp[4] = product[12];
1425     tmp[5] = product[13];
1426     tmp[6] = 0;
1427     carry += vli_add(result, result, tmp);
1428 
1429     /* d1 */
1430     tmp[0] = product[7];
1431     tmp[1] = product[8];
1432     tmp[2] = product[9];
1433     tmp[3] = product[10];
1434     tmp[4] = product[11];
1435     tmp[5] = product[12];
1436     tmp[6] = product[13];
1437     carry -= vli_sub(result, result, tmp);
1438 
1439     /* d2 */
1440     tmp[0] = product[11];
1441     tmp[1] = product[12];
1442     tmp[2] = product[13];
1443     tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0;
1444     carry -= vli_sub(result, result, tmp);
1445 
1446     if (carry < 0) {
1447         do {
1448             carry += vli_add(result, result, curve_p);
1449         } while (carry < 0);
1450     } else {
1451         while (carry || vli_cmp(curve_p, result) != 1) {
1452             carry -= vli_sub(result, result, curve_p);
1453         }
1454     }
1455 }
1456 #endif /* uECC_WORD_SIZE */
1457 
1458 #endif /* uECC_CURVE */
1459 #endif /* !asm_mmod_fast */
1460 
1461 /* Computes result = (left * right) % curve_p. */
1462 static void vli_modMult_fast(uECC_word_t *result,
1463                              const uECC_word_t *left,
1464                              const uECC_word_t *right) {
1465     uECC_word_t product[2 * uECC_WORDS];
1466     vli_mult(product, left, right);
1467     vli_mmod_fast(result, product);
1468 }
1469 
1470 #if uECC_SQUARE_FUNC
1471 
1472 /* Computes result = left^2 % curve_p. */
1473 static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) {
1474     uECC_word_t product[2 * uECC_WORDS];
1475     vli_square(product, left);
1476     vli_mmod_fast(result, product);
1477 }
1478 
1479 #else /* uECC_SQUARE_FUNC */
1480 
1481 #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left))
1482 
1483 #endif /* uECC_SQUARE_FUNC */
1484 
1485 
1486 #define EVEN(vli) (!(vli[0] & 1))
1487 /* Computes result = (1 / input) % mod. All VLIs are the same size.
1488    See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
1489    https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
1490 #if !asm_modInv
1491 static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
1492     uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS];
1493     uECC_word_t carry;
1494     cmpresult_t cmpResult;
1495 
1496     if (vli_isZero(input)) {
1497         vli_clear(result);
1498         return;
1499     }
1500 
1501     vli_set(a, input);
1502     vli_set(b, mod);
1503     vli_clear(u);
1504     u[0] = 1;
1505     vli_clear(v);
1506     while ((cmpResult = vli_cmp(a, b)) != 0) {
1507         carry = 0;
1508         if (EVEN(a)) {
1509             vli_rshift1(a);
1510             if (!EVEN(u)) {
1511                 carry = vli_add(u, u, mod);
1512             }
1513             vli_rshift1(u);
1514             if (carry) {
1515                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1516             }
1517         } else if (EVEN(b)) {
1518             vli_rshift1(b);
1519             if (!EVEN(v)) {
1520                 carry = vli_add(v, v, mod);
1521             }
1522             vli_rshift1(v);
1523             if (carry) {
1524                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1525             }
1526         } else if (cmpResult > 0) {
1527             vli_sub(a, a, b);
1528             vli_rshift1(a);
1529             if (vli_cmp(u, v) < 0) {
1530                 vli_add(u, u, mod);
1531             }
1532             vli_sub(u, u, v);
1533             if (!EVEN(u)) {
1534                 carry = vli_add(u, u, mod);
1535             }
1536             vli_rshift1(u);
1537             if (carry) {
1538                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1539             }
1540         } else {
1541             vli_sub(b, b, a);
1542             vli_rshift1(b);
1543             if (vli_cmp(v, u) < 0) {
1544                 vli_add(v, v, mod);
1545             }
1546             vli_sub(v, v, u);
1547             if (!EVEN(v)) {
1548                 carry = vli_add(v, v, mod);
1549             }
1550             vli_rshift1(v);
1551             if (carry) {
1552                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1553             }
1554         }
1555     }
1556     vli_set(result, u);
1557 }
1558 #endif /* !asm_modInv */
1559 
1560 /* ------ Point operations ------ */
1561 
1562 /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */
1563 static cmpresult_t EccPoint_isZero(const EccPoint *point) {
1564     return (vli_isZero(point->x) && vli_isZero(point->y));
1565 }
1566 
1567 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
1568 From http://eprint.iacr.org/2011/338.pdf
1569 */
1570 
1571 /* Double in place */
1572 #if (uECC_CURVE == uECC_secp256k1)
1573 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1574                                      uECC_word_t * RESTRICT Y1,
1575                                      uECC_word_t * RESTRICT Z1) {
1576     /* t1 = X, t2 = Y, t3 = Z */
1577     uECC_word_t t4[uECC_WORDS];
1578     uECC_word_t t5[uECC_WORDS];
1579 
1580     if (vli_isZero(Z1)) {
1581         return;
1582     }
1583 
1584     vli_modSquare_fast(t5, Y1);   /* t5 = y1^2 */
1585     vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */
1586     vli_modSquare_fast(X1, X1);   /* t1 = x1^2 */
1587     vli_modSquare_fast(t5, t5);   /* t5 = y1^4 */
1588     vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */
1589 
1590     vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */
1591     vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */
1592     if (vli_testBit(Y1, 0)) {
1593         uECC_word_t carry = vli_add(Y1, Y1, curve_p);
1594         vli_rshift1(Y1);
1595         Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1);
1596     } else {
1597         vli_rshift1(Y1);
1598     }
1599     /* t2 = 3/2*(x1^2) = B */
1600 
1601     vli_modSquare_fast(X1, Y1);      /* t1 = B^2 */
1602     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */
1603     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */
1604 
1605     vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */
1606     vli_modMult_fast(Y1, Y1, t4);    /* t2 = B * (A - x3) */
1607     vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */
1608 }
1609 #else
1610 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1611                                      uECC_word_t * RESTRICT Y1,
1612                                      uECC_word_t * RESTRICT Z1) {
1613     /* t1 = X, t2 = Y, t3 = Z */
1614     uECC_word_t t4[uECC_WORDS];
1615     uECC_word_t t5[uECC_WORDS];
1616 
1617     if (vli_isZero(Z1)) {
1618         return;
1619     }
1620 
1621     vli_modSquare_fast(t4, Y1);   /* t4 = y1^2 */
1622     vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
1623     vli_modSquare_fast(t4, t4);   /* t4 = y1^4 */
1624     vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
1625     vli_modSquare_fast(Z1, Z1);   /* t3 = z1^2 */
1626 
1627     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
1628     vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
1629     vli_modSub_fast(Z1, X1, Z1);     /* t3 = x1 - z1^2 */
1630     vli_modMult_fast(X1, X1, Z1);    /* t1 = x1^2 - z1^4 */
1631 
1632     vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
1633     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
1634     if (vli_testBit(X1, 0)) {
1635         uECC_word_t l_carry = vli_add(X1, X1, curve_p);
1636         vli_rshift1(X1);
1637         X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1);
1638     } else {
1639         vli_rshift1(X1);
1640     }
1641     /* t1 = 3/2*(x1^2 - z1^4) = B */
1642 
1643     vli_modSquare_fast(Z1, X1);   /* t3 = B^2 */
1644     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - A */
1645     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - 2A = x3 */
1646     vli_modSub_fast(t5, t5, Z1);  /* t5 = A - x3 */
1647     vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
1648     vli_modSub_fast(t4, X1, t4);  /* t4 = B * (A - x3) - y1^4 = y3 */
1649 
1650     vli_set(X1, Z1);
1651     vli_set(Z1, Y1);
1652     vli_set(Y1, t4);
1653 }
1654 #endif
1655 
1656 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1657 static void apply_z(uECC_word_t * RESTRICT X1,
1658                     uECC_word_t * RESTRICT Y1,
1659                     const uECC_word_t * RESTRICT Z) {
1660     uECC_word_t t1[uECC_WORDS];
1661 
1662     vli_modSquare_fast(t1, Z);    /* z^2 */
1663     vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
1664     vli_modMult_fast(t1, t1, Z);  /* z^3 */
1665     vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
1666 }
1667 
1668 /* P = (x1, y1) => 2P, (x2, y2) => P' */
1669 static void XYcZ_initial_double(uECC_word_t * RESTRICT X1,
1670                                 uECC_word_t * RESTRICT Y1,
1671                                 uECC_word_t * RESTRICT X2,
1672                                 uECC_word_t * RESTRICT Y2,
1673                                 const uECC_word_t * RESTRICT initial_Z) {
1674     uECC_word_t z[uECC_WORDS];
1675     if (initial_Z) {
1676         vli_set(z, initial_Z);
1677     } else {
1678         vli_clear(z);
1679         z[0] = 1;
1680     }
1681 
1682     vli_set(X2, X1);
1683     vli_set(Y2, Y1);
1684 
1685     apply_z(X1, Y1, z);
1686     EccPoint_double_jacobian(X1, Y1, z);
1687     apply_z(X2, Y2, z);
1688 }
1689 
1690 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1691    Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1692    or P => P', Q => P + Q
1693 */
1694 static void XYcZ_add(uECC_word_t * RESTRICT X1,
1695                      uECC_word_t * RESTRICT Y1,
1696                      uECC_word_t * RESTRICT X2,
1697                      uECC_word_t * RESTRICT Y2) {
1698     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1699     uECC_word_t t5[uECC_WORDS];
1700 
1701     vli_modSub_fast(t5, X2, X1);  /* t5 = x2 - x1 */
1702     vli_modSquare_fast(t5, t5);   /* t5 = (x2 - x1)^2 = A */
1703     vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
1704     vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
1705     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y2 - y1 */
1706     vli_modSquare_fast(t5, Y2);   /* t5 = (y2 - y1)^2 = D */
1707 
1708     vli_modSub_fast(t5, t5, X1);  /* t5 = D - B */
1709     vli_modSub_fast(t5, t5, X2);  /* t5 = D - B - C = x3 */
1710     vli_modSub_fast(X2, X2, X1);  /* t3 = C - B */
1711     vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
1712     vli_modSub_fast(X2, X1, t5);  /* t3 = B - x3 */
1713     vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
1714     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y3 */
1715 
1716     vli_set(X2, t5);
1717 }
1718 
1719 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1720    Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1721    or P => P - Q, Q => P + Q
1722 */
1723 static void XYcZ_addC(uECC_word_t * RESTRICT X1,
1724                       uECC_word_t * RESTRICT Y1,
1725                       uECC_word_t * RESTRICT X2,
1726                       uECC_word_t * RESTRICT Y2) {
1727     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1728     uECC_word_t t5[uECC_WORDS];
1729     uECC_word_t t6[uECC_WORDS];
1730     uECC_word_t t7[uECC_WORDS];
1731 
1732     vli_modSub_fast(t5, X2, X1);     /* t5 = x2 - x1 */
1733     vli_modSquare_fast(t5, t5);      /* t5 = (x2 - x1)^2 = A */
1734     vli_modMult_fast(X1, X1, t5);    /* t1 = x1*A = B */
1735     vli_modMult_fast(X2, X2, t5);    /* t3 = x2*A = C */
1736     vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
1737     vli_modSub_fast(Y2, Y2, Y1);     /* t4 = y2 - y1 */
1738 
1739     vli_modSub_fast(t6, X2, X1);     /* t6 = C - B */
1740     vli_modMult_fast(Y1, Y1, t6);    /* t2 = y1 * (C - B) = E */
1741     vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
1742     vli_modSquare_fast(X2, Y2);      /* t3 = (y2 - y1)^2 = D */
1743     vli_modSub_fast(X2, X2, t6);     /* t3 = D - (B + C) = x3 */
1744 
1745     vli_modSub_fast(t7, X1, X2);  /* t7 = B - x3 */
1746     vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
1747     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = (y2 - y1)*(B - x3) - E = y3 */
1748 
1749     vli_modSquare_fast(t7, t5);   /* t7 = (y2 + y1)^2 = F */
1750     vli_modSub_fast(t7, t7, t6);  /* t7 = F - (B + C) = x3' */
1751     vli_modSub_fast(t6, t7, X1);  /* t6 = x3' - B */
1752     vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
1753     vli_modSub_fast(Y1, t6, Y1);  /* t2 = (y2 + y1)*(x3' - B) - E = y3' */
1754 
1755     vli_set(X1, t7);
1756 }
1757 
1758 static void EccPoint_mult(EccPoint * RESTRICT result,
1759                           const EccPoint * RESTRICT point,
1760                           const uECC_word_t * RESTRICT scalar,
1761                           const uECC_word_t * RESTRICT initialZ,
1762                           bitcount_t numBits) {
1763     /* R0 and R1 */
1764     uECC_word_t Rx[2][uECC_WORDS];
1765     uECC_word_t Ry[2][uECC_WORDS];
1766     uECC_word_t z[uECC_WORDS];
1767     bitcount_t i;
1768     uECC_word_t nb;
1769 
1770     vli_set(Rx[1], point->x);
1771     vli_set(Ry[1], point->y);
1772 
1773     XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ);
1774 
1775     for (i = numBits - 2; i > 0; --i) {
1776         nb = !vli_testBit(scalar, i);
1777         XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1778         XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1779     }
1780 
1781     nb = !vli_testBit(scalar, 0);
1782     XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1783 
1784     /* Find final 1/Z value. */
1785     vli_modSub_fast(z, Rx[1], Rx[0]);   /* X1 - X0 */
1786     vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
1787     vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */
1788     vli_modInv(z, z, curve_p);          /* 1 / (xP * Yb * (X1 - X0)) */
1789     vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */
1790     vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
1791     /* End 1/Z calculation */
1792 
1793     XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1794     apply_z(Rx[0], Ry[0], z);
1795 
1796     vli_set(result->x, Rx[0]);
1797     vli_set(result->y, Ry[0]);
1798 }
1799 
1800 static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) {
1801     uECC_word_t tmp1[uECC_WORDS];
1802     uECC_word_t tmp2[uECC_WORDS];
1803     uECC_word_t *p2[2] = {tmp1, tmp2};
1804     uECC_word_t carry;
1805 
1806     /* Make sure the private key is in the range [1, n-1]. */
1807     if (vli_isZero(private)) {
1808         return 0;
1809     }
1810 
1811 #if (uECC_CURVE == uECC_secp160r1)
1812     // Don't regularize the bitcount for secp160r1, since it would have a larger performance
1813     // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to
1814     // a significant increase in code size.
1815 
1816     EccPoint_mult(result, &curve_G, private, NULL, vli_numBits(private, uECC_WORDS));
1817 #else
1818     if (vli_cmp(curve_n, private) != 1) {
1819         return 0;
1820     }
1821 
1822     // Regularize the bitcount for the private key so that attackers cannot use a side channel
1823     // attack to learn the number of leading zeros.
1824     carry = vli_add(tmp1, private, curve_n);
1825     vli_add(tmp2, tmp1, curve_n);
1826     EccPoint_mult(result, &curve_G, p2[!carry], NULL, (uECC_BYTES * 8) + 1);
1827 #endif
1828 
1829     if (EccPoint_isZero(result)) {
1830         return 0;
1831     }
1832     return 1;
1833 }
1834 
1835 #ifdef ENABLE_MICRO_ECC_COMPRESSION
1836 
1837 #if uECC_CURVE == uECC_secp224r1
1838 
1839 /* Routine 3.2.4 RS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1840 static void mod_sqrt_secp224r1_rs(uECC_word_t *d1,
1841                                   uECC_word_t *e1,
1842                                   uECC_word_t *f1,
1843                                   const uECC_word_t *d0,
1844                                   const uECC_word_t *e0,
1845                                   const uECC_word_t *f0) {
1846     uECC_word_t t[uECC_WORDS];
1847 
1848     vli_modSquare_fast(t, d0);                 /* t <-- d0 ^ 2 */
1849     vli_modMult_fast(e1, d0, e0);              /* e1 <-- d0 * e0 */
1850     vli_modAdd(d1, t, f0, curve_p);            /* d1 <-- t  + f0 */
1851     vli_modAdd(e1, e1, e1, curve_p);           /* e1 <-- e1 + e1 */
1852     vli_modMult_fast(f1, t, f0);               /* f1 <-- t  * f0 */
1853     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1854     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1855 }
1856 
1857 /* Routine 3.2.5 RSS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1858 static void mod_sqrt_secp224r1_rss(uECC_word_t *d1,
1859                                    uECC_word_t *e1,
1860                                    uECC_word_t *f1,
1861                                    const uECC_word_t *d0,
1862                                    const uECC_word_t *e0,
1863                                    const uECC_word_t *f0,
1864                                    const bitcount_t j) {
1865     bitcount_t i;
1866 
1867     vli_set(d1, d0);                           /* d1 <-- d0 */
1868     vli_set(e1, e0);                           /* e1 <-- e0 */
1869     vli_set(f1, f0);                           /* f1 <-- f0 */
1870     for (i = 1; i <= j; i++) {
1871         mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */
1872     }
1873 }
1874 
1875 /* Routine 3.2.6 RM;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1876 static void mod_sqrt_secp224r1_rm(uECC_word_t *d2,
1877                                   uECC_word_t *e2,
1878                                   uECC_word_t *f2,
1879                                   const uECC_word_t *c,
1880                                   const uECC_word_t *d0,
1881                                   const uECC_word_t *e0,
1882                                   const uECC_word_t *d1,
1883                                   const uECC_word_t *e1) {
1884     uECC_word_t t1[uECC_WORDS];
1885     uECC_word_t t2[uECC_WORDS];
1886 
1887     vli_modMult_fast(t1, e0, e1);              /* t1 <-- e0 * e1 */
1888     vli_modMult_fast(t1, t1, c);               /* t1 <-- t1 * c */
1889     vli_modSub_fast(t1, curve_p, t1);          /* t1 <-- p  - t1 */
1890     vli_modMult_fast(t2, d0, d1);              /* t2 <-- d0 * d1 */
1891     vli_modAdd(t2, t2, t1, curve_p);           /* t2 <-- t2 + t1 */
1892     vli_modMult_fast(t1, d0, e1);              /* t1 <-- d0 * e1 */
1893     vli_modMult_fast(e2, d1, e0);              /* e2 <-- d1 * e0 */
1894     vli_modAdd(e2, e2, t1, curve_p);           /* e2 <-- e2 + t1 */
1895     vli_modSquare_fast(f2, e2);                /* f2 <-- e2^2 */
1896     vli_modMult_fast(f2, f2, c);               /* f2 <-- f2 * c */
1897     vli_modSub_fast(f2, curve_p, f2);          /* f2 <-- p  - f2 */
1898     vli_set(d2, t2);                           /* d2 <-- t2 */
1899 }
1900 
1901 /* Routine 3.2.7 RP;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1902 static void mod_sqrt_secp224r1_rp(uECC_word_t *d1,
1903                                   uECC_word_t *e1,
1904                                   uECC_word_t *f1,
1905                                   const uECC_word_t *c,
1906                                   const uECC_word_t *r) {
1907     wordcount_t i;
1908     wordcount_t pow2i = 1;
1909     uECC_word_t d0[uECC_WORDS];
1910     uECC_word_t e0[uECC_WORDS] = {1};          /* e0 <-- 1 */
1911     uECC_word_t f0[uECC_WORDS];
1912 
1913     vli_set(d0, r);                            /* d0 <-- r */
1914     vli_modSub_fast(f0, curve_p, c);           /* f0 <-- p  - c */
1915     for (i = 0; i <= 6; i++) {
1916         mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */
1917         mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0);  /* RM (d1,e1,f1,c,d1,e1,d0,e0) */
1918         vli_set(d0, d1);                       /* d0 <-- d1 */
1919         vli_set(e0, e1);                       /* e0 <-- e1 */
1920         vli_set(f0, f1);                       /* f0 <-- f1 */
1921         pow2i *= 2;
1922     }
1923 }
1924 
1925 /* Compute a = sqrt(a) (mod curve_p). */
1926 /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1927 static void mod_sqrt(uECC_word_t *a) {
1928     bitcount_t i;
1929     uECC_word_t e1[uECC_WORDS];
1930     uECC_word_t f1[uECC_WORDS];
1931     uECC_word_t d0[uECC_WORDS];
1932     uECC_word_t e0[uECC_WORDS];
1933     uECC_word_t f0[uECC_WORDS];
1934     uECC_word_t d1[uECC_WORDS];
1935 
1936     // s = a; using constant instead of random value
1937     mod_sqrt_secp224r1_rp(d0, e0, f0, a, a);           /* RP (d0, e0, f0, c, s) */
1938     mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0);     /* RS (d1, e1, f1, d0, e0, f0) */
1939     for (i = 1; i <= 95; i++) {
1940         vli_set(d0, d1);                               /* d0 <-- d1 */
1941         vli_set(e0, e1);                               /* e0 <-- e1 */
1942         vli_set(f0, f1);                               /* f0 <-- f1 */
1943         mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */
1944         if (vli_isZero(d1)) {                          /* if d1 == 0 */
1945 	        break;
1946         }
1947     }
1948     vli_modInv(f1, e0, curve_p);                       /* f1 <-- 1 / e0 */
1949     vli_modMult_fast(a, d0, f1);                       /* a  <-- d0 / e0 */
1950 }
1951 
1952 #else /* uECC_CURVE */
1953 
1954 /* Compute a = sqrt(a) (mod curve_p). */
1955 static void mod_sqrt(uECC_word_t *a) {
1956     bitcount_t i;
1957     uECC_word_t p1[uECC_WORDS] = {1};
1958     uECC_word_t l_result[uECC_WORDS] = {1};
1959 
1960     /* Since curve_p == 3 (mod 4) for all supported curves, we can
1961        compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
1962     vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
1963     for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) {
1964         vli_modSquare_fast(l_result, l_result);
1965         if (vli_testBit(p1, i)) {
1966             vli_modMult_fast(l_result, l_result, a);
1967         }
1968     }
1969     vli_set(a, l_result);
1970 }
1971 #endif /* uECC_CURVE */
1972 
1973 #endif /* ENABLE_MICRO_ECC_COMPRESSION */
1974 
1975 
1976 #if uECC_WORD_SIZE == 1
1977 
1978 static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) {
1979     uint8_t i;
1980     for (i = 0; i < uECC_BYTES; ++i) {
1981         dest[i] = src[(uECC_BYTES - 1) - i];
1982     }
1983 }
1984 
1985 #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src))
1986 
1987 #elif uECC_WORD_SIZE == 4
1988 
1989 static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) {
1990     unsigned i;
1991     for (i = 0; i < uECC_WORDS; ++i) {
1992         uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
1993         digit[0] = native[i] >> 24;
1994         digit[1] = native[i] >> 16;
1995         digit[2] = native[i] >> 8;
1996         digit[3] = native[i];
1997     }
1998 }
1999 
2000 static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) {
2001     unsigned i;
2002     for (i = 0; i < uECC_WORDS; ++i) {
2003         const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
2004         native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) |
2005                     ((uint32_t)digit[2] << 8) | (uint32_t)digit[3];
2006     }
2007 }
2008 
2009 #else
2010 
2011 static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) {
2012     unsigned i;
2013     for (i = 0; i < uECC_WORDS; ++i) {
2014         uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
2015         digit[0] = native[i] >> 56;
2016         digit[1] = native[i] >> 48;
2017         digit[2] = native[i] >> 40;
2018         digit[3] = native[i] >> 32;
2019         digit[4] = native[i] >> 24;
2020         digit[5] = native[i] >> 16;
2021         digit[6] = native[i] >> 8;
2022         digit[7] = native[i];
2023     }
2024 }
2025 
2026 static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) {
2027     unsigned i;
2028     for (i = 0; i < uECC_WORDS; ++i) {
2029         const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
2030         native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) |
2031                     ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) |
2032                     ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) |
2033                     ((uint64_t)digit[6] << 8) | (uint64_t)digit[7];
2034     }
2035 }
2036 
2037 #endif /* uECC_WORD_SIZE */
2038 
2039 int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) {
2040     uECC_word_t private[uECC_WORDS];
2041     EccPoint public;
2042     uECC_word_t tries;
2043     for (tries = 0; tries < MAX_TRIES; ++tries) {
2044         if (g_rng_function((uint8_t *)private, sizeof(private)) &&
2045                 EccPoint_compute_public_key(&public, private)) {
2046             vli_nativeToBytes(private_key, private);
2047             vli_nativeToBytes(public_key, public.x);
2048             vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2049             return 1;
2050         }
2051     }
2052     return 0;
2053 }
2054 
2055 int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2],
2056                        const uint8_t private_key[uECC_BYTES],
2057                        uint8_t secret[uECC_BYTES]) {
2058     EccPoint public;
2059     EccPoint product;
2060     uECC_word_t private[uECC_WORDS];
2061     uECC_word_t tmp[uECC_WORDS];
2062     uECC_word_t *p2[2] = {private, tmp};
2063     uECC_word_t random[uECC_WORDS];
2064     uECC_word_t *initial_Z = NULL;
2065     uECC_word_t tries;
2066     uECC_word_t carry;
2067 
2068     // Try to get a random initial Z value to improve protection against side-channel
2069     // attacks. If the RNG fails every time (eg it was not defined), we continue so that
2070     // uECC_shared_secret() can still work without an RNG defined.
2071     for (tries = 0; tries < MAX_TRIES; ++tries) {
2072         if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) {
2073             initial_Z = random;
2074             break;
2075         }
2076     }
2077 
2078     vli_bytesToNative(private, private_key);
2079     vli_bytesToNative(public.x, public_key);
2080     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2081 
2082 #if (uECC_CURVE == uECC_secp160r1)
2083     // Don't regularize the bitcount for secp160r1.
2084     EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS));
2085 #else
2086     // Regularize the bitcount for the private key so that attackers cannot use a side channel
2087     // attack to learn the number of leading zeros.
2088     carry = vli_add(private, private, curve_n);
2089     vli_add(tmp, private, curve_n);
2090     EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1);
2091 #endif
2092 
2093     vli_nativeToBytes(secret, product.x);
2094     return !EccPoint_isZero(&product);
2095 }
2096 
2097 #ifdef ENABLE_MICRO_ECC_COMPRESSION
2098 
2099 void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) {
2100     wordcount_t i;
2101     for (i = 0; i < uECC_BYTES; ++i) {
2102         compressed[i+1] = public_key[i];
2103     }
2104     compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01);
2105 }
2106 
2107 #endif
2108 
2109 /* Computes result = x^3 + ax + b. result must not overlap x. */
2110 static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) {
2111     static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE);
2112 #if (uECC_CURVE == uECC_secp256k1)
2113     vli_modSquare_fast(result, x); /* r = x^2 */
2114     vli_modMult_fast(result, result, x); /* r = x^3 */
2115     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */
2116 #else
2117     uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */
2118 
2119     vli_modSquare_fast(result, x); /* r = x^2 */
2120     vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */
2121     vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
2122     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */
2123 #endif
2124 }
2125 
2126 #ifdef ENABLE_MICRO_ECC_COMPRESSION
2127 
2128 void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) {
2129     EccPoint point;
2130     vli_bytesToNative(point.x, compressed + 1);
2131     curve_x_side(point.y, point.x);
2132     mod_sqrt(point.y);
2133 
2134     if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) {
2135         vli_sub(point.y, curve_p, point.y);
2136     }
2137 
2138     vli_nativeToBytes(public_key, point.x);
2139     vli_nativeToBytes(public_key + uECC_BYTES, point.y);
2140 }
2141 
2142 #endif /* ENABLE_MICRO_ECC_COMPRESSION */
2143 
2144 int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) {
2145     uECC_word_t tmp1[uECC_WORDS];
2146     uECC_word_t tmp2[uECC_WORDS];
2147     EccPoint public;
2148 
2149     vli_bytesToNative(public.x, public_key);
2150     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2151 
2152     // The point at infinity is invalid.
2153     if (EccPoint_isZero(&public)) {
2154         return 0;
2155     }
2156 
2157     // x and y must be smaller than p.
2158     if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) {
2159         return 0;
2160     }
2161 
2162     vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */
2163     curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */
2164 
2165     /* Make sure that y^2 == x^3 + ax + b */
2166     return (vli_cmp(tmp1, tmp2) == 0);
2167 }
2168 
2169 int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES],
2170                             uint8_t public_key[uECC_BYTES * 2]) {
2171     uECC_word_t private[uECC_WORDS];
2172     EccPoint public;
2173 
2174     vli_bytesToNative(private, private_key);
2175 
2176     if (!EccPoint_compute_public_key(&public, private)) {
2177         return 0;
2178     }
2179 
2180     vli_nativeToBytes(public_key, public.x);
2181     vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2182     return 1;
2183 }
2184 
2185 int uECC_bytes(void) {
2186     return uECC_BYTES;
2187 }
2188 
2189 int uECC_curve(void) {
2190     return uECC_CURVE;
2191 }
2192 
2193 /* -------- ECDSA code -------- */
2194 
2195 #ifdef ENABLE_MICRO_ECC_ECDSA
2196 
2197 #if (uECC_CURVE == uECC_secp160r1)
2198 static void vli_clear_n(uECC_word_t *vli) {
2199     vli_clear(vli);
2200     vli[uECC_N_WORDS - 1] = 0;
2201 }
2202 
2203 static uECC_word_t vli_isZero_n(const uECC_word_t *vli) {
2204     if (vli[uECC_N_WORDS - 1]) {
2205         return 0;
2206     }
2207     return vli_isZero(vli);
2208 }
2209 
2210 static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) {
2211     vli_set(dest, src);
2212     dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1];
2213 }
2214 
2215 static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) {
2216     if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) {
2217         return 1;
2218     } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) {
2219         return -1;
2220     }
2221     return vli_cmp(left, right);
2222 }
2223 
2224 static void vli_rshift1_n(uECC_word_t *vli) {
2225     vli_rshift1(vli);
2226     vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1);
2227     vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1;
2228 }
2229 
2230 static uECC_word_t vli_add_n(uECC_word_t *result,
2231                              const uECC_word_t *left,
2232                              const uECC_word_t *right) {
2233     uECC_word_t carry = vli_add(result, left, right);
2234     uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry;
2235     if (sum != left[uECC_N_WORDS - 1]) {
2236         carry = (sum < left[uECC_N_WORDS - 1]);
2237     }
2238     result[uECC_N_WORDS - 1] = sum;
2239     return carry;
2240 }
2241 
2242 static uECC_word_t vli_sub_n(uECC_word_t *result,
2243                              const uECC_word_t *left,
2244                              const uECC_word_t *right) {
2245     uECC_word_t borrow = vli_sub(result, left, right);
2246     uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow;
2247     if (diff != left[uECC_N_WORDS - 1]) {
2248         borrow = (diff > left[uECC_N_WORDS - 1]);
2249     }
2250     result[uECC_N_WORDS - 1] = diff;
2251     return borrow;
2252 }
2253 
2254 #if !muladd_exists
2255 static void muladd(uECC_word_t a,
2256                    uECC_word_t b,
2257                    uECC_word_t *r0,
2258                    uECC_word_t *r1,
2259                    uECC_word_t *r2) {
2260     uECC_dword_t p = (uECC_dword_t)a * b;
2261     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
2262     r01 += p;
2263     *r2 += (r01 < p);
2264     *r1 = r01 >> uECC_WORD_BITS;
2265     *r0 = (uECC_word_t)r01;
2266 }
2267 #define muladd_exists 1
2268 #endif
2269 
2270 static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2271     uECC_word_t r0 = 0;
2272     uECC_word_t r1 = 0;
2273     uECC_word_t r2 = 0;
2274     wordcount_t i, k;
2275 
2276     for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) {
2277         wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS);
2278         wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1);
2279         for (i = min; i <= max; ++i) {
2280             muladd(left[i], right[k - i], &r0, &r1, &r2);
2281         }
2282         result[k] = r0;
2283         r0 = r1;
2284         r1 = r2;
2285         r2 = 0;
2286     }
2287     result[uECC_N_WORDS * 2 - 1] = r0;
2288 }
2289 
2290 static void vli_modAdd_n(uECC_word_t *result,
2291                          const uECC_word_t *left,
2292                          const uECC_word_t *right,
2293                          const uECC_word_t *mod) {
2294     uECC_word_t carry = vli_add_n(result, left, right);
2295     if (carry || vli_cmp_n(result, mod) >= 0) {
2296         vli_sub_n(result, result, mod);
2297     }
2298 }
2299 
2300 static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
2301     uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS];
2302     uECC_word_t carry;
2303     cmpresult_t cmpResult;
2304 
2305     if (vli_isZero_n(input)) {
2306         vli_clear_n(result);
2307         return;
2308     }
2309 
2310     vli_set_n(a, input);
2311     vli_set_n(b, mod);
2312     vli_clear_n(u);
2313     u[0] = 1;
2314     vli_clear_n(v);
2315     while ((cmpResult = vli_cmp_n(a, b)) != 0) {
2316         carry = 0;
2317         if (EVEN(a)) {
2318             vli_rshift1_n(a);
2319             if (!EVEN(u)) {
2320                 carry = vli_add_n(u, u, mod);
2321             }
2322             vli_rshift1_n(u);
2323             if (carry) {
2324                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2325             }
2326         } else if (EVEN(b)) {
2327             vli_rshift1_n(b);
2328             if (!EVEN(v)) {
2329                 carry = vli_add_n(v, v, mod);
2330             }
2331             vli_rshift1_n(v);
2332             if (carry) {
2333                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2334             }
2335         } else if (cmpResult > 0) {
2336             vli_sub_n(a, a, b);
2337             vli_rshift1_n(a);
2338             if (vli_cmp_n(u, v) < 0) {
2339                 vli_add_n(u, u, mod);
2340             }
2341             vli_sub_n(u, u, v);
2342             if (!EVEN(u)) {
2343                 carry = vli_add_n(u, u, mod);
2344             }
2345             vli_rshift1_n(u);
2346             if (carry) {
2347                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2348             }
2349         } else {
2350             vli_sub_n(b, b, a);
2351             vli_rshift1_n(b);
2352             if (vli_cmp_n(v, u) < 0) {
2353                 vli_add_n(v, v, mod);
2354             }
2355             vli_sub_n(v, v, u);
2356             if (!EVEN(v)) {
2357                 carry = vli_add_n(v, v, mod);
2358             }
2359             vli_rshift1_n(v);
2360             if (carry) {
2361                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2362             }
2363         }
2364     }
2365     vli_set_n(result, u);
2366 }
2367 
2368 static void vli2_rshift1_n(uECC_word_t *vli) {
2369     vli_rshift1_n(vli);
2370     vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1);
2371     vli_rshift1_n(vli + uECC_N_WORDS);
2372 }
2373 
2374 static uECC_word_t vli2_sub_n(uECC_word_t *result,
2375                               const uECC_word_t *left,
2376                               const uECC_word_t *right) {
2377     uECC_word_t borrow = 0;
2378     wordcount_t i;
2379     for (i = 0; i < uECC_N_WORDS * 2; ++i) {
2380         uECC_word_t diff = left[i] - right[i] - borrow;
2381         if (diff != left[i]) {
2382             borrow = (diff > left[i]);
2383         }
2384         result[i] = diff;
2385     }
2386     return borrow;
2387 }
2388 
2389 /* Computes result = (left * right) % curve_n. */
2390 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2391     bitcount_t i;
2392     uECC_word_t product[2 * uECC_N_WORDS];
2393     uECC_word_t modMultiple[2 * uECC_N_WORDS];
2394     uECC_word_t tmp[2 * uECC_N_WORDS];
2395     uECC_word_t *v[2] = {tmp, product};
2396     uECC_word_t index = 1;
2397 
2398     vli_mult_n(product, left, right);
2399     vli_clear_n(modMultiple);
2400     vli_set(modMultiple + uECC_N_WORDS + 1, curve_n);
2401     vli_rshift1(modMultiple + uECC_N_WORDS + 1);
2402     modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2403     modMultiple[uECC_N_WORDS] = HIGH_BIT_SET;
2404 
2405     for (i = 0;
2406          i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1));
2407          ++i) {
2408         uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple);
2409         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2410         vli2_rshift1_n(modMultiple);
2411     }
2412     vli_set_n(result, v[index]);
2413 }
2414 
2415 #else
2416 
2417 #define vli_cmp_n vli_cmp
2418 #define vli_modInv_n vli_modInv
2419 #define vli_modAdd_n vli_modAdd
2420 
2421 static void vli2_rshift1(uECC_word_t *vli) {
2422     vli_rshift1(vli);
2423     vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1);
2424     vli_rshift1(vli + uECC_WORDS);
2425 }
2426 
2427 static uECC_word_t vli2_sub(uECC_word_t *result,
2428                             const uECC_word_t *left,
2429                             const uECC_word_t *right) {
2430     uECC_word_t borrow = 0;
2431     wordcount_t i;
2432     for (i = 0; i < uECC_WORDS * 2; ++i) {
2433         uECC_word_t diff = left[i] - right[i] - borrow;
2434         if (diff != left[i]) {
2435             borrow = (diff > left[i]);
2436         }
2437         result[i] = diff;
2438     }
2439     return borrow;
2440 }
2441 
2442 /* Computes result = (left * right) % curve_n. */
2443 static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2444     uECC_word_t product[2 * uECC_WORDS];
2445     uECC_word_t modMultiple[2 * uECC_WORDS];
2446     uECC_word_t tmp[2 * uECC_WORDS];
2447     uECC_word_t *v[2] = {tmp, product};
2448     bitcount_t i;
2449     uECC_word_t index = 1;
2450 
2451     vli_mult(product, left, right);
2452     vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */
2453     vli_clear(modMultiple);
2454 
2455     for (i = 0; i <= uECC_BYTES * 8; ++i) {
2456         uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple);
2457         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2458         vli2_rshift1(modMultiple);
2459     }
2460     vli_set(result, v[index]);
2461 }
2462 #endif /* (uECC_CURVE != uECC_secp160r1) */
2463 
2464 static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES],
2465                             const uint8_t message_hash[uECC_BYTES],
2466                             uECC_word_t k[uECC_N_WORDS],
2467                             uint8_t signature[uECC_BYTES*2]) {
2468     uECC_word_t tmp[uECC_N_WORDS];
2469     uECC_word_t s[uECC_N_WORDS];
2470     uECC_word_t *k2[2] = {tmp, s};
2471     EccPoint p;
2472     uECC_word_t carry;
2473     uECC_word_t tries;
2474 
2475     /* Make sure 0 < k < curve_n */
2476     if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) {
2477         return 0;
2478     }
2479 
2480 #if (uECC_CURVE == uECC_secp160r1)
2481     /* Make sure that we don't leak timing information about k.
2482        See http://eprint.iacr.org/2011/232.pdf */
2483     vli_add_n(tmp, k, curve_n);
2484     carry = (tmp[uECC_WORDS] & 0x02);
2485     vli_add_n(s, tmp, curve_n);
2486 
2487     /* p = k * G */
2488     EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 2);
2489 #else
2490     /* Make sure that we don't leak timing information about k.
2491        See http://eprint.iacr.org/2011/232.pdf */
2492     carry = vli_add(tmp, k, curve_n);
2493     vli_add(s, tmp, curve_n);
2494 
2495     /* p = k * G */
2496     EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 1);
2497 
2498     /* r = x1 (mod n) */
2499     if (vli_cmp(curve_n, p.x) != 1) {
2500         vli_sub(p.x, p.x, curve_n);
2501     }
2502 #endif
2503     if (vli_isZero(p.x)) {
2504         return 0;
2505     }
2506 
2507     // Attempt to get a random number to prevent side channel analysis of k.
2508     // If the RNG fails every time (eg it was not defined), we continue so that
2509     // deterministic signing can still work (with reduced security) without
2510     // an RNG defined.
2511     carry = 0; // use to signal that the RNG succeeded at least once.
2512     for (tries = 0; tries < MAX_TRIES; ++tries) {
2513         if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) {
2514             continue;
2515         }
2516         carry = 1;
2517         if (!vli_isZero(tmp)) {
2518             break;
2519         }
2520     }
2521     if (!carry) {
2522         vli_clear(tmp);
2523         tmp[0] = 1;
2524     }
2525 
2526     /* Prevent side channel analysis of vli_modInv() to determine
2527        bits of k / the private key by premultiplying by a random number */
2528     vli_modMult_n(k, k, tmp); /* k' = rand * k */
2529     vli_modInv_n(k, k, curve_n); /* k = 1 / k' */
2530     vli_modMult_n(k, k, tmp); /* k = 1 / k */
2531 
2532     vli_nativeToBytes(signature, p.x); /* store r */
2533 
2534     tmp[uECC_N_WORDS - 1] = 0;
2535     vli_bytesToNative(tmp, private_key); /* tmp = d */
2536     s[uECC_N_WORDS - 1] = 0;
2537     vli_set(s, p.x);
2538     vli_modMult_n(s, tmp, s); /* s = r*d */
2539 
2540     vli_bytesToNative(tmp, message_hash);
2541     vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */
2542     vli_modMult_n(s, s, k); /* s = (e + r*d) / k */
2543 #if (uECC_CURVE == uECC_secp160r1)
2544     if (s[uECC_N_WORDS - 1]) {
2545         return 0;
2546     }
2547 #endif
2548     vli_nativeToBytes(signature + uECC_BYTES, s);
2549     return 1;
2550 }
2551 
2552 int uECC_sign(const uint8_t private_key[uECC_BYTES],
2553               const uint8_t message_hash[uECC_BYTES],
2554               uint8_t signature[uECC_BYTES*2]) {
2555     uECC_word_t k[uECC_N_WORDS];
2556     uECC_word_t tries;
2557 
2558     for (tries = 0; tries < MAX_TRIES; ++tries) {
2559         if(g_rng_function((uint8_t *)k, sizeof(k))) {
2560         #if (uECC_CURVE == uECC_secp160r1)
2561             k[uECC_WORDS] &= 0x01;
2562         #endif
2563             if (uECC_sign_with_k(private_key, message_hash, k, signature)) {
2564                 return 1;
2565             }
2566         }
2567     }
2568     return 0;
2569 }
2570 
2571 /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
2572    the same size as the hash result size. */
2573 static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) {
2574     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2575     unsigned i;
2576     for (i = 0; i < hash_context->result_size; ++i)
2577         pad[i] = K[i] ^ 0x36;
2578     for (; i < hash_context->block_size; ++i)
2579         pad[i] = 0x36;
2580 
2581     hash_context->init_hash(hash_context);
2582     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2583 }
2584 
2585 static void HMAC_update(uECC_HashContext *hash_context,
2586                         const uint8_t *message,
2587                         unsigned message_size) {
2588     hash_context->update_hash(hash_context, message, message_size);
2589 }
2590 
2591 static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) {
2592     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2593     unsigned i;
2594     for (i = 0; i < hash_context->result_size; ++i)
2595         pad[i] = K[i] ^ 0x5c;
2596     for (; i < hash_context->block_size; ++i)
2597         pad[i] = 0x5c;
2598 
2599     hash_context->finish_hash(hash_context, result);
2600 
2601     hash_context->init_hash(hash_context);
2602     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2603     hash_context->update_hash(hash_context, result, hash_context->result_size);
2604     hash_context->finish_hash(hash_context, result);
2605 }
2606 
2607 /* V = HMAC_K(V) */
2608 static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) {
2609     HMAC_init(hash_context, K);
2610     HMAC_update(hash_context, V, hash_context->result_size);
2611     HMAC_finish(hash_context, K, V);
2612 }
2613 
2614 /* Deterministic signing, similar to RFC 6979. Differences are:
2615     * We just use (truncated) H(m) directly rather than bits2octets(H(m))
2616       (it is not reduced modulo curve_n).
2617     * We generate a value for k (aka T) directly rather than converting endianness.
2618 
2619    Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */
2620 int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES],
2621                             const uint8_t message_hash[uECC_BYTES],
2622                             uECC_HashContext *hash_context,
2623                             uint8_t signature[uECC_BYTES*2]) {
2624     uint8_t *K = hash_context->tmp;
2625     uint8_t *V = K + hash_context->result_size;
2626     uECC_word_t tries;
2627     unsigned i;
2628     for (i = 0; i < hash_context->result_size; ++i) {
2629         V[i] = 0x01;
2630         K[i] = 0;
2631     }
2632 
2633     // K = HMAC_K(V || 0x00 || int2octets(x) || h(m))
2634     HMAC_init(hash_context, K);
2635     V[hash_context->result_size] = 0x00;
2636     HMAC_update(hash_context, V, hash_context->result_size + 1);
2637     HMAC_update(hash_context, private_key, uECC_BYTES);
2638     HMAC_update(hash_context, message_hash, uECC_BYTES);
2639     HMAC_finish(hash_context, K, K);
2640 
2641     update_V(hash_context, K, V);
2642 
2643     // K = HMAC_K(V || 0x01 || int2octets(x) || h(m))
2644     HMAC_init(hash_context, K);
2645     V[hash_context->result_size] = 0x01;
2646     HMAC_update(hash_context, V, hash_context->result_size + 1);
2647     HMAC_update(hash_context, private_key, uECC_BYTES);
2648     HMAC_update(hash_context, message_hash, uECC_BYTES);
2649     HMAC_finish(hash_context, K, K);
2650 
2651     update_V(hash_context, K, V);
2652 
2653     for (tries = 0; tries < MAX_TRIES; ++tries) {
2654         uECC_word_t T[uECC_N_WORDS];
2655         uint8_t *T_ptr = (uint8_t *)T;
2656         unsigned T_bytes = 0;
2657         while (T_bytes < sizeof(T)) {
2658             update_V(hash_context, K, V);
2659             for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) {
2660                 T_ptr[T_bytes] = V[i];
2661             }
2662         }
2663     #if (uECC_CURVE == uECC_secp160r1)
2664         T[uECC_WORDS] &= 0x01;
2665     #endif
2666 
2667         if (uECC_sign_with_k(private_key, message_hash, T, signature)) {
2668             return 1;
2669         }
2670 
2671         // K = HMAC_K(V || 0x00)
2672         HMAC_init(hash_context, K);
2673         V[hash_context->result_size] = 0x00;
2674         HMAC_update(hash_context, V, hash_context->result_size + 1);
2675         HMAC_finish(hash_context, K, K);
2676 
2677         update_V(hash_context, K, V);
2678     }
2679     return 0;
2680 }
2681 
2682 static bitcount_t smax(bitcount_t a, bitcount_t b) {
2683     return (a > b ? a : b);
2684 }
2685 
2686 int uECC_verify(const uint8_t public_key[uECC_BYTES*2],
2687                 const uint8_t hash[uECC_BYTES],
2688                 const uint8_t signature[uECC_BYTES*2]) {
2689     uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS];
2690     uECC_word_t z[uECC_N_WORDS];
2691     EccPoint public, sum;
2692     uECC_word_t rx[uECC_WORDS];
2693     uECC_word_t ry[uECC_WORDS];
2694     uECC_word_t tx[uECC_WORDS];
2695     uECC_word_t ty[uECC_WORDS];
2696     uECC_word_t tz[uECC_WORDS];
2697     const EccPoint *points[4];
2698     const EccPoint *point;
2699     bitcount_t numBits;
2700     bitcount_t i;
2701     uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS];
2702     r[uECC_N_WORDS - 1] = 0;
2703     s[uECC_N_WORDS - 1] = 0;
2704 
2705     vli_bytesToNative(public.x, public_key);
2706     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2707     vli_bytesToNative(r, signature);
2708     vli_bytesToNative(s, signature + uECC_BYTES);
2709 
2710     if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */
2711         return 0;
2712     }
2713 
2714 #if (uECC_CURVE != uECC_secp160r1)
2715     if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */
2716         return 0;
2717     }
2718 #endif
2719 
2720     /* Calculate u1 and u2. */
2721     vli_modInv_n(z, s, curve_n); /* Z = s^-1 */
2722     u1[uECC_N_WORDS - 1] = 0;
2723     vli_bytesToNative(u1, hash);
2724     vli_modMult_n(u1, u1, z); /* u1 = e/s */
2725     vli_modMult_n(u2, r, z); /* u2 = r/s */
2726 
2727     /* Calculate sum = G + Q. */
2728     vli_set(sum.x, public.x);
2729     vli_set(sum.y, public.y);
2730     vli_set(tx, curve_G.x);
2731     vli_set(ty, curve_G.y);
2732     vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */
2733     XYcZ_add(tx, ty, sum.x, sum.y);
2734     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2735     apply_z(sum.x, sum.y, z);
2736 
2737     /* Use Shamir's trick to calculate u1*G + u2*Q */
2738     points[0] = 0;
2739     points[1] = &curve_G;
2740     points[2] = &public;
2741     points[3] = &sum;
2742     numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS));
2743 
2744     point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)];
2745     vli_set(rx, point->x);
2746     vli_set(ry, point->y);
2747     vli_clear(z);
2748     z[0] = 1;
2749 
2750     for (i = numBits - 2; i >= 0; --i) {
2751         uECC_word_t index;
2752         EccPoint_double_jacobian(rx, ry, z);
2753 
2754         index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
2755         point = points[index];
2756         if (point) {
2757             vli_set(tx, point->x);
2758             vli_set(ty, point->y);
2759             apply_z(tx, ty, z);
2760             vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */
2761             XYcZ_add(tx, ty, rx, ry);
2762             vli_modMult_fast(z, z, tz);
2763         }
2764     }
2765 
2766     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2767     apply_z(rx, ry, z);
2768 
2769     /* v = x1 (mod n) */
2770 #if (uECC_CURVE != uECC_secp160r1)
2771     if (vli_cmp(curve_n, rx) != 1) {
2772         vli_sub(rx, rx, curve_n);
2773     }
2774 #endif
2775 
2776     /* Accept only if v == r. */
2777     return vli_equal(rx, r);
2778 }
2779 
2780 #endif
2781