Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge - WIP] feat: ZK OS integration #1150

Draft
wants to merge 2 commits into
base: release-v25-protocol-defense
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import {ZkSyncHyperchainBase} from "./ZkSyncHyperchainBase.sol";
import {COMMIT_TIMESTAMP_NOT_OLDER, COMMIT_TIMESTAMP_APPROXIMATION_DELTA, EMPTY_STRING_KECCAK, L2_TO_L1_LOG_SERIALIZE_SIZE, MAX_L2_TO_L1_LOGS_COMMITMENT_BYTES, PACKED_L2_BLOCK_TIMESTAMP_MASK, PUBLIC_INPUT_SHIFT, POINT_EVALUATION_PRECOMPILE_ADDR} from "../../../common/Config.sol";
import {IExecutor, L2_LOG_ADDRESS_OFFSET, L2_LOG_KEY_OFFSET, L2_LOG_VALUE_OFFSET, SystemLogKey, LogProcessingOutput, PubdataSource, BLS_MODULUS, PUBDATA_COMMITMENT_SIZE, PUBDATA_COMMITMENT_CLAIMED_VALUE_OFFSET, PUBDATA_COMMITMENT_COMMITMENT_OFFSET, MAX_NUMBER_OF_BLOBS, TOTAL_BLOBS_IN_COMMITMENT, BLOB_SIZE_BYTES} from "../../chain-interfaces/IExecutor.sol";

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataSource is not used

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name BLOB_SIZE_BYTES is not used

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataSource is not used

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name BLOB_SIZE_BYTES is not used

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataSource is not used

Check failure on line 7 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name BLOB_SIZE_BYTES is not used
import {PriorityQueue, PriorityOperation} from "../../libraries/PriorityQueue.sol";
import {UncheckedMath} from "../../../common/libraries/UncheckedMath.sol";
import {UnsafeBytes} from "../../../common/libraries/UnsafeBytes.sol";
import {L2_BOOTLOADER_ADDRESS, L2_TO_L1_MESSENGER_SYSTEM_CONTRACT_ADDR, L2_SYSTEM_CONTEXT_SYSTEM_CONTRACT_ADDR, L2_PUBDATA_CHUNK_PUBLISHER_ADDR} from "../../../common/L2ContractAddresses.sol";
import {PubdataPricingMode} from "../ZkSyncHyperchainStorage.sol";

Check failure on line 12 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataPricingMode is not used

Check failure on line 12 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataPricingMode is not used

Check failure on line 12 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name PubdataPricingMode is not used
import {IStateTransitionManager} from "../../IStateTransitionManager.sol";
import {BatchNumberMismatch, TimeNotReached, TooManyBlobs, ValueMismatch, InvalidPubdataMode, InvalidPubdataLength, HashMismatch, NonIncreasingTimestamp, TimestampError, InvalidLogSender, TxHashMismatch, UnexpectedSystemLog, MissingSystemLogs, LogAlreadyProcessed, InvalidProtocolVersion, CanOnlyProcessOneBatch, BatchHashMismatch, UpgradeBatchNumberIsNotZero, NonSequentialBatch, CantExecuteUnprovenBatches, SystemLogsSizeTooBig, InvalidNumberOfBlobs, VerifiedBatchesExceedsCommittedBatches, InvalidProof, RevertedBatchNotAfterNewLastBatch, CantRevertExecutedBatch, PointEvalFailed, EmptyBlobVersionHash, NonEmptyBlobVersionHash, BlobHashCommitmentError, CalldataLengthTooBig, InvalidPubdataHash, L2TimestampTooBig, PriorityOperationsRollingHashMismatch, PubdataCommitmentsEmpty, PointEvalCallFailed, PubdataCommitmentsTooBig, InvalidPubdataCommitmentsSize} from "../../../common/L1ContractErrors.sol";

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name ValueMismatch is not used

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name InvalidPubdataMode is not used

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name ValueMismatch is not used

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name InvalidPubdataMode is not used

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name ValueMismatch is not used

Check failure on line 14 in l1-contracts/contracts/state-transition/chain-deps/facets/Executor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name InvalidPubdataMode is not used

// While formally the following import is not used, it is needed to inherit documentation from it
import {IZkSyncHyperchainBase} from "../../chain-interfaces/IZkSyncHyperchainBase.sol";
Expand Down Expand Up @@ -39,70 +39,75 @@
revert BatchNumberMismatch(_previousBatch.batchNumber + 1, _newBatch.batchNumber);
}

