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

Crypto White 高水平讲解

作者:Crypto White trustless创始人

来源:https://biquanlibai.notion.site/BitVM-2b8557e0ea004d0cbc52951782bc9754

bilibili视频讲解

Youtube讲解

准备知识

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)

        2. 验证:用一个基于ZKP的validity proof做简洁的验证

      3. Optimistic Rollup

        1. 发布:将计算编译为合约内虚拟机(AVM、OVM)的字节码,发布字节码的commitment

        2. 验证:fraud proof & interactive proving,V要求P在链上重新执行部分计算直至发现错误

  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一些链下交易

    1. 交易的input包含P和V的联合签名,确保交易的inputs和outputs符合各个场景下(challenge、response等)的格式要求

    2. 交易的output使用Taproot,即满足多个条件中的任意一个即可unlock这个output

      1. output只展示树根,input展示任一叶子(locking script)及其对应的unlocking script,执行成功则解锁output

      2. 既能将复杂计算上链(可以包含很多并行的解锁条件),又节省链上空间和保护隐私(只展示其中的一个解锁条件)

    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

    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

  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

    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,减小发布的尺寸

Challenges and Responses

  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

  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解锁

总结

  1. 虽然不能详细验证逻辑是否严密,但是直观感觉是可行的

  2. 原文中的P&V联合pre-sign并不能达到预期的效果

    1. 首先,交易的input只有运行时才能确定,所以无法pre-sign这些交易

      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

最后更新于

这有帮助吗?