From b19fb502ed42e0df6c36d6f7e00aefea5e1c72e5 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 7 May 2024 14:31:12 +0000 Subject: [PATCH 1/4] fix: registering PublicDataWitness in JsonRpcServer --- .../src/interfaces/aztec-node.ts | 2 +- .../circuit-types/src/interfaces/index.ts | 2 +- ...ic_data_tree.ts => public_data_witness.ts} | 43 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) rename yarn-project/circuit-types/src/interfaces/{public_data_tree.ts => public_data_witness.ts} (50%) diff --git a/yarn-project/circuit-types/src/interfaces/aztec-node.ts b/yarn-project/circuit-types/src/interfaces/aztec-node.ts index fd8b71c1126..95806f7cf3f 100644 --- a/yarn-project/circuit-types/src/interfaces/aztec-node.ts +++ b/yarn-project/circuit-types/src/interfaces/aztec-node.ts @@ -27,7 +27,7 @@ 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'; +import { type PublicDataWitness } from './public_data_witness.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..9788806e620 100644 --- a/yarn-project/circuit-types/src/interfaces/index.ts +++ b/yarn-project/circuit-types/src/interfaces/index.ts @@ -4,7 +4,7 @@ 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 './public_data_witness.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_witness.ts similarity index 50% rename from yarn-project/circuit-types/src/interfaces/public_data_tree.ts rename to yarn-project/circuit-types/src/interfaces/public_data_witness.ts index 1ae620154ee..5ce0f40ee31 100644 --- a/yarn-project/circuit-types/src/interfaces/public_data_tree.ts +++ b/yarn-project/circuit-types/src/interfaces/public_data_witness.ts @@ -1,6 +1,8 @@ -import { Fr, type PUBLIC_DATA_TREE_HEIGHT, type PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; +import { Fr, type PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; import { type SiblingPath } from '../sibling_path/index.js'; +import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; +import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; /** * Public data witness. @@ -39,4 +41,43 @@ export class PublicDataWitness { ...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 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), + reader.readObject(SiblingPath), + ); + } + + /** + * 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')); + } } From 41d19a963350663fe5b08aef37bfe4b9bb8f17a8 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 7 May 2024 14:58:45 +0000 Subject: [PATCH 2/4] WIP --- .../src/aztec-node/http_rpc_server.ts | 2 ++ .../src/interfaces/public_data_witness.ts | 18 +++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) 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/interfaces/public_data_witness.ts b/yarn-project/circuit-types/src/interfaces/public_data_witness.ts index 5ce0f40ee31..0722fb3cc1c 100644 --- a/yarn-project/circuit-types/src/interfaces/public_data_witness.ts +++ b/yarn-project/circuit-types/src/interfaces/public_data_witness.ts @@ -1,8 +1,8 @@ -import { Fr, type PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; - -import { type SiblingPath } from '../sibling_path/index.js'; -import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; +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/index.js'; /** * Public data witness. @@ -43,11 +43,7 @@ export class PublicDataWitness { } toBuffer(): Buffer { - return serializeToBuffer([ - this.index, - this.leafPreimage, - this.siblingPath, - ]); + return serializeToBuffer([this.index, this.leafPreimage, this.siblingPath]); } /** @@ -59,7 +55,7 @@ export class PublicDataWitness { /** * Deserializes an PublicDataWitness object from a buffer. - * @param buf - Buffer to deserialize. + * @param buf - Buffer or BufferReader to deserialize. * @returns An instance of PublicDataWitness. */ static fromBuffer(buffer: Buffer | BufferReader): PublicDataWitness { @@ -68,7 +64,7 @@ export class PublicDataWitness { return new PublicDataWitness( toBigIntBE(reader.readBytes(32)), reader.readObject(PublicDataTreeLeafPreimage), - reader.readObject(SiblingPath), + SiblingPath.fromBuffer(reader.readBytes(4 + 32 * PUBLIC_DATA_TREE_HEIGHT)), ); } From c2f52000e8f40ab0a2b6720276418b46e8c642cd Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 7 May 2024 15:19:32 +0000 Subject: [PATCH 3/4] test --- .../interfaces/public_data_witness.test.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 yarn-project/circuit-types/src/interfaces/public_data_witness.test.ts diff --git a/yarn-project/circuit-types/src/interfaces/public_data_witness.test.ts b/yarn-project/circuit-types/src/interfaces/public_data_witness.test.ts new file mode 100644 index 00000000000..76ddf6e0b52 --- /dev/null +++ b/yarn-project/circuit-types/src/interfaces/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 { SiblingPath } from '../sibling_path/sibling_path.js'; +import { PublicDataWitness } from './public_data_witness.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); +} From dac515df59aa6566ffe6a998e7ff20d3582941c5 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 7 May 2024 15:26:01 +0000 Subject: [PATCH 4/4] moving witness out of interfaces --- yarn-project/circuit-types/src/index.ts | 1 + yarn-project/circuit-types/src/interfaces/aztec-node.ts | 2 +- yarn-project/circuit-types/src/interfaces/index.ts | 1 - .../src/{interfaces => }/public_data_witness.test.ts | 2 +- .../circuit-types/src/{interfaces => }/public_data_witness.ts | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename yarn-project/circuit-types/src/{interfaces => }/public_data_witness.test.ts (95%) rename yarn-project/circuit-types/src/{interfaces => }/public_data_witness.ts (97%) 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 95806f7cf3f..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_witness.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 9788806e620..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_witness.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_witness.test.ts b/yarn-project/circuit-types/src/public_data_witness.test.ts similarity index 95% rename from yarn-project/circuit-types/src/interfaces/public_data_witness.test.ts rename to yarn-project/circuit-types/src/public_data_witness.test.ts index 76ddf6e0b52..c2b031217d9 100644 --- a/yarn-project/circuit-types/src/interfaces/public_data_witness.test.ts +++ b/yarn-project/circuit-types/src/public_data_witness.test.ts @@ -3,8 +3,8 @@ import { fr } from '@aztec/circuits.js/testing'; import { toBufferBE } from '@aztec/foundation/bigint-buffer'; import { randomInt } from '@aztec/foundation/crypto'; -import { SiblingPath } from '../sibling_path/sibling_path.js'; import { PublicDataWitness } from './public_data_witness.js'; +import { SiblingPath } from './sibling_path/sibling_path.js'; describe('contract_artifact', () => { it('serializes and deserializes an instance', () => { diff --git a/yarn-project/circuit-types/src/interfaces/public_data_witness.ts b/yarn-project/circuit-types/src/public_data_witness.ts similarity index 97% rename from yarn-project/circuit-types/src/interfaces/public_data_witness.ts rename to yarn-project/circuit-types/src/public_data_witness.ts index 0722fb3cc1c..a8a80a76e0c 100644 --- a/yarn-project/circuit-types/src/interfaces/public_data_witness.ts +++ b/yarn-project/circuit-types/src/public_data_witness.ts @@ -2,7 +2,7 @@ import { Fr, PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/ import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; -import { SiblingPath } from '../sibling_path/index.js'; +import { SiblingPath } from './sibling_path/sibling_path.js'; /** * Public data witness.