From 36aba2f14ea46cdea33a9c5194b4dd1d4f3957e6 Mon Sep 17 00:00:00 2001 From: zkronos73 Date: Wed, 20 Dec 2023 12:55:03 +0000 Subject: [PATCH] fix incCounter constraint on paddings and add one specific test. --- pil/padding_kk.pil | 3 +- pil/padding_pg.pil | 4 +-- pil/padding_sha256.pil | 3 +- test/sm/sm_paddings_empty.test.js | 57 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 test/sm/sm_paddings_empty.test.js diff --git a/pil/padding_kk.pil b/pil/padding_kk.pil index 294dec65..b403b494 100644 --- a/pil/padding_kk.pil +++ b/pil/padding_kk.pil @@ -91,8 +91,7 @@ namespace PaddingKK(%N); // if lastHash=0 && lastBLock=1 then incCounter'=incCounter+1 // if lastHash=1 && lastBlock=1 then incCounter'=1 incCounter' = incCounter*(1-lastBlock) + (incCounter +1)*(lastBlock-lastHash) + lastHash; - incCounter * Global.L1 = 0; - + (incCounter - 1) * Global.L1 = 0; // chunkReader generator diff --git a/pil/padding_pg.pil b/pil/padding_pg.pil index 5e11a164..cd29673d 100644 --- a/pil/padding_pg.pil +++ b/pil/padding_pg.pil @@ -112,9 +112,7 @@ namespace PaddingPG(%N); // if lastHash=0 && lastBLock=1 then incCounter'=incCounter+1 // if lastHash=1 && lastBlock=1 then incCounter'=1 incCounter' = incCounter*(1-lastBlock) + (incCounter +1)*(lastBlock-lastHash) + lastHash; - incCounter * Global.L1 = 0; - - + (incCounter - 1) * Global.L1 = 0; pol commit len; len' * lastHash = rem' * lastHash; diff --git a/pil/padding_sha256.pil b/pil/padding_sha256.pil index 08b6fa74..c7d9650f 100644 --- a/pil/padding_sha256.pil +++ b/pil/padding_sha256.pil @@ -105,8 +105,7 @@ namespace PaddingSha256(%N); // if lastHash=0 && lastBLock=1 then incCounter'=incCounter+1 // if lastHash=1 && lastBlock=1 then incCounter'=1 incCounter' = incCounter*(1-lastBlock) + (incCounter +1)*(lastBlock-lastHash) + lastHash; - incCounter * Global.L1 = 0; - + (incCounter - 1) * Global.L1 = 0; // chunkReader generator diff --git a/test/sm/sm_paddings_empty.test.js b/test/sm/sm_paddings_empty.test.js new file mode 100644 index 00000000..193b7c63 --- /dev/null +++ b/test/sm/sm_paddings_empty.test.js @@ -0,0 +1,57 @@ +const chai = require('chai'); + +const { assert } = chai; +const { F1Field } = require('ffjavascript'); + +const { + newConstantPolsArray, newCommitPolsArray, compile, verifyPil, +} = require('pilcom'); + +const smPaddingSha256 = require('../../src/sm/sm_padding_sha256'); +const smPaddingPG = require('../../src/sm/sm_padding_pg'); +const smPaddingKK = require('../../src/sm/sm_padding_kk'); +const smGlobal = require('../../src/sm/sm_global'); + +// input = []; + +const input = []; + +describe('test paddings (poseidon, keccak, sha256) empty', async function () { + this.timeout(10000000); + it('no hash test', async () => { + const Fr = new F1Field('0xFFFFFFFF00000001'); + const pil = await compile(Fr, 'pil/main.pil', null, { + defines: { N: 2 ** 18 }, + namespaces: ['Global','PaddingPG','PaddingKK','PaddingSha256'] + }); + const constPols = newConstantPolsArray(pil); + const cmPols = newCommitPolsArray(pil); + await smPaddingPG.buildConstants(constPols.PaddingPG); + await smPaddingKK.buildConstants(constPols.PaddingKK); + await smPaddingSha256.buildConstants(constPols.PaddingSha256); + await smGlobal.buildConstants(constPols.Global); + + await smPaddingPG.execute(cmPols.PaddingPG, []); + await smPaddingKK.execute(cmPols.PaddingKK, []); + await smPaddingSha256.execute(cmPols.PaddingSha256, []); + + for (let i = 0; i < cmPols.$$array.length; i++) { + const arr = cmPols.$$array[i]; + for (let j = 0; j < arr.length; j++) { + assert(typeof (arr[j]) === 'bigint', `pol: ${i} w: ${j} desc: ${JSON.stringify(cmPols.$$defArray[i], null, 1)}`); + } + } + + const res = await verifyPil(Fr, pil, cmPols, constPols); + + if (res.length !== 0) { + // eslint-disable-next-line no-console + console.log('Pil does not pass'); + for (let i = 0; i < res.length; i++) { + // eslint-disable-next-line no-console + console.log(res[i]); + } + assert(0); + } + }); +});