Running Bitcoin Scripts on CKB-VM

CryptapeCryptape
5 min read

CKB has drawn inspiration from Bitcoin's robust and secure design principles, particularly in its approach to the UTXO validation model and PoW consensus. The ckb-btc-vm project showcases the versatility of CKB by enabling direct Bitcoin Script validation on CKB's RISC-V based virtual machine, CKB-VM.

This demo highlights the potential for enhanced interoperability between Bitcoin and CKB, opening up new possibilities for cross-chain applications and expanded functionalities within the blockchain ecosystem.

Technical Overview

In general, Bitcoin and CKB both act as verification layers; however, Bitcoin is more like a dedicated layer for validating Bitcoin transfers, while CKB is a general verification layer capable of handling various transactions types.

They share a common UTXO-based Script verification model, yet differ in their scripting structures and capacities. Bitcoin Script is simple and stack-based running on Bitcoin Script Interpreter, whereas CKB Script is register-based running on CKB-VM with extensive programmability, state storage and introspection capacities.

The table below summarizes the differences between Bitcoin Script and CKB Script across several aspects:

AspectBitcoin ScriptCKB Script
StatefulnessStatelessStateful. Can maintain and update state (data) across multiple transactions
IntrospectionLimited. Proposed opcodes like OP_CTV to impose future spending constraintsExtensive. With Lock Script and Type Script to inspect and interact with other Scripts and the entire transaction.
Script StructureSeries of opcodes on a stack, limited to basic transaction conditionsWritten in high-level languages and compiled to the RISC-V instruction set
ProgrammabilityLimited by stack-based scripting and predefined opcodesRISC-V based VM enables extensive programmability and multiple high-level languages
InteroperabilityLimitedSupports other VMs like EVM, enhancing cross-chain compatibility
UpgradeabilityRequires network-wide hard forksAllows upgrades without hard forks
Use CasesTransaction validation, basic contractsTransaction validation, smart contracts, dApps

Validation flow of CKB Scripting

Validation flow of Bitcoin Scripting

How ckb-btc-vm Works

RISC-V, where CKB-VM is based on, provides low-level access to the CPU, enabling highly efficient and extensive programmability. This makes it possible to run other VMs on top of CKB-VM to mimic the behavior of other blockchains—exactly how ckb-btc-vm works.

By porting the C++ code of the Bitcoin Script Interpreter without any changes, Bitcoin interpreter can run on top of CKB-VM, allowing the execution of Bitcoin Script opcodes and the validation of Bitcoin transactions.

The execution process of ckb-btc-vm follows these steps:

  1. Input: Bitcoin transactions are inputted in the mempool's API response format as an argument.

  2. Parsing: The transaction data is parsed.

  3. Script Validation: The parsed transaction is processed through the ported Bitcoin Script Interpreter on CKB-VM for script validation, mirroring the validation process of Bitcoin nodes to ensure the compliance of all inputs and outputs with Bitcoin's scripting rules.

Running ckb-btc-vm

To run ckb-btc-vm, build the binary that contains the ported version of the Bitcoin Script Interpreter:

$ git clone --recursive <https://github.com/xxuejie/ckb-bitcoin-vm>
$ make
# For macOS, use:
$ make CLANG=clang

The binary is located in build/bitcoin_vm. To run the binary, you can upload it to the CKB blockchain and execute it on a CKB transaction. Alternatively, with a simpler method, you can use a standalone debugger tool, ckb-debugger, to execute the bitcoin_vm binary off-chain. The command below uses ckb-debugger to execute the bitcoin_vm binary on a transaction fetched from the mempool.space API, which provides raw Bitcoin transaction data.

ckb-debugger --bin build/bitcoin_vm bitcoin_vm "$(curl -s <https://mempool.space/api/tx/382b61d20ad4fce5764aae6f4d4e7fa10abbb3f9ed8692fb262b70a3ed494d5c>)"

The bitcoin_vm binary accepts full Bitcoin transaction in mempool's API response format as a CLI argument, parses the transaction, and runs all Bitcoin script validations. It then provides a detailed validation outcome, including the number of cycles—a metric indicating the efficiency and resource requirements of the transaction validation.

Vin 0 takes 986664
 cycles to validate
Run result: 0
Total cycles consumed: 1605056(1.5M)
Transfer cycles: 128990(126.0K), running cycles: 1476066(1.4M)

You can also save the transaction locally and run it repeatedly:

$ curl <https://mempool.space/api/tx/15e10745f15593a899cef391191bdd3d7c12412cc4696b7bcb669d0feadc8521> > nowitness1.json
$ ckb-debugger --bin build/bitcoin_vm bitcoin_vm "$(<nowitness1.json)"
Vin 0 takes 1008698
 cycles to validate
Vin 1 takes 989294
 cycles to validate
Vin 2 takes 1020211
 cycles to validate
Vin 3 takes 1023986
 cycles to validate
Vin 4 takes 1024188
 cycles to validate
Run result: 0
Total cycles consumed: 6612526(6.3M)
Transfer cycles: 130154(127.1K), running cycles: 6482372(6.2M)

Potential Use Cases

Bitcoin Script validation on the CKB-VM allows developers to build applications that leverage the security and decentralization of both Bitcoin and Nervos Network.

One potential use case is enhancing Bitcoin address interoperability within CKB. Currently, there are implementations of Bitcoin locks on the CKB blockchain (See the project ccc-locks), with the most commonly supported addresses being P2PKH and P2WPKH. By integrating ckb-btc-vm, CKB Script can execute nearly all Bitcoin opcodes (excluding a few requiring specific Bitcoin transaction data that might be difficult to provide in certain CKB applications like ccc-locks. For example, OP_CHECKLOCKTIMEVERIFY require reading nLockTime from Bitcoin transaction). This opens up possibilities to validate and use a wider range of Bitcoin addresses on CKB.

CKB-VM’s Versatility

The ckb-btc-vm source code is hosted on Github. This project leverages a fork of musl libc, ckb-libcxx, and optimizations to port C++ code onto CKB-VM (A detailed blog post on this topic will be out soon).

ckb-btc-vm presents an exciting journey exploring the potential of CKB-VM and CKB blockchain. It not only highlights CKB-VM's flexibility, more importantly, provides an intriguing and enjoyable experiment for engineers, which we indeed love and do frequently!

Beyond Bitcoin Interpreter, we have also experimented with other VMs, such as:

  • Polyjuice: A port of the C++ Ethereum virtual machines into RISC-V binaries, enabling Ethereum-compatible smart contracts on CKB

  • ckb-js-vm, ckb-duktape, ckb-lua-vm: Integrable high-level language VMs executing CKB Scripts written in JavaScript and Lua

If you're interested in such projects, check out the following links to explore the full potential of CKB-VM:


Cryptape is seeking system engineer wizards! Join us to revolutionize blockchain technology: join@cryptape.com

0
Subscribe to my newsletter

Read articles from Cryptape directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cryptape
Cryptape