uint8 pubdataSource = uint8(bytes1(_newBatch.pubdataCommitments[0]));
PubdataPricingMode pricingMode = s.feeParams.pubdataPricingMode;
if (
pricingMode != PubdataPricingMode.Validium &&
pubdataSource != uint8(PubdataSource.Calldata) &&
pubdataSource != uint8(PubdataSource.Blob)
) {
revert InvalidPubdataMode();
}
// TODO: commented for ZKOS testing
// uint8 pubdataSource = uint8(bytes1(_newBatch.pubdataCommitments[0]));
// PubdataPricingMode pricingMode = s.feeParams.pubdataPricingMode;
// if (
// pricingMode != PubdataPricingMode.Validium &&
// pubdataSource != uint8(PubdataSource.Calldata) &&
// pubdataSource != uint8(PubdataSource.Blob)
// ) {
// revert InvalidPubdataMode();
// }

// Check that batch contain all meta information for L2 logs.
// Get the chained hash of priority transaction hashes.
LogProcessingOutput memory logOutput = _processL2Logs(_newBatch, _expectedSystemContractUpgradeTxHash);

bytes32[] memory blobCommitments = new bytes32[](MAX_NUMBER_OF_BLOBS);
if (pricingMode == PubdataPricingMode.Validium) {
// skipping data validation for validium, we just check that the data is empty
if (_newBatch.pubdataCommitments.length != 1) {
revert CalldataLengthTooBig();
}
for (uint8 i = uint8(SystemLogKey.BLOB_ONE_HASH_KEY); i <= uint8(SystemLogKey.BLOB_SIX_HASH_KEY); ++i) {
logOutput.blobHashes[i - uint8(SystemLogKey.BLOB_ONE_HASH_KEY)] = bytes32(0);
}
} else if (pubdataSource == uint8(PubdataSource.Blob)) {
// In this scenario, pubdataCommitments is a list of: opening point (16 bytes) || claimed value (32 bytes) || commitment (48 bytes) || proof (48 bytes)) = 144 bytes
blobCommitments = _verifyBlobInformation(_newBatch.pubdataCommitments[1:], logOutput.blobHashes);
} else if (pubdataSource == uint8(PubdataSource.Calldata)) {
// In this scenario pubdataCommitments is actual pubdata consisting of l2 to l1 logs, l2 to l1 message, compressed smart contract bytecode, and compressed state diffs
if (_newBatch.pubdataCommitments.length > BLOB_SIZE_BYTES) {
revert InvalidPubdataLength();
}
bytes32 pubdataHash = keccak256(_newBatch.pubdataCommitments[1:_newBatch.pubdataCommitments.length - 32]);
if (logOutput.pubdataHash != pubdataHash) {
revert InvalidPubdataHash(pubdataHash, logOutput.pubdataHash);
}
blobCommitments[0] = bytes32(
_newBatch.pubdataCommitments[_newBatch.pubdataCommitments.length - 32:_newBatch
.pubdataCommitments
.length]
);
}

if (_previousBatch.batchHash != logOutput.previousBatchHash) {
revert HashMismatch(logOutput.previousBatchHash, _previousBatch.batchHash);
}
// Check that the priority operation hash in the L2 logs is as expected
if (logOutput.chainedPriorityTxsHash != _newBatch.priorityOperationsHash) {
revert HashMismatch(logOutput.chainedPriorityTxsHash, _newBatch.priorityOperationsHash);
}
// Check that the number of processed priority operations is as expected
if (logOutput.numberOfLayer1Txs != _newBatch.numberOfLayer1Txs) {
revert ValueMismatch(logOutput.numberOfLayer1Txs, _newBatch.numberOfLayer1Txs);
}

