Skip to content

Commit

Permalink
impl serde
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Feb 9, 2024
1 parent 41e9079 commit 8f91a1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
26 changes: 26 additions & 0 deletions yarn-project/simulator/src/avm/opcodes/hashing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ describe('Hashing Opcodes', () => {
});

describe('Poseidon2', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Poseidon2.opcode, // opcode
...Buffer.from('12345678', 'hex'), // destOffset
...Buffer.from('23456789', 'hex'), // hashOffset
...Buffer.from('3456789a', 'hex'), // hashSize
]);
const inst = new Poseidon2(/*destOffset=*/ 0x12345678, /*hashOffset=*/ 0x23456789, /*hashSize=*/ 0x3456789a);

expect(Poseidon2.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});

it('Should hash correctly', async () => {
const args = [new Field(1n), new Field(2n), new Field(3n)];
const hashOffset = 0;
Expand All @@ -30,6 +43,19 @@ describe('Hashing Opcodes', () => {
});

describe('Keccak', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Keccak.opcode, // opcode
...Buffer.from('12345678', 'hex'), // destOffset
...Buffer.from('23456789', 'hex'), // hashOffset
...Buffer.from('3456789a', 'hex'), // hashSize
]);
const inst = new Keccak(/*destOffset=*/ 0x12345678, /*hashOffset=*/ 0x23456789, /*hashSize=*/ 0x3456789a);

expect(Keccak.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});

it('Should hash correctly', async () => {
const args = [new Field(1n), new Field(2n), new Field(3n)];
const hashOffset = 0;
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/avm/opcodes/hashing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { keccak, poseidonHash } from '@aztec/foundation/crypto';

import { AvmContext } from '../avm_context.js';
import { Field } from '../avm_memory_types.js';
import { OperandType } from '../serialization/instruction_serialization.js';
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
import { Instruction } from './instruction.js';

export class Poseidon2 extends Instruction {
static type: string = 'POSEIDON2';
static numberOfOperands = 2;
static readonly opcode: Opcode = Opcode.POSEIDON;

// Informs (de)serialization. See Instruction.deserialize.
static readonly wireFormat: OperandType[] = [
Expand Down Expand Up @@ -42,7 +42,7 @@ export class Poseidon2 extends Instruction {

export class Keccak extends Instruction {
static type: string = 'KECCAK';
static numberOfOperands = 2;
static readonly opcode: Opcode = Opcode.KECCAK;

// Informs (de)serialization. See Instruction.deserialize.
static readonly wireFormat: OperandType[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Keccak, Poseidon2 } from '../opcodes/hashing.js';
import {
Add,
Address,
Expand Down Expand Up @@ -126,8 +127,8 @@ const INSTRUCTION_SET = () =>
[Revert.opcode, Revert],

// //// Gadgets
// //[Keccak.opcode, Keccak],
// //[Poseidon.opcode, Poseidon],
[Keccak.opcode, Keccak],
[Poseidon2.opcode, Poseidon2],
]);

interface Serializable {
Expand Down

0 comments on commit 8f91a1b

Please sign in to comment.