Skip to content

Commit

Permalink
serialization cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 28, 2024
1 parent 88444a5 commit c97bbca
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 36 deletions.
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/contract_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EthAddress } from '@aztec/foundation/eth-address';
import {
BufferReader,
numToInt32BE,
serializeBufferArrayToVector,
serializeArrayOfBufferableToVector,
serializeToBuffer,
} from '@aztec/foundation/serialize';
import { ContractClass, ContractClassPublic, ContractInstanceWithAddress } from '@aztec/types/contracts';
Expand Down Expand Up @@ -158,7 +158,7 @@ export class ExtendedContractData {
/** Public key hash of the contract. */
public readonly publicKeyHash: Fr,
) {
this.bytecode = serializeBufferArrayToVector(publicFunctions.map(fn => fn.toBuffer()));
this.bytecode = serializeArrayOfBufferableToVector(publicFunctions.map(fn => fn.toBuffer()));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/sibling_path/sibling_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Tuple,
assertLength,
deserializeArrayFromVector,
serializeBufferArrayToVector,
serializeArrayOfBufferableToVector,
} from '@aztec/foundation/serialize';
import { Hasher } from '@aztec/types/interfaces';

Expand Down Expand Up @@ -61,7 +61,7 @@ export class SiblingPath<N extends number> {
* @returns The buffer representation of this object.
*/
public toBuffer(): Buffer {
return serializeBufferArrayToVector(this.data);
return serializeArrayOfBufferableToVector(this.data);
}

/**
Expand Down
26 changes: 6 additions & 20 deletions yarn-project/circuit-types/src/tx_effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { makeTuple } from '@aztec/foundation/array';
import { padArrayEnd } from '@aztec/foundation/collection';
import { sha256 } from '@aztec/foundation/crypto';
import { BufferReader, Tuple } from '@aztec/foundation/serialize';
import { BufferReader, Tuple, serializeArrayOfBufferableToVector } from '@aztec/foundation/serialize';

export class TxEffect {
constructor(
Expand Down Expand Up @@ -46,20 +46,6 @@ export class TxEffect {
) {}

toBuffer(): Buffer {
// TODO(benesjan): move to foundation package?
const serializeAndPrefixWithUint8 = (
arr: {
/**
* Serialize to a buffer.
*/
toBuffer: () => Buffer;
}[],
) => {
const length = Buffer.alloc(1);
length.writeUInt8(arr.length);
return Buffer.concat([length, ...arr.map(x => x.toBuffer())]);
};

