Skip to content

Commit

Permalink
VCM improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Jun 17, 2024
1 parent 61a980b commit 6cc395f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ module.exports = class Processor {

async _processChangeL2BlockTx(tx) {
// Reduce counters
this.vcm.computeFunctionCounters('processChangeL2Block');
this.vcm.computeFunctionCounters('processChangeL2Block', { verifyMerkleProof: tx.indexL1InfoTree !== 0 });

// write old blockhash (oldStateRoot) on storage
// Get old blockNumber
Expand Down
66 changes: 37 additions & 29 deletions src/virtual-counters-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/* eslint-disable no-use-before-define */
/* eslint-disable prefer-destructuring */

const totalSteps = 2 ** 23;
// Maximum counters poseidon level when interacting with a small smt (blockInfoTree, touchedAccountsTree..), is constant
const MCPL = 23;
// Maximum counters poseidon level when interacting with a big smt (stateTree), is variable and can be updated
Expand All @@ -19,56 +18,58 @@ const FPEC = Scalar.e('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
const FNEC = Scalar.e('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141');
const FNEC_MINUS_ONE = Scalar.sub(FNEC, Scalar.e(1));
const spentCountersByFunction = {};

module.exports = class VirtualCountersManager {
/**
* constructor class
* @param {Object} config - database
* @param {Boolean} config.verbose - Activate or deactivate verbose mode, default: false
*/
constructor(config = {}) {
this.totalSteps = config.steps || 2 ** 23;
this.verbose = config.verbose || false;
this.consumptionReport = []

Check failure on line 30 in src/virtual-counters-manager.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon
this.MCPReduction = config.MCPReduction || 0.6;
// Compute counter initial amounts
this.currentCounters = {
S: {
amount: totalSteps,
amount: this.totalSteps,
name: 'steps',
initAmount: totalSteps,
initAmount: this.totalSteps,
},
A: {
amount: Math.floor(totalSteps / 32),
amount: Math.floor(this.totalSteps / 32),
name: 'arith',
initAmount: Math.floor(totalSteps / 32),
initAmount: Math.floor(this.totalSteps / 32),
},
B: {
amount: Math.floor(totalSteps / 16),
amount: Math.floor(this.totalSteps / 16),
name: 'binary',
initAmount: Math.floor(totalSteps / 16),
initAmount: Math.floor(this.totalSteps / 16),
},
M: {
amount: Math.floor(totalSteps / 32),
amount: Math.floor(this.totalSteps / 32),
name: 'memAlign',
initAmount: Math.floor(totalSteps / 32),
initAmount: Math.floor(this.totalSteps / 32),
},
K: {
amount: Math.floor((totalSteps / 155286) * 44),
amount: Math.floor((this.totalSteps / 155286) * 44),
name: 'keccaks',
initAmount: Math.floor((totalSteps / 155286) * 44),
initAmount: Math.floor((this.totalSteps / 155286) * 44),
},
D: {
amount: Math.floor(totalSteps / 56),
amount: Math.floor(this.totalSteps / 56),
name: 'padding',
initAmount: Math.floor(totalSteps / 56),
initAmount: Math.floor(this.totalSteps / 56),
},
P: {
amount: Math.floor(totalSteps / 30),
amount: Math.floor(this.totalSteps / 30),
name: 'poseidon',
initAmount: Math.floor(totalSteps / 30),
initAmount: Math.floor(this.totalSteps / 30),
},
SHA: {
amount: Math.floor((totalSteps - 1) / 31488) * 7,
amount: Math.floor((this.totalSteps - 1) / 31488) * 7,
name: 'sha256',
initAmount: Math.floor((totalSteps - 1) / 31488) * 7,
initAmount: Math.floor((this.totalSteps - 1) / 31488) * 7,
},
};
this.currentCountersSnapshot = {};
Expand Down Expand Up @@ -110,7 +111,7 @@ module.exports = class VirtualCountersManager {
* @param {Number} levels number of levels
*/
setSMTLevels(levels) {
MCP = levels;
MCP = Math.floor(levels * this.MCPReduction);
}

/**
Expand All @@ -130,6 +131,10 @@ module.exports = class VirtualCountersManager {
spentCounters[this.currentCountersSnapshot[counter].name] = this.currentCountersSnapshot[counter].amount - this.currentCounters[counter].amount;
});
this._verbose(spentCounters);
this.consumptionReport.push({
function: this.calledFunc,
vcounters: spentCounters,
});
this.currentCountersSnapshot = spentCounters;
// Fill counters consumption by function
if (!spentCountersByFunction[this.calledFunc]) {
Expand Down Expand Up @@ -242,14 +247,17 @@ module.exports = class VirtualCountersManager {
this._processContractCall({ ...input, ...{ isCreate: false, isCreate2: false } });
}

processChangeL2Block() {
processChangeL2Block(input) {
this._checkInput(input, ['verifyMerkleProof']);
this._reduceCounters(70, 'S');
this._reduceCounters(4 + 4, 'B');
this._reduceCounters(6 * MCP, 'P');
this._reduceCounters(2, 'K');
this._consolidateBlock();
this._setupNewBlockInfoTree();
this._verifyMerkleProof();
if(input.verifyMerkleProof) {

Check failure on line 258 in src/virtual-counters-manager.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected space(s) after "if"
this._verifyMerkleProof();
}
}

_verifyMerkleProof() {
Expand Down Expand Up @@ -912,7 +920,7 @@ module.exports = class VirtualCountersManager {
this._opcode(input);
this._checkInput(input, ['inputSize']);
this._reduceCounters(40, 'S');
this._reduceCounters(Math.ceil((input.inputSize + 1) / 32), 'K');
this._reduceCounters(Math.ceil((input.inputSize + 1) / 136), 'K');
this._saveMem({ length: input.inputSize });
this._multiCall('_divArith', 2);
this._mulArith();
Expand Down Expand Up @@ -992,7 +1000,7 @@ module.exports = class VirtualCountersManager {
this._divArith();
this._reduceCounters(Math.ceil(input.inputSize / 56) + 4, 'P');
this._reduceCounters(Math.ceil(input.inputSize / 56) + 4, 'D');
this._multiCall('_opLogLoop', Math.floor(input.inputSize + 1 / 32));
this._multiCall('_opLogLoop', Math.floor((input.inputSize + 1) / 32));
this._mLoadX();
this._SHRarith();
this._fillBlockInfoTreeWithLog();
Expand Down Expand Up @@ -1511,7 +1519,7 @@ module.exports = class VirtualCountersManager {
}

_mLoadX() {
this._reduceCounters(40, 'S');
this._reduceCounters(30, 'S');
this._reduceCounters(2, 'B');
this._reduceCounters(1, 'M');
this._offsetUtil();
Expand All @@ -1520,7 +1528,7 @@ module.exports = class VirtualCountersManager {
}

_mStoreX() {
this._reduceCounters(100, 'S');
this._reduceCounters(80, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(1, 'M');
this._offsetUtil();
Expand All @@ -1529,7 +1537,7 @@ module.exports = class VirtualCountersManager {
}

_mStore32() {
this._reduceCounters(100, 'S');
this._reduceCounters(80, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(1, 'M');
this._offsetUtil();
Expand Down Expand Up @@ -1682,7 +1690,7 @@ module.exports = class VirtualCountersManager {
}

_mulArith() {
this._reduceCounters(50, 'S');
this._reduceCounters(40, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(1, 'A');
}
Expand Down Expand Up @@ -1722,7 +1730,7 @@ module.exports = class VirtualCountersManager {
}

_SHRarith() {
this._reduceCounters(50, 'S');
this._reduceCounters(40, 'S');
this._reduceCounters(2, 'B');
this._reduceCounters(1, 'A');
this._divArith();
Expand All @@ -1740,7 +1748,7 @@ module.exports = class VirtualCountersManager {
}

_divArith() {
this._reduceCounters(50, 'S');
this._reduceCounters(40, 'S');
this._reduceCounters(3, 'B');
this._reduceCounters(1, 'A');
}
Expand Down

0 comments on commit 6cc395f

Please sign in to comment.