see https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L36-L49
181700
def cast_to_bool(v)
      case v
        when Numeric
          return v != 0
        when String
          v.each_byte.with_index do |b, i|
            return !(i == (v.bytesize - 1) && b == 0x80) unless b == 0
          end
          false
        else
          false
      end
    end
879079
function() {
            while (i < tb.seqCount) {
                var ns = ba[p];
                if (p + ns + 2 + (tb.version == 0 ? 4 : 8) >= ba.length) {
                    headerBlocksFetched += headerBlockSize;
                    headerBlockSize = Math.max(HEADER_BLOCK_SIZE,Math.floor(headerBlocksFetched*tb.seqCount/i));
                    return tb.data.slice(o + p, headerBlockSize).fetch(function (r) {
                        o += p;
                        p = 0;
                        ba = new Uint8Array(r);
                        parseSeqInfo();
                    });
                } else {
                    ++p;
                    var name = '';
                    for (var j = 1; j <= ns; ++j) {
                        name += String.fromCharCode(ba[p++]);
                    }
                    if (tb.version == 0) {
                        var offset = tb.readInt(ba, p);
                        p += 4;
                    } else {
                        var offset = tb.readInt64(ba, p);
                        p += 8;
                    }
                    tb.seqDict[name] = new TwoBitSeq(tb, offset);
                    ++i;
                }
            }
            return cnt(tb);
        }
988208
static function fromUtf8( $multi_char, $offs, &$len )
    {
        $char_code = false;
        if ( ( ord( $multi_char[$offs + 0] ) & 0x80 ) == 0x00 ) // 7 bit, 1 char
        {
            $char_code = ord( $multi_char[$offs + 0] );
            $len = 1;
        }
        else if ( ( ord( $multi_char[$offs + 0] ) & 0xe0 ) == 0xc0 ) // 11 bit, 2 chars
        {
            $len = 2;
            if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 )
                return $char_code;
            $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x1f ) << 6) +
                           (( ord( $multi_char[$offs + 1] ) & 0x3f )) );
            if ( $char_code < 128 ) // Illegal multibyte, should use less than 2 chars
            {
                $char_code = false;
            }
        }
        else if ( ( ord( $multi_char[$offs + 0] ) & 0xf0 ) == 0xe0 ) // 16 bit, 3 chars
        {
            $len = 3;
            if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 )
                return $char_code;
            $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x0f ) << 12) +
                           (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 6) +
                           (( ord( $multi_char[$offs + 2] ) & 0x3f )) );
            if ( $char_code < 2048 ) // Illegal multibyte, should use less than 3 chars
            {
                $char_code = false;
            }
        }
        else if ( ( ord( $multi_char[$offs + 0] ) & 0xf8 ) == 0xf0 ) // 21 bit, 4 chars
        {
            $len = 4;
            if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 )
                return $char_code;
            $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x07 ) << 18) +
                           (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 12) +
                           (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 6) +
                           (( ord( $multi_char[$offs + 3] ) & 0x3f )) );
            if ( $char_code < 65536 ) // Illegal multibyte, should use less than 4 chars
            {
                $char_code = false;
            }
        }
        else if ( ( ord( $multi_char[$offs + 0] ) & 0xfc ) == 0xf8 ) // 26 bit, 5 chars
        {
            $len = 5;
            if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 )
                return $char_code;
            $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x03 ) << 24) +
                           (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 18) +
                           (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 12) +
                           (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 6) +
                           (( ord( $multi_char[$offs + 4] ) & 0x3f )) );
            if ( $char_code < 2097152 ) // Illegal multibyte, should use less than 5 chars
            {
                $char_code = false;
            }
        }
        else if ( ( ord( $multi_char[$offs + 0] ) & 0xfe ) == 0xfc ) // 31 bit, 6 chars
        {
            $len = 6;
            if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 or
                 ( ord( $multi_char[$offs + 5] ) & 0xc0 ) != 0x80 )
                return $char_code;
            $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x01 ) << 30) +
                           (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 24) +
                           (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 18) +
                           (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 12) +
                           (( ord( $multi_char[$offs + 4] ) & 0x3f ) << 6) +
                           (( ord( $multi_char[$offs + 5] ) & 0x3f )) );
            if ( $char_code < 67108864 ) // Illegal multibyte, should use less than 6 chars
            {
                $char_code = false;
            }
        }
        return $char_code;
    }
