> For the complete documentation index, see [llms.txt](https://bitvm.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bitvm.gitbook.io/doc/featured-articles/bitvm-zai-bi-te-bi-shang-shi-xian-zhi-neng-he-yue.md).

# BitVM 在比特币上实现智能合约

Crypto White 高水平讲解

> 作者：Crypto White [trustless](https://trustless.org/)创始人
>
> 来源：<https://biquanlibai.notion.site/BitVM-2b8557e0ea004d0cbc52951782bc9754>

[bilibili视频讲解](https://www.bilibili.com/video/BV1Vw41167ci/?spm_id_from=333.337.search-card.all.click\&vd_source=3665ce738f6828ece34a7506d9296f36)

[Youtube讲解](https://www.youtube.com/watch?v=DQuhnM6B9tI)

## 准备知识

### Commitment

1. Commitment Scheme
   1. 举例：Merkle tree
      1. 流程
         1. commit：P（Prover）构建tree，发布root
         2. reveal：P展现一个leaf及其branch
         3. check：V（Verifier）验证branch
      2. 特性
         1. binding：commit之后，所有可以reveal的leaf就已确定
         2. hiding：reveal时只展示了其中的一个
   2. 一些Commitment
      1. 各种hash：SHA256、Pedersen、MiMC
      2. 各种tree：Merkle、MPT
      3. ZKP系列：KZG、FRI、IPA
2. Commit一段计算
   1. 流程
      1. commit：P发布计算`f(x)=35`，承诺知道其解`x`

         ```
         # 计算f(x) = x^3 + x + 5 = 35
         # 解x = 3
         def f(x):
           y = x**3
           assert y + x + 5 == 35
         ```
      2. reveal：P公布解`x`
      3. check：V将解`x`代入计算`f(x)=35`进行验证
      4. 解的变量`x`又称作计算的input，中间变量`y`又称作witness
   2. 实现
      1. 链上运行的时候要减少发布的尺寸和验证的计算量
      2. ZK Rollup
         1. 发布：将计算编译为电路，发布电路的commitment（逻辑和变量在ZKP电路中又称作gate constraints和copy constraints）

            <figure><img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FE10EVkyVdHttI3Gr0WJw%2Fimage.png?alt=media&amp;token=abc65d4f-7778-4aaa-8c0a-242ec0f226b8" alt="" width="190"><figcaption><p><a href="https://vitalik.ca/general/2019/09/22/plonk.html">https://vitalik.ca/general/2019/09/22/plonk.html</a></p></figcaption></figure>

            <figure><img src="file:///Users/frankmiao/newhuotech/75b7f234-c2aa-42b3-b44a-283b4772b105_Export-9854fa44-fd6a-42df-8b85-bffb2b752897/BitVM%2073b0bb72d6084f1ea9afdabe56228045/Untitled.png?lastModify=1705482625" alt=""><figcaption></figcaption></figure>
         2. 验证：用一个基于ZKP的validity proof做简洁的验证
      3. Optimistic Rollup

         1. 发布：将计算编译为合约内虚拟机（AVM、OVM）的字节码，发布字节码的commitment
         2. 验证：fraud proof & interactive proving，V要求P在链上重新执行部分计算直至发现错误

         <figure><img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FVTMqKJGmfugZ75M2ORLG%2Fimage.png?alt=media&amp;token=a5407398-b37a-41c3-aaab-61dd0b6da37d" alt="" width="335"><figcaption><p><a href="https://zerocap.com/insights/research-lab/arbitrum-arb-deep-dive/">https://zerocap.com/insights/research-lab/arbitrum-arb-deep-dive/</a></p></figcaption></figure>
3. BitVM提出了在比特币上Commit一段计算的方法，其过程完全借鉴Optimistic Rollup
4. 发布：计算→Binary Circuit→opcode script→taproot commitment，编译结果是链上可执行的
5. 验证：pre-sign几种类型的交易（challenge、response等），需要interactive proving的时候将他们上链来进行

### Pre-sign Off-chain Transactions

1. 由P和V pre-sign一些链下交易

   ![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FxWVT3L7Wxd6aptZAO8aF%2Fimage.png?alt=media\&token=d0d3f673-7d71-4b2a-a90e-9c228fe899e2)

   1. 交易的input包含P和V的联合签名，确保交易的inputs和outputs符合各个场景下（challenge、response等）的格式要求
   2. 交易的output使用[Taproot](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#user-content-Constructing_and_spending_Taproot_outputs)，即满足多个条件中的任意一个即可unlock这个output

      1. output只展示树根，input展示任一叶子（locking script）及其对应的unlocking script，执行成功则解锁output
      2. 既能将复杂计算上链（可以包含很多并行的解锁条件），又节省链上空间和保护隐私（只展示其中的一个解锁条件）

      <figure><img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FRZ1ZhTT5eLDJA6NMk3a5%2Fimage.png?alt=media&amp;token=e85a4b90-d771-4a81-9d69-2ad1171a4f65" alt="" width="375"><figcaption><p><a href="https://blog.bitmex.com/the-schnorr-signature-taproot-softfork-proposal/">https://blog.bitmex.com/the-schnorr-signature-taproot-softfork-proposal/</a></p></figcaption></figure>

   2.Taproot也是一种Commitment Scheme这些交易在interactive proving的时候上链，以保证interactive proving按照既定流程进行。

## BitVM

### Binary Circuit Commitment

1. 计算→Binary Circuit→opcode script→taproot commitment，重点提出了
   1. 用Bit Value Commitment实现电路中的copy constraints
   2. 用Logic Gate Commitment实现电路中的gate constraints
2. Bit Value Commitment

   ![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FXIKJHse9178JnJiKt6Tu%2Fimage.png?alt=media\&token=dd9a5d05-592b-4741-a82a-638c994fe0f3)

   1. 一段Bitcoin script，可简记为`<hash_0> <hash_1> OP_BITCOMMITMENT`
      1. unlock的时候，输入preimage0则返回0，输入preimage1则返回1
   2. Commitment Scheme
      1. commit：P发布`<hash_0> <hash_1> OP_BITCOMMITMENT`
      2. reveal：P展现`<preimage>`
      3. check：V验证`<preimage> <hash_0> <hash_1> OP_BITCOMMITMENT`
   3. 特性
      1. binding：commit之后，所有可以reveal的preimage就已确定
      2. hiding：reveal时只展示了其中的一个preimage
   4. 在电路中多处使用同一个Bit Value Commitment，实现copy constraints
      1. 如果P把两个preimage都reveal了，则破坏了copy constraints，这种情况称做Equivocation，允许V立刻punish P

Logic Gate Commitment

![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FVmve6RAd13xKh0m9KBkg%2Fimage.png?alt=media\&token=896da1e7-b28e-4dae-9149-bce76bd2964c)

![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FmZwU7G4d635Hk0rUiVSt%2Fimage.png?alt=media\&token=0997b7f1-d758-4542-a466-cbe27648eceb)

1. 将Bit Value Commitment作为输入和输出组装成logic gate，实现gate constraints
2. 可简记为`<hash_c0> <hash_c1> <hash_b0> <hash_b1> <hash_a0> <hash_a1> OP_GATECOMMITMENT`，对应的unlocking script是`<preimage_a> <preimage_b> <preimage_c>`
3. P发布一个Logic Gate Commitment表示其承诺3个Bit Value Commitment打开后符合gate的逻辑约束
4. 组合成Binary Circuit Commitment

   <img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FtUL2BIZmlcJQwIAICfQl%2Fimage.png?alt=media&amp;token=aff3cdd9-159c-40b9-bffc-74af92735e4e" alt="" data-size="original">

   1. A, B, C, D是input，E, F, G, H, I, J, L是witness
   2. P发布一个Binary Circuit Commitment
      1. 即发布电路中所有的Logic Gate Commitment`<hash_c0> <hash_c1> <hash_b0> <hash_b1> <hash_a0> <hash_a1> OP_GATECOMMITMENT`；其中一些包含相同的`<hash_0> <hash_1>`则表示电路相连
      2. 表示其承诺电路中所有Bit Value Commitment打开后符合电路的逻辑约束
5. 最终发布Circuit Taproot，减小发布的尺寸

   <figure><img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2F2sEiLB4hg2DpOobI8pmw%2Fimage.png?alt=media&amp;token=0020548c-0234-4264-a59d-b7f9c5e90cd7" alt="" width="375"><figcaption></figcaption></figure>

### Challenges and Responses

<figure><img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2Fk8zvjGqzGYtwCeKkLolA%2Fimage.png?alt=media&amp;token=af58cebd-4e5a-4ab7-aa1c-d201f8dcde6c" alt=""><figcaption></figcaption></figure>

1. P和V生成这些交易的过程即为commit，因为其中包含了Circuit Taproot
2. P\&V fund
   1. P和V共同下注
      1. 赌约：如果通过check则P拿走所有赌注，否则V拿走所有赌注
      2. 利益驱使下，P会提供有效的reveal，V会challenge其有效性
   2. 要求P reveal电路的所有input
   3. 否则V可以在一段时间后转走赌注
3. P reveal
   1. P reveal电路的所有input
   2. V可以挑战gate constraint、可以惩罚copy constraint
   3. 否则P可以在一段时间后转走赌注
4. P和V interactive proving

<img src="https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FUeYTKa0iRIboxDibc2aF%2Fimage.png?alt=media&amp;token=e3e1e001-2d5a-46eb-abcf-165a196aff9c" alt="" data-size="original">

1. challenges和responses pair
   1. pair之间的hashlock不同，script一样
   2. pair内每一行有同样的hashlock
   3. V用preimage解开一个challenge后，P就可以用这个preimage和gate inputs去解锁对应的response
2. 如果P无法在链上正确执行challenged gate，V可以在一段时间后转走赌注
3. 如果V没有新的challenge，P可以在一段时间后转走赌注
4. V punish
   1. P在reveal和response阶段都会reveal一些Bit Value Commitment
   2. 此时如果V发现P存在equivocation，可以立刻punish
   3. 用电路中任一Bit Value Commitment的两个preimage解锁

      ![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FBjGPY499fUgW1spyS3em%2Fimage.png?alt=media\&token=478b4a47-09f6-4452-9c84-4dd4df8e5994)

## 总结

1. 虽然不能详细验证逻辑是否严密，但是直观感觉是可行的
2. 原文中的P\&V联合pre-sign并不能达到预期的效果
   1. 首先，交易的input只有运行时才能确定，所以无法pre-sign这些交易

      ![](https://2933093085-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2E93vAVWL3E2kQSMHQMn%2Fuploads%2FUORLxusoLG71my4aUeQv%2Fimage.png?alt=media\&token=f25aeb48-7d45-4a0a-a01e-bcd78767cafc)

      <https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch06.asciidoc>
   2. 其次，P\&V联合sign的话，受利益驱使，P可以不签名以阻止V challenge，V也可以不签名以阻止P response
   3. 感觉改成可信第三方sign更合理
      1. 允许任何人可以生成交易参与interactive proving
      2. 交易必须经过第三方签名，第三方只保证交易的格式符合流程要求，不保证交易本身的正确性
3. BitVM理论上可以在Bitcoin上实现智能合约和Optimistic Rollup，但有以下难点
   1. Prover reveal的时候，交易的input的空间可能不够
      1. 计算输入的每个bit都会转化成一个preimage，占用空间很大
      2. Bitcoin一个block中的input用SegWit的话最多支持4MB
   2. 把计算编译成Binary Circuit，gate可能太多
      1. 要challenge-response很多次才能找到fraud
