Skip to content

Commit

Permalink
Merge pull request #91 from 0xPolygonHermez/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
krlosMata authored Mar 2, 2023
2 parents 59d4ab8 + 347ae69 commit 6168ea9
Show file tree
Hide file tree
Showing 24 changed files with 871 additions and 296 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ module.exports.ZkEVMDB = require('./src/zkevm-db');
module.exports.getPoseidon = require('./src/poseidon_opt');
module.exports.MTBridge = require('./src/mt-bridge');
module.exports.mtBridgeUtils = require('./src/mt-bridge-utils');
module.exports.Database = require('./src/database');
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@0xpolygonhermez/zkevm-commonjs",
"description": "Javascript library implementing common utilities for zkevm",
"version": "0.6.0.0",
"version": "0.8.0.0",
"main": "index.js",
"scripts": {
"setup": "npm i",
Expand All @@ -11,7 +11,8 @@
"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": "./tools/update-tests/update-tests.sh",
"test:database": "npx mocha ./test/database.test.js"
},
"repository": {
"type": "git",
Expand All @@ -34,7 +35,7 @@
},
"homepage": "https://github.com/0xPolygonHermez/zkevm-commonjs#readme",
"devDependencies": {
"@0xpolygonhermez/zkevm-contracts": "github:0xPolygonHermez/zkevm-contracts#v0.6.0.0",
"@0xpolygonhermez/zkevm-contracts": "github:0xPolygonHermez/zkevm-contracts#v0.8.0.0-rc.2-fork.1",
"@ethersproject/abi": "^5.6.4",
"@nomiclabs/hardhat-ethers": "^2.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.2",
Expand All @@ -50,10 +51,11 @@
"@ethereumjs/block": "^3.6.2",
"@ethereumjs/tx": "^3.4.0",
"@polygon-hermez/common": "2.6.4",
"@polygon-hermez/vm": "5.7.28",
"@polygon-hermez/vm": "5.7.30",
"ethereumjs-util": "^7.1.4",
"ethers": "^5.5.4",
"ffjavascript": "^0.2.55",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"pg": "^8.7.1"
}
}
6 changes: 5 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports.SMT_KEY_SC_CODE = 2;
module.exports.SMT_KEY_SC_STORAGE = 3;
module.exports.SMT_KEY_SC_LENGTH = 4;

// SMT touched-tree constant keys
module.exports.SMT_KEY_TOUCHED_ADDR = 5;
module.exports.SMT_KEY_TOUCHED_SLOTS = 6;

// SMT constant
module.exports.BYTECODE_ELEMENTS_HASH = 8;
module.exports.BYTECODE_BYTES_ELEMENT = 7;
Expand All @@ -32,7 +36,7 @@ module.exports.HASH_POSEIDON_ALL_ZEROES = '0xc71603f33a1144ca7953db0ab48808f4c40

// EVM constant
module.exports.ADDRESS_BRIDGE = '0x9D98DeAbC42dd696Deb9e40b4f1CAB7dDBF55988';
module.exports.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = '0xAE4bB80bE56B819606589DE61d5ec3b522EEB032';
module.exports.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = '0xa40D5f56745a118D0906a34E69aeC8C0Db1cB8fA';
module.exports.GLOBAL_EXIT_ROOT_STORAGE_POS = 0;
module.exports.LOCAL_EXIT_ROOT_STORAGE_POS = 1;
module.exports.BATCH_GAS_LIMIT = 30000000;
Expand Down
64 changes: 40 additions & 24 deletions src/contract-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function calculateAccInputHash(
}

/**
* Compute input for SNARK circuit: sha256(aggrAddress, oldStateRoot, oldAccInputHash, oldNumBatch, chainID, newStateRoot, newAccInputHash, newLocalExitRoot, newNumBatch) % FrSNARK
* Compute input for SNARK circuit: sha256(aggrAddress, oldStateRoot, oldAccInputHash, oldNumBatch, chainID, forkID, newStateRoot, newAccInputHash, newLocalExitRoot, newNumBatch) % FrSNARK
* @param {String} oldStateRoot - Current state Root
* @param {String} newStateRoot - New State root once the batch is processed
* @param {String} oldAccInputHash - initial accumulateInputHash
Expand All @@ -45,6 +45,7 @@ function calculateAccInputHash(
* @param {Number} newNumBatch - final batch number
* @param {Number} chainID - L2 chainID
* @param {String} aggregatorAddress - Aggregator Ethereum address in hex string
* @param {Number} forkID - L2 rom fork identifier
* @returns {String} - input snark in hex encoding
*/
async function calculateSnarkInput(
Expand All @@ -57,6 +58,7 @@ async function calculateSnarkInput(
newNumBatch,
chainID,
aggregatorAddress,
forkID,
) {
// 20 bytes agggregator address
const strAggregatorAddress = padZeros((Scalar.fromString(aggregatorAddress, 16)).toString(16), 40);
Expand All @@ -73,6 +75,9 @@ async function calculateSnarkInput(
// 8 bytes for chainID
const strChainID = padZeros(Scalar.e(chainID).toString(16), 16);

// 8 bytes for forkID
const strForkID = padZeros(Scalar.e(forkID).toString(16), 16);

// 32 bytes each field element for oldStateRoot
const strNewStateRoot = padZeros((Scalar.fromString(newStateRoot, 16)).toString(16), 64);

Expand All @@ -91,6 +96,7 @@ async function calculateSnarkInput(
.concat(strOldAccInputHash)
.concat(strOldNumBatch)
.concat(strChainID)
.concat(strForkID)
.concat(strNewStateRoot)
.concat(strNewAccInputHash)
.concat(strNewLocalExitRoot)
Expand All @@ -117,35 +123,45 @@ function calculateBatchHashData(

/**
* Prepare zkSnark inputs for smart contract
* @param {Object} proof - Contain the proof data related from snarkJs
* @param {Array} publicSignals - Contain the public input array from snarkJs
* @param {Object} proofJson - Contain the proof data related from snarkJs
* @returns {Object} - Proof structure ready to be sent to smart contract
*/
function generateSolidityInputs(
proof,
publicSignals,
proofJson,
) {
const proofA = [proof.pi_a[0],
proof.pi_a[1],
];
const proofB = [
[
proof.pi_b[0][1],
proof.pi_b[0][0],
],
const { evaluations, polynomials } = proofJson;
const arrayStrings = Array(24).fill('bytes32');
const proof = ethers.utils.defaultAbiCoder.encode(
arrayStrings,
[
proof.pi_b[1][1],
proof.pi_b[1][0],
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.C1[0]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.C1[1]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.C2[0]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.C2[1]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.W1[0]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.W1[1]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.W2[0]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(polynomials.W2[1]).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.ql).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.qr).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.qm).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.qo).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.qc).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.s1).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.s2).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.s3).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.a).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.b).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.c).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.z).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.zw).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.t1w).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.t2w).toHexString(), 32),
ethers.utils.hexZeroPad(ethers.BigNumber.from(evaluations.inv).toHexString(), 32),
],
];
const proofC = [proof.pi_c[0],
proof.pi_c[1],
];
const input = publicSignals;

return {
proofA, proofB, proofC, input,
};
);

return proof;
}

module.exports = {
Expand Down
Loading

0 comments on commit 6168ea9

Please sign in to comment.