BSV 上的图灵完备的“规则110”

我们已经在 BSV 上实现了 规则 110。类似于二维元胞自动机 (CA: cellular automata) 康威生命游戏,一维 CA 规则 110 也是图灵完备的。通过推论,我们再次证明了比特币是图灵完备的。

规则 110

在这里插入图片描述

Rule 110 元胞自动机是一维基本 CA,其中 0 和 1 的线性模式根据一组简单的规则演化。模式中的一个点在新一代中是 0 还是 1 取决于它的当前值和它的两个邻居的值。规则 110 具有以下规则集:

在这里插入图片描述

“规则110”的名称是基于该规则可以概括为二进制序列01101110,对应十进制值是110。

在这里插入图片描述

图灵完备

尽管它很简单,但规则 110 是图灵完备的,正如在基本元胞自动机中的普遍性(Cook 2004)中所证明的那样。这意味着,原则上,它可以模拟任何计算或计算机程序。规则 110 可以说是最简单的已知图灵完备系统。

实现

类似实现康威生命游戏,我们已经实现了规则 110。

contract rule110 {
    static const int N = 5; //size of board
    static const int N2 = 3; //size of board
    static bytes LIVE = b'01';
    static bytes DEAD = b'00';

    @state
    bytes board;
    
    public function play(int amount, SigHashPreimage txPreimage) {
        this.board = this.computeNewBoard(this.board);
        require(this.propagateState(txPreimage, amount));
    }

    function computeNewBoard(bytes board) : bytes {
        bytes res = b'';
        res += DEAD;
        loop (N2) : i {
            res += this.newState(board[i : i + 3]);
        }
        res += DEAD;
        return res;
    }
    
    function newState(bytes arg) : bytes {
        /*
          Current pattern	        111	110	101	100	011	010	001	000
          New state for center cell	 0	 1	 1	0	 1	 1	 1	 0
        */
        bytes a = arg[0 : 1];
        bytes b = arg[1 : 2];
        bytes c = arg[2 : 3];
        bytes res = LIVE;
        if (a == LIVE && b == LIVE && c == LIVE) {
            res = DEAD;
        }
        if (a == LIVE && b == DEAD && c == DEAD) {
            res = DEAD;
        }
        if (a == DEAD && b == DEAD && c == DEAD) {
            res = DEAD;
        }
        return res;
    }

    function propagateState(SigHashPreimage txPreimage, int value) : bool {
        SigHashType sigHashType = SigHash.ANYONECANPAY | SigHash.SINGLE | SigHash.FORKID;
        // this ensures the preimage is for the current tx
        require(Tx.checkPreimageSigHashType(txPreimage, sigHashType));
        bytes outputScript = this.getStateScript();
        bytes output = Utils.buildOutput(outputScript, value);
        return hash256(output) == SigHash.hashOutputs(txPreimage);
    }
}

规则 110 合约

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sCrypt Web3应用开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值