Skip to content

Commit

Permalink
Bufferable in sha256ToField
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 26, 2024
1 parent 4c7667b commit 77bc989
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 255 deletions.
6 changes: 3 additions & 3 deletions yarn-project/circuit-types/src/l2_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class L2Block {
*/
// TODO(#4844)
getPublicInputsHash(): Fr {
const buf = serializeToBuffer(
const preimage = [
this.header.globalVariables,
AppendOnlyTreeSnapshot.zero(), // this.startNoteHashTreeSnapshot / commitments,
AppendOnlyTreeSnapshot.zero(), // this.startNullifierTreeSnapshot,
Expand All @@ -158,9 +158,9 @@ export class L2Block {
this.header.state.l1ToL2MessageTree,
this.archive,
this.body.getTxsEffectsHash(),
);
];

return sha256ToField(buf);
return sha256ToField(preimage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class L1ToL2Message {
}

hash(): Fr {
return sha256ToField(serializeToBuffer(...this.toFields()));
return sha256ToField(this.toFields());
}

static fromBuffer(buffer: Buffer | BufferReader): L1ToL2Message {
Expand Down
22 changes: 10 additions & 12 deletions yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ describe('e2e_cross_chain_messaging', () => {
await crossChainTestHarness.makeMessageConsumable(msgHash);

// 3. Consume L1 -> L2 message and mint private tokens on L2
const content = sha256ToField(
Buffer.concat([
Buffer.from(toFunctionSelector('mint_private(bytes32,uint256)').substring(2), 'hex'),
serializeToBuffer(...[secretHashForL2MessageConsumption, new Fr(bridgeAmount)]),
]),
);
const content = sha256ToField([
Buffer.from(toFunctionSelector('mint_private(bytes32,uint256)').substring(2), 'hex'),
secretHashForL2MessageConsumption,
new Fr(bridgeAmount),
]);
const wrongMessage = new L1ToL2Message(
new L1Actor(crossChainTestHarness.tokenPortalAddress, crossChainTestHarness.publicClient.chain.id),
new L2Actor(l2Bridge.address, 1),
Expand Down Expand Up @@ -235,12 +234,11 @@ describe('e2e_cross_chain_messaging', () => {
// Wait for the message to be available for consumption
await crossChainTestHarness.makeMessageConsumable(msgHash);

const content = sha256ToField(
Buffer.concat([
Buffer.from(toFunctionSelector('mint_public(bytes32,uint256)').substring(2), 'hex'),
serializeToBuffer(...[ownerAddress, new Fr(bridgeAmount)]),
]),
);
const content = sha256ToField([
Buffer.from(toFunctionSelector('mint_public(bytes32,uint256)').substring(2), 'hex'),
ownerAddress,
new Fr(bridgeAmount),
]);
const wrongMessage = new L1ToL2Message(
new L1Actor(crossChainTestHarness.tokenPortalAddress, crossChainTestHarness.publicClient.chain.id),
new L2Actor(l2Bridge.address, 1),
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/end-to-end/src/e2e_outbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ describe('E2E Outbox Tests', () => {

function makeL2ToL1Message(recipient: EthAddress, content: Fr = Fr.ZERO): Fr {
const leaf = sha256ToField(
Buffer.concat([
contract.address.toBuffer(),
new Fr(1).toBuffer(), // aztec version
[
contract.address,
new Fr(1), // aztec version
recipient.toBuffer32(),
new Fr(deployL1ContractsValues.publicClient.chain.id).toBuffer(), // chain id
content.toBuffer(),
]),
new Fr(deployL1ContractsValues.publicClient.chain.id), // chain id
content,
]
);

return leaf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,11 @@ describe('e2e_public_cross_chain_messaging', () => {

await crossChainTestHarness.makeMessageConsumable(msgHash);

const content = sha256ToField(
Buffer.concat([
Buffer.from(toFunctionSelector('mint_public(bytes32,uint256)').substring(2), 'hex'),
serializeToBuffer(...[user2Wallet.getAddress(), new Fr(bridgeAmount)]),
]),
);
const content = sha256ToField([
Buffer.from(toFunctionSelector('mint_public(bytes32,uint256)').substring(2), 'hex'),
user2Wallet.getAddress(),
new Fr(bridgeAmount),
]);
const wrongMessage = new L1ToL2Message(
new L1Actor(crossChainTestHarness.tokenPortalAddress, crossChainTestHarness.publicClient.chain.id),
new L2Actor(l2Bridge.address, 1),
Expand Down Expand Up @@ -204,12 +203,11 @@ describe('e2e_public_cross_chain_messaging', () => {
await crossChainTestHarness.makeMessageConsumable(msgHash);

// Wrong message hash
const content = sha256ToField(
Buffer.concat([
Buffer.from(toFunctionSelector('mint_private(bytes32,uint256)').substring(2), 'hex'),
serializeToBuffer(...[secretHash, new Fr(bridgeAmount)]),
]),
);
const content = sha256ToField([
Buffer.from(toFunctionSelector('mint_private(bytes32,uint256)').substring(2), 'hex'),
secretHash,
new Fr(bridgeAmount),
]);
const wrongMessage = new L1ToL2Message(
new L1Actor(crossChainTestHarness.tokenPortalAddress, crossChainTestHarness.publicClient.chain.id),
new L2Actor(l2Bridge.address, 1),
Expand Down Expand Up @@ -256,15 +254,13 @@ describe('e2e_public_cross_chain_messaging', () => {
content: content.toString() as Hex,
};

const leaf = sha256ToField(
Buffer.concat([
testContract.address.toBuffer(),
new Fr(1).toBuffer(), // aztec version
recipient.toBuffer32(),
new Fr(crossChainTestHarness.publicClient.chain.id).toBuffer(), // chain id
content.toBuffer(),
]),
);
const leaf = sha256ToField([
testContract.address,
new Fr(1), // aztec version
recipient.toBuffer32(),
new Fr(crossChainTestHarness.publicClient.chain.id), // chain id
content,
]);

const [l2MessageIndex, siblingPath] = await aztecNode.getL2ToL1MessageMembershipWitness(
l2TxReceipt.blockNumber!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,21 @@ export class CrossChainTestHarness {

getL2ToL1MessageLeaf(withdrawAmount: bigint, callerOnL1: EthAddress = EthAddress.ZERO): Fr {
const content = sha256ToField(
Buffer.concat([
[
Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'),
this.ethAccount.toBuffer32(),
new Fr(withdrawAmount).toBuffer(),
callerOnL1.toBuffer32(),
]),
]
);
const leaf = sha256ToField(
Buffer.concat([
[
this.l2Bridge.address.toBuffer(),
new Fr(1).toBuffer(), // aztec version
this.tokenPortalAddress.toBuffer32() ?? Buffer.alloc(32, 0),
new Fr(this.publicClient.chain.id).toBuffer(), // chain id
content.toBuffer(),
]),
]
);

return leaf;
Expand Down
Loading

0 comments on commit 77bc989

Please sign in to comment.