const nonZeroNoteHashes = this.newNoteHashes.filter(h => !h.isZero());
const nonZeroNullifiers = this.newNullifiers.filter(h => !h.isZero());
const nonZeroL2ToL1Msgs = this.newL2ToL1Msgs.filter(h => !h.isZero());
Expand All @@ -68,11 +54,11 @@ export class TxEffect {
const nonZeroContractData = this.contractData.filter(h => !h.isEmpty());

return Buffer.concat([
serializeAndPrefixWithUint8(nonZeroNoteHashes),
serializeAndPrefixWithUint8(nonZeroNullifiers),
serializeAndPrefixWithUint8(nonZeroL2ToL1Msgs),
serializeAndPrefixWithUint8(nonZeroPublicDataWrites),
serializeAndPrefixWithUint8(nonZeroContractLeaves),
serializeArrayOfBufferableToVector(nonZeroNoteHashes, 1),
serializeArrayOfBufferableToVector(nonZeroNullifiers, 1),
serializeArrayOfBufferableToVector(nonZeroL2ToL1Msgs, 1),
serializeArrayOfBufferableToVector(nonZeroPublicDataWrites, 1),
serializeArrayOfBufferableToVector(nonZeroContractLeaves, 1),
// We don't prefix the contract data with the length because we already have that info before contract leaves
...nonZeroContractData.map(x => x.toBuffer()),
this.encryptedLogs.toBuffer(),
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/contract/public_bytecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FunctionSelector } from '@aztec/foundation/abi';
import {
BufferReader,
numToInt32BE,
serializeBufferArrayToVector,
serializeArrayOfBufferableToVector,
serializeToBuffer,
} from '@aztec/foundation/serialize';
import { ContractClass } from '@aztec/types/contracts';
Expand All @@ -14,7 +14,7 @@ import { FUNCTION_SELECTOR_NUM_BYTES } from '../constants.gen.js';
* @remarks This function should no longer be necessary once we have a single bytecode per contract.
*/
export function packBytecode(publicFns: ContractClass['publicFunctions']): Buffer {
return serializeBufferArrayToVector(
return serializeArrayOfBufferableToVector(
publicFns.map(fn => serializeToBuffer(fn.selector, fn.isInternal, numToInt32BE(fn.bytecode.length), fn.bytecode)),
);
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/foundation/src/serialize/buffer_reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { jest } from '@jest/globals';
import { randomBytes } from '../crypto/index.js';
import { Fq, Fr } from '../fields/fields.js';
import { BufferReader } from './buffer_reader.js';
import { serializeBufferArrayToVector } from './serialize.js';
import { serializeArrayOfBufferableToVector } from './serialize.js';

const ARRAY = Array.from(Array(32)).map((_, idx) => (idx % 2 === 0 ? 0 : 1));
const BUFFER = Buffer.from(ARRAY);
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('buffer reader', () => {
uintBuf.writeUInt32BE(num, 0);
return uintBuf;
});
const uintArrVec = serializeBufferArrayToVector(uintBufArr);
const uintArrVec = serializeArrayOfBufferableToVector(uintBufArr);
vectorBufferReader = new BufferReader(uintArrVec);
});

Expand Down
1 change: 0 additions & 1 deletion yarn-project/foundation/src/serialize/buffer_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ export class BufferReader {
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
* @returns An array of deserialized elements of type T.
*/
// TODO(benesjan): This name is quite inconsistent with readVector. Rename readVector?
public readVectorUint8Prefix<T>(itemDeserializer: {
/**
* A method to deserialize data from a buffer.
Expand Down
21 changes: 14 additions & 7 deletions yarn-project/foundation/src/serialize/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import { numToUInt32BE } from './free_funcs.js';

/**
* For serializing an array of fixed length buffers.
* TODO move to foundation pkg.
* @param arr - Array of buffers.
* @param arr - Array of bufferable.
* @param prefixLength - The length of the prefix (denominated in bytes).
* @returns The serialized buffers.
*/
export function serializeBufferArrayToVector(arr: Buffer[]): Buffer {
const lengthBuf = Buffer.alloc(4);
lengthBuf.writeUInt32BE(arr.length, 0);
export function serializeArrayOfBufferableToVector(objs: Bufferable[], prefixLength = 4): Buffer {
const arr = serializeToBufferArray(objs);
let lengthBuf: Buffer;
if (prefixLength === 1) {
lengthBuf = Buffer.alloc(1);
lengthBuf.writeUInt8(arr.length, 0);
} else if (prefixLength === 4) {
lengthBuf = Buffer.alloc(4);
lengthBuf.writeUInt32BE(arr.length, 0);
} else {
throw new Error(`Unsupported prefix length. Got ${prefixLength}, expected 1 or 4`);
}
return Buffer.concat([lengthBuf, ...arr]);
}

Expand All @@ -37,8 +46,6 @@ type DeserializeFn<T> = (
* @param vector - The vector to deserialize.
* @param offset - The position in the vector to start deserializing from.
* @returns Deserialized array and how many bytes we advanced by.
*
* TODO: move to foundation pkg.
*/
export function deserializeArrayFromVector<T>(
deserialize: DeserializeFn<T>,
Expand Down

0 comments on commit c97bbca

Please sign in to comment.