| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa.math; |
| 13 |
|
| 14 |
import net.i2p.crypto.eddsa.Utils; |
| 15 |
|
| 16 |
import java.io.Serializable; |
| 17 |
import java.util.Arrays; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class GroupElement implements Serializable { |
| 35 |
private static final long serialVersionUID = 2395879087349587L; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public enum Representation { |
| 48 |
|
| 49 |
P2, |
| 50 |
|
| 51 |
P3, |
| 52 |
|
| 53 |
P1P1, |
| 54 |
|
| 55 |
PRECOMP, |
| 56 |
|
| 57 |
CACHED |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static GroupElement p2( |
| 70 |
final Curve curve, |
| 71 |
final FieldElement X, |
| 72 |
final FieldElement Y, |
| 73 |
final FieldElement Z) { |
| 74 |
return new GroupElement(curve, Representation.P2, X, Y, Z, null); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public static GroupElement p3( |
| 88 |
final Curve curve, |
| 89 |
final FieldElement X, |
| 90 |
final FieldElement Y, |
| 91 |
final FieldElement Z, |
| 92 |
final FieldElement T) { |
| 93 |
return new GroupElement(curve, Representation.P3, X, Y, Z, T); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static GroupElement p1p1( |
| 107 |
final Curve curve, |
| 108 |
final FieldElement X, |
| 109 |
final FieldElement Y, |
| 110 |
final FieldElement Z, |
| 111 |
final FieldElement T) { |
| 112 |
return new GroupElement(curve, Representation.P1P1, X, Y, Z, T); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static GroupElement precomp( |
| 125 |
final Curve curve, |
| 126 |
final FieldElement ypx, |
| 127 |
final FieldElement ymx, |
| 128 |
final FieldElement xy2d) { |
| 129 |
return new GroupElement(curve, Representation.PRECOMP, ypx, ymx, xy2d, null); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static GroupElement cached( |
| 143 |
final Curve curve, |
| 144 |
final FieldElement YpX, |
| 145 |
final FieldElement YmX, |
| 146 |
final FieldElement Z, |
| 147 |
final FieldElement T2d) { |
| 148 |
return new GroupElement(curve, Representation.CACHED, YpX, YmX, Z, T2d); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
final Curve curve; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
final Representation repr; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
final FieldElement X; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
final FieldElement Y; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
final FieldElement Z; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
final FieldElement T; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
GroupElement[][] precmp; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
GroupElement[] dblPrecmp; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public GroupElement( |
| 208 |
final Curve curve, |
| 209 |
final Representation repr, |
| 210 |
final FieldElement X, |
| 211 |
final FieldElement Y, |
| 212 |
final FieldElement Z, |
| 213 |
final FieldElement T) { |
| 214 |
this.curve = curve; |
| 215 |
this.repr = repr; |
| 216 |
this.X = X; |
| 217 |
this.Y = Y; |
| 218 |
this.Z = Z; |
| 219 |
this.T = T; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
$. |
| 230 |
$. |
| 231 |
. |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public GroupElement(final Curve curve, final byte[] s) { |
| 239 |
FieldElement x, y, yy, u, v, v3, vxx, check; |
| 240 |
y = curve.getField().fromByteArray(s); |
| 241 |
yy = y.square(); |
| 242 |
|
| 243 |
|
| 244 |
u = yy.subtractOne(); |
| 245 |
|
| 246 |
|
| 247 |
v = yy.multiply(curve.getD()).addOne(); |
| 248 |
|
| 249 |
|
| 250 |
v3 = v.square().multiply(v); |
| 251 |
|
| 252 |
|
| 253 |
x = v3.square().multiply(v).multiply(u); |
| 254 |
|
| 255 |
|
| 256 |
x = x.pow22523(); |
| 257 |
|
| 258 |
|
| 259 |
x = v3.multiply(u).multiply(x); |
| 260 |
|
| 261 |
vxx = x.square().multiply(v); |
| 262 |
check = vxx.subtract(u); |
| 263 |
if (check.isNonZero()) { |
| 264 |
check = vxx.add(u); |
| 265 |
|
| 266 |
if (check.isNonZero()) |
| 267 |
throw new IllegalArgumentException("not a valid GroupElement"); |
| 268 |
x = x.multiply(curve.getI()); |
| 269 |
} |
| 270 |
|
| 271 |
if ((x.isNegative() ? 1 : 0) != Utils.bit(s, curve.getField().getb()-1)) { |
| 272 |
x = x.negate(); |
| 273 |
} |
| 274 |
|
| 275 |
this.curve = curve; |
| 276 |
this.repr = Representation.P3; |
| 277 |
this.X = x; |
| 278 |
this.Y = y; |
| 279 |
this.Z = curve.getField().ONE; |
| 280 |
this.T = this.X.multiply(this.Y); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public Curve getCurve() { |
| 289 |
return this.curve; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public Representation getRepresentation() { |
| 298 |
return this.repr; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public FieldElement getX() { |
| 308 |
return this.X; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public FieldElement getY() { |
| 318 |
return this.Y; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public FieldElement getZ() { |
| 328 |
return this.Z; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public FieldElement getT() { |
| 338 |
return this.T; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public byte[] toByteArray() { |
| 347 |
switch (this.repr) { |
| 348 |
case P2: |
| 349 |
case P3: |
| 350 |
FieldElement recip = Z.invert(); |
| 351 |
FieldElement x = X.multiply(recip); |
| 352 |
FieldElement y = Y.multiply(recip); |
| 353 |
byte[] s = y.toByteArray(); |
| 354 |
s[s.length-1] |= (x.isNegative() ? (byte) 0x80 : 0); |
| 355 |
return s; |
| 356 |
default: |
| 357 |
return toP2().toByteArray(); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public GroupElement toP2() { |
| 367 |
return toRep(Representation.P2); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public GroupElement toP3() { |
| 376 |
return toRep(Representation.P3); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public GroupElement toCached() { |
| 385 |
return toRep(Representation.CACHED); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
private GroupElement toRep(final Representation repr) { |
| 404 |
switch (this.repr) { |
| 405 |
case P2: |
| 406 |
switch (repr) { |
| 407 |
case P2: |
| 408 |
return p2(this.curve, this.X, this.Y, this.Z); |
| 409 |
default: |
| 410 |
throw new IllegalArgumentException(); |
| 411 |
} |
| 412 |
case P3: |
| 413 |
switch (repr) { |
| 414 |
case P2: |
| 415 |
return p2(this.curve, this.X, this.Y, this.Z); |
| 416 |
case P3: |
| 417 |
return p3(this.curve, this.X, this.Y, this.Z, this.T); |
| 418 |
case CACHED: |
| 419 |
return cached(this.curve, this.Y.add(this.X), this.Y.subtract(this.X), this.Z, this.T.multiply(this.curve.get2D())); |
| 420 |
default: |
| 421 |
throw new IllegalArgumentException(); |
| 422 |
} |
| 423 |
case P1P1: |
| 424 |
switch (repr) { |
| 425 |
case P2: |
| 426 |
return p2(this.curve, this.X.multiply(this.T), Y.multiply(this.Z), this.Z.multiply(this.T)); |
| 427 |
case P3: |
| 428 |
return p3(this.curve, this.X.multiply(this.T), Y.multiply(this.Z), this.Z.multiply(this.T), this.X.multiply(this.Y)); |
| 429 |
case P1P1: |
| 430 |
return p1p1(this.curve, this.X, this.Y, this.Z, this.T); |
| 431 |
default: |
| 432 |
throw new IllegalArgumentException(); |
| 433 |
} |
| 434 |
case PRECOMP: |
| 435 |
switch (repr) { |
| 436 |
case PRECOMP: |
| 437 |
return precomp(this.curve, this.X, this.Y, this.Z); |
| 438 |
default: |
| 439 |
throw new IllegalArgumentException(); |
| 440 |
} |
| 441 |
case CACHED: |
| 442 |
switch (repr) { |
| 443 |
case CACHED: |
| 444 |
return cached(this.curve, this.X, this.Y, this.Z, this.T); |
| 445 |
default: |
| 446 |
throw new IllegalArgumentException(); |
| 447 |
} |
| 448 |
default: |
| 449 |
throw new UnsupportedOperationException(); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public synchronized void precompute(final boolean precomputeSingle) { |
| 462 |
GroupElement Bi; |
| 463 |
|
| 464 |
if (precomputeSingle && this.precmp == null) { |
| 465 |
|
| 466 |
this.precmp = new GroupElement[32][8]; |
| 467 |
|
| 468 |
Bi = this; |
| 469 |
for (int i = 0; i < 32; i++) { |
| 470 |
GroupElement Bij = Bi; |
| 471 |
for (int j = 0; j < 8; j++) { |
| 472 |
final FieldElement recip = Bij.Z.invert(); |
| 473 |
final FieldElement x = Bij.X.multiply(recip); |
| 474 |
final FieldElement y = Bij.Y.multiply(recip); |
| 475 |
this.precmp[i][j] = precomp(this.curve, y.add(x), y.subtract(x), x.multiply(y).multiply(this.curve.get2D())); |
| 476 |
Bij = Bij.add(Bi.toCached()).toP3(); |
| 477 |
} |
| 478 |
|
| 479 |
for (int k = 0; k < 8; k++) { |
| 480 |
Bi = Bi.add(Bi.toCached()).toP3(); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if (this.dblPrecmp != null) |
| 488 |
return; |
| 489 |
this.dblPrecmp = new GroupElement[8]; |
| 490 |
Bi = this; |
| 491 |
for (int i = 0; i < 8; i++) { |
| 492 |
final FieldElement recip = Bi.Z.invert(); |
| 493 |
final FieldElement x = Bi.X.multiply(recip); |
| 494 |
final FieldElement y = Bi.Y.multiply(recip); |
| 495 |
this.dblPrecmp[i] = precomp(this.curve, y.add(x), y.subtract(x), x.multiply(y).multiply(this.curve.get2D())); |
| 496 |
|
| 497 |
Bi = this.add(this.add(Bi.toCached()).toP3().toCached()).toP3(); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
\\ |
| 527 |
); \\ |
| 528 |
\\ |
| 529 |
. |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public GroupElement dbl() { |
| 535 |
switch (this.repr) { |
| 536 |
case P2: |
| 537 |
case P3: |
| 538 |
FieldElement XX, YY, B, A, AA, Yn, Zn; |
| 539 |
XX = this.X.square(); |
| 540 |
YY = this.Y.square(); |
| 541 |
B = this.Z.squareAndDouble(); |
| 542 |
A = this.X.add(this.Y); |
| 543 |
AA = A.square(); |
| 544 |
Yn = YY.add(XX); |
| 545 |
Zn = YY.subtract(XX); |
| 546 |
return p1p1(this.curve, AA.subtract(Yn), Yn, Zn, B.subtract(Zn)); |
| 547 |
default: |
| 548 |
throw new UnsupportedOperationException(); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
private GroupElement madd(GroupElement q) { |
| 598 |
if (this.repr != Representation.P3) |
| 599 |
throw new UnsupportedOperationException(); |
| 600 |
if (q.repr != Representation.PRECOMP) |
| 601 |
throw new IllegalArgumentException(); |
| 602 |
|
| 603 |
FieldElement YpX, YmX, A, B, C, D; |
| 604 |
YpX = this.Y.add(this.X); |
| 605 |
YmX = this.Y.subtract(this.X); |
| 606 |
A = YpX.multiply(q.X); |
| 607 |
B = YmX.multiply(q.Y); |
| 608 |
C = q.Z.multiply(this.T); |
| 609 |
D = this.Z.add(this.Z); |
| 610 |
return p1p1(this.curve, A.subtract(B), A.add(B), D.add(C), D.subtract(C)); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private GroupElement msub(GroupElement q) { |
| 627 |
if (this.repr != Representation.P3) |
| 628 |
throw new UnsupportedOperationException(); |
| 629 |
if (q.repr != Representation.PRECOMP) |
| 630 |
throw new IllegalArgumentException(); |
| 631 |
|
| 632 |
FieldElement YpX, YmX, A, B, C, D; |
| 633 |
YpX = this.Y.add(this.X); |
| 634 |
YmX = this.Y.subtract(this.X); |
| 635 |
A = YpX.multiply(q.Y); |
| 636 |
B = YmX.multiply(q.X); |
| 637 |
C = q.Z.multiply(this.T); |
| 638 |
D = this.Z.add(this.Z); |
| 639 |
return p1p1(this.curve, A.subtract(B), A.add(B), D.subtract(C), D.add(C)); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
public GroupElement add(GroupElement q) { |
| 669 |
if (this.repr != Representation.P3) |
| 670 |
throw new UnsupportedOperationException(); |
| 671 |
if (q.repr != Representation.CACHED) |
| 672 |
throw new IllegalArgumentException(); |
| 673 |
|
| 674 |
FieldElement YpX, YmX, A, B, C, ZZ, D; |
| 675 |
YpX = this.Y.add(this.X); |
| 676 |
YmX = this.Y.subtract(this.X); |
| 677 |
A = YpX.multiply(q.X); |
| 678 |
B = YmX.multiply(q.Y); |
| 679 |
C = q.T.multiply(this.T); |
| 680 |
ZZ = this.Z.multiply(q.Z); |
| 681 |
D = ZZ.add(ZZ); |
| 682 |
return p1p1(this.curve, A.subtract(B), A.add(B), D.add(C), D.subtract(C)); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
public GroupElement sub(GroupElement q) { |
| 698 |
if (this.repr != Representation.P3) |
| 699 |
throw new UnsupportedOperationException(); |
| 700 |
if (q.repr != Representation.CACHED) |
| 701 |
throw new IllegalArgumentException(); |
| 702 |
|
| 703 |
FieldElement YpX, YmX, A, B, C, ZZ, D; |
| 704 |
YpX = Y.add(X); |
| 705 |
YmX = Y.subtract(X); |
| 706 |
A = YpX.multiply(q.Y); |
| 707 |
B = YmX.multiply(q.X); |
| 708 |
C = q.T.multiply(T); |
| 709 |
ZZ = Z.multiply(q.Z); |
| 710 |
D = ZZ.add(ZZ); |
| 711 |
return p1p1(curve, A.subtract(B), A.add(B), D.subtract(C), D.add(C)); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
public GroupElement negate() { |
| 722 |
if (this.repr != Representation.P3) |
| 723 |
throw new UnsupportedOperationException(); |
| 724 |
return this.curve.getZero(Representation.P3).sub(toCached()).toP3(); |
| 725 |
} |
| 726 |
|
| 727 |
@Override |
| 728 |
public int hashCode() { |
| 729 |
return Arrays.hashCode(this.toByteArray()); |
| 730 |
} |
| 731 |
|
| 732 |
@Override |
| 733 |
public boolean equals(Object obj) { |
| 734 |
if (obj == this) |
| 735 |
return true; |
| 736 |
if (!(obj instanceof GroupElement)) |
| 737 |
return false; |
| 738 |
GroupElement ge = (GroupElement) obj; |
| 739 |
if (!this.repr.equals(ge.repr)) { |
| 740 |
try { |
| 741 |
ge = ge.toRep(this.repr); |
| 742 |
} catch (RuntimeException e) { |
| 743 |
return false; |
| 744 |
} |
| 745 |
} |
| 746 |
switch (this.repr) { |
| 747 |
case P2: |
| 748 |
case P3: |
| 749 |
|
| 750 |
if (this.Z.equals(ge.Z)) |
| 751 |
return this.X.equals(ge.X) && this.Y.equals(ge.Y); |
| 752 |
|
| 753 |
final FieldElement x1 = this.X.multiply(ge.Z); |
| 754 |
final FieldElement y1 = this.Y.multiply(ge.Z); |
| 755 |
final FieldElement x2 = ge.X.multiply(this.Z); |
| 756 |
final FieldElement y2 = ge.Y.multiply(this.Z); |
| 757 |
return x1.equals(x2) && y1.equals(y2); |
| 758 |
case P1P1: |
| 759 |
return toP2().equals(ge); |
| 760 |
case PRECOMP: |
| 761 |
|
| 762 |
return this.X.equals(ge.X) && this.Y.equals(ge.Y) && this.Z.equals(ge.Z); |
| 763 |
case CACHED: |
| 764 |
|
| 765 |
if (this.Z.equals(ge.Z)) |
| 766 |
return this.X.equals(ge.X) && this.Y.equals(ge.Y) && this.T.equals(ge.T); |
| 767 |
|
| 768 |
final FieldElement x3 = this.X.multiply(ge.Z); |
| 769 |
final FieldElement y3 = this.Y.multiply(ge.Z); |
| 770 |
final FieldElement t3 = this.T.multiply(ge.Z); |
| 771 |
final FieldElement x4 = ge.X.multiply(this.Z); |
| 772 |
final FieldElement y4 = ge.Y.multiply(this.Z); |
| 773 |
final FieldElement t4 = ge.T.multiply(this.Z); |
| 774 |
return x3.equals(x4) && y3.equals(y4) && t3.equals(t4); |
| 775 |
default: |
| 776 |
return false; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
static byte[] toRadix16(final byte[] a) { |
| 789 |
final byte[] e = new byte[64]; |
| 790 |
int i; |
| 791 |
|
| 792 |
for (i = 0; i < 32; i++) { |
| 793 |
e[2*i+0] = (byte) (a[i] & 15); |
| 794 |
e[2*i+1] = (byte) ((a[i] >> 4) & 15); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
int carry = 0; |
| 799 |
for (i = 0; i < 63; i++) { |
| 800 |
e[i] += carry; |
| 801 |
carry = e[i] + 8; |
| 802 |
carry >>= 4; |
| 803 |
e[i] -= carry << 4; |
| 804 |
} |
| 805 |
e[63] += carry; |
| 806 |
|
| 807 |
return e; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
GroupElement cmov(final GroupElement u, final int b) { |
| 823 |
return precomp(curve, X.cmov(u.X, b), Y.cmov(u.Y, b), Z.cmov(u.Z, b)); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
GroupElement select(final int pos, final int b) { |
| 841 |
|
| 842 |
final int bnegative = Utils.negative(b); |
| 843 |
|
| 844 |
final int babs = b - (((-bnegative) & b) << 1); |
| 845 |
|
| 846 |
|
| 847 |
final GroupElement t = this.curve.getZero(Representation.PRECOMP) |
| 848 |
.cmov(this.precmp[pos][0], Utils.equal(babs, 1)) |
| 849 |
.cmov(this.precmp[pos][1], Utils.equal(babs, 2)) |
| 850 |
.cmov(this.precmp[pos][2], Utils.equal(babs, 3)) |
| 851 |
.cmov(this.precmp[pos][3], Utils.equal(babs, 4)) |
| 852 |
.cmov(this.precmp[pos][4], Utils.equal(babs, 5)) |
| 853 |
.cmov(this.precmp[pos][5], Utils.equal(babs, 6)) |
| 854 |
.cmov(this.precmp[pos][6], Utils.equal(babs, 7)) |
| 855 |
.cmov(this.precmp[pos][7], Utils.equal(babs, 8)); |
| 856 |
|
| 857 |
final GroupElement tminus = precomp(curve, t.Y, t.X, t.Z.negate()); |
| 858 |
|
| 859 |
return t.cmov(tminus, bnegative); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public GroupElement scalarMultiply(final byte[] a) { |
| 874 |
GroupElement t; |
| 875 |
int i; |
| 876 |
|
| 877 |
final byte[] e = toRadix16(a); |
| 878 |
|
| 879 |
GroupElement h = this.curve.getZero(Representation.P3); |
| 880 |
synchronized(this) { |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
for (i = 1; i < 64; i += 2) { |
| 886 |
t = select(i/2, e[i]); |
| 887 |
h = h.madd(t).toP3(); |
| 888 |
} |
| 889 |
|
| 890 |
h = h.dbl().toP2().dbl().toP2().dbl().toP2().dbl().toP3(); |
| 891 |
|
| 892 |
for (i = 0; i < 64; i += 2) { |
| 893 |
t = select(i/2, e[i]); |
| 894 |
h = h.madd(t).toP3(); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
return h; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
static byte[] slide(final byte[] a) { |
| 914 |
byte[] r = new byte[256]; |
| 915 |
|
| 916 |
|
| 917 |
for (int i = 0; i < 256; ++i) { |
| 918 |
r[i] = (byte) (1 & (a[i >> 3] >> (i & 7))); |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
for (int i = 0; i < 256; ++i) { |
| 923 |
if (r[i] != 0) { |
| 924 |
for (int b = 1; b <= 6 && i + b < 256; ++b) { |
| 925 |
|
| 926 |
if (r[i + b] != 0) { |
| 927 |
if (r[i] + (r[i + b] << b) <= 15) { |
| 928 |
r[i] += r[i + b] << b; |
| 929 |
r[i + b] = 0; |
| 930 |
} else if (r[i] - (r[i + b] << b) >= -15) { |
| 931 |
r[i] -= r[i + b] << b; |
| 932 |
for (int k = i + b; k < 256; ++k) { |
| 933 |
if (r[k] == 0) { |
| 934 |
r[k] = 1; |
| 935 |
break; |
| 936 |
} |
| 937 |
r[k] = 0; |
| 938 |
} |
| 939 |
} else |
| 940 |
break; |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
return r; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
public GroupElement doubleScalarMultiplyVariableTime(final GroupElement A, final byte[] a, final byte[] b) { |
| 961 |
|
| 962 |
final byte[] aslide = slide(a); |
| 963 |
final byte[] bslide = slide(b); |
| 964 |
|
| 965 |
GroupElement r = this.curve.getZero(Representation.P2); |
| 966 |
|
| 967 |
int i; |
| 968 |
for (i = 255; i >= 0; --i) { |
| 969 |
if (aslide[i] != 0 || bslide[i] != 0) break; |
| 970 |
} |
| 971 |
|
| 972 |
synchronized(this) { |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
for (; i >= 0; --i) { |
| 979 |
GroupElement t = r.dbl(); |
| 980 |
|
| 981 |
if (aslide[i] > 0) { |
| 982 |
t = t.toP3().madd(A.dblPrecmp[aslide[i]/2]); |
| 983 |
} else if(aslide[i] < 0) { |
| 984 |
t = t.toP3().msub(A.dblPrecmp[(-aslide[i])/2]); |
| 985 |
} |
| 986 |
|
| 987 |
if (bslide[i] > 0) { |
| 988 |
t = t.toP3().madd(this.dblPrecmp[bslide[i]/2]); |
| 989 |
} else if(bslide[i] < 0) { |
| 990 |
t = t.toP3().msub(this.dblPrecmp[(-bslide[i])/2]); |
| 991 |
} |
| 992 |
|
| 993 |
r = t.toP2(); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
return r; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
public boolean isOnCurve() { |
| 1005 |
return isOnCurve(curve); |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
public boolean isOnCurve(Curve curve) { |
| 1014 |
switch (repr) { |
| 1015 |
case P2: |
| 1016 |
case P3: |
| 1017 |
FieldElement recip = Z.invert(); |
| 1018 |
FieldElement x = X.multiply(recip); |
| 1019 |
FieldElement y = Y.multiply(recip); |
| 1020 |
FieldElement xx = x.square(); |
| 1021 |
FieldElement yy = y.square(); |
| 1022 |
FieldElement dxxyy = curve.getD().multiply(xx).multiply(yy); |
| 1023 |
return curve.getField().ONE.add(dxxyy).add(xx).equals(yy); |
| 1024 |
|
| 1025 |
default: |
| 1026 |
return toP2().isOnCurve(curve); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
@Override |
| 1031 |
public String toString() { |
| 1032 |
return "[GroupElement\nX="+X+"\nY="+Y+"\nZ="+Z+"\nT="+T+"\n]"; |
| 1033 |
} |
| 1034 |
} |