From 2cfd43c83f65935b8c17fc8ea12c7d4f5b32ed02 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 13 Mar 2024 14:47:04 +0000 Subject: [PATCH] serialization tests --- .../src/structs/parity/base_parity_inputs.test.ts | 11 +++++++++++ .../src/structs/parity/base_parity_inputs.ts | 11 ++++++++++- .../src/structs/parity/parity_public_inputs.test.ts | 11 +++++++++++ .../src/structs/parity/parity_public_inputs.ts | 7 ++++++- .../src/structs/parity/root_parity_input.test.ts | 11 +++++++++++ .../src/structs/parity/root_parity_input.ts | 7 ++++++- .../src/structs/parity/root_parity_inputs.test.ts | 11 +++++++++++ .../src/structs/parity/root_parity_inputs.ts | 11 ++++++++++- yarn-project/circuits.js/src/tests/factories.ts | 12 ++++++++++++ 9 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 yarn-project/circuits.js/src/structs/parity/base_parity_inputs.test.ts create mode 100644 yarn-project/circuits.js/src/structs/parity/parity_public_inputs.test.ts create mode 100644 yarn-project/circuits.js/src/structs/parity/root_parity_input.test.ts create mode 100644 yarn-project/circuits.js/src/structs/parity/root_parity_inputs.test.ts diff --git a/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.test.ts b/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.test.ts new file mode 100644 index 000000000000..8631ac2b2b0e --- /dev/null +++ b/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.test.ts @@ -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); + }); +}); diff --git a/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.ts b/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.ts index cdb97c2de466..c369424cdc08 100644 --- a/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.ts +++ b/yarn-project/circuits.js/src/structs/parity/base_parity_inputs.ts @@ -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'; @@ -18,4 +18,13 @@ export class BaseParityInputs { const msgs = array.slice(start, end); return new BaseParityInputs(msgs as Tuple); } + + 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)); + } } diff --git a/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.test.ts b/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.test.ts new file mode 100644 index 000000000000..f4ea958145ff --- /dev/null +++ b/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.test.ts @@ -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); + }); +}); diff --git a/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.ts b/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.ts index 7a19c37566a1..7f70c6239efe 100644 --- a/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/parity/parity_public_inputs.ts @@ -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'; @@ -29,4 +29,9 @@ export class ParityPublicInputs { static getFields(fields: FieldsOf) { 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)); + } } diff --git a/yarn-project/circuits.js/src/structs/parity/root_parity_input.test.ts b/yarn-project/circuits.js/src/structs/parity/root_parity_input.test.ts new file mode 100644 index 000000000000..1dd76321e0cd --- /dev/null +++ b/yarn-project/circuits.js/src/structs/parity/root_parity_input.test.ts @@ -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); + }); +}); diff --git a/yarn-project/circuits.js/src/structs/parity/root_parity_input.ts b/yarn-project/circuits.js/src/structs/parity/root_parity_input.ts index d38a6f4b1c7f..1c55e5c83bb4 100644 --- a/yarn-project/circuits.js/src/structs/parity/root_parity_input.ts +++ b/yarn-project/circuits.js/src/structs/parity/root_parity_input.ts @@ -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'; @@ -23,4 +23,9 @@ export class RootParityInput { static getFields(fields: FieldsOf) { 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)); + } } diff --git a/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.test.ts b/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.test.ts new file mode 100644 index 000000000000..97c28baa9fc1 --- /dev/null +++ b/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.test.ts @@ -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); + }); +}); diff --git a/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.ts b/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.ts index 04c0a09a66dc..4a73162a6af3 100644 --- a/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.ts +++ b/yarn-project/circuits.js/src/structs/parity/root_parity_inputs.ts @@ -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'; @@ -8,4 +8,13 @@ export class RootParityInputs { /** Public inputs of children and their proofs. */ public readonly children: Tuple, ) {} + + 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)); + } } diff --git a/yarn-project/circuits.js/src/tests/factories.ts b/yarn-project/circuits.js/src/tests/factories.ts index fc4db48d91d3..7f8f327a87c5 100644 --- a/yarn-project/circuits.js/src/tests/factories.ts +++ b/yarn-project/circuits.js/src/tests/factories.ts @@ -12,6 +12,7 @@ import { AggregationObject, AppendOnlyTreeSnapshot, BaseOrMergeRollupPublicInputs, + BaseParityInputs, BaseRollupInputs, CallContext, CallRequest, @@ -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, @@ -110,6 +113,7 @@ import { ReadRequestContext, RollupTypes, RootParityInput, + RootParityInputs, RootRollupInputs, RootRollupPublicInputs, SideEffect, @@ -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.