From 0c518de5c45c7c9fa12d5f91a95c7768d86c8139 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 10:13:09 +0000 Subject: [PATCH 01/15] refactor: using poseidon2 when computing a nullifier --- .../aztec-nr/address-note/src/address_note.nr | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/noir-projects/aztec-nr/address-note/src/address_note.nr b/noir-projects/aztec-nr/address-note/src/address_note.nr index 440cfdf3b6f..891b5a2f734 100644 --- a/noir-projects/aztec-nr/address-note/src/address_note.nr +++ b/noir-projects/aztec-nr/address-note/src/address_note.nr @@ -2,10 +2,10 @@ use dep::aztec::log::emit_encrypted_log; // docs:end:encrypted_import use dep::aztec::{ - protocol_types::{address::AztecAddress, traits::Empty}, + protocol_types::{address::AztecAddress, traits::Empty, hash::poseidon2_hash, constants::GENERATOR_INDEX__NULLIFIER}, note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - hash::pedersen_hash, context::PrivateContext + context::PrivateContext }; global ADDRESS_NOTE_LEN: Field = 3; @@ -24,21 +24,21 @@ impl NoteInterface for AddressNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn compute_nullifier_without_context(self) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. From 5a66cb5b7008fa7ba2178f5ec5d5fbeb91968be3 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 10:44:12 +0000 Subject: [PATCH 02/15] WIP --- .../aztec-nr/address-note/src/address_note.nr | 4 ++-- .../aztec-nr/value-note/src/value_note.nr | 19 ++++++++------- .../src/subscription_note.nr | 23 ++++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/noir-projects/aztec-nr/address-note/src/address_note.nr b/noir-projects/aztec-nr/address-note/src/address_note.nr index 891b5a2f734..c940d8b404a 100644 --- a/noir-projects/aztec-nr/address-note/src/address_note.nr +++ b/noir-projects/aztec-nr/address-note/src/address_note.nr @@ -2,10 +2,10 @@ use dep::aztec::log::emit_encrypted_log; // docs:end:encrypted_import use dep::aztec::{ - protocol_types::{address::AztecAddress, traits::Empty, hash::poseidon2_hash, constants::GENERATOR_INDEX__NULLIFIER}, + protocol_types::{address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NULLIFIER}, note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - context::PrivateContext + context::PrivateContext, hash::poseidon2_hash }; global ADDRESS_NOTE_LEN: Field = 3; diff --git a/noir-projects/aztec-nr/value-note/src/value_note.nr b/noir-projects/aztec-nr/value-note/src/value_note.nr index 8875209b9de..31d0fb7d695 100644 --- a/noir-projects/aztec-nr/value-note/src/value_note.nr +++ b/noir-projects/aztec-nr/value-note/src/value_note.nr @@ -1,8 +1,11 @@ use dep::aztec::{ - protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}}, + protocol_types::{ + address::AztecAddress, traits::{Deserialize, Serialize}, + constants::GENERATOR_INDEX__NULLIFIER +}, note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - log::emit_encrypted_log, hash::pedersen_hash, context::PrivateContext + log::emit_encrypted_log, hash::poseidon2_hash, context::PrivateContext }; global VALUE_NOTE_LEN: Field = 3; // 3 plus a header. @@ -22,11 +25,11 @@ impl NoteInterface for ValueNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // docs:end:nullifier @@ -34,11 +37,11 @@ impl NoteInterface for ValueNote { fn compute_nullifier_without_context(self) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr index dc984c37338..0b30b02b70e 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr @@ -1,6 +1,7 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteHeader, emit_encrypted_log, NoteInterface}; use dep::aztec::{ - note::utils::compute_note_hash_for_consumption, hash::pedersen_hash, + constants::GENERATOR_INDEX__NULLIFIER, note::utils::compute_note_hash_for_consumption, + hash::poseidon2_hash, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} }; @@ -17,23 +18,23 @@ struct SubscriptionNote { impl NoteInterface for SubscriptionNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn compute_nullifier_without_context(self) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. From abad169a8ea86411e4ef04cb559b450fad89da5d Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 11:02:43 +0000 Subject: [PATCH 03/15] WIP --- .../src/subscription_note.nr | 2 +- .../src/types/card_note.nr | 12 +++++---- .../src/ecdsa_public_key_note.nr | 22 ++++++++-------- .../src/public_key_note.nr | 25 +++++++++--------- .../src/types/token_note.nr | 21 ++++++++------- .../src/types/transparent_note.nr | 12 +++++---- .../token_contract/src/types/token_note.nr | 26 +++++++++---------- .../src/types/transparent_note.nr | 12 +++++---- 8 files changed, 71 insertions(+), 61 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr index 0b30b02b70e..c24e8e6ee74 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr @@ -1,6 +1,6 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteHeader, emit_encrypted_log, NoteInterface}; use dep::aztec::{ - constants::GENERATOR_INDEX__NULLIFIER, note::utils::compute_note_hash_for_consumption, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} }; diff --git a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr index 617b24ef025..c4ecfacc323 100644 --- a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr +++ b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteInterface, NoteHeader, PrivateContex use dep::aztec::{ note::{utils::compute_note_hash_for_consumption}, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - hash::pedersen_hash, protocol_types::traits::Empty + hash::poseidon2_hash, protocol_types::{traits::Empty, constants::GENERATOR_INDEX__NULLIFIER}, }; // Shows how to create a custom note @@ -28,19 +28,21 @@ impl NoteInterface for CardNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn compute_nullifier_without_context(self) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. diff --git a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr index 22144f965af..62a22e438d1 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr @@ -6,7 +6,7 @@ use dep::aztec::prelude::{ use dep::aztec::{ note::utils::compute_note_hash_for_consumption, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - hash::pedersen_hash + hash::poseidon2_hash, protocol_types::constants::GENERATOR_INDEX__NULLIFIER, }; global ECDSA_PUBLIC_KEY_NOTE_LEN: Field = 5; @@ -67,23 +67,23 @@ impl NoteInterface for EcdsaPublicKeyNote { } fn compute_nullifier(self, context: &mut PrivateContext) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn compute_nullifier_without_context(self) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. diff --git a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr index 39d25636db7..66c1b77ac26 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr @@ -1,7 +1,8 @@ use dep::aztec::prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}; use dep::aztec::{ - note::utils::compute_note_hash_for_consumption, hash::pedersen_hash, - oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} + note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, + oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER, }; global PUBLIC_KEY_NOTE_LEN: Field = 3; @@ -17,23 +18,23 @@ struct PublicKeyNote { impl NoteInterface for PublicKeyNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn compute_nullifier_without_context(self) -> Field { - let unique_siloed_note_hash = compute_note_hash_for_consumption(self); + let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ - unique_siloed_note_hash, + poseidon2_hash([ + note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr index 3bd6b23d854..3ac3081ba77 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr @@ -1,6 +1,9 @@ -use dep::aztec::prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}; -use dep::aztec::{note::utils::compute_note_hash_for_consumption, hash::pedersen_hash}; -use dep::aztec::oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}; +use dep::aztec::{ + prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, + oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, +}; trait OwnedNote { fn new(amount: U128, owner: AztecAddress) -> Self; @@ -27,22 +30,22 @@ impl NoteInterface for TokenNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // docs:end:nullifier fn compute_nullifier_without_context(self) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index 3e722a207f8..9b4eb9ef23a 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -1,8 +1,8 @@ // docs:start:token_types_all -use dep::aztec::prelude::{NoteHeader, NoteInterface, PrivateContext}; use dep::aztec::{ note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption}, - hash::{compute_secret_hash, pedersen_hash} + hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext}, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER }; global TRANSPARENT_NOTE_LEN: Field = 2; @@ -52,9 +52,11 @@ impl NoteInterface for TransparentNote { // circuit. // This achieves that the note can only be spent by the party that knows the secret. fn compute_nullifier_without_context(self) -> Field { - let siloed_note_hash = compute_note_hash_for_consumption(self); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([siloed_note_hash], 0) + let note_hash_for_nullify = compute_note_hash_for_consumption(self); + poseidon2_hash([ + note_hash_for_nullify, + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn broadcast(self, context: &mut PrivateContext, slot: Field) { diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr index cd76d49659c..3ac3081ba77 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr @@ -1,9 +1,9 @@ -use dep::aztec::prelude::{ - AztecAddress, NoteInterface, NoteGetterOptions, NoteViewerOptions, NoteHeader, PrivateContext, - PrivateSet, Map, emit_encrypted_log +use dep::aztec::{ + prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, + oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, }; -use dep::aztec::{note::utils::compute_note_hash_for_consumption, hash::pedersen_hash}; -use dep::aztec::oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}; trait OwnedNote { fn new(amount: U128, owner: AztecAddress) -> Self; @@ -30,22 +30,22 @@ impl NoteInterface for TokenNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = context.request_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // docs:end:nullifier fn compute_nullifier_without_context(self) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); let secret = get_app_nullifier_secret_key(self.owner); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([ + poseidon2_hash([ note_hash_for_nullify, secret, - ],0) + GENERATOR_INDEX__NULLIFIER as Field, + ]) } // Broadcasts the note as an encrypted log on L1. @@ -63,7 +63,7 @@ impl NoteInterface for TokenNote { ); } } - } +} impl OwnedNote for TokenNote { fn new(amount: U128, owner: AztecAddress) -> Self { @@ -82,5 +82,5 @@ impl OwnedNote for TokenNote { fn get_owner(self) -> AztecAddress { self.owner } - + } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index 3e722a207f8..9b4eb9ef23a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -1,8 +1,8 @@ // docs:start:token_types_all -use dep::aztec::prelude::{NoteHeader, NoteInterface, PrivateContext}; use dep::aztec::{ note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption}, - hash::{compute_secret_hash, pedersen_hash} + hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext}, + protocol_types::constants::GENERATOR_INDEX__NULLIFIER }; global TRANSPARENT_NOTE_LEN: Field = 2; @@ -52,9 +52,11 @@ impl NoteInterface for TransparentNote { // circuit. // This achieves that the note can only be spent by the party that knows the secret. fn compute_nullifier_without_context(self) -> Field { - let siloed_note_hash = compute_note_hash_for_consumption(self); - // TODO(#1205) Should use a non-zero generator index. - pedersen_hash([siloed_note_hash], 0) + let note_hash_for_nullify = compute_note_hash_for_consumption(self); + poseidon2_hash([ + note_hash_for_nullify, + GENERATOR_INDEX__NULLIFIER as Field, + ]) } fn broadcast(self, context: &mut PrivateContext, slot: Field) { From cb794d4c717ad67bbf9e0b93933e249b29702039 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 12:06:00 +0000 Subject: [PATCH 04/15] fix --- .../simulator/src/client/private_execution.test.ts | 9 ++++++--- yarn-project/simulator/src/client/simulator.test.ts | 7 +++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 7156422a39b..a72fb637a09 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -5,6 +5,7 @@ import { CompleteAddress, FunctionData, GasSettings, + GeneratorIndex, type GrumpkinPrivateKey, Header, L1_TO_L2_MSG_TREE_HEIGHT, @@ -32,7 +33,7 @@ import { import { asyncMap } from '@aztec/foundation/async-map'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { times } from '@aztec/foundation/collection'; -import { pedersenHash, randomInt } from '@aztec/foundation/crypto'; +import { pedersenHash, poseidon2Hash, randomInt } from '@aztec/foundation/crypto'; import { EthAddress } from '@aztec/foundation/eth-address'; import { Fr } from '@aztec/foundation/fields'; import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log'; @@ -878,9 +879,10 @@ describe('Private Execution test suite', () => { expect(result.returnValues).toEqual([new Fr(amountToTransfer)]); const nullifier = result.callStackItem.publicInputs.newNullifiers[0]; - const expectedNullifier = pedersenHash([ + const expectedNullifier = poseidon2Hash([ innerNoteHash, computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), + GeneratorIndex.NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); }); @@ -946,9 +948,10 @@ describe('Private Execution test suite', () => { expect(execGetThenNullify.returnValues).toEqual([new Fr(amountToTransfer)]); const nullifier = execGetThenNullify.callStackItem.publicInputs.newNullifiers[0]; - const expectedNullifier = pedersenHash([ + const expectedNullifier = poseidon2Hash([ innerNoteHash, computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), + GeneratorIndex.NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); }); diff --git a/yarn-project/simulator/src/client/simulator.test.ts b/yarn-project/simulator/src/client/simulator.test.ts index c22244d2881..4e18033ec6b 100644 --- a/yarn-project/simulator/src/client/simulator.test.ts +++ b/yarn-project/simulator/src/client/simulator.test.ts @@ -1,5 +1,5 @@ import { type AztecNode, CompleteAddress, Note } from '@aztec/circuit-types'; -import { computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js'; +import { GeneratorIndex, computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js'; import { computeUniqueCommitment, siloNoteHash } from '@aztec/circuits.js/hash'; import { ABIParameterVisibility, @@ -7,7 +7,7 @@ import { getFunctionArtifact, } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { pedersenHash } from '@aztec/foundation/crypto'; +import { pedersenHash, poseidon2Hash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token'; @@ -67,8 +67,7 @@ describe('Simulator', () => { const innerNoteHash = pedersenHash([storageSlot, tokenNoteHash]); const siloedNoteHash = siloNoteHash(contractAddress, innerNoteHash); const uniqueSiloedNoteHash = computeUniqueCommitment(nonce, siloedNoteHash); - // TODO(#5832): all the pedersen hashes in notes should be replaced with poseidon2 - const innerNullifier = pedersenHash([uniqueSiloedNoteHash, appNullifierSecretKey]); + const innerNullifier = poseidon2Hash([uniqueSiloedNoteHash, appNullifierSecretKey, GeneratorIndex.NULLIFIER]); const result = await simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, noteTypeId, note); From d913c815ba7112f1412170c6b4769dd5d6fc8934 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 15:09:38 +0000 Subject: [PATCH 05/15] WIP --- noir-projects/aztec-nr/address-note/src/address_note.nr | 8 ++++---- noir-projects/aztec-nr/aztec/src/hash.nr | 4 ++-- noir-projects/aztec-nr/value-note/src/value_note.nr | 6 +++--- .../app_subscription_contract/src/subscription_note.nr | 6 +++--- .../docs_example_contract/src/types/card_note.nr | 6 +++--- .../ecdsa_account_contract/src/ecdsa_public_key_note.nr | 6 +++--- .../schnorr_account_contract/src/public_key_note.nr | 6 +++--- .../token_blacklist_contract/src/types/token_note.nr | 6 +++--- .../src/types/transparent_note.nr | 4 ++-- .../contracts/token_contract/src/types/token_note.nr | 6 +++--- .../token_contract/src/types/transparent_note.nr | 4 ++-- .../noir-protocol-circuits/crates/types/src/constants.nr | 3 ++- yarn-project/circuits.js/src/constants.gen.ts | 3 ++- yarn-project/circuits.js/src/hash/hash.ts | 2 +- .../simulator/src/client/private_execution.test.ts | 4 ++-- yarn-project/simulator/src/client/simulator.test.ts | 6 +++++- 16 files changed, 43 insertions(+), 37 deletions(-) diff --git a/noir-projects/aztec-nr/address-note/src/address_note.nr b/noir-projects/aztec-nr/address-note/src/address_note.nr index c940d8b404a..982b9ba0053 100644 --- a/noir-projects/aztec-nr/address-note/src/address_note.nr +++ b/noir-projects/aztec-nr/address-note/src/address_note.nr @@ -2,7 +2,7 @@ use dep::aztec::log::emit_encrypted_log; // docs:end:encrypted_import use dep::aztec::{ - protocol_types::{address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NULLIFIER}, + protocol_types::{address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER}, note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, context::PrivateContext, hash::poseidon2_hash @@ -19,7 +19,7 @@ struct AddressNote { randomness: Field, } -impl NoteInterface for AddressNote { +impl NoteInterface for AddressNote { fn compute_nullifier(self, context: &mut PrivateContext) -> Field { let note_hash_for_nullify = compute_note_hash_for_consumption(self); @@ -27,7 +27,7 @@ impl NoteInterface for AddressNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -37,7 +37,7 @@ impl NoteInterface for AddressNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 8406cf8c298..4b12c21ea75 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -1,7 +1,7 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{ - GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__NULLIFIER, ARGS_HASH_CHUNK_COUNT, + GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} @@ -45,7 +45,7 @@ pub fn compute_message_hash( pub fn compute_message_nullifier(message_hash: Field, secret: Field, leaf_index: Field) -> Field { pedersen_hash( [message_hash, secret, leaf_index], - GENERATOR_INDEX__NULLIFIER + GENERATOR_INDEX__MESSAGE_NULLIFIER ) } diff --git a/noir-projects/aztec-nr/value-note/src/value_note.nr b/noir-projects/aztec-nr/value-note/src/value_note.nr index 31d0fb7d695..b67cd8a98dc 100644 --- a/noir-projects/aztec-nr/value-note/src/value_note.nr +++ b/noir-projects/aztec-nr/value-note/src/value_note.nr @@ -1,7 +1,7 @@ use dep::aztec::{ protocol_types::{ address::AztecAddress, traits::{Deserialize, Serialize}, - constants::GENERATOR_INDEX__NULLIFIER + constants::GENERATOR_INDEX__NOTE_NULLIFIER }, note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, @@ -28,7 +28,7 @@ impl NoteInterface for ValueNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -40,7 +40,7 @@ impl NoteInterface for ValueNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr index c24e8e6ee74..435f61191b3 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr @@ -1,6 +1,6 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteHeader, emit_encrypted_log, NoteInterface}; use dep::aztec::{ - protocol_types::constants::GENERATOR_INDEX__NULLIFIER, note::utils::compute_note_hash_for_consumption, + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} }; @@ -23,7 +23,7 @@ impl NoteInterface for SubscriptionNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -33,7 +33,7 @@ impl NoteInterface for SubscriptionNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr index c4ecfacc323..efab4af4892 100644 --- a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr +++ b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteInterface, NoteHeader, PrivateContex use dep::aztec::{ note::{utils::compute_note_hash_for_consumption}, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - hash::poseidon2_hash, protocol_types::{traits::Empty, constants::GENERATOR_INDEX__NULLIFIER}, + hash::poseidon2_hash, protocol_types::{traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER}, }; // Shows how to create a custom note @@ -31,7 +31,7 @@ impl NoteInterface for CardNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -41,7 +41,7 @@ impl NoteInterface for CardNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr index 62a22e438d1..b97ff8bad97 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr @@ -6,7 +6,7 @@ use dep::aztec::prelude::{ use dep::aztec::{ note::utils::compute_note_hash_for_consumption, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - hash::poseidon2_hash, protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + hash::poseidon2_hash, protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, }; global ECDSA_PUBLIC_KEY_NOTE_LEN: Field = 5; @@ -72,7 +72,7 @@ impl NoteInterface for EcdsaPublicKeyNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -82,7 +82,7 @@ impl NoteInterface for EcdsaPublicKeyNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr index 66c1b77ac26..c0031833c26 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContex use dep::aztec::{ note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, oracle::{nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, - protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, }; global PUBLIC_KEY_NOTE_LEN: Field = 3; @@ -23,7 +23,7 @@ impl NoteInterface for PublicKeyNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } @@ -33,7 +33,7 @@ impl NoteInterface for PublicKeyNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr index 3ac3081ba77..87ac609218c 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr @@ -1,6 +1,6 @@ use dep::aztec::{ prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, - protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, }; @@ -33,7 +33,7 @@ impl NoteInterface for TokenNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } // docs:end:nullifier @@ -44,7 +44,7 @@ impl NoteInterface for TokenNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index 9b4eb9ef23a..d5cf7197cef 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -2,7 +2,7 @@ use dep::aztec::{ note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption}, hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext}, - protocol_types::constants::GENERATOR_INDEX__NULLIFIER + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER }; global TRANSPARENT_NOTE_LEN: Field = 2; @@ -55,7 +55,7 @@ impl NoteInterface for TransparentNote { let note_hash_for_nullify = compute_note_hash_for_consumption(self); poseidon2_hash([ note_hash_for_nullify, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr index 3ac3081ba77..87ac609218c 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr @@ -1,6 +1,6 @@ use dep::aztec::{ prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, - protocol_types::constants::GENERATOR_INDEX__NULLIFIER, + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, }; @@ -33,7 +33,7 @@ impl NoteInterface for TokenNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } // docs:end:nullifier @@ -44,7 +44,7 @@ impl NoteInterface for TokenNote { poseidon2_hash([ note_hash_for_nullify, secret, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index 9b4eb9ef23a..d5cf7197cef 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -2,7 +2,7 @@ use dep::aztec::{ note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption}, hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext}, - protocol_types::constants::GENERATOR_INDEX__NULLIFIER + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER }; global TRANSPARENT_NOTE_LEN: Field = 2; @@ -55,7 +55,7 @@ impl NoteInterface for TransparentNote { let note_hash_for_nullify = compute_note_hash_for_consumption(self); poseidon2_hash([ note_hash_for_nullify, - GENERATOR_INDEX__NULLIFIER as Field, + GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index e324d2d231d..5dc288e472e 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -199,7 +199,7 @@ global GENERATOR_INDEX__NOTE_HASH = 1; global GENERATOR_INDEX__NOTE_HASH_NONCE = 2; global GENERATOR_INDEX__UNIQUE_NOTE_HASH = 3; global GENERATOR_INDEX__SILOED_NOTE_HASH = 4; -global GENERATOR_INDEX__NULLIFIER = 5; +global GENERATOR_INDEX__MESSAGE_NULLIFIER = 5; global GENERATOR_INDEX__INITIALIZATION_NULLIFIER = 6; global GENERATOR_INDEX__OUTER_NULLIFIER = 7; global GENERATOR_INDEX__PUBLIC_DATA_READ = 8; @@ -242,3 +242,4 @@ global GENERATOR_INDEX__IVSK_M = 48; global GENERATOR_INDEX__OVSK_M = 49; global GENERATOR_INDEX__TSK_M = 50; global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51; +global GENERATOR_INDEX__NOTE_NULLIFIER = 52; diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index 1ebe97036c4..aa18be81a9c 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -163,7 +163,7 @@ export enum GeneratorIndex { NOTE_HASH_NONCE = 2, UNIQUE_NOTE_HASH = 3, SILOED_NOTE_HASH = 4, - NULLIFIER = 5, + MESSAGE_NULLIFIER = 5, INITIALIZATION_NULLIFIER = 6, OUTER_NULLIFIER = 7, PUBLIC_DATA_READ = 8, @@ -202,4 +202,5 @@ export enum GeneratorIndex { OVSK_M = 49, TSK_M = 50, PUBLIC_KEYS_HASH = 51, + NOTE_NULLIFIER = 52, } diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index b5496f75713..f06cbdbec56 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -171,6 +171,6 @@ export function computeL1ToL2MessageNullifier( secret: Fr, messageIndex: bigint, ) { - const innerMessageNullifier = pedersenHash([messageHash, secret, messageIndex], GeneratorIndex.NULLIFIER); + const innerMessageNullifier = pedersenHash([messageHash, secret, messageIndex], GeneratorIndex.MESSAGE_NULLIFIER); return siloNullifier(contract, innerMessageNullifier); } diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index a72fb637a09..0cd345aa9e0 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -882,7 +882,7 @@ describe('Private Execution test suite', () => { const expectedNullifier = poseidon2Hash([ innerNoteHash, computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), - GeneratorIndex.NULLIFIER, + GeneratorIndex.NOTE_NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); }); @@ -951,7 +951,7 @@ describe('Private Execution test suite', () => { const expectedNullifier = poseidon2Hash([ innerNoteHash, computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), - GeneratorIndex.NULLIFIER, + GeneratorIndex.NOTE_NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); }); diff --git a/yarn-project/simulator/src/client/simulator.test.ts b/yarn-project/simulator/src/client/simulator.test.ts index 4e18033ec6b..87ca2ef7fc4 100644 --- a/yarn-project/simulator/src/client/simulator.test.ts +++ b/yarn-project/simulator/src/client/simulator.test.ts @@ -67,7 +67,11 @@ describe('Simulator', () => { const innerNoteHash = pedersenHash([storageSlot, tokenNoteHash]); const siloedNoteHash = siloNoteHash(contractAddress, innerNoteHash); const uniqueSiloedNoteHash = computeUniqueCommitment(nonce, siloedNoteHash); - const innerNullifier = poseidon2Hash([uniqueSiloedNoteHash, appNullifierSecretKey, GeneratorIndex.NULLIFIER]); + const innerNullifier = poseidon2Hash([ + uniqueSiloedNoteHash, + appNullifierSecretKey, + GeneratorIndex.NOTE_NULLIFIER, + ]); const result = await simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, noteTypeId, note); From 0788323fdd265e5ddd0bddec72fb5e16d714cee7 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 15:25:47 +0000 Subject: [PATCH 06/15] secret_hash funcs cleanup --- noir-projects/aztec-nr/aztec/src/context/avm_context.nr | 4 ++-- noir-projects/aztec-nr/aztec/src/hash.nr | 9 ++++++--- noir-projects/aztec-nr/aztec/src/messaging.nr | 4 ++-- .../contracts/gas_token_contract/src/main.nr | 2 +- .../noir-contracts/contracts/test_contract/src/main.nr | 4 ++-- .../contracts/token_blacklist_contract/src/main.nr | 8 ++++---- .../contracts/token_bridge_contract/src/main.nr | 2 -- .../noir-contracts/contracts/token_contract/src/main.nr | 4 ++-- .../noir-protocol-circuits/crates/types/src/constants.nr | 1 + yarn-project/circuits.js/src/constants.gen.ts | 1 + 10 files changed, 21 insertions(+), 18 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr index 16d7de0223b..83139c09d4b 100644 --- a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr @@ -1,4 +1,4 @@ -use crate::hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}; +use crate::hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}; use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{L1_TO_L2_MESSAGE_LENGTH, NESTED_CALL_L2_GAS_BUFFER}, header::Header @@ -96,7 +96,7 @@ impl PublicContextInterface for AvmContext { } fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field) { - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_message_secret_hash(secret); let message_hash = compute_message_hash( sender, self.chain_id(), diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 4b12c21ea75..ecd81832764 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -2,16 +2,19 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{ GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, - GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH + GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, GENERATOR_INDEX__NOTE_SECRET_HASH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; -pub fn compute_secret_hash(secret: Field) -> Field { - // TODO(#1205) This is probably not the right index to use +pub fn compute_message_secret_hash(secret: Field) -> Field { pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET) } +pub fn compute_note_secret_hash(secret: Field) -> Field { + pedersen_hash([secret], GENERATOR_INDEX__NOTE_SECRET_HASH) +} + pub fn compute_message_hash( sender: EthAddress, chain_id: Field, diff --git a/noir-projects/aztec-nr/aztec/src/messaging.nr b/noir-projects/aztec-nr/aztec/src/messaging.nr index 77087758f82..c4b197060eb 100644 --- a/noir-projects/aztec-nr/aztec/src/messaging.nr +++ b/noir-projects/aztec-nr/aztec/src/messaging.nr @@ -1,5 +1,5 @@ use crate::{ - hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}, + hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}, oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness }; @@ -15,7 +15,7 @@ pub fn process_l1_to_l2_message( content: Field, secret: Field ) -> Field { - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_message_secret_hash(secret); let message_hash = compute_message_hash( portal_contract_address, chain_id, diff --git a/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr index cc19bf9f5f2..fd2bb0356ec 100644 --- a/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr @@ -2,7 +2,7 @@ mod lib; contract GasToken { use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}}; - use dep::aztec::{hash::compute_secret_hash, state_vars::{SharedImmutable, PublicMutable, Map}}; + use dep::aztec::state_vars::{SharedImmutable, PublicMutable, Map}; use crate::lib::{calculate_fee, get_bridge_gas_msg_hash}; diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index f7a40cf41a8..dcae1e54e27 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -19,7 +19,7 @@ contract Test { use dep::aztec::{ context::{Context, inputs::private_context_inputs::PrivateContextInputs}, - hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, + hash::{pedersen_hash, compute_note_secret_hash, ArgsHasher}, note::{ lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus @@ -351,7 +351,7 @@ contract Test { // Adapted from TokenContract#redeem_shield but without an initcheck so it can be run in simulator/src/client/private_execution.test.ts fn consume_note_from_secret(secret: Field) { let notes_set = storage.example_set; - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); let mut options = NoteGetterOptions::new(); options = options.select(TestNote::properties().value, secret_hash, Option::none()).set_limit(1); let notes = notes_set.get_notes(options); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index a6572b4e54c..6258677ea01 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -13,9 +13,9 @@ contract TokenBlacklist { use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; use dep::aztec::{ - note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, - hash::compute_secret_hash, - state_vars::{Map, PublicMutable, PrivateSet, SharedMutable, SharedImmutable} + note::note_getter_options::NoteGetterOptions, + hash::compute_note_secret_hash, + state_vars::{Map, PublicMutable, PrivateSet, SharedMutable} }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -152,7 +152,7 @@ contract TokenBlacklist { assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); let pending_shields = storage.pending_shields; - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr index 7cb053fb512..e503e3f691a 100644 --- a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr @@ -8,8 +8,6 @@ contract TokenBridge { use dep::aztec::prelude::{FunctionSelector, AztecAddress, EthAddress, PublicMutable, SharedImmutable}; - use dep::aztec::{context::Context, hash::compute_secret_hash}; - use dep::token_portal_content_hash_lib::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash}; use dep::token::Token; diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index cc892421ece..b98b96bec8c 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -15,7 +15,7 @@ contract Token { use dep::compressed_string::FieldCompressedString; use dep::aztec::prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}; - use dep::aztec::hash::compute_secret_hash; + use dep::aztec::hash::compute_note_secret_hash; // docs:start:import_authwit use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -239,7 +239,7 @@ contract Token { #[aztec(private)] fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) { let pending_shields = storage.pending_shields; - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 5dc288e472e..6d710c69e7b 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -243,3 +243,4 @@ global GENERATOR_INDEX__OVSK_M = 49; global GENERATOR_INDEX__TSK_M = 50; global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51; global GENERATOR_INDEX__NOTE_NULLIFIER = 52; +global GENERATOR_INDEX__NOTE_SECRET_HASH = 53; diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index aa18be81a9c..bf16bd2b4d5 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -203,4 +203,5 @@ export enum GeneratorIndex { TSK_M = 50, PUBLIC_KEYS_HASH = 51, NOTE_NULLIFIER = 52, + NOTE_SECRET_HASH = 53, } From 5d4907d33ebca421109b4393d180c5b9a61010a0 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 16:01:52 +0000 Subject: [PATCH 07/15] wip --- noir-projects/aztec-nr/aztec/src/hash.nr | 6 +----- .../contracts/test_contract/src/main.nr | 4 ++-- .../contracts/token_blacklist_contract/src/main.nr | 6 +++--- .../src/types/transparent_note.nr | 2 ++ .../contracts/token_contract/src/main.nr | 13 +++++++++---- .../token_contract/src/types/transparent_note.nr | 2 ++ .../crates/types/src/constants.nr | 1 - yarn-project/circuits.js/src/hash/hash.ts | 10 +++++----- .../simulator/src/client/private_execution.test.ts | 7 +++++-- 9 files changed, 29 insertions(+), 22 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index ecd81832764..bfeb3ff5392 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -2,7 +2,7 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{ GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, - GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, GENERATOR_INDEX__NOTE_SECRET_HASH + GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; @@ -11,10 +11,6 @@ pub fn compute_message_secret_hash(secret: Field) -> Field { pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET) } -pub fn compute_note_secret_hash(secret: Field) -> Field { - pedersen_hash([secret], GENERATOR_INDEX__NOTE_SECRET_HASH) -} - pub fn compute_message_hash( sender: EthAddress, chain_id: Field, diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index dcae1e54e27..5f9bc335646 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -19,7 +19,7 @@ contract Test { use dep::aztec::{ context::{Context, inputs::private_context_inputs::PrivateContextInputs}, - hash::{pedersen_hash, compute_note_secret_hash, ArgsHasher}, + hash::{pedersen_hash, poseidon2_hash, ArgsHasher}, note::{ lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus @@ -351,7 +351,7 @@ contract Test { // Adapted from TokenContract#redeem_shield but without an initcheck so it can be run in simulator/src/client/private_execution.test.ts fn consume_note_from_secret(secret: Field) { let notes_set = storage.example_set; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, 92543]); // global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; let mut options = NoteGetterOptions::new(); options = options.select(TestNote::properties().value, secret_hash, Option::none()).set_limit(1); let notes = notes_set.get_notes(options); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index 6258677ea01..52b3c0b93ea 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -13,9 +13,9 @@ contract TokenBlacklist { use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; use dep::aztec::{ - note::note_getter_options::NoteGetterOptions, + note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, hash::compute_note_secret_hash, - state_vars::{Map, PublicMutable, PrivateSet, SharedMutable} + state_vars::{Map, PublicMutable, PrivateSet, SharedMutable, SharedImmutable} }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -255,4 +255,4 @@ contract TokenBlacklist { unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field { storage.public_balances.at(owner).read().to_field() } -} +} \ No newline at end of file diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index d5cf7197cef..6f06d084a51 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -6,6 +6,8 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; +// Defined here as it's not a protocol constant. Copied over to private execution test. +global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index b98b96bec8c..7c4087a6986 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -14,14 +14,19 @@ contract Token { use dep::compressed_string::FieldCompressedString; - use dep::aztec::prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}; - use dep::aztec::hash::compute_note_secret_hash; + use dep::aztec::{ + hash::poseidon2_hash, + prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress} + }; // docs:start:import_authwit use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; // docs:end:import_authwit - use crate::types::{transparent_note::TransparentNote, token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap}; + use crate::types::{ + transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, + token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap + }; // docs:end::imports // docs:start:storage_struct @@ -239,7 +244,7 @@ contract Token { #[aztec(private)] fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) { let pending_shields = storage.pending_shields; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index d5cf7197cef..19b5025eacd 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -6,6 +6,8 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; +// Defined here as it's not a protocol constant. Copied over to private execution test and test contract. +global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 6d710c69e7b..5dc288e472e 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -243,4 +243,3 @@ global GENERATOR_INDEX__OVSK_M = 49; global GENERATOR_INDEX__TSK_M = 50; global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51; global GENERATOR_INDEX__NOTE_NULLIFIER = 52; -global GENERATOR_INDEX__NOTE_SECRET_HASH = 53; diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index f06cbdbec56..a011300861e 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -157,12 +157,12 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) { } /** - * 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 + * Computes a hash of a secret as is done in the L1 to L2 message flow. + * @param secret - The secret to hash (could be generated however you want e.g. `Fr.random()`) + * @returns The hash */ -export function computeMessageSecretHash(secretMessage: Fr) { - return pedersenHash([secretMessage], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET); +export function computeMessageSecretHash(secret: Fr) { + return pedersenHash([secret], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET); } export function computeL1ToL2MessageNullifier( diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 0cd345aa9e0..10cdff0172f 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -21,7 +21,7 @@ import { nonEmptySideEffects, sideEffectArrayToValueArray, } from '@aztec/circuits.js'; -import { computeCommitmentNonce, computeMessageSecretHash, computeVarArgsHash } from '@aztec/circuits.js/hash'; +import { computeCommitmentNonce, computeVarArgsHash } from '@aztec/circuits.js/hash'; import { makeHeader } from '@aztec/circuits.js/testing'; import { type FunctionArtifact, @@ -62,6 +62,9 @@ import { AcirSimulator } from './simulator.js'; jest.setTimeout(60_000); +// Copied over from `transparent_note.nr` - not placed in constants.nr as it's not a protocol constant +const GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; + describe('Private Execution test suite', () => { let oracle: MockProxy; let node: MockProxy; @@ -727,7 +730,7 @@ describe('Private Execution test suite', () => { it('Should be able to consume a dummy public to private message', async () => { const artifact = getFunctionArtifact(TestContractArtifact, 'consume_note_from_secret'); const secret = new Fr(1n); - const secretHash = computeMessageSecretHash(secret); + const secretHash = poseidon2Hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); const note = new Note([secretHash]); const storageSlot = new Fr(5); oracle.getNotes.mockResolvedValue([ From ef69c9ae2947a4d8b45ecb1a2c9dd7271b0ed878 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 06:48:51 +0000 Subject: [PATCH 08/15] deps cleanup + updating prelude with SharedMutable --- noir-projects/aztec-nr/aztec/src/prelude.nr | 2 +- .../token_blacklist_contract/src/main.nr | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/prelude.nr b/noir-projects/aztec-nr/aztec/src/prelude.nr index 08177f65e7d..8a9809fa2b1 100644 --- a/noir-projects/aztec-nr/aztec/src/prelude.nr +++ b/noir-projects/aztec-nr/aztec/src/prelude.nr @@ -7,7 +7,7 @@ use crate::{ state_vars::{ map::Map, private_immutable::PrivateImmutable, private_mutable::PrivateMutable, public_immutable::PublicImmutable, public_mutable::PublicMutable, private_set::PrivateSet, - shared_immutable::SharedImmutable, storage::Storable + shared_immutable::SharedImmutable, shared_mutable::SharedMutable, storage::Storable }, log::emit_encrypted_log, context::{PrivateContext, PackedReturns, FunctionReturns}, note::{ diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index 52b3c0b93ea..d5d232aa535 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -10,17 +10,17 @@ mod types; contract TokenBlacklist { // Libs - - use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; use dep::aztec::{ - note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, - hash::compute_note_secret_hash, - state_vars::{Map, PublicMutable, PrivateSet, SharedMutable, SharedImmutable} + hash::poseidon2_hash, + prelude::{AztecAddress, FunctionSelector, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable} }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; - use crate::types::{transparent_note::TransparentNote, token_note::TokenNote, balances_map::BalancesMap, roles::UserFlags}; + use crate::types::{ + transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, token_note::TokenNote, + balances_map::BalancesMap, roles::UserFlags + }; // Changing an address' roles has a certain block delay before it goes into effect. global CHANGE_ROLES_DELAY_BLOCKS = 5; @@ -152,7 +152,7 @@ contract TokenBlacklist { assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); let pending_shields = storage.pending_shields; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); @@ -255,4 +255,4 @@ contract TokenBlacklist { unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field { storage.public_balances.at(owner).read().to_field() } -} \ No newline at end of file +} From 76d23dc3b971520b01442eb62a8dfdf98e855252 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 07:04:54 +0000 Subject: [PATCH 09/15] constants update --- yarn-project/circuits.js/src/constants.gen.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index bf16bd2b4d5..aa18be81a9c 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -203,5 +203,4 @@ export enum GeneratorIndex { TSK_M = 50, PUBLIC_KEYS_HASH = 51, NOTE_NULLIFIER = 52, - NOTE_SECRET_HASH = 53, } From 1024604cc4e1bfbfbe5da93e421ef08b93026707 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 07:52:51 +0000 Subject: [PATCH 10/15] renaming compute_message_secret_hash as compute_message_secret --- .../references/portals/data_structures.md | 2 +- .../contracts/references/portals/inbox.md | 2 +- .../portals/communicate_with_portal.md | 4 ++-- .../developers/tutorials/writing_dapp/testing.md | 2 +- .../aztec-nr/aztec/src/context/avm_context.nr | 4 ++-- noir-projects/aztec-nr/aztec/src/hash.nr | 6 +++--- noir-projects/aztec-nr/aztec/src/messaging.nr | 4 ++-- .../token_blacklist_contract/src/main.nr | 9 +++------ .../src/types/transparent_note.nr | 2 -- .../contracts/token_contract/src/main.nr | 9 +++------ .../token_contract/src/types/transparent_note.nr | 2 -- .../crates/types/src/constants.nr | 2 +- .../src/fee/private_fee_payment_method.ts | 4 ++-- yarn-project/aztec.js/src/index.ts | 2 +- yarn-project/aztec/src/examples/token.ts | 4 ++-- yarn-project/circuits.js/src/constants.gen.ts | 2 +- yarn-project/circuits.js/src/hash/hash.test.ts | 4 ++-- yarn-project/circuits.js/src/hash/hash.ts | 7 ++++--- .../src/composed/e2e_persistence.test.ts | 14 +++++++------- .../src/composed/e2e_sandbox_example.test.ts | 6 +++--- .../end-to-end/src/e2e_account_init_fees.test.ts | 6 +++--- .../src/e2e_blacklist_token_contract.test.ts | 6 +++--- .../end-to-end/src/e2e_cheat_codes.test.ts | 4 ++-- .../src/e2e_crowdfunding_and_claim.test.ts | 4 ++-- .../end-to-end/src/e2e_escrow_contract.test.ts | 6 +++--- yarn-project/end-to-end/src/e2e_fees.test.ts | 16 ++++++++-------- .../end-to-end/src/e2e_lending_contract.test.ts | 4 ++-- .../src/e2e_multiple_accounts_1_enc_key.test.ts | 4 ++-- .../src/e2e_public_cross_chain_messaging.test.ts | 4 ++-- .../src/e2e_token_contract/minting.test.ts | 4 ++-- .../src/e2e_token_contract/shielding.test.ts | 4 ++-- .../e2e_token_contract/token_contract_test.ts | 4 ++-- .../end-to-end/src/flakey_e2e_2_pxes.test.ts | 4 ++-- .../end-to-end/src/guides/dapp_testing.test.ts | 8 ++++---- .../guides/writing_an_account_contract.test.ts | 4 ++-- .../end-to-end/src/sample-dapp/index.mjs | 4 ++-- .../end-to-end/src/sample-dapp/index.test.mjs | 4 ++-- yarn-project/end-to-end/src/shared/browser.ts | 4 ++-- .../src/shared/cross_chain_test_harness.ts | 9 +++++++-- .../src/shared/gas_portal_test_harness.ts | 4 ++-- yarn-project/simulator/src/test/utils.ts | 4 ++-- 41 files changed, 99 insertions(+), 103 deletions(-) diff --git a/docs/docs/developers/contracts/references/portals/data_structures.md b/docs/docs/developers/contracts/references/portals/data_structures.md index ba97bfd5272..4b639c24fb5 100644 --- a/docs/docs/developers/contracts/references/portals/data_structures.md +++ b/docs/docs/developers/contracts/references/portals/data_structures.md @@ -40,7 +40,7 @@ A message that is sent from L1 to L2. | `sender` | `L1Actor` | The actor on L1 that is sending the message. | | `recipient` | `L2Actor` | The actor on L2 that is to receive the message. | | `content` | `field (~254 bits)` | The field element containing the content to be sent to L2. | -| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | ## `L2ToL1Message` diff --git a/docs/docs/developers/contracts/references/portals/inbox.md b/docs/docs/developers/contracts/references/portals/inbox.md index 73b18fc8888..4f0eae50aed 100644 --- a/docs/docs/developers/contracts/references/portals/inbox.md +++ b/docs/docs/developers/contracts/references/portals/inbox.md @@ -17,7 +17,7 @@ Sends a message from L1 to L2. | -------------- | ------- | ----------- | | Recipient | `L2Actor` | The recipient of the message. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. | | Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field for rollup purposes. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions) | -| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | | ReturnValue | `bytes32` | The message hash, used as an identifier | #### Edge cases diff --git a/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md b/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md index 8d559abe6f0..427020830dd 100644 --- a/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md +++ b/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md @@ -17,7 +17,7 @@ When sending messages, we need to specify quite a bit of information beyond just | Name | Type | Description | | ----------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Recipient | `L2Actor` | The message recipient. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. | -| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | | Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions) With all that information at hand, we can call the `sendL2Message` function on the Inbox. The function will return a `field` (inside `bytes32`) that is the hash of the message. This hash can be used as an identifier to spot when your message has been included in a rollup block. @@ -56,7 +56,7 @@ In Solidity, you can use our `Hash.sha256ToField()` method: #include_code deposit_public l1-contracts/test/portals/TokenPortal.sol solidity -The `secret_hash` uses the pederson hash which fits in a field element. You can use the utility method `computeMessageSecretHash()`in `@aztec/aztec.js` npm package to generate a secret and its corresponding hash. +The `secret_hash` uses the pederson hash which fits in a field element. You can use the utility method `computeSecretHash()`in `@aztec/aztec.js` npm package to generate a secret and its corresponding hash. After the transaction has been mined, the message is consumed, a nullifier is emitted and the tokens have been minted on Aztec and are ready for claiming. diff --git a/docs/docs/developers/tutorials/writing_dapp/testing.md b/docs/docs/developers/tutorials/writing_dapp/testing.md index 8aea22e1de0..3917c059625 100644 --- a/docs/docs/developers/tutorials/writing_dapp/testing.md +++ b/docs/docs/developers/tutorials/writing_dapp/testing.md @@ -26,7 +26,7 @@ import { ExtendedNote, Fr, Note, - computeMessageSecretHash, + computeSecretHash, createPXEClient, waitForPXE, } from "@aztec/aztec.js"; diff --git a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr index 83139c09d4b..16d7de0223b 100644 --- a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr @@ -1,4 +1,4 @@ -use crate::hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}; +use crate::hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}; use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{L1_TO_L2_MESSAGE_LENGTH, NESTED_CALL_L2_GAS_BUFFER}, header::Header @@ -96,7 +96,7 @@ impl PublicContextInterface for AvmContext { } fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field) { - let secret_hash = compute_message_secret_hash(secret); + let secret_hash = compute_secret_hash(secret); let message_hash = compute_message_hash( sender, self.chain_id(), diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index bfeb3ff5392..435df049fa2 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -1,14 +1,14 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{ - GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, + GENERATOR_INDEX__SECRET_HASH, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; -pub fn compute_message_secret_hash(secret: Field) -> Field { - pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET) +pub fn compute_secret_hash(secret: Field) -> Field { + pedersen_hash([secret], GENERATOR_INDEX__SECRET_HASH) } pub fn compute_message_hash( diff --git a/noir-projects/aztec-nr/aztec/src/messaging.nr b/noir-projects/aztec-nr/aztec/src/messaging.nr index c4b197060eb..77087758f82 100644 --- a/noir-projects/aztec-nr/aztec/src/messaging.nr +++ b/noir-projects/aztec-nr/aztec/src/messaging.nr @@ -1,5 +1,5 @@ use crate::{ - hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}, + hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}, oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness }; @@ -15,7 +15,7 @@ pub fn process_l1_to_l2_message( content: Field, secret: Field ) -> Field { - let secret_hash = compute_message_secret_hash(secret); + let secret_hash = compute_secret_hash(secret); let message_hash = compute_message_hash( portal_contract_address, chain_id, diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index d5d232aa535..4fc172d63fc 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -11,16 +11,13 @@ mod types; contract TokenBlacklist { // Libs use dep::aztec::{ - hash::poseidon2_hash, + hash::compute_secret_hash, prelude::{AztecAddress, FunctionSelector, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable} }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; - use crate::types::{ - transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, token_note::TokenNote, - balances_map::BalancesMap, roles::UserFlags - }; + use crate::types::{transparent_note::TransparentNote, token_note::TokenNote, balances_map::BalancesMap, roles::UserFlags}; // Changing an address' roles has a certain block delay before it goes into effect. global CHANGE_ROLES_DELAY_BLOCKS = 5; @@ -152,7 +149,7 @@ contract TokenBlacklist { assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); let pending_shields = storage.pending_shields; - let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); + let secret_hash = compute_secret_hash(secret); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index 6f06d084a51..d5cf7197cef 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -6,8 +6,6 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; -// Defined here as it's not a protocol constant. Copied over to private execution test. -global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index 7c4087a6986..e488c5f4f43 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -15,7 +15,7 @@ contract Token { use dep::compressed_string::FieldCompressedString; use dep::aztec::{ - hash::poseidon2_hash, + hash::compute_secret_hash, prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress} }; @@ -23,10 +23,7 @@ contract Token { use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; // docs:end:import_authwit - use crate::types::{ - transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, - token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap - }; + use crate::types::{transparent_note::TransparentNote, token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap}; // docs:end::imports // docs:start:storage_struct @@ -244,7 +241,7 @@ contract Token { #[aztec(private)] fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) { let pending_shields = storage.pending_shields; - let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); + let secret_hash = compute_secret_hash(secret); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index 19b5025eacd..d5cf7197cef 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -6,8 +6,6 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; -// Defined here as it's not a protocol constant. Copied over to private execution test and test contract. -global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 5dc288e472e..ba07e8518b1 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -214,7 +214,7 @@ global GENERATOR_INDEX__CONTRACT_LEAF = 16; global GENERATOR_INDEX__CALL_CONTEXT = 17; global GENERATOR_INDEX__CALL_STACK_ITEM = 18; global GENERATOR_INDEX__CALL_STACK_ITEM_2 = 19; -global GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET = 20; +global GENERATOR_INDEX__SECRET_HASH = 20; global GENERATOR_INDEX__L2_TO_L1_MSG = 21; global GENERATOR_INDEX__TX_CONTEXT = 22; global GENERATOR_INDEX__PUBLIC_LEAF_INDEX = 23; diff --git a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts index e6abb694172..f3298ed09ef 100644 --- a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts @@ -1,6 +1,6 @@ import { type FunctionCall } from '@aztec/circuit-types'; import { FunctionData, type GasSettings } from '@aztec/circuits.js'; -import { computeMessageSecretHash } from '@aztec/circuits.js/hash'; +import { computeSecretHash } from '@aztec/circuits.js/hash'; import { FunctionSelector } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr } from '@aztec/foundation/fields'; @@ -71,7 +71,7 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod { ); await this.wallet.createAuthWit(messageHash); - const secretHashForRebate = computeMessageSecretHash(this.rebateSecret); + const secretHashForRebate = computeSecretHash(this.rebateSecret); return [ { diff --git a/yarn-project/aztec.js/src/index.ts b/yarn-project/aztec.js/src/index.ts index fc91aa829b5..cb9179a84a7 100644 --- a/yarn-project/aztec.js/src/index.ts +++ b/yarn-project/aztec.js/src/index.ts @@ -75,7 +75,7 @@ export { INITIAL_L2_BLOCK_NUM, } from '@aztec/circuits.js'; -export { computeMessageSecretHash } from '@aztec/circuits.js/hash'; +export { computeSecretHash } from '@aztec/circuits.js/hash'; export { computeAppNullifierSecretKey, diff --git a/yarn-project/aztec/src/examples/token.ts b/yarn-project/aztec/src/examples/token.ts index 01702d9d5d1..323d32310ad 100644 --- a/yarn-project/aztec/src/examples/token.ts +++ b/yarn-project/aztec/src/examples/token.ts @@ -1,5 +1,5 @@ import { getSingleKeyAccount } from '@aztec/accounts/single_key'; -import { type AccountWallet, Fr, Note, computeMessageSecretHash, createPXEClient } from '@aztec/aztec.js'; +import { type AccountWallet, Fr, Note, computeSecretHash, createPXEClient } from '@aztec/aztec.js'; import { ExtendedNote } from '@aztec/circuit-types'; import { createDebugLogger } from '@aztec/foundation/log'; import { TokenContract } from '@aztec/noir-contracts.js/Token'; @@ -45,7 +45,7 @@ async function main() { // Create a secret and a corresponding hash that will be used to mint funds privately const aliceSecret = Fr.random(); - const aliceSecretHash = computeMessageSecretHash(aliceSecret); + const aliceSecretHash = computeSecretHash(aliceSecret); const receipt = await tokenAlice.methods.mint_private(ALICE_MINT_BALANCE, aliceSecretHash).send().wait(); // Add the newly created "pending shield" note to PXE diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index aa18be81a9c..c40cdec3ec5 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -178,7 +178,7 @@ export enum GeneratorIndex { CALL_CONTEXT = 17, CALL_STACK_ITEM = 18, CALL_STACK_ITEM_2 = 19, - L1_TO_L2_MESSAGE_SECRET = 20, + SECRET_HASH = 20, L2_TO_L1_MSG = 21, TX_CONTEXT = 22, PUBLIC_LEAF_INDEX = 23, diff --git a/yarn-project/circuits.js/src/hash/hash.test.ts b/yarn-project/circuits.js/src/hash/hash.test.ts index ce91f4c10d7..cb1cd862661 100644 --- a/yarn-project/circuits.js/src/hash/hash.test.ts +++ b/yarn-project/circuits.js/src/hash/hash.test.ts @@ -6,10 +6,10 @@ import { makeAztecAddress, makeVerificationKey } from '../tests/factories.js'; import { computeCommitmentNonce, computeCommitmentsHash, - computeMessageSecretHash, computeNullifierHash, computePublicDataTreeLeafSlot, computePublicDataTreeValue, + computeSecretHash, computeUniqueCommitment, computeVarArgsHash, hashVK, @@ -85,7 +85,7 @@ describe('hash', () => { it('compute secret message hash', () => { const value = new Fr(8n); - const hash = computeMessageSecretHash(value); + const hash = computeSecretHash(value); expect(hash).toMatchSnapshot(); }); diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index a011300861e..94998bf7ee3 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -157,12 +157,13 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) { } /** - * Computes a hash of a secret as is done in the L1 to L2 message flow. + * Computes a hash of a secret. + * @dev This function is used to generate secrets for the L1 to L2 message flow and for the TransparentNote. * @param secret - The secret to hash (could be generated however you want e.g. `Fr.random()`) * @returns The hash */ -export function computeMessageSecretHash(secret: Fr) { - return pedersenHash([secret], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET); +export function computeSecretHash(secret: Fr) { + return pedersenHash([secret], GeneratorIndex.SECRET_HASH); } export function computeL1ToL2MessageNullifier( diff --git a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts index 15bb02259a3..8eafef594ed 100644 --- a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts +++ b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts @@ -5,7 +5,7 @@ import { ExtendedNote, Note, type TxHash, - computeMessageSecretHash, + computeSecretHash, waitForAccountSynch, } from '@aztec/aztec.js'; import { type Salt } from '@aztec/aztec.js/account'; @@ -72,13 +72,13 @@ describe('Aztec persistence', () => { const secret = Fr.random(); - const mintTxReceipt = await contract.methods.mint_private(1000n, computeMessageSecretHash(secret)).send().wait(); + const mintTxReceipt = await contract.methods.mint_private(1000n, computeSecretHash(secret)).send().wait(); await addPendingShieldNoteToPXE( ownerWallet, contractAddress, 1000n, - computeMessageSecretHash(secret), + computeSecretHash(secret), mintTxReceipt.txHash, ); @@ -130,12 +130,12 @@ describe('Aztec persistence', () => { const balance = await contract.methods.balance_of_private(ownerWallet.getAddress()).simulate(); const secret = Fr.random(); - const mintTxReceipt = await contract.methods.mint_private(1000n, computeMessageSecretHash(secret)).send().wait(); + const mintTxReceipt = await contract.methods.mint_private(1000n, computeSecretHash(secret)).send().wait(); await addPendingShieldNoteToPXE( ownerWallet, contractAddress, 1000n, - computeMessageSecretHash(secret), + computeSecretHash(secret), mintTxReceipt.txHash, ); @@ -270,7 +270,7 @@ describe('Aztec persistence', () => { secret = Fr.random(); mintAmount = 1000n; const mintTxReceipt = await contract.methods - .mint_private(mintAmount, computeMessageSecretHash(secret)) + .mint_private(mintAmount, computeSecretHash(secret)) .send() .wait(); mintTxHash = mintTxReceipt.txHash; @@ -311,7 +311,7 @@ describe('Aztec persistence', () => { ownerWallet, contractAddress, mintAmount, - computeMessageSecretHash(secret), + computeSecretHash(secret), mintTxHash, ); diff --git a/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts b/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts index bbc794b8096..1ae3c9362f6 100644 --- a/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts +++ b/yarn-project/end-to-end/src/composed/e2e_sandbox_example.test.ts @@ -7,7 +7,7 @@ import { GrumpkinScalar, Note, type PXE, - computeMessageSecretHash, + computeSecretHash, createDebugLogger, createPXEClient, waitForPXE, @@ -69,7 +69,7 @@ describe('e2e_sandbox_example', () => { // Create a secret and a corresponding hash that will be used to mint funds privately const aliceSecret = Fr.random(); - const aliceSecretHash = computeMessageSecretHash(aliceSecret); + const aliceSecretHash = computeSecretHash(aliceSecret); logger.info(`Minting tokens to Alice...`); // Mint the initial supply privately "to secret hash" @@ -144,7 +144,7 @@ describe('e2e_sandbox_example', () => { await tokenContractAlice.methods.set_minter(bob, true).send().wait(); const bobSecret = Fr.random(); - const bobSecretHash = computeMessageSecretHash(bobSecret); + const bobSecretHash = computeSecretHash(bobSecret); // Bob now has a secret 🥷 const mintQuantity = 10_000n; diff --git a/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts b/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts index d8acec1c0d5..75c225f4d52 100644 --- a/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts +++ b/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts @@ -12,7 +12,7 @@ import { type TxHash, TxStatus, type Wallet, - computeMessageSecretHash, + computeSecretHash, deriveKeys, } from '@aztec/aztec.js'; import { type AztecAddress, CompleteAddress, Fq, GasSettings } from '@aztec/circuits.js'; @@ -173,7 +173,7 @@ describe('e2e_fees_account_init', () => { await bobsAccountManager.register(); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const mintTx = await bananaCoin.methods.mint_private(mintedPrivateBananas, secretHash).send().wait(); await addTransparentNoteToPxe(sequencersAddress, mintedPrivateBananas, secretHash, mintTx.txHash); @@ -219,7 +219,7 @@ describe('e2e_fees_account_init', () => { // the new account should have received a refund await expect( // this rejects if note can't be added - addTransparentNoteToPxe(bobsAddress, maxFee - actualFee, computeMessageSecretHash(rebateSecret), tx.txHash), + addTransparentNoteToPxe(bobsAddress, maxFee - actualFee, computeSecretHash(rebateSecret), tx.txHash), ).resolves.toBeUndefined(); // and it can redeem the refund diff --git a/yarn-project/end-to-end/src/e2e_blacklist_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_blacklist_token_contract.test.ts index a10290d717c..05c2b994a91 100644 --- a/yarn-project/end-to-end/src/e2e_blacklist_token_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_blacklist_token_contract.test.ts @@ -9,7 +9,7 @@ import { type TxHash, type Wallet, computeAuthWitMessageHash, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { TokenBlacklistContract, type TokenContract } from '@aztec/noir-contracts.js'; @@ -246,7 +246,7 @@ describe('e2e_blacklist_token_contract', () => { let txHash: TxHash; beforeAll(() => { - secretHash = computeMessageSecretHash(secret); + secretHash = computeSecretHash(secret); }); describe('Mint flow', () => { @@ -641,7 +641,7 @@ describe('e2e_blacklist_token_contract', () => { let secretHash: Fr; beforeAll(() => { - secretHash = computeMessageSecretHash(secret); + secretHash = computeSecretHash(secret); }); it('on behalf of self', async () => { diff --git a/yarn-project/end-to-end/src/e2e_cheat_codes.test.ts b/yarn-project/end-to-end/src/e2e_cheat_codes.test.ts index 01127055f91..4fd2e8d6d6f 100644 --- a/yarn-project/end-to-end/src/e2e_cheat_codes.test.ts +++ b/yarn-project/end-to-end/src/e2e_cheat_codes.test.ts @@ -7,7 +7,7 @@ import { Note, type PXE, type Wallet, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { RollupAbi } from '@aztec/l1-artifacts'; import { TestContract, TokenContract } from '@aztec/noir-contracts.js'; @@ -218,7 +218,7 @@ describe('e2e_cheat_codes', () => { // docs:start:load_private_cheatcode const mintAmount = 100n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); // docs:start:pxe_add_note diff --git a/yarn-project/end-to-end/src/e2e_crowdfunding_and_claim.test.ts b/yarn-project/end-to-end/src/e2e_crowdfunding_and_claim.test.ts index f697a02251f..ca9f711bf2d 100644 --- a/yarn-project/end-to-end/src/e2e_crowdfunding_and_claim.test.ts +++ b/yarn-project/end-to-end/src/e2e_crowdfunding_and_claim.test.ts @@ -8,7 +8,7 @@ import { Note, type PXE, type TxHash, - computeMessageSecretHash, + computeSecretHash, deriveKeys, } from '@aztec/aztec.js'; import { computePartialAddress } from '@aztec/circuits.js'; @@ -134,7 +134,7 @@ describe('e2e_crowdfunding_and_claim', () => { const mintDNTToDonors = async () => { const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const [txReceipt1, txReceipt2] = await Promise.all([ donationToken.withWallet(operatorWallet).methods.mint_private(1234n, secretHash).send().wait(), diff --git a/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts b/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts index de4f2f48152..12017b84bb8 100644 --- a/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts @@ -7,7 +7,7 @@ import { Fr, Note, type PXE, - computeMessageSecretHash, + computeSecretHash, deriveKeys, } from '@aztec/aztec.js'; import { computePartialAddress } from '@aztec/circuits.js'; @@ -58,7 +58,7 @@ describe('e2e_escrow_contract', () => { const mintAmount = 100n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); @@ -110,7 +110,7 @@ describe('e2e_escrow_contract', () => { logger.info(`Minting funds in token contract to ${owner}`); const mintAmount = 50n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/e2e_fees.test.ts b/yarn-project/end-to-end/src/e2e_fees.test.ts index 1d4ecce5e3d..795fa7719cf 100644 --- a/yarn-project/end-to-end/src/e2e_fees.test.ts +++ b/yarn-project/end-to-end/src/e2e_fees.test.ts @@ -14,7 +14,7 @@ import { TxStatus, type Wallet, computeAuthWitMessageHash, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { FunctionData, GasSettings } from '@aztec/circuits.js'; import { type ContractArtifact, decodeFunctionSignature } from '@aztec/foundation/abi'; @@ -298,7 +298,7 @@ describe('e2e_fees', () => { await expect( // this rejects if note can't be added - addPendingShieldNoteToPXE(0, RefundAmount, computeMessageSecretHash(RefundSecret), tx.txHash), + addPendingShieldNoteToPXE(0, RefundAmount, computeSecretHash(RefundSecret), tx.txHash), ).resolves.toBeUndefined(); }); @@ -361,7 +361,7 @@ describe('e2e_fees', () => { await expect( // this rejects if note can't be added - addPendingShieldNoteToPXE(0, RefundAmount, computeMessageSecretHash(RefundSecret), tx.txHash), + addPendingShieldNoteToPXE(0, RefundAmount, computeSecretHash(RefundSecret), tx.txHash), ).resolves.toBeUndefined(); }); @@ -393,7 +393,7 @@ describe('e2e_fees', () => { */ const shieldedBananas = 1n; const shieldSecret = Fr.random(); - const shieldSecretHash = computeMessageSecretHash(shieldSecret); + const shieldSecretHash = computeSecretHash(shieldSecret); const tx = await bananaCoin.methods .shield(aliceAddress, shieldedBananas, shieldSecretHash, 0n) .send({ @@ -428,7 +428,7 @@ describe('e2e_fees', () => { await expect(addPendingShieldNoteToPXE(0, shieldedBananas, shieldSecretHash, tx.txHash)).resolves.toBeUndefined(); await expect( - addPendingShieldNoteToPXE(0, RefundAmount, computeMessageSecretHash(RefundSecret), tx.txHash), + addPendingShieldNoteToPXE(0, RefundAmount, computeSecretHash(RefundSecret), tx.txHash), ).resolves.toBeUndefined(); }); @@ -436,7 +436,7 @@ describe('e2e_fees', () => { const privateTransfer = 1n; const shieldedBananas = 1n; const shieldSecret = Fr.random(); - const shieldSecretHash = computeMessageSecretHash(shieldSecret); + const shieldSecretHash = computeSecretHash(shieldSecret); /** * PRIVATE SETUP @@ -505,7 +505,7 @@ describe('e2e_fees', () => { await expect(addPendingShieldNoteToPXE(0, shieldedBananas, shieldSecretHash, tx.txHash)).resolves.toBeUndefined(); await expect( - addPendingShieldNoteToPXE(0, RefundAmount, computeMessageSecretHash(RefundSecret), tx.txHash), + addPendingShieldNoteToPXE(0, RefundAmount, computeSecretHash(RefundSecret), tx.txHash), ).resolves.toBeUndefined(); }); @@ -646,7 +646,7 @@ describe('e2e_fees', () => { const mintPrivate = async (amount: bigint, address: AztecAddress) => { // Mint bananas privately const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); logger.debug(`Minting ${amount} bananas privately for ${address} with secret ${secretHash.toString()}`); const receipt = await bananaCoin.methods.mint_private(amount, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/e2e_lending_contract.test.ts b/yarn-project/end-to-end/src/e2e_lending_contract.test.ts index 738887cdd42..2dd4614f80e 100644 --- a/yarn-project/end-to-end/src/e2e_lending_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_lending_contract.test.ts @@ -6,7 +6,7 @@ import { Fr, Note, computeAuthWitMessageHash, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { LendingContract, PriceFeedContract, TokenContract } from '@aztec/noir-contracts.js'; @@ -96,7 +96,7 @@ describe('e2e_lending_contract', () => { const mintAmount = 10000n; for (const asset of assets) { const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const a = asset.methods.mint_public(lendingAccount.address, mintAmount).send(); const b = asset.methods.mint_private(mintAmount, secretHash).send(); diff --git a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts index 599a69d8956..cf239abd44f 100644 --- a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts +++ b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts @@ -10,7 +10,7 @@ import { Note, type PXE, type Wallet, - computeMessageSecretHash, + computeSecretHash, deriveKeys, } from '@aztec/aztec.js'; import { TokenContract } from '@aztec/noir-contracts.js/Token'; @@ -59,7 +59,7 @@ describe('e2e_multiple_accounts_1_enc_key', () => { logger.info(`Token deployed at ${tokenAddress}`); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(initialBalance, secretHash).send().wait(); 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 e8aa0bacee3..6108e156ed3 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 @@ -13,7 +13,7 @@ import { L2Actor, type PXE, computeAuthWitMessageHash, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { sha256ToField } from '@aztec/foundation/crypto'; import { InboxAbi, OutboxAbi } from '@aztec/l1-artifacts'; @@ -341,7 +341,7 @@ describe('e2e_public_cross_chain_messaging', () => { new L1Actor(crossChainTestHarness.ethAccount, crossChainTestHarness.publicClient.chain.id), new L2Actor(testContract.address, 1), Fr.random(), // content - computeMessageSecretHash(secret), // secretHash + computeSecretHash(secret), // secretHash ); await sendL2Message(message); diff --git a/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts index 2fa48998dcb..7a833d6a157 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts @@ -1,4 +1,4 @@ -import { Fr, type TxHash, computeMessageSecretHash } from '@aztec/aztec.js'; +import { Fr, type TxHash, computeSecretHash } from '@aztec/aztec.js'; import { BITSIZE_TOO_BIG_ERROR, U128_OVERFLOW_ERROR } from '../fixtures/fixtures.js'; import { TokenContractTest } from './token_contract_test.js'; @@ -71,7 +71,7 @@ describe('e2e_token_contract minting', () => { let txHash: TxHash; beforeAll(() => { - secretHash = computeMessageSecretHash(secret); + secretHash = computeSecretHash(secret); }); describe('Mint flow', () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/shielding.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/shielding.test.ts index 99fcd3c1336..b0cee961f35 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/shielding.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/shielding.test.ts @@ -1,4 +1,4 @@ -import { Fr, computeMessageSecretHash } from '@aztec/aztec.js'; +import { Fr, computeSecretHash } from '@aztec/aztec.js'; import { U128_UNDERFLOW_ERROR } from '../fixtures/fixtures.js'; import { TokenContractTest } from './token_contract_test.js'; @@ -15,7 +15,7 @@ describe('e2e_token_contract shield + redeem shield', () => { await t.setup(); // Have to destructure again to ensure we have latest refs. ({ asset, accounts, tokenSim, wallets } = t); - secretHash = computeMessageSecretHash(secret); + secretHash = computeSecretHash(secret); }); afterAll(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts index 9e6fb3f110c..6325ead8df7 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts @@ -7,7 +7,7 @@ import { Fr, Note, type TxHash, - computeMessageSecretHash, + computeSecretHash, createDebugLogger, } from '@aztec/aztec.js'; import { DocsExampleContract, TokenContract } from '@aztec/noir-contracts.js'; @@ -141,7 +141,7 @@ export class TokenContractTest { this.logger.verbose(`Minting ${amount} privately...`); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await asset.methods.mint_private(amount, secretHash).send().wait(); await this.addPendingShieldNoteToPXE(0, amount, secretHash, receipt.txHash); diff --git a/yarn-project/end-to-end/src/flakey_e2e_2_pxes.test.ts b/yarn-project/end-to-end/src/flakey_e2e_2_pxes.test.ts index ce6e676d3ad..ea0b055b11a 100644 --- a/yarn-project/end-to-end/src/flakey_e2e_2_pxes.test.ts +++ b/yarn-project/end-to-end/src/flakey_e2e_2_pxes.test.ts @@ -8,7 +8,7 @@ import { Note, type PXE, type Wallet, - computeMessageSecretHash, + computeSecretHash, retryUntil, } from '@aztec/aztec.js'; import { ChildContract, TokenContract } from '@aztec/noir-contracts.js'; @@ -93,7 +93,7 @@ describe('e2e_2_pxes', () => { const mintTokens = async (contract: TokenContract, recipient: AztecAddress, balance: bigint, pxe: PXE) => { const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await contract.methods.mint_private(balance, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index 8cde4b58e96..78267af530f 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -7,7 +7,7 @@ import { Note, type PXE, TxStatus, - computeMessageSecretHash, + computeSecretHash, createPXEClient, waitForPXE, } from '@aztec/aztec.js'; @@ -47,7 +47,7 @@ describe('guides/dapp/testing', () => { const mintAmount = 20n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); const note = new Note([new Fr(mintAmount), secretHash]); @@ -88,7 +88,7 @@ describe('guides/dapp/testing', () => { const recipientAddress = recipient.getAddress(); const mintAmount = 20n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); const note = new Note([new Fr(mintAmount), secretHash]); @@ -150,7 +150,7 @@ describe('guides/dapp/testing', () => { const ownerAddress = owner.getAddress(); const mintAmount = 100n; const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const receipt = await token.methods.mint_private(100n, secretHash).send().wait(); const note = new Note([new Fr(mintAmount), secretHash]); diff --git a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts index d708dc1fc69..2072a7ddda5 100644 --- a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts +++ b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts @@ -10,7 +10,7 @@ import { GrumpkinScalar, Note, Schnorr, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { SchnorrHardcodedAccountContractArtifact } from '@aztec/noir-contracts.js/SchnorrHardcodedAccount'; import { TokenContract } from '@aztec/noir-contracts.js/Token'; @@ -68,7 +68,7 @@ describe('guides/writing_an_account_contract', () => { logger.info(`Deployed token contract at ${token.address}`); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const mintAmount = 50n; const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/sample-dapp/index.mjs b/yarn-project/end-to-end/src/sample-dapp/index.mjs index 861c6f2cc03..6f421f61f3a 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.mjs @@ -1,5 +1,5 @@ import { getInitialTestAccountsWallets } from '@aztec/accounts/testing'; -import { ExtendedNote, Fr, Note, computeMessageSecretHash, createPXEClient } from '@aztec/aztec.js'; +import { ExtendedNote, Fr, Note, computeSecretHash, createPXEClient } from '@aztec/aztec.js'; import { fileURLToPath } from '@aztec/foundation/url'; import { getToken } from './contracts.mjs'; @@ -34,7 +34,7 @@ async function mintPrivateFunds(pxe) { const mintAmount = 20n; const secret = Fr.random(); - const secretHash = await computeMessageSecretHash(secret); + const secretHash = await computeSecretHash(secret); const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); const storageSlot = new Fr(5); diff --git a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs index 837c5386c97..c9e18f6ed6e 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs @@ -4,7 +4,7 @@ import { ExtendedNote, Fr, Note, - computeMessageSecretHash, + computeSecretHash, createPXEClient, waitForPXE, } from '@aztec/aztec.js'; @@ -27,7 +27,7 @@ describe('token', () => { const initialBalance = 20n; const secret = Fr.random(); - const secretHash = await computeMessageSecretHash(secret); + const secretHash = await computeSecretHash(secret); const receipt = await token.methods.mint_private(initialBalance, secretHash).send().wait(); const storageSlot = new Fr(5); diff --git a/yarn-project/end-to-end/src/shared/browser.ts b/yarn-project/end-to-end/src/shared/browser.ts index 516bd3aa87e..370a7698337 100644 --- a/yarn-project/end-to-end/src/shared/browser.ts +++ b/yarn-project/end-to-end/src/shared/browser.ts @@ -221,7 +221,7 @@ export const browserTestSuite = ( Fr, ExtendedNote, Note, - computeMessageSecretHash, + computeSecretHash, getDeployedTestAccountsWallets, INITIAL_TEST_SECRET_KEYS, INITIAL_TEST_SIGNING_KEYS, @@ -261,7 +261,7 @@ export const browserTestSuite = ( console.log(`Contract Deployed: ${token.address}`); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); const mintPrivateReceipt = await token.methods.mint_private(initialBalance, secretHash).send().wait(); const storageSlot = new Fr(5); 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 5da650d74d4..994d41e7d21 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 @@ -13,7 +13,7 @@ import { type TxHash, type TxReceipt, type Wallet, - computeMessageSecretHash, + computeSecretHash, deployL1Contract, retryUntil, } from '@aztec/aztec.js'; @@ -223,10 +223,15 @@ export class CrossChainTestHarness { public ownerAddress: AztecAddress, ) {} + /** + * Used to generate a claim secret using pedersen's hash function. + * @dev Used for both L1 to L2 messages and transparent note (pending shields) secrets. + * @returns A tuple of the secret and its hash. + */ generateClaimSecret(): [Fr, Fr] { this.logger.debug("Generating a claim secret using pedersen's hash function"); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); this.logger.info('Generated claim secret: ' + secretHash.toString()); return [secret, secretHash]; } diff --git a/yarn-project/end-to-end/src/shared/gas_portal_test_harness.ts b/yarn-project/end-to-end/src/shared/gas_portal_test_harness.ts index ff3a4dc4389..dee68998224 100644 --- a/yarn-project/end-to-end/src/shared/gas_portal_test_harness.ts +++ b/yarn-project/end-to-end/src/shared/gas_portal_test_harness.ts @@ -6,7 +6,7 @@ import { Fr, type PXE, type Wallet, - computeMessageSecretHash, + computeSecretHash, } from '@aztec/aztec.js'; import { GasPortalAbi, OutboxAbi, PortalERC20Abi } from '@aztec/l1-artifacts'; import { GasTokenContract } from '@aztec/noir-contracts.js'; @@ -153,7 +153,7 @@ class GasBridgingTestHarness implements IGasBridgingTestHarness { generateClaimSecret(): [Fr, Fr] { this.logger.debug("Generating a claim secret using pedersen's hash function"); const secret = Fr.random(); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); this.logger.info('Generated claim secret: ' + secretHash.toString()); return [secret, secretHash]; } diff --git a/yarn-project/simulator/src/test/utils.ts b/yarn-project/simulator/src/test/utils.ts index 56231284d8e..69769b28c7b 100644 --- a/yarn-project/simulator/src/test/utils.ts +++ b/yarn-project/simulator/src/test/utils.ts @@ -1,6 +1,6 @@ import { L1Actor, L1ToL2Message, L2Actor } from '@aztec/circuit-types'; import { type AztecAddress, EthAddress, type Fr } from '@aztec/circuits.js'; -import { computeMessageSecretHash } from '@aztec/circuits.js/hash'; +import { computeSecretHash } from '@aztec/circuits.js/hash'; import { sha256ToField } from '@aztec/foundation/crypto'; /** @@ -21,7 +21,7 @@ export const buildL1ToL2Message = ( const selectorBuf = Buffer.from(selector, 'hex'); const content = sha256ToField([selectorBuf, ...contentPreimage]); - const secretHash = computeMessageSecretHash(secret); + const secretHash = computeSecretHash(secret); return new L1ToL2Message(new L1Actor(EthAddress.random(), 1), new L2Actor(targetContract, 1), content, secretHash); }; From 1d78ca861b2f4e35944e90ab21ae20ff77060b0a Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 08:49:35 +0000 Subject: [PATCH 11/15] fixes --- .../aztec-nr/aztec/src/note/utils.nr | 11 +++++-- .../src/types/transparent_note.nr | 2 +- .../crates/types/src/constants.nr | 2 ++ .../src/transforms/note_interface.rs | 6 ++-- yarn-project/circuits.js/src/constants.gen.ts | 2 ++ .../circuits.js/src/hash/hash.test.ts | 4 +-- yarn-project/circuits.js/src/hash/hash.ts | 30 +++++++++++++++---- .../src/client/client_execution_context.ts | 4 +-- .../simulator/src/client/simulator.test.ts | 8 ++--- .../simulator/src/public/index.test.ts | 6 ++-- 10 files changed, 51 insertions(+), 24 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/note/utils.nr b/noir-projects/aztec-nr/aztec/src/note/utils.nr index 49311d794af..c5c06b46bcb 100644 --- a/noir-projects/aztec-nr/aztec/src/note/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/note/utils.nr @@ -2,7 +2,10 @@ use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interf use dep::protocol_types::{ address::AztecAddress, - constants::{GENERATOR_INDEX__OUTER_NULLIFIER, GENERATOR_INDEX__UNIQUE_NOTE_HASH, GENERATOR_INDEX__SILOED_NOTE_HASH}, + constants::{ + GENERATOR_INDEX__OUTER_NULLIFIER, GENERATOR_INDEX__UNIQUE_NOTE_HASH, + GENERATOR_INDEX__SILOED_NOTE_HASH, GENERATOR_INDEX__INNER_NOTE_HASH +}, hash::pedersen_hash, utils::arr_copy_slice }; @@ -20,8 +23,10 @@ fn compute_inner_note_hash(note: Note) -> Field where Note: NoteInterfa let header = note.get_header(); let note_hash = note.compute_note_content_hash(); - // TODO(#1205) Do we need a generator index here? - pedersen_hash([header.storage_slot, note_hash], 0) + pedersen_hash( + [header.storage_slot, note_hash], + GENERATOR_INDEX__INNER_NOTE_HASH + ) } fn compute_siloed_note_hash(note_with_header: Note) -> Field where Note: NoteInterface { diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index d5cf7197cef..9fc1d0737fc 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -2,7 +2,7 @@ use dep::aztec::{ note::{note_getter_options::PropertySelector, utils::compute_note_hash_for_consumption}, hash::poseidon2_hash, prelude::{NoteHeader, NoteInterface, PrivateContext}, - protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER + protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, }; global TRANSPARENT_NOTE_LEN: Field = 2; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index ba07e8518b1..10649ef22cf 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -243,3 +243,5 @@ global GENERATOR_INDEX__OVSK_M = 49; global GENERATOR_INDEX__TSK_M = 50; global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51; global GENERATOR_INDEX__NOTE_NULLIFIER = 52; +global GENERATOR_INDEX__INNER_NOTE_HASH = 53; +global GENERATOR_INDEX__NOTE_CONTENT_HASH = 54; diff --git a/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs b/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs index 70db1ebd336..f183c69b27a 100644 --- a/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs +++ b/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs @@ -418,8 +418,7 @@ fn generate_note_properties_fn( // Automatically generate the method to compute the note's content hash as: // fn compute_note_content_hash(self: NoteType) -> Field { -// // TODO(#1205) Should use a non-zero generator index. -// dep::aztec::hash::pedersen_hash(self.serialize_content(), 0) +// dep::aztec::hash::pedersen_hash(self.serialize_content(), dep::aztec::protocol_types::constants::GENERATOR_INDEX__NOTE_CONTENT_HASH) // } // fn generate_compute_note_content_hash( @@ -429,8 +428,7 @@ fn generate_compute_note_content_hash( let function_source = format!( " fn compute_note_content_hash(self: {}) -> Field {{ - // TODO(#1205) Should use a non-zero generator index. - dep::aztec::hash::pedersen_hash(self.serialize_content(), 0) + dep::aztec::hash::pedersen_hash(self.serialize_content(), dep::aztec::protocol_types::constants::GENERATOR_INDEX__NOTE_CONTENT_HASH) }} ", note_type diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index c40cdec3ec5..f141aa31070 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -203,4 +203,6 @@ export enum GeneratorIndex { TSK_M = 50, PUBLIC_KEYS_HASH = 51, NOTE_NULLIFIER = 52, + INNER_NOTE_HASH = 53, + NOTE_CONTENT_HASH = 54, } diff --git a/yarn-project/circuits.js/src/hash/hash.test.ts b/yarn-project/circuits.js/src/hash/hash.test.ts index cb1cd862661..477990a388b 100644 --- a/yarn-project/circuits.js/src/hash/hash.test.ts +++ b/yarn-project/circuits.js/src/hash/hash.test.ts @@ -10,7 +10,7 @@ import { computePublicDataTreeLeafSlot, computePublicDataTreeValue, computeSecretHash, - computeUniqueCommitment, + computeUniqueNoteHash, computeVarArgsHash, hashVK, siloNoteHash, @@ -35,7 +35,7 @@ describe('hash', () => { it('computes unique commitment', () => { const nonce = new Fr(123n); const innerCommitment = new Fr(456); - const res = computeUniqueCommitment(nonce, innerCommitment); + const res = computeUniqueNoteHash(nonce, innerCommitment); expect(res).toMatchSnapshot(); }); diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index 94998bf7ee3..7f3d489b7ba 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -76,13 +76,33 @@ export function siloNoteHash(contract: AztecAddress, innerNoteHash: Fr): Fr { } /** - * Computes a unique commitment. It includes a nonce which contains data that guarantees the commitment will be unique. + * Computes a note content hash. + * @param noteContent - The note content (e.g. note.items). + * @returns A note content hash. + */ +export function computeNoteContentHash(noteContent: Fr[]): Fr { + return pedersenHash(noteContent, GeneratorIndex.NOTE_CONTENT_HASH); +} + +/** + * Computes an inner note hash, given a storage slot and a note hash. + * @param storageSlot - The storage slot. + * @param noteHash - The note hash. + * @returns An inner note hash. + */ +export function computeInnerNoteHash(storageSlot: Fr, noteHash: Fr): Fr { + return pedersenHash([storageSlot, noteHash], GeneratorIndex.INNER_NOTE_HASH); +} + +/** + * Computes a unique note hash. + * @dev Includes a nonce which contains data that guarantees the resulting note hash will be unique. * @param nonce - The contract address. - * @param siloedCommitment - An siloed commitment. - * @returns A unique commitment. + * @param siloedNoteHash - An siloed note hash. + * @returns A unique note hash. */ -export function computeUniqueCommitment(nonce: Fr, siloedCommitment: Fr): Fr { - return pedersenHash([nonce, siloedCommitment], GeneratorIndex.UNIQUE_NOTE_HASH); +export function computeUniqueNoteHash(nonce: Fr, siloedNoteHash: Fr): Fr { + return pedersenHash([nonce, siloedNoteHash], GeneratorIndex.UNIQUE_NOTE_HASH); } /** diff --git a/yarn-project/simulator/src/client/client_execution_context.ts b/yarn-project/simulator/src/client/client_execution_context.ts index 88720fba6f8..0ec755281cc 100644 --- a/yarn-project/simulator/src/client/client_execution_context.ts +++ b/yarn-project/simulator/src/client/client_execution_context.ts @@ -22,7 +22,7 @@ import { type TxContext, } from '@aztec/circuits.js'; import { type Grumpkin } from '@aztec/circuits.js/barretenberg'; -import { computePublicDataTreeLeafSlot, computeUniqueCommitment, siloNoteHash } from '@aztec/circuits.js/hash'; +import { computePublicDataTreeLeafSlot, computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash'; import { type FunctionAbi, type FunctionArtifact, countArgumentsSize } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr, type Point } from '@aztec/foundation/fields'; @@ -257,7 +257,7 @@ export class ClientExecutionContext extends ViewDataOracle { notes.forEach(n => { if (n.index !== undefined) { const siloedNoteHash = siloNoteHash(n.contractAddress, n.innerNoteHash); - const uniqueSiloedNoteHash = computeUniqueCommitment(n.nonce, siloedNoteHash); + const uniqueSiloedNoteHash = computeUniqueNoteHash(n.nonce, siloedNoteHash); // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386) // Should always be uniqueSiloedNoteHash when publicly created notes include nonces. const noteHashForReadRequest = n.nonce.isZero() ? siloedNoteHash : uniqueSiloedNoteHash; diff --git a/yarn-project/simulator/src/client/simulator.test.ts b/yarn-project/simulator/src/client/simulator.test.ts index 87ca2ef7fc4..e4af77f494b 100644 --- a/yarn-project/simulator/src/client/simulator.test.ts +++ b/yarn-project/simulator/src/client/simulator.test.ts @@ -1,6 +1,6 @@ import { type AztecNode, CompleteAddress, Note } from '@aztec/circuit-types'; import { GeneratorIndex, computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js'; -import { computeUniqueCommitment, siloNoteHash } from '@aztec/circuits.js/hash'; +import { computeInnerNoteHash, computeNoteContentHash, computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash'; import { ABIParameterVisibility, type FunctionArtifactWithDebugMetadata, @@ -63,10 +63,10 @@ describe('Simulator', () => { oracle.getFunctionArtifactByName.mockResolvedValue(artifact); const note = createNote(); - const tokenNoteHash = pedersenHash(note.items); - const innerNoteHash = pedersenHash([storageSlot, tokenNoteHash]); + const tokenNoteHash = computeNoteContentHash(note.items); + const innerNoteHash = computeInnerNoteHash(storageSlot, tokenNoteHash); const siloedNoteHash = siloNoteHash(contractAddress, innerNoteHash); - const uniqueSiloedNoteHash = computeUniqueCommitment(nonce, siloedNoteHash); + const uniqueSiloedNoteHash = computeUniqueNoteHash(nonce, siloedNoteHash); const innerNullifier = poseidon2Hash([ uniqueSiloedNoteHash, appNullifierSecretKey, diff --git a/yarn-project/simulator/src/public/index.test.ts b/yarn-project/simulator/src/public/index.test.ts index 04568e3a46a..886ebf355fb 100644 --- a/yarn-project/simulator/src/public/index.test.ts +++ b/yarn-project/simulator/src/public/index.test.ts @@ -12,7 +12,7 @@ import { NullifierLeaf, NullifierLeafPreimage, } from '@aztec/circuits.js'; -import { siloNullifier } from '@aztec/circuits.js/hash'; +import { computeInnerNoteHash, computeNoteContentHash, siloNullifier } from '@aztec/circuits.js/hash'; import { makeHeader } from '@aztec/circuits.js/testing'; import { type FunctionArtifact, FunctionSelector, encodeArguments } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; @@ -334,9 +334,9 @@ describe('ACIR public execution simulator', () => { // Assert the note hash was created expect(result.newNoteHashes.length).toEqual(1); - const expectedNoteHash = pedersenHash([amount, secretHash]); + const expectedNoteHash = computeNoteContentHash([amount, secretHash]); const storageSlot = new Fr(5); // for pending_shields - const expectedInnerNoteHash = pedersenHash([storageSlot, expectedNoteHash]); + const expectedInnerNoteHash = computeInnerNoteHash(storageSlot, expectedNoteHash); expect(result.newNoteHashes[0].value).toEqual(expectedInnerNoteHash); }); From 77b6d7d488fa03dce74bdd0ab72c4f3ee1b07ef3 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 09:04:45 +0000 Subject: [PATCH 12/15] fmt --- .../end-to-end/src/composed/e2e_persistence.test.ts | 13 ++----------- .../end-to-end/src/sample-dapp/index.test.mjs | 10 +--------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts index 8eafef594ed..14fa82a73fe 100644 --- a/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts +++ b/yarn-project/end-to-end/src/composed/e2e_persistence.test.ts @@ -269,10 +269,7 @@ describe('Aztec persistence', () => { // mint some tokens with a secret we know and redeem later on a separate PXE secret = Fr.random(); mintAmount = 1000n; - const mintTxReceipt = await contract.methods - .mint_private(mintAmount, computeSecretHash(secret)) - .send() - .wait(); + const mintTxReceipt = await contract.methods.mint_private(mintAmount, computeSecretHash(secret)).send().wait(); mintTxHash = mintTxReceipt.txHash; // publicly reveal that I have 1000 tokens @@ -307,13 +304,7 @@ describe('Aztec persistence', () => { it('allows consuming transparent note created on another PXE', async () => { // this was created in the temporary PXE in `beforeAll` - await addPendingShieldNoteToPXE( - ownerWallet, - contractAddress, - mintAmount, - computeSecretHash(secret), - mintTxHash, - ); + await addPendingShieldNoteToPXE(ownerWallet, contractAddress, mintAmount, computeSecretHash(secret), mintTxHash); const balanceBeforeRedeem = await contract.methods.balance_of_private(ownerWallet.getAddress()).simulate(); diff --git a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs index c9e18f6ed6e..9508ab1631b 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs @@ -1,13 +1,5 @@ import { createAccount } from '@aztec/accounts/testing'; -import { - Contract, - ExtendedNote, - Fr, - Note, - computeSecretHash, - createPXEClient, - waitForPXE, -} from '@aztec/aztec.js'; +import { Contract, ExtendedNote, Fr, Note, computeSecretHash, createPXEClient, waitForPXE } from '@aztec/aztec.js'; import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token'; const { PXE_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; From 6957b651d6e531a6c8c61443ee7e9960beca82da Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 09:25:00 +0000 Subject: [PATCH 13/15] cleanup --- .../noir-contracts/contracts/test_contract/src/main.nr | 4 ++-- .../simulator/src/client/private_execution.test.ts | 7 ++----- yarn-project/simulator/src/client/simulator.test.ts | 9 +++++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index 5f9bc335646..c8876df738e 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -19,7 +19,7 @@ contract Test { use dep::aztec::{ context::{Context, inputs::private_context_inputs::PrivateContextInputs}, - hash::{pedersen_hash, poseidon2_hash, ArgsHasher}, + hash::{compute_secret_hash, pedersen_hash, ArgsHasher}, note::{ lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus @@ -351,7 +351,7 @@ contract Test { // Adapted from TokenContract#redeem_shield but without an initcheck so it can be run in simulator/src/client/private_execution.test.ts fn consume_note_from_secret(secret: Field) { let notes_set = storage.example_set; - let secret_hash = poseidon2_hash([secret, 92543]); // global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; + let secret_hash = compute_secret_hash(secret); let mut options = NoteGetterOptions::new(); options = options.select(TestNote::properties().value, secret_hash, Option::none()).set_limit(1); let notes = notes_set.get_notes(options); diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 10cdff0172f..ecff905aea0 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -21,7 +21,7 @@ import { nonEmptySideEffects, sideEffectArrayToValueArray, } from '@aztec/circuits.js'; -import { computeCommitmentNonce, computeVarArgsHash } from '@aztec/circuits.js/hash'; +import { computeCommitmentNonce, computeSecretHash, computeVarArgsHash } from '@aztec/circuits.js/hash'; import { makeHeader } from '@aztec/circuits.js/testing'; import { type FunctionArtifact, @@ -62,9 +62,6 @@ import { AcirSimulator } from './simulator.js'; jest.setTimeout(60_000); -// Copied over from `transparent_note.nr` - not placed in constants.nr as it's not a protocol constant -const GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; - describe('Private Execution test suite', () => { let oracle: MockProxy; let node: MockProxy; @@ -730,7 +727,7 @@ describe('Private Execution test suite', () => { it('Should be able to consume a dummy public to private message', async () => { const artifact = getFunctionArtifact(TestContractArtifact, 'consume_note_from_secret'); const secret = new Fr(1n); - const secretHash = poseidon2Hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); + const secretHash = computeSecretHash(secret); const note = new Note([secretHash]); const storageSlot = new Fr(5); oracle.getNotes.mockResolvedValue([ diff --git a/yarn-project/simulator/src/client/simulator.test.ts b/yarn-project/simulator/src/client/simulator.test.ts index e4af77f494b..62bb0c13b07 100644 --- a/yarn-project/simulator/src/client/simulator.test.ts +++ b/yarn-project/simulator/src/client/simulator.test.ts @@ -1,13 +1,18 @@ import { type AztecNode, CompleteAddress, Note } from '@aztec/circuit-types'; import { GeneratorIndex, computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js'; -import { computeInnerNoteHash, computeNoteContentHash, computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash'; +import { + computeInnerNoteHash, + computeNoteContentHash, + computeUniqueNoteHash, + siloNoteHash, +} from '@aztec/circuits.js/hash'; import { ABIParameterVisibility, type FunctionArtifactWithDebugMetadata, getFunctionArtifact, } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { pedersenHash, poseidon2Hash } from '@aztec/foundation/crypto'; +import { poseidon2Hash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token'; From fbeac1d80005fd2860f14098a42413ca6d0caf18 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Apr 2024 09:34:04 +0000 Subject: [PATCH 14/15] diff cleanup --- .../noir-contracts/contracts/test_contract/src/main.nr | 2 +- .../contracts/token_blacklist_contract/src/types/token_note.nr | 3 +-- .../contracts/token_contract/src/types/token_note.nr | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index c8876df738e..f7a40cf41a8 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -19,7 +19,7 @@ contract Test { use dep::aztec::{ context::{Context, inputs::private_context_inputs::PrivateContextInputs}, - hash::{compute_secret_hash, pedersen_hash, ArgsHasher}, + hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, note::{ lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr index 87ac609218c..5f6edf94d5f 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr @@ -2,7 +2,7 @@ use dep::aztec::{ prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, - oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, + oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} }; trait OwnedNote { @@ -82,5 +82,4 @@ impl OwnedNote for TokenNote { fn get_owner(self) -> AztecAddress { self.owner } - } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr index 87ac609218c..5f6edf94d5f 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr @@ -2,7 +2,7 @@ use dep::aztec::{ prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext, emit_encrypted_log}, protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER, note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash, - oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key}, + oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key, get_public_key::get_public_key} }; trait OwnedNote { @@ -82,5 +82,4 @@ impl OwnedNote for TokenNote { fn get_owner(self) -> AztecAddress { self.owner } - } From 9ee5330eca1dd748f855248528c26f17dac768dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Wed, 24 Apr 2024 08:44:24 +0200 Subject: [PATCH 15/15] Apply suggestions from code review --- .../developers/contracts/references/portals/data_structures.md | 2 +- docs/docs/developers/contracts/references/portals/inbox.md | 2 +- .../writing_contracts/portals/communicate_with_portal.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/developers/contracts/references/portals/data_structures.md b/docs/docs/developers/contracts/references/portals/data_structures.md index 4b639c24fb5..f7a87619edb 100644 --- a/docs/docs/developers/contracts/references/portals/data_structures.md +++ b/docs/docs/developers/contracts/references/portals/data_structures.md @@ -40,7 +40,7 @@ A message that is sent from L1 to L2. | `sender` | `L1Actor` | The actor on L1 that is sending the message. | | `recipient` | `L2Actor` | The actor on L2 that is to receive the message. | | `content` | `field (~254 bits)` | The field element containing the content to be sent to L2. | -| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | ## `L2ToL1Message` diff --git a/docs/docs/developers/contracts/references/portals/inbox.md b/docs/docs/developers/contracts/references/portals/inbox.md index 4f0eae50aed..3a33a1dbf06 100644 --- a/docs/docs/developers/contracts/references/portals/inbox.md +++ b/docs/docs/developers/contracts/references/portals/inbox.md @@ -17,7 +17,7 @@ Sends a message from L1 to L2. | -------------- | ------- | ----------- | | Recipient | `L2Actor` | The recipient of the message. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. | | Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field for rollup purposes. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions) | -| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | | ReturnValue | `bytes32` | The message hash, used as an identifier | #### Edge cases diff --git a/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md b/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md index 427020830dd..f84fa3b80ba 100644 --- a/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md +++ b/docs/docs/developers/contracts/writing_contracts/portals/communicate_with_portal.md @@ -17,7 +17,7 @@ When sending messages, we need to specify quite a bit of information beyond just | Name | Type | Description | | ----------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Recipient | `L2Actor` | The message recipient. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. | -| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | +| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. | | Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions) With all that information at hand, we can call the `sendL2Message` function on the Inbox. The function will return a `field` (inside `bytes32`) that is the hash of the message. This hash can be used as an identifier to spot when your message has been included in a rollup block.