Skip to content

Commit

Permalink
feat: comptime deriving generators in macros (#9195)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Oct 25, 2024
1 parent 84205d8 commit c4b629c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 98 deletions.
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 () => {
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.

0 comments on commit c4b629c

Please sign in to comment.