Skip to content

Commit

Permalink
Compress public bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Feb 14, 2024
1 parent e58136c commit 11aece0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions yarn-project/circuits.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@aztec/types": "workspace:^",
"eslint": "^8.35.0",
"lodash.chunk": "^4.2.0",
"pako": "^2.1.0",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
15 changes: 12 additions & 3 deletions yarn-project/circuits.js/src/contract/public_bytecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ import {
} from '@aztec/foundation/serialize';
import { ContractClass } from '@aztec/types/contracts';

import { deflate, inflate } from 'pako';

import { FUNCTION_SELECTOR_NUM_BYTES } from '../constants.gen.js';

/**
* Packs together a set of public functions for a contract class.
* @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(
publicFns.map(fn => serializeToBuffer(fn.selector, fn.isInternal, numToInt32BE(fn.bytecode.length), fn.bytecode)),
return Buffer.from(
deflate(
serializeBufferArrayToVector(
publicFns.map(fn =>
serializeToBuffer(fn.selector, fn.isInternal, numToInt32BE(fn.bytecode.length), fn.bytecode),
),
),
{ raw: true, level: 9 },
),
);
}

Expand All @@ -24,7 +33,7 @@ export function packBytecode(publicFns: ContractClass['publicFunctions']): Buffe
* @remarks This function should no longer be necessary once we have a single bytecode per contract.
*/
export function unpackBytecode(buffer: Buffer): ContractClass['publicFunctions'] {
const reader = BufferReader.asReader(buffer);
const reader = BufferReader.asReader(inflate(buffer, { raw: true }));
return reader.readVector({
fromBuffer: (reader: BufferReader) => ({
selector: FunctionSelector.fromBuffer(reader.readBytes(FUNCTION_SELECTOR_NUM_BYTES)),
Expand Down
1 change: 1 addition & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ __metadata:
eslint: ^8.35.0
jest: ^29.5.0
lodash.chunk: ^4.2.0
pako: ^2.1.0
prettier: ^2.8.4
ts-jest: ^29.1.0
ts-node: ^10.9.1
Expand Down

0 comments on commit 11aece0

Please sign in to comment.