基于circom、snarkjs实现零知识证明不透漏具体地理位置的区域监控

简介: 文章介绍了如何使用circom和snarkjs工具基于零知识证明算法Groth16实现不泄露具体地理位置的区域监控系统,详细说明了开发环境搭建、电路设计、计算和证明过程,并提供了相应的命令和代码示例。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

第一章 背景

  1. 实现目标:基于地理位置坐标实现对用户所在区域的零知识监控,即用户无需无需上报个人详细的地理位置信息,即可证明自己在合法的区域。

  2. Zk算法:Zk算法基于Groth16实现,该算法目前是公认的在加密领域应用最多的算法,包括著名ZCash,其生成的证据最小,认证时间较快。

第二章 开发准备

  1. 依赖组件:circom 、snarkjs

  2. 开发环境:Visual Code

    环境安装

    1. 安装Node(如果已经安装请跳过)

      sudo apt update
      sudo apt install nodejs npm -y
      node --version
      
    2. 安装Rust(如果已经安装请跳过)

      sudo apt install curl -y
      sudo apt install cmake build-essential -y
      curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
      
    3. 安装Circom(如果已经安装请跳过)

      git clone https://github.com/iden3/circom.git
      cd circom
      source $HOME/.cargo/env
      cargo build --release
      

      在这一步特别提醒一下,如果用户已经安装了请确保安装了正确的版本,因为之前用户可能通过执行npm install circom命令来安装的,这种方法提示也是安装成功,但是版本和库都不对,此时需要通过执行which circom命令来查看具体安装的位置,然后进入到对应的目录把相关的文件夹全部删除掉,通过执行npm uninstall circom是没有效果的。最后可以通过执行circom --version,如果显示的是类似2.*.*就代表OK了。

    4. 更新Circom

      cargo install --path circom
      circom --help
      
    5. 安装Snarkjs(如果已经安装请跳过)

      sudo npm install -g snarkjs
      

      如果执行到此处都没有错误的话,那么就是安装环境OK了。

### 第三章 计算和证明

#### 1\. 设计电路

在VS code中新建文件InRange.circom,编写如下电路代码

```auto

template Main() {
    signal input latitudeRange[2];
    signal input longitudeRange[2];

    signal input fishingLocation[2];

    signal output out;

    component gt1 = GreaterEqThan(9);
    gt1.in[0] <== fishingLocation[0];
    gt1.in[1] <== latitudeRange[0];
    gt1.out === 1;
}

template GreaterEqThan(n) {
    signal input in[2];
    signal output out;

    component lt = LessThan(n);

    lt.in[0] <== in[1];
    lt.in[1] <== in[0]+1;
    lt.out ==> out;
}

template LessThan(n) {
    assert(n <= 252);
    signal input in[2];
    signal output out;

    component n2b = Num2Bits(n+1);

    n2b.in <== in[0]+ (1<<n) - in[1];

    out <== 1-n2b.out[n];
}

template Num2Bits(n) {
    signal input in;
    signal output out[n];
    var lc1=0;

    var e2=1;
    for (var i = 0; i<n; i++) {
        out[i] <-- (in >> i) & 1;
        out[i] * (out[i] -1 ) === 0;
        lc1 += out[i] * e2;
        e2 = e2+e2;
    }

    lc1 === in;
}

component main = Main();
```

#### 2\. 编译电路

执行如下命令进行编译

```auto
circom InRange.circom --r1cs --wasm --sym
```

