零知识证明DApp开发新手教程【circom/solidity】

简介:

在这个教程中,我们将学习如何开发一个基于以太坊的零知识身份证明DApp,学习如何开发Circom零知识电路、如何生成并方法Solidity零知识验证智能合约,以及如何利用Javascript在链下生成零知识证据,并在教程最后提供完整的源代码下载。

区块链开发教程链接:以太坊 |比特币 | EOS | Tendermint | Hyperledger Fabric | Omni/USDT | Ripple

1、零知识身份证明DApp概述

我们将开发一个零知识应用来证明一个用户属于特定的群组而无需透露用户的具体信息,使用流程如下图所示:

在这里插入图片描述

我们的开发过程分为以下几个步骤:

  • 开发零知识电路
  • 生成用于验证零知识电路的Solidity库
  • 开发智能合约并集成上述Solidity库
  • 本地生成证据并在链上进行验证

2、零知识证明以太坊DApp开发环境搭建

就像你不需要完全理解HTTP协议也可以开发web应用一样,已经有很多工具可以帮助开发基于零知识的DApp而无需密码学或数学基础。

我推荐如下的开发语言和工具链:

  • JavaScript/TypeScript:应用采用javascript/typescript开发,因为这两者在以太坊生态中得到很好的支持
  • Solidity: 智能合约用Solidity开发,因为它很成熟并且社区很好
  • Truffle:使用Truffle作为智能合约开发和部署框架
  • Circom:使用Circom来开发零知识证明电路

3、CIRCOM零知识电路开发:判断私钥是否匹配公钥集

我们的目标是创建一个电路,该电路可以判别输入的私钥是否对应于输入的公钥集合之一。该电路的伪代码如下:

// Note that a private key is a scalar value (int)
// whereas a public key is a point in space (Tuple[int, int])
const zk_identity = (private_key, public_keys) => {
  // derive_public_from_private is a function that
  // returns a public key given a private key
  derived_public_key = derive_public_from_private(private_key)
  for (let pk in public_keys):
    if derived_public_key === pk:
      return true
  return false
}

我们现在要开始用circom编写零知识电路了。circom的语法可以查阅其官方文档

首先创建项目文件夹并安装必要的依赖包:

npm install circom circomlib snarkjs websnark

mkdir contracts
mkdir circuits
mkdir -p build/circuits

touch circuits/circuit.circom

现在编写电路文件circuit.circom,首先引入(incluude)必要的基础电路并定义PublicKey模板:

include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/escalarmulfix.circom";
include "../node_modules/circomlib/circuits/comparators.circom";

template PublicKey() {
  // Note: private key needs to be hashed, and then pruned
  // to make sure its compatible with the babyJubJub curve
  signal private input in;
  signal output out[2];

  component privBits = Num2Bits(253);
  privBits.in <== in;

  var BASE8 = [
    5299619240641551281634865583518297030282874472190772894086521144482721001553,
    16950150798460657717958625567821834550301663161624707787222815936182638968203
  ];

  component mulFix = EscalarMulFix(253, BASE8);
  for (var i = 0; i < 253; i++) {
    mulFix.e[i] <== privBits.out[i];
  }

  out[0] <== mulFix.out[0];
  out[1] <== mulFix.out[1];
}

PublicKey模板的作用是在babyJubJub曲线上找出私钥(电路输入)对应的公钥(电路输出)。注意在上面的电路中,我们将输入私钥声明为私有信号,因此在生成的证据中不会包含任何可以重构该输入私钥的信息。

一旦完成上述的基础模块,现在就可以构建我们的零知识证明电路的主逻辑了 —— 验证指定的用户是否属于一个群组:

include ...

template PublicKey() {
  ...
}

