From 53392c8722d8ffa295b1446320eb41475ccf7ca6 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Thu, 24 Nov 2022 09:20:54 +0100 Subject: [PATCH] add first merkletree test with Poseidon hash Signed-off-by: Csaba Kiraly --- package.json | 6 +++-- test/merkletree.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 test/merkletree.js diff --git a/package.json b/package.json index 4708634..ecd28c2 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "author": "", "license": "ISC", "dependencies": { + "@zk-kit/protocols": "^1.11.1", + "circomlib": "^2.0.2", + "circomlibjs": "^0.0.8", "mocha": "^10.1.0", - "snarkjs": "^0.5.0", - "circomlib": "^2.0.2" + "snarkjs": "^0.5.0" } } diff --git a/test/merkletree.js b/test/merkletree.js new file mode 100644 index 0000000..851b94c --- /dev/null +++ b/test/merkletree.js @@ -0,0 +1,61 @@ +//import { generateMerkleProof, generateMerkleTree } from "@zk-kit/protocols" +import { IncrementalMerkleTree } from "@zk-kit/incremental-merkle-tree" +import { poseidon } from "circomlibjs" +import assert from "assert"; +import path from "path"; +import * as snarkjs from "snarkjs"; + +describe("MerkleTree", function () { + + const depth = 21 + const numberOfLeaves = 2 ** 2 + const hashFn = poseidon + const zeroValue = BigInt(0) + const arity = 2 + const queryLen = 1 + + const r1csPath = path.join("circuits", "storer.r1cs"); + const wasmPath = path.join("circuits", "storer_js", "storer.wasm"); + + var tree + it ("generate Merkle Tree from data", async () => { + tree = new IncrementalMerkleTree(hashFn, depth, zeroValue, arity) + for (let i = 0; i < numberOfLeaves; i += 1) { + tree.insert(BigInt(i + 1)) + } + }) + + var merkleProof + it ("create proof", async () => { + merkleProof = tree.createProof(0) + }) + + it ("verify proof", async () => { + assert(tree.verifyProof(merkleProof)) + console.warn(merkleProof) + }) + + const wtns = {type: "mem"}; + it ("witness calculate", async () => { + + // inputs defined in circuit: + // signal input chunks[qLen][blockSize]; + // signal input chunkHashes[qLen]; + // signal input indices[qLen]; + // signal input treeSiblings[qLen][nLevels]; + // signal input root; + + console.warn(hashFn([1])) + let chunks = [[1,2]] + await snarkjs.wtns.calculate( + { chunks: chunks, + chunkHashes: [hashFn(chunks[0])], + indices: [0], + treeSiblings: [merkleProof.siblings.slice(1)], + root: merkleProof.root + }, wasmPath, wtns); + console.warn("witness: ", wtns); + // console.warn("witness: ", wtns.data.length, " bytes"); + // console.warn("witness: ", sizeOf(wtns.data), " bytes"); + }) +})