我在玩circom和circomlib。
我正在使用一个简单的mimcsponge哈希电路,看看是否可以通过javascript前端创建正确的输入。
我正在运行的电路
template sponge_test() {
signal input l;
signal input r;
signal input o;
// instantiate - 2 inputs 220 rounds of hashing and 1 output
component hasher = MiMCSponge(2, 220, 1);
// signals in hasher
hasher.ins[0] <== l;
hasher.ins[1] <== r;
// addition constant
hasher.k <== 0;
o === hasher.outs[0];
}
component main = sponge_test();
在我的javascript前端,我正在导入circomlib
import { buildMimcSponge } from 'circomlibjs';
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
export async function getProof(message) {
var hasher = await buildMimcSponge();
var h = hasher.multiHash([BigInt("0x3"), BigInt("0x4")]);
// returns byte array
console.log(h);
// back to hexstring
console.log(toHexString(h));
}
然后我创建了一个input.json,看起来像这样:
{
"l": "0x3",
"r": "0x4",
"o": "0x690f48aba976f2786371b7fa3e941df623e96329e0570dc610f59b7fcfa94723"
}
其中包括我用于哈希输入的值和打印十六进制值得到的输出,然后运行以下脚本
# Compile the circuit
circom ${CIRCUIT}.circom --r1cs --wasm --sym --c
# Generate the witness.wtns
node ${CIRCUIT}_js/generate_witness.js ${CIRCUIT}_js/${CIRCUIT}.wasm input.json ${CIRCUIT}_js/witness.wtns
并得到断言(o===hasher.outs[0])
失败的错误。
现在,通过查看节点库,我知道mimcsponge电路在circomlib的javascript实现中也使用了220轮,我还能在哪里得到不一致的哈希结果?
所以我发现阅读has是使用以下内容完成的。我相信这是因为它特定于正在使用的椭圆曲线。
hasher.F.toString(h, 16);
这产生了电路所接受的预期结果。
如果有人有进一步的见解,我很乐意进一步了解。