btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

script.c (24186B)


      1 
      2 #include "script.h"
      3 #include "op.h"
      4 #include "script_num.h"
      5 #include "stack.h"
      6 #include "alloc.h"
      7 #include "valstack.h"
      8 #include "compiler.h"
      9 #include "sha256.h"
     10 #include "ripemd160.h"
     11 #include <stdio.h>
     12 #include <inttypes.h>
     13 
     14 /* #define SCRIPTERR(serr) script_add_error(c, opcode, serr) */
     15 #define SCRIPTERR(serr) { err = serr; goto evalerror; }
     16 
     17 int g_silence_script_err = 0;
     18 int g_silence_script_warn = 0;
     19 
     20 int script_add_error(int c, enum opcode cur_op, const char *serror) {
     21   // TODO: set_error
     22   if (!g_silence_script_err)
     23     fprintf(stderr, "error: %s @ op %d (%s)\n", serror, c, op_name(cur_op));
     24   return 0;
     25 }
     26 
     27 void script_add_warning(const char *warning) {
     28   // TODO: set_error
     29   if (!g_silence_script_warn)
     30     fprintf(stderr, "warning: %s\n", warning);
     31 }
     32 
     33 
     34 int
     35 cast_to_bool(struct val val) {
     36   // TODO: implement cast_to_bool
     37   switch (val.type) {
     38   case VT_SMALLINT:
     39     return val.ind != 0;
     40   case VT_SCRIPTNUM: {
     41     struct num *sn = num_pool_get(val.ind);
     42     return sn->val != 0;
     43   }
     44   case VT_DATA: {
     45     u32 len;
     46     const u8 * bytes = byte_pool_get(val.ind, &len);
     47     return *bytes != 0;
     48   }
     49   }
     50   assert(!"Unhandled val.type in cast_to_bool");
     51 }
     52 
     53 int script_getop(const u8 **p, const u8 *end, enum opcode *popcode, u8 *buf,
     54 		 int bufsize, u32 *outlen)
     55 {
     56   *popcode = OP_INVALIDOPCODE;
     57   u32 nsize = 0;
     58 
     59   int opcode;
     60 
     61   if (buf)
     62     memset(buf, 0, bufsize);
     63 
     64   opcode = **p;
     65   *p += 1;
     66 
     67   if (opcode <= OP_PUSHDATA4) {
     68     if (opcode < OP_PUSHDATA1) {
     69       nsize = opcode;
     70     }
     71     else {
     72       switch (opcode) {
     73       case OP_PUSHDATA1:
     74         if ((end - *p) < 1) {
     75           return 0;
     76         }
     77         nsize = **p;
     78         (*p)++;
     79         break;
     80       case OP_PUSHDATA2:
     81         if ((end - *p) < 2) {
     82           return 0;
     83         }
     84         nsize = **(u16**)p;
     85         *p += 2;
     86         break;
     87       case OP_PUSHDATA4:
     88         if ((end - *p) < 4) {
     89           return 0;
     90         }
     91         nsize = **(u32**)p;
     92         *p += 4;
     93         break;
     94       default:
     95         break;
     96       }
     97     }
     98 
     99     if ((end - *p) < 0 || (end - *p) < nsize) {
    100       return 0;
    101     }
    102 
    103     if (buf) {
    104       *outlen = nsize;
    105       memcpy(buf, *p, nsize);
    106     }
    107     *p += nsize;
    108   }
    109 
    110   *popcode = opcode;
    111 
    112   return 1;
    113 }
    114 
    115 
    116 int
    117 script_eval(const u8 *script, size_t script_size, struct stack *stack,
    118             struct result *result) {
    119   int op_count = 0;
    120   u32 tmplen = 0;
    121   char *err = NULL;
    122   const u8 *p = script;
    123   const u8 *top = script + script_size;
    124   static char tmpbuf[32];
    125   static u8 tmpbytes[MAX_SCRIPT_ELEMENT_SIZE];
    126   static u8 tmpbytes2[MAX_SCRIPT_ELEMENT_SIZE];
    127   enum opcode opcode;
    128   struct val val_true = val_from_int(1);
    129   struct val val_false = val_from_int(0);
    130   static const struct num bn_one  = {.val = 1, .ind = -1};
    131   static const struct num bn_zero = {.val = 0, .ind = -1};
    132   struct val val;
    133   struct stack _altstack;
    134   struct stack _ifstack;
    135   struct stack *altstack = &_altstack;
    136   struct stack *ifstack = &_ifstack;
    137   int flags = CO_WARNINGS_ARE_ERRORS | CO_WARN_MINIMAL;
    138   int c = 0;
    139   // TODO: require minimal?
    140   int require_minimal =
    141     !(flags & ~(CO_WARNINGS_ARE_ERRORS | CO_WARN_MINIMAL));
    142   stack_init(altstack);
    143   stack_init(ifstack);
    144 
    145   while (p < top) {
    146     c++;
    147     script_getop(&p, top, &opcode, tmpbytes, sizeof(tmpbytes), &tmplen);
    148     int if_exec = !stack_any_val(ifstack, falseval);
    149 
    150     // Note OP_RESERVED does not count towards the opcode limit.
    151     if (opcode > OP_16 && ++op_count > MAX_OPS_PER_SCRIPT)
    152       SCRIPTERR("MAX_OPS_PER_SCRIPT");
    153 
    154     if (opcode == OP_CAT ||
    155         opcode == OP_SUBSTR ||
    156         opcode == OP_LEFT ||
    157         opcode == OP_RIGHT ||
    158         opcode == OP_INVERT ||
    159         opcode == OP_AND ||
    160         opcode == OP_OR ||
    161         opcode == OP_XOR ||
    162         opcode == OP_2MUL ||
    163         opcode == OP_2DIV ||
    164         opcode == OP_MUL ||
    165         opcode == OP_DIV ||
    166         opcode == OP_MOD ||
    167         opcode == OP_LSHIFT ||
    168         opcode == OP_RSHIFT) {
    169 
    170       SCRIPTERR("DISABLED_OPCODE"); // Disabled opcodes.
    171     }
    172 
    173     if (if_exec && opcode <= OP_PUSHDATA4) {
    174       /* if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode)) { */
    175       /*   return set_error(serror, MINIMALDATA); */
    176       /* } */
    177       stack_push_raw(stack, tmpbytes, tmplen);
    178     } else if (if_exec || (OP_IF <= opcode && opcode <= OP_ENDIF))
    179     switch (opcode) {
    180     case OP_1NEGATE:
    181     case OP_1:
    182     case OP_2:
    183     case OP_3:
    184     case OP_4:
    185     case OP_5:
    186     case OP_6:
    187     case OP_7:
    188     case OP_8:
    189     case OP_9:
    190     case OP_10:
    191     case OP_11:
    192     case OP_12:
    193     case OP_13:
    194     case OP_14:
    195     case OP_15:
    196     case OP_16:
    197     {
    198       struct num sn;
    199       sn_from_int((int)opcode - (int)(OP_1 - 1), &sn);
    200       struct val val = sn_to_val(&sn);
    201       stack_push_val(stack, val);
    202     }
    203     break;
    204 
    205     case OP_NOP: break;
    206 
    207     case OP_NOP1: case OP_NOP4: case OP_NOP5:
    208     case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
    209       {
    210         /* if (script->flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) */
    211         script_add_warning("DISCOURAGE_UPGRADABLE_NOPS");
    212       }
    213       break;
    214 
    215     case OP_IF:
    216     case OP_NOTIF:
    217     {
    218         // <expression> if [statements] [else [statements]] endif
    219         int ifval = 0;
    220         if (if_exec)
    221         {
    222             if (stack_size(stack) < 1)
    223               SCRIPTERR("UNBALANCED_CONDITIONAL");
    224             struct val val = stack_top_val(stack, -1);
    225             // TODO: minimal if?
    226             // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2016-August/013014.html
    227             /* if (sigversion == SIGVERSION_WITNESS_V0 && (flags & SCRIPT_VERIFY_MINIMALIF)) { */
    228             /*     if (vch.size() > 1) */
    229             /*         SCRIPTERR(MINIMALIF); */
    230             /*     if (vch.size() == 1 && vch[0] != 1) */
    231             /*         SCRIPTERR(MINIMALIF); */
    232             /* } */
    233             ifval = cast_to_bool(val);
    234             if (opcode == OP_NOTIF)
    235                 ifval = !ifval;
    236             stack_pop(stack);
    237         }
    238         stack_push_val(ifstack, smallintval(ifval));
    239     }
    240     break;
    241 
    242     case OP_ELSE:
    243     {
    244       if (stack_size(ifstack) == 0)
    245         SCRIPTERR("UNBALANCED_CONDITIONAL");
    246       struct val v = stack_pop_val(ifstack);
    247       assert(v.type == VT_SMALLINT);
    248       v.ind = !v.ind;
    249       stack_push_val(ifstack, v);
    250     }
    251     break;
    252 
    253     case OP_ENDIF:
    254     {
    255       if (stack_size(ifstack) == 0)
    256         SCRIPTERR("UNBALANCED_CONDITIONAL");
    257       stack_pop(ifstack);
    258     }
    259     break;
    260 
    261     case OP_RETURN:
    262     {
    263       SCRIPTERR("OP_RETURN");
    264     }
    265     break;
    266 
    267     case OP_INVALIDOPCODE:
    268     {
    269       SCRIPTERR("INVALID_OPCODE");
    270     }
    271     break;
    272 
    273     case OP_FROMALTSTACK:
    274     {
    275         if (stack_size(altstack) < 1)
    276           SCRIPTERR("INVALID_ALTSTACK_OPERATION");
    277         stack_push(stack, stack_top(altstack, -1));
    278         stack_pop(altstack);
    279     }
    280     break;
    281 
    282     case OP_2DROP:
    283     {
    284         // (x1 x2 -- )
    285         if (stack_size(stack) < 2)
    286           SCRIPTERR("INVALID_STACK_OPERATION");
    287         stack_pop(stack);
    288         stack_pop(stack);
    289     }
    290     break;
    291 
    292     case OP_2DUP:
    293     {
    294         // (x1 x2 -- x1 x2 x1 x2)
    295         if (stack_size(stack) < 2)
    296           SCRIPTERR("INVALID_STACK_OPERATION");
    297         struct val v1 = stack_top_val(stack, -2);
    298         struct val v2 = stack_top_val(stack, -1);
    299         stack_push_val(stack, val_copy(v1));
    300         stack_push_val(stack, val_copy(v2));
    301     }
    302     break;
    303 
    304     case OP_3DUP:
    305     {
    306         // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
    307         if (stack_size(stack) < 3)
    308           SCRIPTERR("INVALID_STACK_OPERATION");
    309         struct val v1 = stack_top_val(stack, -3);
    310         struct val v2 = stack_top_val(stack, -2);
    311         struct val v3 = stack_top_val(stack, -1);
    312         stack_push_val(stack, val_copy(v1));
    313         stack_push_val(stack, val_copy(v2));
    314         stack_push_val(stack, val_copy(v3));
    315     }
    316     break;
    317 
    318     case OP_2OVER:
    319     {
    320         // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
    321         if (stack_size(stack) < 4)
    322           SCRIPTERR("INVALID_STACK_OPERATION");
    323         struct val v1 = stack_top_val(stack, -4);
    324         struct val v2 = stack_top_val(stack, -3);
    325         stack_push_val(stack, val_copy(v1));
    326         stack_push_val(stack, val_copy(v2));
    327     }
    328     break;
    329 
    330     case OP_2ROT:
    331     {
    332         // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
    333         if (stack_size(stack) < 6)
    334           SCRIPTERR("INVALID_STACK_OPERATION");
    335         struct val t6 = stack_top_val(stack, -6);
    336         struct val t5 = stack_top_val(stack, -5);
    337         *(stack->top - 6) = *(stack->top - 4);
    338         *(stack->top - 5) = *(stack->top - 3);
    339         *(stack->top - 4) = *(stack->top - 2);
    340         *(stack->top - 3) = *(stack->top - 1);
    341         stack_set_val(stack, -2, t6);
    342         stack_set_val(stack, -1, t5);
    343     }
    344     break;
    345 
    346     case OP_2SWAP:
    347     {
    348         // (x1 x2 x3 x4 -- x3 x4 x1 x2)
    349         if (stack_size(stack) < 4)
    350           SCRIPTERR("INVALID_STACK_OPERATION");
    351 
    352         struct val t4 = stack_top_val(stack, -4);
    353         struct val t3 = stack_top_val(stack, -3);
    354 
    355         *(stack->top - 4) = *(stack->top - 2);
    356         *(stack->top - 3) = *(stack->top - 1);
    357         stack_set_val(stack, -2, t4);
    358         stack_set_val(stack, -1, t3);
    359     }
    360     break;
    361 
    362     case OP_IFDUP:
    363     {
    364         // (x - 0 | x x)
    365         if (stack_size(stack) < 1)
    366           SCRIPTERR("INVALID_STACK_OPERATION");
    367         struct val v = stack_top_val(stack, -1);
    368         if (cast_to_bool(v))
    369             stack_push_val(stack, val_copy(v));
    370     }
    371     break;
    372 
    373     case OP_DEPTH:
    374     {
    375         // -- stacksize
    376       struct num sn;
    377       sn_from_int(stack_size(stack), &sn);
    378       struct val v = sn_to_val(&sn);
    379       stack_push_val(stack, v);
    380     }
    381     break;
    382 
    383     case OP_DROP:
    384     {
    385       // (x -- )
    386       if (stack_size(stack) < 1)
    387         SCRIPTERR("INVALID_STACK_OPERATION");
    388       stack_pop(stack);
    389     }
    390     break;
    391 
    392     case OP_DUP:
    393     {
    394         // (x -- x x)
    395         if (stack_size(stack) < 1)
    396           SCRIPTERR("INVALID_STACK_OPERATION");
    397         struct val val = stack_top_val(stack, (-1));
    398         stack_push_val(stack, val_copy(val));
    399     }
    400     break;
    401 
    402     case OP_NIP:
    403     {
    404         // (x1 x2 -- x2)
    405         if (stack_size(stack) < 2)
    406           SCRIPTERR("INVALID_STACK_OPERATION");
    407         // TODO: sanity check - stack_size() == stack_end(stack)
    408         stack_set_val(stack, -2, stack_top_val(stack, -1));
    409         stack_pop(stack);
    410     }
    411     break;
    412 
    413     case OP_OVER:
    414     {
    415         // (x1 x2 -- x1 x2 x1)
    416         if (stack_size(stack) < 2)
    417           SCRIPTERR("INVALID_STACK_OPERATION");
    418         struct val val = stack_top_val(stack, (-2));
    419         stack_push_val(stack, val_copy(val));
    420     }
    421     break;
    422 
    423 
    424     case OP_PICK:
    425     case OP_ROLL:
    426     {
    427         // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
    428         // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
    429         if (stack_size(stack) < 2)
    430             SCRIPTERR("INVALID_STACK_OPERATION");
    431         struct num *n;
    432 
    433         enum sn_result res =
    434           sn_from_val(stack_top_val(stack, -1), &n);
    435 
    436         if (res != SN_SUCCESS) {
    437           sprintf(tmpbuf, "invalid scriptnum %d", res);
    438           SCRIPTERR(tmpbuf);
    439         }
    440 
    441         stack_pop(stack);
    442         if (n->val < 0 || n->val >= (int)stack_size(stack))
    443             SCRIPTERR("INVALID_STACK_OPERATION");
    444         struct val val = stack_top_val(stack, (-(n->val))-1);
    445         if (opcode == OP_ROLL)
    446           stack_erase(stack, -(n->val) - 1);
    447         stack_push_val(stack, val);
    448     }
    449     break;
    450 
    451     case OP_ROT:
    452     {
    453         // (x1 x2 x3 -- x2 x3 x1)
    454         //  x2 x1 x3  after first swap
    455         //  x2 x3 x1  after second swap
    456         if (stack_size(stack) < 3)
    457             SCRIPTERR("INVALID_STACK_OPERATION");
    458         stack_swap(stack, -3, -2);
    459         stack_swap(stack, -2, -1);
    460     }
    461     break;
    462 
    463     case OP_SWAP:
    464     {
    465       // (x1 x2 -- x2 x1)
    466       if (stack_size(stack) < 2)
    467         SCRIPTERR("INVALID_STACK_OPERATION");
    468       stack_swap(stack, -2, -1);
    469     }
    470     break;
    471 
    472     case OP_TUCK:
    473     {
    474         // (x1 x2 -- x2 x1 x2)
    475         if (stack_size(stack) < 2)
    476             SCRIPTERR("INVALID_STACK_OPERATION");
    477         struct val val = stack_top_val(stack, -1);
    478         stack_swap(stack, -2, -1);
    479         stack_push_val(stack, val_copy(val));
    480     }
    481     break;
    482 
    483 
    484     case OP_SIZE:
    485     {
    486         // (in -- in size)
    487         if (stack_size(stack) < 1)
    488             SCRIPTERR("INVALID_STACK_OPERATION");
    489         struct val val = stack_top_val(stack, -1);
    490         u32 size = val_size(val);
    491         val = val_from_int(size);
    492         stack_push_val(stack, val);
    493     }
    494     break;
    495 
    496 
    497     //
    498     // Bitwise logic
    499     //
    500     case OP_EQUAL:
    501     case OP_EQUALVERIFY:
    502     //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL
    503     {
    504         // (x1 x2 - bool)
    505         if (stack_size(stack) < 2)
    506             SCRIPTERR("INVALID_STACK_OPERATION");
    507         struct val v1 = stack_top_val(stack, -2);
    508         struct val v2 = stack_top_val(stack, -1);
    509         int equal = val_eq(v1, v2);
    510         // OP_NOTEQUAL is disabled because it would be too easy to say
    511         // something like n != 1 and have some wiseguy pass in 1 with extra
    512         // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
    513         //if (opcode == OP_NOTEQUAL)
    514         //    fEqual = !fEqual;
    515         stack_pop(stack);
    516         stack_pop(stack);
    517         stack_push_val(stack, equal ? val_true : val_false);
    518         if (opcode == OP_EQUALVERIFY)
    519         {
    520             if (equal)
    521                 stack_pop(stack);
    522             else
    523                 SCRIPTERR("EQUALVERIFY");
    524         }
    525     }
    526     break;
    527 
    528 
    529     //
    530     // Numeric
    531     //
    532     case OP_1ADD:
    533     case OP_1SUB:
    534     case OP_NEGATE:
    535     case OP_ABS:
    536     case OP_NOT:
    537     case OP_0NOTEQUAL:
    538     {
    539         // (in -- out)
    540         if (stack_size(stack) < 1)
    541             SCRIPTERR("INVALID_STACK_OPERATION");
    542         struct num *bn;
    543         enum sn_result res =
    544           sn_from_val(stack_top_val(stack, -1), &bn);
    545 
    546         if (res != SN_SUCCESS) {
    547           sprintf(tmpbuf, "invalid scriptnum %d", res);
    548           SCRIPTERR(tmpbuf);
    549         }
    550 
    551         switch (opcode)
    552         {
    553         case OP_1ADD:       bn->val += 1; break;
    554         case OP_1SUB:       bn->val -= 1; break;
    555         case OP_NEGATE:     bn->val = -bn->val; break;
    556         case OP_ABS:
    557           if (bn->val < bn_zero.val)
    558             bn->val = -(bn->val);
    559           break;
    560         case OP_NOT:        bn->val = (bn->val == 0); break;
    561         case OP_0NOTEQUAL:  bn->val = (bn->val != 0); break;
    562         default:            assert(!"invalid opcode"); break;
    563         }
    564         stack_pop(stack);
    565         stack_push_val(stack, sn_to_val(bn));
    566     }
    567     break;
    568 
    569     case OP_ADD:
    570     case OP_SUB:
    571     case OP_BOOLAND:
    572     case OP_BOOLOR:
    573     case OP_NUMEQUAL:
    574     case OP_NUMEQUALVERIFY:
    575     case OP_NUMNOTEQUAL:
    576     case OP_LESSTHAN:
    577     case OP_GREATERTHAN:
    578     case OP_LESSTHANOREQUAL:
    579     case OP_GREATERTHANOREQUAL:
    580     case OP_MIN:
    581     case OP_MAX:
    582     {
    583         // (x1 x2 -- out)
    584         if (stack_size(stack) < 2)
    585             SCRIPTERR("INVALID_STACK_OPERATION");
    586         struct num *bn1, *bn2, bn;
    587         enum sn_result res;
    588         bn.ind = -1;
    589         res = sn_from_val(stack_top_val(stack, -2), &bn1);
    590         if (res == SN_ERR_OVERFLOWED_INT)
    591           SCRIPTERR("SCRIPT_INT_OVERFLOW");
    592         res = sn_from_val(stack_top_val(stack, -1), &bn2);
    593         if (res == SN_ERR_OVERFLOWED_INT)
    594           SCRIPTERR("SCRIPT_INT_OVERFLOW");
    595         /* struct num bn(0); */
    596         switch (opcode)
    597         {
    598         case OP_ADD:
    599           bn.val = bn1->val + bn2->val;
    600           break;
    601 
    602         case OP_SUB:
    603             bn.val = bn1->val - bn2->val;
    604             break;
    605 
    606         case OP_BOOLAND:             bn.val = bn1->val != 0 && bn2->val != 0;
    607           break;
    608         case OP_BOOLOR:              bn.val = bn1->val != 0 || bn2->val != 0;
    609           break;
    610         case OP_NUMEQUAL:            bn.val = bn1->val == bn2->val; break;
    611         case OP_NUMEQUALVERIFY:      bn.val = bn1->val == bn2->val; break;
    612         case OP_NUMNOTEQUAL:         bn.val = bn1->val != bn2->val; break;
    613         case OP_LESSTHAN:            bn.val = bn1->val < bn2->val; break;
    614         case OP_GREATERTHAN:         bn.val = bn1->val > bn2->val; break;
    615         case OP_LESSTHANOREQUAL:     bn.val = bn1->val <= bn2->val; break;
    616         case OP_GREATERTHANOREQUAL:  bn.val = bn1->val >= bn2->val; break;
    617         case OP_MIN:
    618           bn.val = bn1->val < bn2->val ? bn1->val : bn2->val;
    619           break;
    620         case OP_MAX:
    621           bn.val = bn1->val > bn2->val ? bn1->val : bn2->val;
    622           break;
    623         default:                     assert(!"invalid opcode"); break;
    624         }
    625         stack_pop(stack);
    626         stack_pop(stack);
    627         struct val bnval = sn_to_val(&bn);
    628         stack_push_val(stack, bnval);
    629 
    630         if (opcode == OP_NUMEQUALVERIFY)
    631         {
    632             if (cast_to_bool(stack_top_val(stack, -1)))
    633                 stack_pop(stack);
    634             else
    635                 SCRIPTERR("NUMEQUALVERIFY");
    636         }
    637     }
    638     break;
    639 
    640     case OP_WITHIN:
    641     {
    642         // (x min max -- out)
    643         if (stack_size(stack) < 3)
    644             SCRIPTERR("INVALID_STACK_OPERATION");
    645         struct num *bn1, *bn2, *bn3;
    646         sn_from_val(stack_top_val(stack, -3), &bn1);
    647         sn_from_val(stack_top_val(stack, -2), &bn2);
    648         sn_from_val(stack_top_val(stack, -1), &bn3);
    649         int fval = bn2->val <= bn1->val && bn1->val < bn3->val;
    650         stack_pop(stack);
    651         stack_pop(stack);
    652         stack_pop(stack);
    653         stack_push_val(stack, fval ? val_true : val_false);
    654     }
    655     break;
    656 
    657     case OP_RIPEMD160:
    658     case OP_SHA1:
    659     case OP_SHA256:
    660     case OP_HASH160:
    661     case OP_HASH256:
    662     {
    663       // (in -- hash)
    664       if (stack_size(stack) < 1)
    665         SCRIPTERR("script_err_invalid_stack_operation");
    666 
    667       u16 hashind;
    668       u32 valsize;
    669       u32 hashlen =
    670         (opcode == OP_RIPEMD160 || opcode == OP_SHA1 ||
    671          opcode == OP_HASH160) ? 20 : 32;
    672 
    673       struct val v = stack_top_val(stack, -1);
    674 
    675       val_bytes(v, &valsize, tmpbytes, sizeof(tmpbytes));
    676       u8 *hash = byte_pool_new(hashlen, &hashind);
    677 
    678       if (opcode == OP_RIPEMD160) {
    679         ripemd160((struct ripemd160*)hash, tmpbytes, valsize);
    680       }
    681       else if (opcode == OP_SHA256) {
    682         sha256((struct sha256*)hash, tmpbytes, valsize);
    683       }
    684       else if (opcode == OP_HASH160) {
    685         sha256((struct sha256*)tmpbytes2, tmpbytes, valsize);
    686         ripemd160((struct ripemd160*)hash, tmpbytes2, 32);
    687       }
    688       else
    689         SCRIPTERR("unhandled hash opcode");
    690       /*   cripemd160().write(vch.data(), vch.size()).finalize(vchhash.data()); */
    691       /* else if (opcode == op_sha1) */
    692       /*   csha1().write(vch.data(), vch.size()).finalize(vchhash.data()); */
    693       /* else if (opcode == op_sha256) */
    694       /*   csha256().write(vch.data(), vch.size()).finalize(vchhash.data()); */
    695       /* else if (opcode == op_hash256) */
    696       /*   chash256().write(vch.data(), vch.size()).finalize(vchhash.data()); */
    697 
    698       v.ind = hashind;
    699       v.type = VT_RAW;
    700 
    701       stack_pop(stack);
    702       stack_push_val(stack, v);
    703     }
    704     break;
    705 
    706     default: {
    707       SCRIPTERR("UNHANDLED_OPCODE");
    708     }
    709 
    710     }
    711   }
    712 
    713   if (stack_size(ifstack) != 0)
    714     SCRIPTERR("UNBALANCED_CONDITIONAL");
    715 
    716 
    717 evalerror:
    718   result->error = err;
    719   result->op_count = c;
    720   result->last_op = opcode;
    721 
    722   stack_free(altstack);
    723   stack_free(ifstack);
    724   return !err;
    725 }
    726 
    727 
    728 static char hexchar(unsigned int val)
    729 {
    730 	if (val < 10)
    731 		return '0' + val;
    732 	if (val < 16)
    733 		return 'a' + val - 10;
    734 	assert(!"hexchar invalid val");
    735 }
    736 
    737 static void hex_print(const u8 *buf, size_t bufsize) {
    738 	for (size_t i = 0; i < bufsize; i++) {
    739 		unsigned int c = buf[i];
    740 		printf("%c%c", hexchar(c >> 4), hexchar(c & 0xF) );
    741 	}
    742 }
    743 
    744 
    745 static bool is_push_data(enum opcode op)
    746 {
    747 	return (op > OP_0 && op < OP_PUSHDATA1) ||
    748 		op == OP_PUSHDATA1 ||
    749 		op == OP_PUSHDATA2 ||
    750 		op == OP_PUSHDATA4;
    751 }
    752 
    753 void script_print(const u8 *script, size_t script_size, int abbrev_data) {
    754   u32 len;
    755   static u8 tmpbuf[4096];
    756   const u8 *p = script;
    757   const u8 *top = script + script_size;
    758   bool first = true;
    759 
    760   while (p < top) {
    761     enum opcode opcode;
    762     script_getop(&p, top, &opcode, tmpbuf, sizeof(tmpbuf), &len);
    763 
    764     if (!first)
    765 	    putchar(' ');
    766     first = false;
    767 
    768     if (is_push_data(opcode)) {
    769 	    if (abbrev_data)
    770 		    printf("data(%d)", len);
    771 	    else
    772 		    hex_print(tmpbuf, len);
    773     }
    774     else
    775 	    printf("%s", op_name(opcode));
    776 
    777     len = 0;
    778   }
    779 }
    780 
    781 void script_print_vals(struct stack *stack) {
    782   void **p = stack->bottom;
    783   bool first = true;
    784   while (p < stack->top) {
    785     struct val val;
    786     memcpy(&val, &*p++, sizeof(struct val));
    787     if (first)
    788 	    first = false;
    789     else
    790 	    printf(" ");
    791     val_print(val);
    792   }
    793   putchar('\n');
    794 }
    795 
    796 void
    797 script_push_int(struct stack *script, s64 intval) {
    798   /* u16 len; */
    799   /* u16 i; */
    800   /* static u8 buf[8]; */
    801   struct val val = val_from_int(intval);
    802   stack_push_val(script, val);
    803   /* val_serialize(val, &len, buf, ARRAY_SIZE(buf)); */
    804   /* for (i = 0; i < len; ++i) */
    805   /*   stack_push_small(u8, script, &buf[i]); */
    806 }
    807 
    808 
    809 void
    810 script_push_str(struct stack *script, const char *str) {
    811   struct val val;
    812   u16 ind;
    813   u8 *bytes;
    814   bytes = byte_pool_new(strlen(str), &ind);
    815   strcpy((char*)bytes, str);
    816 
    817   val.type = VT_DATA;
    818   val.ind  = ind;
    819 
    820   stack_push_val(script, val);
    821 }
    822 
    823 void
    824 script_push_datastr(struct stack *script, const char *str, int raw) {
    825   int count = 0;
    826   u8 *bytes;
    827   const char *p = str;
    828   u16 ind;
    829   u16 nbytes = strlen(str) / 2;
    830   struct val val;
    831 
    832   bytes = byte_pool_new(nbytes, &ind);
    833 
    834   /* WARNING: no sanitization or error-checking whatsoever */
    835   for(count = 0; count < nbytes; count++) {
    836     sscanf(p, "%2hhx", &bytes[count]);
    837     p += 2;
    838   }
    839 
    840   val.type = raw ? VT_RAW : VT_DATA;
    841   val.ind  = ind;
    842 
    843   stack_push_val(script, val);
    844 }
    845 
    846 
    847 void
    848 script_push_raw(struct stack *stack, const char *data) {
    849   script_push_datastr(stack, data, 1);
    850 }
    851 
    852 void script_serialize_data(struct val val, u32 *len, u8 *buf) {
    853   u8 *p;
    854   p = byte_pool_get(val.ind, len);
    855   if (*len < OP_PUSHDATA1) {
    856     *buf++ = *len;
    857     memcpy(buf, p, *len);
    858     *len += 1;
    859   }
    860   else if (*len <= 0xFF) {
    861     *buf++ = OP_PUSHDATA1;
    862     *buf++ = *len;
    863     memcpy(buf, p, *len);
    864     *len += 2;
    865   }
    866   else if (*len <= 0xFFFF) {
    867     *buf++ = OP_PUSHDATA2;
    868     u16 *sp = (u16*)buf;
    869     // TODO: writele16
    870     *sp = *len;
    871     buf += 2;
    872     memcpy(buf, p, *len);
    873     *len += 3;
    874   }
    875   else {
    876     *buf++ = OP_PUSHDATA4;
    877     u32 *ip = (u32*)buf;
    878     // TODO: writele32
    879     *ip = *len;
    880     buf += 4;
    881     memcpy(buf, p, *len);
    882     *len += 5;
    883   }
    884   return;
    885 }
    886 
    887 void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len)
    888 {
    889   struct val *valp;
    890   void **sp;
    891   u8 *p = buf;
    892   u32 valsize = 0;
    893   *len = 0;
    894   sp = stack->bottom;
    895 
    896   while (sp < stack->top) {
    897     /* printf("%02x %02x %02x %02x | ", huh[0], huh[1], huh[2], huh[3]); */
    898     /* printf("%d %d\n", val.type, val.ind); */
    899     valp = (struct val*)sp;
    900 
    901     switch (valp->type) {
    902     case VT_SCRIPTNUM: {
    903       struct num *sn = num_pool_get(valp->ind);
    904 
    905       if (sn->val == -1) {
    906         valsize = 1;
    907         *p = OP_1NEGATE;
    908       }
    909       else if (sn->val == 0 ) {
    910         valsize = 1;
    911         *p = 0;
    912       }
    913       else if (sn->val >= 1 && sn->val <= 16 ) {
    914         valsize = 1;
    915         *p = OP_1 - 1 + sn->val;
    916       }
    917       else {
    918         val_serialize(*valp, &valsize, p+1, buflen-((p+1)-buf));
    919         *p = (u8)valsize;
    920         valsize++;
    921       }
    922       break;
    923     }
    924     case VT_DATA:
    925       script_serialize_data(*valp, &valsize, p);
    926       break;
    927     default:
    928       val_serialize(*valp, &valsize, p, buflen-(p-buf));
    929     }
    930 
    931     p += valsize;
    932     *len += valsize;
    933     assert(p-buf <= buflen);
    934     sp++;
    935   }
    936 
    937 }
    938 
    939 void stack_serialize(struct stack *stack, u8 *buf, int buflen, int *len) {
    940   struct val *valp;
    941   void **sp;
    942   u8 *p = buf;
    943   u32 valsize;
    944   *len = 0;
    945   sp = stack->bottom;
    946 
    947   while (sp < stack->top) {
    948     /* printf("%02x %02x %02x %02x | ", huh[0], huh[1], huh[2], huh[3]); */
    949     /* printf("%d %d\n", val.type, val.ind); */
    950     valp = (struct val*)sp;
    951 
    952     val_serialize(*valp, &valsize, p, buflen-(p-buf));
    953 
    954     p += valsize;
    955     *len += valsize;
    956     assert(p-buf <= buflen);
    957     sp++;
    958   }
    959 }
    960 
    961 void
    962 script_handle_input(struct stack *stack UNUSED, const char *str UNUSED) {
    963 }