btcs

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

op.c (13369B)


      1 
      2 #include "script.h"
      3 #include "oplookup.h"
      4 #include "op.h"
      5 #include "alloc.h"
      6 #include "misc.h"
      7 
      8 #include <inttypes.h>
      9 #include <stdio.h>
     10 #include <string.h>
     11 #include <ctype.h>
     12 
     13 
     14 
     15 static enum opcode
     16 op_from_token(enum opcode_token opcode) {
     17   switch (opcode) {
     18   case _OP_0: return OP_0;
     19   case _OP_FALSE: return OP_FALSE;
     20   case _OP_PUSHDATA1: return OP_PUSHDATA1;
     21   case _OP_PUSHDATA2: return OP_PUSHDATA2;
     22   case _OP_PUSHDATA4: return OP_PUSHDATA4;
     23   case _OP_1NEGATE: return OP_1NEGATE;
     24   case _OP_RESERVED: return OP_RESERVED;
     25   case _OP_1: return OP_1;
     26   case _OP_TRUE: return OP_TRUE;
     27   case _OP_2: return OP_2;
     28   case _OP_3: return OP_3;
     29   case _OP_4: return OP_4;
     30   case _OP_5: return OP_5;
     31   case _OP_6: return OP_6;
     32   case _OP_7: return OP_7;
     33   case _OP_8: return OP_8;
     34   case _OP_9: return OP_9;
     35   case _OP_10: return OP_10;
     36   case _OP_11: return OP_11;
     37   case _OP_12: return OP_12;
     38   case _OP_13: return OP_13;
     39   case _OP_14: return OP_14;
     40   case _OP_15: return OP_15;
     41   case _OP_16: return OP_16;
     42   case _OP_NOP: return OP_NOP;
     43   case _OP_VER: return OP_VER;
     44   case _OP_IF: return OP_IF;
     45   case _OP_NOTIF: return OP_NOTIF;
     46   case _OP_VERIF: return OP_VERIF;
     47   case _OP_VERNOTIF: return OP_VERNOTIF;
     48   case _OP_ELSE: return OP_ELSE;
     49   case _OP_ENDIF: return OP_ENDIF;
     50   case _OP_VERIFY: return OP_VERIFY;
     51   case _OP_RETURN: return OP_RETURN;
     52   case _OP_TOALTSTACK: return OP_TOALTSTACK;
     53   case _OP_FROMALTSTACK: return OP_FROMALTSTACK;
     54   case _OP_2DROP: return OP_2DROP;
     55   case _OP_2DUP: return OP_2DUP;
     56   case _OP_3DUP: return OP_3DUP;
     57   case _OP_2OVER: return OP_2OVER;
     58   case _OP_2ROT: return OP_2ROT;
     59   case _OP_2SWAP: return OP_2SWAP;
     60   case _OP_IFDUP: return OP_IFDUP;
     61   case _OP_DEPTH: return OP_DEPTH;
     62   case _OP_DROP: return OP_DROP;
     63   case _OP_DUP: return OP_DUP;
     64   case _OP_NIP: return OP_NIP;
     65   case _OP_OVER: return OP_OVER;
     66   case _OP_PICK: return OP_PICK;
     67   case _OP_ROLL: return OP_ROLL;
     68   case _OP_ROT: return OP_ROT;
     69   case _OP_SWAP: return OP_SWAP;
     70   case _OP_TUCK: return OP_TUCK;
     71   case _OP_CAT: return OP_CAT;
     72   case _OP_SUBSTR: return OP_SUBSTR;
     73   case _OP_LEFT: return OP_LEFT;
     74   case _OP_RIGHT: return OP_RIGHT;
     75   case _OP_SIZE: return OP_SIZE;
     76   case _OP_INVERT: return OP_INVERT;
     77   case _OP_AND: return OP_AND;
     78   case _OP_OR: return OP_OR;
     79   case _OP_XOR: return OP_XOR;
     80   case _OP_EQUAL: return OP_EQUAL;
     81   case _OP_EQUALVERIFY: return OP_EQUALVERIFY;
     82   case _OP_RESERVED1: return OP_RESERVED1;
     83   case _OP_RESERVED2: return OP_RESERVED2;
     84   case _OP_1ADD: return OP_1ADD;
     85   case _OP_1SUB: return OP_1SUB;
     86   case _OP_2MUL: return OP_2MUL;
     87   case _OP_2DIV: return OP_2DIV;
     88   case _OP_NEGATE: return OP_NEGATE;
     89   case _OP_ABS: return OP_ABS;
     90   case _OP_NOT: return OP_NOT;
     91   case _OP_0NOTEQUAL: return OP_0NOTEQUAL;
     92   case _OP_ADD: return OP_ADD;
     93   case _OP_SUB: return OP_SUB;
     94   case _OP_MUL: return OP_MUL;
     95   case _OP_DIV: return OP_DIV;
     96   case _OP_MOD: return OP_MOD;
     97   case _OP_LSHIFT: return OP_LSHIFT;
     98   case _OP_RSHIFT: return OP_RSHIFT;
     99   case _OP_BOOLAND: return OP_BOOLAND;
    100   case _OP_BOOLOR: return OP_BOOLOR;
    101   case _OP_NUMEQUAL: return OP_NUMEQUAL;
    102   case _OP_NUMEQUALVERIFY: return OP_NUMEQUALVERIFY;
    103   case _OP_NUMNOTEQUAL: return OP_NUMNOTEQUAL;
    104   case _OP_LESSTHAN: return OP_LESSTHAN;
    105   case _OP_GREATERTHAN: return OP_GREATERTHAN;
    106   case _OP_LESSTHANOREQUAL: return OP_LESSTHANOREQUAL;
    107   case _OP_GREATERTHANOREQUAL: return OP_GREATERTHANOREQUAL;
    108   case _OP_MIN: return OP_MIN;
    109   case _OP_MAX: return OP_MAX;
    110   case _OP_WITHIN: return OP_WITHIN;
    111   case _OP_RIPEMD160: return OP_RIPEMD160;
    112   case _OP_SHA1: return OP_SHA1;
    113   case _OP_SHA256: return OP_SHA256;
    114   case _OP_HASH160: return OP_HASH160;
    115   case _OP_HASH256: return OP_HASH256;
    116   case _OP_CODESEPARATOR: return OP_CODESEPARATOR;
    117   case _OP_CHECKSIG: return OP_CHECKSIG;
    118   case _OP_CHECKSIGVERIFY: return OP_CHECKSIGVERIFY;
    119   case _OP_CHECKMULTISIG: return OP_CHECKMULTISIG;
    120   case _OP_CHECKMULTISIGVERIFY: return OP_CHECKMULTISIGVERIFY;
    121   case _OP_NOP1: return OP_NOP1;
    122   case _OP_CHECKLOCKTIMEVERIFY: return OP_CHECKLOCKTIMEVERIFY;
    123   case _OP_NOP2: return OP_NOP2;
    124   case _OP_CHECKSEQUENCEVERIFY: return OP_CHECKSEQUENCEVERIFY;
    125   case _OP_NOP3: return OP_NOP3;
    126   case _OP_NOP4: return OP_NOP4;
    127   case _OP_NOP5: return OP_NOP5;
    128   case _OP_NOP6: return OP_NOP6;
    129   case _OP_NOP7: return OP_NOP7;
    130   case _OP_NOP8: return OP_NOP8;
    131   case _OP_NOP9: return OP_NOP9;
    132   case _OP_NOP10: return OP_NOP10;
    133   case _OP_SMALLINTEGER: return OP_SMALLINTEGER;
    134   case _OP_PUBKEYS: return OP_PUBKEYS;
    135   case _OP_PUBKEYHASH: return OP_PUBKEYHASH;
    136   case _OP_PUBKEY: return OP_PUBKEY;
    137   case _OP_INVALIDOPCODE: return OP_INVALIDOPCODE;
    138   }
    139 
    140   return OP_INVALIDOPCODE;
    141 }
    142 
    143 const char*
    144 op_name(enum opcode opcode)
    145 {
    146     switch (opcode)
    147     {
    148     // push value
    149     case OP_0                      : return "0";
    150     case OP_PUSHDATA1              : return "PUSHDATA1";
    151     case OP_PUSHDATA2              : return "PUSHDATA2";
    152     case OP_PUSHDATA4              : return "PUSHDATA4";
    153     case OP_1NEGATE                : return "-1";
    154     case OP_RESERVED               : return "RESERVED";
    155     case OP_1                      : return "1";
    156     case OP_2                      : return "2";
    157     case OP_3                      : return "3";
    158     case OP_4                      : return "4";
    159     case OP_5                      : return "5";
    160     case OP_6                      : return "6";
    161     case OP_7                      : return "7";
    162     case OP_8                      : return "8";
    163     case OP_9                      : return "9";
    164     case OP_10                     : return "10";
    165     case OP_11                     : return "11";
    166     case OP_12                     : return "12";
    167     case OP_13                     : return "13";
    168     case OP_14                     : return "14";
    169     case OP_15                     : return "15";
    170     case OP_16                     : return "16";
    171 
    172     // control
    173     case OP_NOP                    : return "NOP";
    174     case OP_VER                    : return "VER";
    175     case OP_IF                     : return "IF";
    176     case OP_NOTIF                  : return "NOTIF";
    177     case OP_VERIF                  : return "VERIF";
    178     case OP_VERNOTIF               : return "VERNOTIF";
    179     case OP_ELSE                   : return "ELSE";
    180     case OP_ENDIF                  : return "ENDIF";
    181     case OP_VERIFY                 : return "VERIFY";
    182     case OP_RETURN                 : return "RETURN";
    183 
    184     // stack ops
    185     case OP_TOALTSTACK             : return "TOALTSTACK";
    186     case OP_FROMALTSTACK           : return "FROMALTSTACK";
    187     case OP_2DROP                  : return "2DROP";
    188     case OP_2DUP                   : return "2DUP";
    189     case OP_3DUP                   : return "3DUP";
    190     case OP_2OVER                  : return "2OVER";
    191     case OP_2ROT                   : return "2ROT";
    192     case OP_2SWAP                  : return "2SWAP";
    193     case OP_IFDUP                  : return "IFDUP";
    194     case OP_DEPTH                  : return "DEPTH";
    195     case OP_DROP                   : return "DROP";
    196     case OP_DUP                    : return "DUP";
    197     case OP_NIP                    : return "NIP";
    198     case OP_OVER                   : return "OVER";
    199     case OP_PICK                   : return "PICK";
    200     case OP_ROLL                   : return "ROLL";
    201     case OP_ROT                    : return "ROT";
    202     case OP_SWAP                   : return "SWAP";
    203     case OP_TUCK                   : return "TUCK";
    204 
    205     // splice ops
    206     case OP_CAT                    : return "CAT";
    207     case OP_SUBSTR                 : return "SUBSTR";
    208     case OP_LEFT                   : return "LEFT";
    209     case OP_RIGHT                  : return "RIGHT";
    210     case OP_SIZE                   : return "SIZE";
    211 
    212     // bit logic
    213     case OP_INVERT                 : return "INVERT";
    214     case OP_AND                    : return "AND";
    215     case OP_OR                     : return "OR";
    216     case OP_XOR                    : return "XOR";
    217     case OP_EQUAL                  : return "EQUAL";
    218     case OP_EQUALVERIFY            : return "EQUALVERIFY";
    219     case OP_RESERVED1              : return "RESERVED1";
    220     case OP_RESERVED2              : return "RESERVED2";
    221 
    222     // numeric
    223     case OP_1ADD                   : return "1ADD";
    224     case OP_1SUB                   : return "1SUB";
    225     case OP_2MUL                   : return "2MUL";
    226     case OP_2DIV                   : return "2DIV";
    227     case OP_NEGATE                 : return "NEGATE";
    228     case OP_ABS                    : return "ABS";
    229     case OP_NOT                    : return "NOT";
    230     case OP_0NOTEQUAL              : return "0NOTEQUAL";
    231     case OP_ADD                    : return "ADD";
    232     case OP_SUB                    : return "SUB";
    233     case OP_MUL                    : return "MUL";
    234     case OP_DIV                    : return "DIV";
    235     case OP_MOD                    : return "MOD";
    236     case OP_LSHIFT                 : return "LSHIFT";
    237     case OP_RSHIFT                 : return "RSHIFT";
    238     case OP_BOOLAND                : return "BOOLAND";
    239     case OP_BOOLOR                 : return "BOOLOR";
    240     case OP_NUMEQUAL               : return "NUMEQUAL";
    241     case OP_NUMEQUALVERIFY         : return "NUMEQUALVERIFY";
    242     case OP_NUMNOTEQUAL            : return "NUMNOTEQUAL";
    243     case OP_LESSTHAN               : return "LESSTHAN";
    244     case OP_GREATERTHAN            : return "GREATERTHAN";
    245     case OP_LESSTHANOREQUAL        : return "LESSTHANOREQUAL";
    246     case OP_GREATERTHANOREQUAL     : return "GREATERTHANOREQUAL";
    247     case OP_MIN                    : return "MIN";
    248     case OP_MAX                    : return "MAX";
    249     case OP_WITHIN                 : return "WITHIN";
    250 
    251     // crypto
    252     case OP_RIPEMD160              : return "RIPEMD160";
    253     case OP_SHA1                   : return "SHA1";
    254     case OP_SHA256                 : return "SHA256";
    255     case OP_HASH160                : return "HASH160";
    256     case OP_HASH256                : return "HASH256";
    257     case OP_CODESEPARATOR          : return "CODESEPARATOR";
    258     case OP_CHECKSIG               : return "CHECKSIG";
    259     case OP_CHECKSIGVERIFY         : return "CHECKSIGVERIFY";
    260     case OP_CHECKMULTISIG          : return "CHECKMULTISIG";
    261     case OP_CHECKMULTISIGVERIFY    : return "CHECKMULTISIGVERIFY";
    262 
    263     // expansion
    264     case OP_NOP1                   : return "NOP1";
    265     case OP_CHECKLOCKTIMEVERIFY    : return "CHECKLOCKTIMEVERIFY";
    266     case OP_CHECKSEQUENCEVERIFY    : return "CHECKSEQUENCEVERIFY";
    267     case OP_NOP4                   : return "NOP4";
    268     case OP_NOP5                   : return "NOP5";
    269     case OP_NOP6                   : return "NOP6";
    270     case OP_NOP7                   : return "NOP7";
    271     case OP_NOP8                   : return "NOP8";
    272     case OP_NOP9                   : return "NOP9";
    273     case OP_NOP10                  : return "NOP10";
    274 
    275     case OP_INVALIDOPCODE          : return "INVALIDOPCODE";
    276 
    277     // Note:
    278     //  The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
    279     //  as kind of implementation hack, they are *NOT* real opcodes.  If found in real
    280     //  Script, just let the default: case deal with them.
    281 
    282     default: {
    283       static char buf[32];
    284       sprintf(buf, "%02x", opcode);
    285       return buf;
    286     }
    287     }
    288 }
    289 
    290 
    291 
    292 
    293 static u32
    294 hash_fnv(u32 d, const char *str) {
    295   u8 *p = (u8*)str;
    296 
    297   if (d == 0)
    298     d = 0x01000193;
    299 
    300   while(*p)
    301     d = ((d * 0x01000193) ^ *p++) & 0xffffffff;
    302 
    303   return d;
    304 }
    305 
    306 
    307 enum opcode_token
    308 op_hash(const char *str) {
    309   int d = opcodes_g[hash_fnv(0, str) % ARRAY_SIZE(opcodes_g)];
    310   if (d < 0) d = opcodes_v[-d-1];
    311   else       d = opcodes_v[hash_fnv(d, str) % ARRAY_SIZE(opcodes_v)];
    312   return d;
    313 }
    314 
    315 
    316 
    317 const char *
    318 op_normalized_tok(const char *tok, int hash) {
    319   if (hash != _OP_TRUE  &&
    320       hash != _OP_FALSE &&
    321       hash != _OP_NOP2  &&
    322       hash != _OP_NOP3) return tok;
    323 
    324   if (strcmp(tok, "TRUE") == 0) return "1";
    325   else if (strcmp(tok, "FALSE") == 0) return "0";
    326   else if (strcmp(tok, "NOP2") == 0) return "CHECKLOCKTIMEVERIFY";
    327   else if (strcmp(tok, "NOP3") == 0) return "CHECKSEQUENCEVERIFY";
    328 
    329   return tok;
    330 }
    331 
    332 
    333 enum opcode
    334 op_parse_opcode(const char *str) {
    335   int hash     = op_hash(str);
    336   int maybe_op = op_from_token(hash);
    337 
    338   if (strcmp(op_name(maybe_op), op_normalized_tok(str, hash)) == 0)
    339     return maybe_op;
    340   else
    341     return OP_INVALIDOPCODE;
    342 }
    343 
    344 
    345 enum opcode
    346 op_tokenize(char *str) {
    347   int i;
    348 
    349   for (i = 0; str[i]; ++i)
    350     str[i] = toupper(str[i]);
    351 
    352   return op_parse_opcode(str);
    353 }
    354 
    355 
    356 void
    357 val_print(struct val val) {
    358   struct num *n;
    359   switch(val.type) {
    360   case VT_SCRIPTNUM:
    361     assert(val.ind != -1);
    362     n = num_pool_get(val.ind);
    363     assert(n);
    364     printf("%" PRId64, n->val);
    365     break;
    366   case VT_OP:
    367     printf("OP_%s", op_name(val.ind));
    368     break;
    369   case VT_SMALLINT:
    370     printf("%d", val.ind);
    371     break;
    372   case VT_DATA: {
    373     u32 len;
    374     u8 *data = byte_pool_get(val.ind, &len);
    375 
    376     if (len == 0)
    377       printf("0");
    378     else
    379       printf("@");
    380 
    381     print_bytes(data, len, 0);
    382     break;
    383   }
    384   case VT_RAW: {
    385     u32 len;
    386     u8 *data = byte_pool_get(val.ind, &len);
    387     printf("0x");
    388     print_bytes(data, len, 0);
    389     break;
    390   }
    391   default:
    392     assert(!"val_print data");
    393   }
    394 }
    395 
    396 const char *
    397 val_name(struct val val) {
    398   switch(val.type) {
    399   case VT_SCRIPTNUM:  return "n"; break;
    400   case VT_SMALLINT:  return "s"; break;
    401   case VT_DATA: return "d"; break;
    402   }
    403   return "UNK_VAL";
    404 }