poseidoncircuit.js
1 const chai = require("chai"); 2 const path = require("path"); 3 const wasm_tester = require("circom_tester").wasm; 4 5 const buildPoseidon = require("circomlibjs").buildPoseidon; 6 7 const assert = chai.assert; 8 9 describe("Poseidon Circuit test", function () { 10 let poseidon; 11 let F; 12 let circuit6; 13 let circuit3; 14 let circuitEx; 15 16 this.timeout(1000000); 17 18 before( async () => { 19 poseidon = await buildPoseidon(); 20 F = poseidon.F; 21 circuit6 = await wasm_tester(path.join(__dirname, "circuits", "poseidon6_test.circom")); 22 circuit3 = await wasm_tester(path.join(__dirname, "circuits", "poseidon3_test.circom")); 23 circuitEx = await wasm_tester(path.join(__dirname, "circuits", "poseidonex_test.circom")); 24 }); 25 26 it("Should check constrain of hash([1, 2]) t=6", async () => { 27 const w = await circuit6.calculateWitness({inputs: [1, 2, 0,0,0]}, true); 28 29 const res2 = poseidon([1,2,0,0,0]); 30 31 assert(F.eq(F.e("1018317224307729531995786483840663576608797660851238720571059489595066344487"), F.e(res2))); 32 await circuit6.assertOut(w, {out : F.toObject(res2)}); 33 await circuit6.checkConstraints(w); 34 }); 35 36 it("Should check constrain of hash([3, 4]) t=6", async () => { 37 const w = await circuit6.calculateWitness({inputs: [3, 4,5,10,23]}); 38 39 const res2 = poseidon([3, 4,5,10,23]); 40 41 assert(F.eq(F.e("13034429309846638789535561449942021891039729847501137143363028890275222221409"), F.e(res2))); 42 await circuit6.assertOut(w, {out : F.toObject(res2)}); 43 await circuit6.checkConstraints(w); 44 }); 45 46 47 it("Should check constrain of hash([1, 2]) t=3", async () => { 48 const w = await circuit3.calculateWitness({inputs: [1, 2]}); 49 50 const res2 = poseidon([1,2]); 51 52 assert(F.eq(F.e("7853200120776062878684798364095072458815029376092732009249414926327459813530"), F.e(res2))); 53 await circuit3.assertOut(w, {out : F.toObject(res2)}); 54 await circuit3.checkConstraints(w); 55 }); 56 57 it("Should check constrain of hash([3, 4]) t=3", async () => { 58 const w = await circuit3.calculateWitness({inputs: [3, 4]}); 59 60 const res2 = poseidon([3, 4]); 61 62 assert(F.eq(F.e("14763215145315200506921711489642608356394854266165572616578112107564877678998"), F.e(res2))); 63 await circuit3.assertOut(w, {out : F.toObject(res2)}); 64 await circuit3.checkConstraints(w); 65 }); 66 67 it("Should check constrain of hash with state and 16 ins and outs", async () => { 68 const ins = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 69 const w = await circuitEx.calculateWitness({inputs: ins, initialState: 17}); 70 71 const res2 = poseidon(ins, 17, 17); 72 const res2f = []; 73 for (let i=0; i<res2.length; i++) { 74 res2f[i] = F.toObject(res2[i]); 75 } 76 await circuitEx.assertOut(w, {out : res2f}); 77 await circuitEx.checkConstraints(w); 78 }); 79 80 });