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

Implement bitwise shifting #251

Merged
merged 6 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/opFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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.

Copy link
Member

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)

},
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())
Copy link
Member

Choose a reason for hiding this comment

The 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
}
},
Copy link
Member

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#0x1d-sar-arithmetic-shift-right

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.

Copy link
Member Author

@axic axic Jul 24, 2018

Choose a reason for hiding this comment

The 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 (MAX_INTEGER) and making sure the bottom part where actual data remainded is 0.

// 0x20 range - crypto
SHA3: function (offset, length, runState) {
var data = memLoad(runState, offset, length)
Expand Down
3 changes: 3 additions & 0 deletions lib/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const codes = {
0x18: ['XOR', 3, 2, 1, false],
0x19: ['NOT', 3, 1, 1, false],
0x1a: ['BYTE', 3, 2, 1, false],
0x1b: ['SHL', 3, 2, 1, false],
0x1c: ['SHR', 3, 2, 1, false],
0x1d: ['SAR', 3, 2, 1, false],

// 0x20 range - crypto
0x20: ['SHA3', 30, 2, 1, false],
Expand Down
4 changes: 2 additions & 2 deletions lib/runCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ module.exports = function (opts, cb) {
if (result !== undefined) {
if (retNum !== 1) {
// opcode post-stack mismatch
return cb(VmError(ERROR.INTERNAL_ERROR))
return cb(new VmError(ERROR.INTERNAL_ERROR))
}

runState.stack.push(result)
} else {
if (!opInfo.async && retNum !== 0) {
// opcode post-stack mismatch
return cb(VmError(ERROR.INTERNAL_ERROR))
return cb(new VmError(ERROR.INTERNAL_ERROR))
}
}

Expand Down