From f6e04bae88f742679b9ca0749a280005074140e6 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 13:34:21 +0000 Subject: [PATCH 01/10] added note tagging oracle --- .../aztec-nr/aztec/src/oracle/notes.nr | 12 +++++++++ yarn-project/key-store/src/key_store.ts | 16 ++++------- .../pxe/src/simulator_oracle/index.ts | 26 ++++++++++++++++++ .../simulator/src/acvm/oracle/oracle.ts | 8 ++++++ .../simulator/src/acvm/oracle/typed_oracle.ts | 6 ++++- .../src/client/client_execution_context.ts | 27 ------------------- .../simulator/src/client/db_oracle.ts | 2 ++ .../simulator/src/client/view_data_oracle.ts | 4 +++ 8 files changed, 62 insertions(+), 39 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr index 90c3dcad8626..7c4822307fd7 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr @@ -198,3 +198,15 @@ pub unconstrained fn check_nullifier_exists(inner_nullifier: Field) -> bool { #[oracle(checkNullifierExists)] unconstrained fn check_nullifier_exists_oracle(_inner_nullifier: Field) -> Field {} + +/// Returns the tagging secret for a given sender and recipient pair. For this to work, PXE must know the ivpsk_m of the sender. +/// For the recipient's side, only the address is needed. +pub unconstrained fn get_tagging_secret(sender: AztecAddress, recipient: AztecAddress) -> Field { + get_tagging_secret_oracle(sender, recipient) +} + +#[oracle(getTaggingSecret)] +unconstrained fn get_tagging_secret_oracle( + _sender: AztecAddress, + _recipient: AztecAddress, +) -> Field {} diff --git a/yarn-project/key-store/src/key_store.ts b/yarn-project/key-store/src/key_store.ts index c6ee5e1b121f..c0d41bd9318d 100644 --- a/yarn-project/key-store/src/key_store.ts +++ b/yarn-project/key-store/src/key_store.ts @@ -205,13 +205,12 @@ export class KeyStore { } /** - * Retrieves application incoming viewing secret key. + * Retrieves master incoming viewing secret key. * @throws If the account does not exist in the key store. - * @param account - The account to retrieve the application incoming viewing secret key for. - * @param app - The application address to retrieve the incoming viewing secret key for. - * @returns A Promise that resolves to the application incoming viewing secret key. + * @param account - The account to retrieve the master incoming viewing secret key for. + * @returns A Promise that resolves to the master incoming viewing secret key. */ - public async getAppIncomingViewingSecretKey(account: AztecAddress, app: AztecAddress): Promise { + public async getMasterIncomingViewingSecretKey(account: AztecAddress): Promise { const masterIncomingViewingSecretKeyBuffer = this.#keys.get(`${account.toString()}-ivsk_m`); if (!masterIncomingViewingSecretKeyBuffer) { throw new Error( @@ -220,12 +219,7 @@ export class KeyStore { } const masterIncomingViewingSecretKey = GrumpkinScalar.fromBuffer(masterIncomingViewingSecretKeyBuffer); - return Promise.resolve( - poseidon2HashWithSeparator( - [masterIncomingViewingSecretKey.hi, masterIncomingViewingSecretKey.lo, app], - GeneratorIndex.IVSK_M, - ), - ); + return Promise.resolve(masterIncomingViewingSecretKey); } /** diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index fac1a6a43ecb..f3c653e3ebf2 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -11,13 +11,18 @@ import { type AztecAddress, type CompleteAddress, type ContractInstance, + Fq, type Fr, type FunctionSelector, type Header, type KeyValidationRequest, type L1_TO_L2_MSG_TREE_HEIGHT, + computePoint, + computePreaddress, } from '@aztec/circuits.js'; +import { Grumpkin } from '@aztec/circuits.js/barretenberg'; import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/abi'; +import { poseidon2Hash } from '@aztec/foundation/crypto'; import { createDebugLogger } from '@aztec/foundation/log'; import { type KeyStore } from '@aztec/key-store'; import { type DBOracle, MessageLoadOracleInputs } from '@aztec/simulator'; @@ -226,4 +231,25 @@ export class SimulatorOracle implements DBOracle { public getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector): Promise { return this.contractDataOracle.getDebugFunctionName(contractAddress, selector); } + + public async getTaggingSecret( + contractAddress: AztecAddress, + sender: AztecAddress, + recipient: AztecAddress, + ): Promise { + const senderCompleteAddress = await this.getCompleteAddress(sender); + const senderPreaddress = computePreaddress( + senderCompleteAddress.publicKeys.hash(), + senderCompleteAddress.partialAddress, + ); + const ivskSender = await this.keyStore.getMasterIncomingViewingSecretKey(senderPreaddress); + // TODO: #8970 - Computation of address point from x coordinate might fail + const recipientAddressPoint = computePoint(recipient); + const curve = new Grumpkin(); + // Given A (sender) -> B (recipient) and h == preaddress + // Compute shared secret as S = (h_A + ivsk_A) * Addr_Point_B + const sharedSecret = curve.mul(recipientAddressPoint, ivskSender.add(new Fq(senderPreaddress.toBigInt()))); + // Silo the secret to the app so it can't be used to track other app's notes + return poseidon2Hash([sharedSecret.x, sharedSecret.y, contractAddress]); + } } diff --git a/yarn-project/simulator/src/acvm/oracle/oracle.ts b/yarn-project/simulator/src/acvm/oracle/oracle.ts index 103ac0a8f552..103888b4bd5b 100644 --- a/yarn-project/simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/oracle.ts @@ -408,4 +408,12 @@ export class Oracle { notifySetMinRevertibleSideEffectCounter([minRevertibleSideEffectCounter]: ACVMField[]) { this.typedOracle.notifySetMinRevertibleSideEffectCounter(frToNumber(fromACVMField(minRevertibleSideEffectCounter))); } + + async getTaggingSecret([sender]: ACVMField[], [recipient]: ACVMField[]): Promise { + const taggingSecret = await this.typedOracle.getTaggingSecret( + AztecAddress.fromString(sender), + AztecAddress.fromString(recipient), + ); + return toACVMField(taggingSecret); + } } diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index ed3eb107b0c2..132b3a3a4c23 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -16,7 +16,7 @@ import { } from '@aztec/circuits.js'; import { type FunctionSelector, type NoteSelector } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr } from '@aztec/foundation/fields'; +import { Fr, Point } from '@aztec/foundation/fields'; /** * Information about a note needed during execution. @@ -252,4 +252,8 @@ export abstract class TypedOracle { debugLog(_message: string, _fields: Fr[]): void { throw new OracleMethodNotAvailableError('debugLog'); } + + getTaggingSecret(_sender: AztecAddress, _recipient: AztecAddress): Promise { + throw new OracleMethodNotAvailableError('getTaggingSecret'); + } } diff --git a/yarn-project/simulator/src/client/client_execution_context.ts b/yarn-project/simulator/src/client/client_execution_context.ts index bd729f1dccbd..06c51c9b0bf4 100644 --- a/yarn-project/simulator/src/client/client_execution_context.ts +++ b/yarn-project/simulator/src/client/client_execution_context.ts @@ -602,33 +602,6 @@ export class ClientExecutionContext extends ViewDataOracle { ); } - /** - * Read the public storage data. - * @param contractAddress - The address to read storage from. - * @param startStorageSlot - The starting storage slot. - * @param blockNumber - The block number to read storage at. - * @param numberOfElements - Number of elements to read from the starting storage slot. - */ - public override async storageRead( - contractAddress: Fr, - startStorageSlot: Fr, - blockNumber: number, - numberOfElements: number, - ): Promise { - const values = []; - for (let i = 0n; i < numberOfElements; i++) { - const storageSlot = new Fr(startStorageSlot.value + i); - - const value = await this.aztecNode.getPublicStorageAt(contractAddress, storageSlot, blockNumber); - this.log.debug( - `Oracle storage read: slot=${storageSlot.toString()} address-${contractAddress.toString()} value=${value}`, - ); - - values.push(value); - } - return values; - } - public override debugLog(message: string, fields: Fr[]) { this.log.verbose(`debug_log ${applyStringFormatting(message, fields)}`); } diff --git a/yarn-project/simulator/src/client/db_oracle.ts b/yarn-project/simulator/src/client/db_oracle.ts index 6d44cfe39c9b..a364b22a1e24 100644 --- a/yarn-project/simulator/src/client/db_oracle.ts +++ b/yarn-project/simulator/src/client/db_oracle.ts @@ -193,4 +193,6 @@ export interface DBOracle extends CommitmentsDB { * @returns The block number. */ getBlockNumber(): Promise; + + getTaggingSecret(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress): Promise; } diff --git a/yarn-project/simulator/src/client/view_data_oracle.ts b/yarn-project/simulator/src/client/view_data_oracle.ts index d4d1f7364e8e..402c39e392a4 100644 --- a/yarn-project/simulator/src/client/view_data_oracle.ts +++ b/yarn-project/simulator/src/client/view_data_oracle.ts @@ -288,4 +288,8 @@ export class ViewDataOracle extends TypedOracle { const formattedStr = applyStringFormatting(message, fields); this.log.verbose(`debug_log ${formattedStr}`); } + + public override async getTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { + return await this.db.getTaggingSecret(this.contractAddress, sender, recipient); + } } From caaf00d436431828bbf7b616f2f0b15806962fb6 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 14:00:27 +0000 Subject: [PATCH 02/10] txe support --- yarn-project/txe/src/oracle/txe_oracle.ts | 24 +++++++++++++++++-- .../txe/src/txe_service/txe_service.ts | 8 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index 027f60b36303..ea84dfdf5610 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -30,10 +30,12 @@ import { type PublicDataTreeLeafPreimage, TxContext, computeContractClassId, + computePoint, + computePreaddress, deriveKeys, getContractClassFromArtifact, } from '@aztec/circuits.js'; -import { Schnorr } from '@aztec/circuits.js/barretenberg'; +import { Grumpkin, Schnorr } from '@aztec/circuits.js/barretenberg'; import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/circuits.js/hash'; import { type ContractArtifact, @@ -43,7 +45,8 @@ import { countArgumentsSize, } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr } from '@aztec/foundation/fields'; +import { poseidon2Hash } from '@aztec/foundation/crypto'; +import { Fq, Fr } from '@aztec/foundation/fields'; import { type Logger, applyStringFormatting } from '@aztec/foundation/log'; import { Timer } from '@aztec/foundation/timer'; import { type KeyStore } from '@aztec/key-store'; @@ -747,6 +750,23 @@ export class TXE implements TypedOracle { return; } + async getTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { + const senderCompleteAddress = await this.getCompleteAddress(sender); + const senderPreaddress = computePreaddress( + senderCompleteAddress.publicKeys.hash(), + senderCompleteAddress.partialAddress, + ); + const ivskSender = await this.keyStore.getMasterIncomingViewingSecretKey(senderPreaddress); + // TODO: #8970 - Computation of address point from x coordinate might fail + const recipientAddressPoint = computePoint(recipient); + const curve = new Grumpkin(); + // Given A (sender) -> B (recipient) and h == preaddress + // Compute shared secret as S = (h_A + ivsk_A) * Addr_Point_B + const sharedSecret = curve.mul(recipientAddressPoint, ivskSender.add(new Fq(senderPreaddress.toBigInt()))); + // Silo the secret to the app so it can't be used to track other app's notes + return poseidon2Hash([sharedSecret.x, sharedSecret.y, this.contractAddress]); + } + // AVM oracles async avmOpcodeCall( diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index 13cc7637562c..d65182ab9a70 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -599,6 +599,14 @@ export class TXEService { return toForeignCallResult([]); } + async getTaggingSecret(sender: ForeignCallSingle, recipient: ForeignCallSingle) { + const secret = await this.typedOracle.getTaggingSecret( + AztecAddress.fromField(fromSingle(sender)), + AztecAddress.fromField(fromSingle(recipient)), + ); + return toForeignCallResult([toSingle(secret)]); + } + // AVM opcodes avmOpcodeEmitUnencryptedLog(_message: ForeignCallArray) { From 9393ad501c50e8293f2cba383885b86b6f528171 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 14:07:42 +0000 Subject: [PATCH 03/10] added comments --- yarn-project/pxe/src/simulator_oracle/index.ts | 7 +++++++ yarn-project/simulator/src/client/db_oracle.ts | 7 +++++++ yarn-project/simulator/src/client/view_data_oracle.ts | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index f3c653e3ebf2..9d8e7fabf593 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -232,6 +232,13 @@ export class SimulatorOracle implements DBOracle { return this.contractDataOracle.getDebugFunctionName(contractAddress, selector); } + /** + * Returns the tagging secret for a given sender and recipient pair. For this to work, the ivpsk_m of the sender must be known. + * @param contractAddress - The contract address to silo the secret for + * @param sender - The address sending the note + * @param recipient - The address receiving the note + * @returns A tagging secret that can be used to tag notes. + */ public async getTaggingSecret( contractAddress: AztecAddress, sender: AztecAddress, diff --git a/yarn-project/simulator/src/client/db_oracle.ts b/yarn-project/simulator/src/client/db_oracle.ts index a364b22a1e24..3f0df0ac4406 100644 --- a/yarn-project/simulator/src/client/db_oracle.ts +++ b/yarn-project/simulator/src/client/db_oracle.ts @@ -194,5 +194,12 @@ export interface DBOracle extends CommitmentsDB { */ getBlockNumber(): Promise; + /** + * Returns the tagging secret for a given sender and recipient pair. For this to work, the ivpsk_m of the sender must be known. + * @param contractAddress - The contract address to silo the secret for + * @param sender - The address sending the note + * @param recipient - The address receiving the note + * @returns A tagging secret that can be used to tag notes. + */ getTaggingSecret(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress): Promise; } diff --git a/yarn-project/simulator/src/client/view_data_oracle.ts b/yarn-project/simulator/src/client/view_data_oracle.ts index 402c39e392a4..8da5bc56d15b 100644 --- a/yarn-project/simulator/src/client/view_data_oracle.ts +++ b/yarn-project/simulator/src/client/view_data_oracle.ts @@ -289,6 +289,13 @@ export class ViewDataOracle extends TypedOracle { this.log.verbose(`debug_log ${formattedStr}`); } + /** + * Returns the tagging secret for a given sender and recipient pair, siloed to the current contract address. + * For this to work, the ivpsk_m of the sender must be known. + * @param sender - The address sending the note + * @param recipient - The address receiving the note + * @returns A tagging secret that can be used to tag notes. + */ public override async getTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { return await this.db.getTaggingSecret(this.contractAddress, sender, recipient); } From a8152130b7a61579c50e6104f1dd4a56c3b6e8f6 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 14:09:15 +0000 Subject: [PATCH 04/10] fmt and comments --- yarn-project/simulator/src/acvm/oracle/typed_oracle.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index 132b3a3a4c23..1d8d8b7130a8 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -16,7 +16,7 @@ import { } from '@aztec/circuits.js'; import { type FunctionSelector, type NoteSelector } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr, Point } from '@aztec/foundation/fields'; +import { Fr } from '@aztec/foundation/fields'; /** * Information about a note needed during execution. From 172cb8fcc9c6c7b6c84d0eae986618a1d92b091a Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 14:14:00 +0000 Subject: [PATCH 05/10] fix --- yarn-project/key-store/src/key_store.test.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/yarn-project/key-store/src/key_store.test.ts b/yarn-project/key-store/src/key_store.test.ts index 7c1e4ce68b5a..a816660a9a79 100644 --- a/yarn-project/key-store/src/key_store.test.ts +++ b/yarn-project/key-store/src/key_store.test.ts @@ -43,6 +43,11 @@ describe('KeyStore', () => { `"0x07cec19d32f1cbaaacf16edc081021b696c86dff14160779373ffc77b04568e7076f25b0e7f0d02fd6433d788483e2262c1e45c5962790b40d1cd7efbd5253d3"`, ); + const masterIncomingViewingSecretKey = await keyStore.getMasterIncomingViewingSecretKey(accountAddress); + expect(masterIncomingViewingSecretKey.toString()).toMatchInlineSnapshot( + `"0x1d1d920024dd64e019c23de36d27aefe4d9d4d05983b99cf85bea9e85fd60020"`, + ); + // Arbitrary app contract address const appAddress = AztecAddress.fromBigInt(624n); @@ -53,11 +58,6 @@ describe('KeyStore', () => { ); expect(obtainedMasterNullifierPublicKey).toEqual(masterNullifierPublicKey); - const appIncomingViewingSecretKey = await keyStore.getAppIncomingViewingSecretKey(accountAddress, appAddress); - expect(appIncomingViewingSecretKey.toString()).toMatchInlineSnapshot( - `"0x0247d73d16cf0939cc783b3cee140b37b294b6cbc1c0295d530f3f637c9b8034"`, - ); - const appOutgoingViewingSecretKey = await keyStore.getAppOutgoingViewingSecretKey(accountAddress, appAddress); expect(appOutgoingViewingSecretKey.toString()).toMatchInlineSnapshot( `"0x296c9931262d8b95b4cbbcc66ac4c97d2cc3fab4da5eedc08fcff80f1ce37e34"`, @@ -76,8 +76,10 @@ describe('KeyStore', () => { ); // Manages to find master incoming viewing secret key for pub key - const masterIncomingViewingSecretKey = await keyStore.getMasterSecretKey(masterIncomingViewingPublicKey); - expect(masterIncomingViewingSecretKey.toString()).toMatchInlineSnapshot( + const masterIncomingViewingSecretKeyFromPublicKey = await keyStore.getMasterSecretKey( + masterIncomingViewingPublicKey, + ); + expect(masterIncomingViewingSecretKeyFromPublicKey.toString()).toMatchInlineSnapshot( `"0x1d1d920024dd64e019c23de36d27aefe4d9d4d05983b99cf85bea9e85fd60020"`, ); }); From cb84d4bdcfc95f59c708aca583eba22ee94e6799 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 15:57:16 +0000 Subject: [PATCH 06/10] comments and renaming --- noir-projects/aztec-nr/aztec/src/oracle/notes.nr | 14 +++++++++----- yarn-project/simulator/src/acvm/oracle/oracle.ts | 4 ++-- .../simulator/src/acvm/oracle/typed_oracle.ts | 2 +- .../simulator/src/client/view_data_oracle.ts | 2 +- yarn-project/txe/src/oracle/txe_oracle.ts | 2 +- yarn-project/txe/src/txe_service/txe_service.ts | 4 ++-- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr index 7c4822307fd7..642dcb4a0e03 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr @@ -199,14 +199,18 @@ pub unconstrained fn check_nullifier_exists(inner_nullifier: Field) -> bool { #[oracle(checkNullifierExists)] unconstrained fn check_nullifier_exists_oracle(_inner_nullifier: Field) -> Field {} -/// Returns the tagging secret for a given sender and recipient pair. For this to work, PXE must know the ivpsk_m of the sender. +/// Returns the tagging secret for a given sender and recipient pair, siloed for the current contract address. +/// For this to work, PXE must know the ivpsk_m of the sender. /// For the recipient's side, only the address is needed. -pub unconstrained fn get_tagging_secret(sender: AztecAddress, recipient: AztecAddress) -> Field { - get_tagging_secret_oracle(sender, recipient) +pub unconstrained fn get_app_tagging_secret( + sender: AztecAddress, + recipient: AztecAddress, +) -> Field { + get_app_tagging_secret_oracle(sender, recipient) } -#[oracle(getTaggingSecret)] -unconstrained fn get_tagging_secret_oracle( +#[oracle(getAppTaggingSecret)] +unconstrained fn get_app_tagging_secret_oracle( _sender: AztecAddress, _recipient: AztecAddress, ) -> Field {} diff --git a/yarn-project/simulator/src/acvm/oracle/oracle.ts b/yarn-project/simulator/src/acvm/oracle/oracle.ts index 103888b4bd5b..e3c315f07083 100644 --- a/yarn-project/simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/oracle.ts @@ -409,8 +409,8 @@ export class Oracle { this.typedOracle.notifySetMinRevertibleSideEffectCounter(frToNumber(fromACVMField(minRevertibleSideEffectCounter))); } - async getTaggingSecret([sender]: ACVMField[], [recipient]: ACVMField[]): Promise { - const taggingSecret = await this.typedOracle.getTaggingSecret( + async getAppTaggingSecret([sender]: ACVMField[], [recipient]: ACVMField[]): Promise { + const taggingSecret = await this.typedOracle.getAppTaggingSecret( AztecAddress.fromString(sender), AztecAddress.fromString(recipient), ); diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index 1d8d8b7130a8..27164e95bdb0 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -253,7 +253,7 @@ export abstract class TypedOracle { throw new OracleMethodNotAvailableError('debugLog'); } - getTaggingSecret(_sender: AztecAddress, _recipient: AztecAddress): Promise { + getAppTaggingSecret(_sender: AztecAddress, _recipient: AztecAddress): Promise { throw new OracleMethodNotAvailableError('getTaggingSecret'); } } diff --git a/yarn-project/simulator/src/client/view_data_oracle.ts b/yarn-project/simulator/src/client/view_data_oracle.ts index 8da5bc56d15b..ecbfe8bc4aaa 100644 --- a/yarn-project/simulator/src/client/view_data_oracle.ts +++ b/yarn-project/simulator/src/client/view_data_oracle.ts @@ -296,7 +296,7 @@ export class ViewDataOracle extends TypedOracle { * @param recipient - The address receiving the note * @returns A tagging secret that can be used to tag notes. */ - public override async getTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { + public override async getAppTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { return await this.db.getTaggingSecret(this.contractAddress, sender, recipient); } } diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index ea84dfdf5610..09517b464c5d 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -750,7 +750,7 @@ export class TXE implements TypedOracle { return; } - async getTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { + async getAppTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { const senderCompleteAddress = await this.getCompleteAddress(sender); const senderPreaddress = computePreaddress( senderCompleteAddress.publicKeys.hash(), diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index d65182ab9a70..3469c9ad6aa9 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -599,8 +599,8 @@ export class TXEService { return toForeignCallResult([]); } - async getTaggingSecret(sender: ForeignCallSingle, recipient: ForeignCallSingle) { - const secret = await this.typedOracle.getTaggingSecret( + async getAppTaggingSecret(sender: ForeignCallSingle, recipient: ForeignCallSingle) { + const secret = await this.typedOracle.getAppTaggingSecret( AztecAddress.fromField(fromSingle(sender)), AztecAddress.fromField(fromSingle(recipient)), ); From 4b232037b52113047845ad40189b2fbee4f71683 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 16:14:21 +0000 Subject: [PATCH 07/10] refactor --- yarn-project/circuits.js/src/keys/derivation.ts | 14 ++++++++++++++ yarn-project/pxe/src/simulator_oracle/index.ts | 16 ++++------------ .../simulator/src/acvm/oracle/typed_oracle.ts | 2 +- yarn-project/simulator/src/client/db_oracle.ts | 2 +- .../simulator/src/client/view_data_oracle.ts | 2 +- yarn-project/txe/src/oracle/txe_oracle.ts | 14 +++----------- 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/yarn-project/circuits.js/src/keys/derivation.ts b/yarn-project/circuits.js/src/keys/derivation.ts index 1caff8bff86d..74a46b02a979 100644 --- a/yarn-project/circuits.js/src/keys/derivation.ts +++ b/yarn-project/circuits.js/src/keys/derivation.ts @@ -4,6 +4,7 @@ import { Fq, Fr, GrumpkinScalar, Point } from '@aztec/foundation/fields'; import { Grumpkin } from '../barretenberg/crypto/grumpkin/index.js'; import { GeneratorIndex } from '../constants.gen.js'; +import { CompleteAddress } from '../index.js'; import { PublicKeys } from '../types/public_keys.js'; import { type KeyPrefix } from './key_types.js'; import { getKeyGenerator } from './utils.js'; @@ -125,3 +126,16 @@ export function deriveKeys(secretKey: Fr) { publicKeys, }; } + +export function computeTaggingSecret(senderCompleteAddress: CompleteAddress, senderIvsk: Fq, recipient: AztecAddress) { + const senderPreaddress = computePreaddress( + senderCompleteAddress.publicKeys.hash(), + senderCompleteAddress.partialAddress, + ); + // TODO: #8970 - Computation of address point from x coordinate might fail + const recipientAddressPoint = computePoint(recipient); + const curve = new Grumpkin(); + // Given A (sender) -> B (recipient) and h == preaddress + // Compute shared secret as S = (h_A + ivsk_A) * Addr_Point_B + return curve.mul(recipientAddressPoint, senderIvsk.add(new Fq(senderPreaddress.toBigInt()))); +} diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index 9d8e7fabf593..0046a2952815 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -19,6 +19,7 @@ import { type L1_TO_L2_MSG_TREE_HEIGHT, computePoint, computePreaddress, + computeTaggingSecret, } from '@aztec/circuits.js'; import { Grumpkin } from '@aztec/circuits.js/barretenberg'; import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/abi'; @@ -239,23 +240,14 @@ export class SimulatorOracle implements DBOracle { * @param recipient - The address receiving the note * @returns A tagging secret that can be used to tag notes. */ - public async getTaggingSecret( + public async getAppTaggingSecret( contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress, ): Promise { const senderCompleteAddress = await this.getCompleteAddress(sender); - const senderPreaddress = computePreaddress( - senderCompleteAddress.publicKeys.hash(), - senderCompleteAddress.partialAddress, - ); - const ivskSender = await this.keyStore.getMasterIncomingViewingSecretKey(senderPreaddress); - // TODO: #8970 - Computation of address point from x coordinate might fail - const recipientAddressPoint = computePoint(recipient); - const curve = new Grumpkin(); - // Given A (sender) -> B (recipient) and h == preaddress - // Compute shared secret as S = (h_A + ivsk_A) * Addr_Point_B - const sharedSecret = curve.mul(recipientAddressPoint, ivskSender.add(new Fq(senderPreaddress.toBigInt()))); + const senderIvsk = await this.keyStore.getMasterIncomingViewingSecretKey(sender); + const sharedSecret = computeTaggingSecret(senderCompleteAddress, senderIvsk, recipient); // Silo the secret to the app so it can't be used to track other app's notes return poseidon2Hash([sharedSecret.x, sharedSecret.y, contractAddress]); } diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index 27164e95bdb0..1e66f0f150c7 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -254,6 +254,6 @@ export abstract class TypedOracle { } getAppTaggingSecret(_sender: AztecAddress, _recipient: AztecAddress): Promise { - throw new OracleMethodNotAvailableError('getTaggingSecret'); + throw new OracleMethodNotAvailableError('getAppTaggingSecret'); } } diff --git a/yarn-project/simulator/src/client/db_oracle.ts b/yarn-project/simulator/src/client/db_oracle.ts index 3f0df0ac4406..c80d4fed5df9 100644 --- a/yarn-project/simulator/src/client/db_oracle.ts +++ b/yarn-project/simulator/src/client/db_oracle.ts @@ -201,5 +201,5 @@ export interface DBOracle extends CommitmentsDB { * @param recipient - The address receiving the note * @returns A tagging secret that can be used to tag notes. */ - getTaggingSecret(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress): Promise; + getAppTaggingSecret(contractAddress: AztecAddress, sender: AztecAddress, recipient: AztecAddress): Promise; } diff --git a/yarn-project/simulator/src/client/view_data_oracle.ts b/yarn-project/simulator/src/client/view_data_oracle.ts index ecbfe8bc4aaa..644c4b8b6bdc 100644 --- a/yarn-project/simulator/src/client/view_data_oracle.ts +++ b/yarn-project/simulator/src/client/view_data_oracle.ts @@ -297,6 +297,6 @@ export class ViewDataOracle extends TypedOracle { * @returns A tagging secret that can be used to tag notes. */ public override async getAppTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { - return await this.db.getTaggingSecret(this.contractAddress, sender, recipient); + return await this.db.getAppTaggingSecret(this.contractAddress, sender, recipient); } } diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index 09517b464c5d..e2fa27d44e27 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -32,6 +32,7 @@ import { computeContractClassId, computePoint, computePreaddress, + computeTaggingSecret, deriveKeys, getContractClassFromArtifact, } from '@aztec/circuits.js'; @@ -752,17 +753,8 @@ export class TXE implements TypedOracle { async getAppTaggingSecret(sender: AztecAddress, recipient: AztecAddress): Promise { const senderCompleteAddress = await this.getCompleteAddress(sender); - const senderPreaddress = computePreaddress( - senderCompleteAddress.publicKeys.hash(), - senderCompleteAddress.partialAddress, - ); - const ivskSender = await this.keyStore.getMasterIncomingViewingSecretKey(senderPreaddress); - // TODO: #8970 - Computation of address point from x coordinate might fail - const recipientAddressPoint = computePoint(recipient); - const curve = new Grumpkin(); - // Given A (sender) -> B (recipient) and h == preaddress - // Compute shared secret as S = (h_A + ivsk_A) * Addr_Point_B - const sharedSecret = curve.mul(recipientAddressPoint, ivskSender.add(new Fq(senderPreaddress.toBigInt()))); + const senderIvsk = await this.keyStore.getMasterIncomingViewingSecretKey(sender); + const sharedSecret = computeTaggingSecret(senderCompleteAddress, senderIvsk, recipient); // Silo the secret to the app so it can't be used to track other app's notes return poseidon2Hash([sharedSecret.x, sharedSecret.y, this.contractAddress]); } From b0a4dea9a395633694eec976e34356fb3de8f6b2 Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 16:41:59 +0000 Subject: [PATCH 08/10] fmt --- yarn-project/circuits.js/src/keys/derivation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/circuits.js/src/keys/derivation.ts b/yarn-project/circuits.js/src/keys/derivation.ts index 74a46b02a979..ae298c208b4b 100644 --- a/yarn-project/circuits.js/src/keys/derivation.ts +++ b/yarn-project/circuits.js/src/keys/derivation.ts @@ -4,7 +4,7 @@ import { Fq, Fr, GrumpkinScalar, Point } from '@aztec/foundation/fields'; import { Grumpkin } from '../barretenberg/crypto/grumpkin/index.js'; import { GeneratorIndex } from '../constants.gen.js'; -import { CompleteAddress } from '../index.js'; +import { type CompleteAddress } from '../index.js'; import { PublicKeys } from '../types/public_keys.js'; import { type KeyPrefix } from './key_types.js'; import { getKeyGenerator } from './utils.js'; From 5e8580269e7f42c8df3554d86471387e1d5ae9ee Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 16:42:26 +0000 Subject: [PATCH 09/10] fmt --- yarn-project/pxe/src/simulator_oracle/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index 0046a2952815..1b18b32c3188 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -11,17 +11,13 @@ import { type AztecAddress, type CompleteAddress, type ContractInstance, - Fq, type Fr, type FunctionSelector, type Header, type KeyValidationRequest, type L1_TO_L2_MSG_TREE_HEIGHT, - computePoint, - computePreaddress, computeTaggingSecret, } from '@aztec/circuits.js'; -import { Grumpkin } from '@aztec/circuits.js/barretenberg'; import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/abi'; import { poseidon2Hash } from '@aztec/foundation/crypto'; import { createDebugLogger } from '@aztec/foundation/log'; From 6c32236cd1f5503692e55de5a2a12990b1baf2ba Mon Sep 17 00:00:00 2001 From: thunkar Date: Fri, 25 Oct 2024 17:18:38 +0000 Subject: [PATCH 10/10] more fmt --- yarn-project/txe/src/oracle/txe_oracle.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index e2fa27d44e27..3823d6d940d4 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -30,13 +30,11 @@ import { type PublicDataTreeLeafPreimage, TxContext, computeContractClassId, - computePoint, - computePreaddress, computeTaggingSecret, deriveKeys, getContractClassFromArtifact, } from '@aztec/circuits.js'; -import { Grumpkin, Schnorr } from '@aztec/circuits.js/barretenberg'; +import { Schnorr } from '@aztec/circuits.js/barretenberg'; import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/circuits.js/hash'; import { type ContractArtifact, @@ -47,7 +45,7 @@ import { } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { poseidon2Hash } from '@aztec/foundation/crypto'; -import { Fq, Fr } from '@aztec/foundation/fields'; +import { Fr } from '@aztec/foundation/fields'; import { type Logger, applyStringFormatting } from '@aztec/foundation/log'; import { Timer } from '@aztec/foundation/timer'; import { type KeyStore } from '@aztec/key-store';