diff --git a/noir-projects/aztec-nr/aztec/src/generators.nr b/noir-projects/aztec-nr/aztec/src/generators.nr index 7af58db4602..d6318f2bec9 100644 --- a/noir-projects/aztec-nr/aztec/src/generators.nr +++ b/noir-projects/aztec-nr/aztec/src/generators.nr @@ -7,14 +7,6 @@ global Ga3 = Point { x: 0x0edb1e293c3ce91bfc04e3ceaa50d2c541fa9d091c72eb403efb1c // If you change this update `G_SLOT` in `yarn-project/simulator/src/client/test_utils.ts` as well global G_slot = Point { x: 0x041223147b680850dc82e8a55a952d4df20256fe0593d949a9541ca00f0abf15, y: 0x0a8c72e60d0e60f5d804549d48f3044d06140b98ed717a9b532af630c1530791, is_infinite: false }; -// TODO(#7551): nuke this func - is only temporarily used in note_interface.rs before we get some AVM compatible -// hash func hashing to a point -pub fn field_to_point(slot_preimage: Field) -> Point { - // We use the unsafe version because the multi_scalar_mul will constrain the scalars. - let slot_preimage_scalar = dep::std::hash::from_field_unsafe(slot_preimage); - dep::std::embedded_curve_ops::multi_scalar_mul([G_slot], [slot_preimage_scalar]) -} - mod test { use crate::generators::{Ga1, Ga2, Ga3, G_slot}; use dep::protocol_types::point::Point; diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 7e68dfc2efe..0fa65e93549 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -4,8 +4,9 @@ use dep::protocol_types::{ GENERATOR_INDEX__SECRET_HASH, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, MAX_ARGS_LENGTH }, - traits::Hash, hash::{pedersen_hash, compute_siloed_nullifier, sha256_to_field} + traits::Hash, hash::{pedersen_hash, compute_siloed_nullifier, sha256_to_field, pedersen_commitment} }; +// Note: pedersen_commitment is used only as a re-export here use crate::oracle::logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog}; pub fn compute_secret_hash(secret: Field) -> 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 14ba48f7c08..20480cf6cb7 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 @@ -5,6 +5,13 @@ use dep::aztec::{ keys::getters::get_nsk_app }; +// TODO(#7738): Nuke the following imports +use dep::aztec::{ + generators::{Ga1 as G_amt, Ga2 as G_npk, Ga3 as G_rnd}, + protocol_types::{point::Point, scalar::Scalar} +}; +use dep::std::{embedded_curve_ops::multi_scalar_mul, hash::from_field_unsafe}; + trait OwnedNote { fn new(amount: U128, owner_npk_m_hash: Field) -> Self; fn get_amount(self) -> U128; @@ -48,6 +55,21 @@ impl NoteInterface for TokenNote { ]); (note_hash_for_nullify, nullifier) } + + // TODO(#7738): Nuke this function and have it auto-generated by macros + fn compute_note_hiding_point(self) -> Point { + let npk_m_hash_scalar = from_field_unsafe(self.npk_m_hash); + let randomness_scalar = from_field_unsafe(self.randomness); + multi_scalar_mul( + [G_amt, G_npk, G_rnd], + [Scalar { + lo: self.amount.to_integer(), + hi: 0 + }, + npk_m_hash_scalar, + randomness_scalar] + ) + } } impl OwnedNote for TokenNote { diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr index 44162202a69..0ef3129d7c0 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -12,12 +12,11 @@ use crate::{ }, merkle_tree::root::root_from_sibling_path, messaging::l2_to_l1_message::ScopedL2ToL1Message, recursion::verification_key::VerificationKey, traits::is_empty, - utils::field::field_from_bytes_32_trunc + utils::field::field_from_bytes_32_trunc, point::Point }; -use std::hash::{pedersen_hash_with_separator, sha256}; pub fn sha256_to_field(bytes_to_hash: [u8; N]) -> Field { - let sha256_hashed = sha256(bytes_to_hash); + let sha256_hashed = std::hash::sha256(bytes_to_hash); let hash_in_a_field = field_from_bytes_32_trunc(sha256_hashed); hash_in_a_field @@ -256,6 +255,10 @@ pub fn poseidon2_hash(inputs: [Field; N]) -> Field { std::hash::poseidon2::Poseidon2::hash(inputs, N) } +pub fn pedersen_commitment(inputs: [Field; N], hash_index: u32) -> Point { + std::hash::pedersen_commitment_with_separator(inputs, hash_index) +} + #[test] fn smoke_sha256_to_field() { let full_buffer = [ @@ -273,7 +276,7 @@ fn smoke_sha256_to_field() { assert(result == 0x448ebbc9e1a31220a2f3830c18eef61b9bd070e5084b7fa2a359fe729184c7); // to show correctness of the current ver (truncate one byte) vs old ver (mod full bytes): - let result_bytes = sha256(full_buffer); + let result_bytes = std::hash::sha256(full_buffer); let truncated_field = crate::utils::field::field_from_bytes_32_trunc(result_bytes); assert(truncated_field == result); let mod_res = result + (result_bytes[31] as Field); 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 251ec46e101..5eeac681818 100644 --- a/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs +++ b/noir/noir-repo/aztec_macros/src/transforms/note_interface.rs @@ -503,14 +503,10 @@ fn generate_compute_note_hiding_point( note_type: &String, impl_span: Option, ) -> Result { - // TODO(#7551): Replace the pedersen hash with something that returns a point directly to avoid - // the superfluous call to field_to_point(...). I was trying to use pedersen_commitment for that - // but that is currently not supported by the AVM (but might be soon). let function_source = format!( " fn compute_note_hiding_point(self: {}) -> aztec::protocol_types::point::Point {{ - let h = aztec::hash::pedersen_hash(self.serialize_content(), aztec::protocol_types::constants::GENERATOR_INDEX__NOTE_HIDING_POINT); - aztec::generators::field_to_point(h) + aztec::hash::pedersen_commitment(self.serialize_content(), aztec::protocol_types::constants::GENERATOR_INDEX__NOTE_HIDING_POINT) }} ", note_type diff --git a/yarn-project/simulator/src/client/simulator.test.ts b/yarn-project/simulator/src/client/simulator.test.ts index 8b15e719630..27e6355367e 100644 --- a/yarn-project/simulator/src/client/simulator.test.ts +++ b/yarn-project/simulator/src/client/simulator.test.ts @@ -5,7 +5,7 @@ import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/ab import { AztecAddress } from '@aztec/foundation/aztec-address'; import { poseidon2Hash } from '@aztec/foundation/crypto'; import { Fr, type Point } from '@aztec/foundation/fields'; -import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token'; +import { TokenBlacklistContractArtifact } from '@aztec/noir-contracts.js'; import { type MockProxy, mock } from 'jest-mock-extended'; @@ -47,10 +47,13 @@ describe('Simulator', () => { }); describe('computeNoteHashAndOptionallyANullifier', () => { - const artifact = getFunctionArtifact(TokenContractArtifact, 'compute_note_hash_and_optionally_a_nullifier'); + const artifact = getFunctionArtifact( + TokenBlacklistContractArtifact, + 'compute_note_hash_and_optionally_a_nullifier', + ); const nonce = Fr.random(); - const storageSlot = TokenContractArtifact.storageLayout['balances'].slot; - const noteTypeId = TokenContractArtifact.notes['TokenNote'].id; + const storageSlot = TokenBlacklistContractArtifact.storageLayout['balances'].slot; + const noteTypeId = TokenBlacklistContractArtifact.notes['TokenNote'].id; const createNote = (amount = 123n) => new Note([new Fr(amount), ownerMasterNullifierPublicKey.hash(), Fr.random()]); diff --git a/yarn-project/simulator/src/client/test_utils.ts b/yarn-project/simulator/src/client/test_utils.ts index 0470397fafd..f8e34baa86b 100644 --- a/yarn-project/simulator/src/client/test_utils.ts +++ b/yarn-project/simulator/src/client/test_utils.ts @@ -1,6 +1,6 @@ import { Fq, Fr, GeneratorIndex, Point } from '@aztec/circuits.js'; import { Grumpkin } from '@aztec/circuits.js/barretenberg'; -import { pedersenHash } from '@aztec/foundation/crypto'; +import { pedersenCommit } from '@aztec/foundation/crypto'; // Copied over from `noir-projects/aztec-nr/aztec/src/generators.nr` const G_SLOT = new Point( @@ -15,10 +15,11 @@ const G_SLOT = new Point( * @returns A note hiding point. */ export function computeNoteHidingPoint(noteContent: Fr[]): Point { - // TODO(#7551): how this is computed will need to be updated - const h = pedersenHash(noteContent, GeneratorIndex.NOTE_HIDING_POINT); - const grumpkin = new Grumpkin(); - return grumpkin.mul(G_SLOT, new Fq(h.toBigInt())); + const c = pedersenCommit( + noteContent.map(f => f.toBuffer()), + GeneratorIndex.NOTE_HIDING_POINT, + ); + return new Point(new Fr(c[0]), new Fr(c[1]), false); } /**