Skip to content

Commit

Permalink
serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 13, 2024
1 parent 43d9f40 commit 2cfd43c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { makeBaseParityInputs } from '../../tests/factories.js';
import { BaseParityInputs } from './base_parity_inputs.js';

describe('BaseParityInputs', () => {
it(`serializes a BaseParityInputs to buffer and deserializes it back`, () => {
const expected = makeBaseParityInputs();
const buffer = expected.toBuffer();
const res = BaseParityInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fr } from '@aztec/foundation/fields';
import { Tuple } from '@aztec/foundation/serialize';
import { BufferReader, Tuple, serializeToBuffer } from '@aztec/foundation/serialize';

import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, NUM_MSGS_PER_BASE_PARITY } from '../../constants.gen.js';

Expand All @@ -18,4 +18,13 @@ export class BaseParityInputs {
const msgs = array.slice(start, end);
return new BaseParityInputs(msgs as Tuple<Fr, typeof NUM_MSGS_PER_BASE_PARITY>);
}

toBuffer() {
return serializeToBuffer(this.msgs);
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new BaseParityInputs(reader.readArray(NUM_MSGS_PER_BASE_PARITY, Fr));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { makeParityPublicInputs } from '../../tests/factories.js';
import { ParityPublicInputs } from './parity_public_inputs.js';

describe('ParityPublicInputs', () => {
it(`serializes a ParityPublicInputs to buffer and deserializes it back`, () => {
const expected = makeParityPublicInputs();
const buffer = expected.toBuffer();
const res = ParityPublicInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fr } from '@aztec/foundation/fields';
import { serializeToBuffer } from '@aztec/foundation/serialize';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import { AggregationObject } from '../aggregation_object.js';
Expand Down Expand Up @@ -29,4 +29,9 @@ export class ParityPublicInputs {
static getFields(fields: FieldsOf<ParityPublicInputs>) {
return [fields.aggregationObject, fields.shaRoot, fields.convertedRoot] as const;
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new ParityPublicInputs(reader.readObject(AggregationObject), reader.readBytes(32), reader.readObject(Fr));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { makeRootParityInput } from '../../tests/factories.js';
import { RootParityInput } from './root_parity_input.js';

describe('RootParityInput', () => {
it(`serializes a RootParityInput to buffer and deserializes it back`, () => {
const expected = makeRootParityInput();
const buffer = expected.toBuffer();
const res = RootParityInput.fromBuffer(buffer);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serializeToBuffer } from '@aztec/foundation/serialize';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import { Proof } from '../proof.js';
Expand All @@ -23,4 +23,9 @@ export class RootParityInput {
static getFields(fields: FieldsOf<RootParityInput>) {
return [fields.proof, fields.publicInputs] as const;
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new RootParityInput(reader.readObject(Proof), reader.readObject(ParityPublicInputs));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { makeRootParityInputs } from '../../tests/factories.js';
import { RootParityInputs } from './root_parity_inputs.js';

describe('RootParityInputs', () => {
it(`serializes a RootParityInputs to buffer and deserializes it back`, () => {
const expected = makeRootParityInputs();
const buffer = expected.toBuffer();
const res = RootParityInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tuple } from '@aztec/foundation/serialize';
import { BufferReader, Tuple, serializeToBuffer } from '@aztec/foundation/serialize';

import { NUM_BASE_PARITY_PER_ROOT_PARITY } from '../../constants.gen.js';
import { RootParityInput } from './root_parity_input.js';
Expand All @@ -8,4 +8,13 @@ export class RootParityInputs {
/** Public inputs of children and their proofs. */
public readonly children: Tuple<RootParityInput, typeof NUM_BASE_PARITY_PER_ROOT_PARITY>,
) {}

toBuffer() {
return serializeToBuffer(this.children);
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new RootParityInputs(reader.readArray(NUM_BASE_PARITY_PER_ROOT_PARITY, RootParityInput));
}
}
12 changes: 12 additions & 0 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AggregationObject,
AppendOnlyTreeSnapshot,
BaseOrMergeRollupPublicInputs,
BaseParityInputs,
BaseRollupInputs,
CallContext,
CallRequest,
Expand Down Expand Up @@ -71,7 +72,9 @@ import {
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT,
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
NUM_BASE_PARITY_PER_ROOT_PARITY,
NUM_FIELDS_PER_SHA256,
NUM_MSGS_PER_BASE_PARITY,
NoteHashReadRequestMembershipWitness,
NullifierKeyValidationRequest,
NullifierKeyValidationRequestContext,
Expand Down Expand Up @@ -110,6 +113,7 @@ import {
ReadRequestContext,
RollupTypes,
RootParityInput,
RootParityInputs,
RootRollupInputs,
RootRollupPublicInputs,
SideEffect,
Expand Down Expand Up @@ -1046,6 +1050,14 @@ export function makeParityPublicInputs(seed = 0): ParityPublicInputs {
);
}

export function makeBaseParityInputs(seed = 0): BaseParityInputs {
return new BaseParityInputs(makeTuple(NUM_MSGS_PER_BASE_PARITY, fr, seed + 0x3000));
}

export function makeRootParityInputs(seed = 0): RootParityInputs {
return new RootParityInputs(makeTuple(NUM_BASE_PARITY_PER_ROOT_PARITY, makeRootParityInput, seed + 0x4000));
}

/**
* Makes root rollup public inputs.
* @param seed - The seed to use for generating the root rollup public inputs.
Expand Down

0 comments on commit 2cfd43c

Please sign in to comment.