Skip to content

Commit

Permalink
chore(avm): hashing tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Apr 12, 2024
1 parent cac9cba commit c765d83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
21 changes: 8 additions & 13 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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[]) => {
Expand Down Expand Up @@ -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()));
}
9 changes: 9 additions & 0 deletions yarn-project/simulator/src/avm/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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()));
}
24 changes: 12 additions & 12 deletions yarn-project/simulator/src/avm/opcodes/hashing.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -249,17 +249,17 @@ 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,
/*messageOffset*/ AddressingMode.INDIRECT,
/*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));
Expand Down

0 comments on commit c765d83

Please sign in to comment.