Skip to content

Commit

Permalink
feat: Plumbing for slot numbers (#7663)
Browse files Browse the repository at this point in the history
Fixes #7594 partially, only putting down plumbing here. The constraints
comes later to not wreck all tests in the same pr and make it a huge
mess.
  • Loading branch information
LHerskind authored Aug 2, 2024
1 parent 7a763c0 commit e7c1dc3
Show file tree
Hide file tree
Showing 38 changed files with 366 additions and 212 deletions.
22 changes: 11 additions & 11 deletions barretenberg/cpp/pil/avm/constants_gen.pil
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ namespace constants(256);
pol ADDRESS_SELECTOR = 1;
pol STORAGE_ADDRESS_SELECTOR = 1;
pol FUNCTION_SELECTOR_SELECTOR = 2;
pol START_GLOBAL_VARIABLES = 28;
pol CHAIN_ID_SELECTOR = 28;
pol VERSION_SELECTOR = 29;
pol BLOCK_NUMBER_SELECTOR = 30;
pol TIMESTAMP_SELECTOR = 31;
pol COINBASE_SELECTOR = 32;
pol FEE_PER_DA_GAS_SELECTOR = 34;
pol FEE_PER_L2_GAS_SELECTOR = 35;
pol END_GLOBAL_VARIABLES = 36;
pol START_SIDE_EFFECT_COUNTER = 36;
pol TRANSACTION_FEE_SELECTOR = 39;
pol START_GLOBAL_VARIABLES = 29;
pol CHAIN_ID_SELECTOR = 29;
pol VERSION_SELECTOR = 30;
pol BLOCK_NUMBER_SELECTOR = 31;
pol TIMESTAMP_SELECTOR = 33;
pol COINBASE_SELECTOR = 34;
pol FEE_PER_DA_GAS_SELECTOR = 36;
pol FEE_PER_L2_GAS_SELECTOR = 37;
pol END_GLOBAL_VARIABLES = 38;
pol START_SIDE_EFFECT_COUNTER = 38;
pol TRANSACTION_FEE_SELECTOR = 41;
pol START_NOTE_HASH_EXISTS_WRITE_OFFSET = 0;
pol START_NULLIFIER_EXISTS_OFFSET = 16;
pol START_NULLIFIER_NON_EXISTS_OFFSET = 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1053,49 +1053,49 @@ template <typename FF_> class mainImpl {
}
{
using Accumulator = typename std::tuple_element_t<120, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_transaction_fee * (new_term.kernel_kernel_in_offset - FF(39)));
auto tmp = (new_term.main_sel_op_transaction_fee * (new_term.kernel_kernel_in_offset - FF(41)));
tmp *= scaling_factor;
std::get<120>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<121, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_chain_id * (new_term.kernel_kernel_in_offset - FF(28)));
auto tmp = (new_term.main_sel_op_chain_id * (new_term.kernel_kernel_in_offset - FF(29)));
tmp *= scaling_factor;
std::get<121>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<122, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_version * (new_term.kernel_kernel_in_offset - FF(29)));
auto tmp = (new_term.main_sel_op_version * (new_term.kernel_kernel_in_offset - FF(30)));
tmp *= scaling_factor;
std::get<122>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<123, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_block_number * (new_term.kernel_kernel_in_offset - FF(30)));
auto tmp = (new_term.main_sel_op_block_number * (new_term.kernel_kernel_in_offset - FF(31)));
tmp *= scaling_factor;
std::get<123>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<124, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_timestamp * (new_term.kernel_kernel_in_offset - FF(31)));
auto tmp = (new_term.main_sel_op_timestamp * (new_term.kernel_kernel_in_offset - FF(33)));
tmp *= scaling_factor;
std::get<124>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<125, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_coinbase * (new_term.kernel_kernel_in_offset - FF(32)));
auto tmp = (new_term.main_sel_op_coinbase * (new_term.kernel_kernel_in_offset - FF(34)));
tmp *= scaling_factor;
std::get<125>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<126, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_fee_per_da_gas * (new_term.kernel_kernel_in_offset - FF(34)));
auto tmp = (new_term.main_sel_op_fee_per_da_gas * (new_term.kernel_kernel_in_offset - FF(36)));
tmp *= scaling_factor;
std::get<126>(evals) += typename Accumulator::View(tmp);
}
{
using Accumulator = typename std::tuple_element_t<127, ContainerOverSubrelations>;
auto tmp = (new_term.main_sel_op_fee_per_l2_gas * (new_term.kernel_kernel_in_offset - FF(35)));
auto tmp = (new_term.main_sel_op_fee_per_l2_gas * (new_term.kernel_kernel_in_offset - FF(37)));
tmp *= scaling_factor;
std::get<127>(evals) += typename Accumulator::View(tmp);
}
Expand Down
30 changes: 15 additions & 15 deletions barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define CONTENT_COMMITMENT_LENGTH 4
#define CONTRACT_STORAGE_READ_LENGTH 3
#define CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH 3
#define GLOBAL_VARIABLES_LENGTH 8
#define GLOBAL_VARIABLES_LENGTH 9
#define APPEND_ONLY_TREE_SNAPSHOT_LENGTH 2
#define L2_TO_L1_MESSAGE_LENGTH 3
#define PARTIAL_STATE_REFERENCE_LENGTH 6
Expand All @@ -30,24 +30,24 @@
#define PUBLIC_CALL_REQUEST_LENGTH 14
#define STATE_REFERENCE_LENGTH 8
#define TOTAL_FEES_LENGTH 1
#define HEADER_LENGTH 23
#define PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH 689
#define PUBLIC_CONTEXT_INPUTS_LENGTH 40
#define HEADER_LENGTH 24
#define PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH 691
#define PUBLIC_CONTEXT_INPUTS_LENGTH 42
#define SENDER_SELECTOR 0
#define ADDRESS_SELECTOR 1
#define STORAGE_ADDRESS_SELECTOR 1
#define FUNCTION_SELECTOR_SELECTOR 2
#define START_GLOBAL_VARIABLES 28
#define CHAIN_ID_SELECTOR 28
#define VERSION_SELECTOR 29
#define BLOCK_NUMBER_SELECTOR 30
#define TIMESTAMP_SELECTOR 31
#define COINBASE_SELECTOR 32
#define FEE_PER_DA_GAS_SELECTOR 34
#define FEE_PER_L2_GAS_SELECTOR 35
#define END_GLOBAL_VARIABLES 36
#define START_SIDE_EFFECT_COUNTER 36
#define TRANSACTION_FEE_SELECTOR 39
#define START_GLOBAL_VARIABLES 29
#define CHAIN_ID_SELECTOR 29
#define VERSION_SELECTOR 30
#define BLOCK_NUMBER_SELECTOR 31
#define TIMESTAMP_SELECTOR 33
#define COINBASE_SELECTOR 34
#define FEE_PER_DA_GAS_SELECTOR 36
#define FEE_PER_L2_GAS_SELECTOR 37
#define END_GLOBAL_VARIABLES 38
#define START_SIDE_EFFECT_COUNTER 38
#define TRANSACTION_FEE_SELECTOR 41
#define START_NOTE_HASH_EXISTS_WRITE_OFFSET 0
#define START_NULLIFIER_EXISTS_OFFSET 16
#define START_NULLIFIER_NON_EXISTS_OFFSET 32
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract Rollup is Leonidas, IRollup {

// Decode and validate header
HeaderLib.Header memory header = HeaderLib.decode(_header);
HeaderLib.validate(header, VERSION, lastBlockTs, archive());
HeaderLib.validate(header, VERSION, getCurrentSlot(), lastBlockTs, archive());

if (header.globalVariables.blockNumber != pendingBlockCount) {
revert Errors.Rollup__InvalidBlockNumber(
Expand Down
49 changes: 25 additions & 24 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ library Constants {
uint256 internal constant ETH_ADDRESS_LENGTH = 1;
uint256 internal constant FUNCTION_DATA_LENGTH = 2;
uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 8;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 9;
uint256 internal constant APPEND_ONLY_TREE_SNAPSHOT_LENGTH = 2;
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 6;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 3;
Expand Down Expand Up @@ -173,26 +173,26 @@ library Constants {
uint256 internal constant TX_CONTEXT_LENGTH = 9;
uint256 internal constant TX_REQUEST_LENGTH = 13;
uint256 internal constant TOTAL_FEES_LENGTH = 1;
uint256 internal constant HEADER_LENGTH = 23;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 645;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 689;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 648;
uint256 internal constant PUBLIC_CONTEXT_INPUTS_LENGTH = 40;
uint256 internal constant HEADER_LENGTH = 24;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 646;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 691;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 649;
uint256 internal constant PUBLIC_CONTEXT_INPUTS_LENGTH = 42;
uint256 internal constant AGGREGATION_OBJECT_LENGTH = 16;
uint256 internal constant SCOPED_READ_REQUEST_LEN = 3;
uint256 internal constant PUBLIC_DATA_READ_LENGTH = 2;
uint256 internal constant PRIVATE_VALIDATION_REQUESTS_LENGTH = 772;
uint256 internal constant PUBLIC_VALIDATION_REQUESTS_LENGTH = 514;
uint256 internal constant PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 364;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 41;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 43;
uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1336;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2165;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2167;
uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1215;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3435;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 415;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 11;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 28;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3437;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 417;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 12;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 29;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand All @@ -212,18 +212,19 @@ library Constants {
uint256 internal constant ADDRESS_SELECTOR = 1;
uint256 internal constant STORAGE_ADDRESS_SELECTOR = 1;
uint256 internal constant FUNCTION_SELECTOR_SELECTOR = 2;
uint256 internal constant START_GLOBAL_VARIABLES = 28;
uint256 internal constant CHAIN_ID_SELECTOR = 28;
uint256 internal constant VERSION_SELECTOR = 29;
uint256 internal constant BLOCK_NUMBER_SELECTOR = 30;
uint256 internal constant TIMESTAMP_SELECTOR = 31;
uint256 internal constant COINBASE_SELECTOR = 32;
uint256 internal constant UNUSED_FEE_RECIPIENT_SELECTOR = 33;
uint256 internal constant FEE_PER_DA_GAS_SELECTOR = 34;
uint256 internal constant FEE_PER_L2_GAS_SELECTOR = 35;
uint256 internal constant END_GLOBAL_VARIABLES = 36;
uint256 internal constant START_SIDE_EFFECT_COUNTER = 36;
uint256 internal constant TRANSACTION_FEE_SELECTOR = 39;
uint256 internal constant START_GLOBAL_VARIABLES = 29;
uint256 internal constant CHAIN_ID_SELECTOR = 29;
uint256 internal constant VERSION_SELECTOR = 30;
uint256 internal constant BLOCK_NUMBER_SELECTOR = 31;
uint256 internal constant SLOT_NUMBER_SELECTOR = 32;
uint256 internal constant TIMESTAMP_SELECTOR = 33;
uint256 internal constant COINBASE_SELECTOR = 34;
uint256 internal constant UNUSED_FEE_RECIPIENT_SELECTOR = 35;
uint256 internal constant FEE_PER_DA_GAS_SELECTOR = 36;
uint256 internal constant FEE_PER_L2_GAS_SELECTOR = 37;
uint256 internal constant END_GLOBAL_VARIABLES = 38;
uint256 internal constant START_SIDE_EFFECT_COUNTER = 38;
uint256 internal constant TRANSACTION_FEE_SELECTOR = 41;
uint256 internal constant START_NOTE_HASH_EXISTS_WRITE_OFFSET = 0;
uint256 internal constant START_NULLIFIER_EXISTS_OFFSET = 16;
uint256 internal constant START_NULLIFIER_NON_EXISTS_OFFSET = 32;
Expand Down
64 changes: 39 additions & 25 deletions l1-contracts/src/core/libraries/HeaderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ import {Hash} from "./Hash.sol";
* | 0x0134 | 0x20 | chainId
* | 0x0154 | 0x20 | version
* | 0x0174 | 0x20 | blockNumber
* | 0x0194 | 0x20 | timestamp
* | 0x01b4 | 0x14 | coinbase
* | 0x01c8 | 0x20 | feeRecipient
* | 0x01e8 | 0x20 | gasFees.feePerDaGas
* | 0x0208 | 0x20 | gasFees.feePerL2Gas
* | 0x0194 | 0x20 | slotNumber
* | 0x01b4 | 0x20 | timestamp
* | 0x01d4 | 0x14 | coinbase
* | 0x01e8 | 0x20 | feeRecipient
* | 0x0208 | 0x20 | gasFees.feePerDaGas
* | 0x0228 | 0x20 | gasFees.feePerL2Gas
* | | | }
* | | | }
* | 0x0228 | 0x20 | total_fees
* | 0x0248 | 0x20 | total_fees
* | --- | --- | ---
*/
library HeaderLib {
Expand Down Expand Up @@ -83,6 +84,7 @@ library HeaderLib {
uint256 chainId;
uint256 version;
uint256 blockNumber;
uint256 slotNumber;
uint256 timestamp;
address coinbase;
bytes32 feeRecipient;
Expand All @@ -104,19 +106,25 @@ library HeaderLib {
uint256 totalFees;
}

uint256 private constant HEADER_LENGTH = 0x248; // Header byte length
uint256 private constant HEADER_LENGTH = 0x268; // Header byte length

/**
* @notice Validates the header
* @param _header - The decoded header
* @param _version - The expected version
* @param _slot - The expected slot number
* @dev @todo currently unused, but must be constrained and used to
* constrain the timestamp instead of `lastBlockTs`
* @param _lastBlockTs - The timestamp of the last block
* @param _archive - The expected archive root
*/
function validate(Header memory _header, uint256 _version, uint256 _lastBlockTs, bytes32 _archive)
internal
view
{
function validate(
Header memory _header,
uint256 _version,
uint256 _slot,
uint256 _lastBlockTs,
bytes32 _archive
) internal view {
if (block.chainid != _header.globalVariables.chainId) {
revert Errors.Rollup__InvalidChainId(block.chainid, _header.globalVariables.chainId);
}
Expand All @@ -127,6 +135,8 @@ library HeaderLib {

// block number already constrained by archive root check

// @todo Constrain slot number + update timestamp to be linked to slot number

if (_header.globalVariables.timestamp > block.timestamp) {
revert Errors.Rollup__TimestampInFuture();
}
Expand Down Expand Up @@ -186,20 +196,21 @@ library HeaderLib {
header.globalVariables.chainId = uint256(bytes32(_header[0x0134:0x0154]));
header.globalVariables.version = uint256(bytes32(_header[0x0154:0x0174]));
header.globalVariables.blockNumber = uint256(bytes32(_header[0x0174:0x0194]));
header.globalVariables.timestamp = uint256(bytes32(_header[0x0194:0x01b4]));
header.globalVariables.coinbase = address(bytes20(_header[0x01b4:0x01c8]));
header.globalVariables.feeRecipient = bytes32(_header[0x01c8:0x01e8]);
header.globalVariables.gasFees.feePerDaGas = uint256(bytes32(_header[0x01e8:0x0208]));
header.globalVariables.gasFees.feePerL2Gas = uint256(bytes32(_header[0x0208:0x0228]));
header.globalVariables.slotNumber = uint256(bytes32(_header[0x0194:0x01b4]));
header.globalVariables.timestamp = uint256(bytes32(_header[0x01b4:0x01d4]));
header.globalVariables.coinbase = address(bytes20(_header[0x01d4:0x01e8]));
header.globalVariables.feeRecipient = bytes32(_header[0x01e8:0x0208]);
header.globalVariables.gasFees.feePerDaGas = uint256(bytes32(_header[0x0208:0x0228]));
header.globalVariables.gasFees.feePerL2Gas = uint256(bytes32(_header[0x0228:0x0248]));

// Reading totalFees
header.totalFees = uint256(bytes32(_header[0x0228:0x0248]));
header.totalFees = uint256(bytes32(_header[0x0248:0x0268]));

return header;
}

function toFields(Header memory _header) internal pure returns (bytes32[] memory) {
bytes32[] memory fields = new bytes32[](23);
bytes32[] memory fields = new bytes32[](24);

// must match the order in the Header.getFields
fields[0] = _header.lastArchive.root;
Expand All @@ -225,15 +236,18 @@ library HeaderLib {
fields[14] = bytes32(_header.globalVariables.chainId);
fields[15] = bytes32(_header.globalVariables.version);
fields[16] = bytes32(_header.globalVariables.blockNumber);
fields[17] = bytes32(_header.globalVariables.timestamp);
fields[18] = bytes32(uint256(uint160(_header.globalVariables.coinbase)));
fields[19] = bytes32(_header.globalVariables.feeRecipient);
fields[20] = bytes32(_header.globalVariables.gasFees.feePerDaGas);
fields[21] = bytes32(_header.globalVariables.gasFees.feePerL2Gas);
fields[22] = bytes32(_header.totalFees);
fields[17] = bytes32(_header.globalVariables.slotNumber);
fields[18] = bytes32(_header.globalVariables.timestamp);
fields[19] = bytes32(uint256(uint160(_header.globalVariables.coinbase)));
fields[20] = bytes32(_header.globalVariables.feeRecipient);
fields[21] = bytes32(_header.globalVariables.gasFees.feePerDaGas);
fields[22] = bytes32(_header.globalVariables.gasFees.feePerL2Gas);
fields[23] = bytes32(_header.totalFees);

// fail if the header structure has changed without updating this function
assert(fields.length == Constants.HEADER_LENGTH);
if (fields.length != Constants.HEADER_LENGTH) {
revert Errors.HeaderLib__InvalidHeaderSize(Constants.HEADER_LENGTH, fields.length);
}

return fields;
}
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ contract RollupTest is DecoderBase {

uint256 ts = block.timestamp + 1;
assembly {
mstore(add(header, add(0x20, 0x0194)), ts)
mstore(add(header, add(0x20, 0x01b4)), ts)
}

availabilityOracle.publish(body);
Expand Down
1 change: 1 addition & 0 deletions l1-contracts/test/decoders/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ contract DecoderBase is Test {
address coinbase;
bytes32 feeRecipient;
GasFees gasFees;
uint256 slotNumber;
uint256 timestamp;
uint256 version;
}
Expand Down
3 changes: 3 additions & 0 deletions l1-contracts/test/decoders/Decoders.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ contract DecodersTest is DecoderBase {
assertEq(
header.globalVariables.blockNumber, globalVariables.blockNumber, "Invalid block number"
);
assertEq(
header.globalVariables.slotNumber, globalVariables.slotNumber, "Invalid slot number"
);
assertEq(header.globalVariables.chainId, globalVariables.chainId, "Invalid chain Id");
assertEq(header.globalVariables.timestamp, globalVariables.timestamp, "Invalid timestamp");
assertEq(header.globalVariables.version, globalVariables.version, "Invalid version");
Expand Down
Loading

0 comments on commit e7c1dc3

Please sign in to comment.