template ZkIdentity(groupSize) {
  // Public Keys in the smart contract
  // Note: this assumes that the publicKeys
  // are all unique
  signal input publicKeys[groupSize][2];

  // Prover's private key
  signal private input privateKey;

  // Prover's derived public key
  component publicKey = PublicKey();
  publicKey.in <== privateKey;

  // Make sure that derived public key needs to
  // matche to at least one public key in the
  // smart contract to validate their identity
  var sum = 0;

  // Create a component to check if two values are
  // equal
  component equals[groupSize][2];
  for (var i = 0; i < groupSize; i++) {
    // Helper component to check if two
    // values are equal
    // We don't want to use ===
    // as that will fail immediately if
    // the predicate doesn't hold true
    equals[i][0] = IsEqual();
    equals[i][1] = IsEqual();

    equals[i][0].in[0] <== publicKeys[i][0];
    equals[i][0].in[1] <== publicKey.out[0];

    equals[i][1].in[0] <== publicKeys[i][1];
    equals[i][1].in[1] <== publicKey.out[1];

    sum += equals[i][0].out;
    sum += equals[i][1].out;
  }

  // equals[i][j].out will return 1 if the values are equal
  // and 0 if the values are not equal
  // Therefore, if the derived public key (a point in space)
  // matches a public keys listed in the smart contract, the sum of
  // all the equals[i][j].out should be equal to 2
  sum === 2;
}


// Main entry point
component main = ZkIdentity(2);

现在我们编译、设置并生成该电路的Solidity验证器:

$(npm bin)/circom circuits/circuit.circom -o build/circuits/circuit.json

# snarkjs setup might take a few seconds
$(npm bin)/snarkjs setup --protocol groth -c build/circuits/circuit.json --pk build/circuits/provingKey.json --vk build/circuits/verifyingKey.json

# Generate solidity lib to verify proof
$(npm bin)/snarkjs generateverifier --pk build/circuits/provingKey.json --vk build/circuits/verifyingKey.json -v contracts/Verifier.sol

# You should now have a new "Verifier.sol" in your contracts directory
# $ ls contracts
# Migrations.sol Verifier.sol

注意我们使用groth协议生成证明密钥和验证密钥,因为我们希望使用websnark来生成证据,因为websnark要比snarkjs性能好的多。

一旦完成上面的环节,我们就已经实现了零知识证明逻辑。下面的部分我们将介绍如何使用生成的Solidity零知识验证合约。

4、Solidity零知识验证合约

在完成零知识电路的设置之后,会生成一个名为Verifier.sol的solidity库。如果你查看这个文件的内容,就会看到其中包含如下的函数:

...

  function verifyProof(
            uint[2] memory a,
            uint[2][2] memory b,
            uint[2] memory c,
            uint[4] memory input
        ) public view returns (bool r) {
        Proof memory proof;
        proof.A = Pairing.G1Point(a[0], a[1]);
        proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
        proof.C = Pairing.G1Point(c[0], c[1]);
        uint[] memory inputValues = new uint[](input.length);
        for(uint i = 0; i < input.length; i++){
            inputValues[i] = input[i];
        }
        if (verify(inputValues, proof) == 0) {
            return true;
        } else {
            return false;
        }
    }

...

这是用于验证零知识证据有效性的辅助函数。verifyProof函数接收4个参数,但是我们只关心其中表示电路公共输入的input参数,我们将使用它在智能合约代码中验证用户的身份。让我们看一下具体的实现代码:

pragma solidity 0.5.11;

import "./Verifier.sol";

contract ZkIdentity is Verifier {
    address public owner;
    uint256[2][2] public publicKeys;

    constructor() public {
        owner = msg.sender;
        publicKeys = [
            [
                11588997684490517626294634429607198421449322964619894214090255452938985192043,
                15263799208273363060537485776371352256460743310329028590780329826273136298011
            ],
            [
                3554016859368109379302439886604355056694273932204896584100714954675075151666,
                17802713187051641282792755605644920157679664448965917618898436110214540390950
            ]
        ];
    }

    function isInGroup(
        uint256[2] memory a,
        uint256[2][2] memory b,
        uint256[2] memory c,
        uint256[4] memory input // public inputs
    ) public view returns (bool) {
        if (
            input[0] != publicKeys[0][0] &&
            input[1] != publicKeys[0][1] &&
            input[2] != publicKeys[1][0] &&
            input[3] != publicKeys[1][1]
        ) {
            revert("Supplied public keys do not match contracts");
        }

        return verifyProof(a, b, c, input);
    }
}

我们创建一个新的合约ZkIdentity.sol,它继承自生成的Verifier.sol,有一个包含2个成员公钥的初始群组,以及一个名为isInGroup的函数,该函数首先验证电路的公开输入信号与智能合约中的群组一致,然后返回对输入证据的验证结果。

