diff --git a/test/circuit/circuit.circom b/test/circuit/circuit.circom new file mode 100644 index 0000000..80694ad --- /dev/null +++ b/test/circuit/circuit.circom @@ -0,0 +1,16 @@ +template Multiplier(n) { + signal private input a; + signal private input b; + signal output c; + + signal int[n]; + + int[0] <== a*a + b; + for (var i=1; i 0, + "boolean": () => 4, + "number": () => 8, + "string": item => 2 * item.length, + "object": item => !item ? 0 : Object + .keys(item) + .reduce((total, key) => sizeOf(key) + sizeOf(item[key]) + total, 0) +}; + +const sizeOf = value => typeSizes[typeof value](value); + +describe("Full process", function () { + this.timeout(1000000000); + + let curve; + const ptau_0 = {type: "mem"}; + const ptau_1 = {type: "mem"}; + const ptau_2 = {type: "mem"}; + const ptau_beacon = {type: "mem"}; + const ptau_final = {type: "mem"}; + const ptau_challenge2 = {type: "mem"}; + const ptau_response2 = {type: "mem"}; + const zkey_0 = {type: "mem"}; + const zkey_1 = {type: "mem"}; + const zkey_2 = {type: "mem"}; + const zkey_final = {type: "mem"}; + const zkey_plonk = {type: "mem"}; + const bellman_1 = {type: "mem"}; + const bellman_2 = {type: "mem"}; + let vKey; + let vKeyPlonk; + const wtns = {type: "mem"}; + let proof; + let publicSignals; + + before( async () => { + curve = await buildBn128(); +// curve.Fr.s = 10; + }); + after( async () => { + await curve.terminate(); + // console.log(process._getActiveHandles()); + // console.log(process._getActiveRequests()); + }); + + it ("powersoftau new", async () => { + await snarkjs.powersOfTau.newAccumulator(curve, 11, ptau_0); + }); + + it ("powersoftau contribute ", async () => { + await snarkjs.powersOfTau.contribute(ptau_0, ptau_1, "C1", "Entropy1"); + }); + + it ("powersoftau export challenge", async () => { + await snarkjs.powersOfTau.exportChallenge(ptau_1, ptau_challenge2); + }); + + it ("powersoftau challenge contribute", async () => { + await snarkjs.powersOfTau.challengeContribute(curve, ptau_challenge2, ptau_response2, "Entropy2"); + }); + + it ("powersoftau import response", async () => { + await snarkjs.powersOfTau.importResponse(ptau_1, ptau_response2, ptau_2, "C2", true); + }); + + it ("powersoftau beacon", async () => { + await snarkjs.powersOfTau.beacon(ptau_2, ptau_beacon, "B3", "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", 10); + }); + + it ("powersoftau prepare phase2", async () => { + await snarkjs.powersOfTau.preparePhase2(ptau_beacon, ptau_final); + }); + + it ("powersoftau verify", async () => { + const res = await snarkjs.powersOfTau.verify(ptau_final); + assert(res); + }); + + it ("groth16 setup", async () => { + await snarkjs.zKey.newZKey(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_0); + }); + + it ("zkey contribute ", async () => { + await snarkjs.zKey.contribute(zkey_0, zkey_1, "p2_C1", "pa_Entropy1"); + }); + + it ("zkey export bellman", async () => { + await snarkjs.zKey.exportBellman(zkey_1, bellman_1); + }); + + it ("zkey bellman contribute", async () => { + await snarkjs.zKey.bellmanContribute(curve, bellman_1, bellman_2, "pa_Entropy2"); + }); + + it ("zkey import bellman", async () => { + await snarkjs.zKey.importBellman(zkey_1, bellman_2, zkey_2, "C2"); + }); + + it ("zkey beacon", async () => { + await snarkjs.zKey.beacon(zkey_2, zkey_final, "B3", "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", 10); + }); + + it ("zkey verify r1cs", async () => { + const res = await snarkjs.zKey.verifyFromR1cs(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_final); + assert(res); + }); + + it ("zkey verify init", async () => { + const res = await snarkjs.zKey.verifyFromInit(zkey_0, ptau_final, zkey_final); + assert(res); + }); + + it ("zkey export verificationkey", async () => { + vKey = await snarkjs.zKey.exportVerificationKey(zkey_final); + }); + + it ("witness calculate", async () => { + await snarkjs.wtns.calculate({a: 11, b:2}, path.join("test", "circuit", "circuit.wasm"), wtns); + //console.warn("witness: ", wtns); + console.warn("witness: ", wtns.data.length, " bytes"); + console.warn("witness: ", sizeOf(wtns.data), " bytes"); + }); + + it ("groth16 proof", async () => { + const res = await snarkjs.groth16.prove(zkey_final, wtns); + proof = res.proof; + publicSignals = res.publicSignals; + console.warn("proof: ", sizeOf(proof), " bytes"); + console.warn("public: ", sizeOf(publicSignals), " bytes"); + }); + + + it ("groth16 verify", async () => { + const res = await snarkjs.groth16.verify(vKey, publicSignals, proof); + assert(res == true); + }); + + it ("plonk setup", async () => { + await snarkjs.plonk.setup(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_plonk); + }); + + it ("zkey export verificationkey", async () => { + vKey = await snarkjs.zKey.exportVerificationKey(zkey_plonk); + }); + + it ("plonk proof", async () => { + const res = await snarkjs.plonk.prove(zkey_plonk, wtns); + proof = res.proof; + publicSignals = res.publicSignals; + console.warn("proof: ", proof, " bytes"); + console.warn("public: ", publicSignals, " bytes"); + }); + + + it ("plonk verify", async () => { + const res = await snarkjs.plonk.verify(vKey, publicSignals, proof); + assert(res == true); + }); + + +}); diff --git a/test/plonk_circuit/circuit.circom b/test/plonk_circuit/circuit.circom new file mode 100644 index 0000000..f5f9d38 --- /dev/null +++ b/test/plonk_circuit/circuit.circom @@ -0,0 +1,17 @@ +template TestPlonk() { + signal input a; + signal private input b; + signal output c; + + signal i1; + signal i2; + signal i4; + + i1 <== a+b+3; + + i2 <== i1*i1; + i4 <== i2*i2; + c <== i1*i4; +} + +component main = TestPlonk(); diff --git a/test/plonk_circuit/circuit.r1cs b/test/plonk_circuit/circuit.r1cs new file mode 100644 index 0000000..5fc028b Binary files /dev/null and b/test/plonk_circuit/circuit.r1cs differ diff --git a/test/plonk_circuit/circuit.wasm b/test/plonk_circuit/circuit.wasm new file mode 100644 index 0000000..f1b228f Binary files /dev/null and b/test/plonk_circuit/circuit.wasm differ diff --git a/test/sha256.cjs b/test/sha256.cjs new file mode 100644 index 0000000..f686db6 --- /dev/null +++ b/test/sha256.cjs @@ -0,0 +1,175 @@ +const chai = require("chai"); +const path = require("path"); +const crypto = require("crypto"); +const F1Field = require("ffjavascript").F1Field; +const Scalar = require("ffjavascript").Scalar; +exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +const Fr = new F1Field(exports.p); + +const assert = chai.assert; + +const sha256 = require("./helpers/sha256"); + +const wasm_tester = require("circom_tester").wasm; + +const snarkjs = require("snarkjs"); +const buildBn128 = require("ffjavascript").buildBn128; + +// const printSignal = require("./helpers/printsignal"); + + +function buffer2bitArray(b) { + const res = []; + for (let i=0; i> (7-j) &1)); + } + } + return res; +} + +function bitArray2buffer(a) { + const len = Math.floor((a.length -1 )/8)+1; + const b = new Buffer.alloc(len); + + for (let i=0; i { + curve = await buildBn128(); +// curve.Fr.s = 10; + }); + after( async () => { + await curve.terminate(); + // console.log(process._getActiveHandles()); + // console.log(process._getActiveRequests()); + }); + + it ("powersoftau new", async () => { + await snarkjs.powersOfTau.newAccumulator(curve, 11, ptau_0); + }); + + it ("powersoftau contribute ", async () => { + await snarkjs.powersOfTau.contribute(ptau_0, ptau_1, "C1", "Entropy1"); + }); + + it ("powersoftau export challenge", async () => { + await snarkjs.powersOfTau.exportChallenge(ptau_1, ptau_challenge2); + }); + + it ("powersoftau challenge contribute", async () => { + await snarkjs.powersOfTau.challengeContribute(curve, ptau_challenge2, ptau_response2, "Entropy2"); + }); + + it ("powersoftau import response", async () => { + await snarkjs.powersOfTau.importResponse(ptau_1, ptau_response2, ptau_2, "C2", true); + }); + + it ("powersoftau beacon", async () => { + await snarkjs.powersOfTau.beacon(ptau_2, ptau_beacon, "B3", "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", 10); + }); + + it ("powersoftau prepare phase2", async () => { + await snarkjs.powersOfTau.preparePhase2(ptau_beacon, ptau_final); + }); + + it ("powersoftau verify", async () => { + const res = await snarkjs.powersOfTau.verify(ptau_final); + assert(res); + }); + + it("Should work bits to array and array to bits", async () => { + const b = new Buffer.alloc(64); + for (let i=0; i<64; i++) { + b[i] = i+1; + } + const a = buffer2bitArray(b); + const b2 = bitArray2buffer(a); + + assert.equal(b.toString("hex"), b2.toString("hex"), true); + }); + + it ("compile circuit 64 bytes (512 bits)", async () => { + cir = await wasm_tester(path.join(__dirname, "../circuits", "sha256_test512.circom")); + }); + + it("Should calculate a hash of 2 compressor", async () => { + + const b = new Buffer.alloc(64); + for (let i=0; i<64; i++) { + b[i] = i+1; + } + + const hash = crypto.createHash("sha256") + .update(b) + .digest("hex"); + + const arrIn = buffer2bitArray(b); + const witness = await cir.calculateWitness({ "in": arrIn }, true); + console.warn("witness: ", witness.length, " bits"); + + const arrOut = witness.slice(1, 257); + const hash2 = bitArray2buffer(arrOut).toString("hex"); + + assert.equal(hash, hash2); + + }).timeout(1000000); + + it ("compile circuit 640 bytes", async () => { + cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_test5120.circom")); + }); + + it("Should calculate a hash of 20 compressor", async () => { + + const b = new Buffer.alloc(640); + for (let i=0; i<640; i++) { + b[i] = i+1; + } + + const hash = crypto.createHash("sha256") + .update(b) + .digest("hex"); + + const arrIn = buffer2bitArray(b); + const witness = await cir.calculateWitness({ "in": arrIn }, true); + console.warn("witness: ", witness.length, " bits"); + + const arrOut = witness.slice(1, 257); + const hash2 = bitArray2buffer(arrOut).toString("hex"); + + assert.equal(hash, hash2); + + }).timeout(1000000); + +});