From 15204cc801c6fbb39bb7ca303e6a4746acc49260 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 24 Apr 2024 17:09:33 +0100 Subject: [PATCH] feat: serialize public kernel private inputs (#5971) Serialize `PublicKernelCircuitPrivateInputs` to and from strings --- ...blic_kernel_circuit_private_inputs.test.ts | 6 ++++ .../public_kernel_circuit_private_inputs.ts | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.test.ts b/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.test.ts index 0240c1b081c..9f783f1a305 100644 --- a/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.test.ts +++ b/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.test.ts @@ -14,4 +14,10 @@ describe('PublicKernelCircuitPrivateInputs', () => { expect(original).toEqual(serialized); expect(original).not.toBe(serialized); }); + + it('serializes to and deserializes from a string', () => { + const original = makePublicKernelCircuitPrivateInputs(123); + const serialized = PublicKernelCircuitPrivateInputs.fromString(original.toString()); + expect(original).toEqual(serialized); + }); }); diff --git a/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.ts b/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.ts index 63f377cd63c..190593832db 100644 --- a/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.ts +++ b/yarn-project/circuits.js/src/structs/kernel/public_kernel_circuit_private_inputs.ts @@ -18,10 +18,27 @@ export class PublicKernelCircuitPrivateInputs { public readonly publicCall: PublicCallData, ) {} + /** + * Serializes the object to a buffer. + * @returns - Buffer representation of the object. + */ toBuffer() { return serializeToBuffer(this.previousKernel, this.publicCall); } + /** + * Serializes the object to a hex string. + * @returns - Hex string representation of the object. + */ + toString() { + return this.toBuffer().toString('hex'); + } + + /** + * Deserializes the object from a buffer. + * @param buffer - Buffer to deserialize. + * @returns - Deserialized object. + */ static fromBuffer(buffer: BufferReader | Buffer) { const reader = BufferReader.asReader(buffer); const previousKernel = reader.readObject(PublicKernelData); @@ -29,6 +46,19 @@ export class PublicKernelCircuitPrivateInputs { return new PublicKernelCircuitPrivateInputs(previousKernel, publicCall); } + /** + * Deserializes the object from a hex string. + * @param str - Hex string to deserialize. + * @returns - Deserialized object. + */ + static fromString(str: string) { + return PublicKernelCircuitPrivateInputs.fromBuffer(Buffer.from(str, 'hex')); + } + + /** + * Clones the object. + * @returns - Cloned object. + */ clone() { return PublicKernelCircuitPrivateInputs.fromBuffer(this.toBuffer()); }