Skip to content

Commit

Permalink
feat(public): only deploy/register public_dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Oct 3, 2024
1 parent 0be1d99 commit b8252a2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 68 deletions.
23 changes: 18 additions & 5 deletions yarn-project/circuits.js/src/contract/contract_class.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ContractArtifact, type FunctionArtifact, FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';
import { type ContractClass, type ContractClassWithId } from '@aztec/types/contracts';
import { type ContractClass, type ContractClassWithId, type PublicFunction } from '@aztec/types/contracts';

import { PUBLIC_DISPATCH_SELECTOR } from '../constants.gen.js';
import { computeArtifactHash } from './artifact_hash.js';
import { type ContractClassIdPreimage, computeContractClassIdWithPreimage } from './contract_class_id.js';
import { packBytecode } from './public_bytecode.js';

/** Contract artifact including its artifact hash */
type ContractArtifactWithHash = ContractArtifact & { artifactHash: Fr };
Expand All @@ -17,15 +17,27 @@ export function getContractClassFromArtifact(
artifact: ContractArtifact | ContractArtifactWithHash,
): ContractClassWithId & ContractClassIdPreimage {
const artifactHash = 'artifactHash' in artifact ? artifact.artifactHash : computeArtifactHash(artifact);
const publicFunctions: ContractClass['publicFunctions'] = artifact.functions
const artifactPublicFunctions: ContractClass['publicFunctions'] = artifact.functions
.filter(f => f.functionType === FunctionType.PUBLIC)
.map(f => ({
selector: FunctionSelector.fromNameAndParameters(f.name, f.parameters),
bytecode: f.bytecode,
}))
.sort(cmpFunctionArtifacts);

const packedBytecode = packBytecode(publicFunctions);
let packedBytecode = Buffer.alloc(0);
let dispatchFunction: PublicFunction | undefined = undefined;
if (artifactPublicFunctions.length > 0) {
dispatchFunction = artifactPublicFunctions.find(f =>
f.selector.equals(FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR))),
);
if (!dispatchFunction) {
throw new Error(
'A contract with public functions should define a public_dispatch(Field) function as its public entrypoint.',
);
}
packedBytecode = dispatchFunction.bytecode;
}

const privateFunctions: ContractClass['privateFunctions'] = artifact.functions
.filter(f => f.functionType === FunctionType.PRIVATE)
Expand All @@ -35,7 +47,8 @@ export function getContractClassFromArtifact(
const contractClass: ContractClass = {
version: 1,
artifactHash,
publicFunctions,
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Remove public functions.
publicFunctions: dispatchFunction ? [dispatchFunction] : [],
packedBytecode,
privateFunctions,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { bufferFromFields } from '@aztec/foundation/abi';
import { FunctionSelector, bufferFromFields } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';
import { type ContractClassPublic } from '@aztec/types/contracts';
import { type ContractClassPublic, type PublicFunction } from '@aztec/types/contracts';

import chunk from 'lodash.chunk';

import { REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE } from '../../constants.gen.js';
import { PUBLIC_DISPATCH_SELECTOR, REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE } from '../../constants.gen.js';
import { computeContractClassId, computePublicBytecodeCommitment } from '../contract_class_id.js';
import { unpackBytecode } from '../public_bytecode.js';

/** Event emitted from the ContractClassRegisterer. */
export class ContractClassRegisteredEvent {
Expand Down Expand Up @@ -73,12 +72,21 @@ export class ContractClassRegisteredEvent {
throw new Error(`Unexpected contract class version ${this.version}`);
}

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Remove public functions.
const publicFunctions: PublicFunction[] = [];
if (this.packedPublicBytecode.length > 0) {
publicFunctions.push({
selector: FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR)),
bytecode: this.packedPublicBytecode,
});
}

return {
id: this.contractClassId,
artifactHash: this.artifactHash,
packedBytecode: this.packedPublicBytecode,
privateFunctionsRoot: this.privateFunctionsRoot,
publicFunctions: unpackBytecode(this.packedPublicBytecode),
publicFunctions: publicFunctions,
version: this.version,
privateFunctions: [],
unconstrainedFunctions: [],
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export * from './events/contract_instance_deployed_event.js';
export * from './events/private_function_broadcasted_event.js';
export * from './events/unconstrained_function_broadcasted_event.js';
export * from './private_function.js';
export * from './public_bytecode.js';
export * from './private_function_membership_proof.js';
export * from './unconstrained_function_membership_proof.js';
19 changes: 0 additions & 19 deletions yarn-project/circuits.js/src/contract/public_bytecode.test.ts

This file was deleted.

34 changes: 0 additions & 34 deletions yarn-project/circuits.js/src/contract/public_bytecode.ts

This file was deleted.

3 changes: 1 addition & 2 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ import {
computeContractClassId,
computePublicBytecodeCommitment,
makeRecursiveProof,
packBytecode,
} from '../index.js';
import { ContentCommitment, NUM_BYTES_PER_SHA256 } from '../structs/content_commitment.js';
import { Gas } from '../structs/gas.js';
Expand Down Expand Up @@ -1362,7 +1361,7 @@ export function makeContractClassPublic(seed = 0): ContractClassPublic {
const artifactHash = fr(seed + 1);
const publicFunctions = makeTuple(3, makeContractClassPublicFunction, seed + 2);
const privateFunctionsRoot = fr(seed + 3);
const packedBytecode = packBytecode(publicFunctions);
const packedBytecode = publicFunctions[0].bytecode;
const publicBytecodeCommitment = computePublicBytecodeCommitment(packedBytecode);
const id = computeContractClassId({ artifactHash, privateFunctionsRoot, publicBytecodeCommitment });
return {
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/types/src/contracts/contract_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export interface ContractClass {
artifactHash: Fr;
/** List of individual private functions, constructors included. */
privateFunctions: PrivateFunction[];
/** List of individual public functions. Should be removed once we switch to the AVM where all public bytecode is bundled together. */
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Remove public functions.
/** Contains the public_dispatch function (and only that) if there's any public code in the contract. */
publicFunctions: PublicFunction[];
/** Packed bytecode representation of the AVM bytecode for all public functions in this contract. Unused for now, see `publicFunctions`. */
/** Bytecode for the public_dispatch function, or empty. */
packedBytecode: Buffer;
}

Expand Down

0 comments on commit b8252a2

Please sign in to comment.