| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
²y² |
| 36 |
|
| 37 |
²⁴ - 1. |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 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); |
| 117 |
mont_mult(y3, y1, a, s, ctx); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 143 |
|
| 144 |
mont_add(t0, x1, y1, s, ctx); |
| 145 |
mont_mult(t0, t0, t0, s, ctx); |
| 146 |
mont_mult(t1, x1, x1, s, ctx); |
| 147 |
mont_mult(t2, y1, y1, s, ctx); |
| 148 |
mont_add(t3, t1, t2, s, ctx); |
| 149 |
mont_mult(t4, z1, z1, s, ctx); |
| 150 |
mont_sub(t5, t3, t4, s, ctx); |
| 151 |
mont_sub(t5, t5, t4, s, ctx); |
| 152 |
mont_sub(x3, t0, t3, s, ctx); |
| 153 |
mont_mult(x3, x3, t5, s, ctx); |
| 154 |
mont_sub(y3, t1, t2, s, ctx); |
| 155 |
mont_mult(y3, y3, t3, s, ctx); |
| 156 |
mont_mult(z3, t3, t5, s, ctx); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 187 |
|
| 188 |
mont_mult(t0, z1, z2, s, ctx); |
| 189 |
mont_mult(t1, t0, t0, s, ctx); |
| 190 |
mont_mult(t2, x1, x2, s, ctx); |
| 191 |
mont_mult(t3, y1, y2, s, ctx); |
| 192 |
mont_add(t4, x1, y1, s, ctx); |
| 193 |
mont_add(t5, x2, y2, s, ctx); |
| 194 |
mont_mult(t4, t4, t5, s, ctx); |
| 195 |
mont_mult(t5, t2, t3, s, ctx); |
| 196 |
mont_mult(t5, t5, d, s, ctx); |
| 197 |
mont_sub(x3, t4, t2, s, ctx); |
| 198 |
mont_sub(x3, x3, t3, s, ctx); |
| 199 |
mont_sub(t4, t1, t5, s, ctx); |
| 200 |
mont_mult(x3, x3, t4, s, ctx); |
| 201 |
mont_mult(x3, x3, t0, s, ctx); |
| 202 |
mont_add(t5, t1, t5, s, ctx); |
| 203 |
mont_sub(y3, t3, t2, s, ctx); |
| 204 |
mont_mult(y3, y3, t5, s, ctx); |
| 205 |
mont_mult(y3, y3, t0, s, ctx); |
| 206 |
mont_mult(z3, t4, t5, s, ctx); |
| 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)); |
| 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 |
|
| 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 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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 |
|
| 262 |
ed448_add_internal(R1, R0, R1, |
| 263 |
Pin->ec_ctx->d, |
| 264 |
Pin->wp, |
| 265 |
Pin->ec_ctx->mont_ctx); |
| 266 |
|
| 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 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 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 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 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 |
|
| 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 |
|
| 403 |
² */ |
| 404 |
mont_mult(wp->a, ecp->y, ecp->y, scratch, ctx); / |
| 405 |
mont_mult(wp->b, ecp->x, ecp->x, scratch, ctx); / |
| 406 |
mont_mult(wp->c, wp->a, wp->b, scratch, ctx); */ |
| 407 |
mont_mult(wp->c, ec_ctx->d, wp->c, scratch, ctx); */ |
| 408 |
mont_add(wp->c, ecp->z, wp->c, scratch, ctx); */ |
| 409 |
mont_add(wp->a, wp->a, wp->b, scratch, ctx); */ |
| 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 |
|
| 429 |
|
| 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 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 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 |
|
| 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 |
|
| 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); |
| 598 |
mont_mult(wp->d, ecp2->x, ecp1->z, scratch, ctx); |
| 599 |
mont_mult(wp->e, ecp1->y, ecp2->z, scratch, ctx); |
| 600 |
mont_mult(wp->f, ecp2->y, ecp1->z, scratch, ctx); |
| 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 |
|
| 628 |
|
| 629 |
|
| 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 |
} |