Skip to content

Commit

Permalink
Merge pull request #175 from 0xPolygonHermez/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
krlosMata committed Sep 20, 2024
2 parents 69ba1ae + 00dbce0 commit bb0e77e
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 92 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ package-lock.json
tools/fill-genesis/*.ignore.json

# input examples
tools/inputs-examples/
tools/inputs-examples/

# ignore
*.ignore/
tools/*.ignore/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:selfdestruct": "npx mocha ./test/processor.test.js --selfdestruct",
"eslint": "npx eslint src/** test/*.test.js && npx eslint tools",
"eslint:fix": "npx eslint src/** test/*.test.js --fix && npx eslint tools --fix",
"test:update": "./tools/update-tests/update-tests.sh",
"test:update": "cd test && npx hardhat compile && cd .. && ./tools/update-tests/update-tests.sh",
"test:database": "npx mocha ./test/database.test.js",
"build:inputs": "npx mocha ./test/processor.test.js --update --geninputs && npx mocha ./test/processor.test.js --etrog --update --geninputs"
},
Expand Down
24 changes: 12 additions & 12 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ module.exports = class Processor {
try {
const decodedObject = decodeCustomRawTxProverMethod(rawTx);
txDecoded = decodedObject.txDecoded;
/*
* The RLP encoding, encodes the 0 integer as "0x" ( empty byte array),
* In order to be compatible with Scalar or Number we will update the 0x integer cases with 0x00
*/
const txParams = Object.keys(txDecoded);

txParams.forEach((key) => {
if (txDecoded[key] === '0x' && key !== 'data' && key !== 'to') {
txDecoded[key] = '0x00';
}
});
rlpSignData = decodedObject.rlpSignData;
} catch (error) {
this.decodedTxs.push({ isInvalid: true, reason: 'TX INVALID: Failed to RLP decode signing data', tx: txDecoded });
Expand Down Expand Up @@ -231,17 +242,6 @@ module.exports = class Processor {
continue;
}

/*
* The RLP encoding, encodes the 0 integer as "0x" ( empty byte array),
* In order to be compatible with Scalar or Number we will update the 0x integer cases with 0x00
*/
const txParams = Object.keys(txDecoded);

txParams.forEach((key) => {
if (txDecoded[key] === '0x' && key !== 'data' && key !== 'to') {
txDecoded[key] = '0x00';
}
});
this.decodedTxs.push({ isInvalid: false, reason: '', tx: txDecoded });
}
}
Expand Down 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
103 changes: 56 additions & 47 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,61 @@ 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.configSteps = config.steps || 2 ** 23;
// safe guard counters to not take into account (%RANGE = 1 / SAFE_RANGE)
this.safeRange = config.safeRange || 20;
this.totalSteps = Math.floor(this.configSteps - this.configSteps / this.safeRange);
this.verbose = config.verbose || false;
this.consumptionReport = [];
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 / 31),
name: 'poseidon',
initAmount: Math.floor(totalSteps / 30),
initAmount: Math.floor(this.totalSteps / 31),
},
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 +114,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 +134,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,18 +250,22 @@ 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) {
this._verifyMerkleProof();
}
}

