hermeznetwork / circuits

Implements hermez network zk-Snarks circuits
GNU Affero General Public License v3.0
55 stars 21 forks source link

hash batch optimization #19

Open krlosMata opened 3 years ago

krlosMata commented 3 years ago

Hash batch

Perform a hash of all possible state batch variables into a single value hashBatch. This will allow to save data on-chain and reduces the gas cost of the forgeBatch function.

The trade-off is that the entire bash state has to be submitted to the forgeBatch function call.

Note that this apply every time that anydata has to be checked against stateRoot or exitRoot. For example in withdraw functions and further massive migrations implementations.

Specifciation

// nullifyAmount = 0 for L2 transactions

txsData = txData[0] || txData[MAX_TXS - 1]

hashTxData = SHA256(txsData)


- batch hash:

dataBatch: [ uint256 ] stateRoot [ uint256 ] exitRoot [ uint256 ] hashTxData

hashBatch = SHA256(dataBatch)


- Contract
  - new mapping:

// store hashBatch for each batch number mapping(uint32 => uint256) public hashBatchMap;

  - new forgeBatch call:
- function forgeBatch(
    uint256 lastStateRoot, // needed to check against mapping hashBatchMap 
    uint256 lastExitRoot, // needed to check against mapping hashBatchMap
    uint256 lastHashTxData, // needed to check against mapping hashBatchMap
    uint48 newLastIdx,
    uint256 newStRoot,
    uint256 newExitRoot,
    bytes calldata encodedL1CoordinatorTx,
    bytes calldata l2TxsData,
    bytes calldata feeIdxCoordinator,
    uint8 verifierIdx,
    bool l1Batch,
    uint256[2] calldata proofA,
    uint256[2][2] calldata proofB,
    uint256[2] calldata proofC
) external virtual

```