Skip to content

Commit 2f60675

Browse files
committed
8294997: Improve ECC math operations
Reviewed-by: xuelei
1 parent 94caecb commit 2f60675

File tree

5 files changed

+26
-47
lines changed

5 files changed

+26
-47
lines changed

make/jdk/src/classes/build/tools/intpoly/FieldGen.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ public int getCoefficient() {
246246
}
247247

248248
public BigInteger getValue() {
249-
return BigInteger.valueOf(2).pow(power)
250-
.multiply(BigInteger.valueOf(coefficient));
249+
return BigInteger.valueOf(coefficient).shiftLeft(power);
251250
}
252251

253252
public String toString() {
@@ -663,14 +662,12 @@ private String generate(FieldParams params) throws IOException {
663662
subtract = true;
664663
}
665664
String coefExpr = "BigInteger.valueOf(" + coefValue + ")";
666-
String powExpr = "BigInteger.valueOf(2).pow(" + t.getPower() + ")";
665+
String powExpr = ".shiftLeft(" + t.getPower() + ")";
667666
String termExpr = "ERROR";
668667
if (t.getPower() == 0) {
669668
termExpr = coefExpr;
670-
} else if (coefValue == 1) {
671-
termExpr = powExpr;
672669
} else {
673-
termExpr = powExpr + ".multiply(" + coefExpr + ")";
670+
termExpr = coefExpr + powExpr;
674671
}
675672
if (subtract) {
676673
result.appendLine("result = result.subtract(" + termExpr + ");");

src/java.base/share/classes/sun/security/util/math/intpoly/IntegerPolynomial.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ private void setLimbsValue(BigInteger v, long[] limbs) {
329329
}
330330

331331
protected void setLimbsValuePositive(BigInteger v, long[] limbs) {
332-
BigInteger mod = BigInteger.valueOf(1 << bitsPerLimb);
332+
assert bitsPerLimb < 32;
333+
long limbMask = (1L << bitsPerLimb) - 1;
333334
for (int i = 0; i < limbs.length; i++) {
334-
limbs[i] = v.mod(mod).longValue();
335+
limbs[i] = v.intValue() & limbMask;
335336
v = v.shiftRight(bitsPerLimb);
336337
}
337338
}

src/jdk.crypto.ec/share/classes/sun/security/ec/ECOperations.java

+16-35
Original file line numberDiff line numberDiff line change
@@ -304,25 +304,19 @@ private void setDouble(ProjectivePoint.Mutable p, MutableIntegerModuloP t0,
304304
p.getY().setValue(t2).setProduct(b);
305305
p.getY().setDifference(p.getZ());
306306

307-
p.getX().setValue(p.getY()).setProduct(two);
308-
p.getY().setSum(p.getX());
309-
p.getY().setReduced();
307+
p.getY().setProduct(three);
310308
p.getX().setValue(t1).setDifference(p.getY());
311309

312310
p.getY().setSum(t1);
313311
p.getY().setProduct(p.getX());
314312
p.getX().setProduct(t3);
315313

316-
t3.setValue(t2).setProduct(two);
317-
t2.setSum(t3);
314+
t2.setProduct(three);
318315
p.getZ().setProduct(b);
319316

320-
t2.setReduced();
321317
p.getZ().setDifference(t2);
322318
p.getZ().setDifference(t0);
323-
t3.setValue(p.getZ()).setProduct(two);
324-
p.getZ().setReduced();
325-
p.getZ().setSum(t3);
319+
p.getZ().setProduct(three);
326320
t0.setProduct(three);
327321

328322
t0.setDifference(t2);
@@ -382,26 +376,19 @@ private void setSum(ProjectivePoint.Mutable p, AffinePoint p2,
382376
p.getZ().setProduct(b);
383377

384378
p.getX().setValue(p.getY()).setDifference(p.getZ());
385-
p.getX().setReduced();
386-
p.getZ().setValue(p.getX()).setProduct(two);
387-
p.getX().setSum(p.getZ());
379+
p.getX().setProduct(three);
388380

389381
p.getZ().setValue(t1).setDifference(p.getX());
390382
p.getX().setSum(t1);
391383
p.getY().setProduct(b);
392384

393-
t1.setValue(t2).setProduct(two);
394-
t2.setSum(t1);
395-
t2.setReduced();
385+
t2.setProduct(three);
396386
p.getY().setDifference(t2);
397387

398388
p.getY().setDifference(t0);
399-
p.getY().setReduced();
400-
t1.setValue(p.getY()).setProduct(two);
401-
p.getY().setSum(t1);
389+
p.getY().setProduct(three);
402390

403-
t1.setValue(t0).setProduct(two);
404-
t0.setSum(t1);
391+
t0.setProduct(three);
405392
t0.setDifference(t2);
406393

407394
t1.setValue(t4).setProduct(p.getY());
@@ -413,8 +400,8 @@ private void setSum(ProjectivePoint.Mutable p, AffinePoint p2,
413400
p.getX().setDifference(t1);
414401

415402
p.getZ().setProduct(t4);
416-
t1.setValue(t3).setProduct(t0);
417-
p.getZ().setSum(t1);
403+
t3.setProduct(t0);
404+
p.getZ().setSum(t3);
418405

419406
}
420407

@@ -453,26 +440,20 @@ private void setSum(ProjectivePoint.Mutable p, ProjectivePoint.Mutable p2,
453440

454441
p.getZ().setValue(t2).setProduct(b);
455442
p.getX().setValue(p.getY()).setDifference(p.getZ());
456-
p.getZ().setValue(p.getX()).setProduct(two);
457443

458-
p.getX().setSum(p.getZ());
459-
p.getX().setReduced();
444+
p.getX().setProduct(three);
445+
460446
p.getZ().setValue(t1).setDifference(p.getX());
461447
p.getX().setSum(t1);
462448

463449
p.getY().setProduct(b);
464-
t1.setValue(t2).setSum(t2);
465-
t2.setSum(t1);
466-
t2.setReduced();
450+
t2.setProduct(three);
467451

468452
p.getY().setDifference(t2);
469453
p.getY().setDifference(t0);
470-
p.getY().setReduced();
471-
t1.setValue(p.getY()).setSum(p.getY());
454+
p.getY().setProduct(three);
472455

473-
p.getY().setSum(t1);
474-
t1.setValue(t0).setProduct(two);
475-
t0.setSum(t1);
456+
t0.setProduct(three);
476457

477458
t0.setDifference(t2);
478459
t1.setValue(t4).setProduct(p.getY());
@@ -484,9 +465,9 @@ private void setSum(ProjectivePoint.Mutable p, ProjectivePoint.Mutable p2,
484465

485466
p.getX().setDifference(t1);
486467
p.getZ().setProduct(t4);
487-
t1.setValue(t3).setProduct(t0);
488468

489-
p.getZ().setSum(t1);
469+
t3.setProduct(t0);
470+
p.getZ().setSum(t3);
490471

491472
}
492473
}

src/jdk.crypto.ec/share/classes/sun/security/ec/ed/Ed25519Operations.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -137,7 +137,7 @@ AffinePoint decodeAffinePoint(Function<String, T> exception,
137137
throw exception.apply("Invalid point");
138138
}
139139

140-
if (xLSB != x.asBigInteger().mod(BigInteger.valueOf(2)).intValue()) {
140+
if (xLSB != (x.asBigInteger().intValue() & 1)) {
141141
x.setAdditiveInverse();
142142
}
143143

src/jdk.crypto.ec/share/classes/sun/security/ec/ed/Ed448Operations.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -127,7 +127,7 @@ AffinePoint decodeAffinePoint(Function<String, T> exception, int xLSB,
127127
throw exception.apply("Invalid point");
128128
}
129129

130-
if (xLSB != x.asBigInteger().mod(TWO).intValue()) {
130+
if (xLSB != (x.asBigInteger().intValue() & 1)) {
131131
x.setAdditiveInverse();
132132
}
133133

0 commit comments

Comments
 (0)