Skip to content

Commit

Permalink
Log optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv authored and laisolizq committed Feb 14, 2024
1 parent f0d18f8 commit 212478e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
npm run test:zkasm
- name: Generate tests
run: |
npm run test:gen
node tools/parallel-testing/gen-parallel-tests.js
- name: Run tests
run: |
export NUM_CPUS=31
Expand Down
33 changes: 18 additions & 15 deletions main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* - stack output: none
*/
VAR GLOBAL opLogAux

VAR GLOBAL logNextHashPIdAux
opLOG0:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand Down Expand Up @@ -45,15 +45,15 @@ opLOG1:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 3 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
1 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -66,15 +66,15 @@ opLOG2:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 4 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*2 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
2 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -87,15 +87,15 @@ opLOG3:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 5 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*3 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
3 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -108,15 +108,15 @@ opLOG4:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 6 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*4 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
4 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -136,6 +136,12 @@ initLogLoop:
0 => HASHPOS
32 => D
$ => B :MLOAD(currentLogIndex)
$ => A :MLOAD(nextHashPId)
; Update nextHashPId
A + 1 :MSTORE(nextHashPId)
; store nextHashPId to aux variable
A :MSTORE(logNextHashPIdAux)

logLoop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -144,7 +150,7 @@ logLoop:
C - 32 :JMPN(opLogFinal)
:CALL(MLOAD32); in: [E: offset] out: [A: value]
E :MSTORE(opLogAux)
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
A :HASHP(E)
$ => E :MLOAD(opLogAux)
$${storeLog(B, 0, A)} ; storeLog(indexLog, isTopic, bytesToStore)
Expand All @@ -156,14 +162,14 @@ opLogFinal:
$${storeLog(B, 0, A)}; storeLog(indexLog, isTopic, bytesToStore)
32 - C => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
C => D
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
A :HASHP(E)
:JMP(opSaveTopicsInit) ; instruction added to allow executing $$ function

opSaveTopicsInit:
; save topics
$ => A :MLOAD(numTopics)
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
32 => D

opSaveTopicsLoop:
Expand All @@ -174,16 +180,13 @@ opSaveTopicsLoop:
; check stack underflow
SP - 1 => SP
; check out-of-gas
GAS - %LOG_TOPIC_GAS => GAS :JMPN(outOfGas)
$ => C :MLOAD(SP) ; [topic => C]
C :HASHP(E)
$${storeLog(B, 1, C)} ; storeLog(indexLog, isTopic, bytesToStore)
A - 1 => A :JMP(opSaveTopicsLoop)

finishSaveTopics:
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
; Update nextHashPId
E + 1 :MSTORE(nextHashPId)
; Compute hash of the log
HASHPOS :HASHPLEN(E)
$ => D :HASHPDIGEST(E), CALL(fillBlockInfoTreeWithLog); in: [D: linearPoseidon(log_data + log_topics)]
Expand Down
7 changes: 6 additions & 1 deletion tools/parallel-testing/gen-parallel-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const pathMainPil = path.join(__dirname, '../../node_modules/@0xpolygonhermez/zk
const inputs = [];
const testsFolder = path.join(__dirname, 'parallel-tests');
const sampleDir = path.join(__dirname, 'parallel-tests-sample/sample.test.js');
const { argv } = require('yargs');

async function genTestsFiles() {
if (!fs.existsSync(testsFolder)) {
Expand All @@ -26,7 +27,11 @@ async function genTestsFiles() {
for (const inputPath of inputs) {
const name = inputPath.split('/').slice(-1)[0].replace('json', 'test.js');
const sample = fs.readFileSync(sampleDir, 'utf-8');
const test = sample.replace('%%INPUT_PATH%%', `${inputPath}`);
let test = sample.replace('%%INPUT_PATH%%', `${inputPath}`);
// Replace skip vcounters flag
if (argv.skipVCounters) {
test = test.replace('%%SKIP_VCOUNTERS%%', 'yes');
}
fs.writeFileSync(`${testsFolder}/${name}`, test);
}
expect(true).to.be.equal(true);
Expand Down
5 changes: 4 additions & 1 deletion tools/parallel-testing/parallel-tests-sample/sample.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const checkerDir = path.join(__dirname, 'checker.txt');
const inputPath = '%%INPUT_PATH%%';
const nameFile = path.basename(inputPath);
const input = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
const skipVcounters = '%%SKIP_VCOUNTERS%%';

it(`${nameFile}`, async () => {
if (fs.existsSync(checkerDir)) {
Expand Down Expand Up @@ -55,7 +56,9 @@ async function runTest(cmPols, steps) {
};

const res = await smMain.execute(cmPols.Main, input, rom, config);
compareCounters(input.virtualCounters, res.counters);
if (skipVcounters !== 'yes') {
compareCounters(input.virtualCounters, res.counters);
}
} catch (err) {
fs.writeFileSync(checkerDir, `Failed test ${inputPath} - ${err}}`);
throw err;
Expand Down

0 comments on commit 212478e

Please sign in to comment.