OP_PICK should take the nth element from the back of the stack and take it on top:
for input
xn ... x2 x1 x0
gives
xn ... x2 x1 x0 xn
I have used it in some scripts, but I've noticed some strange behaviour.
For instance, using the tool in:
http://webbtc.com/script
an input script
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
and an output script
19 OP_PICK
produces an error.
However 18 OP_PICK works. For 25 elements in the stack it should work in both cases.
The problem doesn't seem to be the number 19. If I add one input
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3
the scripts works fine.
I have tried looking at the source code, but in https://github.com/bitcoin/bitcoin/blob/ce56f5621a94dcc2159ebe57e43da727eab18e6c/src/script/interpreter.cpp around line 551 I find:
case OP_OVER:
{
// (x1 x2 -- x1 x2 x1)
if (stack.size() < 2)
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
valtype vch = stacktop(-2);
stack.push_back(vch);
}
break;
case OP_PICK:
case OP_ROLL:
{
// (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
if (stack.size() < 2)
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
int n = CScriptNum(stacktop(-1), fRequireMinimal).getint();
popstack(stack);
I'm not really sure how OP_PICK is implemented. Can anyone point me to the code for OP_PICK or explain the errors?