0

I was looking at an example of the multiply opcode(MUL) on the website www.evm.codes and noticed it multiplied:

0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF x 2

resulting in the output:

0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE

how is this possible if the largest possible size of a word on the stack is 32 bytes??

1 Answer 1

1

That is precisely because you have only 32 bytes, and the EVM manages overflows, removing the most significant bits in the result.

The multiply operation is just an add operation you do multiple times.

To sum bytes, you can work the same way with decimals: add the rightmost numbers first, write the sum, and carry the remainder.

So when you want to do 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE * 2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE you start from the rightmost byte:

0xFF * 2 = 0xFF + 0xFF = 0x01FE so the result is 0xFE with 0x01 remainder.

Now, you need to carry that 0x01 for all the other sum operations that are all the same:

0xFF * 2 = 0xFF + 0xFF = 0x01FE + 1 previous remainder = 0x01FF so the result is 0xFF with 0x01 remainder

Repeat that for 32 times, and now you have 33 bytes:

0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE

But you can only deal with 32 bytes, not 33, so you lose the first byte, 0x01 and the final result is

0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.