Debian Codegraph @

sources/ippcp/gfpec/ecnist/ifma_ecpoint_p521.c

raw symbols SCIP
1 /*************************************************************************
2 * Copyright (C) 2021 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *************************************************************************/
16
17 #include "owndefs.h"
18
19 #if (_IPP32E >= _IPP32E_K1)
20
21 #include "gfpec/ecnist/ifma_arith_p521.h"
22 #include "gfpec/ecnist/ifma_ecpoint_p521.h"
23 #include "gfpec/pcpgfpecstuff.h"
24
25 #define add(R, A, B) fe521_add_no_red((R), (A), (B))
26 #define sub(R, A, B) fe521_sub_no_red((R), (A), (B))
27 #define mul(R, A, B) ifma_amm52_p521(&(R), (A), (B))
28 #define sqr(R, A) ifma_ams52_p521(&(R), (A))
29 #define div2(R, A) ifma_half52_p521(&(R), (A))
30 #define norm(R, A) ifma_norm52_p521(&(R), (A))
31 #define lnorm(R, A) ifma_lnorm52_p521(&(R), (A))
32 #define inv(R, A) ifma_aminv52_p521(&(R), (A))
33 #define from_mont(R, A) ifma_frommont52_p521(&(R), (A))
34 /* duplicate mult/sqr/norm */
35 #define mul_dual(R1, A1, B1, R2, A2, B2) ifma_amm52_dual_p521(&(R1), (A1), (B1), &(R2), (A2), (B2))
36 #define sqr_dual(R1, A1, R2, A2) ifma_ams52_dual_p521(&(R1), (A1), &(R2), (A2))
37 #define norm_dual(R1, A1, R2, A2) ifma_norm52_dual_p521(&(R1), (A1), &(R2), (A2))
38 #define lnorm_dual(R1, A1, R2, A2) ifma_lnorm52_dual_p521(&(R1), (A1), &(R2), (A2))
39
40 /* clang-format off */
41 IPP_OWN_DEFN(void, ifma_ec_nistp521_get_affine_coords, (fe521 prx[],
42 fe521 pry[],
43 const P521_POINT_IFMA* a))
44 /* clang-format on */
45 {
46
47 fe521 z1, z2, z3;
48 FE521_SET(z1) = FE521_SET(z2) = FE521_SET(z3) = m256_setzero_i64();
49 inv(z1, a->z); /* 1/z */
50 sqr(z2, z1); /* (1/z)^2 */
51 lnorm(z2, z2); /**/
52
53 /* x = x/z^2 */
54 if (NULL != prx) {
55 mul(*prx, a->x, z2); /* x = x/z^2 */
56 lnorm(*prx, *prx); /**/
57 }
58 /* y = y/z^3 */
59 if (NULL != pry) {
60 mul(z3, z1, z2); /* (1/z)^3 */
61 lnorm(z3, z3); /**/
62 mul(*pry, a->y, z3); /* y = y/z^3 */
63 lnorm(*pry, *pry); /**/
64 }
65
66 return;
67 }
68
69 IPP_OWN_DEFN(void, ifma_ec_nistp521_dbl_point, (P521_POINT_IFMA * r, const P521_POINT_IFMA* p))
70 {
71 /*
72 * Algorithm (Gueron - Enhanced Montgomery Multiplication)
73 * l1 = 3x^2 + a*z^4 = (if p384 a = -3) = 3*(x^2 - z^4) = 3*(x - z^2)*(x + z^2)
74 * z2 = 2*y*z
75 * l2 = 4*x*y^2
76 * x2 = l1^2 - 2*l2
77 * l3 = 8*y^4
78 * y2 = l1*(l2 - x2) - l3
79 *
80 * sum aripmetic: 8 mul; 9 add/sub; 1 div2.
81 */
82 const fe521* x1 = &p->x;
83 const fe521* y1 = &p->y;
84 const fe521* z1 = &p->z;
85
86 fe521 x2;
87 fe521 y2;
88 fe521 z2;
89 FE521_SET(x2) = FE521_SET(y2) = FE521_SET(z2) = m256_setzero_i64();
90
91 fe521 T, U, V, A, B, H;
92 FE521_SET(T) = FE521_SET(U) = FE521_SET(V) = m256_setzero_i64();
93 FE521_SET(A) = FE521_SET(B) = FE521_SET(H) = m256_setzero_i64();
94
95 fe521 M2, M4, M8;
96 FE521_LOADU(M2, p521_x2); /* 2*p */
97 FE521_LOADU(M4, p521_x4); /* 4*p */
98 FE521_LOADU(M8, p521_x8); /* 8*p */
99
100 add(T, *y1, *y1); /* T = 2*y1 */
101 lnorm(T, T);
102
103 /* V = 4*y1^2 */
104 /* U = z1^2 */
105 sqr_dual(V, T, U, *z1);
106
107 sub(B, *x1, U); /* B = 2*p + x1 - z1^2 */
108 add(B, B, M2);
109 add(U, *x1, U); /* U = x1 + z1^2 */
110
111
112 lnorm_dual(V, V, U, U); /* normalization */
113 norm(B, B);
114
115 /* A = 4*x1*y1^2 */
116 /* B = (x1 - z1^2)*(x1 + z1^2) */
117 mul_dual(A, V, *x1, B, B, U);
118
119 add(x2, A, A); /* x2 = 8*x1*y1^2 (4p) */
120 add(H, B, B);
121 add(B, B, H); /* B(l1) = 3*(x1 - z1^2)*(x1 + z1^2) */
122
123
124 lnorm(B, B); /* normalization */
125
126 /* U = l1^2 */
127 /* y2 = 16*y^2 */
128 sqr_dual(U, B, y2, V);
129
130 sub(x2, U, x2); /* x2 = 4*p + l1^2 - 2*l2 */
131 add(x2, x2, M4);
132 div2(y2, y2);
133
134 sub(U, A, x2); /* U = 8*p + l2 - x2 */
135 add(U, U, M8);
136
137
138 norm(U, U); /* normalization */
139
140 /* z2 = 2*y1*z1 */
141 /* U = B(l1)*(A(l2) - x2) */
142 mul_dual(z2, T, *z1, U, U, B);
143
144 sub(y2, U, y2); /* y2 = 2*p + B(l1)*(A(l2) - x2) - y2(l3) */
145 add(y2, y2, M2);
146
147 norm_dual(r->x, x2, r->y, y2); /* normalization */
148 lnorm(r->z, z2);
149 return;
150 }
151
152 /* clang-format off */
153 IPP_OWN_DEFN(void, ifma_ec_nistp521_add_point, (P521_POINT_IFMA* r,
154 const P521_POINT_IFMA* p,
155 const P521_POINT_IFMA* q))
156 /* clang-format on */
157 {
158 /*
159 * Algorithm (Gueron - Enhanced Montgomery Multiplication)
160 *
161 * A = x1*z2^2 B = x2*z1^2 C = y1*z2^3 D = y2*z1^3
162 * E = B - A F = D - C
163 * x3 = -E^3 - 2*A*E^2 + F^2
164 * y3 = -C*E^3 + F*(A*E^2 - x3)
165 * z3 = z1*z2*E
166 */
167 const fe521* x1 = &p->x;
168 const fe521* y1 = &p->y;
169 const fe521* z1 = &p->z;
170 const mask8 p_is_inf = FE521_IS_ZERO(p->z);
171
172 const fe521* x2 = &q->x;
173 const fe521* y2 = &q->y;
174 const fe521* z2 = &q->z;
175 const mask8 q_is_inf = FE521_IS_ZERO(q->z);
176
177 fe521 x3;
178 fe521 y3;
179 fe521 z3;
180 FE521_SET(x3) = FE521_SET(y3) = FE521_SET(z3) = m256_setzero_i64();
181
182 fe521 M2, M4, M8;
183 FE521_LOADU(M2, p521_x2); /* 2*p */
184 FE521_LOADU(M4, p521_x4); /* 4*p */
185 FE521_LOADU(M8, p521_x8); /* 8*p */
186
187 fe521 U1, U2, S1, S2, H, R;
188 FE521_SET(U1) = FE521_SET(U2) = FE521_SET(S1) = m256_setzero_i64();
189 FE521_SET(S2) = FE521_SET(H) = FE521_SET(R) = m256_setzero_i64();
190
191 /* s1 = y1*z2 */
192 /* u1 = z2^2 */
193 mul_dual(S1, *y1, *z2, U1, *z2, *z2);
194
195 /* normalization */
196 lnorm_dual(S1, S1, U1, U1);
197
198 /* s2 = y2*z1 */
199 /* u2 = z1^2 */
200 mul_dual(S2, *y2, *z1, U2, *z1, *z1);
201
202 /* normalization */
203 lnorm_dual(S2, S2, U2, U2);
204
205 /* s1 = y1*z2^3 (C) */
206 /* s2 = y2*z1^3 (D) */
207 mul_dual(S1, S1, U1, S2, S2, U2);
208
209 /* normalization */
210 /* (need by correct compute F = D - C) */
211 lnorm_dual(S1, S1, S2, S2);
212
213 /* u1 = x1*z2^2 (A) */
214 /* u2 = x2*z1^2 (B) */
215 mul_dual(U1, *x1, U1, U2, *x2, U2);
216
217 /* normalization */
218 lnorm_dual(U1, U1, U2, U2);
219
220 sub(R, S2, S1); /* r = D - C (F) */
221 sub(H, U2, U1); /* h = B - A (E) */
222
223 /* checking the equality of X and Y coordinates (D - C == 0) and (B - A == 0) */
224 const mask8 f_are_zero = FE521_IS_ZERO(R);
225 const mask8 e_are_zero = FE521_IS_ZERO(H);
226 const mask8 point_is_equal = ((e_are_zero & f_are_zero) & (~p_is_inf) & (~q_is_inf));
227
228 __ALIGN64 P521_POINT_IFMA r2;
229 FE521_SET(r2.x) = FE521_SET(r2.y) = FE521_SET(r2.z) = m256_setzero_i64();
230 if ((mask8)0xFF == point_is_equal) {
231 ifma_ec_nistp521_dbl_point(&r2, p);
232 }
233
234 add(R, R, M2);
235 add(H, H, M2);
236
237 /* normalization */
238 norm_dual(R, R, H, H);
239
240 /* z3 = z1*z2 */
241 /* u2 = E^2 */
242 mul_dual(z3, *z1, *z2, U2, H, H);
243
244 /* normalization */
245 lnorm_dual(z3, z3, U2, U2);
246 /* z3 = (z1*z2)*E */
247 /* s2 = F^2 */
248 mul_dual(z3, z3, H, S2, R, R);
249 mul(H, H, U2); /* h = E^3 */
250
251 /* normalization */
252 lnorm(H, H);
253
254 mul(U1, U1, U2); /* u1 = A*E^2 */
255 sub(x3, S2, H); /* x3 = F^2 - E^3 */
256 add(x3, x3, M2);
257 add(U2, U1, U1); /* u2 = 2*A*E^2 */
258 mul(S1, S1, H); /* s1 = C*E^3 */
259 sub(x3, x3, U2); /* x3 = (F^2 - E^3) -2*A*E^2 */
260 add(x3, x3, M4);
261
262 sub(y3, U1, x3); /* y3 = A*E^2 - x3 */
263 add(y3, y3, M8);
264
265 /* normalization */
266 norm(y3, y3);
267
268 mul(y3, y3, R); /* y3 = F*(A*E^2 - x3) */
269 sub(y3, y3, S1); /* y3 = F*(A*E^2 - x3) - C*E^3 */
270 add(y3, y3, M2);
271
272 /* normalization */
273 norm_dual(x3, x3, y3, y3);
274 lnorm(z3, z3);
275
276 /* T = p_is_inf ? q : T */
277 FE521_MASK_MOV(x3, x3, p_is_inf, *x2);
278 FE521_MASK_MOV(y3, y3, p_is_inf, *y2);
279 FE521_MASK_MOV(z3, z3, p_is_inf, *z2);
280
281 /* T = q_is_inf ? p : T */
282 FE521_MASK_MOV(x3, x3, q_is_inf, *x1);
283 FE521_MASK_MOV(y3, y3, q_is_inf, *y1);
284 FE521_MASK_MOV(z3, z3, q_is_inf, *z1);
285
286 /* r = point_is_equal ? r2 : T */
287 FE521_MASK_MOV(x3, x3, point_is_equal, r2.x);
288 FE521_MASK_MOV(y3, y3, point_is_equal, r2.y);
289 FE521_MASK_MOV(z3, z3, point_is_equal, r2.z);
290
291 FE521_COPY(r->x, x3);
292 FE521_COPY(r->y, y3);
293 FE521_COPY(r->z, z3);
294
295 return;
296 }
297
298 /* clang-format off */
299 IPP_OWN_DEFN(void, ifma_ec_nistp521_add_point_affine, (P521_POINT_IFMA* r,
300 const P521_POINT_IFMA* p,
301 const P521_POINT_AFFINE_IFMA* q))
302 /* clang-format on */
303 {
304 /*
305 * Algorithm (Gueron - Enhanced Montgomery Multiplication)
306 *
307 * A = x1 B = x2*z1^2 C = y1 D = y2*z1^3
308 * E = B - A(x1) F = D - C(y1)
309 * x3 = -E^3 - 2*A(x1)*E^2 + F^2
310 * y3 = -C(y1)*E^3 + F*(A(x1)*E^2 - x3)
311 * z3 = z1*E
312 */
313 /* coordinates of p (jacobian projective) */
314 const fe521* x1 = &p->x;
315 const fe521* y1 = &p->y;
316 const fe521* z1 = &p->z;
317 const mask8 p_is_inf = FE521_IS_ZERO(p->z);
318
319 /* coordinate of q (affine) */
320 const fe521* x2 = &q->x;
321 const fe521* y2 = &q->y;
322 const mask8 q_is_inf = (FE521_IS_ZERO(q->x) & FE521_IS_ZERO(q->y));
323
324 fe521 x3;
325 fe521 y3;
326 fe521 z3;
327 FE521_SET(x3) = FE521_SET(y3) = FE521_SET(z3) = m256_setzero_i64();
328
329 fe521 M2, M4, M8;
330 FE521_LOADU(M2, p521_x2); /* 2*p */
331 FE521_LOADU(M4, p521_x4); /* 4*p */
332 FE521_LOADU(M8, p521_x8); /* 8*p */
333
334 fe521 U2, S2, H, R;
335 FE521_SET(U2) = FE521_SET(S2) = FE521_SET(H) = FE521_SET(R) = m256_setzero_i64();
336
337 /* R = z1^2 */
338 /* S2 = y2*z1 */
339 mul_dual(R, *z1, *z1, S2, *y2, *z1);
340
341 lnorm_dual(R, R, S2, S2);
342
343 /* U2 = x2*z1^2 (B) */
344 /* S2 = y2*z1^3 (D) */
345 mul_dual(U2, *x2, R, S2, S2, R);
346
347 sub(H, U2, *x1); /* H = B - A (E) */
348 add(H, H, M8);
349 sub(R, S2, *y1); /* R = D - C (F) */
350 add(R, R, M4);
351
352 norm_dual(H, H, R, R);
353
354 /* z3 = z1*E */
355 mul(z3, H, *z1);
356
357 /* U2 = E^2 */
358 /* S2 = F^2 */
359 sqr_dual(U2, H, S2, R);
360
361 lnorm(U2, U2);
362
363 mul(H, H, U2); /* H = E^3 */
364
365 lnorm(H, H);
366
367 /* U2 = A*E^2 */
368 /* y3 = C*E^3 */
369 mul_dual(U2, U2, *x1, y3, H, *y1);
370
371 add(x3, U2, U2); /* x2 = 2*A*E^2 */
372 sub(x3, S2, x3); /* x3 = F^2 - 2*A*E^2 */
373 add(x3, x3, M4);
374 sub(x3, x3, H); /* x3 = F^2 - 2*A*E^2 - E^3 */
375 add(x3, x3, M2);
376
377 sub(U2, U2, x3); /* U2 = A*E^2 - x3 */
378 add(U2, U2, M8);
379 norm(U2, U2);
380 mul(U2, U2, R); /* U2 = F*(A*E^2 - x3) */
381 sub(y3, U2, y3); /* y3 = F*(A*E^2 - x3) - C*E^2 */
382 add(y3, y3, M2);
383
384 /* normalization */
385 norm_dual(x3, x3, y3, y3);
386 lnorm(z3, z3);
387
388 fe521 ONE;
389 FE521_LOADU(ONE, P521R1_R52);
390 /* T = p_is_inf ? q : T */
391 FE521_MASK_MOV(x3, x3, p_is_inf, *x2);
392 FE521_MASK_MOV(y3, y3, p_is_inf, *y2);
393 FE521_MASK_MOV(z3, z3, p_is_inf, ONE);
394
395 /* T = q_is_inf ? p : T */
396 FE521_MASK_MOV(x3, x3, q_is_inf, *x1);
397 FE521_MASK_MOV(y3, y3, q_is_inf, *y1);
398 FE521_MASK_MOV(z3, z3, q_is_inf, *z1);
399
400 FE521_COPY(r->x, x3);
401 FE521_COPY(r->y, y3);
402 FE521_COPY(r->z, z3);
403
404 return;
405 }
406
407 /* P521 mont(a) */
408 static const __ALIGN64 Ipp64u p512_a[P521R1_NUM_CHUNK][P521R1_LENFE521_52] = {
409 {0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFE, 0x000FFFFFFFFFFFFF, 0x000FFFFFFFFFFFFF},
410 {0x000FFFFFFFFFFFFF, 0x000FFFFFFFFFFFFF, 0x000FFFFFFFFFFFFF, 0x000FFFFFFFFFFFFF},
411 {0x000FFFFFFFFFFFFF, 0x000FFFFFFFFFFFFF, 0x0000000000000001, 0x0000000000000000}
412 };
413 /* P521R1 mont(b) */
414 static const __ALIGN64 Ipp64u p512_b[P521R1_NUM_CHUNK][P521R1_LENFE521_52] = {
415 {0x00014654FAE58638, 0x00028FEA35A81F80, 0x000C41E961A78F7A, 0x000DD8DF839AB9EF},
416 {0x00049BD8B29605E9, 0x0000AB0C9CA8F63F, 0x0005A44C8C77884F, 0x00092DCCD98AF9DC},
417 {0x0005B42A077516D3, 0x000E4D0FC94D10D0, 0x0000000000000000, 0x0000000000000000}
418 };
419
420 /* clang-format off */
421 IPP_OWN_DEFN(int, ifma_ec_nistp521_is_on_curve, (const P521_POINT_IFMA* p,
422 const int use_jproj_coords))
423 /* clang-format on */
424 {
425 /*
426 * Algorithm
427 *
428 * y^2 = x^3 + a*x + b (1)
429 *
430 * if input
431 * * Jacobian projection coordinate (x,y,z) - represent by (x/z^2,y/z^3,1)
432 * * Affine coordinate (x/z^2,y/z^3,z/z=1)
433 *
434 * mult formala (1) by z^6
435 *
436 * y^2 = x^3 + a*x*z^4 + b*z^6
437 */
438 fe521 M6;
439 FE521_LOADU(M6, p521_x6);
440 fe521 a, b;
441 FE521_LOADU(a, p512_a);
442 FE521_LOADU(b, p512_b);
443
444 fe521 rh, Z4, Z6, tmp;
445 FE521_SET(rh) = FE521_SET(Z4) = FE521_SET(Z6) = FE521_SET(tmp) = m256_setzero_i64();
446
447 sqr(rh, p->x); /* rh = x^2 */
448 /* rh = x*(x^2 + a*z^4) + b*z^6 = x*(x^2 - 3*z^4) + b*z^6 */
449 if (0 != use_jproj_coords) {
450 sqr(tmp, p->z); /* tmp = z^2 */
451 lnorm(tmp, tmp);
452
453 sqr(Z4, tmp); /* z4 = z^4 */
454 lnorm(Z4, Z4);
455 mul(Z6, Z4, tmp); /* z6 = z^6 */
456 lnorm(Z6, Z6);
457
458 add(tmp, Z4, Z4); /* tmp = 2*z^4 */
459 add(tmp, tmp, Z4); /* tmp = 3*z^4 */
460
461 sub(rh, rh, tmp); /* rh = x^2 - 3*z^4 */
462 add(rh, rh, M6);
463 norm(rh, rh);
464
465 /* rh = x*(x^2 - 3*z^4) */
466 /* tmp = b*z^6 */
467 mul_dual(rh, rh, p->x, tmp, Z6, b);
468
469 add(rh, rh, tmp); /* rh = x*(x^2 - 3*z^4) + b*z^6 */
470 }
471 /* rh = x*(x^2 + a) + b */
472 else {
473 add(rh, rh, a); /* rh = x^2 + a */
474 lnorm(rh, rh);
475 mul(rh, rh, p->x); /* rh = x*(x^2 + a) */
476 add(rh, rh, b); /* rh = x*(x^2 + a) + b */
477 }
478 lnorm(rh, rh);
479
480 /* rl = Y^2 */
481 sqr(tmp, p->y); /* tmp = y^2 */
482 lnorm(tmp, tmp); /**/
483
484 /* from mont */
485 from_mont(tmp, tmp);
486 from_mont(rh, rh);
487
488 const mask8 mask = FE521_CMP_MASK(rh, tmp, _MM_CMPINT_EQ);
489 return (mask == 0xF) ? 1 : 0;
490 }
491
492 #undef add
493 #undef sub
494 #undef mul
495 #undef sqr
496 #undef div2
497 #undef norm
498 #undef from_mont
499 #undef mul_dual
500 #undef sqr_dual
501 #undef norm_dual
502
503 static __NOINLINE void clear_secret_context(Ipp16u* wval,
504 Ipp32s* chunk_no,
505 Ipp32s* chunk_shift,
506 Ipp8u* sign,
507 Ipp8u* digit,
508 P521_POINT_IFMA* R,
509 P521_POINT_IFMA* H,
510 P521_POINT_AFFINE_IFMA* A)
511 {
512 *wval = 0;
513 *chunk_no = 0;
514 *chunk_shift = 0;
515 *sign = 0;
516 *digit = 0;
517
518 if (NULL != R)
519 FE521_SET((*R).x) = FE521_SET((*R).y) = FE521_SET((*R).z) = m256_setzero_i64();
520 if (NULL != H)
521 FE521_SET((*H).x) = FE521_SET((*H).y) = FE521_SET((*H).z) = m256_setzero_i64();
522 if (NULL != A)
523 FE521_SET((*A).x) = FE521_SET((*A).y) = m256_setzero_i64();
524 return;
525 }
526
527 #define WIN_SIZE (5)
528
529 IPPCP_INLINE mask8 is_eq_mask(const Ipp32s a, const Ipp32s b)
530 {
531 const Ipp32s eq = a ^ b;
532 const Ipp32s v = ~eq & (eq - 1);
533 const Ipp32s msb = 0 - (v >> (sizeof(a) * 8 - 1));
534 return (mask8)(0 - msb);
535 }
536
537 IPPCP_INLINE void extract_table_point(P521_POINT_IFMA* r,
538 const Ipp32s digit,
539 const P521_POINT_IFMA tbl[])
540 {
541 Ipp32s idx = digit - 1;
542
543 __ALIGN64 P521_POINT_IFMA R;
544 FE521_SET(R.x) = FE521_SET(R.y) = FE521_SET(R.z) = m256_setzero_i64();
545
546 for (Ipp32s n = 0; n < (1 << (WIN_SIZE - 1)); ++n) {
547 const mask8 mask = is_eq_mask(n, idx);
548
549 FE521_MASK_MOV(R.x, R.x, mask, tbl[n].x);
550 FE521_MASK_MOV(R.y, R.y, mask, tbl[n].y);
551 FE521_MASK_MOV(R.z, R.z, mask, tbl[n].z);
552 }
553
554 FE521_COPY(r->x, R.x);
555 FE521_COPY(r->y, R.y);
556 FE521_COPY(r->z, R.z);
557 }
558
559 #define dbl_point ifma_ec_nistp521_dbl_point
560 #define add_point ifma_ec_nistp521_add_point
561 #define neg_coord(R, A) ifma_neg52_p521(&(R), (A))
562 #define add_point_affine ifma_ec_nistp521_add_point_affine
563
564 /* r = n*P = (P + P + ... + P) */
565 /* clang-format off */
566 IPP_OWN_DEFN(void, ifma_ec_nistp521_mul_point, (P521_POINT_IFMA* r,
567 const P521_POINT_IFMA* p,
568 const Ipp8u* pExtendedScalar,
569 const int scalarBitSize))
570 /* clang-format on */
571 {
572 /* default params */
573 __ALIGN64 P521_POINT_IFMA tbl[(1 << (WIN_SIZE - 1))];
574
575 __ALIGN64 P521_POINT_IFMA R;
576 __ALIGN64 P521_POINT_IFMA H;
577
578 fe521 negHy;
579 FE521_SET(negHy) = m256_setzero_i64();
580 FE521_SET(R.x) = FE521_SET(R.y) = FE521_SET(R.z) = m256_setzero_i64();
581 FE521_SET(H.x) = FE521_SET(H.y) = FE521_SET(H.z) = m256_setzero_i64();
582
583 /* compute tbl[] = [n]P, n = 1, ... , 2^(win_size - 1)
584 * tbl[2*n] = tbl[2*n - 1] + p
585 * tbl[2*n + 1] = [2]*tbl[n]
586 */
587 /* tbl[0] = p */
588 FE521_COPY(tbl[0].x, p->x);
589 FE521_COPY(tbl[0].y, p->y);
590 FE521_COPY(tbl[0].z, p->z);
591 /* tbl[1] = [2]*p */
592 dbl_point(/* r = */ (tbl + 1), /* a = */ p);
593 for (int n = 1; n < ((1 << (WIN_SIZE - 1)) / 2); ++n) {
594 add_point(/* r = */ (tbl + 2 * n), /* a = */ (tbl + 2 * n - 1), /* b = */ p);
595 dbl_point(/* r = */ (tbl + 2 * n + 1), /* a = */ (tbl + n));
596 }
597
598 Ipp16u wval;
599 Ipp8u digit, sign;
600 const Ipp32s mask = ((1 << (WIN_SIZE + 1)) - 1); /* mask 0b111111 */
601 Ipp32s bit = scalarBitSize - (scalarBitSize % WIN_SIZE);
602
603 Ipp32s chunk_no = (bit - 1) / 8;
604 Ipp32s chunk_shift = (bit - 1) % 8;
605
606 if (0 != bit) {
607 wval = *((Ipp16u*)(pExtendedScalar + chunk_no));
608 wval = (Ipp16u)((wval >> chunk_shift) & mask);
609 } else {
610 wval = 0;
611 }
612
613 booth_recode(/* sign = */ &sign, /* digit = */ &digit, /* in = */ (Ipp8u)wval, WIN_SIZE);
614 extract_table_point(/* r = */ &R, /* digit = */ (Ipp32s)digit, /* tbl = */ tbl);
615
616 for (bit -= WIN_SIZE; bit >= WIN_SIZE; bit -= WIN_SIZE) {
617 dbl_point(&R, &R);
618 dbl_point(&R, &R);
619 dbl_point(&R, &R);
620 dbl_point(&R, &R);
621 #if (WIN_SIZE == 5)
622 dbl_point(&R, &R);
623 #endif
624 chunk_no = (bit - 1) / 8;
625 chunk_shift = (bit - 1) % 8;
626
627 wval = *((Ipp16u*)(pExtendedScalar + chunk_no));
628 wval = (Ipp16u)((wval >> chunk_shift) & mask);
629
630 booth_recode(/* sign = */ &sign, /* digit = */ &digit, /* in = */ (Ipp8u)wval, WIN_SIZE);
631 extract_table_point(/* r = */ &H, /* idx = */ (Ipp32s)digit, /* tbl = */ tbl);
632
633 neg_coord(negHy, H.y);
634
635 const mask8 mask_neg = (mask8)(~(sign - 1));
636 FE521_MASK_MOV(H.y, H.y, mask_neg, negHy);
637 add_point(/* r = */ &R, /* a = */ &R, /* b = */ &H);
638 }
639
640 /* last window */
641 dbl_point(&R, &R);
642 dbl_point(&R, &R);
643 dbl_point(&R, &R);
644 dbl_point(&R, &R);
645 #if (WIN_SIZE == 5)
646 dbl_point(&R, &R);
647 #endif
648
649 wval = *((Ipp16u*)(pExtendedScalar + 0));
650 wval = (wval << 1) & mask;
651
652 booth_recode(/* sign = */ &sign, /* digit = */ &digit, /* in = */ (Ipp8u)wval, WIN_SIZE);
653 extract_table_point(/* r = */ &H, /* idx = */ (Ipp32s)digit, /* tbl = */ tbl);
654
655 neg_coord(negHy, H.y);
656
657 const mask8 mask_neg = (mask8)(~(sign - 1));
658 FE521_MASK_MOV(H.y, H.y, mask_neg, negHy);
659
660 add_point(/* r = */ &R, /* a = */ &R, /* b = */ &H);
661
662 FE521_COPY(r->x, R.x);
663 FE521_COPY(r->y, R.y);
664 FE521_COPY(r->z, R.z);
665
666 /* clear secret data */
667 clear_secret_context(&wval, &chunk_no, &chunk_shift, &sign, &digit, &R, &H, NULL);
668
669 return;
670 }
671
672 #include "gfpec/ecnist/ifma_ecprecomp4_p521.h"
673
674 /* affine */
675 #define BP_WIN_SIZE BASE_POINT_WIN_SIZE
676 #define BP_N_ENTRY BASE_POINT_N_ENTRY
677
678 IPPCP_INLINE void extract_point_affine(P521_POINT_AFFINE_IFMA* r,
679 const P521_POINT_AFFINE_IFMA_MEM* tbl,
680 const Ipp32s digit)
681 {
682 Ipp32s idx = digit - 1;
683
684 fe521 x, y;
685 FE521_SET(x) = FE521_SET(y) = m256_setzero_i64();
686
687 for (Ipp32s n = 0; n < (1 << ((BP_WIN_SIZE)-1)); ++n, ++tbl) {
688 const mask8 mask = is_eq_mask(n, idx);
689
690 /* x */
691 FE521_LO(x) = m256_mask_mov_i64(FE521_LO(x), mask, m256_loadu_i64(FE521_LO(tbl->x)));
692 FE521_MID(x) = m256_mask_mov_i64(FE521_MID(x), mask, m256_loadu_i64(FE521_MID(tbl->x)));
693 FE521_HI(x) = m256_mask_mov_i64(FE521_HI(x), mask, m256_loadu_i64(FE521_HI(tbl->x)));
694 /* y */
695 FE521_LO(y) = m256_mask_mov_i64(FE521_LO(y), mask, m256_loadu_i64(FE521_LO(tbl->y)));
696 FE521_MID(y) = m256_mask_mov_i64(FE521_MID(y), mask, m256_loadu_i64(FE521_MID(tbl->y)));
697 FE521_HI(y) = m256_mask_mov_i64(FE521_HI(y), mask, m256_loadu_i64(FE521_HI(tbl->y)));
698 }
699
700 FE521_COPY(r->x, x);
701 FE521_COPY(r->y, y);
702 }
703
704 /* clang-format off */
705 IPP_OWN_DEFN(void, ifma_ec_nistp521_mul_pointbase, (P521_POINT_IFMA* r,
706 const Ipp8u* pExtendedScalar,
707 int scalarBitSize))
708 /* clang-format on */
709 {
710 /* precompute table */
711 const P521_POINT_AFFINE_IFMA_MEM* tbl = &ifma_ec_nistp521r1_bp_precomp[0][0];
712
713 __ALIGN64 P521_POINT_IFMA R;
714 __ALIGN64 P521_POINT_AFFINE_IFMA A;
715 fe521 Ty;
716 FE521_SET(R.x) = FE521_SET(R.y) = FE521_SET(R.z) = m256_setzero_i64();
717 FE521_SET(A.x) = FE521_SET(A.y) = m256_setzero_i64();
718 FE521_SET(Ty) = m256_setzero_i64();
719
720 Ipp16u wval;
721 Ipp8u digit, sign;
722 const Ipp32s mask = ((1 << (BP_WIN_SIZE + 1)) - 1); /* mask 0b11111 */
723 Ipp32s bit = 0;
724 Ipp32s chunk_no, chunk_shift;
725
726 wval = *((Ipp16u*)(pExtendedScalar + 0));
727 wval = (Ipp16u)((wval << 1) & mask);
728
729 booth_recode(&sign, &digit, (Ipp8u)wval, BP_WIN_SIZE);
730 extract_point_affine(&A, tbl, (Ipp32s)digit);
731 tbl += BP_N_ENTRY;
732
733 /* A = sign == 1 ? -A : A */
734 neg_coord(Ty, A.y);
735 mask8 mask_neg = (mask8)(~(sign - 1));
736 FE521_MASK_MOV(A.y, A.y, mask_neg, Ty);
737
738 /* R += A */
739 add_point_affine(&R, &R, &A);
740
741 for (bit += BP_WIN_SIZE; bit <= scalarBitSize; bit += BP_WIN_SIZE) {
742 chunk_no = (bit - 1) / 8;
743 chunk_shift = (bit - 1) % 8;
744
745 wval = *((Ipp16u*)(pExtendedScalar + chunk_no));
746 wval = (Ipp16u)((wval >> chunk_shift) & mask);
747
748 booth_recode(&sign, &digit, (Ipp8u)wval, BP_WIN_SIZE);
749 extract_point_affine(&A, tbl, (Ipp32s)digit);
750 tbl += BP_N_ENTRY;
751
752 /* A = sign == 1 ? -A : A */
753 neg_coord(Ty, A.y);
754 mask_neg = (mask8)(~(sign - 1));
755 FE521_MASK_MOV(A.y, A.y, mask_neg, Ty);
756
757 /* R += A */
758 add_point_affine(&R, &R, &A);
759 }
760
761 FE521_COPY(r->x, R.x);
762 FE521_COPY(r->y, R.y);
763 FE521_COPY(r->z, R.z);
764
765 /* clear secret data */
766 clear_secret_context(&wval, &chunk_no, &chunk_shift, &sign, &digit, &R, NULL, &A);
767 return;
768 }
769
770 #undef dbl_point
771 #undef add_point
772 #undef neg_coord
773 #undef add_point_affine
774
775 #endif // (_IPP32E >= _IPP32E_K1)