diff --git a/yarn-project/simulator/src/avm/avm_simulator.test.ts b/yarn-project/simulator/src/avm/avm_simulator.test.ts index a0628806798..d6026089f1c 100644 --- a/yarn-project/simulator/src/avm/avm_simulator.test.ts +++ b/yarn-project/simulator/src/avm/avm_simulator.test.ts @@ -13,7 +13,7 @@ import { strict as assert } from 'assert'; import { isAvmBytecode } from '../public/transitional_adaptors.js'; import { AvmMachineState } from './avm_machine_state.js'; -import { Field, type MemoryValue, TypeTag, Uint8 } from './avm_memory_types.js'; +import { type MemoryValue, TypeTag, type Uint8 } from './avm_memory_types.js'; import { AvmSimulator } from './avm_simulator.js'; import { adjustCalldataIndex, @@ -22,6 +22,8 @@ import { initGlobalVariables, initL1ToL2MessageOracleInput, initMachineState, + randomMemoryBytes, + randomMemoryFields, } from './fixtures/index.js'; import { Add, CalldataCopy, Return } from './opcodes/index.js'; import { encodeToBytecode } from './serialization/bytecode_serialization.js'; @@ -124,19 +126,19 @@ describe('AVM simulator: transpiled Noir contracts', () => { describe.each([ [ 'sha256_hash', - /*input=*/ randomBytes(10), + /*input=*/ randomMemoryBytes(10), /*output=*/ (bytes: Uint8[]) => [...sha256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b)), ], [ 'keccak_hash', - /*input=*/ randomBytes(10), + /*input=*/ randomMemoryBytes(10), /*output=*/ (bytes: Uint8[]) => [...keccak256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b)), ], - ['poseidon2_hash', /*input=*/ randomFields(10), /*output=*/ (fields: Fieldable[]) => [poseidon2Hash(fields)]], - ['pedersen_hash', /*input=*/ randomFields(10), /*output=*/ (fields: Fieldable[]) => [pedersenHash(fields)]], + ['poseidon2_hash', /*input=*/ randomMemoryFields(10), /*output=*/ (fields: Fieldable[]) => [poseidon2Hash(fields)]], + ['pedersen_hash', /*input=*/ randomMemoryFields(10), /*output=*/ (fields: Fieldable[]) => [pedersenHash(fields)]], [ 'pedersen_hash_with_index', - /*input=*/ randomFields(10), + /*input=*/ randomMemoryFields(10), /*output=*/ (fields: Fieldable[]) => [pedersenHash(fields, /*index=*/ 20)], ], ])('Hashes in noir contracts', (name: string, input: MemoryValue[], output: (msg: any[]) => Fr[]) => { @@ -920,10 +922,3 @@ function getAvmNestedCallsTestContractBytecode(functionName: string): Buffer { ); return artifact.bytecode; } - -function randomBytes(length: number): Uint8[] { - return [...Array(length)].map(_ => new Uint8(Math.floor(Math.random() * 255))); -} -function randomFields(length: number): Field[] { - return [...Array(length)].map(_ => new Field(Fr.random())); -} diff --git a/yarn-project/simulator/src/avm/fixtures/index.ts b/yarn-project/simulator/src/avm/fixtures/index.ts index 05a71155031..f73e82f2fac 100644 --- a/yarn-project/simulator/src/avm/fixtures/index.ts +++ b/yarn-project/simulator/src/avm/fixtures/index.ts @@ -17,6 +17,7 @@ import { import { AvmContext } from '../avm_context.js'; import { AvmContextInputs, AvmExecutionEnvironment } from '../avm_execution_environment.js'; import { AvmMachineState } from '../avm_machine_state.js'; +import { Field, Uint8 } from '../avm_memory_types.js'; import { HostStorage } from '../journal/host_storage.js'; import { AvmPersistableStateManager } from '../journal/journal.js'; @@ -136,3 +137,11 @@ export function anyAvmContextInputs() { } return tv; } + +export function randomMemoryBytes(length: number): Uint8[] { + return [...Array(length)].map(_ => new Uint8(Math.floor(Math.random() * 255))); +} + +export function randomMemoryFields(length: number): Field[] { + return [...Array(length)].map(_ => new Field(Fr.random())); +} diff --git a/yarn-project/simulator/src/avm/opcodes/hashing.test.ts b/yarn-project/simulator/src/avm/opcodes/hashing.test.ts index 63229c3a349..d0528225878 100644 --- a/yarn-project/simulator/src/avm/opcodes/hashing.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/hashing.test.ts @@ -1,8 +1,8 @@ import { keccak256, pedersenHash, sha256 } from '@aztec/foundation/crypto'; import { type AvmContext } from '../avm_context.js'; -import { Field, Uint8, Uint32 } from '../avm_memory_types.js'; -import { initContext } from '../fixtures/index.js'; +import { Field, type Uint8, Uint32 } from '../avm_memory_types.js'; +import { initContext, randomMemoryBytes, randomMemoryFields } from '../fixtures/index.js'; import { Addressing, AddressingMode } from './addressing_mode.js'; import { Keccak, Pedersen, Poseidon2, Sha256 } from './hashing.js'; @@ -88,7 +88,7 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - direct', async () => { - const args = [...Array(10)].map(_ => new Uint8(Math.floor(Math.random() * 255))); + const args = randomMemoryBytes(10); const indirect = 0; const messageOffset = 0; const messageSizeOffset = 15; @@ -107,7 +107,7 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - indirect', async () => { - const args = [...Array(10)].map(_ => new Uint8(Math.floor(Math.random() * 255))); + const args = randomMemoryBytes(10); const indirect = new Addressing([ /*dstOffset=*/ AddressingMode.INDIRECT, /*messageOffset*/ AddressingMode.INDIRECT, @@ -157,7 +157,7 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - direct', async () => { - const args = [...Array(10)].map(_ => new Uint8(Math.floor(Math.random() * 255))); + const args = randomMemoryBytes(10); const indirect = 0; const messageOffset = 0; const messageSizeOffset = 15; @@ -176,7 +176,7 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - indirect', async () => { - const args = [...Array(10)].map(_ => new Uint8(Math.floor(Math.random() * 255))); + const args = randomMemoryBytes(10); const indirect = new Addressing([ /*dstOffset=*/ AddressingMode.INDIRECT, /*messageOffset*/ AddressingMode.INDIRECT, @@ -228,10 +228,10 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - direct', async () => { - const args = [new Field(1n), new Field(2n), new Field(3n)]; + const args = randomMemoryFields(10); const messageOffset = 0; - const sizeOffset = 10; - const genIndexOffset = 20; + const sizeOffset = 20; + const genIndexOffset = 30; const indirect = 0; const genIndex = 20; @@ -249,7 +249,7 @@ describe('Hashing Opcodes', () => { }); it('Should hash correctly - indirect', async () => { - const args = [new Field(1n), new Field(2n), new Field(3n)]; + const args = randomMemoryFields(10); const indirect = new Addressing([ /*genIndexOffset=*/ AddressingMode.DIRECT, /*dstOffset=*/ AddressingMode.DIRECT, @@ -257,9 +257,9 @@ describe('Hashing Opcodes', () => { /*messageSizeOffset*/ AddressingMode.INDIRECT, ]).toWire(); const messageOffset = 0; - const sizeOffset = 10; + const sizeOffset = 20; const realLocation = 4; - const realSizeLocation = 20; + const realSizeLocation = 21; const genIndexOffset = 50; context.machineState.memory.set(messageOffset, new Uint32(realLocation));