Skip to content

Commit

Permalink
fix: sol decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed May 4, 2023
1 parent dcd6f2a commit b226f2b
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 239 deletions.
132 changes: 78 additions & 54 deletions l1-contracts/src/core/Decoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,58 @@ pragma solidity >=0.8.18;
*
* | byte start | num bytes | name
* | --- | --- | ---
* | 0x00 | 0x04 | L2 block number
* | 0x04 | 0x20 | startPrivateDataTreeSnapshot.root
* | 0x24 | 0x04 | startPrivateDataTreeSnapshot.nextAvailableLeafIndex
* | 0x28 | 0x20 | startNullifierTreeSnapshot.root
* | 0x48 | 0x04 | startNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x4c | 0x20 | startContractTreeSnapshot.root
* | 0x6c | 0x04 | startContractTreeSnapshot.nextAvailableLeafIndex
* | 0x70 | 0x20 | startTreeOfHistoricPrivateDataTreeRootsSnapshot.root
* | 0x90 | 0x04 | startTreeOfHistoricPrivateDataTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x94 | 0x20 | startTreeOfHistoricContractTreeRootsSnapshot.root
* | 0xb4 | 0x04 | startTreeOfHistoricContractTreeRootsSnapshot.nextAvailableLeafIndex
* | 0xb8 | 0x20 | startPublicDataTreeRoot
* | 0xd8 | 0x20 | startL1ToL2MessagesTreeSnapshot.root
* | 0xf8 | 0x04 | startL1ToL2MessagesTreeSnapshot.nextAvailableLeafIndex
* | 0xfc | 0x20 | startTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.root
* | 0x11c | 0x04 | startTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x120 | 0x20 | endPrivateDataTreeSnapshot.root
* | 0x140 | 0x04 | endPrivateDataTreeSnapshot.nextAvailableLeafIndex
* | 0x144 | 0x20 | endNullifierTreeSnapshot.root
* | 0x164 | 0x04 | endNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x168 | 0x20 | endContractTreeSnapshot.root
* | 0x188 | 0x04 | endContractTreeSnapshot.nextAvailableLeafIndex
* | 0x18c | 0x20 | endTreeOfHistoricPrivateDataTreeRootsSnapshot.root
* | 0x1ac | 0x04 | endTreeOfHistoricPrivateDataTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x1b0 | 0x20 | endTreeOfHistoricContractTreeRootsSnapshot.root
* | 0x1d0 | 0x04 | endTreeOfHistoricContractTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x1d4 | 0x20 | endPublicDataTreeRoot
* | 0x1f4 | 0x20 | endL1ToL2MessagesTreeSnapshot.root
* | 0x214 | 0x04 | endL1ToL2MessagesTreeSnapshot.nextAvailableLeafIndex
* | 0x218 | 0x20 | endTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.root
* | 0x238 | 0x04 | endTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x23c | 0x04 | len(newCommitments) denoted x
* | 0x240 | x | newCommits
* | 0x240 + x | 0x04 | len(newNullifiers) denoted y
* | 0x244 + x | y | newNullifiers
* | 0x244 + a + b | 0x04 | len(newPublicDataWrites) denoted c
* | 0x248 + a + b | c | newPublicDataWrites (each element 64 bytes)
* | 0x248 + a + b + c | 0x04 | len(newContracts) denoted d
* | 0x24c + a + b + c | d | newContracts (each element 32 bytes)
* | 0x24c + a + b + c + d | d | newContractData (each element 52 bytes)
* | 0x24c + a + b + c + d | 0x04 | len(l1ToL2Messages) denoted e
* | 0x250 + a + b + c + d + e| e | l1ToL2Messages
* |--- |--- | ---
* TODO: a,b,c,d,e are number of elements and not bytes, need to be multiplied by the length of the elements.
* | 0x00 | 0x04 | L2 block number
* | 0x04 | 0x20 | startPrivateDataTreeSnapshot.root
* | 0x24 | 0x04 | startPrivateDataTreeSnapshot.nextAvailableLeafIndex
* | 0x28 | 0x20 | startNullifierTreeSnapshot.root
* | 0x48 | 0x04 | startNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x4c | 0x20 | startContractTreeSnapshot.root
* | 0x6c | 0x04 | startContractTreeSnapshot.nextAvailableLeafIndex
* | 0x70 | 0x20 | startTreeOfHistoricPrivateDataTreeRootsSnapshot.root
* | 0x90 | 0x04 | startTreeOfHistoricPrivateDataTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x94 | 0x20 | startTreeOfHistoricContractTreeRootsSnapshot.root
* | 0xb4 | 0x04 | startTreeOfHistoricContractTreeRootsSnapshot.nextAvailableLeafIndex
* | 0xb8 | 0x20 | startPublicDataTreeRoot
* | 0xd8 | 0x20 | startL1ToL2MessagesTreeSnapshot.root
* | 0xf8 | 0x04 | startL1ToL2MessagesTreeSnapshot.nextAvailableLeafIndex
* | 0xfc | 0x20 | startTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.root
* | 0x11c | 0x04 | startTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x120 | 0x20 | endPrivateDataTreeSnapshot.root
* | 0x140 | 0x04 | endPrivateDataTreeSnapshot.nextAvailableLeafIndex
* | 0x144 | 0x20 | endNullifierTreeSnapshot.root
* | 0x164 | 0x04 | endNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x168 | 0x20 | endContractTreeSnapshot.root
* | 0x188 | 0x04 | endContractTreeSnapshot.nextAvailableLeafIndex
* | 0x18c | 0x20 | endTreeOfHistoricPrivateDataTreeRootsSnapshot.root
* | 0x1ac | 0x04 | endTreeOfHistoricPrivateDataTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x1b0 | 0x20 | endTreeOfHistoricContractTreeRootsSnapshot.root
* | 0x1d0 | 0x04 | endTreeOfHistoricContractTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x1d4 | 0x20 | endPublicDataTreeRoot
* | 0x1f4 | 0x20 | endL1ToL2MessagesTreeSnapshot.root
* | 0x214 | 0x04 | endL1ToL2MessagesTreeSnapshot.nextAvailableLeafIndex
* | 0x218 | 0x20 | endTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.root
* | 0x238 | 0x04 | endTreeOfHistoricL1ToL2MessagesTreeRootsSnapshot.nextAvailableLeafIndex
* | 0x23c | 0x04 | len(newCommitments) denoted x
* | 0x240 | x | newCommits
* | 0x240 + x | 0x04 | len(newNullifiers) denoted y
* | 0x244 + x | y | newNullifiers
* | 0x244 + a + b | 0x04 | len(newPublicDataWrites) denoted c
* | 0x248 + a + b | c | newPublicDataWrites (each element 64 bytes)
* | 0x248 + a + b + c | 0x04 | len(newL2ToL1msgs) denoted d
* | 0x24c + a + b + c | d | newL2ToL1msgs (each element 32 bytes)
* | 0x24c + a + b + c + d | 0x04 | len(newContracts) denoted e
* | 0x250 + a + b + c + d | e | newContracts (each element 32 bytes)
* | 0x250 + a + b + c + d + e | e | newContractData (each element 52 bytes)
* | 0x250 + a + b + c + d + e | 0x04 | len(l1ToL2Messages) denoted f
* | 0x254 + a + b + c + d + e + f | f | l1ToL2Messages
* |--- |--- | ---
* TODO: a,b,c,d,e,f are number of elements and not bytes, need to be multiplied by the length of the elements.
*/
contract Decoder {
uint256 internal constant COMMITMENTS_PER_KERNEL = 4;
uint256 internal constant NULLIFIERS_PER_KERNEL = 4;
uint256 internal constant PUBLIC_DATA_WRITES_PER_KERNEL = 4;
uint256 internal constant L2_TO_L1_MSGS_PER_KERNEL = 2;
uint256 internal constant CONTRACTS_PER_KERNEL = 1;
uint256 internal constant L1_TO_L2_MESSAGES_PER_ROLLUP = 16;

Expand Down Expand Up @@ -166,6 +169,7 @@ contract Decoder {
uint256 commitmentCount;
uint256 nullifierCount;
uint256 dataWritesCount;
uint256 l2ToL1MessagesCount;
uint256 contractCount;
uint256 l1Tol2MessagesCount;
}
Expand All @@ -174,6 +178,7 @@ contract Decoder {
uint256 commitmentOffset;
uint256 nullifierOffset;
uint256 publicDataOffset;
uint256 l2ToL1MsgsOffset;
uint256 contractOffset;
uint256 contractDataOffset;
uint256 l1ToL2MessagesOffset;
Expand Down Expand Up @@ -201,6 +206,8 @@ contract Decoder {
offset := add(add(offset, 0x4), mul(nullifierCount, 0x20))
let dataWritesCount := and(shr(224, calldataload(offset)), 0xffffffff)
offset := add(add(offset, 0x4), mul(nullifierCount, 0x40))
let l2ToL1Count := and(shr(224, calldataload(offset)), 0xffffffff)
offset := add(add(offset, 0x4), mul(l2ToL1Count, 0x20))
let contractCount := and(shr(224, calldataload(offset)), 0xffffffff)
offset := add(add(offset, 0x4), mul(contractCount, 0x54))
let l1Tol2MessagesCount := and(shr(224, calldataload(offset)), 0xffffffff)
Expand All @@ -209,8 +216,9 @@ contract Decoder {
mstore(lengths, commitmentCount)
mstore(add(lengths, 0x20), nullifierCount)
mstore(add(lengths, 0x40), dataWritesCount)
mstore(add(lengths, 0x60), contractCount)
mstore(add(lengths, 0x80), l1Tol2MessagesCount) // currently included to allow optimisation where empty messages are not included in calldata
mstore(add(lengths, 0x60), l2ToL1Count)
mstore(add(lengths, 0x80), contractCount)
mstore(add(lengths, 0xa0), l1Tol2MessagesCount) // currently included to allow optimisation where empty messages are not included in calldata
}
}

Expand All @@ -223,9 +231,10 @@ contract Decoder {
offsets.commitmentOffset = 0x240;
offsets.nullifierOffset = offsets.commitmentOffset + 0x4 + lengths.commitmentCount * 0x20;
offsets.publicDataOffset = offsets.nullifierOffset + 0x4 + lengths.nullifierCount * 0x20;
offsets.contractOffset = offsets.publicDataOffset + 0x4 + lengths.dataWritesCount * 0x40;
offsets.l2ToL1MsgsOffset = offsets.publicDataOffset + 0x4 + lengths.dataWritesCount * 0x40;
offsets.contractOffset = offsets.l2ToL1MsgsOffset + 0x4 + lengths.l2ToL1MessagesCount * 0x20;
offsets.contractDataOffset = offsets.contractOffset + lengths.contractCount * 0x20;
offsets.l1ToL2MessagesOffset = offsets.contractDataOffset + lengths.contractCount * 0x54;
offsets.l1ToL2MessagesOffset = offsets.contractDataOffset + 0x4 + lengths.contractCount * 0x34;

for (uint256 i = 0; i < baseLeafs.length; i++) {
/**
Expand All @@ -237,6 +246,8 @@ contract Decoder {
* newNullifiersKernel2,
* newPublicDataWritesKernel1,
* newPublicDataWritesKernel2,
* newL2ToL1MsgsKernel1,
* newL2ToL1MsgsKernel2,
* newContractLeafKernel1,
* newContractLeafKernel2,
* newContractDataKernel1.aztecAddress,
Expand All @@ -249,7 +260,7 @@ contract Decoder {
*/
// Create the leaf to contain commitments (8 * 0x20) + nullifiers (8 * 0x20)
// + new public data writes (8 * 0x40) + contract deployments (2 * 0x60)
bytes memory baseLeaf = new bytes(0x4c0);
bytes memory baseLeaf = new bytes(0x540);

assembly {
let dstOffset := 0x20
Expand All @@ -275,14 +286,24 @@ contract Decoder {
)
dstOffset := add(dstOffset, mul(0x08, 0x40))

// Adding new l2 to l1 msgs
calldatacopy(
add(baseLeaf, dstOffset),
add(_l2Block.offset, mload(add(offsets, 0x60))),
mul(0x04, 0x20)
)
dstOffset := add(dstOffset, mul(0x04, 0x20))

// Adding Contract Leafs
calldatacopy(
add(baseLeaf, dstOffset), add(_l2Block.offset, mload(add(offsets, 0x60))), mul(2, 0x20)
add(baseLeaf, dstOffset),
add(_l2Block.offset, mload(add(offsets, 0x80))),
mul(0x2, 0x20)
)
dstOffset := add(dstOffset, mul(2, 0x20))

// Kernel1.contract.aztecAddress
let contractDataOffset := mload(add(offsets, 0x80))
let contractDataOffset := mload(add(offsets, 0xa0))
calldatacopy(add(baseLeaf, dstOffset), add(_l2Block.offset, contractDataOffset), 0x20)
dstOffset := add(dstOffset, 0x20)

Expand All @@ -292,7 +313,7 @@ contract Decoder {
calldatacopy(
add(baseLeaf, dstOffset), add(_l2Block.offset, add(contractDataOffset, 0x20)), 0x14
)
dstOffset := add(dstOffset, 0x20)
dstOffset := add(dstOffset, 0x14)

// Kernel2.contract.aztecAddress
calldatacopy(
Expand All @@ -311,6 +332,7 @@ contract Decoder {
offsets.commitmentOffset += 2 * COMMITMENTS_PER_KERNEL * 0x20;
offsets.nullifierOffset += 2 * NULLIFIERS_PER_KERNEL * 0x20;
offsets.publicDataOffset += 2 * PUBLIC_DATA_WRITES_PER_KERNEL * 0x40;
offsets.l2ToL1MsgsOffset += 2 * L2_TO_L1_MSGS_PER_KERNEL * 0x20;
offsets.contractOffset += 2 * 0x20;
offsets.contractDataOffset += 2 * 0x34;

Expand All @@ -323,11 +345,13 @@ contract Decoder {
bytes32 messagesHash;
{
uint256 messagesHashPreimageSize = 0x20 * L1_TO_L2_MESSAGES_PER_ROLLUP;
bytes memory messagesHashPreimage = new bytes(messagesHashPreimageSize);
bytes memory messagesHashPreimage = new bytes(
messagesHashPreimageSize
);
assembly {
calldatacopy(
add(messagesHashPreimage, 0x20),
add(mload(add(offsets, 0xa0)), 20),
add(_l2Block.offset, mload(add(offsets, 0xc0))),
messagesHashPreimageSize
)
}
Expand Down
Loading

0 comments on commit b226f2b

Please sign in to comment.