Skip to content

Commit

Permalink
feat: serialize public kernel private inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Apr 24, 2024
1 parent b3fdb3b commit 8de4637
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 8de4637

Please sign in to comment.