966537
function (cp) {
	        return (cp < 0x10000) ? String.fromCharCode(cp) :
	            String.fromCharCode(0xD800 + ((cp - 0x10000) >> 10)) +
	                String.fromCharCode(0xDC00 + ((cp - 0x10000) & 1023));
	    }
300539
def to_base58
      h = to_payload.bth
      hex = h + Bitcoin.calc_checksum(h)
      Base58.encode(hex)
    end
1199910
def get_bits(block_representation, coin_symbol='btc', api_key=None):
    '''
    
    '''
    return get_block_overview(block_representation=block_representation,
            coin_symbol=coin_symbol, txn_limit=1, api_key=api_key)['bits']
776202
function parse_TransformInfoHeader(blob, length) {
	var o = {};
	var len = blob.read_shift(4);
	var tgt = blob.l + len - 4;
	blob.l += 4; // must be 0x1
	o.id = blob.read_shift(0, 'lpp4');
	// tgt == len
	o.name = blob.read_shift(0, 'lpp4');
	o.R = parse_CRYPTOVersion(blob, 4);
	o.U = parse_CRYPTOVersion(blob, 4);
	o.W = parse_CRYPTOVersion(blob, 4);
	return o;
}
1369023
def btc_witness_script_deserialize(_script):
    

    script = None
    if isinstance(_script, str) and re.match('^[0-9a-fA-F]*$', _script):
        # convert from hex to bin, safely
        script = binascii.unhexlify(_script)
    else:
        script = _script[:]

    # pointer to byte offset in _script (as an array due to Python scoping rules)
    ptr = [0]

    witness_stack_len = read_var_int(ptr, script)
    witness_stack = []

    for _ in xrange(0, witness_stack_len):

        stack_item = read_var_string(ptr, script)
        witness_stack.append(stack_item)

    return witness_stack
981811
function _encryptBlock($in)
    {
        $p = $this->bctx["p"];
        // extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower
        $sb_0 = $this->bctx["sb"][0];
        $sb_1 = $this->bctx["sb"][1];
        $sb_2 = $this->bctx["sb"][2];
        $sb_3 = $this->bctx["sb"][3];

        $in = unpack("N*", $in);
        $l = $in[1];
        $r = $in[2];

        for ($i = 0; $i < 16; $i+= 2) {
            $l^= $p[$i];
            $r^= $this->safe_intval(($this->safe_intval($sb_0[$l >> 24 & 0xff]  + $sb_1[$l >> 16 & 0xff]) ^
                  $sb_2[$l >>  8 & 0xff]) +
                  $sb_3[$l       & 0xff]);

            $r^= $p[$i + 1];
            $l^= $this->safe_intval(($this->safe_intval($sb_0[$r >> 24 & 0xff]  + $sb_1[$r >> 16 & 0xff]) ^
                  $sb_2[$r >>  8 & 0xff]) +
                  $sb_3[$r       & 0xff]);
        }
        return pack("N*", $r ^ $p[17], $l ^ $p[16]);
    }
