diff --git a/circuits/lib/utils/spongeHash.circom b/circuits/lib/utils/spongeHash.circom index 622eb99e..d805818f 100644 --- a/circuits/lib/utils/spongeHash.circom +++ b/circuits/lib/utils/spongeHash.circom @@ -13,8 +13,8 @@ template SpongeHash(arraySize, hashFnBatchSize) { firstPoseidon.inputs[i] <== getArrayValueByIndex(in, arraySize, i); } - var restLength = arraySize - hashFnBatchSize; - if (restLength > batchSize) { + var restLength = arraySize - hashFnBatchSize > 0 ? arraySize - hashFnBatchSize : 0; + if (restLength > 0) { var r = restLength % batchSize; var diff = r == 0 ? 0 : batchSize - r; iterationCount = (restLength + diff) / batchSize; diff --git a/test/utils/spongeHash.test.ts b/test/utils/spongeHash.test.ts index ff3df806..f17ef398 100644 --- a/test/utils/spongeHash.test.ts +++ b/test/utils/spongeHash.test.ts @@ -2,24 +2,28 @@ import * as fs from "fs"; import path from "path"; import { wasm } from "circom_tester"; +const templateName = "SpongeHash"; const relativePath = "../circuits"; -const generateTemplate = (templateName: string, size: number): void => { +const generateTemplate = ( + size: number, + circuitTemplateName = "SpongeHash" +): void => { const template = ` pragma circom 2.0.0; include "../../circuits/lib/utils/spongeHash.circom"; -template ${templateName}Test() { +template ${circuitTemplateName}Test() { signal input in[{{n}}]; signal output out; -component h = ${templateName}({{n}},6); +component h = ${circuitTemplateName}({{n}},6); for(var i = 0; i < {{n}}; i++) { h.in[i] <== in[i]; } out <== h.out; } -component main = ${templateName}Test(); +component main = ${circuitTemplateName}Test(); `; - const circuitName = `${templateName}${size}.circom`; + const circuitName = `${circuitTemplateName}${size}.circom`; if (!fs.existsSync(path.join(__dirname, relativePath, circuitName))) { fs.writeFileSync( path.join(__dirname, relativePath, circuitName), @@ -29,9 +33,8 @@ component main = ${templateName}Test(); }; describe("Sponge Hash tests", function () { - this.timeout(200000); + this.timeout(400000); it("Test SpongeHash util using hash for different size inputs", async () => { - const templateName = "SpongeHash"; const testCases = [ new Array(64).fill(0), new Array(63).fill(0).map((_, i) => i + 1), @@ -52,8 +55,44 @@ describe("Sponge Hash tests", function () { "5605330091169856132381694679994923791994681609858984566508182442210285386845", ]; - for (let index = 0; index < testCases.length; index++) { - generateTemplate(templateName, testCases[index].length); + await createCircuit(testCases, expected); + }); + + it("Compare SpongeHash with go-iden3-crypto", async () => { + const expected = [ + "7757418611592686851480213421395023492910069335464834810473637859830874759279", + "15336558801450556532856248569924170992202208561737609669134139141992924267169", + "1144067817111460038464347636467015864025755473684726783913963849059920017972", + "17412321031092738336952455023828291176350572898965143678124844674611030278684", + "6186895146109816025093019628248576250523388957868658785525378722128520330607", + "20400040500897583745843009878988256314335038853985262692600694741116813247201", + "5577102071379540901883430718956859114277228199376926918690006383418752242436", + "1152305401687934645444055619201663931885907446826162025284948369145242973368", + "8211686227523893359273736667704216885003209084307215502943363750368107369620", + "7108881484930248270303049372327318360896856726757123411260066018366897025567", + "2265321027947983264707184154085264659877433648022878713272356019112959947364", + "12651359110916308876830620694657526370832930110397701742810260795463743022206", + "5448696623590242880008365208951082811870613001921911478755586779573529615712", + "12138957412533147284529235731676849096990688866708298976199544475739215311830", + "4296304251107177078079123684673490646100950885652358062546507066452904816259", + "5605330091169856132381694679994923791994681609858984566508182442210285386845", + "13988934542900192765733497388343315006075364569889174469414974142779436870312", + "15403024279602420951485303794282139684443426339931496210157338841814828581711", + "21456291545549982243960095968662564139932500401819177068272967144559313156981", + "18204869441381241555967353898895621136239168759533159329850061388567652528934", + "13038015408593191211490686165474468640092531721798660195788216465453248480728", + ]; + + const testCases = expected.map((_, i) => + new Array(i + 1).fill(0).map((_, i) => i + 1) + ); + + await createCircuit(testCases, expected); + }); + + async function createCircuit(testCases: number[][], expected: string[]) { + for (let index = 0; index < expected.length; index++) { + generateTemplate(testCases[index].length); const circuit = await wasm( path.join( __dirname, @@ -79,5 +118,5 @@ describe("Sponge Hash tests", function () { ) ); } - }); + } });