Skip to content

Commit

Permalink
feat: serialize public kernel private inputs (#5971)
Browse files Browse the repository at this point in the history
Serialize `PublicKernelCircuitPrivateInputs` to and from strings
  • Loading branch information
alexghr authored and TomAFrench committed Apr 24, 2024
1 parent 9797eda commit 15204cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,47 @@ 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);
const publicCall = reader.readObject(PublicCallData);
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());
}
Expand Down

0 comments on commit 15204cc

Please sign in to comment.