Polygon zkEVM is a second-layer blockchain for Ethereum, a scaling solution that utilizes zero-knowledge (ZK) proofs. It employs a cryptographic primitive known as a ZK proof to verify state transitions. By combining data availability and execution verification at the Ethereum L1 level, it ensures the security and reliability of L2 state transitions.
zkEVM enables developers to deploy Ethereum smart contracts on L2 without any code modifications, benefiting from the advantages of ZK-Rollups such as low gas costs and fast transaction finalization.
A zero-knowledge proof (ZKP) is a method in cryptography that allows one party (the prover) to prove to another party (the verifier) that a statement is true, without revealing any information other than the fact of the statement’s truth. This method has found extensive application in cryptocurrencies and blockchain technologies as it enables transactions or actions that guarantee a high level of privacy and security.
Blockchain Efficiency StrategyTo understand how the protocol works, one needs to know about state management and the transaction lifecycle.
State ManagementIt is important to explain how the Polygon zkEVM protocol manages L2 Rollup states while ensuring data verifiability and secure state transitions.
To begin, some definitions:
The trusted sequencer generates batches of transactions, but to achieve rapid finality of L2 transactions and avoid waiting for the next L1 block, they are transmitted to L2 network nodes through a broadcasting channel. Each node will execute the batches to locally compute the resulting L2 state.
Once the trusted sequencer records the sequence batches received directly from L1, the L2 network nodes will re-execute them, and they no longer have to trust it.
The off-chain executed batches will ultimately be verified on the blockchain using Zero-Knowledge Proof, after which the resulting L2 state root will be recorded.
As the zkEVM protocol evolves, L2 network nodes will directly receive state updates from L1 nodes. Blockchain state data will be transmitted and updated between the two network levels, improving communication and synchronization between them.
This means that both data availability and transaction execution verification entirely depend on the security assumptions of the first level (L1). In the final stage of the protocol, nodes will rely solely on the data present in L1 to maintain synchronization with each L2 state transition. This underscores the importance and reliability of L1 in ensuring the security and integrity of the entire system.
The diagram below shows how L2 nodes receive transaction packets.
It should be noted that the three packet data formats are accepted by L2 nodes in the chronological order listed above.
Thus, there are three stages of the L2 state, each corresponding to three different ways of state updating by L2 nodes:
The image below illustrates the timeline of L2 state stages in terms of packet processing, as well as the actions that trigger the transition from one stage to another.
Transaction LifecycleThis section details the various forms and stages that L2 user transactions undergo, from their creation in user wallets to their final verification by irrefutable proofs on L1.
Sending TransactionsTransactions in the Polygon zkEVM network are created in users’ wallets and signed with their private keys.
After creation and signing, transactions are sent to the Trusted Sequencer node via the JSON-RPC interface. The transactions are then stored in a pending transaction pool, where they await the Sequencer’s decision to execute or reject them.
Users and zkEVM communicate using JSON-RPC, which is fully compatible with Ethereum RPC. This approach allows any EVM-compatible application, such as wallet software, to function and feel like true Ethereum network users.
Important! It should be noted that in the current design of zkEVM, one transaction is equivalent to one block.
How can this improve blockchain operation?
This reflects a unique approach to transaction processing in the zkEVM network, which can provide high performance and convenience for users.
Thus, this design strategy not only improves RPC and P2P communication between nodes but also enhances compatibility with existing tools and ensures fast finality at the L2 level.
Transaction Execution and Trusted StateThe Trusted Sequencer reads transactions from the pool and decides whether to discard them or to order and execute them. Executed transactions are added to a transaction batch (batches), and the local L2 state of the sequencer is updated.
As previously discussed, once a transaction is added to the L2 state, it is transmitted to all other zkEVM nodes via the broadcasting service. It should be noted that relying on the Trusted Sequencer, we can achieve rapid transaction finality (faster than on L1). However, the obtained L2 state remains in a trusted (Trusted) state until the batch is recorded in the consensus contract.
Users typically interact with the trusted L2 state. However, due to certain protocol characteristics, the process of verifying L2 transactions (to enable funds withdrawal to L1) can take a long time, usually about 30 minutes, but in rare cases up to a week.
What are these rare cases?
Rare Case
As a result, users should be cautious about potential risks associated with high-value transactions, especially those that are irreversible, such as fund withdrawals, OTC transactions, and alternative bridges.
Batch Sequencing and Virtual StateTransaction batches now need to be ordered and verified before they can become part of the virtual (Virtual) state of L2.
Ordering Batches
The Trusted Sequencer adds a batch of transactions to the sequence of batches through the sequenceBatches function of the PolygonZkEVM.sol (L1) contract.
The image below shows the logical structure of the batch sequence.
Thus, batch sequencing (ordering) occurs, and a new virtual state is established, which is then transmitted to L2 nodes.
It’s important to understand that there is a maximum and minimum size for a transaction batch:
It is crucial to understand what these actions entail:
This verification occurs when the sequenceBatches function of the PolygonZkEVM.sol (L1) contract is called. The function iterates through each batch in the sequence and verifies its validity.
A valid batch must meet the following criteria:
If one batch is invalid, the transaction is canceled, discarding the entire sequence. Otherwise, if all batches subject to sequencing are valid, the sequencing process continues.
Transactions that were part of a rejected batch are not “lost” permanently. Instead, they can be resent or included in subsequent batches for processing. In a system where data security and integrity are priorities, this approach helps prevent invalid or fraudulent transactions from being incorporated into the blockchain.
State Integrity
In Polygon’s zk EVM, a mechanism is used to ensure cryptographic integrity of the transaction batch chain.
Here’s how it works:
The hash of a specific batch has the following structure:
Thus, the hash of each batch represents the accumulated hash of all previously sequenced batches, ensuring a chain of trust and cryptographic integrity throughout the system. This approach not only guarantees data security but also facilitates verification of the chain’s integrity without the need to process each transaction individually.
Once the batches have been successfully sequenced in L1, all zkEVM nodes can synchronize their local L2 state by retrieving data directly from the L1 PolygonZkEVM.sol contract, without having to rely solely on the Trusted Sequencer. This achieves the virtual L2 state.
Batch Aggregation and Consolidated StateFinally, we move to the last part where the trusted aggregator must ultimately aggregate the sequences of batches previously transmitted by the trusted sequencer to reach the final stage of the L2 state, that is, the consolidated state.
Let’s envision the aggregator’s operation:
Within the Prover, more complex mechanisms are used to generate proofs, such as SNARK and STARK.
Using the SNARK scheme, we can ensure secure on-chain for exhaustive off-chain computations in an economical manner. For those interested, you can view an entire extensive section in the documentation explaining how the Prover module operates.
After the batches have been successfully aggregated in L1, all zkEVM nodes can verify their local L2 state by extracting and checking the consolidated roots directly from the L1 consensus contract (PolygonZkEVM.sol). As a result, the consolidated L2 state is achieved.
Resistance to MalfunctionTo ensure system resilience, participants must be rewarded for correctly performing their roles and contributing to the protocol’s finality. At present, in the early stages of the protocol, there are centralized sequencers and aggregators, but in the future, the protocol plans to decentralize them. This is why they are currently called Trusted aggregator or Trusted Sequencer.
SequencerUsers must rely on the Trusted Sequencer to execute their transactions at the L2 level. However, users can include their transactions in a forced batch if they cannot execute them through the Trusted Sequencer.
In the diagram above, you can see that users can directly send transaction batches to the L1 consensus contract, where they are passed to the Trusted Sequencer and then sequenced. If for some reason users cannot execute their transactions through the sequencer, they can also send a transaction batch for sequencing to the L1 consensus contract.
It turns out that the Trusted Sequencer will include these forced batches in future sequences to maintain its trusted status. Otherwise, users can demonstrate that they are being censored, and the trust status of the Trusted Sequencer will be revoked.
AggregatorJust as the system cannot achieve L2 state finality without an active and well-functioning sequencer, finality cannot be achieved without an active and well-functioning aggregator.
The absence or failure of the Trusted Aggregator means that transitions between L2 states are never updated on L1. For this reason, the L1 PolygonZkEVM.sol contract has a verifyBatches function that allows anyone to aggregate sequences of batches.
zkEVM Emergency StateThe Emergency State is a state of the consensus contract (PolygonZkEVM.sol and PolygonZkEVMBridge.sol) that, when activated, stops batch sequencing and bridge operations.
The purpose of including an emergency state is to allow the Polygon team to address cases of malfunction or exploitation of any smart contract bugs. It is a safety measure used to protect users’ assets in zkEVM.
The following functions will be disabled in emergency mode:
Polygon ZK EVM represents a revolutionary approach to scaling Ethereum, allowing the use of standard Ethereum smart contract code (EVM bytecode) in a new, more efficient environment. For this, they use zkASM.
zkASM converts smart contract instructions, written in Solidity for Ethereum, into low-level code optimized for creating zk-SNARKs. This includes transforming operations and logic of smart contracts into sequences of simpler instructions.
This is similar to a translator who helps two people with different languages communicate with each other. As a result, Polygon ZK EVM allows the use of existing Ethereum smart contracts while offering the benefits of new technology, such as higher processing speeds and increased privacy.
One of the important criteria both in blockchain development and for smart contract developers themselves is the simplicity of development, testing, contract deployment, and the use of modern development tools.
Since zkEVM Polygon is based on Ethereum’s EVM, development is practically identical to development in the EVM blockchain. Popular tools such as Remix, Foundry, and Hardhat can also be used without any problems.
There are minor differences between EVM and ZkEVM:
The list includes supported EIPs, operations, and additional changes made to create zkEVM. The differences do not affect the developer experience in zkEVM compared to EVM. Gas optimization techniques, interaction with libraries like Web3.js and Ethers.js, and contract deployment work seamlessly on zkEVM without any overhead.
Opcodes
Precompiled Contracts
The following precompiled contracts are supported in zkEVM:
Other precompiled contracts do not affect the state tree of zkEVM and are processed as “revert”, returning all gas to the previous context and setting the success flag to “0”.
Thus, it should be understood that when developing a smart contract, libraries containing other precompiled contracts from this list cannot be used.
Other Minor Differences
Scaling Solution
Polygon PoS primarily uses the Plasma platform and Proof-of-Stake (PoS) consensus mechanism to create a sidechain that operates parallel to the main Ethereum network. On the other hand, Polygon zkEVM utilizes a ZK-Rollup architecture that employs zero-knowledge proofs to provide a Layer 2 solution over Ethereum.
Consensus Mechanism
Polygon PoS relies on a set of validators who participate in the PoS consensus mechanism to verify and confirm transactions within the sidechain. Polygon zkEVM uses a consensus contract that facilitates the seamless participation of multiple coordinators (sequencers and aggregators) to create and verify batches at the L2 level.
Data Availability
In the Polygon PoS network, data is stored on a sidechain, which provides a separate blockchain system for processing transactions. Polygon zkEVM offers two modes of data availability within a hybrid scheme: Validium (data stored off-chain) and Volition (data and proofs of its correctness are on-chain for some transactions, and for others, only proofs are stored).
Smart Contract Compatibility
Polygon PoS is a sidechain that ensures compatibility with the Ethereum Virtual Machine (EVM). This allows developers to deploy and run Ethereum smart contracts on the Polygon PoS sidechain. However, EVM compatibility implies that, despite supporting Ethereum smart contracts, there might be some differences in the execution environment. As a result, developers might need to adapt or use specific sidechain features when working with complex decentralized applications (dapps) and low-level code in Polygon PoS.
Unlike this, Polygon zkEVM is a ZK-Rollup focusing on achieving EVM equivalence. EVM equivalence implies a higher level of compatibility with Ethereum, allowing existing Ethereum smart contracts to be deployed and operated on Polygon zkEVM without any modifications. Developers do not need to change languages or tools, and they can experience a seamless transition when deploying their smart contracts on an equivalent EVM rollup. EVM equivalence effectively recreates the entire Ethereum execution environment.
The key difference is that the EVM equivalence offered by Polygon zkEVM provides “less friction” compared to the EVM compatibility offered by Polygon PoS. Polygon zkEVM is designed to ensure transparent deployment and full compatibility with Ethereum, allowing developers to maintain the same development workflow as on Ethereum, without needing any changes or reimplementation of code. In summary, Polygon zkEVM focuses on creating an almost perfect copy of the Ethereum execution environment, while Polygon PoS focuses on offering compatibility with Ethereum smart contracts in the context of a sidechain.
Security
Polygon PoS relies on its PoS validators to secure the sidechain, which operates independently of Ethereum. Polygon zkEVM inherits the security of the main Ethereum network by posting validity proofs on-chain, ensuring that off-chain computations are correct and secure.
Transaction Finality
Polygon PoS sidechains provide fast transaction finality with relatively low transaction fees. Polygon zkEVM uses zero-knowledge proofs to ensure quick off-chain transaction finality, reducing delays and fees.
Polygon PoS and Polygon zkEVM provide Layer 2 scaling solutions for Ethereum, but they differ in their architecture, consensus mechanisms, data availability parameters, and implementation details. Polygon zkEVM, in particular, uses ZK-Rollup technology to achieve improved scalability, security, and EVM equivalence while providing fast transaction finality.
Pros and Cons of zkEvm PolygonPros:Polygon zkEVM represents significant progress in blockchain technologies, combining Ethereum compatibility and the benefits of zero-knowledge proofs. It opens new horizons for blockchain business by offering high performance, scalability, security, and cost-efficiency. Developing for this blockchain is almost as easy as on a regular EVM blockchain. The scaling already available can help reduce the load on Ethereum and decrease users’ gas expenses. Moreover, developers are already planning to implement the EIP-4844 (Dencun) update soon, which will further reduce gas prices in the main Polygon zkEVM network in the future.
Check out our website and Telegram channel to know more about web3 development!
LinksOverview of Polygon zkEVM: How the Layer 2 solution for Ethereum works was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.