Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/java.base/share/classes/sun/security/ec/ECOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public MutablePoint multiply(AffinePoint affineP, byte[] s) {
PointMultiplier multiplier = null;
if (getField() instanceof IntegerMontgomeryFieldModuloP
&& affineP.equals(Secp256R1GeneratorMontgomeryMultiplier.generator)) {
// Lazy class loading when this function is called (large static constant table)
// Lazy class loading here
multiplier = Secp256R1GeneratorMontgomeryMultiplier.multiplier;
} else {
multiplier = new DefaultMultiplier(this, affineP);
Expand All @@ -215,8 +215,8 @@ public MutablePoint multiply(AffinePoint affineP, byte[] s) {
}

/**
* Multiply an affine ecpoint point by a scalar and return the result as a mutable
* point.
* Multiply an affine ecpoint point by a scalar and return the result as a
* mutable point.
*
* @param ecPoint the point
* @param s the scalar as a little-endian array
Expand Down Expand Up @@ -280,8 +280,8 @@ private void setDouble(ProjectivePoint.Mutable p, MutableIntegerModuloP t0,
* Adds second Mutable (Projective) point to first.
*
* Used by ECDSAOperations. This method constructs new temporaries each time
* it is called. For better efficiency, the (private) method that reuses temporaries
* should be used if more than one sum will be computed.
* it is called. For better efficiency, the (private) method that reuses
* temporaries should be used if more than one sum will be computed.
*
* @param p first point and result
* @param p2 second point to add
Expand All @@ -294,7 +294,8 @@ public void setSum(MutablePoint p, MutablePoint p2) {
MutableIntegerModuloP t3 = zero.mutable();
MutableIntegerModuloP t4 = zero.mutable();

setSum((ProjectivePoint.Mutable) p, (ProjectivePoint.Mutable) p2, t0, t1, t2, t3, t4);
setSum((ProjectivePoint.Mutable) p, (ProjectivePoint.Mutable) p2,
t0, t1, t2, t3, t4);
}

/*
Expand Down Expand Up @@ -543,13 +544,17 @@ private void double4(ProjectivePoint.Mutable p,
}
}

// Represents a multiplier with a larger precomputed table. Intended to be used for Basepoint multiplication
final static class Secp256R1GeneratorMontgomeryMultiplier implements PointMultiplier {
// Represents a multiplier with a larger precomputed table. Intended to be
// used for Basepoint multiplication
final static class Secp256R1GeneratorMontgomeryMultiplier
implements PointMultiplier {
private static final ECOperations secp256r1Ops = new ECOperations(
MontgomeryIntegerPolynomialP256.ONE.getElement(CurveDB.P_256.getCurve().getB()),
P256OrderField.ONE);
public static final AffinePoint generator = AffinePoint.fromECPoint(CurveDB.P_256.getGenerator(), secp256r1Ops.getField());
public static final PointMultiplier multiplier = new Secp256R1GeneratorMontgomeryMultiplier();
MontgomeryIntegerPolynomialP256.ONE.getElement(
CurveDB.P_256.getCurve().getB()), P256OrderField.ONE);
public static final AffinePoint generator = AffinePoint.fromECPoint(
CurveDB.P_256.getGenerator(), secp256r1Ops.getField());
public static final PointMultiplier multiplier =
new Secp256R1GeneratorMontgomeryMultiplier();

private final ImmutableIntegerModuloP zero;
private final ImmutableIntegerModuloP one;
Expand All @@ -566,7 +571,8 @@ private Secp256R1GeneratorMontgomeryMultiplier() {
}
}

private Secp256R1GeneratorMontgomeryMultiplier(IntegerFieldModuloP field, PointMultiplier smallTableMultiplier) {
private Secp256R1GeneratorMontgomeryMultiplier(
IntegerFieldModuloP field, PointMultiplier smallTableMultiplier) {
zero = field.get0();
one = field.get1();

Expand Down Expand Up @@ -628,7 +634,8 @@ private Secp256R1GeneratorMontgomeryMultiplier(IntegerFieldModuloP field, PointM
bi = bi.multiply(BigInteger.TWO.pow(d * 16));
}
if (w == 0) {
points[d][0] = new ProjectivePoint.Immutable(zero.fixed(), one.fixed(), zero.fixed());
points[d][0] = new ProjectivePoint.Immutable(
zero.fixed(), one.fixed(), zero.fixed());
} else {
byte[] s = bi.toByteArray();
ArrayUtil.reverse(s);
Expand Down Expand Up @@ -685,12 +692,13 @@ protected void verifyTables(PointMultiplier multiplier) {
ArrayUtil.reverse(b);
System.arraycopy(b, 0, s, 0, b.length);

// Compare this multiplier to the table (generated by Default multiplier)
// Compare this multiplier to the table
// (generated by Default multiplier)
AffinePoint m = multiplier.pointMultiply(s).asAffine();
AffinePoint v = points[d][w].asAffine();
if (!m.equals(v)) {
java.util.HexFormat hex = java.util.HexFormat.of();
throw new RuntimeException("Bad multiple found at ["+d+"]["+w+"]" + hex.formatHex(s) + " " + m.getX().asBigInteger());
throw new RuntimeException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your cleanup went to far. You should have some message saying they are not equal and if you don't want to print hex, remove getting an instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the message back.. I removed it 'half'-intentionally; Was comparing against the original version and it didn't have any details, thought maybe should follow suit. But I did find this message helpful, so its back.

}
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/java.base/share/classes/sun/security/ec/point/AffinePoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,22 @@ public boolean equals(Object obj) {
// both fields same
xEquals = x.asBigInteger().equals(p.x.asBigInteger());
yEquals = y.asBigInteger().equals(p.y.asBigInteger());
} else if (thisMont) { // mismatched fields should not happen in production, but useful in testing
IntegerMontgomeryFieldModuloP field = (IntegerMontgomeryFieldModuloP)x.getField();
xEquals = x.asBigInteger().equals(field.getElement(p.x.asBigInteger()).asBigInteger());
yEquals = y.asBigInteger().equals(field.getElement(p.y.asBigInteger()).asBigInteger());
} else if (thisMont) {
// mismatched fields should not happen in production, but useful in
// testing
IntegerMontgomeryFieldModuloP field =
(IntegerMontgomeryFieldModuloP)x.getField();
xEquals = x.asBigInteger().equals(
field.getElement(p.x.asBigInteger()).asBigInteger());
yEquals = y.asBigInteger().equals(
field.getElement(p.y.asBigInteger()).asBigInteger());
} else {
IntegerMontgomeryFieldModuloP field = (IntegerMontgomeryFieldModuloP)p.x.getField();
xEquals = field.getElement(x.asBigInteger()).asBigInteger().equals(p.x.asBigInteger());
yEquals = field.getElement(y.asBigInteger()).asBigInteger().equals(p.y.asBigInteger());
IntegerMontgomeryFieldModuloP field =
(IntegerMontgomeryFieldModuloP)p.x.getField();
xEquals = field.getElement(
x.asBigInteger()).asBigInteger().equals(p.x.asBigInteger());
yEquals = field.getElement(
y.asBigInteger()).asBigInteger().equals(p.y.asBigInteger());
}
return xEquals && yEquals;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@

public abstract sealed class IntegerPolynomial implements IntegerFieldModuloP
permits IntegerPolynomial1305, IntegerPolynomial25519,
IntegerPolynomial448, IntegerPolynomialP256, MontgomeryIntegerPolynomialP256,
IntegerPolynomialP384, IntegerPolynomialP521,
IntegerPolynomialModBinP, P256OrderField,
P384OrderField, P521OrderField,
Curve25519OrderField,
IntegerPolynomial448, IntegerPolynomialP256,
MontgomeryIntegerPolynomialP256, IntegerPolynomialP384,
IntegerPolynomialP521, IntegerPolynomialModBinP, P256OrderField,
P384OrderField, P521OrderField, Curve25519OrderField,
Curve448OrderField {

protected static final BigInteger TWO = BigInteger.valueOf(2);
Expand Down
Loading