Skip to content

Commit b778cd5

Browse files
committedNov 29, 2022
8295010: Reduce if required in EC limbs operations
Reviewed-by: djelinski, jjiang
1 parent 54e6d6a commit b778cd5

File tree

8 files changed

+107
-57
lines changed

8 files changed

+107
-57
lines changed
 

‎src/java.base/share/classes/sun/security/util/math/MutableIntegerModuloP.java

-8
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,5 @@ MutableIntegerModuloP setValue(byte[] v, int offset, int length,
150150
* @return this
151151
*/
152152
MutableIntegerModuloP setAdditiveInverse();
153-
154-
/**
155-
* Some implementations required reduction operations to be requested
156-
* by the client at certain times. This method reduces the representation.
157-
*
158-
* @return this
159-
*/
160-
MutableIntegerModuloP setReduced();
161153
}
162154

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

+102-33
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
/**
3636
* A large number polynomial representation using sparse limbs of signed
3737
* long (64-bit) values. Limb values will always fit within a long, so inputs
38-
* to multiplication must be less than 32 bits. All IntegerPolynomial
39-
* implementations allow at most one addition before multiplication. Additions
40-
* after that will result in an ArithmeticException.
38+
* to multiplication must be less than 32 bits.
4139
*
4240
* The following element operations are branch-free for all subclasses:
4341
*
@@ -553,16 +551,22 @@ public MutableElement mutable() {
553551
return new MutableElement(limbs.clone(), numAdds);
554552
}
555553

556-
protected boolean isSummand() {
557-
return numAdds < maxAdds;
558-
}
559-
560554
@Override
561555
public ImmutableElement add(IntegerModuloP genB) {
562556
assert IntegerPolynomial.this == genB.getField();
563-
Element b = (Element) genB;
564-
if (!(isSummand() && b.isSummand())) {
565-
throw new ArithmeticException("Not a valid summand");
557+
Element b = (Element)genB;
558+
559+
// Reduce if required.
560+
// if (numAdds >= maxAdds) {
561+
if (numAdds > 32 - bitsPerLimb) {
562+
reduce(limbs);
563+
numAdds = 0;
564+
}
565+
566+
// if (b.numAdds >= maxAdds) {
567+
if (b.numAdds > 32 - bitsPerLimb) {
568+
reduce(b.limbs);
569+
b.numAdds = 0;
566570
}
567571

568572
long[] newLimbs = new long[limbs.length];
@@ -597,7 +601,18 @@ protected void copyLow(long[] limbs, long[] out) {
597601
@Override
598602
public ImmutableElement multiply(IntegerModuloP genB) {
599603
assert IntegerPolynomial.this == genB.getField();
600-
Element b = (Element) genB;
604+
Element b = (Element)genB;
605+
606+
// Reduce if required.
607+
if (numAdds > maxAdds) {
608+
reduce(limbs);
609+
numAdds = 0;
610+
}
611+
612+
if (b.numAdds > maxAdds) {
613+
reduce(b.limbs);
614+
b.numAdds = 0;
615+
}
601616

602617
long[] newLimbs = new long[limbs.length];
603618
mult(limbs, b.limbs, newLimbs);
@@ -606,24 +621,42 @@ public ImmutableElement multiply(IntegerModuloP genB) {
606621

607622
@Override
608623
public ImmutableElement square() {
624+
// Reduce if required.
625+
if (numAdds > maxAdds) {
626+
reduce(limbs);
627+
numAdds = 0;
628+
}
629+
609630
long[] newLimbs = new long[limbs.length];
610631
IntegerPolynomial.this.square(limbs, newLimbs);
611632
return new ImmutableElement(newLimbs, 0);
612633
}
613634

614635
public void addModPowerTwo(IntegerModuloP arg, byte[] result) {
615636
assert IntegerPolynomial.this == arg.getField();
616-
Element other = (Element) arg;
617-
if (!(isSummand() && other.isSummand())) {
618-
throw new ArithmeticException("Not a valid summand");
637+
Element other = (Element)arg;
638+
639+
// Reduce if required.
640+
if (numAdds > 32 - bitsPerLimb) {
641+
reduce(limbs);
642+
numAdds = 0;
619643
}
644+
645+
if (other.numAdds > 32 - bitsPerLimb) {
646+
reduce(other.limbs);
647+
other.numAdds = 0;
648+
}
649+
620650
addLimbsModPowerTwo(limbs, other.limbs, result);
621651
}
622652

623653
public void asByteArray(byte[] result) {
624-
if (!isSummand()) {
625-
throw new ArithmeticException("Not a valid summand");
654+
// Reduce if required.
655+
if (numAdds != 0) {
656+
reduce(limbs);
657+
numAdds = 0;
626658
}
659+
627660
limbsToByteArray(limbs, result);
628661
}
629662

@@ -698,15 +731,33 @@ public MutableElement setValue(ByteBuffer buf, int length,
698731
@Override
699732
public MutableElement setProduct(IntegerModuloP genB) {
700733
assert IntegerPolynomial.this == genB.getField();
701-
Element b = (Element) genB;
734+
Element b = (Element)genB;
735+
736+
// Reduce if required.
737+
if (numAdds > maxAdds) {
738+
reduce(limbs);
739+
numAdds = 0;
740+
}
741+
742+
if (b.numAdds > maxAdds) {
743+
reduce(b.limbs);
744+
b.numAdds = 0;
745+
}
746+
702747
mult(limbs, b.limbs, limbs);
703748
numAdds = 0;
704749
return this;
705750
}
706751

707752
@Override
708753
public MutableElement setProduct(SmallValue v) {
709-
int value = ((Limb) v).value;
754+
// Reduce if required.
755+
if (numAdds > maxAdds) {
756+
reduce(limbs);
757+
numAdds = 0;
758+
}
759+
760+
int value = ((Limb)v).value;
710761
multByInt(limbs, value);
711762
numAdds = 0;
712763
return this;
@@ -715,9 +766,19 @@ public MutableElement setProduct(SmallValue v) {
715766
@Override
716767
public MutableElement setSum(IntegerModuloP genB) {
717768
assert IntegerPolynomial.this == genB.getField();
718-
Element b = (Element) genB;
719-
if (!(isSummand() && b.isSummand())) {
720-
throw new ArithmeticException("Not a valid summand");
769+
Element b = (Element)genB;
770+
771+
// Reduce if required.
772+
// if (numAdds >= maxAdds) {
773+
if (numAdds > 32 - bitsPerLimb) {
774+
reduce(limbs);
775+
numAdds = 0;
776+
}
777+
778+
// if (b.numAdds >= maxAdds) {
779+
if (b.numAdds > 32 - bitsPerLimb) {
780+
reduce(b.limbs);
781+
b.numAdds = 0;
721782
}
722783

723784
for (int i = 0; i < limbs.length; i++) {
@@ -731,9 +792,19 @@ public MutableElement setSum(IntegerModuloP genB) {
731792
@Override
732793
public MutableElement setDifference(IntegerModuloP genB) {
733794
assert IntegerPolynomial.this == genB.getField();
734-
Element b = (Element) genB;
735-
if (!(isSummand() && b.isSummand())) {
736-
throw new ArithmeticException("Not a valid summand");
795+
Element b = (Element)genB;
796+
797+
// Reduce if required.
798+
// if (numAdds >= maxAdds) {
799+
if (numAdds > 32 - bitsPerLimb) {
800+
reduce(limbs);
801+
numAdds = 0;
802+
}
803+
804+
// if (b.numAdds >= maxAdds) {
805+
if (b.numAdds > 32 - bitsPerLimb) {
806+
reduce(b.limbs);
807+
b.numAdds = 0;
737808
}
738809

739810
for (int i = 0; i < limbs.length; i++) {
@@ -746,6 +817,12 @@ public MutableElement setDifference(IntegerModuloP genB) {
746817

747818
@Override
748819
public MutableElement setSquare() {
820+
// Reduce if required.
821+
if (numAdds > maxAdds) {
822+
reduce(limbs);
823+
numAdds = 0;
824+
}
825+
749826
IntegerPolynomial.this.square(limbs, limbs);
750827
numAdds = 0;
751828
return this;
@@ -758,13 +835,6 @@ public MutableElement setAdditiveInverse() {
758835
}
759836
return this;
760837
}
761-
762-
@Override
763-
public MutableElement setReduced() {
764-
reduce(limbs);
765-
numAdds = 0;
766-
return this;
767-
}
768838
}
769839

770840
class ImmutableElement extends Element implements ImmutableIntegerModuloP {
@@ -795,6 +865,5 @@ static class Limb implements SmallValue {
795865
this.value = value;
796866
}
797867
}
798-
799-
800868
}
869+

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

-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ private void setSum(ProjectivePoint.Mutable p, AffinePoint p2,
293293
t1.setValue(p.getY()).setProduct(p2.getY());
294294
t3.setValue(p2.getX()).setSum(p2.getY());
295295
t4.setValue(p.getX()).setSum(p.getY());
296-
p.getX().setReduced();
297296
t3.setProduct(t4);
298297
t4.setValue(t0).setSum(t1);
299298

@@ -366,7 +365,6 @@ private void setSum(ProjectivePoint.Mutable p, ProjectivePoint.Mutable p2,
366365
p.getX().setProduct(p.getY());
367366
p.getY().setValue(t0).setSum(t2);
368367
p.getY().setAdditiveInverse().setSum(p.getX());
369-
p.getY().setReduced();
370368

371369
p.getZ().setValue(t2).setProduct(b);
372370
p.getX().setValue(p.getY()).setDifference(p.getZ());

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ protected ExtendedHomogeneousPoint.Mutable setDouble(
191191
// x = A = x^2
192192
p.getY().setSquare();
193193
// y = B = y^2
194-
t2.setValue(p.getX()).setSum(p.getY()).setReduced();
194+
t2.setValue(p.getX()).setSum(p.getY());
195195
// t2 holds H
196196
p.getZ().setSquare().setProduct(two);
197197
// z holds C
198198

199199
p.getT().setValue(t2).setDifference(t1);
200200
// t holds E
201-
t1.setValue(p.getX()).setDifference(p.getY()).setReduced();
201+
t1.setValue(p.getX()).setDifference(p.getY());
202202
// t1 holds G
203203

204204
p.getZ().setSum(t1);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ ProjectivePoint.Mutable setSum(
154154
t3.setValue(d).setProduct(t1).setProduct(p1.getY());
155155
// t3 holds E
156156
// do part of the final calculation of x and y to free up t1
157-
p1.getX().setDifference(t1).setReduced().setDifference(p1.getY());
157+
p1.getX().setDifference(t1).setDifference(p1.getY());
158158
p1.getY().setDifference(t1);
159159
t1.setValue(p1.getZ()).setSquare();
160160
// t2 holds B
@@ -185,7 +185,7 @@ protected ProjectivePoint.Mutable setDouble(ProjectivePoint.Mutable p,
185185
p.getZ().setSquare();
186186
// z holds H
187187

188-
t1.setValue(t2).setSum(p.getY()).setReduced();
188+
t1.setValue(t2).setSum(p.getY());
189189
// t1 holds E
190190
t2.setDifference(p.getY());
191191
p.getY().setValue(t1).setProduct(t2);

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

-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ public byte[] sign(EdDSAParameterSpec sigParams, byte[] privateKey,
116116
IntegerModuloP rElem = subField.getElement(r);
117117
MutableIntegerModuloP S = kElem.mutable().setProduct(sElem);
118118
S.setSum(rElem);
119-
// need to be reduced before output conversion
120-
S.setReduced();
121119
byte[] sArr = S.asByteArray(byteLength);
122120
byte[] rArr = encode(byteLength, R);
123121

‎test/jdk/sun/security/util/math/BigIntegerModuloP.java

-7
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,6 @@ public MutableElement setAdditiveInverse() {
263263
v = BigInteger.ZERO.subtract(v);
264264
return this;
265265
}
266-
267-
@Override
268-
public MutableElement setReduced() {
269-
// do nothing
270-
return this;
271-
}
272-
273266
}
274267

275268
private class SmallElement extends ImmutableElement implements SmallValue {

‎test/jdk/sun/security/util/math/TestIntegerModuloP.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static void runOperationsTest(Random random, int length,
302302
if (elem.test.getField() instanceof IntegerPolynomial) {
303303
IntegerPolynomial field =
304304
(IntegerPolynomial) elem.test.getField();
305-
int numAdds = field.getMaxAdds();
305+
int numAdds = 10; // check for addition overflow
306306
for (int j = 1; j < numAdds; j++) {
307307
ElemFunction addFunc3 = ADD_FUNCTIONS.
308308
get(random.nextInt(ADD_FUNCTIONS.size()));

0 commit comments

Comments
 (0)
Please sign in to comment.