作为更大用例的单元测试,我正在检查我在前端执行的 pedersen 哈希确实与通过 circom 电路完成的预期哈希一致。我在电路中使用一个简单的断言并生成一个见证,并将散列值和未散列值提供给电路,重新创建散列以确保它通过。
我正在使用 circomlibjs 在前端运行 Pedersen 哈希。作为单元测试,我有。一个带有简单断言的电路,用于检查前端的结果是否与 circom 电路中的 pedersen 哈希值一致。
我正在使用的电路:
include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/pedersen.circom";
template check() {
signal input unhashed;
signal input hashed;
signal output createdHash[2];
component hasher = Pedersen(256);
component unhashedBits = Num2Bits(256);
unhashedBits.in <== unhashed;
for (var i = 0; i < 256; i++){
hasher.in[i] <== unhashedBits.out[i];
}
createdHash[0] <== hasher.out[0];
createdHash[1] <== hasher.out[1];
hashed === createdHash[1];
}
component main = check();
Run Code Online (Sandbox Code Playgroud)
在前端,我正在运行以下命令,
import { buildPedersenHash } from 'circomlibjs';
export function buff2hex(buff) { …Run Code Online (Sandbox Code Playgroud) 我按照官方文档使用这个电路:
pragma circom 2.0.0;
/*This circuit template checks that c is the multiplication of a and b.*/
template Multiplier2 () {
// Declaration of signals.
signal input a;
signal input b;
signal output c;
// Constraints.
c <== a * b;
}
Run Code Online (Sandbox Code Playgroud)
并提供以下输入文件(input.json):
{"a": "3", "b": "11"}
Run Code Online (Sandbox Code Playgroud)
然后编译生成witness/proof,并验证:
circom multiplier2.circom --r1cs --wasm --sym --c
node generate_witness.js multiplier2.wasm input.json witness.wtns
snarkjs powersoftau new bn128 12 pot12_0000.ptau -v
snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v
snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau …Run Code Online (Sandbox Code Playgroud)