1169157
def sighash_for_legacy(index, script_code, hash_type)
      ins = inputs.map.with_index do |i, idx|
        if idx == index
          i.to_payload(script_code.delete_opcode(Bitcoin::Opcodes::OP_CODESEPARATOR))
        else
          case hash_type & 0x1f
            when SIGHASH_TYPE[:none], SIGHASH_TYPE[:single]
              i.to_payload(Bitcoin::Script.new, 0)
            else
              i.to_payload(Bitcoin::Script.new)
          end
        end
      end

      outs = outputs.map(&:to_payload)
      out_size = Bitcoin.pack_var_int(outputs.size)

      case hash_type & 0x1f
        when SIGHASH_TYPE[:none]
          outs = ''
          out_size = Bitcoin.pack_var_int(0)
        when SIGHASH_TYPE[:single]
          return "\x01".ljust(32, "\x00") if index >= outputs.size
          outs = outputs[0...(index + 1)].map.with_index { |o, idx| (idx == index) ? o.to_payload : o.to_empty_payload }.join
          out_size = Bitcoin.pack_var_int(index + 1)
      end

      if hash_type & SIGHASH_TYPE[:anyonecanpay] != 0
        ins = [ins[index]]
      end

      buf = [[version].pack('V'), Bitcoin.pack_var_int(ins.size),
          ins, out_size, outs, [lock_time, hash_type].pack('VV')].join

      Bitcoin.double_sha256(buf)
    end
56598
function hextob64nl(s) {
    var b64 = hextob64(s);
    var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
    b64nl = b64nl.replace(/\r\n$/, '');
    return b64nl;
}
1284485
static void int2bin(
	 int value,             /* input : decimal value */
	 int no_of_bits,        /* input : number of bits to use */
	 short[] bitstream, int bitstreams       /* output: bitstream  */
	)
	{
	   int pt_bitstream;
	   short   i, bit;

	   pt_bitstream = bitstreams + no_of_bits;

	   for (i = 0; i < no_of_bits; i++)
	   {
	     bit = (short)(value & 0x0001);      /* get lsb */
	     if (bit == 0)
	         bitstream[--pt_bitstream] = LD8KConstants.BIT_0;
	     else
	         bitstream[--pt_bitstream] = LD8KConstants.BIT_1;
	     value >>= 1;
	   }
	}
1031620
def from_bytes(TxIn, byte_string):
        '''
        
        '''
        outpoint = Outpoint.from_bytes(byte_string[:36])

        script_sig_len = VarInt.from_bytes(byte_string[36:45])
        script_start = 36 + len(script_sig_len)
        script_end = script_start + script_sig_len.number
        script_sig = byte_string[script_start:script_end]

        sequence = byte_string[script_end:script_end + 4]
        if script_sig == b'':
            stack_script = b''
            redeem_script = b''
        else:
            stack_script, redeem_script = TxIn._parse_script_sig(script_sig)
        return TxIn(
            outpoint=outpoint,
            stack_script=stack_script,
            redeem_script=redeem_script,
            sequence=sequence)
948564
function binl(x, len) {
        var T, j, i, l,
          h0 = 0x67452301,
          h1 = 0xefcdab89,
          h2 = 0x98badcfe,
          h3 = 0x10325476,
          h4 = 0xc3d2e1f0,
          A1, B1, C1, D1, E1,
          A2, B2, C2, D2, E2;

        /* append padding */
        x[len >> 5] |= 0x80 << (len % 32);
        x[(((len + 64) >>> 9) << 4) + 14] = len;
        l = x.length;

        for (i = 0; i < l; i += 16) {
          A1 = A2 = h0;
          B1 = B2 = h1;
          C1 = C2 = h2;
          D1 = D2 = h3;
          E1 = E2 = h4;
          for (j = 0; j <= 79; j += 1) {
            T = safe_add(A1, rmd160_f(j, B1, C1, D1));
            T = safe_add(T, x[i + rmd160_r1[j]]);
            T = safe_add(T, rmd160_K1(j));
            T = safe_add(bit_rol(T, rmd160_s1[j]), E1);
            A1 = E1;
            E1 = D1;
            D1 = bit_rol(C1, 10);
            C1 = B1;
            B1 = T;
            T = safe_add(A2, rmd160_f(79 - j, B2, C2, D2));
            T = safe_add(T, x[i + rmd160_r2[j]]);
            T = safe_add(T, rmd160_K2(j));
            T = safe_add(bit_rol(T, rmd160_s2[j]), E2);
            A2 = E2;
            E2 = D2;
            D2 = bit_rol(C2, 10);
            C2 = B2;
            B2 = T;
          }

          T = safe_add(h1, safe_add(C1, D2));
          h1 = safe_add(h2, safe_add(D1, E2));
          h2 = safe_add(h3, safe_add(E1, A2));
          h3 = safe_add(h4, safe_add(A1, B2));
          h4 = safe_add(h0, safe_add(B1, C2));
          h0 = T;
        }
        return [h0, h1, h2, h3, h4];
      }