// Check the timestamp of the new batch
_verifyBatchTimestamp(logOutput.packedBatchAndL2BlockTimestamp, _newBatch.timestamp, _previousBatch.timestamp);
// TODO: commented for ZKOS testing
// if (pricingMode == PubdataPricingMode.Validium) {
// // skipping data validation for validium, we just check that the data is empty
// if (_newBatch.pubdataCommitments.length != 1) {
// revert CalldataLengthTooBig();
// }
// for (uint8 i = uint8(SystemLogKey.BLOB_ONE_HASH_KEY); i <= uint8(SystemLogKey.BLOB_SIX_HASH_KEY); ++i) {
// logOutput.blobHashes[i - uint8(SystemLogKey.BLOB_ONE_HASH_KEY)] = bytes32(0);
// }
// } else if (pubdataSource == uint8(PubdataSource.Blob)) {
// // In this scenario, pubdataCommitments is a list of: opening point (16 bytes) || claimed value (32 bytes) || commitment (48 bytes) || proof (48 bytes)) = 144 bytes
// blobCommitments = _verifyBlobInformation(_newBatch.pubdataCommitments[1:], logOutput.blobHashes);
// } else if (pubdataSource == uint8(PubdataSource.Calldata)) {
// // In this scenario pubdataCommitments is actual pubdata consisting of l2 to l1 logs, l2 to l1 message, compressed smart contract bytecode, and compressed state diffs
// if (_newBatch.pubdataCommitments.length > BLOB_SIZE_BYTES) {
// revert InvalidPubdataLength();
// }
// bytes32 pubdataHash = keccak256(_newBatch.pubdataCommitments[1:_newBatch.pubdataCommitments.length - 32]);
// if (logOutput.pubdataHash != pubdataHash) {
// revert InvalidPubdataHash(pubdataHash, logOutput.pubdataHash);
// }
// blobCommitments[0] = bytes32(
// _newBatch.pubdataCommitments[_newBatch.pubdataCommitments.length - 32:_newBatch
// .pubdataCommitments
// .length]
// );
// }
//
// if (_previousBatch.batchHash != logOutput.previousBatchHash) {
// revert HashMismatch(logOutput.previousBatchHash, _previousBatch.batchHash);
// }
// // Check that the priority operation hash in the L2 logs is as expected
// if (logOutput.chainedPriorityTxsHash != _newBatch.priorityOperationsHash) {
// revert HashMismatch(logOutput.chainedPriorityTxsHash, _newBatch.priorityOperationsHash);
// }
// // Check that the number of processed priority operations is as expected
// if (logOutput.numberOfLayer1Txs != _newBatch.numberOfLayer1Txs) {
// revert ValueMismatch(logOutput.numberOfLayer1Txs, _newBatch.numberOfLayer1Txs);
// }
//
// // Check the timestamp of the new batch
// _verifyBatchTimestamp(logOutput.packedBatchAndL2BlockTimestamp, _newBatch.timestamp, _previousBatch.timestamp);

// Create batch commitment for the proof verification
bytes32 commitment = _createBatchCommitment(
_newBatch,
logOutput.stateDiffHash,
blobCommitments,
logOutput.blobHashes
);

// TODO: commented for ZKOS testing
bytes32 commitment;
// bytes32 commitment = _createBatchCommitment(
// _newBatch,
// logOutput.stateDiffHash,
// blobCommitments,
// logOutput.blobHashes
// );

return
StoredBatchInfo({
Expand Down Expand Up @@ -252,15 +257,16 @@
// We only require 13 logs to be checked, the 14th is if we are expecting a protocol upgrade
// Without the protocol upgrade we expect 13 logs: 2^13 - 1 = 8191
// With the protocol upgrade we expect 14 logs: 2^14 - 1 = 16383
if (_expectedSystemContractUpgradeTxHash == bytes32(0)) {
if (processedLogs != 8191) {
revert MissingSystemLogs(8191, processedLogs);
}
} else {
if (processedLogs != 16383) {
revert MissingSystemLogs(16383, processedLogs);
}
}
// TODO: commented for ZKOS testing
// if (_expectedSystemContractUpgradeTxHash == bytes32(0)) {
// if (processedLogs != 8191) {
// revert MissingSystemLogs(8191, processedLogs);
// }
// } else {
// if (processedLogs != 16383) {
// revert MissingSystemLogs(16383, processedLogs);
// }
// }
}

/// @inheritdoc IExecutor
Expand Down Expand Up @@ -408,10 +414,11 @@
revert BatchHashMismatch(s.storedBatchHashes[currentBatchNumber], _hashStoredBatchInfo(_storedBatch));
}

bytes32 priorityOperationsHash = _collectOperationsFromPriorityQueue(_storedBatch.numberOfLayer1Txs);
if (priorityOperationsHash != _storedBatch.priorityOperationsHash) {
revert PriorityOperationsRollingHashMismatch();
}
// TODO: commented for ZKOS testing
// bytes32 priorityOperationsHash = _collectOperationsFromPriorityQueue(_storedBatch.numberOfLayer1Txs);
// if (priorityOperationsHash != _storedBatch.priorityOperationsHash) {
// revert PriorityOperationsRollingHashMismatch();
// }

// Save root hash of L2 -> L1 logs tree
s.l2LogsRootHashes[currentBatchNumber] = _storedBatch.l2LogsTreeRoot;
Expand Down Expand Up @@ -508,7 +515,8 @@
revert VerifiedBatchesExceedsCommittedBatches();
}

_verifyProof(proofPublicInput, _proof);
// TODO: commented for ZKOS testing
// _verifyProof(proofPublicInput, _proof);

emit BlocksVerification(s.totalBatchesVerified, currentTotalBatchesVerified);
s.totalBatchesVerified = currentTotalBatchesVerified;
Expand Down
Loading