基于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

相关文章
|
4月前
|
存储 定位技术 数据库
如何使用Qchan搭建更好保护个人隐私的本地图床并在公网可访问
如何使用Qchan搭建更好保护个人隐私的本地图床并在公网可访问
|
3月前
|
机器学习/深度学习 安全 网络安全
【计算巢】数字取证:追踪和分析网络犯罪的方法
【6月更文挑战第4天】本文探讨了数字取证在网络安全中的关键作用,通过Python编程展示如何分析网络日志以发现线索。数字取证利用科学方法收集、分析电子数据,以应对黑客入侵、数据泄露等网络犯罪。文中提供的Python代码示例演示了从服务器日志中提取IP地址并统计访问次数,以识别异常行为。此外,实际的数字取证还包括数据恢复、恶意软件分析等复杂技术,并需遵循法律程序和伦理标准。随着技术发展,数字取证将更有效地保障网络空间的和平与秩序。
63 5
|
4月前
|
监控 安全 5G
UWB人员精准定位系统源码,实现实时定位、人机料配对、物料标签配置、智慧调度、轨迹追踪
人员定位管理系统通过在厂区、车间部署UWB定位基站,实时采集人员、机具、物料上定位标签回传的位置信息数据,采用多维定位模式,精确定位人、机具、物料的实时位置,实现实时定位、人机料配对、物料标签配置、智慧调度、轨迹追踪、工时统计、区域物料统计、电子围栏等应用功能。
|
机器学习/深度学习 编解码 监控
智慧交通day04-特定目标车辆追踪01:总览概述
通过对目标外观模型进行建模, 然后在之后的帧中找到目标. 例如, 区域匹配、特征点跟踪、基于主动轮廓的跟踪算法、光流法等. 最常用的是特征匹配法, 首先提取目标特征, 然后在后续的帧中找到最相似的特征进行目标定位, 常用的特征有: SIFT特征、SURF特征、Harris角点等.
106 0
|
分布式计算 安全 BI
报表统计_数据的区域分布_环境准备 | 学习笔记
快速学习报表统计_数据的区域分布_环境准备
111 0
报表统计_数据的区域分布_环境准备 | 学习笔记
|
分布式计算 Java BI
报表统计_数据的区域分布_代码开发 | 学习笔记
快速学习报表统计_数据的区域分布_代码开发
129 0
报表统计_数据的区域分布_代码开发 | 学习笔记
|
数据采集 Web App开发 存储
什么是内容农场-充斥着劣质信息的采集站
这个充满劣质信息的收集站有一个特殊的名字——内容农场,Wikipedia有一个特殊的条目来介绍它
396 0
|
Web App开发 安全
绝密追踪:利用像素图片收集攻击目标信息
本文讲的是绝密追踪:利用像素图片收集攻击目标信息,网络犯罪团伙正在滥用一种常见的邮件营销手段。通过名为“像素图片追踪”(pixel tracking)的技术,他们可以收集攻击目标的网络信息提高钓鱼攻击效率。
2163 0
|
监控 定位技术
老司机教你分析日志:分析用户的地理位置信息
地理位置的需求 通常我们分析用户的需求,了解到用户当前位置在哪里非常重要,例如,可以根据用户的地理位置,针对性的推广本地广告。 通常,我们可以在客户端获取定位权限来获取GPS信息。但是如果用户关闭了定位呢?如何获取呢?我们还有另外一种方法,就是通过用户当前的IP来定位。
4151 0