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-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-03-16 19:59:08 +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-03-16 19:59:08 +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-03-16 19:59:08 +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-03-16 19:59:08 +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];
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Storer test", function () {
|
|
|
|
this.timeout(100000);
|
|
|
|
|
|
|
|
const a = Array.from(crypto.randomBytes(32).values()).map((v) => BigInt(v));
|
2023-03-08 00:27:00 +00:00
|
|
|
const aHash = digest(a);
|
2023-02-09 16:40:23 +00:00
|
|
|
const b = Array.from(crypto.randomBytes(32).values()).map((v) => BigInt(v));
|
2023-03-08 00:27:00 +00:00
|
|
|
const bHash = digest(b);
|
2023-02-09 16:40:23 +00:00
|
|
|
const c = Array.from(crypto.randomBytes(32).values()).map((v) => BigInt(v));
|
2023-03-08 00:27:00 +00:00
|
|
|
const cHash = digest(c);
|
2023-02-09 16:40:23 +00:00
|
|
|
const d = Array.from(crypto.randomBytes(32).values()).map((v) => BigInt(v));
|
2023-03-08 00:27:00 +00:00
|
|
|
const dHash = digest(d);
|
2023-02-09 16:40:23 +00:00
|
|
|
const salt = Array.from(crypto.randomBytes(32).values()).map((v) => BigInt(v));
|
2023-03-08 00:27:00 +00:00
|
|
|
const saltHash = digest(salt);
|
2023-02-09 16:40:23 +00:00
|
|
|
|
|
|
|
it("Should merkelize", async () => {
|
|
|
|
let root = merkelize([aHash, bHash]);
|
2023-03-16 19:59:08 +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 () => {
|
|
|
|
const cir = await wasm_tester(path.join(__dirname, "./circuits", "storer_test.circom"));
|
|
|
|
|
|
|
|
const root = merkelize([aHash, bHash, cHash, dHash]);
|
|
|
|
|
2023-03-16 19:59:08 +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);
|
|
|
|
}).timeout(100000);
|
|
|
|
|
|
|
|
it("Should verify chunk is correct and part of dataset", async () => {
|
|
|
|
const cir = await wasm_tester(path.join(__dirname, "./circuits", "storer_test.circom"));
|
|
|
|
|
|
|
|
const root = merkelize([aHash, bHash, cHash, dHash]);
|
|
|
|
|
2023-03-16 19:59:08 +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/);
|
|
|
|
|
|
|
|
}).timeout(100000);
|
|
|
|
});
|