> With these options we generate three types of files:
> 
> `--r1cs`: it generates the file `multiplier2.r1cs` that contains the [R1CS constraint system](https://docs.circom.io/background/background#rank-1-constraint-system "R1CS constraint system") of the circuit in binary format.
> 
> `--wasm`: it generates the directory `multiplier2_js` that contains the `Wasm` code (multiplier2.wasm) and other files needed to generate the [witness](https://docs.circom.io/background/background#witness "witness").
> 
> `--sym` : it generates the file `multiplier2.sym` , a symbols file required for debugging or for printing the constraint system in an annotated mode.
> 
> `--c` : it generates the directory `multiplier2_cpp` that contains several files (multiplier2.cpp, multiplier2.dat, and other common files for every compiled program like main.cpp, MakeFile, etc) needed to compile the C code to generate the witness.
  1. 执行成功后,会生成如下截图的文件1:

3. 设计输入

设计电路的输入是下一步计算证明的必要前提,在相同目录下新建input.json输入文件,编写如下输入,本案例中即一些地理位置数据:

{
    "latitudeRange": [ 20, 21],
    "longitudeRange": [ 176, 190],
    "fishingLocation": [ 20, 180]
}

4. 计算获得证据(Computing the witness)

  1. 利用WebAssembly计算证据,即执行如下命令:

    node generate_witness.js InRange.wasm ../input.json ../witness.wtns
    

    该命令会输出witness.wtns,下一步运算需要用到。

5. 证明电路(Proving circuits with zk)

  1. Phase1: Powers of Tau

    First, we start a new "powers of tau" ceremony,即执行如下命令,生成 pot12_0000.ptau

    snarkjs powersoftau new bn128 12 pot12_0000.ptau -v
    

    Then, we contribute to the ceremony,即执行如下命令,生成pot12_0001.ptau,此处会提示输入随机字符,随意输入即可。

    snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v
    
  2. Phase2: circuit-specific

    start the generation of this phase,即执行如下命令,生成pot12_final.ptau

    snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau -v
    

    Next,we generate a .zkey file that will contain the proving and verification keys together with all phase 2 contributions.即执行如下命令,生成

    snarkjs groth16 setup InRange.r1cs pot12_final.ptau multiplier2_0000.zkey
    

    Contribute to the phase 2 of the ceremony,即执行如下命令:

    snarkjs zkey contribute multiplier2_0000.zkey multiplier2_0001.zkey --name="1st Contributor Name" -v\
    

    Export the verification key,执行如下命令:

    snarkjs zkey export verificationkey multiplier2_0001.zkey verification_key.json
    
  3. Generating a Proof

    Once the witness is computed and the trusted setup is already executed, we can generate a zk-proof associated to the circuit and the witness,即执行如下命令:

    snarkjs groth16 prove multiplier2_0001.zkey witness.wtns proof.json public.json
    
  4. Verifying a Proof

    To verify the proof,执行如下命令:

    snarkjs groth16 verify verification_key.json public.json proof.json
    

    The command uses the files verification_key.json we exported earlier,proof.json and public.json to check if the proof is valid. If the proof is valid, the command outputs an OK.

    最终证明输出结果截图如下:

第四章 参考:

  1. https://iden3.io/circom

  2. https://www.samsclass.info/141/proj/C523.htm

相关文章
|
JavaScript 前端开发 区块链
|
10月前
|
传感器 存储 数据采集
基于 STM32 的睡眠质量检测仪设计与实现【开源免费】
在当今快节奏的生活方式下,越来越多的人面临 失眠、睡眠不足、深度睡眠时间偏短 等健康问题。良好的睡眠不仅是缓解疲劳的关键,更是维持身体免疫力和心理健康的重要保障。传统的睡眠质量检测往往依赖昂贵的医疗设备或专业睡眠实验室,而这些方式成本高、使用不便,不适合日常监测。
基于 STM32 的睡眠质量检测仪设计与实现【开源免费】
|
9月前
|
缓存 监控 算法
京东item_search_best 畅销榜接口深度分析及 Python 实现
京东item_search_best接口可实时获取京东各品类畅销商品排名、销量、价格等核心数据,支持多维度榜单分析与品牌竞品监控,助力商家精准选品、制定市场策略,全面把握消费趋势。
|
10月前
|
网络安全 数据安全/隐私保护 开发者
诊断并修复SSH连接Github时遇到的"connection closed"错误。
解决"connection closed"错误往往是一个排除法的过程。需要从基础的网络检查做起,逐步过渡到深入的配置和服务端日志审查。每一步都应当仔细验证,确保不遗漏可能导致连接问题的任何细节。在执行以上步骤后,大多数SSH连接问题可以得到解决。如果所有步骤都未能解决问题,可能需要寻求更专业的技术支持,或者在GitHub社区寻找是否有其他开发者遇到并解决了类似的问题。
1221 0
|
人工智能 编解码
CogVideoX-Flash:智谱首个免费AI视频生成模型,支持文生视频、图生视频,分辨率最高可达4K
CogVideoX-Flash 是智谱推出的首个免费AI视频生成模型,支持文生视频、图生视频,最高支持4K分辨率,广泛应用于内容创作、教育、广告等领域。
1662 5
CogVideoX-Flash:智谱首个免费AI视频生成模型,支持文生视频、图生视频,分辨率最高可达4K
|
供应链 算法 安全
探索区块链技术中的隐私保护机制
探索区块链技术中的隐私保护机制
611 27
|
机器学习/深度学习 人工智能 算法
ProtGPS:MIT再造生命科学新基建!蛋白质AI一键预测定位+设计新序列,登Nature子刊
ProtGPS 是麻省理工学院和怀特黑德研究所联合开发的蛋白质语言模型,能够预测蛋白质在细胞内的亚细胞定位,并设计具有特定亚细胞定位的新型蛋白质。
1182 17
ProtGPS:MIT再造生命科学新基建!蛋白质AI一键预测定位+设计新序列,登Nature子刊
|
Ubuntu Shell
ubuntu追加path环境变量
以上是追加PATH环境变量的方法。不同的方法适用于不同的应用场景,你可以根据自己的需求选择适合的方法。需要注意的是,在对系统文件进行更改时,一定要确保正确无误,避免系统的命令路径出错导致不必要的麻烦。
1470 3
ubuntu追加path环境变量
|
存储 弹性计算 搜索推荐
快速部署 Qdrant 社区版
Qdrant是一个矢量相似性搜索引擎,提供生产就绪服务和方便的 API,用于存储、搜索和管理具有额外负载的点(即矢量)。您可以将有效负载视为附加信息,可以帮助您深入搜索并接收可以提供给用户的有用信息。本文介绍如何使用计算巢快速部署Qdrant服务。
快速部署 Qdrant 社区版
|
机器学习/深度学习 语音技术
语音情感基座模型emotion2vec 问题之emotion2vec模型进行预训练,如何操作
语音情感基座模型emotion2vec 问题之emotion2vec模型进行预训练,如何操作
900 1