_verifyMerkleProof() {
this._reduceCounters(250, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(33, 'K');
}

Expand All @@ -279,9 +291,9 @@ module.exports = class VirtualCountersManager {

_ecAdd() {
// Worst case scenario
this._reduceCounters(323, 'S');
this._reduceCounters(33, 'B');
this._reduceCounters(40, 'A');
this._reduceCounters(800, 'S');
this._reduceCounters(50, 'B');
this._reduceCounters(50, 'A');
}

preECMul() {
Expand All @@ -295,9 +307,9 @@ module.exports = class VirtualCountersManager {

_ecMul() {
// Worst case scenario
this._reduceCounters(162890, 'S');
this._reduceCounters(16395, 'B');
this._reduceCounters(19161, 'A');
this._reduceCounters(175000, 'S');
this._reduceCounters(20000, 'B');
this._reduceCounters(20000, 'A');
}

preECPairing(input) {
Expand All @@ -313,9 +325,9 @@ module.exports = class VirtualCountersManager {

_ecPairing(inputsCount) {
// worst case scenario
this._reduceCounters(16 + inputsCount * 184017 + 171253, 'S');
this._reduceCounters(inputsCount * 3986 + 650, 'B');
this._reduceCounters(inputsCount * 13694 + 15411, 'A');
this._reduceCounters(16 + inputsCount * 200000 + 175000, 'S');
this._reduceCounters(inputsCount * 4100 + 750, 'B');
this._reduceCounters(inputsCount * 15000 + 17500, 'A');
}

preModExp(input) {
Expand Down Expand Up @@ -353,7 +365,7 @@ module.exports = class VirtualCountersManager {
this._checkInput(input, ['calldataLength']);
this._reduceCounters(100, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(Math.ceil((input.calldataLength + 1) / 64), 'SHA');
this._reduceCounters(Math.ceil((input.calldataLength + 1 + 8) / 64), 'SHA');
this._multiCall('_divArith', 2);
this._mStore32();
this._mStoreX();
Expand Down Expand Up @@ -660,28 +672,24 @@ module.exports = class VirtualCountersManager {
this._opcode(input);
this._reduceCounters(10, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(MCP, 'P');
}

opOr(input) {
this._opcode(input);
this._reduceCounters(10, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(MCP, 'P');
}

opXor(input) {
this._opcode(input);
this._reduceCounters(10, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(MCP, 'P');
}

opNot(input) {
this._opcode(input);
this._reduceCounters(10, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(MCP, 'P');
}

opByte(input) {
Expand Down Expand Up @@ -916,7 +924,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 @@ -996,7 +1004,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 @@ -1154,6 +1162,7 @@ module.exports = class VirtualCountersManager {
this._reduceCounters(10, 'S');
for (let i = 0; i < input.pushBytes; i++) {
this._reduceCounters(10, 'S');
this._SHLarith();
}
}
} else {
Expand Down Expand Up @@ -1351,7 +1360,7 @@ module.exports = class VirtualCountersManager {

_opcode(input) {
this._reduceCounters(12, 'S');
if (input.isCreate2 || input.isCreate || input.isDeploy) {
if (input.isCreate2 || input.isCreate) {
this._mLoadX();
this._SHRarith();
}
Expand Down Expand Up @@ -1514,7 +1523,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 @@ -1523,7 +1532,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 @@ -1532,7 +1541,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 @@ -1685,7 +1694,7 @@ module.exports = class VirtualCountersManager {
}

_mulArith() {
this._reduceCounters(50, 'S');
this._reduceCounters(40, 'S');
this._reduceCounters(1, 'B');
this._reduceCounters(1, 'A');
}
Expand All @@ -1697,8 +1706,8 @@ module.exports = class VirtualCountersManager {
}

_SHLarith() {
this._reduceCounters(100, 'S');
this._reduceCounters(4, 'B');
this._reduceCounters(40, 'S');
this._reduceCounters(2, 'B');
this._reduceCounters(2, 'A');
}

Expand All @@ -1725,7 +1734,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 @@ -1739,11 +1748,11 @@ module.exports = class VirtualCountersManager {
this._reduceCounters(200, 'S');
this._reduceCounters(2, 'K');
this._reduceCounters(MCP, 'P');
this._reduceCounters(1, 'B');
this._reduceCounters(2, 'B');
}

_divArith() {
this._reduceCounters(50, 'S');
this._reduceCounters(40, 'S');
this._reduceCounters(3, 'B');
this._reduceCounters(1, 'A');
}
Expand Down
2 changes: 1 addition & 1 deletion test/contract-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('contractUtils', function () {
const expectedBatchHashData = '0x5e7875ab198c4d93379c92990a5d0111af59a0e62b2c4a0e3898e5bd24a18e58';
// TODO: input taken from pil-stark
const expectedStarkHashExecutor = '0xcfae2cfa3b8f3f12abce1bccd90e9b203dfdbe56c0c412114f2d3e67c9a897db';
const expectedSnarkInputHash = '14744991293971375789164639158836701110243288198095088376701471320299286035170';
const expectedSnarkInputHash = '19704504443275424030853930423657339055467823429269903357094355701515609372092';

before(async () => {
testVector = JSON.parse(fs.readFileSync(path.join(pathTestVectors, 'inputs-executor/input_executor.json')));
Expand Down
Loading

0 comments on commit bb0e77e

Please sign in to comment.