Skip to content

Commit

Permalink
chore(avm): more test cleanup (#5771)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro authored Apr 17, 2024
1 parent fd720cc commit 23d0070
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,11 @@ describe('AVM simulator: transpiled Noir contracts', () => {
});

describe.each([
[
'sha256_hash',
/*input=*/ randomMemoryBytes(10),
/*output=*/ (bytes: Uint8[]) => [...sha256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b)),
],
[
'keccak_hash',
/*input=*/ randomMemoryBytes(10),
/*output=*/ (bytes: Uint8[]) => [...keccak256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b)),
],
['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=*/ randomMemoryFields(10),
/*output=*/ (fields: Fieldable[]) => [pedersenHash(fields, /*index=*/ 20)],
],
['sha256_hash', /*input=*/ randomMemoryBytes(10), /*output=*/ sha256FromMemoryBytes],
['keccak_hash', /*input=*/ randomMemoryBytes(10), /*output=*/ keccak256FromMemoryBytes],
['poseidon2_hash', /*input=*/ randomMemoryFields(10), /*output=*/ poseidon2FromMemoryFields],
['pedersen_hash', /*input=*/ randomMemoryFields(10), /*output=*/ pedersenFromMemoryFields],
['pedersen_hash_with_index', /*input=*/ randomMemoryFields(10), /*output=*/ indexedPedersenFromMemoryFields],
])('Hashes in noir contracts', (name: string, input: MemoryValue[], output: (msg: any[]) => Fr[]) => {
it(`Should execute contract function that performs ${name}`, async () => {
const calldata = input.map(e => e.toFr());
Expand Down Expand Up @@ -929,3 +917,23 @@ function getAvmNestedCallsTestContractBytecode(functionName: string): Buffer {
);
return artifact.bytecode;
}

function sha256FromMemoryBytes(bytes: Uint8[]): Fr[] {
return [...sha256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b));
}

function keccak256FromMemoryBytes(bytes: Uint8[]): Fr[] {
return [...keccak256(Buffer.concat(bytes.map(b => b.toBuffer())))].map(b => new Fr(b));
}

function poseidon2FromMemoryFields(fields: Fieldable[]): Fr[] {
return [poseidon2Hash(fields)];
}

function pedersenFromMemoryFields(fields: Fieldable[]): Fr[] {
return [pedersenHash(fields)];
}

function indexedPedersenFromMemoryFields(fields: Fieldable[]): Fr[] {
return [pedersenHash(fields, /*index=*/ 20)];
}

0 comments on commit 23d0070

Please sign in to comment.