diff --git a/yarn-project/acir-simulator/src/client/private_execution.test.ts b/yarn-project/acir-simulator/src/client/private_execution.test.ts index df220ffee7df..60ec1e167d49 100644 --- a/yarn-project/acir-simulator/src/client/private_execution.test.ts +++ b/yarn-project/acir-simulator/src/client/private_execution.test.ts @@ -400,7 +400,8 @@ describe('Private Execution test suite', () => { const secret = new Fr(1n); const preimage = await buildL1ToL2Message([new Fr(bridgedAmount), new Fr(recipient.x)], contractAddress, secret); - const messageKey = preimage.hash(); + // stub message key + const messageKey = Fr.random(); const tree: AppendOnlyTree = await newTree( StandardTree, diff --git a/yarn-project/aztec.js/src/index.ts b/yarn-project/aztec.js/src/index.ts index 64f67ec0e271..3d4b94f9f2d6 100644 --- a/yarn-project/aztec.js/src/index.ts +++ b/yarn-project/aztec.js/src/index.ts @@ -1,5 +1,6 @@ export * from './contract/index.js'; export * from './contract_deployer/index.js'; +export * from './utils/index.js'; // TODO - only export necessary stuffs export * from '@aztec/aztec-rpc'; diff --git a/yarn-project/aztec.js/src/utils/index.ts b/yarn-project/aztec.js/src/utils/index.ts new file mode 100644 index 000000000000..670591ba413e --- /dev/null +++ b/yarn-project/aztec.js/src/utils/index.ts @@ -0,0 +1 @@ +export * from './secrets.js'; diff --git a/yarn-project/aztec.js/src/utils/secrets.ts b/yarn-project/aztec.js/src/utils/secrets.ts new file mode 100644 index 000000000000..758bd6879e46 --- /dev/null +++ b/yarn-project/aztec.js/src/utils/secrets.ts @@ -0,0 +1,12 @@ +import { CircuitsWasm, Fr } from '@aztec/circuits.js'; +import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; + +/** + * Given a secret, it computes its pedersen hash - used to send l1 to l2 messages + * @param secret - the secret to hash - secret could be generated however you want e.g. `Fr.random()` + * @returns the hash + */ +export async function computeMessageSecretHash(secret: Fr): Promise { + const wasm = await CircuitsWasm.get(); + return computeSecretMessageHash(wasm, secret); +} 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 a14506154553..99fef5534c2b 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 @@ -1,9 +1,7 @@ import { AztecNodeService } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCServer, Contract, TxStatus } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, Contract, TxStatus, computeMessageSecretHash } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/foundation/eth-address'; -import { CircuitsWasm } from '@aztec/circuits.js'; -import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; import { DeployL1Contracts } from '@aztec/ethereum'; import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer'; import { Fr } from '@aztec/foundation/fields'; @@ -88,12 +86,11 @@ describe('e2e_cross_chain_messaging', () => { it('Milestone 2: Deposit funds from L1 -> L2 and withdraw back to L1', async () => { // Generate a claim secret using pedersen - // TODO (#741): make this into an aztec.js utility function logger("Generating a claim secret using pedersen's hash function"); - const wasm = await CircuitsWasm.get(); const secret = Fr.random(); - const claimSecretHash = computeSecretMessageHash(wasm, secret); - logger('Generated claim secret: ', claimSecretHash.toString()); + const secretHash = await computeMessageSecretHash(secret); + const secretString = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; + logger('Generated claim secret: ', secretString); logger('Minting tokens on L1'); await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any); @@ -102,7 +99,6 @@ describe('e2e_cross_chain_messaging', () => { expect(await underlyingERC20.read.balanceOf([ethAccount.toString()])).toBe(1000000n); // Deposit tokens to the TokenPortal - const secretString = `0x${claimSecretHash.toBuffer().toString('hex')}` as `0x${string}`; const deadline = 2 ** 32 - 1; // max uint32 - 1 const mintAmount = 100n; diff --git a/yarn-project/end-to-end/src/integration_archiver_l1_to_l2.test.ts b/yarn-project/end-to-end/src/integration_archiver_l1_to_l2.test.ts index 2ef62baa4bfe..ff4a0a0478af 100644 --- a/yarn-project/end-to-end/src/integration_archiver_l1_to_l2.test.ts +++ b/yarn-project/end-to-end/src/integration_archiver_l1_to_l2.test.ts @@ -1,9 +1,7 @@ import { AztecNodeConfig, AztecNodeService } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCServer, Contract } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, Contract, computeMessageSecretHash } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/foundation/eth-address'; -import { CircuitsWasm } from '@aztec/circuits.js'; -import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; import { DeployL1Contracts } from '@aztec/ethereum'; import { Fr } from '@aztec/foundation/fields'; import { DebugLogger } from '@aztec/foundation/log'; @@ -84,12 +82,11 @@ describe('archiver integration with l1 to l2 messages', () => { // create a message, then cancel it // Generate a claim secret using pedersen - // TODO (#741): make this into an aztec.js utility function logger("Generating a claim secret using pedersen's hash function"); - const wasm = await CircuitsWasm.get(); const secret = Fr.random(); - const claimSecretHash = computeSecretMessageHash(wasm, secret); - logger('Generated claim secret: ', claimSecretHash.toString()); + const secretHash = await computeMessageSecretHash(secret); + const secretString = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; + logger('Generated claim secret: ', secretString); logger('Minting tokens on L1'); await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any); @@ -98,7 +95,6 @@ describe('archiver integration with l1 to l2 messages', () => { expect(await underlyingERC20.read.balanceOf([ethAccount.toString()])).toBe(1000000n); // Deposit tokens to the TokenPortal - const secretString = `0x${claimSecretHash.toBuffer().toString('hex')}` as `0x${string}`; const deadline = Number((await publicClient.getBlock()).timestamp + 1000n); const mintAmount = 100n; diff --git a/yarn-project/types/src/l1_to_l2_message.ts b/yarn-project/types/src/l1_to_l2_message.ts index 58ad69a51e76..f8596428fdbb 100644 --- a/yarn-project/types/src/l1_to_l2_message.ts +++ b/yarn-project/types/src/l1_to_l2_message.ts @@ -2,8 +2,6 @@ import { EthAddress } from '@aztec/foundation/eth-address'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr } from '@aztec/foundation/fields'; import { BufferReader, serializeToBuffer } from '@aztec/circuits.js/utils'; -import { sha256 } from '@aztec/foundation/crypto'; -import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer'; /** * Interface of classes allowing for the retrieval of L1 to L2 messages. @@ -74,13 +72,6 @@ export class L1ToL2Message { public readonly entryKey?: Fr, ) {} - // TODO: (#646) - sha256 hash of the message packed the same as solidity - hash(): Fr { - const buf = this.toBuffer(); - const temp = toBigIntBE(sha256(buf)); - return Fr.fromBuffer(toBufferBE(temp % Fr.MODULUS, 32)); - } - /** * Returns each element within its own field so that it can be consumed by an acvm oracle call. * @returns The message as an array of fields (in order).