逻辑并不复杂,不过的确也满足了我们的目标:验证一个用户属于特定的群组而无需透露用户是谁。

在继续下面的部分之前,需要先部署合约到链上。

5、用JavaScript生成零知识证据并与智能合约交互

一旦我们完成了零知识电路并实现了智能合约逻辑,就可以生成证据并调用智能合约的isInGroup方法进行验证了。

下面的伪代码展示了如何生成证据并利用智能合约进行验证,你可以访问这里 查看完整的js代码:

// Assuming below already exists
const provingKey // provingKey.json
const circuit // zero-knowledge circuit we wrote
const zkIdentityContract // Zk-Identity contract instance

const privateKey  // Private key that corresponds to one of the public key in the smart contract
const publicKeys = [
  [
      11588997684490517626294634429607198421449322964619894214090255452938985192043n,
      15263799208273363060537485776371352256460743310329028590780329826273136298011n
  ],
  [
      3554016859368109379302439886604355056694273932204896584100714954675075151666n,
      17802713187051641282792755605644920157679664448965917618898436110214540390950n
  ]
]

const circuitInputs = {
  privateKey,
  publicKeys
}

const witness = circuit.calculateWitness(circuitInputs)
const proof = groth16GenProof(witness, provingKey)

const isInGroup = zkIdentityContract.isInGroup(
  proof.a,
  proof.b,
  proof.c,
  witness.publicSignals
)

运行js代码就可以证明你属于一个群组而无需透露你是谁!

教程的完整代码下载地址


原文链接:零知识证明DApp开发实践 — 汇智网

目录
相关文章
|
18天前
|
设计模式 监控 安全
《Solidity 简易速速上手小册》第5章:智能合约的安全性(2024 最新版)
《Solidity 简易速速上手小册》第5章:智能合约的安全性(2024 最新版)
70 1
|
18天前
|
供应链 监控 安全
《区块链简易速速上手小册》第5章:智能合约(2024 最新版)
《区块链简易速速上手小册》第5章:智能合约(2024 最新版)
30 1
|
18天前
|
存储 安全 区块链
《Solidity 简易速速上手小册》第4章:智能合约的设计与开发(2024 最新版)
《Solidity 简易速速上手小册》第4章:智能合约的设计与开发(2024 最新版)
71 0
|
18天前
|
存储 区块链 数据安全/隐私保护
《区块链简易速速上手小册》第4章:区块链与加密货币(2024 最新版)
《区块链简易速速上手小册》第4章:区块链与加密货币(2024 最新版)
33 1
|
7月前
|
算法 区块链 数据库
MeatForce佛萨奇2.0合约系统开发|DAPP开发
区块链合约交易源码的优势在于其去中心化和透明的特性
|
7月前
|
区块链 数据安全/隐私保护
DAPP众筹互助系统开发|DAPP公排合约系统开发指南
去中心化区块链作为分布式技术的一种创新形式,提供了一种去中心化、透明和可信的数据处理方式
|
8月前
|
供应链 安全 区块链
DAPP矩阵公排合约互助系统开发模式|DAPP互助系统开发指南
智能合约的工作原理基于区块链的去中心化特性
|
11月前
|
新零售 人工智能 JSON
NFT/DAPP盲盒游戏系统开发(开发方案),DAPP/NFT盲盒游戏系统开发(项目案例)及源码部署
新零售是线上与线下结合,组合的价值主要是线下为线上引流,降低引流投入,线下成为线上的体验店,提高转化和复购,线上和线下双渠道一起提高周转,提高效率,线上和线下虽然各自核算收入和投入,但相互之间能产生互促效应。
|
11月前
|
Go 区块链
以太坊挖矿系统开发二开源码
以太坊挖矿系统开发二开源码
DAPP盲盒游戏开发原理丨DAPP盲盒游戏系统开发(开发方案及详细)丨DAPP盲盒游戏源码部署
 New retail is a new retail model in which enterprises,relying on the Internet,upgrade and transform the production,circulation and sales process of goods by using advanced technology such as big data and artificial intelligence,and then reshape the business structure and ecosystem,and deeply integr

热门文章

最新文章