Skip to content

Commit

Permalink
fix: registering PublicDataWitness in JsonRpcServer (#6243)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored May 7, 2024
1 parent 02d3d17 commit e8c4455
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 44 deletions.
2 changes: 2 additions & 0 deletions yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
L2Block,
LogId,
NullifierMembershipWitness,
PublicDataWitness,
SiblingPath,
Tx,
TxEffect,
Expand Down Expand Up @@ -37,6 +38,7 @@ export function createAztecNodeRpcServer(node: AztecNode) {
TxEffect,
LogId,
TxHash,
PublicDataWitness,
SiblingPath,
},
{ Tx, TxReceipt, EncryptedL2BlockL2Logs, UnencryptedL2BlockL2Logs, NullifierMembershipWitness },
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuit-types/src/interfaces/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ 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';
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.
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuit-types/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
42 changes: 0 additions & 42 deletions yarn-project/circuit-types/src/interfaces/public_data_tree.ts

This file was deleted.

31 changes: 31 additions & 0 deletions yarn-project/circuit-types/src/public_data_witness.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
79 changes: 79 additions & 0 deletions yarn-project/circuit-types/src/public_data_witness.ts
Original file line number Diff line number Diff line change
@@ -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<typeof PUBLIC_DATA_TREE_HEIGHT>,
) {}

/**
* 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'));
}
}

0 comments on commit e8c4455

Please sign in to comment.