399676
def btc_script_to_hex(script):
    

    hex_script = ''
    parts = script.split(' ')
    for part in parts:
        if part[0:3] == 'OP_':
            value = OPCODE_VALUES.get(part)
            if not value:
                raise ValueError("Unrecognized opcode {}".format(part))

            hex_script += "%0.2x" % value

        elif hashing.is_hex(part):
            hex_script += '%0.2x' % hashing.count_bytes(part) + part

        else:
            raise Exception('Invalid script - only opcodes and hex characters allowed.')

    return hex_script
681258
function _decryptBlock($in)
    {
        static $invtables;
        if (empty($invtables)) {
            $invtables = &$this->_getInvTables();
        }
        $dt0   = $invtables[0];
        $dt1   = $invtables[1];
        $dt2   = $invtables[2];
        $dt3   = $invtables[3];
        $isbox = $invtables[4];

        $state = array();
        $words = unpack('N*', $in);

        $c  = $this->c;
        $dw = $this->dw;
        $Nb = $this->Nb;
        $Nr = $this->Nr;

        // addRoundKey
        $wc = $Nb - 1;
        foreach ($words as $word) {
            $state[] = $word ^ $dw[++$wc];
        }

        $temp = array();
        for ($round = $Nr - 1; $round > 0; --$round) {
            $i = 0; // $c[0] == 0
            $j = $Nb - $c[1];
            $k = $Nb - $c[2];
            $l = $Nb - $c[3];

            while ($i < $Nb) {
                $temp[$i] = $dt0[$state[$i] >> 24 & 0x000000FF] ^
                            $dt1[$state[$j] >> 16 & 0x000000FF] ^
                            $dt2[$state[$k] >>  8 & 0x000000FF] ^
                            $dt3[$state[$l]       & 0x000000FF] ^
                            $dw[++$wc];
                ++$i;
                $j = ($j + 1) % $Nb;
                $k = ($k + 1) % $Nb;
                $l = ($l + 1) % $Nb;
            }
            $state = $temp;
        }

        // invShiftRows + invSubWord + addRoundKey
        $i = 0; // $c[0] == 0
        $j = $Nb - $c[1];
        $k = $Nb - $c[2];
        $l = $Nb - $c[3];

        while ($i < $Nb) {
            $word = ($state[$i] & 0xFF000000) |
                    ($state[$j] & 0x00FF0000) |
                    ($state[$k] & 0x0000FF00) |
                    ($state[$l] & 0x000000FF);

            $temp[$i] = $dw[$i] ^ ($isbox[$word       & 0x000000FF]        |
                                  ($isbox[$word >>  8 & 0x000000FF] <<  8) |
                                  ($isbox[$word >> 16 & 0x000000FF] << 16) |
                                  ($isbox[$word >> 24 & 0x000000FF] << 24));
            ++$i;
            $j = ($j + 1) % $Nb;
            $k = ($k + 1) % $Nb;
            $l = ($l + 1) % $Nb;
        }

        switch ($Nb) {
            case 8:
                return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]);
            case 7:
                return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]);
            case 6:
                return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]);
            case 5:
                return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]);
            default:
                return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]);
        }
    }
1166321
def _not16(ins):
    ''' 
    '''
    output = _16bit_oper(ins.quad[2])
    output.append('ld a, h')
    output.append('or l')
    output.append('sub 1')
    output.append('sbc a, a')
    output.append('push af')
    return output