-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(vm): implement TIP-7939 CLZ opcode #6656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import static org.tron.common.crypto.Hash.sha3; | ||
| import static org.tron.common.utils.ByteUtil.EMPTY_BYTE_ARRAY; | ||
| import static org.tron.common.utils.ByteUtil.numberOfLeadingZeros; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.ArrayList; | ||
|
|
@@ -287,6 +288,17 @@ public static void sarAction(Program program) { | |
| program.step(); | ||
| } | ||
|
|
||
| public static void clzAction(Program program) { | ||
| DataWord word = program.stackPop(); | ||
| int clz = numberOfLeadingZeros(word.getData()); | ||
| if (clz == 256) { | ||
| program.stackPush(new DataWord(256)); | ||
| } else { | ||
| program.stackPush(DataWord.of((byte) clz)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work overall! One minor suggestion: since DataWord.of(byte num) directly assigns bb[31] = num, casting clz to (byte) when it's in [128, 255] introduces a subtle signed/unsigned ambiguity. Would it be worth simplifying both branches into one using new DataWord(int), which handles the full range cleanly?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review! The |
||
| } | ||
| program.step(); | ||
| } | ||
|
|
||
| public static void sha3Action(Program program) { | ||
| DataWord memOffsetData = program.stackPop(); | ||
| DataWord lengthData = program.stackPop(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -904,6 +904,81 @@ public void testPush0() throws ContractValidateException { | |
| VMConfig.initAllowTvmShangHai(0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCLZ() throws ContractValidateException { | ||
| VMConfig.initAllowTvmOsaka(1); | ||
|
|
||
| invoke = new ProgramInvokeMockImpl(); | ||
| Protocol.Transaction trx = Protocol.Transaction.getDefaultInstance(); | ||
| InternalTransaction interTrx = | ||
| new InternalTransaction(trx, InternalTransaction.TrxType.TRX_UNKNOWN_TYPE); | ||
|
|
||
| // CLZ(0) = 256 | ||
| byte[] op = buildCLZBytecode(new byte[32]); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(256), program.getStack().pop()); | ||
|
|
||
| // CLZ(0x80...00) = 0 (highest bit set) | ||
| byte[] val = new byte[32]; | ||
| val[0] = (byte) 0x80; | ||
| op = buildCLZBytecode(val); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(0), program.getStack().pop()); | ||
|
|
||
| // CLZ(0xFF...FF) = 0 | ||
| val = new byte[32]; | ||
| for (int i = 0; i < 32; i++) { | ||
| val[i] = (byte) 0xFF; | ||
| } | ||
| op = buildCLZBytecode(val); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(0), program.getStack().pop()); | ||
|
|
||
| // CLZ(0x40...00) = 1 | ||
| val = new byte[32]; | ||
| val[0] = (byte) 0x40; | ||
| op = buildCLZBytecode(val); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(1), program.getStack().pop()); | ||
|
|
||
| // CLZ(0x7F...FF) = 1 | ||
| val = new byte[32]; | ||
| for (int i = 0; i < 32; i++) { | ||
| val[i] = (byte) 0xFF; | ||
| } | ||
| val[0] = (byte) 0x7F; | ||
| op = buildCLZBytecode(val); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(1), program.getStack().pop()); | ||
|
|
||
| // CLZ(1) = 255 | ||
| val = new byte[32]; | ||
| val[31] = 0x01; | ||
| op = buildCLZBytecode(val); | ||
| program = new Program(op, op, invoke, interTrx); | ||
| testOperations(program); | ||
| Assert.assertEquals(new DataWord(255), program.getStack().pop()); | ||
|
|
||
| // Verify energy cost = LOW_TIER(5) + PUSH32 cost(3) = 8 | ||
| Assert.assertEquals(8, program.getResult().getEnergyUsed()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test coverage is really solid — the chosen vectors (0, 1, 255, 256, boundary bits) cover the most critical scenarios nicely! 🎯 One thing that might be worth adding: a couple of test cases where the CLZ result falls in the 128-255 range (e.g., |
||
|
|
||
| VMConfig.initAllowTvmOsaka(0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work on properly resetting Would it be worth adding a small negative test to verify that CLZ is correctly rejected when |
||
| } | ||
|
|
||
| // Build bytecode: PUSH32 <value> CLZ | ||
| private byte[] buildCLZBytecode(byte[] value) { | ||
| byte[] op = new byte[34]; | ||
| op[0] = 0x7f; // PUSH32 | ||
| System.arraycopy(value, 0, op, 1, 32); | ||
| op[33] = Op.CLZ; | ||
| return op; | ||
| } | ||
|
|
||
| @Test | ||
| public void testSuicideCost() throws ContractValidateException { | ||
| invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], new byte[21]); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.