diff --git a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts index dd283644826..8270b171ffe 100644 --- a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts +++ b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts @@ -5,6 +5,7 @@ import { L2Block, LogId, NullifierMembershipWitness, + PublicDataWitness, SiblingPath, Tx, TxEffect, @@ -37,6 +38,7 @@ export function createAztecNodeRpcServer(node: AztecNode) { TxEffect, LogId, TxHash, + PublicDataWitness, SiblingPath, }, { Tx, TxReceipt, EncryptedL2BlockL2Logs, UnencryptedL2BlockL2Logs, NullifierMembershipWitness }, diff --git a/yarn-project/circuit-types/src/index.ts b/yarn-project/circuit-types/src/index.ts index fb71e18a5dc..67763d4d6d3 100644 --- a/yarn-project/circuit-types/src/index.ts +++ b/yarn-project/circuit-types/src/index.ts @@ -6,6 +6,7 @@ export * from './l2_block.js'; export * from './body.js'; export * from './l2_block_downloader/index.js'; export * from './l2_block_source.js'; +export * from './public_data_witness.js'; export * from './tx_effect.js'; export * from './logs/index.js'; export * from './merkle_tree_id.js'; diff --git a/yarn-project/circuit-types/src/interfaces/aztec-node.ts b/yarn-project/circuit-types/src/interfaces/aztec-node.ts index fd8b71c1126..d59543943e8 100644 --- a/yarn-project/circuit-types/src/interfaces/aztec-node.ts +++ b/yarn-project/circuit-types/src/interfaces/aztec-node.ts @@ -20,6 +20,7 @@ import { type LogType, } from '../logs/index.js'; import { type MerkleTreeId } from '../merkle_tree_id.js'; +import { type PublicDataWitness } from '../public_data_witness.js'; import { type SiblingPath } from '../sibling_path/index.js'; import { type ProcessOutput, type Tx, type TxHash, type TxReceipt } from '../tx/index.js'; import { type TxEffect } from '../tx_effect.js'; @@ -27,7 +28,6 @@ import { type SequencerConfig } from './configs.js'; import { type L2BlockNumber } from './l2_block_number.js'; import { type NullifierMembershipWitness } from './nullifier_tree.js'; import { type ProverConfig } from './prover-client.js'; -import { type PublicDataWitness } from './public_data_tree.js'; /** * The aztec node. diff --git a/yarn-project/circuit-types/src/interfaces/index.ts b/yarn-project/circuit-types/src/interfaces/index.ts index 71bfeeda4a2..5b13506853c 100644 --- a/yarn-project/circuit-types/src/interfaces/index.ts +++ b/yarn-project/circuit-types/src/interfaces/index.ts @@ -4,7 +4,6 @@ export * from './pxe.js'; export * from './sync-status.js'; export * from './configs.js'; export * from './nullifier_tree.js'; -export * from './public_data_tree.js'; export * from './prover-client.js'; export * from './proving-job.js'; export * from './block-prover.js'; diff --git a/yarn-project/circuit-types/src/interfaces/public_data_tree.ts b/yarn-project/circuit-types/src/interfaces/public_data_tree.ts deleted file mode 100644 index 1ae620154ee..00000000000 --- a/yarn-project/circuit-types/src/interfaces/public_data_tree.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Fr, type PUBLIC_DATA_TREE_HEIGHT, type PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; - -import { type SiblingPath } from '../sibling_path/index.js'; - -/** - * Public data witness. - * @remarks This allows to prove either: - * - That a slot in the public data tree is empty (0 value) if it falls within the range of the leaf. - * - The current value of a slot in the public data tree if it matches exactly the slot of the leaf. - */ -export class PublicDataWitness { - constructor( - /** - * The index of the leaf in the public data tree. - */ - public readonly index: bigint, - /** - * Preimage of a low leaf. All the slots in the range of the leaf are empty, and the current value of the - * leaf slot is stored in the leaf. - */ - public readonly leafPreimage: PublicDataTreeLeafPreimage, - /** - * Sibling path to prove membership of the leaf. - */ - public readonly siblingPath: SiblingPath, - ) {} - - /** - * Returns a field array representation of a public data witness. - * @returns A field array representation of a public data witness. - */ - public toFields(): Fr[] { - return [ - new Fr(this.index), - new Fr(this.leafPreimage.slot), - new Fr(this.leafPreimage.value), - new Fr(this.leafPreimage.nextIndex), - new Fr(this.leafPreimage.nextSlot), - ...this.siblingPath.toFields(), - ]; - } -} diff --git a/yarn-project/circuit-types/src/public_data_witness.test.ts b/yarn-project/circuit-types/src/public_data_witness.test.ts new file mode 100644 index 00000000000..c2b031217d9 --- /dev/null +++ b/yarn-project/circuit-types/src/public_data_witness.test.ts @@ -0,0 +1,31 @@ +import { PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; +import { fr } from '@aztec/circuits.js/testing'; +import { toBufferBE } from '@aztec/foundation/bigint-buffer'; +import { randomInt } from '@aztec/foundation/crypto'; + +import { PublicDataWitness } from './public_data_witness.js'; +import { SiblingPath } from './sibling_path/sibling_path.js'; + +describe('contract_artifact', () => { + it('serializes and deserializes an instance', () => { + const witness = makePublicDataWitness(randomInt(1000000)); + + const deserialized = PublicDataWitness.fromBuffer(witness.toBuffer()); + expect(deserialized).toEqual(witness); + }); +}); + +/** + * Factory function to create a PublicDataWitness based on given seed. + * @param seed - A seed used to derive all parameters. + * @returns A new instance of PublicDataWitness. + */ +function makePublicDataWitness(seed: number): PublicDataWitness { + const leafPreimage = new PublicDataTreeLeafPreimage(fr(seed + 1), fr(seed + 2), fr(seed + 3), BigInt(seed + 4)); + const siblingPath = new SiblingPath( + PUBLIC_DATA_TREE_HEIGHT, + Array.from({ length: PUBLIC_DATA_TREE_HEIGHT }, (_, i) => toBufferBE(BigInt(seed + i + 6), 32)), + ); + + return new PublicDataWitness(BigInt(seed + 5), leafPreimage, siblingPath); +} diff --git a/yarn-project/circuit-types/src/public_data_witness.ts b/yarn-project/circuit-types/src/public_data_witness.ts new file mode 100644 index 00000000000..a8a80a76e0c --- /dev/null +++ b/yarn-project/circuit-types/src/public_data_witness.ts @@ -0,0 +1,79 @@ +import { Fr, PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; +import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; +import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; + +import { SiblingPath } from './sibling_path/sibling_path.js'; + +/** + * Public data witness. + * @remarks This allows to prove either: + * - That a slot in the public data tree is empty (0 value) if it falls within the range of the leaf. + * - The current value of a slot in the public data tree if it matches exactly the slot of the leaf. + */ +export class PublicDataWitness { + constructor( + /** + * The index of the leaf in the public data tree. + */ + public readonly index: bigint, + /** + * Preimage of a low leaf. All the slots in the range of the leaf are empty, and the current value of the + * leaf slot is stored in the leaf. + */ + public readonly leafPreimage: PublicDataTreeLeafPreimage, + /** + * Sibling path to prove membership of the leaf. + */ + public readonly siblingPath: SiblingPath, + ) {} + + /** + * Returns a field array representation of a public data witness. + * @returns A field array representation of a public data witness. + */ + public toFields(): Fr[] { + return [ + new Fr(this.index), + new Fr(this.leafPreimage.slot), + new Fr(this.leafPreimage.value), + new Fr(this.leafPreimage.nextIndex), + new Fr(this.leafPreimage.nextSlot), + ...this.siblingPath.toFields(), + ]; + } + + toBuffer(): Buffer { + return serializeToBuffer([this.index, this.leafPreimage, this.siblingPath]); + } + + /** + * Returns a string representation of the TxEffect object. + */ + toString(): string { + return this.toBuffer().toString('hex'); + } + + /** + * Deserializes an PublicDataWitness object from a buffer. + * @param buf - Buffer or BufferReader to deserialize. + * @returns An instance of PublicDataWitness. + */ + static fromBuffer(buffer: Buffer | BufferReader): PublicDataWitness { + const reader = BufferReader.asReader(buffer); + + return new PublicDataWitness( + toBigIntBE(reader.readBytes(32)), + reader.readObject(PublicDataTreeLeafPreimage), + SiblingPath.fromBuffer(reader.readBytes(4 + 32 * PUBLIC_DATA_TREE_HEIGHT)), + ); + } + + /** + * Deserializes an PublicDataWitness object from a string. + * @param str - String to deserialize. + * @returns An instance of PublicDataWitness. + */ + static fromString(str: string) { + return PublicDataWitness.fromBuffer(Buffer.from(str, 'hex')); + } +}