Skip to content

Commit 0663e17

Browse files
committed
test(vm): add boundary and edge case tests for eip-7823 modexp limit
- baseLen == 1024 boundary value should succeed - baseLen == 1025 just over limit should fail - oversized expLen only should fail - oversized modLen only should fail - all limits exceeded with osaka disabled should succeed
1 parent a8fa3d4 commit 0663e17

1 file changed

Lines changed: 47 additions & 10 deletions

File tree

framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,66 @@
1212
@Slf4j
1313
public class AllowTvmOsakaTest extends VMTestBase {
1414

15+
private static final PrecompiledContracts.PrecompiledContract modExp =
16+
new PrecompiledContracts.ModExp();
17+
18+
private static byte[] toLenBytes(int value) {
19+
byte[] b = new byte[32];
20+
b[28] = (byte) ((value >> 24) & 0xFF);
21+
b[29] = (byte) ((value >> 16) & 0xFF);
22+
b[30] = (byte) ((value >> 8) & 0xFF);
23+
b[31] = (byte) (value & 0xFF);
24+
return b;
25+
}
26+
1527
@Test
1628
public void testEIP7823() {
1729
ConfigLoader.disable = true;
1830
VMConfig.initAllowTvmOsaka(1);
1931

2032
try {
21-
byte[] baseLen = new byte[32];
22-
byte[] expLen = new byte[32];
23-
byte[] modLen = new byte[32];
24-
25-
PrecompiledContracts.PrecompiledContract modExp = new PrecompiledContracts.ModExp();
33+
// all-zero lengths: should succeed
34+
Pair<Boolean, byte[]> result = modExp.execute(
35+
ByteUtil.merge(toLenBytes(0), toLenBytes(0), toLenBytes(0)));
36+
Assert.assertTrue(result.getLeft());
2637

27-
// Valid lens: all zeros (0 <= 1024)
28-
Pair<Boolean, byte[]> result = modExp.execute(ByteUtil.merge(baseLen, expLen, modLen));
38+
// baseLen == 1024: boundary, should succeed
39+
result = modExp.execute(
40+
ByteUtil.merge(toLenBytes(1024), toLenBytes(0), toLenBytes(0)));
2941
Assert.assertTrue(result.getLeft());
3042

31-
// Invalid lens: baseLen = 0x01000000... = 16777216 > 1024
32-
baseLen[0] = 0x01;
33-
result = modExp.execute(ByteUtil.merge(baseLen, expLen, modLen));
43+
// baseLen == 1025: just over the limit, should fail
44+
result = modExp.execute(
45+
ByteUtil.merge(toLenBytes(1025), toLenBytes(0), toLenBytes(0)));
46+
Assert.assertFalse(result.getLeft());
47+
48+
// oversized expLen only: should fail
49+
result = modExp.execute(
50+
ByteUtil.merge(toLenBytes(0), toLenBytes(1025), toLenBytes(0)));
51+
Assert.assertFalse(result.getLeft());
52+
53+
// oversized modLen only: should fail
54+
result = modExp.execute(
55+
ByteUtil.merge(toLenBytes(0), toLenBytes(0), toLenBytes(1025)));
3456
Assert.assertFalse(result.getLeft());
3557
} finally {
3658
VMConfig.initAllowTvmOsaka(0);
3759
ConfigLoader.disable = false;
3860
}
3961
}
62+
63+
@Test
64+
public void testEIP7823DisabledShouldPass() {
65+
ConfigLoader.disable = true;
66+
VMConfig.initAllowTvmOsaka(0);
67+
68+
try {
69+
// all limits exceeded while osaka is disabled: should succeed (no restriction)
70+
Pair<Boolean, byte[]> result = modExp.execute(
71+
ByteUtil.merge(toLenBytes(2048), toLenBytes(2048), toLenBytes(2048)));
72+
Assert.assertTrue(result.getLeft());
73+
} finally {
74+
ConfigLoader.disable = false;
75+
}
76+
}
4077
}

0 commit comments

Comments
 (0)