Skip to content

Commit

Permalink
Merge pull request #46 from 0xPolygonHermez/feature/update-snark-input
Browse files Browse the repository at this point in the history
Feature/update snark input
  • Loading branch information
krlosMata authored Aug 2, 2022
2 parents 1d391bb + 54447c7 commit 7d7d858
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion 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.0.36",
"version": "0.0.37",
"main": "index.js",
"scripts": {
"setup": "npm i",
Expand Down
16 changes: 13 additions & 3 deletions src/contract-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ function calculateStarkInput(
}

/**
* Compute globalHash for SNARK circuit
* Compute input for SNARK circuit
* @param {String} currentStateRoot - Current state Root
* @param {String} currentLocalExitRoot - Current local exit root
* @param {String} newStateRoot - New State root once the batch is processed
* @param {String} newLocalExitRoot - New local exit root once the batch is processed
* @param {String} batchHashData - Batch hash data
* @param {Number} numBatch - Batch number
* @param {Number} timestamp - Block timestamp
* @returns {String} - global hash % FrSNARK in hex encoding
* @param {String} aggregatorAddress - Aggregator Ethereum address in hex string
* @returns {String} - sha256(globalHash, aggregatorAddress) % FrSNARK in hex encoding
*/
function calculateSnarkInput(
currentStateRoot,
Expand All @@ -62,6 +63,7 @@ function calculateSnarkInput(
batchHashData,
numBatch,
timestamp,
aggregatorAddress,
) {
const hashKeccak = calculateStarkInput(
currentStateRoot,
Expand All @@ -73,7 +75,15 @@ function calculateSnarkInput(
timestamp,
);

return `0x${Scalar.mod(Scalar.fromString(hashKeccak, 16), FrSNARK).toString(16).padStart(64, '0')}`;
const hashSha256 = ethers.utils.soliditySha256(
['bytes32', 'address'],
[
hashKeccak,
aggregatorAddress,
],
);

return `0x${Scalar.mod(Scalar.fromString(hashSha256, 16), FrSNARK).toString(16).padStart(64, '0')}`;
}

/**
Expand Down
40 changes: 19 additions & 21 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ module.exports = class Processor {
this.decodedTxs = [];
this.builded = false;
this.starkInput = {};
this.snarkInput = '0x';
this.contractsBytecode = {};
this.oldStateRoot = root;
this.currentStateRoot = root;
this.oldLocalExitRoot = localExitRoot;
this.newLocalExitRoot = localExitRoot;
this.globalExitRoot = globalExitRoot;

this.batchHashData = '0x';
this.inputHash = '0x';

this.sequencerAddress = sequencerAddress;
this.timestamp = timestamp;

Expand Down Expand Up @@ -107,7 +109,6 @@ module.exports = class Processor {

// Calculate stark and snark input
await this._computeStarkInput();
await this._computeSnarkInput();

this.builded = true;
}
Expand Down Expand Up @@ -519,18 +520,18 @@ module.exports = class Processor {
const newLocalExitRoot = smtUtils.h4toString(this.newLocalExitRoot);
const globalExitRoot = smtUtils.h4toString(this.globalExitRoot);

const batchHashData = calculateBatchHashData(
this.batchHashData = calculateBatchHashData(
this.getBatchL2Data(),
globalExitRoot,
this.sequencerAddress,
);

const inputHash = calculateStarkInput(
this.inputHash = calculateStarkInput(
oldStateRoot,
oldLocalExitRoot,
newStateRoot,
newLocalExitRoot, // should be the new exit root, but it's not modified in this version
batchHashData,
newLocalExitRoot,
this.batchHashData,
this.batchNumber,
this.timestamp,
);
Expand All @@ -544,39 +545,35 @@ module.exports = class Processor {
oldLocalExitRoot,
newLocalExitRoot,
globalExitRoot,
batchHashData,
inputHash,
batchHashData: this.batchHashData,
inputHash: this.inputHash,
numBatch: this.batchNumber,
timestamp: this.timestamp,
contractsBytecode: this.contractsBytecode,
};
}

/**
* Compute stark input
* Compute snark input
* @param {String} aggregatorAddress - aggregator ethereum address
* @returns {String} Snark input
*/
_computeSnarkInput() {
_computeSnarkInput(aggregatorAddress) {
// compute circuit inputs
const oldStateRoot = smtUtils.h4toString(this.oldStateRoot);
const newStateRoot = smtUtils.h4toString(this.currentStateRoot);
const oldLocalExitRoot = smtUtils.h4toString(this.oldLocalExitRoot);
const newLocalExitRoot = smtUtils.h4toString(this.newLocalExitRoot);
const globalExitRoot = smtUtils.h4toString(this.globalExitRoot);

const batchHashData = calculateBatchHashData(
this.getBatchL2Data(),
globalExitRoot,
this.sequencerAddress,
);

this.snarkInput = calculateSnarkInput(
return calculateSnarkInput(
oldStateRoot,
oldLocalExitRoot,
newStateRoot,
newLocalExitRoot,
batchHashData,
this.batchHashData,
this.batchNumber,
this.timestamp,
aggregatorAddress,
);
}

Expand All @@ -598,11 +595,12 @@ module.exports = class Processor {

/**
* Return snark input
* @param {String} aggregatorAddress - aggregator Ethereum address
*/
getSnarkInput() {
getSnarkInput(aggregatorAddress) {
this._isBuilded();

return this.snarkInput;
return this._computeSnarkInput(aggregatorAddress);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion test/contract-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('contractUtils', function () {
let testVector;

const expectedBatchHashData = '0xa4e1166ff3f7ecf8c8ff3049fc2e28b03091d3bf0db4bce702d954840196f79d';
const expectedSnarkInputHash = '0x12bcbec575263760c8b89071ddb92de1cab523639aad606b2142c052dfcc42cd';
const expectedSnarkInputHash = '0x0b0a9c614cdc5473f2a9251d171230e92e5ae31fab0b165b13586100dad3a2c0';
const expectedStarkHashExecutor = '0x73855bab378977b439591bdee0bbde9c1b1cf3f48e20418da906ab7abfcc42cf';

before(async () => {
Expand All @@ -34,6 +34,8 @@ describe('contractUtils', function () {
});

it('calculateSnarkInput', async () => {
const aggregatorAddress = '0x123456789ABCDDEF123456789ABCDDEF12345678';

const {
oldLocalExitRoot,
newLocalExitRoot,
Expand All @@ -51,6 +53,7 @@ describe('contractUtils', function () {
expectedBatchHashData,
numBatch,
timestamp,
aggregatorAddress,
);

expect(computedGlobalHash).to.be.equal(expectedSnarkInputHash);
Expand Down

0 comments on commit 7d7d858

Please sign in to comment.