Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 23, 2024
1 parent e8075d0 commit f5a41c8
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 24 deletions.
11 changes: 8 additions & 3 deletions noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -20,8 +23,10 @@ fn compute_inner_note_hash<Note, N>(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, N>(note_with_header: Note) -> Field where Note: NoteInterface<N> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 2 additions & 4 deletions noir/noir-repo/aztec_macros/src/transforms/note_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,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(
Expand All @@ -425,8 +424,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
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/hash/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
computePublicDataTreeLeafSlot,
computePublicDataTreeValue,
computeSecretHash,
computeUniqueCommitment,
computeUniqueNoteHash,
computeVarArgsHash,
hashVK,
siloNoteHash,
Expand All @@ -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();
});

Expand Down
30 changes: 25 additions & 5 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/simulator/src/client/client_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/simulator/src/client/simulator.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit f5a41c8

Please sign in to comment.