Bitcoin ABC 0.30.7
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
field_10x26_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
9
10#include "util.h"
11#include "field.h"
12#include "modinv32_impl.h"
13
14#ifdef VERIFY
15static void secp256k1_fe_verify(const secp256k1_fe *a) {
16 const uint32_t *d = a->n;
17 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
18 r &= (d[0] <= 0x3FFFFFFUL * m);
19 r &= (d[1] <= 0x3FFFFFFUL * m);
20 r &= (d[2] <= 0x3FFFFFFUL * m);
21 r &= (d[3] <= 0x3FFFFFFUL * m);
22 r &= (d[4] <= 0x3FFFFFFUL * m);
23 r &= (d[5] <= 0x3FFFFFFUL * m);
24 r &= (d[6] <= 0x3FFFFFFUL * m);
25 r &= (d[7] <= 0x3FFFFFFUL * m);
26 r &= (d[8] <= 0x3FFFFFFUL * m);
27 r &= (d[9] <= 0x03FFFFFUL * m);
28 r &= (a->magnitude >= 0);
29 r &= (a->magnitude <= 32);
30 if (a->normalized) {
31 r &= (a->magnitude <= 1);
32 if (r && (d[9] == 0x03FFFFFUL)) {
33 uint32_t mid = d[8] & d[7] & d[6] & d[5] & d[4] & d[3] & d[2];
34 if (mid == 0x3FFFFFFUL) {
35 r &= ((d[1] + 0x40UL + ((d[0] + 0x3D1UL) >> 26)) <= 0x3FFFFFFUL);
36 }
37 }
38 }
39 VERIFY_CHECK(r == 1);
40}
41#endif
42
44 uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
45 t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
46
47 /* Reduce t9 at the start so there will be at most a single carry from the first pass */
48 uint32_t m;
49 uint32_t x = t9 >> 22; t9 &= 0x03FFFFFUL;
50
51 /* The first pass ensures the magnitude is 1, ... */
52 t0 += x * 0x3D1UL; t1 += (x << 6);
53 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL;
54 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL;
55 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL; m = t2;
56 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL; m &= t3;
57 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL; m &= t4;
58 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL; m &= t5;
59 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL; m &= t6;
60 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL; m &= t7;
61 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL; m &= t8;
62
63 /* ... except for a possible carry at bit 22 of t9 (i.e. bit 256 of the field element) */
64 VERIFY_CHECK(t9 >> 23 == 0);
65
66 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
67 x = (t9 >> 22) | ((t9 == 0x03FFFFFUL) & (m == 0x3FFFFFFUL)
68 & ((t1 + 0x40UL + ((t0 + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
69
70 /* Apply the final reduction (for constant-time behaviour, we do it always) */
71 t0 += x * 0x3D1UL; t1 += (x << 6);
72 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL;
73 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL;
74 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL;
75 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL;
76 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL;
77 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL;
78 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL;
79 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL;
80 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL;
81
82 /* If t9 didn't carry to bit 22 already, then it should have after any final reduction */
83 VERIFY_CHECK(t9 >> 22 == x);
84
85 /* Mask off the possible multiple of 2^256 from the final reduction */
86 t9 &= 0x03FFFFFUL;
87
88 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
89 r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
90
91#ifdef VERIFY
92 r->magnitude = 1;
93 r->normalized = 1;
94 secp256k1_fe_verify(r);
95#endif
96}
97
99 uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
100 t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
101
102 /* Reduce t9 at the start so there will be at most a single carry from the first pass */
103 uint32_t x = t9 >> 22; t9 &= 0x03FFFFFUL;
104
105 /* The first pass ensures the magnitude is 1, ... */
106 t0 += x * 0x3D1UL; t1 += (x << 6);
107 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL;
108 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL;
109 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL;
110 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL;
111 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL;
112 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL;
113 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL;
114 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL;
115 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL;
116
117 /* ... except for a possible carry at bit 22 of t9 (i.e. bit 256 of the field element) */
118 VERIFY_CHECK(t9 >> 23 == 0);
119
120 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
121 r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
122
123#ifdef VERIFY
124 r->magnitude = 1;
125 secp256k1_fe_verify(r);
126#endif
127}
128
130 uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
131 t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
132
133 /* Reduce t9 at the start so there will be at most a single carry from the first pass */
134 uint32_t m;
135 uint32_t x = t9 >> 22; t9 &= 0x03FFFFFUL;
136
137 /* The first pass ensures the magnitude is 1, ... */
138 t0 += x * 0x3D1UL; t1 += (x << 6);
139 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL;
140 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL;
141 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL; m = t2;
142 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL; m &= t3;
143 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL; m &= t4;
144 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL; m &= t5;
145 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL; m &= t6;
146 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL; m &= t7;
147 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL; m &= t8;
148
149 /* ... except for a possible carry at bit 22 of t9 (i.e. bit 256 of the field element) */
150 VERIFY_CHECK(t9 >> 23 == 0);
151
152 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
153 x = (t9 >> 22) | ((t9 == 0x03FFFFFUL) & (m == 0x3FFFFFFUL)
154 & ((t1 + 0x40UL + ((t0 + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
155
156 if (x) {
157 t0 += 0x3D1UL; t1 += (x << 6);
158 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL;
159 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL;
160 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL;
161 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL;
162 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL;
163 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL;
164 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL;
165 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL;
166 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL;
167
168 /* If t9 didn't carry to bit 22 already, then it should have after any final reduction */
169 VERIFY_CHECK(t9 >> 22 == x);
170
171 /* Mask off the possible multiple of 2^256 from the final reduction */
172 t9 &= 0x03FFFFFUL;
173 }
174
175 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
176 r->n[5] = t5; r->n[6] = t6; r->n[7] = t7; r->n[8] = t8; r->n[9] = t9;
177
178#ifdef VERIFY
179 r->magnitude = 1;
180 r->normalized = 1;
181 secp256k1_fe_verify(r);
182#endif
183}
184
186 uint32_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4],
187 t5 = r->n[5], t6 = r->n[6], t7 = r->n[7], t8 = r->n[8], t9 = r->n[9];
188
189 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
190 uint32_t z0, z1;
191
192 /* Reduce t9 at the start so there will be at most a single carry from the first pass */
193 uint32_t x = t9 >> 22; t9 &= 0x03FFFFFUL;
194
195 /* The first pass ensures the magnitude is 1, ... */
196 t0 += x * 0x3D1UL; t1 += (x << 6);
197 t1 += (t0 >> 26); t0 &= 0x3FFFFFFUL; z0 = t0; z1 = t0 ^ 0x3D0UL;
198 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL; z0 |= t1; z1 &= t1 ^ 0x40UL;
199 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL; z0 |= t2; z1 &= t2;
200 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL; z0 |= t3; z1 &= t3;
201 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL; z0 |= t4; z1 &= t4;
202 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL; z0 |= t5; z1 &= t5;
203 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL; z0 |= t6; z1 &= t6;
204 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL; z0 |= t7; z1 &= t7;
205 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL; z0 |= t8; z1 &= t8;
206 z0 |= t9; z1 &= t9 ^ 0x3C00000UL;
207
208 /* ... except for a possible carry at bit 22 of t9 (i.e. bit 256 of the field element) */
209 VERIFY_CHECK(t9 >> 23 == 0);
210
211 return (z0 == 0) | (z1 == 0x3FFFFFFUL);
212}
213
215 uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
216 uint32_t z0, z1;
217 uint32_t x;
218
219 t0 = r->n[0];
220 t9 = r->n[9];
221
222 /* Reduce t9 at the start so there will be at most a single carry from the first pass */
223 x = t9 >> 22;
224
225 /* The first pass ensures the magnitude is 1, ... */
226 t0 += x * 0x3D1UL;
227
228 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
229 z0 = t0 & 0x3FFFFFFUL;
230 z1 = z0 ^ 0x3D0UL;
231
232 /* Fast return path should catch the majority of cases */
233 if ((z0 != 0UL) & (z1 != 0x3FFFFFFUL)) {
234 return 0;
235 }
236
237 t1 = r->n[1];
238 t2 = r->n[2];
239 t3 = r->n[3];
240 t4 = r->n[4];
241 t5 = r->n[5];
242 t6 = r->n[6];
243 t7 = r->n[7];
244 t8 = r->n[8];
245
246 t9 &= 0x03FFFFFUL;
247 t1 += (x << 6);
248
249 t1 += (t0 >> 26);
250 t2 += (t1 >> 26); t1 &= 0x3FFFFFFUL; z0 |= t1; z1 &= t1 ^ 0x40UL;
251 t3 += (t2 >> 26); t2 &= 0x3FFFFFFUL; z0 |= t2; z1 &= t2;
252 t4 += (t3 >> 26); t3 &= 0x3FFFFFFUL; z0 |= t3; z1 &= t3;
253 t5 += (t4 >> 26); t4 &= 0x3FFFFFFUL; z0 |= t4; z1 &= t4;
254 t6 += (t5 >> 26); t5 &= 0x3FFFFFFUL; z0 |= t5; z1 &= t5;
255 t7 += (t6 >> 26); t6 &= 0x3FFFFFFUL; z0 |= t6; z1 &= t6;
256 t8 += (t7 >> 26); t7 &= 0x3FFFFFFUL; z0 |= t7; z1 &= t7;
257 t9 += (t8 >> 26); t8 &= 0x3FFFFFFUL; z0 |= t8; z1 &= t8;
258 z0 |= t9; z1 &= t9 ^ 0x3C00000UL;
259
260 /* ... except for a possible carry at bit 22 of t9 (i.e. bit 256 of the field element) */
261 VERIFY_CHECK(t9 >> 23 == 0);
262
263 return (z0 == 0) | (z1 == 0x3FFFFFFUL);
264}
265
267 r->n[0] = a;
268 r->n[1] = r->n[2] = r->n[3] = r->n[4] = r->n[5] = r->n[6] = r->n[7] = r->n[8] = r->n[9] = 0;
269#ifdef VERIFY
270 r->magnitude = 1;
271 r->normalized = 1;
272 secp256k1_fe_verify(r);
273#endif
274}
275
277 const uint32_t *t = a->n;
278#ifdef VERIFY
279 VERIFY_CHECK(a->normalized);
280 secp256k1_fe_verify(a);
281#endif
282 return (t[0] | t[1] | t[2] | t[3] | t[4] | t[5] | t[6] | t[7] | t[8] | t[9]) == 0;
283}
284
286#ifdef VERIFY
287 VERIFY_CHECK(a->normalized);
288 secp256k1_fe_verify(a);
289#endif
290 return a->n[0] & 1;
291}
292
294 int i;
295#ifdef VERIFY
296 a->magnitude = 0;
297 a->normalized = 1;
298#endif
299 for (i=0; i<10; i++) {
300 a->n[i] = 0;
301 }
302}
303
304static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
305 int i;
306#ifdef VERIFY
307 VERIFY_CHECK(a->normalized);
308 VERIFY_CHECK(b->normalized);
309 secp256k1_fe_verify(a);
310 secp256k1_fe_verify(b);
311#endif
312 for (i = 9; i >= 0; i--) {
313 if (a->n[i] > b->n[i]) {
314 return 1;
315 }
316 if (a->n[i] < b->n[i]) {
317 return -1;
318 }
319 }
320 return 0;
321}
322
323static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a) {
324 int ret;
325 r->n[0] = (uint32_t)a[31] | ((uint32_t)a[30] << 8) | ((uint32_t)a[29] << 16) | ((uint32_t)(a[28] & 0x3) << 24);
326 r->n[1] = (uint32_t)((a[28] >> 2) & 0x3f) | ((uint32_t)a[27] << 6) | ((uint32_t)a[26] << 14) | ((uint32_t)(a[25] & 0xf) << 22);
327 r->n[2] = (uint32_t)((a[25] >> 4) & 0xf) | ((uint32_t)a[24] << 4) | ((uint32_t)a[23] << 12) | ((uint32_t)(a[22] & 0x3f) << 20);
328 r->n[3] = (uint32_t)((a[22] >> 6) & 0x3) | ((uint32_t)a[21] << 2) | ((uint32_t)a[20] << 10) | ((uint32_t)a[19] << 18);
329 r->n[4] = (uint32_t)a[18] | ((uint32_t)a[17] << 8) | ((uint32_t)a[16] << 16) | ((uint32_t)(a[15] & 0x3) << 24);
330 r->n[5] = (uint32_t)((a[15] >> 2) & 0x3f) | ((uint32_t)a[14] << 6) | ((uint32_t)a[13] << 14) | ((uint32_t)(a[12] & 0xf) << 22);
331 r->n[6] = (uint32_t)((a[12] >> 4) & 0xf) | ((uint32_t)a[11] << 4) | ((uint32_t)a[10] << 12) | ((uint32_t)(a[9] & 0x3f) << 20);
332 r->n[7] = (uint32_t)((a[9] >> 6) & 0x3) | ((uint32_t)a[8] << 2) | ((uint32_t)a[7] << 10) | ((uint32_t)a[6] << 18);
333 r->n[8] = (uint32_t)a[5] | ((uint32_t)a[4] << 8) | ((uint32_t)a[3] << 16) | ((uint32_t)(a[2] & 0x3) << 24);
334 r->n[9] = (uint32_t)((a[2] >> 2) & 0x3f) | ((uint32_t)a[1] << 6) | ((uint32_t)a[0] << 14);
335
336 ret = !((r->n[9] == 0x3FFFFFUL) & ((r->n[8] & r->n[7] & r->n[6] & r->n[5] & r->n[4] & r->n[3] & r->n[2]) == 0x3FFFFFFUL) & ((r->n[1] + 0x40UL + ((r->n[0] + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
337#ifdef VERIFY
338 r->magnitude = 1;
339 if (ret) {
340 r->normalized = 1;
341 secp256k1_fe_verify(r);
342 } else {
343 r->normalized = 0;
344 }
345#endif
346 return ret;
347}
348
350static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
351#ifdef VERIFY
352 VERIFY_CHECK(a->normalized);
353 secp256k1_fe_verify(a);
354#endif
355 r[0] = (a->n[9] >> 14) & 0xff;
356 r[1] = (a->n[9] >> 6) & 0xff;
357 r[2] = ((a->n[9] & 0x3F) << 2) | ((a->n[8] >> 24) & 0x3);
358 r[3] = (a->n[8] >> 16) & 0xff;
359 r[4] = (a->n[8] >> 8) & 0xff;
360 r[5] = a->n[8] & 0xff;
361 r[6] = (a->n[7] >> 18) & 0xff;
362 r[7] = (a->n[7] >> 10) & 0xff;
363 r[8] = (a->n[7] >> 2) & 0xff;
364 r[9] = ((a->n[7] & 0x3) << 6) | ((a->n[6] >> 20) & 0x3f);
365 r[10] = (a->n[6] >> 12) & 0xff;
366 r[11] = (a->n[6] >> 4) & 0xff;
367 r[12] = ((a->n[6] & 0xf) << 4) | ((a->n[5] >> 22) & 0xf);
368 r[13] = (a->n[5] >> 14) & 0xff;
369 r[14] = (a->n[5] >> 6) & 0xff;
370 r[15] = ((a->n[5] & 0x3f) << 2) | ((a->n[4] >> 24) & 0x3);
371 r[16] = (a->n[4] >> 16) & 0xff;
372 r[17] = (a->n[4] >> 8) & 0xff;
373 r[18] = a->n[4] & 0xff;
374 r[19] = (a->n[3] >> 18) & 0xff;
375 r[20] = (a->n[3] >> 10) & 0xff;
376 r[21] = (a->n[3] >> 2) & 0xff;
377 r[22] = ((a->n[3] & 0x3) << 6) | ((a->n[2] >> 20) & 0x3f);
378 r[23] = (a->n[2] >> 12) & 0xff;
379 r[24] = (a->n[2] >> 4) & 0xff;
380 r[25] = ((a->n[2] & 0xf) << 4) | ((a->n[1] >> 22) & 0xf);
381 r[26] = (a->n[1] >> 14) & 0xff;
382 r[27] = (a->n[1] >> 6) & 0xff;
383 r[28] = ((a->n[1] & 0x3f) << 2) | ((a->n[0] >> 24) & 0x3);
384 r[29] = (a->n[0] >> 16) & 0xff;
385 r[30] = (a->n[0] >> 8) & 0xff;
386 r[31] = a->n[0] & 0xff;
387}
388
390#ifdef VERIFY
391 VERIFY_CHECK(a->magnitude <= m);
392 secp256k1_fe_verify(a);
393#endif
394 r->n[0] = 0x3FFFC2FUL * 2 * (m + 1) - a->n[0];
395 r->n[1] = 0x3FFFFBFUL * 2 * (m + 1) - a->n[1];
396 r->n[2] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[2];
397 r->n[3] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[3];
398 r->n[4] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[4];
399 r->n[5] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[5];
400 r->n[6] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[6];
401 r->n[7] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[7];
402 r->n[8] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[8];
403 r->n[9] = 0x03FFFFFUL * 2 * (m + 1) - a->n[9];
404#ifdef VERIFY
405 r->magnitude = m + 1;
406 r->normalized = 0;
407 secp256k1_fe_verify(r);
408#endif
409}
410
412 r->n[0] *= a;
413 r->n[1] *= a;
414 r->n[2] *= a;
415 r->n[3] *= a;
416 r->n[4] *= a;
417 r->n[5] *= a;
418 r->n[6] *= a;
419 r->n[7] *= a;
420 r->n[8] *= a;
421 r->n[9] *= a;
422#ifdef VERIFY
423 r->magnitude *= a;
424 r->normalized = 0;
425 secp256k1_fe_verify(r);
426#endif
427}
428
430#ifdef VERIFY
431 secp256k1_fe_verify(a);
432#endif
433 r->n[0] += a->n[0];
434 r->n[1] += a->n[1];
435 r->n[2] += a->n[2];
436 r->n[3] += a->n[3];
437 r->n[4] += a->n[4];
438 r->n[5] += a->n[5];
439 r->n[6] += a->n[6];
440 r->n[7] += a->n[7];
441 r->n[8] += a->n[8];
442 r->n[9] += a->n[9];
443#ifdef VERIFY
444 r->magnitude += a->magnitude;
445 r->normalized = 0;
446 secp256k1_fe_verify(r);
447#endif
448}
449
450#if defined(USE_EXTERNAL_ASM)
451
452/* External assembler implementation */
453void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t * SECP256K1_RESTRICT b);
454void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a);
455
456#else
457
458#ifdef VERIFY
459#define VERIFY_BITS(x, n) VERIFY_CHECK(((x) >> (n)) == 0)
460#else
461#define VERIFY_BITS(x, n) do { } while(0)
462#endif
463
464SECP256K1_INLINE static void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t * SECP256K1_RESTRICT b) {
465 uint64_t c, d;
466 uint64_t u0, u1, u2, u3, u4, u5, u6, u7, u8;
467 uint32_t t9, t1, t0, t2, t3, t4, t5, t6, t7;
468 const uint32_t M = 0x3FFFFFFUL, R0 = 0x3D10UL, R1 = 0x400UL;
469
470 VERIFY_BITS(a[0], 30);
471 VERIFY_BITS(a[1], 30);
472 VERIFY_BITS(a[2], 30);
473 VERIFY_BITS(a[3], 30);
474 VERIFY_BITS(a[4], 30);
475 VERIFY_BITS(a[5], 30);
476 VERIFY_BITS(a[6], 30);
477 VERIFY_BITS(a[7], 30);
478 VERIFY_BITS(a[8], 30);
479 VERIFY_BITS(a[9], 26);
480 VERIFY_BITS(b[0], 30);
481 VERIFY_BITS(b[1], 30);
482 VERIFY_BITS(b[2], 30);
483 VERIFY_BITS(b[3], 30);
484 VERIFY_BITS(b[4], 30);
485 VERIFY_BITS(b[5], 30);
486 VERIFY_BITS(b[6], 30);
487 VERIFY_BITS(b[7], 30);
488 VERIFY_BITS(b[8], 30);
489 VERIFY_BITS(b[9], 26);
490
497 d = (uint64_t)a[0] * b[9]
498 + (uint64_t)a[1] * b[8]
499 + (uint64_t)a[2] * b[7]
500 + (uint64_t)a[3] * b[6]
501 + (uint64_t)a[4] * b[5]
502 + (uint64_t)a[5] * b[4]
503 + (uint64_t)a[6] * b[3]
504 + (uint64_t)a[7] * b[2]
505 + (uint64_t)a[8] * b[1]
506 + (uint64_t)a[9] * b[0];
507 /* VERIFY_BITS(d, 64); */
508 /* [d 0 0 0 0 0 0 0 0 0] = [p9 0 0 0 0 0 0 0 0 0] */
509 t9 = d & M; d >>= 26;
510 VERIFY_BITS(t9, 26);
511 VERIFY_BITS(d, 38);
512 /* [d t9 0 0 0 0 0 0 0 0 0] = [p9 0 0 0 0 0 0 0 0 0] */
513
514 c = (uint64_t)a[0] * b[0];
515 VERIFY_BITS(c, 60);
516 /* [d t9 0 0 0 0 0 0 0 0 c] = [p9 0 0 0 0 0 0 0 0 p0] */
517 d += (uint64_t)a[1] * b[9]
518 + (uint64_t)a[2] * b[8]
519 + (uint64_t)a[3] * b[7]
520 + (uint64_t)a[4] * b[6]
521 + (uint64_t)a[5] * b[5]
522 + (uint64_t)a[6] * b[4]
523 + (uint64_t)a[7] * b[3]
524 + (uint64_t)a[8] * b[2]
525 + (uint64_t)a[9] * b[1];
526 VERIFY_BITS(d, 63);
527 /* [d t9 0 0 0 0 0 0 0 0 c] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
528 u0 = d & M; d >>= 26; c += u0 * R0;
529 VERIFY_BITS(u0, 26);
530 VERIFY_BITS(d, 37);
531 VERIFY_BITS(c, 61);
532 /* [d u0 t9 0 0 0 0 0 0 0 0 c-u0*R0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
533 t0 = c & M; c >>= 26; c += u0 * R1;
534 VERIFY_BITS(t0, 26);
535 VERIFY_BITS(c, 37);
536 /* [d u0 t9 0 0 0 0 0 0 0 c-u0*R1 t0-u0*R0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
537 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
538
539 c += (uint64_t)a[0] * b[1]
540 + (uint64_t)a[1] * b[0];
541 VERIFY_BITS(c, 62);
542 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p10 p9 0 0 0 0 0 0 0 p1 p0] */
543 d += (uint64_t)a[2] * b[9]
544 + (uint64_t)a[3] * b[8]
545 + (uint64_t)a[4] * b[7]
546 + (uint64_t)a[5] * b[6]
547 + (uint64_t)a[6] * b[5]
548 + (uint64_t)a[7] * b[4]
549 + (uint64_t)a[8] * b[3]
550 + (uint64_t)a[9] * b[2];
551 VERIFY_BITS(d, 63);
552 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
553 u1 = d & M; d >>= 26; c += u1 * R0;
554 VERIFY_BITS(u1, 26);
555 VERIFY_BITS(d, 37);
556 VERIFY_BITS(c, 63);
557 /* [d u1 0 t9 0 0 0 0 0 0 0 c-u1*R0 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
558 t1 = c & M; c >>= 26; c += u1 * R1;
559 VERIFY_BITS(t1, 26);
560 VERIFY_BITS(c, 38);
561 /* [d u1 0 t9 0 0 0 0 0 0 c-u1*R1 t1-u1*R0 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
562 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
563
564 c += (uint64_t)a[0] * b[2]
565 + (uint64_t)a[1] * b[1]
566 + (uint64_t)a[2] * b[0];
567 VERIFY_BITS(c, 62);
568 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
569 d += (uint64_t)a[3] * b[9]
570 + (uint64_t)a[4] * b[8]
571 + (uint64_t)a[5] * b[7]
572 + (uint64_t)a[6] * b[6]
573 + (uint64_t)a[7] * b[5]
574 + (uint64_t)a[8] * b[4]
575 + (uint64_t)a[9] * b[3];
576 VERIFY_BITS(d, 63);
577 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
578 u2 = d & M; d >>= 26; c += u2 * R0;
579 VERIFY_BITS(u2, 26);
580 VERIFY_BITS(d, 37);
581 VERIFY_BITS(c, 63);
582 /* [d u2 0 0 t9 0 0 0 0 0 0 c-u2*R0 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
583 t2 = c & M; c >>= 26; c += u2 * R1;
584 VERIFY_BITS(t2, 26);
585 VERIFY_BITS(c, 38);
586 /* [d u2 0 0 t9 0 0 0 0 0 c-u2*R1 t2-u2*R0 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
587 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
588
589 c += (uint64_t)a[0] * b[3]
590 + (uint64_t)a[1] * b[2]
591 + (uint64_t)a[2] * b[1]
592 + (uint64_t)a[3] * b[0];
593 VERIFY_BITS(c, 63);
594 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
595 d += (uint64_t)a[4] * b[9]
596 + (uint64_t)a[5] * b[8]
597 + (uint64_t)a[6] * b[7]
598 + (uint64_t)a[7] * b[6]
599 + (uint64_t)a[8] * b[5]
600 + (uint64_t)a[9] * b[4];
601 VERIFY_BITS(d, 63);
602 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
603 u3 = d & M; d >>= 26; c += u3 * R0;
604 VERIFY_BITS(u3, 26);
605 VERIFY_BITS(d, 37);
606 /* VERIFY_BITS(c, 64); */
607 /* [d u3 0 0 0 t9 0 0 0 0 0 c-u3*R0 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
608 t3 = c & M; c >>= 26; c += u3 * R1;
609 VERIFY_BITS(t3, 26);
610 VERIFY_BITS(c, 39);
611 /* [d u3 0 0 0 t9 0 0 0 0 c-u3*R1 t3-u3*R0 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
612 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
613
614 c += (uint64_t)a[0] * b[4]
615 + (uint64_t)a[1] * b[3]
616 + (uint64_t)a[2] * b[2]
617 + (uint64_t)a[3] * b[1]
618 + (uint64_t)a[4] * b[0];
619 VERIFY_BITS(c, 63);
620 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
621 d += (uint64_t)a[5] * b[9]
622 + (uint64_t)a[6] * b[8]
623 + (uint64_t)a[7] * b[7]
624 + (uint64_t)a[8] * b[6]
625 + (uint64_t)a[9] * b[5];
626 VERIFY_BITS(d, 62);
627 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
628 u4 = d & M; d >>= 26; c += u4 * R0;
629 VERIFY_BITS(u4, 26);
630 VERIFY_BITS(d, 36);
631 /* VERIFY_BITS(c, 64); */
632 /* [d u4 0 0 0 0 t9 0 0 0 0 c-u4*R0 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
633 t4 = c & M; c >>= 26; c += u4 * R1;
634 VERIFY_BITS(t4, 26);
635 VERIFY_BITS(c, 39);
636 /* [d u4 0 0 0 0 t9 0 0 0 c-u4*R1 t4-u4*R0 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
637 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
638
639 c += (uint64_t)a[0] * b[5]
640 + (uint64_t)a[1] * b[4]
641 + (uint64_t)a[2] * b[3]
642 + (uint64_t)a[3] * b[2]
643 + (uint64_t)a[4] * b[1]
644 + (uint64_t)a[5] * b[0];
645 VERIFY_BITS(c, 63);
646 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
647 d += (uint64_t)a[6] * b[9]
648 + (uint64_t)a[7] * b[8]
649 + (uint64_t)a[8] * b[7]
650 + (uint64_t)a[9] * b[6];
651 VERIFY_BITS(d, 62);
652 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
653 u5 = d & M; d >>= 26; c += u5 * R0;
654 VERIFY_BITS(u5, 26);
655 VERIFY_BITS(d, 36);
656 /* VERIFY_BITS(c, 64); */
657 /* [d u5 0 0 0 0 0 t9 0 0 0 c-u5*R0 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
658 t5 = c & M; c >>= 26; c += u5 * R1;
659 VERIFY_BITS(t5, 26);
660 VERIFY_BITS(c, 39);
661 /* [d u5 0 0 0 0 0 t9 0 0 c-u5*R1 t5-u5*R0 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
662 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
663
664 c += (uint64_t)a[0] * b[6]
665 + (uint64_t)a[1] * b[5]
666 + (uint64_t)a[2] * b[4]
667 + (uint64_t)a[3] * b[3]
668 + (uint64_t)a[4] * b[2]
669 + (uint64_t)a[5] * b[1]
670 + (uint64_t)a[6] * b[0];
671 VERIFY_BITS(c, 63);
672 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
673 d += (uint64_t)a[7] * b[9]
674 + (uint64_t)a[8] * b[8]
675 + (uint64_t)a[9] * b[7];
676 VERIFY_BITS(d, 61);
677 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
678 u6 = d & M; d >>= 26; c += u6 * R0;
679 VERIFY_BITS(u6, 26);
680 VERIFY_BITS(d, 35);
681 /* VERIFY_BITS(c, 64); */
682 /* [d u6 0 0 0 0 0 0 t9 0 0 c-u6*R0 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
683 t6 = c & M; c >>= 26; c += u6 * R1;
684 VERIFY_BITS(t6, 26);
685 VERIFY_BITS(c, 39);
686 /* [d u6 0 0 0 0 0 0 t9 0 c-u6*R1 t6-u6*R0 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
687 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
688
689 c += (uint64_t)a[0] * b[7]
690 + (uint64_t)a[1] * b[6]
691 + (uint64_t)a[2] * b[5]
692 + (uint64_t)a[3] * b[4]
693 + (uint64_t)a[4] * b[3]
694 + (uint64_t)a[5] * b[2]
695 + (uint64_t)a[6] * b[1]
696 + (uint64_t)a[7] * b[0];
697 /* VERIFY_BITS(c, 64); */
698 VERIFY_CHECK(c <= 0x8000007C00000007ULL);
699 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
700 d += (uint64_t)a[8] * b[9]
701 + (uint64_t)a[9] * b[8];
702 VERIFY_BITS(d, 58);
703 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
704 u7 = d & M; d >>= 26; c += u7 * R0;
705 VERIFY_BITS(u7, 26);
706 VERIFY_BITS(d, 32);
707 /* VERIFY_BITS(c, 64); */
708 VERIFY_CHECK(c <= 0x800001703FFFC2F7ULL);
709 /* [d u7 0 0 0 0 0 0 0 t9 0 c-u7*R0 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
710 t7 = c & M; c >>= 26; c += u7 * R1;
711 VERIFY_BITS(t7, 26);
712 VERIFY_BITS(c, 38);
713 /* [d u7 0 0 0 0 0 0 0 t9 c-u7*R1 t7-u7*R0 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
714 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
715
716 c += (uint64_t)a[0] * b[8]
717 + (uint64_t)a[1] * b[7]
718 + (uint64_t)a[2] * b[6]
719 + (uint64_t)a[3] * b[5]
720 + (uint64_t)a[4] * b[4]
721 + (uint64_t)a[5] * b[3]
722 + (uint64_t)a[6] * b[2]
723 + (uint64_t)a[7] * b[1]
724 + (uint64_t)a[8] * b[0];
725 /* VERIFY_BITS(c, 64); */
726 VERIFY_CHECK(c <= 0x9000007B80000008ULL);
727 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
728 d += (uint64_t)a[9] * b[9];
729 VERIFY_BITS(d, 57);
730 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
731 u8 = d & M; d >>= 26; c += u8 * R0;
732 VERIFY_BITS(u8, 26);
733 VERIFY_BITS(d, 31);
734 /* VERIFY_BITS(c, 64); */
735 VERIFY_CHECK(c <= 0x9000016FBFFFC2F8ULL);
736 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 t4 t3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
737
738 r[3] = t3;
739 VERIFY_BITS(r[3], 26);
740 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 t4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
741 r[4] = t4;
742 VERIFY_BITS(r[4], 26);
743 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
744 r[5] = t5;
745 VERIFY_BITS(r[5], 26);
746 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
747 r[6] = t6;
748 VERIFY_BITS(r[6], 26);
749 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
750 r[7] = t7;
751 VERIFY_BITS(r[7], 26);
752 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
753
754 r[8] = c & M; c >>= 26; c += u8 * R1;
755 VERIFY_BITS(r[8], 26);
756 VERIFY_BITS(c, 39);
757 /* [d u8 0 0 0 0 0 0 0 0 t9+c-u8*R1 r8-u8*R0 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
758 /* [d 0 0 0 0 0 0 0 0 0 t9+c r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
759 c += d * R0 + t9;
760 VERIFY_BITS(c, 45);
761 /* [d 0 0 0 0 0 0 0 0 0 c-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
762 r[9] = c & (M >> 4); c >>= 22; c += d * (R1 << 4);
763 VERIFY_BITS(r[9], 22);
764 VERIFY_BITS(c, 46);
765 /* [d 0 0 0 0 0 0 0 0 r9+((c-d*R1<<4)<<22)-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
766 /* [d 0 0 0 0 0 0 0 -d*R1 r9+(c<<22)-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
767 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
768
769 d = c * (R0 >> 4) + t0;
770 VERIFY_BITS(d, 56);
771 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1 d-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
772 r[0] = d & M; d >>= 26;
773 VERIFY_BITS(r[0], 26);
774 VERIFY_BITS(d, 30);
775 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1+d r0-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
776 d += c * (R1 >> 4) + t1;
777 VERIFY_BITS(d, 53);
778 VERIFY_CHECK(d <= 0x10000003FFFFBFULL);
779 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 d-c*R1>>4 r0-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
780 /* [r9 r8 r7 r6 r5 r4 r3 t2 d r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
781 r[1] = d & M; d >>= 26;
782 VERIFY_BITS(r[1], 26);
783 VERIFY_BITS(d, 27);
784 VERIFY_CHECK(d <= 0x4000000ULL);
785 /* [r9 r8 r7 r6 r5 r4 r3 t2+d r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
786 d += t2;
787 VERIFY_BITS(d, 27);
788 /* [r9 r8 r7 r6 r5 r4 r3 d r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
789 r[2] = d;
790 VERIFY_BITS(r[2], 27);
791 /* [r9 r8 r7 r6 r5 r4 r3 r2 r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
792}
793
794SECP256K1_INLINE static void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a) {
795 uint64_t c, d;
796 uint64_t u0, u1, u2, u3, u4, u5, u6, u7, u8;
797 uint32_t t9, t0, t1, t2, t3, t4, t5, t6, t7;
798 const uint32_t M = 0x3FFFFFFUL, R0 = 0x3D10UL, R1 = 0x400UL;
799
800 VERIFY_BITS(a[0], 30);
801 VERIFY_BITS(a[1], 30);
802 VERIFY_BITS(a[2], 30);
803 VERIFY_BITS(a[3], 30);
804 VERIFY_BITS(a[4], 30);
805 VERIFY_BITS(a[5], 30);
806 VERIFY_BITS(a[6], 30);
807 VERIFY_BITS(a[7], 30);
808 VERIFY_BITS(a[8], 30);
809 VERIFY_BITS(a[9], 26);
810
816 d = (uint64_t)(a[0]*2) * a[9]
817 + (uint64_t)(a[1]*2) * a[8]
818 + (uint64_t)(a[2]*2) * a[7]
819 + (uint64_t)(a[3]*2) * a[6]
820 + (uint64_t)(a[4]*2) * a[5];
821 /* VERIFY_BITS(d, 64); */
822 /* [d 0 0 0 0 0 0 0 0 0] = [p9 0 0 0 0 0 0 0 0 0] */
823 t9 = d & M; d >>= 26;
824 VERIFY_BITS(t9, 26);
825 VERIFY_BITS(d, 38);
826 /* [d t9 0 0 0 0 0 0 0 0 0] = [p9 0 0 0 0 0 0 0 0 0] */
827
828 c = (uint64_t)a[0] * a[0];
829 VERIFY_BITS(c, 60);
830 /* [d t9 0 0 0 0 0 0 0 0 c] = [p9 0 0 0 0 0 0 0 0 p0] */
831 d += (uint64_t)(a[1]*2) * a[9]
832 + (uint64_t)(a[2]*2) * a[8]
833 + (uint64_t)(a[3]*2) * a[7]
834 + (uint64_t)(a[4]*2) * a[6]
835 + (uint64_t)a[5] * a[5];
836 VERIFY_BITS(d, 63);
837 /* [d t9 0 0 0 0 0 0 0 0 c] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
838 u0 = d & M; d >>= 26; c += u0 * R0;
839 VERIFY_BITS(u0, 26);
840 VERIFY_BITS(d, 37);
841 VERIFY_BITS(c, 61);
842 /* [d u0 t9 0 0 0 0 0 0 0 0 c-u0*R0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
843 t0 = c & M; c >>= 26; c += u0 * R1;
844 VERIFY_BITS(t0, 26);
845 VERIFY_BITS(c, 37);
846 /* [d u0 t9 0 0 0 0 0 0 0 c-u0*R1 t0-u0*R0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
847 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p10 p9 0 0 0 0 0 0 0 0 p0] */
848
849 c += (uint64_t)(a[0]*2) * a[1];
850 VERIFY_BITS(c, 62);
851 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p10 p9 0 0 0 0 0 0 0 p1 p0] */
852 d += (uint64_t)(a[2]*2) * a[9]
853 + (uint64_t)(a[3]*2) * a[8]
854 + (uint64_t)(a[4]*2) * a[7]
855 + (uint64_t)(a[5]*2) * a[6];
856 VERIFY_BITS(d, 63);
857 /* [d 0 t9 0 0 0 0 0 0 0 c t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
858 u1 = d & M; d >>= 26; c += u1 * R0;
859 VERIFY_BITS(u1, 26);
860 VERIFY_BITS(d, 37);
861 VERIFY_BITS(c, 63);
862 /* [d u1 0 t9 0 0 0 0 0 0 0 c-u1*R0 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
863 t1 = c & M; c >>= 26; c += u1 * R1;
864 VERIFY_BITS(t1, 26);
865 VERIFY_BITS(c, 38);
866 /* [d u1 0 t9 0 0 0 0 0 0 c-u1*R1 t1-u1*R0 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
867 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p11 p10 p9 0 0 0 0 0 0 0 p1 p0] */
868
869 c += (uint64_t)(a[0]*2) * a[2]
870 + (uint64_t)a[1] * a[1];
871 VERIFY_BITS(c, 62);
872 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
873 d += (uint64_t)(a[3]*2) * a[9]
874 + (uint64_t)(a[4]*2) * a[8]
875 + (uint64_t)(a[5]*2) * a[7]
876 + (uint64_t)a[6] * a[6];
877 VERIFY_BITS(d, 63);
878 /* [d 0 0 t9 0 0 0 0 0 0 c t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
879 u2 = d & M; d >>= 26; c += u2 * R0;
880 VERIFY_BITS(u2, 26);
881 VERIFY_BITS(d, 37);
882 VERIFY_BITS(c, 63);
883 /* [d u2 0 0 t9 0 0 0 0 0 0 c-u2*R0 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
884 t2 = c & M; c >>= 26; c += u2 * R1;
885 VERIFY_BITS(t2, 26);
886 VERIFY_BITS(c, 38);
887 /* [d u2 0 0 t9 0 0 0 0 0 c-u2*R1 t2-u2*R0 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
888 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 0 p2 p1 p0] */
889
890 c += (uint64_t)(a[0]*2) * a[3]
891 + (uint64_t)(a[1]*2) * a[2];
892 VERIFY_BITS(c, 63);
893 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
894 d += (uint64_t)(a[4]*2) * a[9]
895 + (uint64_t)(a[5]*2) * a[8]
896 + (uint64_t)(a[6]*2) * a[7];
897 VERIFY_BITS(d, 63);
898 /* [d 0 0 0 t9 0 0 0 0 0 c t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
899 u3 = d & M; d >>= 26; c += u3 * R0;
900 VERIFY_BITS(u3, 26);
901 VERIFY_BITS(d, 37);
902 /* VERIFY_BITS(c, 64); */
903 /* [d u3 0 0 0 t9 0 0 0 0 0 c-u3*R0 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
904 t3 = c & M; c >>= 26; c += u3 * R1;
905 VERIFY_BITS(t3, 26);
906 VERIFY_BITS(c, 39);
907 /* [d u3 0 0 0 t9 0 0 0 0 c-u3*R1 t3-u3*R0 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
908 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 0 p3 p2 p1 p0] */
909
910 c += (uint64_t)(a[0]*2) * a[4]
911 + (uint64_t)(a[1]*2) * a[3]
912 + (uint64_t)a[2] * a[2];
913 VERIFY_BITS(c, 63);
914 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
915 d += (uint64_t)(a[5]*2) * a[9]
916 + (uint64_t)(a[6]*2) * a[8]
917 + (uint64_t)a[7] * a[7];
918 VERIFY_BITS(d, 62);
919 /* [d 0 0 0 0 t9 0 0 0 0 c t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
920 u4 = d & M; d >>= 26; c += u4 * R0;
921 VERIFY_BITS(u4, 26);
922 VERIFY_BITS(d, 36);
923 /* VERIFY_BITS(c, 64); */
924 /* [d u4 0 0 0 0 t9 0 0 0 0 c-u4*R0 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
925 t4 = c & M; c >>= 26; c += u4 * R1;
926 VERIFY_BITS(t4, 26);
927 VERIFY_BITS(c, 39);
928 /* [d u4 0 0 0 0 t9 0 0 0 c-u4*R1 t4-u4*R0 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
929 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 0 p4 p3 p2 p1 p0] */
930
931 c += (uint64_t)(a[0]*2) * a[5]
932 + (uint64_t)(a[1]*2) * a[4]
933 + (uint64_t)(a[2]*2) * a[3];
934 VERIFY_BITS(c, 63);
935 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
936 d += (uint64_t)(a[6]*2) * a[9]
937 + (uint64_t)(a[7]*2) * a[8];
938 VERIFY_BITS(d, 62);
939 /* [d 0 0 0 0 0 t9 0 0 0 c t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
940 u5 = d & M; d >>= 26; c += u5 * R0;
941 VERIFY_BITS(u5, 26);
942 VERIFY_BITS(d, 36);
943 /* VERIFY_BITS(c, 64); */
944 /* [d u5 0 0 0 0 0 t9 0 0 0 c-u5*R0 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
945 t5 = c & M; c >>= 26; c += u5 * R1;
946 VERIFY_BITS(t5, 26);
947 VERIFY_BITS(c, 39);
948 /* [d u5 0 0 0 0 0 t9 0 0 c-u5*R1 t5-u5*R0 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
949 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 0 p5 p4 p3 p2 p1 p0] */
950
951 c += (uint64_t)(a[0]*2) * a[6]
952 + (uint64_t)(a[1]*2) * a[5]
953 + (uint64_t)(a[2]*2) * a[4]
954 + (uint64_t)a[3] * a[3];
955 VERIFY_BITS(c, 63);
956 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
957 d += (uint64_t)(a[7]*2) * a[9]
958 + (uint64_t)a[8] * a[8];
959 VERIFY_BITS(d, 61);
960 /* [d 0 0 0 0 0 0 t9 0 0 c t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
961 u6 = d & M; d >>= 26; c += u6 * R0;
962 VERIFY_BITS(u6, 26);
963 VERIFY_BITS(d, 35);
964 /* VERIFY_BITS(c, 64); */
965 /* [d u6 0 0 0 0 0 0 t9 0 0 c-u6*R0 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
966 t6 = c & M; c >>= 26; c += u6 * R1;
967 VERIFY_BITS(t6, 26);
968 VERIFY_BITS(c, 39);
969 /* [d u6 0 0 0 0 0 0 t9 0 c-u6*R1 t6-u6*R0 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
970 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 0 p6 p5 p4 p3 p2 p1 p0] */
971
972 c += (uint64_t)(a[0]*2) * a[7]
973 + (uint64_t)(a[1]*2) * a[6]
974 + (uint64_t)(a[2]*2) * a[5]
975 + (uint64_t)(a[3]*2) * a[4];
976 /* VERIFY_BITS(c, 64); */
977 VERIFY_CHECK(c <= 0x8000007C00000007ULL);
978 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
979 d += (uint64_t)(a[8]*2) * a[9];
980 VERIFY_BITS(d, 58);
981 /* [d 0 0 0 0 0 0 0 t9 0 c t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
982 u7 = d & M; d >>= 26; c += u7 * R0;
983 VERIFY_BITS(u7, 26);
984 VERIFY_BITS(d, 32);
985 /* VERIFY_BITS(c, 64); */
986 VERIFY_CHECK(c <= 0x800001703FFFC2F7ULL);
987 /* [d u7 0 0 0 0 0 0 0 t9 0 c-u7*R0 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
988 t7 = c & M; c >>= 26; c += u7 * R1;
989 VERIFY_BITS(t7, 26);
990 VERIFY_BITS(c, 38);
991 /* [d u7 0 0 0 0 0 0 0 t9 c-u7*R1 t7-u7*R0 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
992 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 0 p7 p6 p5 p4 p3 p2 p1 p0] */
993
994 c += (uint64_t)(a[0]*2) * a[8]
995 + (uint64_t)(a[1]*2) * a[7]
996 + (uint64_t)(a[2]*2) * a[6]
997 + (uint64_t)(a[3]*2) * a[5]
998 + (uint64_t)a[4] * a[4];
999 /* VERIFY_BITS(c, 64); */
1000 VERIFY_CHECK(c <= 0x9000007B80000008ULL);
1001 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1002 d += (uint64_t)a[9] * a[9];
1003 VERIFY_BITS(d, 57);
1004 /* [d 0 0 0 0 0 0 0 0 t9 c t7 t6 t5 t4 t3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1005 u8 = d & M; d >>= 26; c += u8 * R0;
1006 VERIFY_BITS(u8, 26);
1007 VERIFY_BITS(d, 31);
1008 /* VERIFY_BITS(c, 64); */
1009 VERIFY_CHECK(c <= 0x9000016FBFFFC2F8ULL);
1010 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 t4 t3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1011
1012 r[3] = t3;
1013 VERIFY_BITS(r[3], 26);
1014 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 t4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1015 r[4] = t4;
1016 VERIFY_BITS(r[4], 26);
1017 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 t5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1018 r[5] = t5;
1019 VERIFY_BITS(r[5], 26);
1020 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 t6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1021 r[6] = t6;
1022 VERIFY_BITS(r[6], 26);
1023 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 t7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1024 r[7] = t7;
1025 VERIFY_BITS(r[7], 26);
1026 /* [d u8 0 0 0 0 0 0 0 0 t9 c-u8*R0 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1027
1028 r[8] = c & M; c >>= 26; c += u8 * R1;
1029 VERIFY_BITS(r[8], 26);
1030 VERIFY_BITS(c, 39);
1031 /* [d u8 0 0 0 0 0 0 0 0 t9+c-u8*R1 r8-u8*R0 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1032 /* [d 0 0 0 0 0 0 0 0 0 t9+c r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1033 c += d * R0 + t9;
1034 VERIFY_BITS(c, 45);
1035 /* [d 0 0 0 0 0 0 0 0 0 c-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1036 r[9] = c & (M >> 4); c >>= 22; c += d * (R1 << 4);
1037 VERIFY_BITS(r[9], 22);
1038 VERIFY_BITS(c, 46);
1039 /* [d 0 0 0 0 0 0 0 0 r9+((c-d*R1<<4)<<22)-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1040 /* [d 0 0 0 0 0 0 0 -d*R1 r9+(c<<22)-d*R0 r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1041 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1 t0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1042
1043 d = c * (R0 >> 4) + t0;
1044 VERIFY_BITS(d, 56);
1045 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1 d-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1046 r[0] = d & M; d >>= 26;
1047 VERIFY_BITS(r[0], 26);
1048 VERIFY_BITS(d, 30);
1049 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 t1+d r0-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1050 d += c * (R1 >> 4) + t1;
1051 VERIFY_BITS(d, 53);
1052 VERIFY_CHECK(d <= 0x10000003FFFFBFULL);
1053 /* [r9+(c<<22) r8 r7 r6 r5 r4 r3 t2 d-c*R1>>4 r0-c*R0>>4] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1054 /* [r9 r8 r7 r6 r5 r4 r3 t2 d r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1055 r[1] = d & M; d >>= 26;
1056 VERIFY_BITS(r[1], 26);
1057 VERIFY_BITS(d, 27);
1058 VERIFY_CHECK(d <= 0x4000000ULL);
1059 /* [r9 r8 r7 r6 r5 r4 r3 t2+d r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1060 d += t2;
1061 VERIFY_BITS(d, 27);
1062 /* [r9 r8 r7 r6 r5 r4 r3 d r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1063 r[2] = d;
1064 VERIFY_BITS(r[2], 27);
1065 /* [r9 r8 r7 r6 r5 r4 r3 r2 r1 r0] = [p18 p17 p16 p15 p14 p13 p12 p11 p10 p9 p8 p7 p6 p5 p4 p3 p2 p1 p0] */
1066}
1067#endif
1068
1070#ifdef VERIFY
1071 VERIFY_CHECK(a->magnitude <= 8);
1072 VERIFY_CHECK(b->magnitude <= 8);
1073 secp256k1_fe_verify(a);
1074 secp256k1_fe_verify(b);
1075 VERIFY_CHECK(r != b);
1076 VERIFY_CHECK(a != b);
1077#endif
1078 secp256k1_fe_mul_inner(r->n, a->n, b->n);
1079#ifdef VERIFY
1080 r->magnitude = 1;
1081 r->normalized = 0;
1082 secp256k1_fe_verify(r);
1083#endif
1084}
1085
1086static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
1087#ifdef VERIFY
1088 VERIFY_CHECK(a->magnitude <= 8);
1089 secp256k1_fe_verify(a);
1090#endif
1091 secp256k1_fe_sqr_inner(r->n, a->n);
1092#ifdef VERIFY
1093 r->magnitude = 1;
1094 r->normalized = 0;
1095 secp256k1_fe_verify(r);
1096#endif
1097}
1098
1100 uint32_t mask0, mask1;
1101 VG_CHECK_VERIFY(r->n, sizeof(r->n));
1102 mask0 = flag + ~((uint32_t)0);
1103 mask1 = ~mask0;
1104 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
1105 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
1106 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
1107 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
1108 r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
1109 r->n[5] = (r->n[5] & mask0) | (a->n[5] & mask1);
1110 r->n[6] = (r->n[6] & mask0) | (a->n[6] & mask1);
1111 r->n[7] = (r->n[7] & mask0) | (a->n[7] & mask1);
1112 r->n[8] = (r->n[8] & mask0) | (a->n[8] & mask1);
1113 r->n[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
1114#ifdef VERIFY
1115 if (flag) {
1116 r->magnitude = a->magnitude;
1117 r->normalized = a->normalized;
1118 }
1119#endif
1120}
1121
1123 uint32_t mask0, mask1;
1124 VG_CHECK_VERIFY(r->n, sizeof(r->n));
1125 mask0 = flag + ~((uint32_t)0);
1126 mask1 = ~mask0;
1127 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
1128 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
1129 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
1130 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
1131 r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
1132 r->n[5] = (r->n[5] & mask0) | (a->n[5] & mask1);
1133 r->n[6] = (r->n[6] & mask0) | (a->n[6] & mask1);
1134 r->n[7] = (r->n[7] & mask0) | (a->n[7] & mask1);
1135}
1136
1138#ifdef VERIFY
1139 VERIFY_CHECK(a->normalized);
1140#endif
1141 r->n[0] = a->n[0] | a->n[1] << 26;
1142 r->n[1] = a->n[1] >> 6 | a->n[2] << 20;
1143 r->n[2] = a->n[2] >> 12 | a->n[3] << 14;
1144 r->n[3] = a->n[3] >> 18 | a->n[4] << 8;
1145 r->n[4] = a->n[4] >> 24 | a->n[5] << 2 | a->n[6] << 28;
1146 r->n[5] = a->n[6] >> 4 | a->n[7] << 22;
1147 r->n[6] = a->n[7] >> 10 | a->n[8] << 16;
1148 r->n[7] = a->n[8] >> 16 | a->n[9] << 10;
1149}
1150
1152 r->n[0] = a->n[0] & 0x3FFFFFFUL;
1153 r->n[1] = a->n[0] >> 26 | ((a->n[1] << 6) & 0x3FFFFFFUL);
1154 r->n[2] = a->n[1] >> 20 | ((a->n[2] << 12) & 0x3FFFFFFUL);
1155 r->n[3] = a->n[2] >> 14 | ((a->n[3] << 18) & 0x3FFFFFFUL);
1156 r->n[4] = a->n[3] >> 8 | ((a->n[4] << 24) & 0x3FFFFFFUL);
1157 r->n[5] = (a->n[4] >> 2) & 0x3FFFFFFUL;
1158 r->n[6] = a->n[4] >> 28 | ((a->n[5] << 4) & 0x3FFFFFFUL);
1159 r->n[7] = a->n[5] >> 22 | ((a->n[6] << 10) & 0x3FFFFFFUL);
1160 r->n[8] = a->n[6] >> 16 | ((a->n[7] << 16) & 0x3FFFFFFUL);
1161 r->n[9] = a->n[7] >> 10;
1162#ifdef VERIFY
1163 r->magnitude = 1;
1164 r->normalized = 1;
1165#endif
1166}
1167
1169 const uint32_t M26 = UINT32_MAX >> 6;
1170 const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
1171 a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
1172
1173 /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
1174 * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
1175 */
1176 VERIFY_CHECK(a0 >> 30 == 0);
1177 VERIFY_CHECK(a1 >> 30 == 0);
1178 VERIFY_CHECK(a2 >> 30 == 0);
1179 VERIFY_CHECK(a3 >> 30 == 0);
1180 VERIFY_CHECK(a4 >> 30 == 0);
1181 VERIFY_CHECK(a5 >> 30 == 0);
1182 VERIFY_CHECK(a6 >> 30 == 0);
1183 VERIFY_CHECK(a7 >> 30 == 0);
1184 VERIFY_CHECK(a8 >> 16 == 0);
1185
1186 r->n[0] = a0 & M26;
1187 r->n[1] = (a0 >> 26 | a1 << 4) & M26;
1188 r->n[2] = (a1 >> 22 | a2 << 8) & M26;
1189 r->n[3] = (a2 >> 18 | a3 << 12) & M26;
1190 r->n[4] = (a3 >> 14 | a4 << 16) & M26;
1191 r->n[5] = (a4 >> 10 | a5 << 20) & M26;
1192 r->n[6] = (a5 >> 6 | a6 << 24) & M26;
1193 r->n[7] = (a6 >> 2 ) & M26;
1194 r->n[8] = (a6 >> 28 | a7 << 2) & M26;
1195 r->n[9] = (a7 >> 24 | a8 << 6);
1196
1197#ifdef VERIFY
1198 r->magnitude = 1;
1199 r->normalized = 1;
1200 secp256k1_fe_verify(r);
1201#endif
1202}
1203
1205 const uint32_t M30 = UINT32_MAX >> 2;
1206 const uint64_t a0 = a->n[0], a1 = a->n[1], a2 = a->n[2], a3 = a->n[3], a4 = a->n[4],
1207 a5 = a->n[5], a6 = a->n[6], a7 = a->n[7], a8 = a->n[8], a9 = a->n[9];
1208
1209#ifdef VERIFY
1210 VERIFY_CHECK(a->normalized);
1211#endif
1212
1213 r->v[0] = (a0 | a1 << 26) & M30;
1214 r->v[1] = (a1 >> 4 | a2 << 22) & M30;
1215 r->v[2] = (a2 >> 8 | a3 << 18) & M30;
1216 r->v[3] = (a3 >> 12 | a4 << 14) & M30;
1217 r->v[4] = (a4 >> 16 | a5 << 10) & M30;
1218 r->v[5] = (a5 >> 20 | a6 << 6) & M30;
1219 r->v[6] = (a6 >> 24 | a7 << 2
1220 | a8 << 28) & M30;
1221 r->v[7] = (a8 >> 2 | a9 << 24) & M30;
1222 r->v[8] = a9 >> 6;
1223}
1224
1226 {{-0x3D1, -4, 0, 0, 0, 0, 0, 0, 65536}},
1227 0x2DDACACFL
1228};
1229
1230static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x) {
1231 secp256k1_fe tmp;
1233
1234 tmp = *x;
1236 secp256k1_fe_to_signed30(&s, &tmp);
1239
1241}
1242
1244 secp256k1_fe tmp;
1246
1247 tmp = *x;
1249 secp256k1_fe_to_signed30(&s, &tmp);
1252
1254}
1255
1256#endif /* SECP256K1_FIELD_REPR_IMPL_H */
static SECP256K1_INLINE void secp256k1_fe_set_int(secp256k1_fe *r, int a)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a)
static const secp256k1_modinv32_modinfo secp256k1_const_modinfo_fe
static SECP256K1_INLINE int secp256k1_fe_is_zero(const secp256k1_fe *a)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_mul_int(secp256k1_fe *r, int a)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static SECP256K1_INLINE void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t *SECP256K1_RESTRICT b)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static void secp256k1_fe_from_signed30(secp256k1_fe *r, const secp256k1_modinv32_signed30 *a)
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe *r)
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static SECP256K1_INLINE void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
static void secp256k1_fe_normalize(secp256k1_fe *r)
#define VERIFY_BITS(x, n)
static SECP256K1_INLINE int secp256k1_fe_is_odd(const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_clear(secp256k1_fe *a)
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static SECP256K1_INLINE void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_fe_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_fe *a)
static int secp256k1_fe_normalizes_to_zero(secp256k1_fe *r)
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static void secp256k1_modinv32(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
#define VG_CHECK_VERIFY(x, y)
Definition: util.h:88
#define VERIFY_CHECK(cond)
Definition: util.h:68
#define SECP256K1_RESTRICT
Definition: util.h:158
#define SECP256K1_INLINE
Definition: secp256k1.h:127
uint32_t n[10]
Definition: field_10x26.h:16