-
Notifications
You must be signed in to change notification settings - Fork 757
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
Implement bitwise shifting #251
Changes from all commits
85aad21
ed6b9dc
6218bc6
4c64288
b90ba6e
d843cd2
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 |
---|---|---|
|
@@ -157,6 +157,45 @@ module.exports = { | |
|
||
return new BN(word.shrn((31 - pos.toNumber()) * 8).andln(0xff)) | ||
}, | ||
SHL: function (a, b, runState) { | ||
if (!runState._common.gteHardfork('constantinople')) { | ||
trap(ERROR.INVALID_OPCODE) | ||
} | ||
if (a.gten(256)) { | ||
return new BN(0) | ||
} | ||
return b.shln(a.toNumber()).iand(utils.MAX_INTEGER) | ||
}, | ||
SHR: function (a, b, runState) { | ||
if (!runState._common.gteHardfork('constantinople')) { | ||
trap(ERROR.INVALID_OPCODE) | ||
} | ||
if (a.gten(256)) { | ||
return new BN(0) | ||
} | ||
return b.shrn(a.toNumber()) | ||
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. |
||
}, | ||
SAR: function (a, b, runState) { | ||
if (!runState._common.gteHardfork('constantinople')) { | ||
trap(ERROR.INVALID_OPCODE) | ||
} | ||
const isSigned = b.testn(255) | ||
if (a.gten(256)) { | ||
if (isSigned) { | ||
return new BN(utils.MAX_INTEGER) | ||
} else { | ||
return new BN(0) | ||
} | ||
} | ||
const c = b.shrn(a.toNumber()) | ||
if (isSigned) { | ||
const shiftedOutWidth = 255 - a.toNumber() | ||
const mask = utils.MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth) | ||
return c.ior(mask) | ||
} else { | ||
return c | ||
} | ||
}, | ||
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. I also think I can roughly follow what is going on here and think all EIP conditions are covered and can also place all - this to me appearing a bit esoteric - mask stuff to the places in the EIP. Would have to do an extra hour of bit operation reading though for a better understanding. Since tests are passing I will approve. 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. Here the point is to fill the top with set bits, that is achieved by ORing the mask. The mask is then created by taking an all bits set input ( |
||
// 0x20 range - crypto | ||
SHA3: function (offset, length, runState) { | ||
var data = memLoad(runState, offset, length) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#0x1b-shl-shift-left
I am not super-fluent on bit operations, but I think I have found all conditions from the EIP in the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(always a bit shaky on this signed/unsigned stuff)