Skip to content
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

Bitwise shifting in EVM #105

Closed
axic opened this issue May 4, 2016 · 9 comments
Closed

Bitwise shifting in EVM #105

axic opened this issue May 4, 2016 · 9 comments

Comments

@axic
Copy link
Member

axic commented May 4, 2016

Is there any reason bitwise shifting was omitted from the EVM? Other bitwise operators are present.

In the past few months in Solidity it was a challenge to deal with arbitrary data and even efficiently implement something like hex to string or string to hex conversion.

Rudimentary shift alternatives are: a * (2 ** b) for shl and a / (2 ** b) for shr. One can use SIGNEXTEND to implement arithmetic right shift. (Using this to implement shift in Solidity: ethereum/solidity#527. Having a progression path to native shifting would make sense.)

While there are some alternative opcodes in certain cases (such as the BYTE and MSTORE8) I still think it would be a very useful addition to have native logical shifts (SHL and SHR). I am not fully convinced about the necessity of arithmetic right shift (SAR). Additionally bit rotation (ROR/ROL) might make sense.

@chfast @gcolvin I know you were discussing the option to have 64 bit arithmetic. Did you think about including bit shifting?

@VoR0220
Copy link
Member

VoR0220 commented May 16, 2016

So now that I have some understanding for this...it makes sense why this was left out. Basically bit shifting could be implemented natively by the underlying client....they would simply take in the opcodes that acted like shifting and then use bit shifting underneath....so this kind of makes sense to leave out now...I think.

@gcolvin
Copy link
Contributor

gcolvin commented May 30, 2016

@axic I agree that shifting and rotation should be included, as they are basic arithmetic that can be performed much more efficiently as one operation by the VM than as a sequence of operations by the programmer.

@ebuchman
Copy link

ebuchman commented Jul 2, 2016

this is especially true since the solidity ABI uses bit (well, byte) shifting, and every contract does this, but instead of being implemented as a shift, its a big int division by a massive number. shifts could be a major performance boon. it's a shame they've been left out. that said, maybe this is something the JITs can just do for us instead of fussing with new opcodes.

@ebuchman
Copy link

ebuchman commented Jul 2, 2016

this is potentially related to #101 , tho native contracts seem like overkill when all you're trying to do is shift bits/bytes

@axic
Copy link
Member Author

axic commented Aug 15, 2016

@VoR0220:

Basically bit shifting could be implemented natively by the underlying client....they would simply take in the opcodes that acted like shifting and then use bit shifting underneath....

Either static analysis is done or specific patterns need to be following for it to pick up. I don't think it is a good reason for leaving it out.

@chfast
Copy link
Member

chfast commented Aug 17, 2016

I think the conclusion is that shifts in form of a * (2 ** b) are unnecessary expensive and perform badly if the b is not a constant.

Providing there is strong interest in implementing some hash/crypto functions in EVM, I'm for including shift ops at some point.

@axic
Copy link
Member Author

axic commented Aug 30, 2016

Proposal

0xc: SHL - logical left shift
0xd: SHR - logical right shift

Arithmetic (SAR) right shift can be achieved as a combination of SHR and SIGNEXTEND. Not, because SIGNEXTEND needs the offset of the sign bit.

Both shift operators take two stack elements, with the top element being the index to shift with. Both take stack elements are considered unsigned.

It is aligned with the rest of EVM in case of overflows: they are ignored and the result is truncated.

@axic
Copy link
Member Author

axic commented Aug 30, 2016

I'm still unsure about including rotation opcodes. They can be very useful when implementing hash functions in EVM (such as SHA* and BLAKE).

@axic axic changed the title Bitwise shifting in EVM EVM opcodes: bitwise shifting Sep 2, 2016
@axic axic changed the title EVM opcodes: bitwise shifting Bitwise shifting in EVM Sep 2, 2016
@axic
Copy link
Member Author

axic commented Sep 2, 2016

Replaced by #145.

@axic axic closed this as completed Sep 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants