2023-02-09 16:40:23 +00:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
|
|
|
const crypto = require("crypto");
|
|
|
|
const F1Field = require("ffjavascript").F1Field;
|
|
|
|
const Scalar = require("ffjavascript").Scalar;
|
|
|
|
const {c} = require("circom_tester");
|
|
|
|
const chaiAsPromised = require('chai-as-promised');
|
2023-03-08 00:27:00 +00:00
|
|
|
const poseidon = require("circomlibjs/src/poseidon");
|
|
|
|
const wasm_tester = require("circom_tester").wasm;
|
2023-04-12 22:17:00 +00:00
|
|
|
// const snarkjs = require("snarkjs");
|
|
|
|
// const fs = require("fs");
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
chai.use(chaiAsPromised);
|
|
|
|
|
2023-03-08 00:27:00 +00:00
|
|
|
const p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
|
|
|
const Fr = new F1Field(p);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
const expect = chai.expect;
|
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
function digest(input, chunkSize = 5) {
|
2023-03-08 00:27:00 +00:00
|
|
|
let chunks = Math.ceil(input.length / chunkSize);
|
|
|
|
let concat = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < chunks; i++) {
|
2023-04-12 22:17:00 +00:00
|
|
|
let chunk = input.slice(i * chunkSize, (i + 1) * chunkSize);
|
|
|
|
if (chunk.length < chunkSize) {
|
|
|
|
chunk = chunk.concat(Array(chunkSize - chunk.length).fill(0));
|
|
|
|
}
|
|
|
|
concat.push(poseidon(chunk));
|
2023-03-08 00:27:00 +00:00
|
|
|
}
|
2023-02-09 16:40:23 +00:00
|
|
|
|
2023-03-08 00:27:00 +00:00
|
|
|
if (concat.length > 1) {
|
|
|
|
return poseidon(concat);
|
|
|
|
}
|
|
|
|
|
|
|
|
return concat[0]
|
|
|
|
}
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
function merkelize(leafs) {
|
|
|
|
// simple merkle root (treehash) generator
|
|
|
|
// unbalanced trees will have the last leaf duplicated
|
|
|
|
var merkle = leafs;
|
|
|
|
|
|
|
|
while (merkle.length > 1) {
|
|
|
|
var newMerkle = [];
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
while (i < merkle.length) {
|
2023-04-12 22:17:00 +00:00
|
|
|
newMerkle.push(digest([merkle[i], merkle[i + 1]], 2));
|
2023-02-09 16:40:23 +00:00
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (merkle.length % 2 == 1) {
|
2023-04-12 22:17:00 +00:00
|
|
|
newMerkle.add(digest([merkle[merkle.length - 2], merkle[merkle.length - 2]], 2));
|
2023-02-09 16:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
merkle = newMerkle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return merkle[0];
|
|
|
|
}
|
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
// TODO: should be removed at some point, as the rust test should be sufficient, but left here for now to aid debugging
|
|
|
|
|
2023-02-09 16:40:23 +00:00
|
|
|
describe("Storer test", function () {
|
|
|
|
this.timeout(100000);
|
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
const a = Array.from(crypto.randomBytes(256).values()).map((v) => BigInt(v));
|
|
|
|
const aHash = digest(a, 16);
|
|
|
|
const b = Array.from(crypto.randomBytes(256).values()).map((v) => BigInt(v));
|
|
|
|
const bHash = digest(b, 16);
|
|
|
|
const c = Array.from(crypto.randomBytes(256).values()).map((v) => BigInt(v));
|
|
|
|
const cHash = digest(c, 16);
|
|
|
|
const d = Array.from(crypto.randomBytes(256).values()).map((v) => BigInt(v));
|
|
|
|
const dHash = digest(d, 16);
|
|
|
|
const salt = Array.from(crypto.randomBytes(256).values()).map((v) => BigInt(v));
|
|
|
|
const saltHash = digest(salt, 16);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
it("Should merkelize", async () => {
|
|
|
|
let root = merkelize([aHash, bHash]);
|
2023-04-12 22:17:00 +00:00
|
|
|
let hash = digest([aHash, bHash], 2);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
assert.equal(hash, root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should verify chunk is correct and part of dataset", async () => {
|
2023-04-12 22:17:00 +00:00
|
|
|
const cir = await wasm_tester("src/circuit_tests/storer-test.circom");
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
const root = merkelize([aHash, bHash, cHash, dHash]);
|
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
const parentHashL = digest([aHash, bHash], 2);
|
|
|
|
const parentHashR = digest([cHash, dHash], 2);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
await cir.calculateWitness({
|
|
|
|
"chunks": [[a], [b], [c], [d]],
|
2023-03-08 00:27:00 +00:00
|
|
|
"siblings": [
|
|
|
|
[bHash, parentHashR],
|
|
|
|
[aHash, parentHashR],
|
|
|
|
[dHash, parentHashL],
|
|
|
|
[cHash, parentHashL]],
|
2023-02-09 16:40:23 +00:00
|
|
|
"hashes": [aHash, bHash, cHash, dHash],
|
|
|
|
"path": [0, 1, 2, 3],
|
|
|
|
"root": root,
|
|
|
|
"salt": saltHash,
|
|
|
|
}, true);
|
2023-04-12 22:17:00 +00:00
|
|
|
});
|
2023-02-09 16:40:23 +00:00
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
it("Should verify chunk is not correct and part of dataset", async () => {
|
|
|
|
const cir = await wasm_tester("src/circuit_tests/storer-test.circom");
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
const root = merkelize([aHash, bHash, cHash, dHash]);
|
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
const parentHashL = digest([aHash, bHash], 2);
|
|
|
|
const parentHashR = digest([cHash, dHash], 2);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
const fn = async () => {
|
|
|
|
return await cir.calculateWitness({
|
2023-03-08 00:27:00 +00:00
|
|
|
"chunks": [
|
|
|
|
[salt], // wrong chunk
|
|
|
|
[b],
|
|
|
|
[c],
|
|
|
|
[d]],
|
|
|
|
"siblings": [
|
|
|
|
[bHash, parentHashR],
|
|
|
|
[aHash, parentHashR],
|
|
|
|
[dHash, parentHashL],
|
|
|
|
[cHash, parentHashL]],
|
2023-02-09 16:40:23 +00:00
|
|
|
"hashes": [saltHash, bHash, cHash, dHash],
|
|
|
|
"path": [0, 1, 2, 3],
|
|
|
|
"root": root,
|
|
|
|
"salt": saltHash,
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.isRejected(
|
|
|
|
fn(), Error,
|
|
|
|
/Error: Error: Assert Failed.\nError in template StorageProver_7 line: 75/);
|
2023-04-12 22:17:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function range(start, end) {
|
|
|
|
return Array(end - start + 1).fill().map((_, idx) => start + idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
it("Should test poseidon digest", async () => {
|
|
|
|
const cir = await wasm_tester("src/circuit_tests/poseidon-digest-test.circom");
|
|
|
|
let input = range(0, 255).map((c) => BigInt(c));
|
|
|
|
await cir.calculateWitness({
|
|
|
|
"block": input,
|
|
|
|
"hash": digest(input, 16),
|
|
|
|
});
|
|
|
|
});
|
2023-02-09 16:40:23 +00:00
|
|
|
|
2023-04-12 22:17:00 +00:00
|
|
|
// it("Should prove digest with zkey file", async () => {
|
|
|
|
// let input = range(0, 255).map((c) => BigInt(c));
|
|
|
|
// const {proof, publicSignals} = await snarkjs.groth16.fullProve(
|
|
|
|
// {
|
|
|
|
// "block": input,
|
|
|
|
// "hash": digest(input, 16),
|
|
|
|
// },
|
|
|
|
// "src/circuit_tests/artifacts/poseidon-digest-test_js/poseidon-digest-test.wasm",
|
|
|
|
// "circuit_0000.zkey");
|
|
|
|
|
|
|
|
// const vKey = JSON.parse(fs.readFileSync("verification_key.json"));
|
|
|
|
// const res = await snarkjs.groth16.verify(vKey, publicSignals, proof);
|
|
|
|
// assert(res);
|
|
|
|
// });
|
2023-02-09 16:40:23 +00:00
|
|
|
});
|