Skip to content

Commit 31a2228

Browse files
committed
Add compare(Unsigned) to Integers/Longs
1 parent 73345af commit 31a2228

File tree

20 files changed

+114
-43
lines changed

20 files changed

+114
-43
lines changed

core/src/main/j2me/java/math/BigInteger.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Vector;
66

77
import org.bouncycastle.util.Arrays;
8+
import org.bouncycastle.util.Integers;
89

910
public class BigInteger
1011
{
@@ -978,9 +979,10 @@ private static int compareNoLeadingZeroes(int xIndx, int[] x, int yIndx, int[] y
978979
int v1 = x[xIndx++];
979980
int v2 = y[yIndx++];
980981

981-
if (v1 != v2)
982+
int c = Integers.compareUnsigned(v1, v2);
983+
if (c != 0)
982984
{
983-
return (v1 ^ Integer.MIN_VALUE) < (v2 ^ Integer.MIN_VALUE) ? -1 : 1;
985+
return c;
984986
}
985987
}
986988

core/src/main/j2me/org/bouncycastle/util/Integers.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public static int bitCount(int i)
2222
return i;
2323
}
2424

25+
public static int compare(int x, int y)
26+
{
27+
return x < y ? -1 : x == y ? 0 : 1;
28+
}
29+
30+
public static int compareUnsigned(int x, int y)
31+
{
32+
return compare(x + Integer.MIN_VALUE, y + Integer.MIN_VALUE);
33+
}
34+
2535
public static int highestOneBit(int i)
2636
{
2737
i |= (i >> 1);

core/src/main/j2me/org/bouncycastle/util/Longs.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public class Longs
1313
0x3E, 0x33, 0x05, 0x19, 0x24, 0x27, 0x20, 0x2E, 0x3C, 0x2C, 0x2A, 0x14, 0x16, 0x39, 0x10, 0x09,
1414
0x32, 0x18, 0x23, 0x1F, 0x3B, 0x13, 0x38, 0x0F, 0x31, 0x1E, 0x12, 0x0E, 0x1D, 0x0D, 0x0C, 0x0B };
1515

16+
public static int compare(long x, long y)
17+
{
18+
return x < y ? -1 : x == y ? 0 : 1;
19+
}
20+
21+
public static int compareUnsigned(long x, long y)
22+
{
23+
return compare(x + Long.MIN_VALUE, y + Long.MIN_VALUE);
24+
}
25+
1626
public static long highestOneBit(long i)
1727
{
1828
i |= (i >> 1);

core/src/main/java/org/bouncycastle/crypto/modes/ChaCha20Poly1305.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.bouncycastle.crypto.params.KeyParameter;
1212
import org.bouncycastle.crypto.params.ParametersWithIV;
1313
import org.bouncycastle.util.Arrays;
14+
import org.bouncycastle.util.Longs;
1415
import org.bouncycastle.util.Pack;
1516

1617
public class ChaCha20Poly1305
@@ -530,7 +531,7 @@ private void finishData(int nextState)
530531

531532
private long incrementCount(long count, int increment, long limit)
532533
{
533-
if (count + Long.MIN_VALUE > (limit - increment) + Long.MIN_VALUE)
534+
if (Longs.compareUnsigned(count, limit - increment) > 0)
534535
{
535536
throw new IllegalStateException("Limit exceeded");
536537
}

core/src/main/java/org/bouncycastle/crypto/modes/GCMSIVBlockCipher.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ private void checkAEADStatus(final int pLen)
257257
}
258258

259259
/* Make sure that we haven't breached AEAD data limit */
260-
if (theAEADHasher.getBytesProcessed() + Long.MIN_VALUE
261-
> (MAX_DATALEN - pLen) + Long.MIN_VALUE)
260+
if (Longs.compareUnsigned(theAEADHasher.getBytesProcessed(), MAX_DATALEN - pLen) > 0)
262261
{
263262
throw new IllegalStateException("AEAD byte count exceeded");
264263
}
@@ -291,8 +290,7 @@ private void checkStatus(final int pLen)
291290
dataLimit += BUFLEN;
292291
currBytes = theEncData.size();
293292
}
294-
if (currBytes + Long.MIN_VALUE
295-
> (dataLimit - pLen) + Long.MIN_VALUE)
293+
if (Longs.compareUnsigned(currBytes, dataLimit - pLen) > 0)
296294
{
297295
throw new IllegalStateException("byte count exceeded");
298296
}

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Ed25519.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.math.ec.rfc7748.X25519Field;
99
import org.bouncycastle.math.raw.Interleave;
1010
import org.bouncycastle.math.raw.Nat256;
11+
import org.bouncycastle.util.Integers;
1112

