Debian Codegraph @

src/ed448.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2022, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 /*
33 * Twisted Edward curve with equation:
34 *
35 * ax² + y² = 1 + dx²y²
36 *
37 * where a = 1 and d = -39081 over the prime field modulo 2⁴⁴⁸ - 2²²⁴ - 1.
38 *
39 * Points (x, y) can be represented with projective coordinates
40 * (X, Y, Z) with x = X/Z and y = Y/Z for any non-zero Z.
41 * The point (x, y) can be obtained by normalizing to (x, y, 1).
42 *
43 * The PAI (or neutral point) is (0, 1) or (0, Z, Z) for any non-zero Z.
44 */
45
46 #include <assert.h>
47
48 #include "common.h"
49 #include "mont.h"
50 #include "ed448.h"
51
52 FAKE_INIT(ed448)
53
54 STATIC WorkplaceEd448 *new_workplace(const MontContext *ctx)
55 {
56 WorkplaceEd448 *wp;
57 int res;
58
59 wp = calloc(1, sizeof(WorkplaceEd448));
60 if (NULL == wp)
61 return NULL;
62
63 res = mont_number(&wp->a, 1, ctx);
64 if (res) goto cleanup;
65 res = mont_number(&wp->b, 1, ctx);
66 if (res) goto cleanup;
67 res = mont_number(&wp->c, 1, ctx);
68 if (res) goto cleanup;
69 res = mont_number(&wp->d, 1, ctx);
70 if (res) goto cleanup;
71 res = mont_number(&wp->e, 1, ctx);
72 if (res) goto cleanup;
73 res = mont_number(&wp->f, 1, ctx);
74 if (res) goto cleanup;
75 res = mont_number(&wp->scratch, SCRATCHPAD_NR, ctx);
76 if (res) goto cleanup;
77 return wp;
78
79 cleanup:
80 free(wp->a);
81 free(wp->b);
82 free(wp->c);
83 free(wp->d);
84 free(wp->e);
85 free(wp->f);
86 free(wp->scratch);
87 return NULL;
88 }
89
90 STATIC void free_workplace(WorkplaceEd448 *wp)
91 {
92 if (NULL == wp)
93 return;
94 free(wp->a);
95 free(wp->b);
96 free(wp->c);
97 free(wp->d);
98 free(wp->e);
99 free(wp->f);
100 free(wp->scratch);
101 free(wp);
102 }
103
104 /*
105 * Convert projective coordinates of an Ed448 point to affine
106 */
107 STATIC void ed448_projective_to_affine(uint64_t *x3, uint64_t *y3,
108 const uint64_t *x1, uint64_t *y1, uint64_t *z1,
109 WorkplaceEd448 *tmp,
110 const MontContext *ctx)
111 {
112 uint64_t *a = tmp->a;
113 uint64_t *s = tmp->scratch;
114
115 mont_inv_prime(a, z1, ctx);
116 mont_mult(x3, x1, a, s, ctx); /* X/Z */
117 mont_mult(y3, y1, a, s, ctx); /* Y/Z */
118 }
119
120 /*
121 * Double an EC point on the Ed448 curve
122 *
123 * Input and output points can match.
124 */
125 STATIC void ed448_double_internal(PointEd448 *Pout, const PointEd448 *Pin,
126 WorkplaceEd448 *tmp, const MontContext *ctx)
127 {
128 const uint64_t *x1 = Pin->x;
129 const uint64_t *y1 = Pin->y;
130 const uint64_t *z1 = Pin->z;
131 uint64_t *x3 = Pout->x;
132 uint64_t *y3 = Pout->y;
133 uint64_t *z3 = Pout->z;
134 uint64_t *t0 = tmp->a;
135 uint64_t *t1 = tmp->b;
136 uint64_t *t2 = tmp->c;
137 uint64_t *t3 = tmp->d;
138 uint64_t *t4 = tmp->e;
139 uint64_t *t5 = tmp->f;
140 uint64_t *s = tmp->scratch;
141
142 /* https://datatracker.ietf.org/doc/html/rfc8032#section-5.2.4 */
143
144 mont_add(t0, x1, y1, s, ctx);
145 mont_mult(t0, t0, t0, s, ctx); /* B = (X1+Y1)^2 */
146 mont_mult(t1, x1, x1, s, ctx); /* C = X1^2 */
147 mont_mult(t2, y1, y1, s, ctx); /* D = Y1^2 */
148 mont_add(t3, t1, t2, s, ctx); /* E = C+D */
149 mont_mult(t4, z1, z1, s, ctx); /* H = Z1^2 */
150 mont_sub(t5, t3, t4, s, ctx);
151 mont_sub(t5, t5, t4, s, ctx); /* J = E-2*H */
152 mont_sub(x3, t0, t3, s, ctx);
153 mont_mult(x3, x3, t5, s, ctx); /* X3 = (B-E)*J */
154 mont_sub(y3, t1, t2, s, ctx);
155 mont_mult(y3, y3, t3, s, ctx); /* Y3 = E*(C-D) */
156 mont_mult(z3, t3, t5, s, ctx); /* Z3 = E*J */
157 }
158
159 /*
160 * Add two EC points on the Ed448 curve
161 */
162 STATIC void ed448_add_internal(PointEd448 *Pout,
163 const PointEd448 *Pin1,
164 const PointEd448 *Pin2,
165 const uint64_t *d,
166 WorkplaceEd448 *tmp,
167 const MontContext *ctx)
168 {
169 const uint64_t *x1 = Pin1->x;
170 const uint64_t *y1 = Pin1->y;
171 const uint64_t *z1 = Pin1->z;
172 const uint64_t *x2 = Pin2->x;
173 const uint64_t *y2 = Pin2->y;
174 const uint64_t *z2 = Pin2->z;
175 uint64_t *x3 = Pout->x;
176 uint64_t *y3 = Pout->y;
177 uint64_t *z3 = Pout->z;
178 uint64_t *t0 = tmp->a;
179 uint64_t *t1 = tmp->b;
180 uint64_t *t2 = tmp->c;
181 uint64_t *t3 = tmp->d;
182 uint64_t *t4 = tmp->e;
183 uint64_t *t5 = tmp->f;
184 uint64_t *s = tmp->scratch;
185
186 /* https://datatracker.ietf.org/doc/html/rfc8032#section-5.2.4 */
187
188 mont_mult(t0, z1, z2, s, ctx); /* A = Z1*Z2 */
189 mont_mult(t1, t0, t0, s, ctx); /* B = A^2 */
190 mont_mult(t2, x1, x2, s, ctx); /* C = X1*X2 */
191 mont_mult(t3, y1, y2, s, ctx); /* D = Y1*Y2 */
192 mont_add(t4, x1, y1, s, ctx);
193 mont_add(t5, x2, y2, s, ctx);
194 mont_mult(t4, t4, t5, s, ctx); /* H = (X1+Y1)*(X2+Y2) */
195 mont_mult(t5, t2, t3, s, ctx);
196 mont_mult(t5, t5, d, s, ctx); /* E = d*C*D */
197 mont_sub(x3, t4, t2, s, ctx);
198 mont_sub(x3, x3, t3, s, ctx);
199 mont_sub(t4, t1, t5, s, ctx); /* F = B-E */
200 mont_mult(x3, x3, t4, s, ctx);
201 mont_mult(x3, x3, t0, s, ctx); /* X3 = A*F*(H-C-D) */
202 mont_add(t5, t1, t5, s, ctx); /* G = B+E */
203 mont_sub(y3, t3, t2, s, ctx);
204 mont_mult(y3, y3, t5, s, ctx);
205 mont_mult(y3, y3, t0, s, ctx); /* Y3 = A*G*(D-C) */
206 mont_mult(z3, t4, t5, s, ctx); /* Z3 = F*G */
207 }
208
209 STATIC void cswap(PointEd448 *a, PointEd448 *b, unsigned swap)
210 {
211 uint64_t mask, e, f, g;
212 unsigned int i;
213
214 mask = (uint64_t)(0 - (swap!=0)); /* 0 if swap is 0, all 1s if swap is !=0 */
215
216 for (i=0; i<7; i++) {
217 e = mask & (a->x[i] ^ b->x[i]);
218 a->x[i] ^= e;
219 b->x[i] ^= e;
220 f = mask & (a->y[i] ^ b->y[i]);
221 a->y[i] ^= f;
222 b->y[i] ^= f;
223 g = mask & (a->z[i] ^ b->z[i]);
224 a->z[i] ^= g;
225 b->z[i] ^= g;
226 }
227 }
228
229 /*
230 * Scalar multiplication Q = k*B on the Ed448 curve
231 */
232 STATIC void ed448_scalar_internal(PointEd448 *Pout,
233 const uint8_t *k,
234 size_t len,
235 const PointEd448 *Pin)
236 {
237 PointEd448 *R0=NULL;
238 PointEd448 *R1=NULL;
239 unsigned bit_idx, swap;
240 size_t scan;
241
242 ed448_new_point(&R0, (uint8_t*)"\x00", (uint8_t*)"\x01", 1, Pin->ec_ctx);
243 ed448_clone(&R1, Pin);
244
245 /* https://eprint.iacr.org/2020/956.pdf */
246
247 /* OPTIMIZE: with pre-computed tables in case of fixed-point base multiplication */
248
249 /* Scan all bits from MSB to LSB */
250 bit_idx = 7;
251 swap = 0;
252 scan = 0;
253 while (scan<len) {
254 unsigned bit;
255
256 bit = (k[scan] >> bit_idx) & 1;
257 swap ^= bit;
258
259 cswap(R0, R1, swap);
260
261 /* R1 <-- R0 + R1 */
262 ed448_add_internal(R1, R0, R1,
263 Pin->ec_ctx->d,
264 Pin->wp,
265 Pin->ec_ctx->mont_ctx);
266 /* R0 <-- 2R0 */
267 ed448_double_internal(R0, R0,
268 Pin->wp,
269 Pin->ec_ctx->mont_ctx);
270
271 swap = bit;
272 if (bit_idx-- == 0) {
273 bit_idx = 7;
274 scan++;
275 }
276 }
277 cswap(R0, R1, swap);
278
279 ed448_copy(Pout, R0);
280
281 ed448_free_point(R0);
282 ed448_free_point(R1);
283 }
284
285
286 /* ------------------------------------- */
287
288 /*
289 * Create an Elliptic Curve context for Ed448
290 *
291 * @param pec_ctx The memory area where the pointer to the newly allocated
292 * EC context will be stored.
293 * @return 0 for success, the appropriate error code otherwise
294 */
295 EXPORT_SYM int ed448_new_context(EcContext **pec_ctx)
296 {
297 EcContext *ec_ctx = NULL;
298 int res;
299 MontContext *ctx;
300 const uint8_t mod448_be[56] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
301 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
302 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
303 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
304 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
305 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
306 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
307 const uint8_t d448_be[56] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
308 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
309 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
310 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
311 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
312 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
313 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x67, 0x56};
314
315 if (NULL == pec_ctx)
316 return ERR_NULL;
317
318 *pec_ctx = ec_ctx = (EcContext*)calloc(1, sizeof(EcContext));
319 if (NULL == ec_ctx)
320 return ERR_MEMORY;
321
322 res = mont_context_init(&ec_ctx->mont_ctx, mod448_be, sizeof(mod448_be));
323 if (res) goto cleanup;
324 ctx = ec_ctx->mont_ctx;
325
326 res = mont_from_bytes(&ec_ctx->d, d448_be, sizeof(d448_be), ctx);
327 if (res) goto cleanup;
328
329 return 0;
330
331 cleanup:
332 free(ec_ctx->d);
333 mont_context_free(ec_ctx->mont_ctx);
334 free(ec_ctx);
335 return res;
336 }
337
338 EXPORT_SYM void ed448_free_context(EcContext *ec_ctx)
339 {
340 if (NULL == ec_ctx)
341 return;
342 free(ec_ctx->d);
343 mont_context_free(ec_ctx->mont_ctx);
344 free(ec_ctx);
345 }
346
347 /*
348 * Create a new EC point on the Ed448 curve.
349 *
350 * @param pecp The memory area where the pointer to the newly allocated EC
351 * point will be stored.
352 * Use ed448_free_point() for deallocating it.
353 * @param x The X-coordinate (affine, big-endian, smaller than modulus)
354 * @param y The Y-coordinate (affine, big-endian, smaller than modulus)
355 * @param len The length of x and y in bytes (max 56 bytes)
356 * @param ec_ctx The EC context
357 * @return 0 for success, the appopriate error code otherwise
358 */
359 EXPORT_SYM int ed448_new_point(PointEd448 **pecp,
360 const uint8_t *x,
361 const uint8_t *y,
362 size_t len,
363 const EcContext *ec_ctx)
364 {
365 int res;
366 WorkplaceEd448 *wp = NULL;
367 PointEd448 *ecp = NULL;
368 MontContext *ctx = NULL;
369 uint64_t *scratch = NULL;
370
371 if (NULL == pecp || NULL == x || NULL == y || NULL == ec_ctx)
372 return ERR_NULL;
373 ctx = ec_ctx->mont_ctx;
374
375 if (len == 0)
376 return ERR_NOT_ENOUGH_DATA;
377
378 if (len > ctx->bytes)
379 return ERR_VALUE;
380
381 *pecp = ecp = (PointEd448*)calloc(1, sizeof(PointEd448));
382 if (NULL == ecp)
383 return ERR_MEMORY;
384
385 ecp->ec_ctx = ec_ctx;
386
387 /** No need to treat PAI (x=0, y=1) in a special way **/
388 res = mont_from_bytes(&ecp->x, x, len, ctx);
389 if (res) goto cleanup;
390 res = mont_from_bytes(&ecp->y, y, len, ctx);
391 if (res) goto cleanup;
392 res = mont_number(&ecp->z, 1, ctx);
393 if (res) goto cleanup;
394 mont_set(ecp->z, 1, ctx);
395
396 ecp->wp = new_workplace(ctx);
397 if (NULL == ecp->wp) goto cleanup;
398
399 wp = ecp->wp;
400 scratch = ecp->wp->scratch;
401
402 /* Verify that the point is on the curve **/
403 /* x² + y² = 1 + dx²y² */
404 mont_mult(wp->a, ecp->y, ecp->y, scratch, ctx); /* y² */
405 mont_mult(wp->b, ecp->x, ecp->x, scratch, ctx); /* x² */
406 mont_mult(wp->c, wp->a, wp->b, scratch, ctx); /* x²y² */
407 mont_mult(wp->c, ec_ctx->d, wp->c, scratch, ctx); /* dx²y² */
408 mont_add(wp->c, ecp->z, wp->c, scratch, ctx); /* 1 + dx²y² */
409 mont_add(wp->a, wp->a, wp->b, scratch, ctx); /* x² + y² */
410 res = !mont_is_equal(wp->a, wp->c, ctx);
411 if (res) {
412 res = ERR_EC_POINT;
413 goto cleanup;
414 }
415 return 0;
416
417 cleanup:
418 ed448_free_point(ecp);
419 *pecp = NULL;
420 return res;
421 }
422
423 EXPORT_SYM void ed448_free_point(PointEd448 *ecp)
424 {
425 if (NULL == ecp)
426 return;
427
428 /* The EC context (ecp->ecp_ctx) is allocated once and shared by all
429 * points on the same surve, so we will not free it here.
430 */
431
432 free_workplace(ecp->wp);
433 free(ecp->x);
434 free(ecp->y);
435 free(ecp->z);
436 free(ecp);
437 }
438
439 /*
440 * Encode the affine coordinates of an EC point.
441 *
442 * @param x The location where the affine X-coordinate will be store in big-endian mode
443 * @param y The location where the affine Y-coordinate will be store in big-endian mode
444 * @param len The memory available for x and y in bytes.
445 * It must be able to contain the prime modulus of the curve field.
446 * @param ecp The EC point to encode.
447 */
448 EXPORT_SYM int ed448_get_xy(uint8_t *x, uint8_t *y, size_t len, const PointEd448 *ecp)
449 {
450 uint64_t *xw=NULL, *yw=NULL;
451 MontContext *ctx;
452 int res;
453
454 if (NULL == x || NULL == y || NULL == ecp)
455 return ERR_NULL;
456 ctx = ecp->ec_ctx->mont_ctx;
457
458 if (len < ctx->modulus_len)
459 return ERR_NOT_ENOUGH_DATA;
460
461 res = mont_number(&xw, 1, ctx);
462 if (res) goto cleanup;
463 res = mont_number(&yw, 1, ctx);
464 if (res) goto cleanup;
465
466 ed448_projective_to_affine(xw, yw, ecp->x, ecp->y, ecp->z, ecp->wp, ctx);
467
468 res = mont_to_bytes(x, len, xw, ctx);
469 if (res) goto cleanup;
470 res = mont_to_bytes(y, len, yw, ctx);
471 if (res) goto cleanup;
472
473 res = 0;
474
475 cleanup:
476 free(xw);
477 free(yw);
478 return res;
479 }
480
481 /*
482 * Double an EC point
483 */
484 EXPORT_SYM int ed448_double(PointEd448 *P)
485 {
486 MontContext *ctx;
487
488 if (NULL == P)
489 return ERR_NULL;
490 ctx = P->ec_ctx->mont_ctx;
491
492 ed448_double_internal(P, P, P->wp, ctx);
493 return 0;
494 }
495
496 /*
497 * Add an EC point to another
498 */
499 EXPORT_SYM int ed448_add(PointEd448 *ecpa, const PointEd448 *ecpb)
500 {
501 MontContext *ctx;
502
503 if (NULL == ecpa || NULL == ecpb)
504 return ERR_NULL;
505 if (ecpa->ec_ctx != ecpb->ec_ctx)
506 return ERR_EC_CURVE;
507 ctx = ecpa->ec_ctx->mont_ctx;
508
509 ed448_add_internal(ecpa, ecpa, ecpb,
510 ecpb->ec_ctx->d,
511 ecpb->wp, ctx);
512
513 return 0;
514 }
515
516 /*
517 * Multiply an EC point by a scalar
518 *
519 * @param ecp The EC point to multiply
520 * @param k The scalar, encoded in big endian mode
521 * @param len The length of the scalar, in bytes
522 * @param seed The 64-bit to drive the randomizations against SCAs
523 * @return 0 in case of success, the appropriate error code otherwise
524 */
525 EXPORT_SYM int ed448_scalar(PointEd448 *P, const uint8_t *scalar, size_t scalar_len, uint64_t _)
526 {
527 if (NULL == P || NULL == scalar)
528 return ERR_NULL;
529
530 ed448_scalar_internal(P, scalar, scalar_len, P);
531 return 0;
532 }
533
534 EXPORT_SYM int ed448_clone(PointEd448 **pecp2, const PointEd448 *ecp)
535 {
536 int res = -1;
537 PointEd448 *ecp2;
538 MontContext *ctx;
539
540 if (NULL == pecp2 || NULL == ecp)
541 return ERR_NULL;
542 ctx = ecp->ec_ctx->mont_ctx;
543
544 *pecp2 = ecp2 = (PointEd448*)calloc(1, sizeof(PointEd448));
545 if (NULL == ecp2)
546 return ERR_MEMORY;
547
548 ecp2->ec_ctx = ecp->ec_ctx;
549
550 ecp2->wp = new_workplace(ctx);
551 if (NULL == ecp2->wp) goto cleanup;
552
553 res = mont_number(&ecp2->x, 1, ctx);
554 if (res) goto cleanup;
555 mont_copy(ecp2->x, ecp->x, ctx);
556
557 res = mont_number(&ecp2->y, 1, ctx);
558 if (res) goto cleanup;
559 mont_copy(ecp2->y, ecp->y, ctx);
560
561 res = mont_number(&ecp2->z, 1, ctx);
562 if (res) goto cleanup;
563 mont_copy(ecp2->z, ecp->z, ctx);
564
565 return 0;
566
567 cleanup:
568 free_workplace(ecp2->wp);
569 free(ecp2->x);
570 free(ecp2->y);
571 free(ecp2->z);
572 free(ecp2);
573 *pecp2 = NULL;
574 return res;
575 }
576
577 /*
578 * Compare two EC points and return 0 if they match
579 */
580 EXPORT_SYM int ed448_cmp(const PointEd448 *ecp1, const PointEd448 *ecp2)
581 {
582 MontContext *ctx;
583 WorkplaceEd448 *wp;
584 uint64_t *scratch;
585 int result;
586
587 if (NULL == ecp1 || NULL == ecp2)
588 return ERR_NULL;
589
590 if (ecp1->ec_ctx != ecp2->ec_ctx)
591 return ERR_EC_CURVE;
592
593 /** Normalize to have the same Z coordinate */
594 ctx = ecp1->ec_ctx->mont_ctx;
595 wp = ecp1->wp;
596 scratch = wp->scratch;
597 mont_mult(wp->b, ecp1->x, ecp2->z, scratch, ctx); /* B = X1*Z2 */
598 mont_mult(wp->d, ecp2->x, ecp1->z, scratch, ctx); /* D = X2*Z1 */
599 mont_mult(wp->e, ecp1->y, ecp2->z, scratch, ctx); /* E = Y1*Z2 */
600 mont_mult(wp->f, ecp2->y, ecp1->z, scratch, ctx); /* F = Y2*Z1 */
601 result = (mont_is_equal(wp->b, wp->d, ctx) && mont_is_equal(wp->e, wp->f, ctx)) ? 0 : ERR_VALUE;
602
603 return result;
604 }
605
606 EXPORT_SYM int ed448_neg(PointEd448 *P)
607 {
608 MontContext *ctx;
609
610 if (NULL == P)
611 return ERR_NULL;
612
613 ctx = P->ec_ctx->mont_ctx;
614 mont_sub(P->x, ctx->modulus, P->x, P->wp->scratch, ctx);
615
616 return 0;
617 }
618
619 EXPORT_SYM int ed448_copy(PointEd448 *ecp1, const PointEd448 *ecp2)
620 {
621 const MontContext *ctx;
622
623 if (NULL == ecp1 || NULL == ecp2)
624 return ERR_NULL;
625
626 /*
627 * The EC context is shared by all EC point
628 * on the same curve, so we do not free
629 * nor duplicate it here.
630 */
631 ecp1->ec_ctx = ecp2->ec_ctx;
632
633 ctx = ecp2->ec_ctx->mont_ctx;
634 mont_copy(ecp1->x, ecp2->x, ctx);
635 mont_copy(ecp1->y, ecp2->y, ctx);
636 mont_copy(ecp1->z, ecp2->z, ctx);
637
638 return 0;
639 }