diff --git a/yarn-project/circuit-types/src/l2_block.ts b/yarn-project/circuit-types/src/l2_block.ts index db167a6640e7..c62b4e8c9db9 100644 --- a/yarn-project/circuit-types/src/l2_block.ts +++ b/yarn-project/circuit-types/src/l2_block.ts @@ -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, @@ -158,9 +158,9 @@ export class L2Block { this.header.state.l1ToL2MessageTree, this.archive, this.body.getTxsEffectsHash(), - ); + ]; - return sha256ToField(buf); + return sha256ToField(preimage); } /** diff --git a/yarn-project/circuit-types/src/messaging/l1_to_l2_message.ts b/yarn-project/circuit-types/src/messaging/l1_to_l2_message.ts index 07666712eee1..79980b07da4f 100644 --- a/yarn-project/circuit-types/src/messaging/l1_to_l2_message.ts +++ b/yarn-project/circuit-types/src/messaging/l1_to_l2_message.ts @@ -41,7 +41,7 @@ export class L1ToL2Message { } hash(): Fr { - return sha256ToField(serializeToBuffer(...this.toFields())); + return sha256ToField(this.toFields()); } static fromBuffer(buffer: Buffer | BufferReader): L1ToL2Message { diff --git a/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts b/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts index fe46f29a2b34..57d02f6cccad 100644 --- a/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts +++ b/yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts @@ -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), @@ -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), diff --git a/yarn-project/end-to-end/src/e2e_outbox.test.ts b/yarn-project/end-to-end/src/e2e_outbox.test.ts index 5f7e84ca507b..071c4d882ae3 100644 --- a/yarn-project/end-to-end/src/e2e_outbox.test.ts +++ b/yarn-project/end-to-end/src/e2e_outbox.test.ts @@ -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; diff --git a/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts b/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts index 713c29eefaa1..93cb2c56bc43 100644 --- a/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts +++ b/yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts @@ -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), @@ -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), @@ -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!, diff --git a/yarn-project/end-to-end/src/shared/cross_chain_test_harness.ts b/yarn-project/end-to-end/src/shared/cross_chain_test_harness.ts index 47b49a52d861..887aef53d3b2 100644 --- a/yarn-project/end-to-end/src/shared/cross_chain_test_harness.ts +++ b/yarn-project/end-to-end/src/shared/cross_chain_test_harness.ts @@ -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; diff --git a/yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts b/yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts index cfc3b3de5540..865b0cb9cd45 100644 --- a/yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts +++ b/yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts @@ -246,53 +246,45 @@ export const uniswapL1L2TestSuite = ( .send() .wait(); - const swapPrivateContent = sha256ToField( - Buffer.concat([ - Buffer.from( - toFunctionSelector( - 'swap_private(address,uint256,uint24,address,uint256,bytes32,bytes32,address)', - ).substring(2), - 'hex', + const swapPrivateContent = sha256ToField([ + Buffer.from( + toFunctionSelector('swap_private(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( + 2, ), - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - new Fr(uniswapFeeTier).toBuffer(), - daiCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(minimumOutputAmount).toBuffer(), - secretHashForRedeemingDai.toBuffer(), - secretHashForDepositingSwappedDai.toBuffer(), - ownerEthAddress.toBuffer32(), - ]), - ); - - const swapPrivateLeaf = sha256ToField( - Buffer.concat([ - uniswapL2Contract.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - EthAddress.fromString(uniswapPortal.address).toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - swapPrivateContent.toBuffer(), - ]), - ); - - const withdrawContent = sha256ToField( - Buffer.concat([ - Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), - uniswapPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - uniswapPortalAddress.toBuffer32(), - ]), - ); + 'hex', + ), + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + new Fr(uniswapFeeTier), + daiCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(minimumOutputAmount), + secretHashForRedeemingDai, + secretHashForDepositingSwappedDai, + ownerEthAddress.toBuffer32(), + ]); - const withdrawLeaf = sha256ToField( - Buffer.concat([ - wethCrossChainHarness.l2Bridge.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - withdrawContent.toBuffer(), - ]), - ); + const swapPrivateLeaf = sha256ToField([ + uniswapL2Contract.address, + new Fr(1), // aztec version + EthAddress.fromString(uniswapPortal.address).toBuffer32(), + new Fr(publicClient.chain.id), // chain id + swapPrivateContent, + ]); + + const withdrawContent = sha256ToField([ + Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), + uniswapPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + uniswapPortalAddress.toBuffer32(), + ]); + + const withdrawLeaf = sha256ToField([ + wethCrossChainHarness.l2Bridge.address, + new Fr(1), // aztec version + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(publicClient.chain.id), // chain id + withdrawContent, + ]); // ensure that user's funds were burnt await wethCrossChainHarness.expectPrivateBalanceOnL2(ownerAddress, wethL2BalanceBeforeSwap - wethAmountToBridge); @@ -474,53 +466,45 @@ export const uniswapL1L2TestSuite = ( // 4.2 Call swap_public from user2 on behalf of owner const uniswapL2Interaction = await action.send().wait(); - const swapPublicContent = sha256ToField( - Buffer.concat([ - Buffer.from( - toFunctionSelector('swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( - 2, - ), - 'hex', + const swapPublicContent = sha256ToField([ + Buffer.from( + toFunctionSelector('swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( + 2, ), - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - new Fr(uniswapFeeTier).toBuffer(), - daiCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(minimumOutputAmount).toBuffer(), - ownerAddress.toBuffer(), - secretHashForDepositingSwappedDai.toBuffer(), - ownerEthAddress.toBuffer32(), - ]), - ); - - const swapPublicLeaf = sha256ToField( - Buffer.concat([ - uniswapL2Contract.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - EthAddress.fromString(uniswapPortal.address).toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - swapPublicContent.toBuffer(), - ]), - ); - - const withdrawContent = sha256ToField( - Buffer.concat([ - Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), - uniswapPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - uniswapPortalAddress.toBuffer32(), - ]), - ); - - const withdrawLeaf = sha256ToField( - Buffer.concat([ - wethCrossChainHarness.l2Bridge.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - withdrawContent.toBuffer(), - ]), - ); + 'hex', + ), + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + new Fr(uniswapFeeTier), + daiCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(minimumOutputAmount), + ownerAddress, + secretHashForDepositingSwappedDai, + ownerEthAddress.toBuffer32(), + ]); + + const swapPublicLeaf = sha256ToField([ + uniswapL2Contract.address, + new Fr(1), // aztec version + EthAddress.fromString(uniswapPortal.address).toBuffer32(), + new Fr(publicClient.chain.id), // chain id + swapPublicContent, + ]); + + const withdrawContent = sha256ToField([ + Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), + uniswapPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + uniswapPortalAddress.toBuffer32(), + ]); + + const withdrawLeaf = sha256ToField([ + wethCrossChainHarness.l2Bridge.address, + new Fr(1), // aztec version + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(publicClient.chain.id), // chain id + withdrawContent, + ]); // check weth balance of owner on L2 (we first bridged `wethAmountToBridge` into L2 and now withdrew it!) await wethCrossChainHarness.expectPublicBalanceOnL2(ownerAddress, wethL2BalanceBeforeSwap - wethAmountToBridge); @@ -846,53 +830,45 @@ export const uniswapL1L2TestSuite = ( .send() .wait(); - const swapPrivateContent = sha256ToField( - Buffer.concat([ - Buffer.from( - toFunctionSelector( - 'swap_private(address,uint256,uint24,address,uint256,bytes32,bytes32,address)', - ).substring(2), - 'hex', + const swapPrivateContent = sha256ToField([ + Buffer.from( + toFunctionSelector('swap_private(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( + 2, ), - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - new Fr(uniswapFeeTier).toBuffer(), - daiCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(minimumOutputAmount).toBuffer(), - secretHashForRedeemingDai.toBuffer(), - secretHashForDepositingSwappedDai.toBuffer(), - ownerEthAddress.toBuffer32(), - ]), - ); - - const swapPrivateLeaf = sha256ToField( - Buffer.concat([ - uniswapL2Contract.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - EthAddress.fromString(uniswapPortal.address).toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - swapPrivateContent.toBuffer(), - ]), - ); - - const withdrawContent = sha256ToField( - Buffer.concat([ - Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), - uniswapPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - uniswapPortalAddress.toBuffer32(), - ]), - ); + 'hex', + ), + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + new Fr(uniswapFeeTier), + daiCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(minimumOutputAmount), + secretHashForRedeemingDai, + secretHashForDepositingSwappedDai, + ownerEthAddress.toBuffer32(), + ]); - const withdrawLeaf = sha256ToField( - Buffer.concat([ - wethCrossChainHarness.l2Bridge.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - withdrawContent.toBuffer(), - ]), - ); + const swapPrivateLeaf = sha256ToField([ + uniswapL2Contract.address, + new Fr(1), // aztec version + EthAddress.fromString(uniswapPortal.address).toBuffer32(), + new Fr(publicClient.chain.id), // chain id + swapPrivateContent, + ]); + + const withdrawContent = sha256ToField([ + Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), + uniswapPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + uniswapPortalAddress.toBuffer32(), + ]); + + const withdrawLeaf = sha256ToField([ + wethCrossChainHarness.l2Bridge.address, + new Fr(1), // aztec version + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(publicClient.chain.id), // chain id + withdrawContent, + ]); const [swapPrivateL2MessageIndex, swapPrivateSiblingPath] = await aztecNode.getL2ToL1MessageMembershipWitness( withdrawReceipt.blockNumber!, @@ -977,53 +953,45 @@ export const uniswapL1L2TestSuite = ( .send() .wait(); - const swapPublicContent = sha256ToField( - Buffer.concat([ - Buffer.from( - toFunctionSelector('swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( - 2, - ), - 'hex', + const swapPublicContent = sha256ToField([ + Buffer.from( + toFunctionSelector('swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)').substring( + 2, ), - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - new Fr(uniswapFeeTier).toBuffer(), - daiCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(minimumOutputAmount).toBuffer(), - ownerAddress.toBuffer(), - secretHashForDepositingSwappedDai.toBuffer(), - ownerEthAddress.toBuffer32(), - ]), - ); - - const swapPublicLeaf = sha256ToField( - Buffer.concat([ - uniswapL2Contract.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - EthAddress.fromString(uniswapPortal.address).toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - swapPublicContent.toBuffer(), - ]), - ); - - const withdrawContent = sha256ToField( - Buffer.concat([ - Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), - uniswapPortalAddress.toBuffer32(), - new Fr(wethAmountToBridge).toBuffer(), - uniswapPortalAddress.toBuffer32(), - ]), - ); - - const withdrawLeaf = sha256ToField( - Buffer.concat([ - wethCrossChainHarness.l2Bridge.address.toBuffer(), - new Fr(1).toBuffer(), // aztec version - wethCrossChainHarness.tokenPortalAddress.toBuffer32(), - new Fr(publicClient.chain.id).toBuffer(), // chain id - withdrawContent.toBuffer(), - ]), - ); + 'hex', + ), + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + new Fr(uniswapFeeTier), + daiCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(minimumOutputAmount), + ownerAddress, + secretHashForDepositingSwappedDai, + ownerEthAddress.toBuffer32(), + ]); + + const swapPublicLeaf = sha256ToField([ + uniswapL2Contract.address, + new Fr(1), // aztec version + EthAddress.fromString(uniswapPortal.address).toBuffer32(), + new Fr(publicClient.chain.id), // chain id + swapPublicContent, + ]); + + const withdrawContent = sha256ToField([ + Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'), + uniswapPortalAddress.toBuffer32(), + new Fr(wethAmountToBridge), + uniswapPortalAddress.toBuffer32(), + ]); + + const withdrawLeaf = sha256ToField([ + wethCrossChainHarness.l2Bridge.address, + new Fr(1), // aztec version + wethCrossChainHarness.tokenPortalAddress.toBuffer32(), + new Fr(publicClient.chain.id), // chain id + withdrawContent, + ]); const [swapPublicL2MessageIndex, swapPublicSiblingPath] = await aztecNode.getL2ToL1MessageMembershipWitness( withdrawReceipt.blockNumber!, diff --git a/yarn-project/foundation/src/crypto/sha256/index.ts b/yarn-project/foundation/src/crypto/sha256/index.ts index 0f6fb259e85c..6b9676f39133 100644 --- a/yarn-project/foundation/src/crypto/sha256/index.ts +++ b/yarn-project/foundation/src/crypto/sha256/index.ts @@ -2,9 +2,13 @@ import { default as hash } from 'hash.js'; import { Fr } from '../../fields/fields.js'; import { truncateAndPad } from '../../serialize/free_funcs.js'; +import { Bufferable, serializeToBuffer } from '../../serialize/serialize.js'; export const sha256 = (data: Buffer) => Buffer.from(hash.sha256().update(data).digest()); export const sha256Trunc = (data: Buffer) => truncateAndPad(sha256(data)); -export const sha256ToField = (data: Buffer) => Fr.fromBuffer(sha256Trunc(data)); +export const sha256ToField = (data: Bufferable[]) => { + const buffer = serializeToBuffer(data); + return Fr.fromBuffer(sha256Trunc(buffer)); +}; diff --git a/yarn-project/foundation/src/fields/point.ts b/yarn-project/foundation/src/fields/point.ts index b1135d6e6b55..dfb5065c03d1 100644 --- a/yarn-project/foundation/src/fields/point.ts +++ b/yarn-project/foundation/src/fields/point.ts @@ -1,4 +1,4 @@ -import { BufferReader, FieldReader } from '../serialize/index.js'; +import { BufferReader, FieldReader, serializeToBuffer } from '../serialize/index.js'; import { Fr } from './fields.js'; /** @@ -88,7 +88,7 @@ export class Point { * @returns A Buffer representation of the Point instance. */ toBuffer() { - return Buffer.concat([this.x.toBuffer(), this.y.toBuffer()]); + return serializeToBuffer([this.x, this.y]); } /** diff --git a/yarn-project/pxe/src/database/deferred_note_dao.ts b/yarn-project/pxe/src/database/deferred_note_dao.ts index af0ee6a89b2d..06354bb4074c 100644 --- a/yarn-project/pxe/src/database/deferred_note_dao.ts +++ b/yarn-project/pxe/src/database/deferred_note_dao.ts @@ -29,12 +29,12 @@ export class DeferredNoteDao { toBuffer(): Buffer { return serializeToBuffer( - this.publicKey.toBuffer(), - this.note.toBuffer(), - this.contractAddress.toBuffer(), - this.storageSlot.toBuffer(), - this.noteTypeId.toBuffer(), - this.txHash.toBuffer(), + this.publicKey, + this.note, + this.contractAddress, + this.storageSlot, + this.noteTypeId, + this.txHash, new Vector(this.newNoteHashes), this.dataStartIndexForTx, ); diff --git a/yarn-project/pxe/src/database/note_dao.ts b/yarn-project/pxe/src/database/note_dao.ts index 2255420fc57f..5ca513111823 100644 --- a/yarn-project/pxe/src/database/note_dao.ts +++ b/yarn-project/pxe/src/database/note_dao.ts @@ -1,7 +1,7 @@ import { Note, TxHash } from '@aztec/circuit-types'; import { AztecAddress, Fr, Point, PublicKey } from '@aztec/circuits.js'; -import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer'; -import { BufferReader } from '@aztec/foundation/serialize'; +import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; +import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; import { NoteData } from '@aztec/simulator'; /** @@ -35,17 +35,17 @@ export class NoteDao implements NoteData { ) {} toBuffer(): Buffer { - return Buffer.concat([ - this.note.toBuffer(), - this.contractAddress.toBuffer(), - this.storageSlot.toBuffer(), - this.noteTypeId.toBuffer(), + return serializeToBuffer([ + this.note, + this.contractAddress, + this.storageSlot, + this.noteTypeId, this.txHash.buffer, - this.nonce.toBuffer(), - this.innerNoteHash.toBuffer(), - this.siloedNullifier.toBuffer(), - toBufferBE(this.index, 32), - this.publicKey.toBuffer(), + this.nonce, + this.innerNoteHash, + this.siloedNullifier, + this.index, + this.publicKey, ]); } static fromBuffer(buffer: Buffer | BufferReader) { diff --git a/yarn-project/simulator/src/avm/opcodes/hashing.ts b/yarn-project/simulator/src/avm/opcodes/hashing.ts index ca212dc2d197..a7e54e539e66 100644 --- a/yarn-project/simulator/src/avm/opcodes/hashing.ts +++ b/yarn-project/simulator/src/avm/opcodes/hashing.ts @@ -166,7 +166,7 @@ export class Pedersen extends Instruction { // We hash a set of field elements const messageSize = Number(context.machineState.memory.get(messageSizeOffset).toBigInt()); - const hashData = context.machineState.memory.getSlice(messageOffset, messageSize).map(word => word.toBuffer()); + const hashData = context.machineState.memory.getSlice(messageOffset, messageSize); // No domain sep for now const hash = pedersenHash(hashData); diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 3733ccd67f76..6495690acb18 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -447,7 +447,7 @@ describe('Private Execution test suite', () => { logger(`Parent deployed at ${parentAddress.toShortString()}`); logger(`Calling child function ${childSelector.toString()} at ${childAddress.toShortString()}`); - const args = [Fr.fromBuffer(childAddress.toBuffer()), Fr.fromBuffer(childSelector.toBuffer())]; + const args = [childAddress, childSelector]; const result = await runSimulator({ args, artifact: parentArtifact }); expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(privateIncrement)); @@ -782,7 +782,7 @@ describe('Private Execution test suite', () => { oracle.getPortalContractAddress.mockImplementation(() => Promise.resolve(childPortalContractAddress)); oracle.getFunctionArtifact.mockImplementation(() => Promise.resolve({ ...childContractArtifact, isInternal })); - const args = [Fr.fromBuffer(childAddress.toBuffer()), childSelector.toField(), 42n]; + const args = [childAddress, childSelector, 42n]; const result = await runSimulator({ msgSender: parentAddress, contractAddress: parentAddress, diff --git a/yarn-project/simulator/src/test/utils.ts b/yarn-project/simulator/src/test/utils.ts index f8f5b6b41660..1e0118b9c979 100644 --- a/yarn-project/simulator/src/test/utils.ts +++ b/yarn-project/simulator/src/test/utils.ts @@ -20,8 +20,7 @@ export const buildL1ToL2Message = ( // Write the selector into a buffer. const selectorBuf = Buffer.from(selector, 'hex'); - const contentBuf = Buffer.concat([selectorBuf, ...contentPreimage.map(field => field.toBuffer())]); - const content = sha256ToField(contentBuf); + const content = sha256ToField([selectorBuf, ...contentPreimage]); const secretHash = computeMessageSecretHash(secret); // Eventually the kernel will need to prove the kernel portal pair exists within the contract tree,