Skip to content

Commit

Permalink
TS compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jul 26, 2024
1 parent 94492c5 commit 8b7eab7
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 43 deletions.
3 changes: 1 addition & 2 deletions noir-projects/aztec-nr/aztec/src/generators.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::protocol_types::point::Point;
global Ga1 = Point { x: 0x30426e64aee30e998c13c8ceecda3a77807dbead52bc2f3bf0eae851b4b710c1, y: 0x113156a068f603023240c96b4da5474667db3b8711c521c748212a15bc034ea6, is_infinite: false };
global Ga2 = Point { x: 0x2825c79cc6a5cbbeef7d6a8f1b6a12b312aa338440aefeb4396148c89147c049, y: 0x129bfd1da54b7062d6b544e7e36b90736350f6fba01228c41c72099509f5701e, is_infinite: false };
global Ga3 = Point { x: 0x0edb1e293c3ce91bfc04e3ceaa50d2c541fa9d091c72eb403efb1cfa2cb3357f, y: 0x1341d675fa030ece3113ad53ca34fd13b19b6e9762046734f414824c4d6ade35, is_infinite: false };
global G_slot = Point { x: 0x0edb1e293c3ce91bfc04e3ceaa50d2c541fa9d091c72eb403efb1cfa2cb3357f, y: 0x1341d675fa030ece3113ad53ca34fd13b19b6e9762046734f414824c4d6ade35, is_infinite: false };
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
Expand All @@ -25,7 +25,6 @@ fn test_generators() {
assert_eq(generators[0], Ga1);
assert_eq(generators[1], Ga2);
assert_eq(generators[2], Ga3);
// TODO(benesjan): update this
assert_eq(generators[3], G_slot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ use crate::{hash::pedersen_hash, traits::ToField};
pub fn derive_storage_slot_in_map<K>(storage_slot: Field, key: K) -> Field where K: ToField {
pedersen_hash([storage_slot, key.to_field()], 0)
}

mod test {
use crate::{address::AztecAddress, storage::map::derive_storage_slot_in_map};

#[test]
fn test_derive_storage_slot_in_map_matches_typescript() {
let map_slot = 0x132258fb6962c4387ba659d9556521102d227549a386d39f0b22d1890d59c2b5;
let key = AztecAddress::from_field(0x302dbc2f9b50a73283d5fb2f35bc01eae8935615817a0b4219a057b2ba8a5a3f);

let slot = derive_storage_slot_in_map(map_slot, key);

// The following value was generated by `map_slot.test.ts`
let slot_from_typescript = 0x2e6865f314bd97a5d93eb47180214b9bc61ef070e21f091afd7d441f6bca9565;

assert_eq(slot, slot_from_typescript);
}
}
18 changes: 13 additions & 5 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
import { pedersenHash, pedersenHashBuffer } from '@aztec/foundation/crypto';
import { Fr, type Point } from '@aztec/foundation/fields';
import { Fq, Fr, Point } from '@aztec/foundation/fields';
import { numToUInt8, numToUInt16BE, numToUInt32BE } from '@aztec/foundation/serialize';

import chunk from 'lodash.chunk';

import { Grumpkin } from '../barretenberg/index.js';
import { ARGS_HASH_CHUNK_COUNT, ARGS_HASH_CHUNK_LENGTH, GeneratorIndex, MAX_ARGS_LENGTH } from '../constants.gen.js';
import { deriveBaseSlot } from '../storage_slots/index.js';
import { VerificationKey } from '../structs/index.js';

/**
Expand Down Expand Up @@ -53,6 +52,12 @@ export function siloNoteHash(contract: AztecAddress, uniqueNoteHash: Fr): Fr {
return pedersenHash([contract, uniqueNoteHash], GeneratorIndex.SILOED_NOTE_HASH);
}

const G_SLOT = new Point(
new Fr(0x041223147b680850dc82e8a55a952d4df20256fe0593d949a9541ca00f0abf15n),
new Fr(0x0a8c72e60d0e60f5d804549d48f3044d06140b98ed717a9b532af630c1530791n),
false,
);

/**
* Computes a note content hash as is done by the default implementation injected by macros.
* @param noteContent - The note content (e.g. note.items).
Expand All @@ -61,8 +66,10 @@ export function siloNoteHash(contract: AztecAddress, uniqueNoteHash: Fr): Fr {
* of computing the note content hash.
*/
export function computeNoteContentHash(noteContent: Fr[]): Point {
// TODO(#7551): how this is computed will need to be updated
const h = pedersenHash(noteContent, GeneratorIndex.NOTE_CONTENT_HASH);
return deriveBaseSlot(h);
const grumpkin = new Grumpkin();
return grumpkin.mul(G_SLOT, new Fq(h.toBigInt()));
}

/**
Expand All @@ -71,9 +78,10 @@ export function computeNoteContentHash(noteContent: Fr[]): Point {
* @param noteContentHash - The hash of note content.
* @returns An inner note hash.
*/
export function computeInnerNoteHash(storageSlot: Point, noteContentHash: Point): Point {
export function computeInnerNoteHash(storageSlot: Fr, noteContentHash: Point): Point {
const grumpkin = new Grumpkin();
return grumpkin.add(storageSlot, noteContentHash);
const storageSlotPoint = grumpkin.mul(G_SLOT, new Fq(storageSlot.toBigInt()));
return grumpkin.add(storageSlotPoint, noteContentHash);
}

/**
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './contract/index.js';
export * from './hints/index.js';
export * from './interfaces/index.js';
export * from './keys/index.js';
export * from './storage_slots/index.js';
export * from './structs/index.js';
export * from './types/index.js';
export * from './utils/index.js';
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/storage_slots/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './generators.js';
export * from './map_slot.js';
20 changes: 5 additions & 15 deletions yarn-project/circuits.js/src/storage_slots/map_slot.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr } from '@aztec/foundation/fields';
import { updateInlineTestData } from '@aztec/foundation/testing';

import { deriveStorageSlotInMap } from './map_slot.js';

describe('Map slot', () => {
it('derived map slot matches Noir', () => {
const mapSlot = new Point(
new Fr(0x132258fb6962c4387ba659d9556521102d227549a386d39f0b22d1890d59c2b5n),
new Fr(0x0a9fa1154d046737fdc9562245b2db9eb87d065301c70995c55ea20693e27b44n),
false,
);

const mapSlot = new Fr(0x132258fb6962c4387ba659d9556521102d227549a386d39f0b22d1890d59c2b5n);
const key = AztecAddress.fromString('0x302dbc2f9b50a73283d5fb2f35bc01eae8935615817a0b4219a057b2ba8a5a3f');

const slot = deriveStorageSlotInMap(mapSlot, key);
Expand All @@ -22,14 +17,9 @@ describe('Map slot', () => {

// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data
updateInlineTestData(
'noir-projects/noir-protocol-circuits/crates/types/src/slots.nr',
'slot_x_from_typescript',
slot.x.toString(),
);
updateInlineTestData(
'noir-projects/noir-protocol-circuits/crates/types/src/slots.nr',
'slot_y_from_typescript',
slot.y.toString(),
'noir-projects/noir-protocol-circuits/crates/types/src/storage/map.nr',
'slot_from_typescript',
slot.toString(),
);
});
});
13 changes: 5 additions & 8 deletions yarn-project/circuits.js/src/storage_slots/map_slot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO(benesjan): update cheatcodes to use this
import { G_MAP_SLOT_LAYER_1 } from '@aztec/circuits.js';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { type Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields';
import { pedersenHash } from '@aztec/foundation/crypto';
import { type Fr } from '@aztec/foundation/fields';

/**
* Computes the resulting storage slot for an entry in a map.
Expand All @@ -11,13 +10,11 @@ import { type Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields';
* TODO(#7551): Test that it matches Noir.
*/
export function deriveStorageSlotInMap(
mapSlot: Point,
mapSlot: Fr,
key: {
/** Serialize to a field. */
toField: () => Fr;
},
): Point {
const grumpkin = new Grumpkin();
const scalar = new GrumpkinScalar(key.toField().toBigInt());
return grumpkin.add(grumpkin.mul(G_MAP_SLOT_LAYER_1, scalar), mapSlot);
): Fr {
return pedersenHash([mapSlot, key.toField()]);
}
3 changes: 1 addition & 2 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
type Header,
type KeyValidationRequest,
type L1_TO_L2_MSG_TREE_HEIGHT,
type Point,
} from '@aztec/circuits.js';
import { computeL1ToL2MessageNullifier } from '@aztec/circuits.js/hash';
import { type FunctionArtifact, getFunctionArtifact } from '@aztec/foundation/abi';
Expand Down Expand Up @@ -78,7 +77,7 @@ export class SimulatorOracle implements DBOracle {
return capsule;
}

async getNotes(contractAddress: AztecAddress, storageSlot: Point, status: NoteStatus) {
async getNotes(contractAddress: AztecAddress, storageSlot: Fr, status: NoteStatus) {
const noteDaos = await this.db.getIncomingNotes({
contractAddress,
storageSlot,
Expand Down
13 changes: 7 additions & 6 deletions yarn-project/simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
computeAppNullifierSecretKey,
computeOvskApp,
deriveKeys,
deriveStorageSlotInMap,
getContractInstanceFromDeployParams,
getNonEmptyItems,
} from '@aztec/circuits.js';
Expand Down Expand Up @@ -354,7 +355,7 @@ describe('Private Execution test suite', () => {

expect(result.newNotes).toHaveLength(1);
const newNote = result.newNotes[0];
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
expect(newNote.storageSlot).toEqual(deriveStorageSlotInMap(new Fr(1n), owner));
expect(newNote.noteTypeId).toEqual(valueNoteTypeId); // ValueNote

const noteHashes = getNonEmptyItems(result.callStackItem.publicInputs.noteHashes);
Expand Down Expand Up @@ -384,7 +385,7 @@ describe('Private Execution test suite', () => {

expect(result.newNotes).toHaveLength(1);
const newNote = result.newNotes[0];
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
expect(newNote.storageSlot).toEqual(deriveStorageSlotInMap(new Fr(1n), owner));
expect(newNote.noteTypeId).toEqual(valueNoteTypeId); // ValueNote

const noteHashes = getNonEmptyItems(result.callStackItem.publicInputs.noteHashes);
Expand All @@ -411,8 +412,8 @@ describe('Private Execution test suite', () => {
const amountToTransfer = 100n;
const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create_no_init_check');

const storageSlot = computeSlotForMapping(StatefulTestContractArtifact.storageLayout['notes'].slot, owner);
const recipientStorageSlot = computeSlotForMapping(
const storageSlot = deriveStorageSlotInMap(StatefulTestContractArtifact.storageLayout['notes'].slot, owner);
const recipientStorageSlot = deriveStorageSlotInMap(
StatefulTestContractArtifact.storageLayout['notes'].slot,
recipient,
);
Expand Down Expand Up @@ -488,7 +489,7 @@ describe('Private Execution test suite', () => {
const balance = 160n;
const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create_no_init_check');

const storageSlot = deriveStorageSlotInMap(deriveBaseSlot(new Fr(1n)), owner);
const storageSlot = deriveStorageSlotInMap(new Fr(1n), owner);

const notes = [
buildNote(
Expand Down Expand Up @@ -959,7 +960,7 @@ describe('Private Execution test suite', () => {

expect(result.newNotes).toHaveLength(1);
const noteAndSlot = result.newNotes[0];
expect(noteAndSlot.storageSlot).toEqual(deriveStorageSlotInMap(deriveBaseSlot(new Fr(1n)), owner));
expect(noteAndSlot.storageSlot).toEqual(deriveStorageSlotInMap(new Fr(1n), owner));

expect(noteAndSlot.note.items[0]).toEqual(new Fr(amountToTransfer));

Expand Down
6 changes: 2 additions & 4 deletions yarn-project/simulator/src/public/fee_payment.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { GAS_TOKEN_ADDRESS } from '@aztec/circuits.js';
import { GAS_TOKEN_ADDRESS, deriveStorageSlotInMap } from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { GasTokenArtifact } from '@aztec/protocol-contracts/gas-token';

import { computeSlotForMapping } from '../utils.js';

/**
* Computes the storage slot within the gas token contract for the balance of the fee payer.
*/
export function computeFeePayerBalanceStorageSlot(feePayer: AztecAddress) {
return computeSlotForMapping(GasTokenArtifact.storageLayout.balances.slot, feePayer);
return deriveStorageSlotInMap(GasTokenArtifact.storageLayout.balances.slot, feePayer);
}

/**
Expand Down

0 comments on commit 8b7eab7

Please sign in to comment.