Skip to content

Commit 14774b9

Browse files
FIX: sigmod tests bigintegers to 32Bytes
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
1 parent a091237 commit 14774b9

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

evm/src/test/java/org/hyperledger/besu/evm/UInt256Test.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private Bytes32 bigIntTo32B(final BigInteger x) {
3333
return Bytes32.leftPad(Bytes.wrap(a));
3434
}
3535

36-
private Bytes32 bigIntTo32B(final BigInteger x, final int sign) {
37-
if (sign >= 0) return bigIntTo32B(x);
36+
private Bytes32 bigIntToSigned32B(final BigInteger x) {
37+
if (x.signum() >= 0) return bigIntTo32B(x);
3838
byte[] a = new byte[32];
3939
Arrays.fill(a, (byte) 0xFF);
4040
byte[] b = x.toByteArray();
@@ -343,21 +343,14 @@ public void signedMod() {
343343
for (int i = 0; i < SAMPLE_SIZE; i++) {
344344
int aSize = random.nextInt(1, 33);
345345
int bSize = random.nextInt(1, 33);
346-
boolean neg = random.nextBoolean();
347346
byte[] aArray = new byte[aSize];
348347
byte[] bArray = new byte[bSize];
349348
random.nextBytes(aArray);
350349
random.nextBytes(bArray);
351-
if ((aSize < 32) && (neg)) {
352-
byte[] tmp = new byte[32];
353-
Arrays.fill(tmp, (byte) 0xFF);
354-
System.arraycopy(aArray, 0, tmp, 32 - aArray.length, aArray.length);
355-
aArray = tmp;
356-
}
357350
UInt256 a = UInt256.fromBytesBE(aArray);
358351
UInt256 b = UInt256.fromBytesBE(bArray);
359-
BigInteger aInt = a.isNegative() ? new BigInteger(aArray) : new BigInteger(1, aArray);
360-
BigInteger bInt = b.isNegative() ? new BigInteger(bArray) : new BigInteger(1, bArray);
352+
BigInteger aInt = aArray.length < 32 ? new BigInteger(1, aArray) : new BigInteger(aArray);
353+
BigInteger bInt = bArray.length < 32 ? new BigInteger(1, bArray) : new BigInteger(bArray);
361354
Bytes32 remainder = Bytes32.leftPad(Bytes.wrap(a.signedMod(b).toBytesBE()));
362355
Bytes32 expected;
363356
BigInteger rem = BigInteger.ZERO;
@@ -366,9 +359,9 @@ public void signedMod() {
366359
rem = aInt.abs().mod(bInt.abs());
367360
if ((aInt.compareTo(BigInteger.ZERO) < 0) && (rem.compareTo(BigInteger.ZERO) != 0)) {
368361
rem = rem.negate();
369-
expected = bigIntTo32B(rem, -1);
362+
expected = bigIntToSigned32B(rem);
370363
} else {
371-
expected = bigIntTo32B(rem, 1);
364+
expected = bigIntTo32B(rem);
372365
}
373366
}
374367
assertThat(remainder).isEqualTo(expected);

0 commit comments

Comments
 (0)