From 474396908d5890784352a51dbb9be2e6a8040fa6 Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 7 Jun 2023 16:44:00 +0000 Subject: [PATCH 1/3] fix #741 and #646 --- yarn-project/aztec.js/src/index.ts | 1 + yarn-project/aztec.js/src/utils/index.ts | 1 + yarn-project/aztec.js/src/utils/utils.ts | 14 ++++++++++++++ .../src/e2e_cross_chain_messaging.test.ts | 11 +++-------- .../src/integration_archiver_l1_to_l2.test.ts | 11 +++-------- yarn-project/types/src/l1_to_l2_message.ts | 9 --------- 6 files changed, 22 insertions(+), 25 deletions(-) create mode 100644 yarn-project/aztec.js/src/utils/index.ts create mode 100644 yarn-project/aztec.js/src/utils/utils.ts 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..9be8099fb951 --- /dev/null +++ b/yarn-project/aztec.js/src/utils/index.ts @@ -0,0 +1 @@ +export * from './utils.js'; diff --git a/yarn-project/aztec.js/src/utils/utils.ts b/yarn-project/aztec.js/src/utils/utils.ts new file mode 100644 index 000000000000..baf645e2cade --- /dev/null +++ b/yarn-project/aztec.js/src/utils/utils.ts @@ -0,0 +1,14 @@ +import { CircuitsWasm, Fr } from '@aztec/circuits.js'; +import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; + +/** + * Given a secret, it computes its pederson hash - used to send l1 to l2 messages + * @param secret - the secret to hash (defaults to a random field element) + * @returns secret and its pederson hash (in hex). + */ +export async function createMessageSecretAndHash(secret = Fr.random()) { + const wasm = await CircuitsWasm.get(); + const secretHash = computeSecretMessageHash(wasm, secret); + const secretHashHex = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; + return secretHashHex; +} 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..9a5124fc70b1 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, createMessageSecretAndHash } 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,10 @@ 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 secretString = await createMessageSecretAndHash(secret); + logger('Generated claim secret: ', secretString); logger('Minting tokens on L1'); await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any); @@ -102,7 +98,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..c3d35c16a049 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, createMessageSecretAndHash } 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,10 @@ 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 secretString = await createMessageSecretAndHash(secret); + logger('Generated claim secret: ', secretString); logger('Minting tokens on L1'); await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any); @@ -98,7 +94,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). From 4b8fbc123a6e9276f12a1bf13a81edc334945e32 Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 7 Jun 2023 17:18:04 +0000 Subject: [PATCH 2/3] fix ci, naming, return fr --- .../acir-simulator/src/client/private_execution.test.ts | 3 ++- yarn-project/aztec.js/src/utils/index.ts | 2 +- yarn-project/aztec.js/src/utils/{utils.ts => secrets.ts} | 8 +++----- .../end-to-end/src/e2e_cross_chain_messaging.test.ts | 5 +++-- .../end-to-end/src/integration_archiver_l1_to_l2.test.ts | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) rename yarn-project/aztec.js/src/utils/{utils.ts => secrets.ts} (52%) 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/utils/index.ts b/yarn-project/aztec.js/src/utils/index.ts index 9be8099fb951..670591ba413e 100644 --- a/yarn-project/aztec.js/src/utils/index.ts +++ b/yarn-project/aztec.js/src/utils/index.ts @@ -1 +1 @@ -export * from './utils.js'; +export * from './secrets.js'; diff --git a/yarn-project/aztec.js/src/utils/utils.ts b/yarn-project/aztec.js/src/utils/secrets.ts similarity index 52% rename from yarn-project/aztec.js/src/utils/utils.ts rename to yarn-project/aztec.js/src/utils/secrets.ts index baf645e2cade..f0b4423d4b0c 100644 --- a/yarn-project/aztec.js/src/utils/utils.ts +++ b/yarn-project/aztec.js/src/utils/secrets.ts @@ -4,11 +4,9 @@ import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; /** * Given a secret, it computes its pederson hash - used to send l1 to l2 messages * @param secret - the secret to hash (defaults to a random field element) - * @returns secret and its pederson hash (in hex). + * @returns the hash */ -export async function createMessageSecretAndHash(secret = Fr.random()) { +export async function computeMessageSecretHash(secret = Fr.random()): Promise { const wasm = await CircuitsWasm.get(); - const secretHash = computeSecretMessageHash(wasm, secret); - const secretHashHex = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; - return secretHashHex; + 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 9a5124fc70b1..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,5 +1,5 @@ import { AztecNodeService } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCServer, Contract, TxStatus, createMessageSecretAndHash } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, Contract, TxStatus, computeMessageSecretHash } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/foundation/eth-address'; import { DeployL1Contracts } from '@aztec/ethereum'; @@ -88,7 +88,8 @@ describe('e2e_cross_chain_messaging', () => { // Generate a claim secret using pedersen logger("Generating a claim secret using pedersen's hash function"); const secret = Fr.random(); - const secretString = await createMessageSecretAndHash(secret); + 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'); 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 c3d35c16a049..f71e6dfb3728 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,5 +1,5 @@ import { AztecNodeConfig, AztecNodeService } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCServer, Contract, createMessageSecretAndHash } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, Contract, computeMessageSecretHash } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/foundation/eth-address'; import { DeployL1Contracts } from '@aztec/ethereum'; @@ -83,8 +83,8 @@ describe('archiver integration with l1 to l2 messages', () => { // Generate a claim secret using pedersen logger("Generating a claim secret using pedersen's hash function"); - const secret = Fr.random(); - const secretString = await createMessageSecretAndHash(secret); + const secretHash = await computeMessageSecretHash(); + const secretString = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; logger('Generated claim secret: ', secretString); logger('Minting tokens on L1'); From 197f6319dad88f24486cc9a506a63dc649a481fd Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Thu, 8 Jun 2023 09:43:48 +0000 Subject: [PATCH 3/3] don't pass secret as default val --- yarn-project/aztec.js/src/utils/secrets.ts | 6 +++--- .../end-to-end/src/integration_archiver_l1_to_l2.test.ts | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/yarn-project/aztec.js/src/utils/secrets.ts b/yarn-project/aztec.js/src/utils/secrets.ts index f0b4423d4b0c..758bd6879e46 100644 --- a/yarn-project/aztec.js/src/utils/secrets.ts +++ b/yarn-project/aztec.js/src/utils/secrets.ts @@ -2,11 +2,11 @@ import { CircuitsWasm, Fr } from '@aztec/circuits.js'; import { computeSecretMessageHash } from '@aztec/circuits.js/abis'; /** - * Given a secret, it computes its pederson hash - used to send l1 to l2 messages - * @param secret - the secret to hash (defaults to a random field element) + * 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.random()): Promise { +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/integration_archiver_l1_to_l2.test.ts b/yarn-project/end-to-end/src/integration_archiver_l1_to_l2.test.ts index f71e6dfb3728..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 @@ -83,7 +83,8 @@ describe('archiver integration with l1 to l2 messages', () => { // Generate a claim secret using pedersen logger("Generating a claim secret using pedersen's hash function"); - const secretHash = await computeMessageSecretHash(); + const secret = Fr.random(); + const secretHash = await computeMessageSecretHash(secret); const secretString = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`; logger('Generated claim secret: ', secretString);