Skip to content

Commit

Permalink
feat: add serialisation methods (#5749)
Browse files Browse the repository at this point in the history
Adds `toString`/`toBuffer`/`fromString`/`fromBuffer` to a couple of
classes that didn't have them. Also adds tests for the new functions.
This is needed for future work on proving where proving requests could
be sent over the network.
  • Loading branch information
alexghr authored Apr 15, 2024
1 parent 2ae0ee5 commit 20d290c
Show file tree
Hide file tree
Showing 21 changed files with 428 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ describe('BaseParityInputs', () => {
const res = BaseParityInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it(`serializes a BaseParityInputs to hex string and deserializes it back`, () => {
const expected = makeBaseParityInputs();
const str = expected.toString();
const res = BaseParityInputs.fromString(str);
expect(res).toEqual(expected);
});
});
19 changes: 19 additions & 0 deletions yarn-project/circuits.js/src/structs/parity/base_parity_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,31 @@ export class BaseParityInputs {
return new BaseParityInputs(msgs as Tuple<Fr, typeof NUM_MSGS_PER_BASE_PARITY>);
}

/** Serializes the inputs to a buffer. */
toBuffer() {
return serializeToBuffer(this.msgs);
}

/** Serializes the inputs to a hex string. */
toString() {
return this.toBuffer().toString('hex');
}

/**
* Deserializes the inputs from a buffer.
* @param buffer - The buffer to deserialize from.
*/
static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new BaseParityInputs(reader.readArray(NUM_MSGS_PER_BASE_PARITY, Fr));
}

