1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
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
19 #define uECC_PLATFORM uECC_arch_other
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
29 #define uECC_WORD_SIZE 4
33 #if (uECC_CURVE == uECC_secp160r1) && (uECC_WORD_SIZE == 8)
35 #define uECC_WORD_SIZE 4
36 #if (uECC_PLATFORM == uECC_x86_64)
38 #define uECC_PLATFORM uECC_x86
42 #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8)
43 #error "Unsupported value for uECC_WORD_SIZE"
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")
49 #define uECC_WORD_SIZE 1
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")
55 #define uECC_WORD_SIZE 4
58 #if __STDC_VERSION__ >= 199901L
59 #define RESTRICT restrict
64 #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302)
65 #define SUPPORTS_INT128 1
67 #define SUPPORTS_INT128 0
72 #if (uECC_WORD_SIZE == 1)
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;
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
86 #define uECC_WORDS_1 20
87 #define uECC_WORDS_2 24
88 #define uECC_WORDS_3 32
89 #define uECC_WORDS_4 32
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
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}
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}
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}}
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}}
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}}
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}}
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}
177 #elif (uECC_WORD_SIZE == 4)
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;
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
191 #define uECC_WORDS_1 5
192 #define uECC_WORDS_2 6
193 #define uECC_WORDS_3 8
194 #define uECC_WORDS_4 8
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
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}
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}
211 #define Curve_G_1 { \
212 {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \
213 {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}}
215 #define Curve_G_2 { \
216 {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \
217 {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}}
219 #define Curve_G_3 { \
220 {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81, 0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \
221 {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357, 0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}}
223 #define Curve_G_4 { \
224 {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB, 0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \
225 {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448, 0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}}
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}
232 #elif (uECC_WORD_SIZE == 8)
234 typedef uint64_t uECC_word_t;
236 typedef unsigned __int128 uECC_dword_t;
238 typedef unsigned wordcount_t;
239 typedef int swordcount_t;
240 typedef int bitcount_t;
241 typedef int cmpresult_t;
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
248 #define uECC_WORDS_1 3
249 #define uECC_WORDS_2 3
250 #define uECC_WORDS_3 4
251 #define uECC_WORDS_4 4
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
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}
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}
268 #define Curve_G_1 { \
269 {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \
270 {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}}
272 #define Curve_G_2 { \
273 {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \
274 {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}}
276 #define Curve_G_3 { \
277 {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \
278 {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}}
280 #define Curve_G_4 { \
281 {0x59F2815B16F81798, 0x029BFCDB2DCE28D9, 0x55A06295CE870B07, 0x79BE667EF9DCBBAC}, \
282 {0x9C47D08FFB10D4B8, 0xFD17B448A6855419, 0x5DA4FBFC0E1108A8, 0x483ADA7726A3C465}}
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}
289 #endif /* (uECC_WORD_SIZE == 8) */
291 #define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE)
292 #define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE)
294 typedef struct EccPoint
296 uECC_word_t x[uECC_WORDS];
297 uECC_word_t y[uECC_WORDS];
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);
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);
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);
325 #if (defined(_WIN32) || defined(_WIN64))
328 #define WIN32_LEAN_AND_MEAN
330 #include <wincrypt.h>
332 static int default_RNG(uint8_t *p_dest, unsigned p_size)
335 if(!CryptAcquireContext(&l_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
340 CryptGenRandom(l_prov, p_size, (BYTE *)p_dest);
341 CryptReleaseContext(l_prov, 0);
346 #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \
347 (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)
349 /* Some POSIX-like system with /dev/urandom or /dev/random. */
350 #include <sys/types.h>
358 static int default_RNG(uint8_t *p_dest, unsigned p_size)
360 int l_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
363 l_fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
370 char *l_ptr = (char *)p_dest;
371 size_t l_left = p_size;
374 int l_read = read(l_fd, l_ptr, l_left);
388 #else /* Some other platform */
390 static int default_RNG(uint8_t *p_dest, unsigned p_size)
397 static uECC_RNG_Function g_rng = &default_RNG;
399 void uECC_set_rng(uECC_RNG_Function p_rng)
404 #ifdef __GNUC__ /* Only support GCC inline asm for now */
405 #if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
406 #include "asm_avr.inc"
409 #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || uECC_PLATFORM == uECC_arm_thumb2))
410 #include "asm_arm.inc"
415 static void vli_clear(uECC_word_t *p_vli)
418 for(i = 0; i < uECC_WORDS; ++i)
425 /* Returns 1 if p_vli == 0, 0 otherwise. */
427 static uECC_word_t vli_isZero(const uECC_word_t *p_vli)
430 for(i = 0; i < uECC_WORDS; ++i)
441 /* Returns nonzero if bit p_bit of p_vli is set. */
443 static uECC_word_t vli_testBit(const uECC_word_t *p_vli, bitcount_t p_bit)
445 return (p_vli[p_bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (p_bit & uECC_WORD_BITS_MASK)));
449 /* Counts the number of words in p_vli. */
451 static wordcount_t vli_numDigits(const uECC_word_t *p_vli, wordcount_t p_maxWords)
454 /* Search from the end until we find a non-zero digit.
455 We do it in reverse because we expect that most digits will be nonzero. */
456 for(i = p_maxWords-1; i >= 0 && p_vli[i] == 0; --i)
463 /* Counts the number of bits required to represent p_vli. */
464 static bitcount_t vli_numBits(const uECC_word_t *p_vli, wordcount_t p_maxWords)
469 wordcount_t l_numDigits = vli_numDigits(p_vli, p_maxWords);
475 l_digit = p_vli[l_numDigits - 1];
476 for(i = 0; l_digit; ++i)
481 return (((bitcount_t)(l_numDigits - 1) << uECC_WORD_BITS_SHIFT) + i);
483 #endif /* !asm_numBits */
485 /* Sets p_dest = p_src. */
487 static void vli_set(uECC_word_t *p_dest, const uECC_word_t *p_src)
490 for(i=0; i<uECC_WORDS; ++i)
492 p_dest[i] = p_src[i];
497 /* Returns sign of p_left - p_right. */
499 static cmpresult_t vli_cmp(uECC_word_t *p_left, uECC_word_t *p_right)
502 for(i = uECC_WORDS-1; i >= 0; --i)
504 if(p_left[i] > p_right[i])
508 else if(p_left[i] < p_right[i])
517 /* Computes p_vli = p_vli >> 1. */
519 static void vli_rshift1(uECC_word_t *p_vli)
521 uECC_word_t *l_end = p_vli;
522 uECC_word_t l_carry = 0;
525 while(p_vli-- > l_end)
527 uECC_word_t l_temp = *p_vli;
528 *p_vli = (l_temp >> 1) | l_carry;
529 l_carry = l_temp << (uECC_WORD_BITS - 1);
534 /* Computes p_result = p_left + p_right, returning carry. Can modify in place. */
536 static uECC_word_t vli_add(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
538 uECC_word_t l_carry = 0;
540 for(i = 0; i < uECC_WORDS; ++i)
542 uECC_word_t l_sum = p_left[i] + p_right[i] + l_carry;
543 if(l_sum != p_left[i])
545 l_carry = (l_sum < p_left[i]);
553 /* Computes p_result = p_left - p_right, returning borrow. Can modify in place. */
555 static uECC_word_t vli_sub(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
557 uECC_word_t l_borrow = 0;
559 for(i = 0; i < uECC_WORDS; ++i)
561 uECC_word_t l_diff = p_left[i] - p_right[i] - l_borrow;
562 if(l_diff != p_left[i])
564 l_borrow = (l_diff > p_left[i]);
566 p_result[i] = l_diff;
572 #if (!asm_mult || !asm_square || uECC_CURVE == uECC_secp256k1)
573 static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, uECC_word_t *r1, uECC_word_t *r2)
575 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
576 uint64_t a0 = a & 0xffffffffull;
577 uint64_t a1 = a >> 32;
578 uint64_t b0 = b & 0xffffffffull;
579 uint64_t b1 = b >> 32;
581 uint64_t i0 = a0 * b0;
582 uint64_t i1 = a0 * b1;
583 uint64_t i2 = a1 * b0;
584 uint64_t i3 = a1 * b1;
592 i3 += 0x100000000ull;
595 p0 = (i0 & 0xffffffffull) | (i2 << 32);
596 p1 = i3 + (i2 >> 32);
599 *r1 += (p1 + (*r0 < p0));
600 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
602 uECC_dword_t p = (uECC_dword_t)a * b;
603 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
606 *r1 = r01 >> uECC_WORD_BITS;
607 *r0 = (uECC_word_t)r01;
610 #define muladd_exists 1
614 static void vli_mult(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
622 /* Compute each digit of p_result in sequence, maintaining the carries. */
623 for(k = 0; k < uECC_WORDS; ++k)
625 for(i = 0; i <= k; ++i)
627 muladd(p_left[i], p_right[k-i], &r0, &r1, &r2);
634 for(k = uECC_WORDS; k < uECC_WORDS*2 - 1; ++k)
636 for(i = (k + 1) - uECC_WORDS; i<uECC_WORDS; ++i)
638 muladd(p_left[i], p_right[k-i], &r0, &r1, &r2);
646 p_result[uECC_WORDS*2 - 1] = r0;
653 static void mul2add(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, uECC_word_t *r1, uECC_word_t *r2)
655 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
656 uint64_t a0 = a & 0xffffffffull;
657 uint64_t a1 = a >> 32;
658 uint64_t b0 = b & 0xffffffffull;
659 uint64_t b1 = b >> 32;
661 uint64_t i0 = a0 * b0;
662 uint64_t i1 = a0 * b1;
663 uint64_t i2 = a1 * b0;
664 uint64_t i3 = a1 * b1;
672 i3 += 0x100000000ull;
675 p0 = (i0 & 0xffffffffull) | (i2 << 32);
676 p1 = i3 + (i2 >> 32);
679 p1 = (p1 << 1) | (p0 >> 63);
683 *r1 += (p1 + (*r0 < p0));
684 *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
686 uECC_dword_t p = (uECC_dword_t)a * b;
687 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
688 *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
692 *r1 = r01 >> uECC_WORD_BITS;
693 *r0 = (uECC_word_t)r01;
697 static void vli_square(uECC_word_t *p_result, uECC_word_t *p_left)
705 for(k = 0; k < uECC_WORDS*2 - 1; ++k)
707 uECC_word_t l_min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS);
708 for(i = l_min; i<=k && i<=k-i; ++i)
712 mul2add(p_left[i], p_left[k-i], &r0, &r1, &r2);
716 muladd(p_left[i], p_left[k-i], &r0, &r1, &r2);
725 p_result[uECC_WORDS*2 - 1] = r0;
729 #else /* uECC_SQUARE_FUNC */
731 #define vli_square(result, left, size) vli_mult((result), (left), (left), (size))
733 #endif /* uECC_SQUARE_FUNC */
736 /* Computes p_result = (p_left + p_right) % p_mod.
737 Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
739 static void vli_modAdd(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right, uECC_word_t *p_mod)
741 uECC_word_t l_carry = vli_add(p_result, p_left, p_right);
742 if(l_carry || vli_cmp(p_result, p_mod) >= 0)
743 { /* p_result > p_mod (p_result = p_mod + remainder), so subtract p_mod to get remainder. */
744 vli_sub(p_result, p_result, p_mod);
749 /* Computes p_result = (p_left - p_right) % p_mod.
750 Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
752 static void vli_modSub(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right, uECC_word_t *p_mod)
754 uECC_word_t l_borrow = vli_sub(p_result, p_left, p_right);
756 { /* In this case, p_result == -diff == (max int) - diff.
757 Since -x % d == d - x, we can get the correct result from p_result + p_mod (with overflow). */
758 vli_add(p_result, p_result, p_mod);
764 #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p)
769 #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1)
770 /* omega_mult() is defined farther below for the different curves / word sizes */
771 static void omega_mult(uECC_word_t * RESTRICT p_result, uECC_word_t * RESTRICT p_right);
773 /* Computes p_result = p_product % curve_p
774 see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354
776 Note that this only works if log2(omega) < log2(p)/2 */
777 static void vli_mmod_fast(uECC_word_t *RESTRICT p_result, uECC_word_t *RESTRICT p_product)
779 uECC_word_t l_tmp[2*uECC_WORDS];
783 vli_clear(l_tmp + uECC_WORDS);
785 omega_mult(l_tmp, p_product + uECC_WORDS); /* (Rq, q) = q * c */
787 l_carry = vli_add(p_result, p_product, l_tmp); /* (C, r) = r + q */
788 vli_clear(p_product);
789 omega_mult(p_product, l_tmp + uECC_WORDS); /* Rq*c */
790 l_carry += vli_add(p_result, p_result, p_product); /* (C1, r) = r + Rq*c */
795 vli_sub(p_result, p_result, curve_p);
798 if(vli_cmp(p_result, curve_p) > 0)
800 vli_sub(p_result, p_result, curve_p);
806 #if uECC_CURVE == uECC_secp160r1
808 #if uECC_WORD_SIZE == 1
809 static void omega_mult(uint8_t * RESTRICT p_result, uint8_t * RESTRICT p_right)
814 /* Multiply by (2^31 + 1). */
815 vli_set(p_result + 4, p_right); /* 2^32 */
816 vli_rshift1(p_result + 4); /* 2^31 */
817 p_result[3] = p_right[0] << 7; /* get last bit from shift */
819 l_carry = vli_add(p_result, p_result, p_right); /* 2^31 + 1 */
820 for(i = uECC_WORDS; l_carry; ++i)
822 uint16_t l_sum = (uint16_t)p_result[i] + l_carry;
823 p_result[i] = (uint8_t)l_sum;
824 l_carry = l_sum >> 8;
827 #elif uECC_WORD_SIZE == 4
828 static void omega_mult(uint32_t * RESTRICT p_result, uint32_t * RESTRICT p_right)
833 /* Multiply by (2^31 + 1). */
834 vli_set(p_result + 1, p_right); /* 2^32 */
835 vli_rshift1(p_result + 1); /* 2^31 */
836 p_result[0] = p_right[0] << 31; /* get last bit from shift */
838 l_carry = vli_add(p_result, p_result, p_right); /* 2^31 + 1 */
839 for(i = uECC_WORDS; l_carry; ++i)
841 uint64_t l_sum = (uint64_t)p_result[i] + l_carry;
842 p_result[i] = (uint32_t)l_sum;
843 l_carry = l_sum >> 32;
846 #endif /* uECC_WORD_SIZE */
848 #elif uECC_CURVE == uECC_secp192r1
850 /* Computes p_result = p_product % curve_p.
851 See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
852 #if uECC_WORD_SIZE == 1
853 static void vli_mmod_fast(uint8_t *RESTRICT p_result, uint8_t *RESTRICT p_product)
855 uint8_t l_tmp[uECC_WORDS];
858 vli_set(p_result, p_product);
860 vli_set(l_tmp, &p_product[24]);
861 l_carry = vli_add(p_result, p_result, l_tmp);
863 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;
864 l_tmp[8] = p_product[24]; l_tmp[9] = p_product[25]; l_tmp[10] = p_product[26]; l_tmp[11] = p_product[27];
865 l_tmp[12] = p_product[28]; l_tmp[13] = p_product[29]; l_tmp[14] = p_product[30]; l_tmp[15] = p_product[31];
866 l_tmp[16] = p_product[32]; l_tmp[17] = p_product[33]; l_tmp[18] = p_product[34]; l_tmp[19] = p_product[35];
867 l_tmp[20] = p_product[36]; l_tmp[21] = p_product[37]; l_tmp[22] = p_product[38]; l_tmp[23] = p_product[39];
868 l_carry += vli_add(p_result, p_result, l_tmp);
870 l_tmp[0] = l_tmp[8] = p_product[40];
871 l_tmp[1] = l_tmp[9] = p_product[41];
872 l_tmp[2] = l_tmp[10] = p_product[42];
873 l_tmp[3] = l_tmp[11] = p_product[43];
874 l_tmp[4] = l_tmp[12] = p_product[44];
875 l_tmp[5] = l_tmp[13] = p_product[45];
876 l_tmp[6] = l_tmp[14] = p_product[46];
877 l_tmp[7] = l_tmp[15] = p_product[47];
878 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;
879 l_carry += vli_add(p_result, p_result, l_tmp);
881 while(l_carry || vli_cmp(curve_p, p_result) != 1)
883 l_carry -= vli_sub(p_result, p_result, curve_p);
886 #elif uECC_WORD_SIZE == 4
887 static void vli_mmod_fast(uint32_t *RESTRICT p_result, uint32_t *RESTRICT p_product)
889 uint32_t l_tmp[uECC_WORDS];
892 vli_set(p_result, p_product);
894 vli_set(l_tmp, &p_product[6]);
895 l_carry = vli_add(p_result, p_result, l_tmp);
897 l_tmp[0] = l_tmp[1] = 0;
898 l_tmp[2] = p_product[6];
899 l_tmp[3] = p_product[7];
900 l_tmp[4] = p_product[8];
901 l_tmp[5] = p_product[9];
902 l_carry += vli_add(p_result, p_result, l_tmp);
904 l_tmp[0] = l_tmp[2] = p_product[10];
905 l_tmp[1] = l_tmp[3] = p_product[11];
906 l_tmp[4] = l_tmp[5] = 0;
907 l_carry += vli_add(p_result, p_result, l_tmp);
909 while(l_carry || vli_cmp(curve_p, p_result) != 1)
911 l_carry -= vli_sub(p_result, p_result, curve_p);
915 static void vli_mmod_fast(uint64_t *RESTRICT p_result, uint64_t *RESTRICT p_product)
917 uint64_t l_tmp[uECC_WORDS];
920 vli_set(p_result, p_product);
922 vli_set(l_tmp, &p_product[3]);
923 l_carry = vli_add(p_result, p_result, l_tmp);
926 l_tmp[1] = p_product[3];
927 l_tmp[2] = p_product[4];
928 l_carry += vli_add(p_result, p_result, l_tmp);
930 l_tmp[0] = l_tmp[1] = p_product[5];
932 l_carry += vli_add(p_result, p_result, l_tmp);
934 while(l_carry || vli_cmp(curve_p, p_result) != 1)
936 l_carry -= vli_sub(p_result, p_result, curve_p);
939 #endif /* uECC_WORD_SIZE */
941 #elif uECC_CURVE == uECC_secp256r1
943 /* Computes p_result = p_product % curve_p
944 from http://www.nsa.gov/ia/_files/nist-routines.pdf */
945 #if uECC_WORD_SIZE == 1
946 static void vli_mmod_fast(uint8_t *RESTRICT p_result, uint8_t *RESTRICT p_product)
948 uint8_t l_tmp[uECC_BYTES];
952 vli_set(p_result, p_product);
955 l_tmp[0] = l_tmp[1] = l_tmp[2] = l_tmp[3] = 0;
956 l_tmp[4] = l_tmp[5] = l_tmp[6] = l_tmp[7] = 0;
957 l_tmp[8] = l_tmp[9] = l_tmp[10] = l_tmp[11] = 0;
958 l_tmp[12] = p_product[44]; l_tmp[13] = p_product[45]; l_tmp[14] = p_product[46]; l_tmp[15] = p_product[47];
959 l_tmp[16] = p_product[48]; l_tmp[17] = p_product[49]; l_tmp[18] = p_product[50]; l_tmp[19] = p_product[51];
960 l_tmp[20] = p_product[52]; l_tmp[21] = p_product[53]; l_tmp[22] = p_product[54]; l_tmp[23] = p_product[55];
961 l_tmp[24] = p_product[56]; l_tmp[25] = p_product[57]; l_tmp[26] = p_product[58]; l_tmp[27] = p_product[59];
962 l_tmp[28] = p_product[60]; l_tmp[29] = p_product[61]; l_tmp[30] = p_product[62]; l_tmp[31] = p_product[63];
963 l_carry = vli_add(l_tmp, l_tmp, l_tmp);
964 l_carry += vli_add(p_result, p_result, l_tmp);
967 l_tmp[12] = p_product[48]; l_tmp[13] = p_product[49]; l_tmp[14] = p_product[50]; l_tmp[15] = p_product[51];
968 l_tmp[16] = p_product[52]; l_tmp[17] = p_product[53]; l_tmp[18] = p_product[54]; l_tmp[19] = p_product[55];
969 l_tmp[20] = p_product[56]; l_tmp[21] = p_product[57]; l_tmp[22] = p_product[58]; l_tmp[23] = p_product[59];
970 l_tmp[24] = p_product[60]; l_tmp[25] = p_product[61]; l_tmp[26] = p_product[62]; l_tmp[27] = p_product[63];
971 l_tmp[28] = l_tmp[29] = l_tmp[30] = l_tmp[31] = 0;
972 l_carry += vli_add(l_tmp, l_tmp, l_tmp);
973 l_carry += vli_add(p_result, p_result, l_tmp);
976 l_tmp[0] = p_product[32]; l_tmp[1] = p_product[33]; l_tmp[2] = p_product[34]; l_tmp[3] = p_product[35];
977 l_tmp[4] = p_product[36]; l_tmp[5] = p_product[37]; l_tmp[6] = p_product[38]; l_tmp[7] = p_product[39];
978 l_tmp[8] = p_product[40]; l_tmp[9] = p_product[41]; l_tmp[10] = p_product[42]; l_tmp[11] = p_product[43];
979 l_tmp[12] = l_tmp[13] = l_tmp[14] = l_tmp[15] = 0;
980 l_tmp[16] = l_tmp[17] = l_tmp[18] = l_tmp[19] = 0;
981 l_tmp[20] = l_tmp[21] = l_tmp[22] = l_tmp[23] = 0;
982 l_tmp[24] = p_product[56]; l_tmp[25] = p_product[57]; l_tmp[26] = p_product[58]; l_tmp[27] = p_product[59];
983 l_tmp[28] = p_product[60]; l_tmp[29] = p_product[61]; l_tmp[30] = p_product[62]; l_tmp[31] = p_product[63];
984 l_carry += vli_add(p_result, p_result, l_tmp);
987 l_tmp[0] = p_product[36]; l_tmp[1] = p_product[37]; l_tmp[2] = p_product[38]; l_tmp[3] = p_product[39];
988 l_tmp[4] = p_product[40]; l_tmp[5] = p_product[41]; l_tmp[6] = p_product[42]; l_tmp[7] = p_product[43];
989 l_tmp[8] = p_product[44]; l_tmp[9] = p_product[45]; l_tmp[10] = p_product[46]; l_tmp[11] = p_product[47];
990 l_tmp[12] = p_product[52]; l_tmp[13] = p_product[53]; l_tmp[14] = p_product[54]; l_tmp[15] = p_product[55];
991 l_tmp[16] = p_product[56]; l_tmp[17] = p_product[57]; l_tmp[18] = p_product[58]; l_tmp[19] = p_product[59];
992 l_tmp[20] = p_product[60]; l_tmp[21] = p_product[61]; l_tmp[22] = p_product[62]; l_tmp[23] = p_product[63];
993 l_tmp[24] = p_product[52]; l_tmp[25] = p_product[53]; l_tmp[26] = p_product[54]; l_tmp[27] = p_product[55];
994 l_tmp[28] = p_product[32]; l_tmp[29] = p_product[33]; l_tmp[30] = p_product[34]; l_tmp[31] = p_product[35];
995 l_carry += vli_add(p_result, p_result, l_tmp);
998 l_tmp[0] = p_product[44]; l_tmp[1] = p_product[45]; l_tmp[2] = p_product[46]; l_tmp[3] = p_product[47];
999 l_tmp[4] = p_product[48]; l_tmp[5] = p_product[49]; l_tmp[6] = p_product[50]; l_tmp[7] = p_product[51];
1000 l_tmp[8] = p_product[52]; l_tmp[9] = p_product[53]; l_tmp[10] = p_product[54]; l_tmp[11] = p_product[55];
1001 l_tmp[12] = l_tmp[13] = l_tmp[14] = l_tmp[15] = 0;
1002 l_tmp[16] = l_tmp[17] = l_tmp[18] = l_tmp[19] = 0;
1003 l_tmp[20] = l_tmp[21] = l_tmp[22] = l_tmp[23] = 0;
1004 l_tmp[24] = p_product[32]; l_tmp[25] = p_product[33]; l_tmp[26] = p_product[34]; l_tmp[27] = p_product[35];
1005 l_tmp[28] = p_product[40]; l_tmp[29] = p_product[41]; l_tmp[30] = p_product[42]; l_tmp[31] = p_product[43];
1006 l_carry -= vli_sub(p_result, p_result, l_tmp);
1009 l_tmp[0] = p_product[48]; l_tmp[1] = p_product[49]; l_tmp[2] = p_product[50]; l_tmp[3] = p_product[51];
1010 l_tmp[4] = p_product[52]; l_tmp[5] = p_product[53]; l_tmp[6] = p_product[54]; l_tmp[7] = p_product[55];
1011 l_tmp[8] = p_product[56]; l_tmp[9] = p_product[57]; l_tmp[10] = p_product[58]; l_tmp[11] = p_product[59];
1012 l_tmp[12] = p_product[60]; l_tmp[13] = p_product[61]; l_tmp[14] = p_product[62]; l_tmp[15] = p_product[63];
1013 l_tmp[16] = l_tmp[17] = l_tmp[18] = l_tmp[19] = 0;
1014 l_tmp[20] = l_tmp[21] = l_tmp[22] = l_tmp[23] = 0;
1015 l_tmp[24] = p_product[36]; l_tmp[25] = p_product[37]; l_tmp[26] = p_product[38]; l_tmp[27] = p_product[39];
1016 l_tmp[28] = p_product[44]; l_tmp[29] = p_product[45]; l_tmp[30] = p_product[46]; l_tmp[31] = p_product[47];
1017 l_carry -= vli_sub(p_result, p_result, l_tmp);
1020 l_tmp[0] = p_product[52]; l_tmp[1] = p_product[53]; l_tmp[2] = p_product[54]; l_tmp[3] = p_product[55];
1021 l_tmp[4] = p_product[56]; l_tmp[5] = p_product[57]; l_tmp[6] = p_product[58]; l_tmp[7] = p_product[59];
1022 l_tmp[8] = p_product[60]; l_tmp[9] = p_product[61]; l_tmp[10] = p_product[62]; l_tmp[11] = p_product[63];
1023 l_tmp[12] = p_product[32]; l_tmp[13] = p_product[33]; l_tmp[14] = p_product[34]; l_tmp[15] = p_product[35];
1024 l_tmp[16] = p_product[36]; l_tmp[17] = p_product[37]; l_tmp[18] = p_product[38]; l_tmp[19] = p_product[39];
1025 l_tmp[20] = p_product[40]; l_tmp[21] = p_product[41]; l_tmp[22] = p_product[42]; l_tmp[23] = p_product[43];
1026 l_tmp[24] = l_tmp[25] = l_tmp[26] = l_tmp[27] = 0;
1027 l_tmp[28] = p_product[48]; l_tmp[29] = p_product[49]; l_tmp[30] = p_product[50]; l_tmp[31] = p_product[51];
1028 l_carry -= vli_sub(p_result, p_result, l_tmp);
1031 l_tmp[0] = p_product[56]; l_tmp[1] = p_product[57]; l_tmp[2] = p_product[58]; l_tmp[3] = p_product[59];
1032 l_tmp[4] = p_product[60]; l_tmp[5] = p_product[61]; l_tmp[6] = p_product[62]; l_tmp[7] = p_product[63];
1033 l_tmp[8] = l_tmp[9] = l_tmp[10] = l_tmp[11] = 0;
1034 l_tmp[12] = p_product[36]; l_tmp[13] = p_product[37]; l_tmp[14] = p_product[38]; l_tmp[15] = p_product[39];
1035 l_tmp[16] = p_product[40]; l_tmp[17] = p_product[41]; l_tmp[18] = p_product[42]; l_tmp[19] = p_product[43];
1036 l_tmp[20] = p_product[44]; l_tmp[21] = p_product[45]; l_tmp[22] = p_product[46]; l_tmp[23] = p_product[47];
1037 l_tmp[24] = l_tmp[25] = l_tmp[26] = l_tmp[27] = 0;
1038 l_tmp[28] = p_product[52]; l_tmp[29] = p_product[53]; l_tmp[30] = p_product[54]; l_tmp[31] = p_product[55];
1039 l_carry -= vli_sub(p_result, p_result, l_tmp);
1045 l_carry += vli_add(p_result, p_result, curve_p);
1046 } while(l_carry < 0);
1050 while(l_carry || vli_cmp(curve_p, p_result) != 1)
1052 l_carry -= vli_sub(p_result, p_result, curve_p);
1056 #elif uECC_WORD_SIZE == 4
1057 static void vli_mmod_fast(uint32_t *RESTRICT p_result, uint32_t *RESTRICT p_product)
1059 uint32_t l_tmp[uECC_WORDS];
1063 vli_set(p_result, p_product);
1066 l_tmp[0] = l_tmp[1] = l_tmp[2] = 0;
1067 l_tmp[3] = p_product[11];
1068 l_tmp[4] = p_product[12];
1069 l_tmp[5] = p_product[13];
1070 l_tmp[6] = p_product[14];
1071 l_tmp[7] = p_product[15];
1072 l_carry = vli_add(l_tmp, l_tmp, l_tmp);
1073 l_carry += vli_add(p_result, p_result, l_tmp);
1076 l_tmp[3] = p_product[12];
1077 l_tmp[4] = p_product[13];
1078 l_tmp[5] = p_product[14];
1079 l_tmp[6] = p_product[15];
1081 l_carry += vli_add(l_tmp, l_tmp, l_tmp);
1082 l_carry += vli_add(p_result, p_result, l_tmp);
1085 l_tmp[0] = p_product[8];
1086 l_tmp[1] = p_product[9];
1087 l_tmp[2] = p_product[10];
1088 l_tmp[3] = l_tmp[4] = l_tmp[5] = 0;
1089 l_tmp[6] = p_product[14];
1090 l_tmp[7] = p_product[15];
1091 l_carry += vli_add(p_result, p_result, l_tmp);
1094 l_tmp[0] = p_product[9];
1095 l_tmp[1] = p_product[10];
1096 l_tmp[2] = p_product[11];
1097 l_tmp[3] = p_product[13];
1098 l_tmp[4] = p_product[14];
1099 l_tmp[5] = p_product[15];
1100 l_tmp[6] = p_product[13];
1101 l_tmp[7] = p_product[8];
1102 l_carry += vli_add(p_result, p_result, l_tmp);
1105 l_tmp[0] = p_product[11];
1106 l_tmp[1] = p_product[12];
1107 l_tmp[2] = p_product[13];
1108 l_tmp[3] = l_tmp[4] = l_tmp[5] = 0;
1109 l_tmp[6] = p_product[8];
1110 l_tmp[7] = p_product[10];
1111 l_carry -= vli_sub(p_result, p_result, l_tmp);
1114 l_tmp[0] = p_product[12];
1115 l_tmp[1] = p_product[13];
1116 l_tmp[2] = p_product[14];
1117 l_tmp[3] = p_product[15];
1118 l_tmp[4] = l_tmp[5] = 0;
1119 l_tmp[6] = p_product[9];
1120 l_tmp[7] = p_product[11];
1121 l_carry -= vli_sub(p_result, p_result, l_tmp);
1124 l_tmp[0] = p_product[13];
1125 l_tmp[1] = p_product[14];
1126 l_tmp[2] = p_product[15];
1127 l_tmp[3] = p_product[8];
1128 l_tmp[4] = p_product[9];
1129 l_tmp[5] = p_product[10];
1131 l_tmp[7] = p_product[12];
1132 l_carry -= vli_sub(p_result, p_result, l_tmp);
1135 l_tmp[0] = p_product[14];
1136 l_tmp[1] = p_product[15];
1138 l_tmp[3] = p_product[9];
1139 l_tmp[4] = p_product[10];
1140 l_tmp[5] = p_product[11];
1142 l_tmp[7] = p_product[13];
1143 l_carry -= vli_sub(p_result, p_result, l_tmp);
1149 l_carry += vli_add(p_result, p_result, curve_p);
1150 } while(l_carry < 0);
1154 while(l_carry || vli_cmp(curve_p, p_result) != 1)
1156 l_carry -= vli_sub(p_result, p_result, curve_p);
1161 static void vli_mmod_fast(uint64_t *RESTRICT p_result, uint64_t *RESTRICT p_product)
1163 uint64_t l_tmp[uECC_WORDS];
1167 vli_set(p_result, p_product);
1171 l_tmp[1] = p_product[5] & 0xffffffff00000000ull;
1172 l_tmp[2] = p_product[6];
1173 l_tmp[3] = p_product[7];
1174 l_carry = vli_add(l_tmp, l_tmp, l_tmp);
1175 l_carry += vli_add(p_result, p_result, l_tmp);
1178 l_tmp[1] = p_product[6] << 32;
1179 l_tmp[2] = (p_product[6] >> 32) | (p_product[7] << 32);
1180 l_tmp[3] = p_product[7] >> 32;
1181 l_carry += vli_add(l_tmp, l_tmp, l_tmp);
1182 l_carry += vli_add(p_result, p_result, l_tmp);
1185 l_tmp[0] = p_product[4];
1186 l_tmp[1] = p_product[5] & 0xffffffff;
1188 l_tmp[3] = p_product[7];
1189 l_carry += vli_add(p_result, p_result, l_tmp);
1192 l_tmp[0] = (p_product[4] >> 32) | (p_product[5] << 32);
1193 l_tmp[1] = (p_product[5] >> 32) | (p_product[6] & 0xffffffff00000000ull);
1194 l_tmp[2] = p_product[7];
1195 l_tmp[3] = (p_product[6] >> 32) | (p_product[4] << 32);
1196 l_carry += vli_add(p_result, p_result, l_tmp);
1199 l_tmp[0] = (p_product[5] >> 32) | (p_product[6] << 32);
1200 l_tmp[1] = (p_product[6] >> 32);
1202 l_tmp[3] = (p_product[4] & 0xffffffff) | (p_product[5] << 32);
1203 l_carry -= vli_sub(p_result, p_result, l_tmp);
1206 l_tmp[0] = p_product[6];
1207 l_tmp[1] = p_product[7];
1209 l_tmp[3] = (p_product[4] >> 32) | (p_product[5] & 0xffffffff00000000ull);
1210 l_carry -= vli_sub(p_result, p_result, l_tmp);
1213 l_tmp[0] = (p_product[6] >> 32) | (p_product[7] << 32);
1214 l_tmp[1] = (p_product[7] >> 32) | (p_product[4] << 32);
1215 l_tmp[2] = (p_product[4] >> 32) | (p_product[5] << 32);
1216 l_tmp[3] = (p_product[6] << 32);
1217 l_carry -= vli_sub(p_result, p_result, l_tmp);
1220 l_tmp[0] = p_product[7];
1221 l_tmp[1] = p_product[4] & 0xffffffff00000000ull;
1222 l_tmp[2] = p_product[5];
1223 l_tmp[3] = p_product[6] & 0xffffffff00000000ull;
1224 l_carry -= vli_sub(p_result, p_result, l_tmp);
1230 l_carry += vli_add(p_result, p_result, curve_p);
1231 } while(l_carry < 0);
1235 while(l_carry || vli_cmp(curve_p, p_result) != 1)
1237 l_carry -= vli_sub(p_result, p_result, curve_p);
1241 #endif /* uECC_WORD_SIZE */
1243 #elif uECC_CURVE == uECC_secp256k1
1245 #if uECC_WORD_SIZE == 1
1246 static void omega_mult(uint8_t * RESTRICT p_result, uint8_t * RESTRICT p_right)
1248 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1255 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1256 muladd(0xD1, p_right[0], &r0, &r1, &r2);
1262 for(k = 1; k < uECC_WORDS; ++k)
1264 muladd(0x03, p_right[k-1], &r0, &r1, &r2);
1265 muladd(0xD1, p_right[k], &r0, &r1, &r2);
1272 muladd(0x03, p_right[uECC_WORDS-1], &r0, &r1, &r2);
1273 p_result[uECC_WORDS] = r0;
1274 p_result[uECC_WORDS + 1] = r1;
1276 p_result[4 + uECC_WORDS] = vli_add(p_result + 4, p_result + 4, p_right); /* add the 2^32 multiple */
1278 #elif uECC_WORD_SIZE == 4
1279 static void omega_mult(uint32_t * RESTRICT p_result, uint32_t * RESTRICT p_right)
1281 /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1282 uint32_t l_carry = 0;
1285 for(k = 0; k < uECC_WORDS; ++k)
1287 uint64_t p = (uint64_t)0x3D1 * p_right[k] + l_carry;
1288 p_result[k] = (p & 0xffffffff);
1291 p_result[uECC_WORDS] = l_carry;
1293 p_result[1 + uECC_WORDS] = vli_add(p_result + 1, p_result + 1, p_right); /* add the 2^32 multiple */
1296 static void omega_mult(uint64_t * RESTRICT p_result, uint64_t * RESTRICT p_right)
1304 /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1305 for(k = 0; k < uECC_WORDS; ++k)
1307 muladd(0x1000003D1ull, p_right[k], &r0, &r1, &r2);
1314 p_result[uECC_WORDS] = r0;
1316 #endif /* uECC_WORD_SIZE */
1318 #endif /* uECC_CURVE */
1319 #endif /* !asm_mmod_fast */
1321 /* Computes p_result = (p_left * p_right) % curve_p. */
1322 static void vli_modMult_fast(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
1324 uECC_word_t l_product[2 * uECC_WORDS];
1325 vli_mult(l_product, p_left, p_right);
1326 vli_mmod_fast(p_result, l_product);
1329 #if uECC_SQUARE_FUNC
1331 /* Computes p_result = p_left^2 % curve_p. */
1332 static void vli_modSquare_fast(uECC_word_t *p_result, uECC_word_t *p_left)
1334 uECC_word_t l_product[2 * uECC_WORDS];
1335 vli_square(l_product, p_left);
1336 vli_mmod_fast(p_result, l_product);
1339 #else /* uECC_SQUARE_FUNC */
1341 #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left))
1343 #endif /* uECC_SQUARE_FUNC */
1346 #define EVEN(vli) (!(vli[0] & 1))
1347 /* Computes p_result = (1 / p_input) % p_mod. All VLIs are the same size.
1348 See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
1349 https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
1351 static void vli_modInv(uECC_word_t *p_result, uECC_word_t *p_input, uECC_word_t *p_mod)
1353 uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS];
1354 uECC_word_t l_carry;
1355 cmpresult_t l_cmpResult;
1357 if(vli_isZero(p_input))
1359 vli_clear(p_result);
1363 vli_set(a, p_input);
1368 while((l_cmpResult = vli_cmp(a, b)) != 0)
1376 l_carry = vli_add(u, u, p_mod);
1381 u[uECC_WORDS-1] |= HIGH_BIT_SET;
1389 l_carry = vli_add(v, v, p_mod);
1394 v[uECC_WORDS-1] |= HIGH_BIT_SET;
1397 else if(l_cmpResult > 0)
1401 if(vli_cmp(u, v) < 0)
1403 vli_add(u, u, p_mod);
1408 l_carry = vli_add(u, u, p_mod);
1413 u[uECC_WORDS-1] |= HIGH_BIT_SET;
1420 if(vli_cmp(v, u) < 0)
1422 vli_add(v, v, p_mod);
1427 l_carry = vli_add(v, v, p_mod);
1432 v[uECC_WORDS-1] |= HIGH_BIT_SET;
1437 vli_set(p_result, u);
1439 #endif /* !asm_modInv */
1441 /* ------ Point operations ------ */
1443 /* Returns 1 if p_point is the point at infinity, 0 otherwise. */
1444 static cmpresult_t EccPoint_isZero(EccPoint *p_point)
1446 return (vli_isZero(p_point->x) && vli_isZero(p_point->y));
1449 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
1450 From http://eprint.iacr.org/2011/338.pdf
1453 /* Double in place */
1454 #if (uECC_CURVE == uECC_secp256k1)
1455 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1, uECC_word_t * RESTRICT Z1)
1457 /* t1 = X, t2 = Y, t3 = Z */
1458 uECC_word_t t4[uECC_WORDS];
1459 uECC_word_t t5[uECC_WORDS];
1466 vli_modSquare_fast(t5, Y1); /* t5 = y1^2 */
1467 vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */
1468 vli_modSquare_fast(X1, X1); /* t1 = x1^2 */
1469 vli_modSquare_fast(t5, t5); /* t5 = y1^4 */
1470 vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */
1472 vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */
1473 vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */
1474 if(vli_testBit(Y1, 0))
1476 uECC_word_t l_carry = vli_add(Y1, Y1, curve_p);
1478 Y1[uECC_WORDS-1] |= l_carry << (uECC_WORD_BITS - 1);
1484 /* t2 = 3/2*(x1^2) = B */
1486 vli_modSquare_fast(X1, Y1); /* t1 = B^2 */
1487 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */
1488 vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */
1490 vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */
1491 vli_modMult_fast(Y1, Y1, t4); /* t2 = B * (A - x3) */
1492 vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */
1495 static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1, uECC_word_t * RESTRICT Z1)
1497 /* t1 = X, t2 = Y, t3 = Z */
1498 uECC_word_t t4[uECC_WORDS];
1499 uECC_word_t t5[uECC_WORDS];
1506 vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */
1507 vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
1508 vli_modSquare_fast(t4, t4); /* t4 = y1^4 */
1509 vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
1510 vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */
1512 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
1513 vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
1514 vli_modSub_fast(Z1, X1, Z1); /* t3 = x1 - z1^2 */
1515 vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
1517 vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
1518 vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
1519 if(vli_testBit(X1, 0))
1521 uECC_word_t l_carry = vli_add(X1, X1, curve_p);
1523 X1[uECC_WORDS-1] |= l_carry << (uECC_WORD_BITS - 1);
1529 /* t1 = 3/2*(x1^2 - z1^4) = B */
1531 vli_modSquare_fast(Z1, X1); /* t3 = B^2 */
1532 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - A */
1533 vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - 2A = x3 */
1534 vli_modSub_fast(t5, t5, Z1); /* t5 = A - x3 */
1535 vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
1536 vli_modSub_fast(t4, X1, t4); /* t4 = B * (A - x3) - y1^4 = y3 */
1544 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1545 static void apply_z(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1, uECC_word_t * RESTRICT Z)
1547 uECC_word_t t1[uECC_WORDS];
1549 vli_modSquare_fast(t1, Z); /* z^2 */
1550 vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
1551 vli_modMult_fast(t1, t1, Z); /* z^3 */
1552 vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
1555 /* P = (x1, y1) => 2P, (x2, y2) => P' */
1556 static void XYcZ_initial_double(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1,
1557 uECC_word_t * RESTRICT X2, uECC_word_t * RESTRICT Y2, const uECC_word_t * RESTRICT p_initialZ)
1559 uECC_word_t z[uECC_WORDS];
1568 vli_set(z, p_initialZ);
1573 EccPoint_double_jacobian(X1, Y1, z);
1578 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1579 Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1580 or P => P', Q => P + Q
1582 static void XYcZ_add(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1, uECC_word_t * RESTRICT X2, uECC_word_t * RESTRICT Y2)
1584 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1585 uECC_word_t t5[uECC_WORDS];
1587 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */
1588 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
1589 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
1590 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
1591 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */
1592 vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */
1594 vli_modSub_fast(t5, t5, X1); /* t5 = D - B */
1595 vli_modSub_fast(t5, t5, X2); /* t5 = D - B - C = x3 */
1596 vli_modSub_fast(X2, X2, X1); /* t3 = C - B */
1597 vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
1598 vli_modSub_fast(X2, X1, t5); /* t3 = B - x3 */
1599 vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
1600 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */
1605 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1606 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1607 or P => P - Q, Q => P + Q
1609 static void XYcZ_addC(uECC_word_t * RESTRICT X1, uECC_word_t * RESTRICT Y1, uECC_word_t * RESTRICT X2, uECC_word_t * RESTRICT Y2)
1611 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1612 uECC_word_t t5[uECC_WORDS];
1613 uECC_word_t t6[uECC_WORDS];
1614 uECC_word_t t7[uECC_WORDS];
1616 vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */
1617 vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
1618 vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
1619 vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
1620 vli_modAdd(t5, Y2, Y1, curve_p); /* t4 = y2 + y1 */
1621 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */
1623 vli_modSub_fast(t6, X2, X1); /* t6 = C - B */
1624 vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) */
1625 vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
1626 vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 */
1627 vli_modSub_fast(X2, X2, t6); /* t3 = x3 */
1629 vli_modSub_fast(t7, X1, X2); /* t7 = B - x3 */
1630 vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
1631 vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */
1633 vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */
1634 vli_modSub_fast(t7, t7, t6); /* t7 = x3' */
1635 vli_modSub_fast(t6, t7, X1); /* t6 = x3' - B */
1636 vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
1637 vli_modSub_fast(Y1, t6, Y1); /* t2 = y3' */
1642 static void EccPoint_mult(EccPoint * RESTRICT p_result, EccPoint * RESTRICT p_point,
1643 const uECC_word_t * RESTRICT p_scalar, const uECC_word_t * RESTRICT p_initialZ, bitcount_t p_numBits)
1646 uECC_word_t Rx[2][uECC_WORDS];
1647 uECC_word_t Ry[2][uECC_WORDS];
1648 uECC_word_t z[uECC_WORDS];
1653 vli_set(Rx[1], p_point->x);
1654 vli_set(Ry[1], p_point->y);
1656 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], p_initialZ);
1658 for(i = p_numBits - 2; i > 0; --i)
1660 nb = !vli_testBit(p_scalar, i);
1661 XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
1662 XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
1665 nb = !vli_testBit(p_scalar, 0);
1666 XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
1668 /* Find final 1/Z value. */
1669 vli_modSub_fast(z, Rx[1], Rx[0]); /* X1 - X0 */
1670 vli_modMult_fast(z, z, Ry[1-nb]); /* Yb * (X1 - X0) */
1671 vli_modMult_fast(z, z, p_point->x); /* xP * Yb * (X1 - X0) */
1672 vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */
1673 vli_modMult_fast(z, z, p_point->y); /* yP / (xP * Yb * (X1 - X0)) */
1674 vli_modMult_fast(z, z, Rx[1-nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
1675 /* End 1/Z calculation */
1677 XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
1679 apply_z(Rx[0], Ry[0], z);
1681 vli_set(p_result->x, Rx[0]);
1682 vli_set(p_result->y, Ry[0]);
1685 /* Compute a = sqrt(a) (mod curve_p). */
1686 static void mod_sqrt(uECC_word_t *a)
1689 uECC_word_t p1[uECC_WORDS] = {1};
1690 uECC_word_t l_result[uECC_WORDS] = {1};
1692 /* Since curve_p == 3 (mod 4) for all supported curves, we can
1693 compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
1694 vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
1695 for(i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i)
1697 vli_modSquare_fast(l_result, l_result);
1698 if(vli_testBit(p1, i))
1700 vli_modMult_fast(l_result, l_result, a);
1703 vli_set(a, l_result);
1706 #if uECC_WORD_SIZE == 1
1708 static void vli_nativeToBytes(uint8_t * RESTRICT p_dest, const uint8_t * RESTRICT p_src)
1711 for(i=0; i<uECC_BYTES; ++i)
1713 p_dest[i] = p_src[(uECC_BYTES - 1) - i];
1717 #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src))
1719 #elif uECC_WORD_SIZE == 4
1721 static void vli_nativeToBytes(uint8_t *p_bytes, const uint32_t *p_native)
1724 for(i=0; i<uECC_WORDS; ++i)
1726 uint8_t *p_digit = p_bytes + 4 * (uECC_WORDS - 1 - i);
1727 p_digit[0] = p_native[i] >> 24;
1728 p_digit[1] = p_native[i] >> 16;
1729 p_digit[2] = p_native[i] >> 8;
1730 p_digit[3] = p_native[i];
1734 static void vli_bytesToNative(uint32_t *p_native, const uint8_t *p_bytes)
1737 for(i=0; i<uECC_WORDS; ++i)
1739 const uint8_t *p_digit = p_bytes + 4 * (uECC_WORDS - 1 - i);
1740 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];
1746 static void vli_nativeToBytes(uint8_t *p_bytes, const uint64_t *p_native)
1749 for(i=0; i<uECC_WORDS; ++i)
1751 uint8_t *p_digit = p_bytes + 8 * (uECC_WORDS - 1 - i);
1752 p_digit[0] = p_native[i] >> 56;
1753 p_digit[1] = p_native[i] >> 48;
1754 p_digit[2] = p_native[i] >> 40;
1755 p_digit[3] = p_native[i] >> 32;
1756 p_digit[4] = p_native[i] >> 24;
1757 p_digit[5] = p_native[i] >> 16;
1758 p_digit[6] = p_native[i] >> 8;
1759 p_digit[7] = p_native[i];
1763 static void vli_bytesToNative(uint64_t *p_native, const uint8_t *p_bytes)
1766 for(i=0; i<uECC_WORDS; ++i)
1768 const uint8_t *p_digit = p_bytes + 8 * (uECC_WORDS - 1 - i);
1769 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) |
1770 ((uint64_t)p_digit[4] << 24) | ((uint64_t)p_digit[5] << 16) | ((uint64_t)p_digit[6] << 8) | (uint64_t)p_digit[7];
1774 #endif /* uECC_WORD_SIZE */
1776 int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_BYTES])
1779 uECC_word_t l_private[uECC_WORDS];
1780 uECC_word_t l_tries = 0;
1785 if(!g_rng((uint8_t *)l_private, sizeof(l_private)) || (l_tries++ >= MAX_TRIES))
1789 if(vli_isZero(l_private))
1794 /* Make sure the private key is in the range [1, n-1]. */
1795 #if uECC_CURVE != uECC_secp160r1
1796 if(vli_cmp(curve_n, l_private) != 1)
1802 EccPoint_mult(&l_public, &curve_G, l_private, 0, vli_numBits(l_private, uECC_WORDS));
1803 } while(EccPoint_isZero(&l_public));
1805 vli_nativeToBytes(p_privateKey, l_private);
1806 vli_nativeToBytes(p_publicKey, l_public.x);
1807 vli_nativeToBytes(p_publicKey + uECC_BYTES, l_public.y);
1811 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])
1814 uECC_word_t l_private[uECC_WORDS];
1815 uECC_word_t l_random[uECC_WORDS];
1817 g_rng((uint8_t *)l_random, sizeof(l_random));
1819 vli_bytesToNative(l_private, p_privateKey);
1820 vli_bytesToNative(l_public.x, p_publicKey);
1821 vli_bytesToNative(l_public.y, p_publicKey + uECC_BYTES);
1824 EccPoint_mult(&l_product, &l_public, l_private, (vli_isZero(l_random) ? 0: l_random), vli_numBits(l_private, uECC_WORDS));
1826 vli_nativeToBytes(p_secret, l_product.x);
1828 return !EccPoint_isZero(&l_product);
1831 void uECC_compress(const uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_compressed[uECC_BYTES+1])
1834 for(i=0; i<uECC_BYTES; ++i)
1836 p_compressed[i+1] = p_publicKey[i];
1838 p_compressed[0] = 2 + (p_publicKey[uECC_BYTES * 2 - 1] & 0x01);
1841 void uECC_decompress(const uint8_t p_compressed[uECC_BYTES+1], uint8_t p_publicKey[uECC_BYTES*2])
1844 vli_bytesToNative(l_point.x, p_compressed + 1);
1846 #if (uECC_CURVE == uECC_secp256k1)
1847 vli_modSquare_fast(l_point.y, l_point.x); /* r = x^2 */
1848 vli_modMult_fast(l_point.y, l_point.y, l_point.x); /* r = x^3 */
1849 vli_modAdd(l_point.y, l_point.y, curve_b, curve_p); /* r = x^3 + b */
1851 uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */
1853 vli_modSquare_fast(l_point.y, l_point.x); /* y = x^2 */
1854 vli_modSub_fast(l_point.y, l_point.y, _3); /* y = x^2 - 3 */
1855 vli_modMult_fast(l_point.y, l_point.y, l_point.x); /* y = x^3 - 3x */
1856 vli_modAdd(l_point.y, l_point.y, curve_b, curve_p); /* y = x^3 - 3x + b */
1859 mod_sqrt(l_point.y);
1861 if((l_point.y[0] & 0x01) != (p_compressed[0] & 0x01))
1863 vli_sub(l_point.y, curve_p, l_point.y);
1866 vli_nativeToBytes(p_publicKey, l_point.x);
1867 vli_nativeToBytes(p_publicKey + uECC_BYTES, l_point.y);
1870 /* -------- ECDSA code -------- */
1872 #if (uECC_CURVE == uECC_secp160r1)
1873 static void vli_clear_n(uECC_word_t *p_vli)
1876 p_vli[uECC_N_WORDS - 1] = 0;
1879 static uECC_word_t vli_isZero_n(const uECC_word_t *p_vli)
1881 if(p_vli[uECC_N_WORDS - 1])
1885 return vli_isZero(p_vli);
1888 static void vli_set_n(uECC_word_t *p_dest, const uECC_word_t *p_src)
1890 vli_set(p_dest, p_src);
1891 p_dest[uECC_N_WORDS-1] = p_src[uECC_N_WORDS-1];
1894 static cmpresult_t vli_cmp_n(uECC_word_t *p_left, uECC_word_t *p_right)
1896 if(p_left[uECC_N_WORDS-1] > p_right[uECC_N_WORDS-1])
1900 else if(p_left[uECC_N_WORDS-1] < p_right[uECC_N_WORDS-1])
1904 return vli_cmp(p_left, p_right);
1907 static void vli_rshift1_n(uECC_word_t *p_vli)
1910 p_vli[uECC_N_WORDS-2] |= p_vli[uECC_N_WORDS-1] << (uECC_WORD_BITS - 1);
1911 p_vli[uECC_N_WORDS-1] = p_vli[uECC_N_WORDS-1] >> 1;
1914 static uECC_word_t vli_add_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
1916 uECC_word_t l_carry = vli_add(p_result, p_left, p_right);
1917 uECC_word_t l_sum = p_left[uECC_N_WORDS-1] + p_right[uECC_N_WORDS-1] + l_carry;
1918 if(l_sum != p_left[uECC_N_WORDS-1])
1920 l_carry = (l_sum < p_left[uECC_N_WORDS-1]);
1922 p_result[uECC_N_WORDS-1] = l_sum;
1926 static uECC_word_t vli_sub_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
1928 uECC_word_t l_borrow = vli_sub(p_result, p_left, p_right);
1929 uECC_word_t l_diff = p_left[uECC_N_WORDS-1] - p_right[uECC_N_WORDS-1] - l_borrow;
1930 if(l_diff != p_left[uECC_N_WORDS-1])
1932 l_borrow = (l_diff > p_left[uECC_N_WORDS-1]);
1934 p_result[uECC_N_WORDS-1] = l_diff;
1939 static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, uECC_word_t *r1, uECC_word_t *r2)
1941 uECC_dword_t p = (uECC_dword_t)a * b;
1942 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
1945 *r1 = r01 >> uECC_WORD_BITS;
1946 *r0 = (uECC_word_t)r01;
1948 #define muladd_exists 1
1951 static void vli_mult_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
1958 for(k = 0; k < uECC_N_WORDS*2 - 1; ++k)
1960 wordcount_t l_min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS);
1961 wordcount_t l_max = (k < uECC_N_WORDS ? k : uECC_N_WORDS-1);
1962 for(i = l_min; i <= l_max; ++i)
1964 muladd(p_left[i], p_right[k-i], &r0, &r1, &r2);
1972 p_result[uECC_N_WORDS*2 - 1] = r0;
1975 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)
1977 uECC_word_t l_carry = vli_add_n(p_result, p_left, p_right);
1978 if(l_carry || vli_cmp_n(p_result, p_mod) >= 0)
1980 vli_sub_n(p_result, p_result, p_mod);
1984 static void vli_modInv_n(uECC_word_t *p_result, uECC_word_t *p_input, uECC_word_t *p_mod)
1986 uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS];
1987 uECC_word_t l_carry;
1988 cmpresult_t l_cmpResult;
1990 if(vli_isZero_n(p_input))
1992 vli_clear_n(p_result);
1996 vli_set_n(a, p_input);
1997 vli_set_n(b, p_mod);
2001 while((l_cmpResult = vli_cmp_n(a, b)) != 0)
2007 if(!EVEN(u)) l_carry = vli_add_n(u, u, p_mod);
2009 if(l_carry) u[uECC_N_WORDS-1] |= HIGH_BIT_SET;
2014 if(!EVEN(v)) l_carry = vli_add_n(v, v, p_mod);
2016 if(l_carry) v[uECC_N_WORDS-1] |= HIGH_BIT_SET;
2018 else if(l_cmpResult > 0)
2022 if(vli_cmp_n(u, v) < 0) vli_add_n(u, u, p_mod);
2024 if(!EVEN(u)) l_carry = vli_add_n(u, u, p_mod);
2026 if(l_carry) u[uECC_N_WORDS-1] |= HIGH_BIT_SET;
2032 if(vli_cmp_n(v, u) < 0) vli_add_n(v, v, p_mod);
2034 if(!EVEN(v)) l_carry = vli_add_n(v, v, p_mod);
2036 if(l_carry) v[uECC_N_WORDS-1] |= HIGH_BIT_SET;
2040 vli_set_n(p_result, u);
2043 static void vli2_rshift1_n(uECC_word_t *p_vli)
2045 vli_rshift1_n(p_vli);
2046 p_vli[uECC_N_WORDS-1] |= p_vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1);
2047 vli_rshift1_n(p_vli + uECC_N_WORDS);
2050 static uECC_word_t vli2_sub_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
2052 uECC_word_t l_borrow = 0;
2054 for(i=0; i<uECC_N_WORDS*2; ++i)
2056 uECC_word_t l_diff = p_left[i] - p_right[i] - l_borrow;
2057 if(l_diff != p_left[i])
2059 l_borrow = (l_diff > p_left[i]);
2061 p_result[i] = l_diff;
2066 /* Computes p_result = (p_left * p_right) % curve_n. */
2067 static void vli_modMult_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
2069 uECC_word_t l_product[2 * uECC_N_WORDS];
2070 uECC_word_t l_modMultiple[2 * uECC_N_WORDS];
2071 uECC_word_t l_tmp[2 * uECC_N_WORDS];
2072 uECC_word_t *v[2] = {l_tmp, l_product};
2074 vli_mult_n(l_product, p_left, p_right);
2075 vli_clear_n(l_modMultiple);
2076 vli_set(l_modMultiple + uECC_N_WORDS + 1, curve_n);
2077 vli_rshift1(l_modMultiple + uECC_N_WORDS + 1);
2078 l_modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2079 l_modMultiple[uECC_N_WORDS] = HIGH_BIT_SET;
2082 uECC_word_t l_index = 1;
2083 for(i=0; i<=((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1)); ++i)
2085 uECC_word_t l_borrow = vli2_sub_n(v[1-l_index], v[l_index], l_modMultiple);
2086 l_index = !(l_index ^ l_borrow); /* Swap the index if there was no borrow */
2087 vli2_rshift1_n(l_modMultiple);
2090 vli_set_n(p_result, v[l_index]);
2095 #define vli_modInv_n vli_modInv
2096 #define vli_modAdd_n vli_modAdd
2098 static void vli2_rshift1(uECC_word_t *p_vli)
2101 p_vli[uECC_WORDS-1] |= p_vli[uECC_WORDS] << (uECC_WORD_BITS - 1);
2102 vli_rshift1(p_vli + uECC_WORDS);
2105 static uECC_word_t vli2_sub(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
2107 uECC_word_t l_borrow = 0;
2109 for(i=0; i<uECC_WORDS*2; ++i)
2111 uECC_word_t l_diff = p_left[i] - p_right[i] - l_borrow;
2112 if(l_diff != p_left[i])
2114 l_borrow = (l_diff > p_left[i]);
2116 p_result[i] = l_diff;
2121 /* Computes p_result = (p_left * p_right) % curve_n. */
2122 static void vli_modMult_n(uECC_word_t *p_result, uECC_word_t *p_left, uECC_word_t *p_right)
2124 uECC_word_t l_product[2 * uECC_WORDS];
2125 uECC_word_t l_modMultiple[2 * uECC_WORDS];
2126 uECC_word_t l_tmp[2 * uECC_WORDS];
2127 uECC_word_t *v[2] = {l_tmp, l_product};
2129 vli_mult(l_product, p_left, p_right);
2130 vli_set(l_modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */
2131 vli_clear(l_modMultiple);
2134 uECC_word_t l_index = 1;
2135 for(i=0; i<=uECC_BYTES * 8; ++i)
2137 uECC_word_t l_borrow = vli2_sub(v[1-l_index], v[l_index], l_modMultiple);
2138 l_index = !(l_index ^ l_borrow); /* Swap the index if there was no borrow */
2139 vli2_rshift1(l_modMultiple);
2142 vli_set(p_result, v[l_index]);
2144 #endif /* (uECC_CURVE != uECC_secp160r1) */
2146 int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_BYTES], uint8_t p_signature[uECC_BYTES*2])
2148 uECC_word_t k[uECC_N_WORDS];
2149 uECC_word_t l_tmp[uECC_N_WORDS];
2150 uECC_word_t s[uECC_N_WORDS];
2151 uECC_word_t *k2[2] = {l_tmp, s};
2153 uECC_word_t l_tries = 0;
2158 if(!g_rng((uint8_t *)k, sizeof(k)) || (l_tries++ >= MAX_TRIES))
2168 #if (uECC_CURVE == uECC_secp160r1)
2169 k[uECC_WORDS] &= 0x01;
2170 if(vli_cmp_n(curve_n, k) != 1)
2175 /* make sure that we don't leak timing information about k. See http://eprint.iacr.org/2011/232.pdf */
2176 vli_add_n(l_tmp, k, curve_n);
2177 uECC_word_t l_carry = (l_tmp[uECC_WORDS] & 0x02);
2178 vli_add_n(s, l_tmp, curve_n);
2181 EccPoint_mult(&p, &curve_G, k2[!l_carry], 0, (uECC_BYTES * 8) + 2);
2183 if(vli_cmp(curve_n, k) != 1)
2188 /* make sure that we don't leak timing information about k. See http://eprint.iacr.org/2011/232.pdf */
2189 uECC_word_t l_carry = vli_add(l_tmp, k, curve_n);
2190 vli_add(s, l_tmp, curve_n);
2193 EccPoint_mult(&p, &curve_G, k2[!l_carry], 0, (uECC_BYTES * 8) + 1);
2195 /* r = x1 (mod n) */
2196 if(vli_cmp(curve_n, p.x) != 1)
2198 vli_sub(p.x, p.x, curve_n);
2201 } while(vli_isZero(p.x));
2206 if(!g_rng((uint8_t *)l_tmp, sizeof(l_tmp)) || (l_tries++ >= MAX_TRIES))
2210 } while(vli_isZero(l_tmp));
2212 /* Prevent side channel analysis of vli_modInv() to determine
2213 bits of k / the private key by premultiplying by a random number */
2214 vli_modMult_n(k, k, l_tmp); /* k' = rand * k */
2215 vli_modInv_n(k, k, curve_n); /* k = 1 / k' */
2216 vli_modMult_n(k, k, l_tmp); /* k = 1 / k */
2218 vli_nativeToBytes(p_signature, p.x); /* store r */
2220 l_tmp[uECC_N_WORDS-1] = 0;
2221 vli_bytesToNative(l_tmp, p_privateKey); /* tmp = d */
2222 s[uECC_N_WORDS-1] = 0;
2224 vli_modMult_n(s, l_tmp, s); /* s = r*d */
2226 vli_bytesToNative(l_tmp, p_hash);
2227 vli_modAdd_n(s, l_tmp, s, curve_n); /* s = e + r*d */
2228 vli_modMult_n(s, s, k); /* s = (e + r*d) / k */
2229 #if (uECC_CURVE == uECC_secp160r1)
2230 if(s[uECC_N_WORDS-1])
2235 vli_nativeToBytes(p_signature + uECC_BYTES, s);
2240 static bitcount_t smax(bitcount_t a, bitcount_t b)
2242 return (a > b ? a : b);
2245 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])
2247 uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS];
2248 uECC_word_t z[uECC_N_WORDS];
2249 EccPoint l_public, l_sum;
2250 uECC_word_t rx[uECC_WORDS];
2251 uECC_word_t ry[uECC_WORDS];
2252 uECC_word_t tx[uECC_WORDS];
2253 uECC_word_t ty[uECC_WORDS];
2254 uECC_word_t tz[uECC_WORDS];
2256 uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS];
2257 r[uECC_N_WORDS-1] = 0;
2258 s[uECC_N_WORDS-1] = 0;
2260 vli_bytesToNative(l_public.x, p_publicKey);
2261 vli_bytesToNative(l_public.y, p_publicKey + uECC_BYTES);
2262 vli_bytesToNative(r, p_signature);
2263 vli_bytesToNative(s, p_signature + uECC_BYTES);
2265 if(vli_isZero(r) || vli_isZero(s))
2266 { /* r, s must not be 0. */
2270 #if (uECC_CURVE != uECC_secp160r1)
2271 if(vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1)
2272 { /* r, s must be < n. */
2277 /* Calculate u1 and u2. */
2278 vli_modInv_n(z, s, curve_n); /* Z = s^-1 */
2279 u1[uECC_N_WORDS-1] = 0;
2280 vli_bytesToNative(u1, p_hash);
2281 vli_modMult_n(u1, u1, z); /* u1 = e/s */
2282 vli_modMult_n(u2, r, z); /* u2 = r/s */
2284 /* Calculate l_sum = G + Q. */
2285 vli_set(l_sum.x, l_public.x);
2286 vli_set(l_sum.y, l_public.y);
2287 vli_set(tx, curve_G.x);
2288 vli_set(ty, curve_G.y);
2289 vli_modSub_fast(z, l_sum.x, tx); /* Z = x2 - x1 */
2290 XYcZ_add(tx, ty, l_sum.x, l_sum.y);
2291 vli_modInv(z, z, curve_p); /* Z = 1/Z */
2292 apply_z(l_sum.x, l_sum.y, z);
2294 /* Use Shamir's trick to calculate u1*G + u2*Q */
2295 EccPoint *l_points[4] = {0, &curve_G, &l_public, &l_sum};
2296 bitcount_t l_numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS));
2298 EccPoint *l_point = l_points[(!!vli_testBit(u1, l_numBits-1)) | ((!!vli_testBit(u2, l_numBits-1)) << 1)];
2299 vli_set(rx, l_point->x);
2300 vli_set(ry, l_point->y);
2305 for(i = l_numBits - 2; i >= 0; --i)
2307 EccPoint_double_jacobian(rx, ry, z);
2309 uECC_word_t l_index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
2310 l_point = l_points[l_index];
2313 vli_set(tx, l_point->x);
2314 vli_set(ty, l_point->y);
2316 vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */
2317 XYcZ_add(tx, ty, rx, ry);
2318 vli_modMult_fast(z, z, tz);
2322 vli_modInv(z, z, curve_p); /* Z = 1/Z */
2325 /* v = x1 (mod n) */
2326 #if (uECC_CURVE != uECC_secp160r1)
2327 if(vli_cmp(curve_n, rx) != 1)
2329 vli_sub(rx, rx, curve_n);
2333 /* Accept only if v == r. */
2334 return (vli_cmp(rx, r) == 0);