From 1dd0ebfdec2942ceecb958a15c84085ca8bb3d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 8 Feb 2024 17:17:03 +0100 Subject: [PATCH] chore: simulator utils cleanup (#4507) Cleanup of utils in simulator package. --- .../src/client/private_execution.test.ts | 22 +++++----- .../simulator/src/public/index.test.ts | 8 ++-- yarn-project/simulator/src/utils.ts | 41 ++++--------------- 3 files changed, 24 insertions(+), 47 deletions(-) diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 2ea03803d01..00ea626f4ff 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -316,7 +316,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.toField())); + expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner)); const newCommitments = sideEffectArrayToValueArray( nonEmptySideEffects(result.callStackItem.publicInputs.newCommitments), @@ -336,7 +336,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.toField())); + expect(newNote.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner)); const newCommitments = sideEffectArrayToValueArray( nonEmptySideEffects(result.callStackItem.publicInputs.newCommitments), @@ -353,8 +353,8 @@ describe('Private Execution test suite', () => { const amountToTransfer = 100n; const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create'); - const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField()); - const recipientStorageSlot = computeSlotForMapping(new Fr(1n), recipient.toField()); + const storageSlot = computeSlotForMapping(new Fr(1n), owner); + const recipientStorageSlot = computeSlotForMapping(new Fr(1n), recipient); const notes = [buildNote(60n, owner, storageSlot), buildNote(80n, owner, storageSlot)]; oracle.getNotes.mockResolvedValue(notes); @@ -407,7 +407,7 @@ describe('Private Execution test suite', () => { const balance = 160n; const artifact = getFunctionArtifact(StatefulTestContractArtifact, 'destroy_and_create'); - const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField()); + const storageSlot = computeSlotForMapping(new Fr(1n), owner); const notes = [buildNote(balance, owner, storageSlot)]; oracle.getNotes.mockResolvedValue(notes); @@ -905,7 +905,7 @@ describe('Private Execution test suite', () => { expect(result.newNotes).toHaveLength(1); const noteAndSlot = result.newNotes[0]; - expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField())); + expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner)); expect(noteAndSlot.note.items[0]).toEqual(new Fr(amountToTransfer)); @@ -915,7 +915,7 @@ describe('Private Execution test suite', () => { expect(newCommitments).toHaveLength(1); const commitment = newCommitments[0]; - const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField()); + const storageSlot = computeSlotForMapping(new Fr(1n), owner); const innerNoteHash = await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note); expect(commitment).toEqual(innerNoteHash); @@ -986,7 +986,7 @@ describe('Private Execution test suite', () => { expect(execInsert.newNotes).toHaveLength(1); const noteAndSlot = execInsert.newNotes[0]; - expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField())); + expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner)); expect(noteAndSlot.note.items[0]).toEqual(new Fr(amountToTransfer)); @@ -996,7 +996,7 @@ describe('Private Execution test suite', () => { expect(newCommitments).toHaveLength(1); const commitment = newCommitments[0]; - const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField()); + const storageSlot = computeSlotForMapping(new Fr(1n), owner); const innerNoteHash = await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note); expect(commitment).toEqual(innerNoteHash); @@ -1042,7 +1042,7 @@ describe('Private Execution test suite', () => { expect(result.newNotes).toHaveLength(1); const noteAndSlot = result.newNotes[0]; - expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner.toField())); + expect(noteAndSlot.storageSlot).toEqual(computeSlotForMapping(new Fr(1n), owner)); expect(noteAndSlot.note.items[0]).toEqual(new Fr(amountToTransfer)); @@ -1052,7 +1052,7 @@ describe('Private Execution test suite', () => { expect(newCommitments).toHaveLength(1); const commitment = newCommitments[0]; - const storageSlot = computeSlotForMapping(new Fr(1n), owner.toField()); + const storageSlot = computeSlotForMapping(new Fr(1n), owner); expect(commitment).toEqual( await acirSimulator.computeInnerNoteHash(contractAddress, storageSlot, noteAndSlot.note), ); diff --git a/yarn-project/simulator/src/public/index.test.ts b/yarn-project/simulator/src/public/index.test.ts index 1d1a6a7b2e0..96ae8bf7892 100644 --- a/yarn-project/simulator/src/public/index.test.ts +++ b/yarn-project/simulator/src/public/index.test.ts @@ -88,7 +88,7 @@ describe('ACIR public execution simulator', () => { const execution: PublicExecution = { contractAddress, functionData, args, callContext }; const result = await executor.simulate(execution, GlobalVariables.empty()); - const recipientBalanceStorageSlot = computeSlotForMapping(new Fr(6n), recipient.toField()); + const recipientBalanceStorageSlot = computeSlotForMapping(new Fr(6n), recipient); const totalSupplyStorageSlot = new Fr(4n); const expectedBalance = new Fr(previousBalance.value + mintAmount); @@ -110,7 +110,7 @@ describe('ACIR public execution simulator', () => { ]); const mintersStorageSlot = new Fr(2n); - const isMinterStorageSlot = computeSlotForMapping(mintersStorageSlot, msgSender.toField()); + const isMinterStorageSlot = computeSlotForMapping(mintersStorageSlot, msgSender); // Note: There is only 1 storage read (for the isMinter value) because the other 2 reads get overwritten by // the updates expect(result.contractStorageReads).toEqual([ @@ -152,8 +152,8 @@ describe('ACIR public execution simulator', () => { startSideEffectCounter: 0, }); - recipientStorageSlot = computeSlotForMapping(new Fr(6n), recipient.toField()); - senderStorageSlot = computeSlotForMapping(new Fr(6n), Fr.fromBuffer(sender.toBuffer())); + recipientStorageSlot = computeSlotForMapping(new Fr(6n), recipient); + senderStorageSlot = computeSlotForMapping(new Fr(6n), sender); publicContracts.getBytecode.mockResolvedValue(Buffer.from(transferArtifact.bytecode, 'base64')); diff --git a/yarn-project/simulator/src/utils.ts b/yarn-project/simulator/src/utils.ts index b40f389bac8..a47e4e390ba 100644 --- a/yarn-project/simulator/src/utils.ts +++ b/yarn-project/simulator/src/utils.ts @@ -1,41 +1,18 @@ -import { GrumpkinPrivateKey } from '@aztec/circuits.js'; -import { Grumpkin } from '@aztec/circuits.js/barretenberg'; import { pedersenHash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; -/** - * A point in the format that Aztec.nr uses. - */ -export type NoirPoint = { - /** The x coordinate. */ - x: bigint; - /** The y coordinate. */ - y: bigint; -}; - /** * Computes the resulting storage slot for an entry in a mapping. * @param mappingSlot - The slot of the mapping within state. - * @param owner - The key of the mapping. + * @param key - The key of the mapping. * @returns The slot in the contract storage where the value is stored. */ -export function computeSlotForMapping(mappingSlot: Fr, owner: NoirPoint | Fr) { - const isFr = (owner: NoirPoint | Fr): owner is Fr => typeof (owner as Fr).value === 'bigint'; - const ownerField = isFr(owner) ? owner : new Fr(owner.x); - - return Fr.fromBuffer(pedersenHash([mappingSlot, ownerField].map(f => f.toBuffer()))); -} - -/** - * Computes the public key for a private key. - * @param privateKey - The private key. - * @param grumpkin - The grumpkin instance. - * @returns The public key. - */ -export function toPublicKey(privateKey: GrumpkinPrivateKey, grumpkin: Grumpkin): NoirPoint { - const point = grumpkin.mul(Grumpkin.generator, privateKey); - return { - x: point.x.value, - y: point.y.value, - }; +export function computeSlotForMapping( + mappingSlot: Fr, + key: { + /** Serialize to a field. */ + toField: () => Fr; + }, +) { + return Fr.fromBuffer(pedersenHash([mappingSlot, key.toField()].map(field => field.toBuffer()))); }