Skip to content

[2.0.0.0] implement negative indexes for OP_ROLL and OP_PICK

Griffith requested to merge ggriffith/nexa:OP_ROLL-OP_PICK-negative-numbers into dev

This was a bit annoying to think about because you have to break symmetry either between OP_PICK and OP_ROLL or between positive and negative numbers. There are two possible implementations with different properties. This MR currently implements 1. We should have some discussion on which we should ultimately use.

Note: Stack top is left, bottom is right for all examples below

  1. Symmetry between OP_PICK and OP_ROLL is maintained (the only difference is the presence of the original stackitem) but symmetry between positive and negative numbers is broken (OP_ROLL -1 is a no-op where as OP_ROLL 1 is not)

Note: moving/copying to last index for both OP_ROLL and OP_PICK would be OP_DEPTH OP_NEGATE OP_ROLL

OP_ROLL -1
stack 1 3 5 7 9
copy the top stackitem (1) insert the copy at index 1
1 1 3 5 7 9
remove the original stackitem
1 3 5 7 9
ultimately a no-op breaking symmetry with positive values

OP_PICK -1
stack 1 3 5 7 9
copy the top stackitem (1) and insert the copy at index 1
1 1 3 5 7 9

  1. Symmetry between positive and negative numbers for is maintained but symmetry between OP_ROLL and OP_PICK is broken (maybe) and inserts are off by 1 (inserts are after index N rather than before)

I say OP_ROLL and OP_PICK symmetry is broken here depending on the implementation because the difference between the two is no longer the absence/presence of a single value, the end ordering is different 3 1 5 vs 1 3 5

Note: With this implementation to move to the last/bottom item in the stack it is not OP_DEPTH OP_NEGATE OP_ROLL it would instead be OP_DEPTH OP_1SUB OP_NEGATE OP_ROLL same for OP_PICK depending on the implementation

OP_ROLL -1
stack 1 3 5 7 9
copy the top stackitem, remove the original (1)
stack 3 5 7 9
insert the copy at index 1
stack 3 1 5 7 9

OP_PICK -1
stack 1 3 5 7 9
copy the top stackitem (1) and insert copy at index 1
stack 1 1 3 5 7 9
OR
stack 1 3 5 7 9
copy the top stackitem, remove the original (1)
stack 3 5 7 9
insert the copy at index 1
stack 3 1 5 7 9
add the original back because OP_PICK is a copy not a move
stack 1 3 1 5 7 9

Edited by Griffith

Merge request reports