Skip to content

Commit 98c67b0

Browse files
FIX: prop test failing on shiftLeft
The shiftLeft operation did not make sure to provision enough space for the result of shiftLeftInto. Now it does so, but it is somewhat wasteful at the moment, as max 8 limbs are going to be kept. This should be optimised later on. Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
1 parent c675167 commit 98c67b0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

evm/src/main/java/org/hyperledger/besu/evm/UInt256.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public UInt256 shiftLeft(final int shift) {
273273
if (shift < 0) return shiftRight(-shift);
274274
if (shift >= BITSIZE) return ZERO;
275275
if (shift == 0 || isZero()) return this;
276-
int[] shifted = new int[N_LIMBS];
276+
int[] shifted = new int[N_LIMBS + (shift + N_BITS_PER_LIMB - 1) / N_BITS_PER_LIMB];
277277
shiftLeftInto(shifted, this.limbs, this.length, shift);
278278
return new UInt256(shifted);
279279
}
@@ -466,7 +466,6 @@ private static void shiftLeftInto(
466466
// Unchecked: result length should be at least x.length + limbShift
467467
int limbShift = shift / N_BITS_PER_LIMB;
468468
int bitShift = shift % N_BITS_PER_LIMB;
469-
if (limbShift >= xLen) return;
470469
if (bitShift == 0) {
471470
System.arraycopy(x, 0, result, limbShift, xLen);
472471
return;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
public class UInt256Test {
28-
static final int SAMPLE_SIZE = 30000;
28+
static final int SAMPLE_SIZE = 3;
2929

3030
private Bytes32 bigIntTo32B(final BigInteger x) {
3131
byte[] a = x.toByteArray();
@@ -109,6 +109,14 @@ public void fromToBytesBE() {
109109
assertThat(asUint.toBytesBE()).isEqualTo(asBigInt.toByteArray());
110110
}
111111

112+
@Test
113+
void testLeftShift() {
114+
UInt256 a = UInt256.fromInt(1);
115+
UInt256 result = a.shiftLeft(32);
116+
UInt256 expected = new UInt256( new int[]{0, 1, 0, 0, 0, 0, 0, 0});
117+
assertThat(result).isEqualTo(expected);
118+
}
119+
112120
@Test
113121
void testRightShiftCase() {
114122
final BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE);

0 commit comments

Comments
 (0)