Skip to content

Commit 3d54638

Browse files
authored
chore: simulator utils cleanup (AztecProtocol#4507)
Cleanup of utils in simulator package.
1 parent 10c3128 commit 3d54638

File tree

3 files changed

+24
-47
lines changed

3 files changed

+24
-47
lines changed

yarn-project/simulator/src/client/private_execution.test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe('Private Execution test suite', () => {
316316

317317
expect(result.newNotes).toHaveLength(1);
318318
const newNote = result.newNotes[0];
319-
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField()));
319+
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
320320

321321
const newCommitments = sideEffectArrayToValueArray(
322322
nonEmptySideEffects(result.callStackItem.publicInputs.newCommitments),
@@ -336,7 +336,7 @@ describe('Private Execution test suite', () => {
336336

337337
expect(result.newNotes).toHaveLength(1);
338338
const newNote = result.newNotes[0];
339-
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField()));
339+
expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
340340

341341
const newCommitments = sideEffectArrayToValueArray(
342342
nonEmptySideEffects(result.callStackItem.publicInputs.newCommitments),
@@ -353,8 +353,8 @@ describe('Private Execution test suite', () => {
353353
const amountToTransfer = 100n;
354354
const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create');
355355

356-
const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField());
357-
const recipientStorageSlot = computeSlotForMapping(new Fr(1n), recipient.toField());
356+
const storageSlot = computeSlotForMapping(new Fr(1n), owner);
357+
const recipientStorageSlot = computeSlotForMapping(new Fr(1n), recipient);
358358

359359
const notes = [buildNote(60n, owner, storageSlot), buildNote(80n, owner, storageSlot)];
360360
oracle.getNotes.mockResolvedValue(notes);
@@ -407,7 +407,7 @@ describe('Private Execution test suite', () => {
407407
const balance = 160n;
408408
const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create');
409409

410-
const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField());
410+
const storageSlot = computeSlotForMapping(new Fr(1n), owner);
411411

412412
const notes = [buildNote(balance, owner, storageSlot)];
413413
oracle.getNotes.mockResolvedValue(notes);
@@ -905,7 +905,7 @@ describe('Private Execution test suite', () => {
905905

906906
expect(result.newNotes).toHaveLength(1);
907907
const noteAndSlot = result.newNotes[0];
908-
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField()));
908+
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
909909

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

@@ -915,7 +915,7 @@ describe('Private Execution test suite', () => {
915915
expect(newCommitments).toHaveLength(1);
916916

917917
const commitment = newCommitments[0];
918-
const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField());
918+
const storageSlot = computeSlotForMapping(new Fr(1n), owner);
919919
const innerNoteHash = await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note);
920920
expect(commitment).toEqual(innerNoteHash);
921921

@@ -986,7 +986,7 @@ describe('Private Execution test suite', () => {
986986

987987
expect(execInsert.newNotes).toHaveLength(1);
988988
const noteAndSlot = execInsert.newNotes[0];
989-
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField()));
989+
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
990990

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

@@ -996,7 +996,7 @@ describe('Private Execution test suite', () => {
996996
expect(newCommitments).toHaveLength(1);
997997

998998
const commitment = newCommitments[0];
999-
const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField());
999+
const storageSlot = computeSlotForMapping(new Fr(1n), owner);
10001000
const innerNoteHash = await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note);
10011001
expect(commitment).toEqual(innerNoteHash);
10021002

@@ -1042,7 +1042,7 @@ describe('Private Execution test suite', () => {
10421042

10431043
expect(result.newNotes).toHaveLength(1);
10441044
const noteAndSlot = result.newNotes[0];
1045-
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField()));
1045+
expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner));
10461046

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

@@ -1052,7 +1052,7 @@ describe('Private Execution test suite', () => {
10521052
expect(newCommitments).toHaveLength(1);
10531053

10541054
const commitment = newCommitments[0];
1055-
const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField());
1055+
const storageSlot = computeSlotForMapping(new Fr(1n), owner);
10561056
expect(commitment).toEqual(
10571057
await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note),
10581058
);

yarn-project/simulator/src/public/index.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('ACIR public execution simulator', () => {
8888
const execution: PublicExecution = { contractAddress, functionData, args, callContext };
8989
const result = await executor.simulate(execution, GlobalVariables.empty());
9090

91-
const recipientBalanceStorageSlot = computeSlotForMapping(new Fr(6n), recipient.toField());
91+
const recipientBalanceStorageSlot = computeSlotForMapping(new Fr(6n), recipient);
9292
const totalSupplyStorageSlot = new Fr(4n);
9393

9494
const expectedBalance = new Fr(previousBalance.value + mintAmount);
@@ -110,7 +110,7 @@ describe('ACIR public execution simulator', () => {
110110
]);
111111

112112
const mintersStorageSlot = new Fr(2n);
113-
const isMinterStorageSlot = computeSlotForMapping(mintersStorageSlot, msgSender.toField());
113+
const isMinterStorageSlot = computeSlotForMapping(mintersStorageSlot, msgSender);
114114
// Note: There is only 1 storage read (for the isMinter value) because the other 2 reads get overwritten by
115115
// the updates
116116
expect(result.contractStorageReads).toEqual([
@@ -152,8 +152,8 @@ describe('ACIR public execution simulator', () => {
152152
startSideEffectCounter: 0,
153153
});
154154

155-
recipientStorageSlot = computeSlotForMapping(new Fr(6n), recipient.toField());
156-
senderStorageSlot = computeSlotForMapping(new Fr(6n), Fr.fromBuffer(sender.toBuffer()));
155+
recipientStorageSlot = computeSlotForMapping(new Fr(6n), recipient);
156+
senderStorageSlot = computeSlotForMapping(new Fr(6n), sender);
157157

158158
publicContracts.getBytecode.mockResolvedValue(Buffer.from(transferArtifact.bytecode, 'base64'));
159159

yarn-project/simulator/src/utils.ts

+9-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,18 @@
1-
import { GrumpkinPrivateKey } from '@aztec/circuits.js';
2-
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
31
import { pedersenHash } from '@aztec/foundation/crypto';
42
import { Fr } from '@aztec/foundation/fields';
53

6-
/**
7-
* A point in the format that Aztec.nr uses.
8-
*/
9-
export type NoirPoint = {
10-
/** The x coordinate. */
11-
x: bigint;
12-
/** The y coordinate. */
13-
y: bigint;
14-
};
15-
164
/**
175
* Computes the resulting storage slot for an entry in a mapping.
186
* @param mappingSlot - The slot of the mapping within state.
19-
* @param owner - The key of the mapping.
7+
* @param key - The key of the mapping.
208
* @returns The slot in the contract storage where the value is stored.
219
*/
22-
export function computeSlotForMapping(mappingSlot: Fr, owner: NoirPoint | Fr) {
23-
const isFr = (owner: NoirPoint | Fr): owner is Fr => typeof (owner as Fr).value === 'bigint';
24-
const ownerField = isFr(owner) ? owner : new Fr(owner.x);
25-
26-
return Fr.fromBuffer(pedersenHash([mappingSlot, ownerField].map(f => f.toBuffer())));
27-
}
28-
29-
/**
30-
* Computes the public key for a private key.
31-
* @param privateKey - The private key.
32-
* @param grumpkin - The grumpkin instance.
33-
* @returns The public key.
34-
*/
35-
export function toPublicKey(privateKey: GrumpkinPrivateKey, grumpkin: Grumpkin): NoirPoint {
36-
const point = grumpkin.mul(Grumpkin.generator, privateKey);
37-
return {
38-
x: point.x.value,
39-
y: point.y.value,
40-
};
10+
export function computeSlotForMapping(
11+
mappingSlot: Fr,
12+
key: {
13+
/** Serialize to a field. */
14+
toField: () => Fr;
15+
},
16+
) {
17+
return Fr.fromBuffer(pedersenHash([mappingSlot, key.toField()].map(field => field.toBuffer())));
4118
}

0 commit comments

Comments
 (0)