/**
* Deserializes the inputs from a hex string.
* @param str - The hex string to deserialize from.
* @returns - The deserialized inputs.
*/
static fromString(str: string) {
return BaseParityInputs.fromBuffer(Buffer.from(str, 'hex'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ describe('ParityPublicInputs', () => {
const res = ParityPublicInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it(`serializes a ParityPublicInputs to hex string and deserializes it back`, () => {
const expected = makeParityPublicInputs();
const str = expected.toString();
const res = ParityPublicInputs.fromString(str);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,56 @@ export class ParityPublicInputs {
}
}

/**
* Serializes the inputs to a buffer.
* @returns The inputs serialized to a buffer.
*/
toBuffer() {
return serializeToBuffer(...ParityPublicInputs.getFields(this));
}

/**
* Serializes the inputs to a hex string.
* @returns The inputs serialized to a hex string.
*/
toString() {
return this.toBuffer().toString('hex');
}

/**
* Creates a new ParityPublicInputs instance from the given fields.
* @param fields - The fields to create the instance from.
* @returns The instance.
*/
static from(fields: FieldsOf<ParityPublicInputs>): ParityPublicInputs {
return new ParityPublicInputs(...ParityPublicInputs.getFields(fields));
}

/**
* Extracts the fields from the given instance.
* @param fields - The instance to get the fields from.
* @returns The instance fields.
*/
static getFields(fields: FieldsOf<ParityPublicInputs>) {
return [fields.aggregationObject, fields.shaRoot, fields.convertedRoot] as const;
}

/**
* Deserializes the inputs from a buffer.
* @param buffer - The buffer to deserialize from.
* @returns A new ParityPublicInputs instance.
*/
static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new ParityPublicInputs(reader.readObject(AggregationObject), reader.readObject(Fr), reader.readObject(Fr));
}

/**
* Deserializes the inputs from a hex string.
* @param str - The hex string to deserialize from.
* @returns A new ParityPublicInputs instance.
*/
static fromString(str: string) {
return ParityPublicInputs.fromBuffer(Buffer.from(str, 'hex'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ describe('RootParityInputs', () => {
const res = RootParityInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it(`serializes a RootParityInputs to hex string and deserializes it back`, () => {
const expected = makeRootParityInputs();
const str = expected.toString();
const res = RootParityInputs.fromString(str);
expect(res).toEqual(expected);
});
});
26 changes: 26 additions & 0 deletions yarn-project/circuits.js/src/structs/parity/root_parity_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@ export class RootParityInputs {
public readonly children: Tuple<RootParityInput, typeof NUM_BASE_PARITY_PER_ROOT_PARITY>,
) {}

/**
* Serializes the inputs to a buffer.
* @returns The inputs serialized to a buffer.
*/
toBuffer() {
return serializeToBuffer(this.children);
}

/**
* Serializes the inputs to a hex string.
* @returns The instance serialized to a hex string.
*/
toString() {
return this.toBuffer().toString('hex');
}

/**
* Deserializes the inputs from a buffer.
* @param buffer - The buffer to deserialize from.
* @returns A new RootParityInputs instance.
*/
static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new RootParityInputs(reader.readArray(NUM_BASE_PARITY_PER_ROOT_PARITY, RootParityInput));
}

/**
* Deserializes the inputs from a hex string.
* @param str - A hex string to deserialize from.
* @returns A new RootParityInputs instance.
*/
static fromString(str: string) {
return RootParityInputs.fromBuffer(Buffer.from(str, 'hex'));
}
}
18 changes: 18 additions & 0 deletions yarn-project/circuits.js/src/structs/proof.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { makeProof } from '../tests/factories.js';
import { Proof } from './proof.js';

describe('Proof', () => {
it('serializes to buffer and deserializes it back', () => {
const expected = makeProof();
const buffer = expected.toBuffer();
const res = Proof.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it('serializes to hex string and deserializes it back', () => {
const expected = makeProof();
const str = expected.toString();
const res = Proof.fromString(str);
expect(res).toEqual(expected);
});
});
17 changes: 17 additions & 0 deletions yarn-project/circuits.js/src/structs/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ export class Proof {
public toBuffer() {
return serializeToBuffer(this.buffer.length, this.buffer);
}

/**
* Serialize the Proof instance to a hex string.
* @returns The hex string representation of the proof data.
*/
public toString() {
return this.toBuffer().toString('hex');
}

/**
* Deserialize a Proof instance from a hex string.
* @param str - A hex string to deserialize from.
* @returns - A new Proof instance.
*/
static fromString(str: string) {
return Proof.fromBuffer(Buffer.from(str, 'hex'));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ describe('BaseRollupPublicInputs', () => {
const res = BaseOrMergeRollupPublicInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it(`serializes to hex string and deserializes it back`, () => {
const expected = makeBaseOrMergeRollupPublicInputs();
const str = expected.toString();
const res = BaseOrMergeRollupPublicInputs.fromString(str);
expect(res).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ export class BaseOrMergeRollupPublicInputs {
this.outHash,
);
}

/**
* Serialize this as a hex string.
* @returns - The hex string.
*/
toString() {
return this.toBuffer().toString('hex');
}

/**
* Deserializes from a hex string.
* @param str - A hex string to deserialize from.
* @returns A new BaseOrMergeRollupPublicInputs instance.
*/
static fromString(str: string) {
return BaseOrMergeRollupPublicInputs.fromBuffer(Buffer.from(str, 'hex'));
}
}
18 changes: 18 additions & 0 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { makeBaseRollupInputs } from '../../tests/factories.js';
import { BaseRollupInputs } from './base_rollup.js';

describe('BaseRollupInputs', () => {
it('serializes to buffer and deserializes it back', () => {
const expected = makeBaseRollupInputs();
const buffer = expected.toBuffer();
const res = BaseRollupInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it('serializes to hex string and deserializes it back', () => {
const expected = makeBaseRollupInputs();
const str = expected.toString();
const res = BaseRollupInputs.fromString(str);
expect(res).toEqual(expected);
});
});
57 changes: 50 additions & 7 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { BufferReader, type Tuple, serializeToBuffer } from '@aztec/foundation/s
import { type FieldsOf } from '@aztec/foundation/types';

import {
type ARCHIVE_HEIGHT,
type MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
type PUBLIC_DATA_TREE_HEIGHT,
ARCHIVE_HEIGHT,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
PUBLIC_DATA_TREE_HEIGHT,
} from '../../constants.gen.js';
import { GlobalVariables } from '../global_variables.js';
import { type KernelData } from '../kernel/kernel_data.js';
import { type MembershipWitness } from '../membership_witness.js';
import { type PartialStateReference } from '../partial_state_reference.js';
import { KernelData } from '../kernel/kernel_data.js';
import { MembershipWitness } from '../membership_witness.js';
import { PartialStateReference } from '../partial_state_reference.js';
import { type UInt32 } from '../shared.js';
import { AppendOnlyTreeSnapshot } from './append_only_tree_snapshot.js';
import { NullifierLeaf, NullifierLeafPreimage } from './nullifier_leaf/index.js';
import { PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from './public_data_leaf/index.js';
import { type StateDiffHints } from './state_diff_hints.js';
import { StateDiffHints } from './state_diff_hints.js';

export { NullifierLeaf, NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage };

Expand Down Expand Up @@ -147,7 +147,50 @@ export class BaseRollupInputs {
] as const;
}

/**
* Serializes the inputs to a buffer.
* @returns The inputs serialized to a buffer.
*/
toBuffer() {
return serializeToBuffer(...BaseRollupInputs.getFields(this));
}

/**
* Serializes the inputs to a hex string.
* @returns The instance serialized to a hex string.
*/
toString() {
return this.toBuffer().toString('hex');
}

/**
* Deserializes the inputs from a buffer.
* @param buffer - The buffer to deserialize from.
* @returns A new BaseRollupInputs instance.
*/
static fromBuffer(buffer: Buffer | BufferReader): BaseRollupInputs {
const reader = BufferReader.asReader(buffer);
return new BaseRollupInputs(
reader.readObject(KernelData),
reader.readObject(PartialStateReference),
reader.readObject(StateDiffHints),
reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataTreeLeaf),
reader.readNumbers(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX),
reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataTreeLeafPreimage),
reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, {
fromBuffer: buffer => MembershipWitness.fromBuffer(buffer, PUBLIC_DATA_TREE_HEIGHT),
}),
MembershipWitness.fromBuffer(reader, ARCHIVE_HEIGHT),
reader.readObject(ConstantRollupData),
);
}

/**
* Deserializes the inputs from a hex string.
* @param str - A hex string to deserialize from.
* @returns A new BaseRollupInputs instance.
*/
static fromString(str: string) {
return BaseRollupInputs.fromBuffer(Buffer.from(str, 'hex'));
}
}
18 changes: 18 additions & 0 deletions yarn-project/circuits.js/src/structs/rollup/merge_rollup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { makeMergeRollupInputs } from '../../tests/factories.js';
import { MergeRollupInputs } from './merge_rollup.js';

describe('MergeRollupInputs', () => {
it('serializes to buffer and deserializes it back', () => {
const expected = makeMergeRollupInputs();
const buffer = expected.toBuffer();
const res = MergeRollupInputs.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it('serializes to hex string and deserializes it back', () => {
const expected = makeMergeRollupInputs();
const str = expected.toString();
const res = MergeRollupInputs.fromString(str);
expect(res).toEqual(expected);
});
});
Loading

0 comments on commit 20d290c

Please sign in to comment.