1213
/**
1314
* A low-level implementation of the Ed25519, Ed25519ctx, and Ed25519ph instantiations of the Edwards-Curve
@@ -231,11 +232,11 @@ private static boolean checkPointFullVar(byte[] p)
231232
int y0 = Codec.decode32(p, 0);
232233

233234
// Reject 0 and 1
234-
if (t0 == 0 && (y0 + Integer.MIN_VALUE) <= (1 + Integer.MIN_VALUE))
235+
if (t0 == 0 && Integers.compareUnsigned(y0, 1) <= 0)
235236
return false;
236237

237238
// Reject P - 1 and non-canonical encodings (i.e. >= P)
238-
if (t1 == 0 && (y0 + Integer.MIN_VALUE) >= (P[0] - 1 + Integer.MIN_VALUE))
239+
if (t1 == 0 && Integers.compareUnsigned(y0, P[0] - 1) >= 0)
239240
return false;
240241

241242
t2 |= y0 ^ ORDER8_y1[0];

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Ed448.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bouncycastle.math.ec.rfc7748.X448;
88
import org.bouncycastle.math.ec.rfc7748.X448Field;
99
import org.bouncycastle.math.raw.Nat;
10+
import org.bouncycastle.util.Integers;
1011

1112
/**
1213
* A low-level implementation of the Ed448 and Ed448ph instantiations of the Edwards-Curve Digital Signature
@@ -191,7 +192,7 @@ private static boolean checkPointFullVar(byte[] p)
191192
int yi = Codec.decode32(p, i * 4);
192193

193194
// Reject non-canonical encodings (i.e. >= P)
194-
if (t1 == 0 && (yi + Integer.MIN_VALUE) > (P[i] + Integer.MIN_VALUE))
195+
if (t1 == 0 && Integers.compareUnsigned(yi, P[i]) > 0)
195196
return false;
196197

197198
t0 |= yi;
@@ -201,11 +202,11 @@ private static boolean checkPointFullVar(byte[] p)
201202
int y0 = Codec.decode32(p, 0);
202203

203204
// Reject 0 and 1
204-
if (t0 == 0 && (y0 + Integer.MIN_VALUE) <= (1 + Integer.MIN_VALUE))
205+
if (t0 == 0 && Integers.compareUnsigned(y0, 1) <= 0)
205206
return false;
206207

207208
// Reject P - 1 and non-canonical encodings (i.e. >= P)
208-
if (t1 == 0 && (y0 + Integer.MIN_VALUE) >= (P[0] - 1 + Integer.MIN_VALUE))
209+
if (t1 == 0 && Integers.compareUnsigned(y0, P[0] - 1) >= 0)
209210
return false;
210211

211212
return true;

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Scalar25519.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static boolean reduceBasisVar(int[] k, int[] z0, int[] z1)
339339
ScalarUtil.subShifted_UV(3, s, u0, u1, v0, v1);
340340
}
341341

342-
if (ScalarUtil.lessThan(last, Nu, Nv))
342+
if (ScalarUtil.lessThanUnsigned(last, Nu, Nv))
343343
{
344344
int[] t0 = u0; u0 = v0; v0 = t0;
345345
int[] t1 = u1; u1 = v1; v1 = t1;

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Scalar448.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static boolean reduceBasisVar(int[] k, int[] z0, int[] z1)
604604
ScalarUtil.subShifted_UV(7, s, u0, u1, v0, v1);
605605
}
606606

607-
if (ScalarUtil.lessThan(last, Nu, Nv))
607+
if (ScalarUtil.lessThanUnsigned(last, Nu, Nv))
608608
{
609609
int[] t0 = u0; u0 = v0; v0 = t0;
610610
int[] t1 = u1; u1 = v1; v1 = t1;

core/src/main/java/org/bouncycastle/math/ec/rfc8032/ScalarUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static int getBitLengthPositive(int last, int[] x)
179179
return i * 32 + 32 - Integers.numberOfLeadingZeros(x[i]);
180180
}
181181

182-
static boolean lessThan(int last, int[] x, int[] y)
182+
static boolean lessThanUnsigned(int last, int[] x, int[] y)
183183
{
184184
int i = last;
185185
do

0 commit comments

Comments
 (0)