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