Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: comptime deriving generators in macros #9195

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions noir-projects/aztec-nr/aztec/src/macros/notes/mod.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::note::{note_getter_options::PropertySelector, note_header::NoteHeader};
use crate::{note::{note_getter_options::PropertySelector, note_header::NoteHeader}, prelude::Point};
use protocol_types::meta::{flatten_to_fields, pack_from_fields};
use std::{
collections::umap::UHashMap,
hash::{BuildHasherDefault, poseidon2::Poseidon2Hasher},
hash::{BuildHasherDefault, derive_generators, poseidon2::Poseidon2Hasher},
meta::{typ::fresh_type_variable, type_of, unquote},
};

Expand Down Expand Up @@ -310,16 +310,24 @@ comptime fn generate_multi_scalar_mul(
let mut scalars_list = &[];
let mut args_list = &[];
let mut aux_vars_list = &[];
// TODO(#8648): Generate generators at comptime instead of importing here.
for i in 0..indexed_fields.len() {
let (field_name, typ, index) = indexed_fields[i];
let start_generator_index = index + 1;
let (flattened_field, aux_vars) = flatten_to_fields(field_name, typ, &[]);
for j in 0..flattened_field.len() {
let flattened_as_field = flattened_field[j];
let generator_index = start_generator_index + j;
generators_list = generators_list.push_back(f"aztec::generators::Ga{generator_index}"
.quoted_contents());

let generators: [Point; 1] =
derive_generators("aztec_nr_generators".as_bytes(), generator_index);
let generator_x = generators[0].x;
let generator_y = generators[0].y;

generators_list = generators_list.push_back(
quote {
aztec::protocol_types::point::Point { x: $generator_x, y: $generator_y, is_infinite: false }
},
);
scalars_list =
scalars_list.push_back(quote { std::hash::from_field_unsafe($flattened_as_field) });
}
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
import { asyncMap } from '@aztec/foundation/async-map';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { times } from '@aztec/foundation/collection';
import { poseidon2HashWithSeparator, randomInt } from '@aztec/foundation/crypto';
import { poseidon2Hash, poseidon2HashWithSeparator, 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';
Expand All @@ -74,7 +74,6 @@ import { MessageLoadOracleInputs } from '../acvm/index.js';
import { buildL1ToL2Message } from '../test/utils.js';
import { type DBOracle } from './db_oracle.js';
import { AcirSimulator } from './simulator.js';
import { computeNoteHash } from './test_utils.js';

jest.setTimeout(60_000);

Expand Down Expand Up @@ -314,7 +313,8 @@ describe('Private Execution test suite', () => {
const noteHashIndex = randomInt(1); // mock index in TX's final noteHashes array
const nonce = computeNoteHashNonce(mockFirstNullifier, noteHashIndex);
const note = new Note([new Fr(amount), ownerNpkMHash, Fr.random()]);
const noteHash = computeNoteHash(storageSlot, note.items);
// Note: The following does not correspond to how note hashing is generally done in real notes.
const noteHash = poseidon2Hash([storageSlot, ...note.items]);
return {
contractAddress,
storageSlot,
Expand Down
34 changes: 1 addition & 33 deletions yarn-project/simulator/src/client/simulator.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { type AztecNode, CompleteAddress, Note } from '@aztec/circuit-types';
import { GeneratorIndex, KeyValidationRequest, computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js';
import { computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash';
import { KeyValidationRequest, computeAppNullifierSecretKey, deriveKeys } from '@aztec/circuits.js';
import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
import { Fr, type Point } from '@aztec/foundation/fields';
import { TokenBlacklistContractArtifact } from '@aztec/noir-contracts.js';

import { type MockProxy, mock } from 'jest-mock-extended';

import { type DBOracle } from './db_oracle.js';
import { AcirSimulator } from './simulator.js';
import { computeNoteHash } from './test_utils.js';

describe('Simulator', () => {
let oracle: MockProxy<DBOracle>;
Expand Down Expand Up @@ -59,35 +56,6 @@ describe('Simulator', () => {
const createNote = (amount = 123n) =>
new Note([new Fr(amount), new Fr(0), ownerMasterNullifierPublicKey.hash(), Fr.random()]);

it('should compute note hashes and nullifier', async () => {
Copy link
Contributor Author

@benesjan benesjan Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to nuke this test because it relied on having access to the generators to get the same note hash. This is brittle as we needed to manually copy them and I don't think it provides value as we never need to compute the note hash in typescript (out of tests) and instead it is always note specific. So keeping the TS and Noir code in match just seems like an unnecessary hassle.

oracle.getFunctionArtifactByName.mockResolvedValue(artifact);

const note = createNote();
const noteHash = computeNoteHash(storageSlot, note.items);
const uniqueNoteHash = computeUniqueNoteHash(nonce, noteHash);
const siloedNoteHash = siloNoteHash(contractAddress, uniqueNoteHash);
const innerNullifier = poseidon2HashWithSeparator(
[siloedNoteHash, appNullifierSecretKey],
GeneratorIndex.NOTE_NULLIFIER,
);

const result = await simulator.computeNoteHashAndOptionallyANullifier(
contractAddress,
nonce,
storageSlot,
noteTypeId,
true,
note,
);

expect(result).toEqual({
noteHash,
uniqueNoteHash,
siloedNoteHash,
innerNullifier,
});
});

it('throw if the contract does not implement "compute_note_hash_and_optionally_a_nullifier"', async () => {
oracle.getFunctionArtifactByName.mockResolvedValue(undefined);

Expand Down
57 changes: 0 additions & 57 deletions yarn-project/simulator/src/client/test_utils.ts

This file was deleted.

Loading