Skip to content

Commit

Permalink
feat: update header serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed May 22, 2024
1 parent b22eedb commit 22755c7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
10 changes: 7 additions & 3 deletions l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ contract Rollup is IRollup {
revert Errors.Rollup__UnavailableTxs(header.contentCommitment.txsEffectsHash);
}

bytes32[] memory publicInputs = new bytes32[](40);
bytes32[] memory publicInputs = new bytes32[](
2 +
Constants.HEADER_LENGTH +
16
);
publicInputs[0] = _archive;
// this is the _next_ available leaf in the archive tree
// normally this should be equal to the block number (since leaves are 0-indexed and blocks 1-indexed)
// but in yarn-project/merkle-tree/src/new_tree.ts we prefill the tree so that block N is in leaf N
publicInputs[1] = bytes32(header.globalVariables.blockNumber + 1);

bytes32[22] memory headerFields = HeaderLib.toFields(header);
bytes32[] memory headerFields = HeaderLib.toFields(header);
for (uint256 i = 0; i < headerFields.length; i++) {
publicInputs[i + 2] = headerFields[i];
}
Expand All @@ -95,7 +99,7 @@ contract Rollup is IRollup {
// this snippet copies it into the public inputs needed for verification
// it also guards against empty _aggregationObject used with mocked proofs
for (uint256 i = 0; i < 16 && i * 32 < _aggregationObject.length; i++) {
publicInputs[i + 24] = bytes32(_aggregationObject[i * 32:(i + 1) * 32]);
publicInputs[i + 2 + headerFields.length] = bytes32(_aggregationObject[i * 32:(i + 1) * 32]);
}

if (!verifier.verify(_proof, publicInputs)) {
Expand Down
64 changes: 32 additions & 32 deletions l1-contracts/src/core/libraries/HeaderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,37 +198,37 @@ library HeaderLib {
return header;
}

// must match the order in the Header.getFields
function toFields(Header memory _header) internal pure returns (bytes32[22] memory) {
return [
_header.lastArchive.root,
bytes32(uint256(_header.lastArchive.nextAvailableLeafIndex)),
bytes32(_header.contentCommitment.txTreeHeight),
_header.contentCommitment.txsEffectsHash,
_header.contentCommitment.inHash,
_header.contentCommitment.outHash,
_header.stateReference.l1ToL2MessageTree.root,
bytes32(uint256(_header.stateReference.l1ToL2MessageTree.nextAvailableLeafIndex)),
_header.stateReference.partialStateReference.noteHashTree.root,
bytes32(
uint256(_header.stateReference.partialStateReference.noteHashTree.nextAvailableLeafIndex)
),
_header.stateReference.partialStateReference.nullifierTree.root,
bytes32(
uint256(_header.stateReference.partialStateReference.nullifierTree.nextAvailableLeafIndex)
),
_header.stateReference.partialStateReference.publicDataTree.root,
bytes32(
uint256(_header.stateReference.partialStateReference.publicDataTree.nextAvailableLeafIndex)
),
bytes32(_header.globalVariables.chainId),
bytes32(_header.globalVariables.version),
bytes32(_header.globalVariables.blockNumber),
bytes32(_header.globalVariables.timestamp),
bytes32(uint256(uint160(_header.globalVariables.coinbase))),
bytes32(_header.globalVariables.feeRecipient),
bytes32(_header.globalVariables.gasFees.feePerDaGas),
bytes32(_header.globalVariables.gasFees.feePerL2Gas)
];
function toFields(Header memory _header) internal pure returns (bytes32[] memory) {
bytes32[] memory fields = new bytes32[](23);

// must match the order in the Header.getFields
fields[0] = _header.lastArchive.root;
fields[1] = bytes32(uint256(_header.lastArchive.nextAvailableLeafIndex));
fields[2] = bytes32(_header.contentCommitment.txTreeHeight);
fields[3] = _header.contentCommitment.txsEffectsHash;
fields[4] = _header.contentCommitment.inHash;
fields[5] = _header.contentCommitment.outHash;
fields[6] = _header.stateReference.l1ToL2MessageTree.root;
fields[7] = bytes32(uint256(_header.stateReference.l1ToL2MessageTree.nextAvailableLeafIndex));
fields[8] = _header.stateReference.partialStateReference.noteHashTree.root;
fields[9] = bytes32(uint256(_header.stateReference.partialStateReference.noteHashTree.nextAvailableLeafIndex));
fields[10] = _header.stateReference.partialStateReference.nullifierTree.root;
fields[11] = bytes32(uint256(_header.stateReference.partialStateReference.nullifierTree.nextAvailableLeafIndex));
fields[12] = _header.stateReference.partialStateReference.publicDataTree.root;
fields[13] = bytes32(uint256(_header.stateReference.partialStateReference.publicDataTree.nextAvailableLeafIndex));
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);

// fail if the header structure has changed without updating this function
assert(fields.length == Constants.HEADER_LENGTH);

return fields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('proof_verification', () => {
let block: L2Block;
let aggregationObject: Fr[];
let anvil: Anvil | undefined;
let rpcUrl: string;
let walletClient: WalletClient<HttpTransport, Chain, Account>;
let publicClient: PublicClient<HttpTransport, Chain>;
// eslint-disable-next-line
Expand All @@ -53,7 +52,11 @@ describe('proof_verification', () => {

beforeAll(async () => {
logger = getLogger();
({ anvil, rpcUrl } = await startAnvil());
let rpcUrl = process.env.ETHEREUM_HOST;
if (!rpcUrl) {
({ anvil, rpcUrl } = await startAnvil());
}

({ l1ContractAddresses, publicClient, walletClient } = await setupL1Contracts(
rpcUrl,
mnemonicToAccount(MNEMONIC),
Expand Down Expand Up @@ -99,17 +102,12 @@ describe('proof_verification', () => {
const abi = output.contracts['UltraVerifier.sol']['UltraVerifier'].abi;
const bytecode: string = output.contracts['UltraVerifier.sol']['UltraVerifier'].evm.bytecode.object;

try {
const verifierAddress = await deployL1Contract(walletClient, publicClient, abi, `0x${bytecode}`);
verifierContract = getContract({
address: verifierAddress.toString(),
client: publicClient,
abi,
}) as any;
} catch (err) {
logger.error(anvil?.logs.join(' '));
throw err;
}
const verifierAddress = await deployL1Contract(walletClient, publicClient, abi, `0x${bytecode}`);
verifierContract = getContract({
address: verifierAddress.toString(),
client: publicClient,
abi,
}) as any;
});

afterAll(async () => {
Expand All @@ -119,7 +117,7 @@ describe('proof_verification', () => {
await acvmTeardown();
});

beforeEach(async () => {
beforeAll(async () => {
// regenerate with
// AZTEC_GENERATE_TEST_DATA=1 yarn workspace @aztec/end-to-end test e2e_prover
const blockResult = JSON.parse(
Expand Down Expand Up @@ -181,20 +179,20 @@ describe('proof_verification', () => {
client: walletClient,
});

await rollupContract.write.setVerifier([verifierContract.address]);
logger.info('Rollup only accepts valid proofs now');
await availabilityContract.write.publish([`0x${block.body.toBuffer().toString('hex')}`]);
});

it('verifies proof', async () => {
await availabilityContract.write.publish([`0x${block.body.toBuffer().toString('hex')}`]);

await expect(
rollupContract.write.process([
`0x${block.header.toBuffer().toString('hex')}`,
`0x${block.archive.root.toBuffer().toString('hex')}`,
`0x${serializeToBuffer(aggregationObject).toString('hex')}`,
`0x${proof.withoutPublicInputs().toString('hex')}`,
]),
).resolves.toBeDefined();
const args = [
`0x${block.header.toBuffer().toString('hex')}`,
`0x${block.archive.root.toBuffer().toString('hex')}`,
`0x${serializeToBuffer(aggregationObject).toString('hex')}`,
`0x${proof.withoutPublicInputs().toString('hex')}`,
] as const;

await expect(rollupContract.write.process(args)).resolves.toBeDefined();
});
});
});
Loading

0 comments on commit 22755c7

Please sign in to comment.