Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(public)!: only deploy/register public_dispatch #8988

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ impl TestEnvironment {
_self: Self,
call_interface: C
) where C: CallInterface<M, (), T, Env> {
cheatcodes::assert_public_call_fails(
call_interface.get_contract_address(),
call_interface.get_selector(),
call_interface.get_args()
);
// Public functions are routed through the dispatch function.
let fn_selector = FunctionSelector::from_field(PUBLIC_DISPATCH_SELECTOR);
let calldata = &[call_interface.get_selector().to_field()].append(call_interface.get_args());
cheatcodes::assert_public_call_fails(call_interface.get_contract_address(), fn_selector, calldata);
}

unconstrained fn assert_private_call_fails<C, let M: u32, T, Env>(
Expand Down
5 changes: 2 additions & 3 deletions yarn-project/circuits.js/src/contract/contract_class.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';

import { PUBLIC_DISPATCH_SELECTOR } from '../constants.gen.js';
import { getBenchmarkContractArtifact } from '../tests/fixtures.js';
import { getContractClassFromArtifact } from './contract_class.js';

Expand All @@ -19,9 +20,7 @@ describe('ContractClass', () => {
});

// Check function selectors match
const publicFunctionSelectors = artifact.functions
.filter(fn => fn.functionType === FunctionType.PUBLIC)
.map(fn => FunctionSelector.fromNameAndParameters(fn));
const publicFunctionSelectors = [FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR))];
const privateFunctionSelectors = artifact.functions
.filter(fn => fn.functionType === FunctionType.PRIVATE)
.map(fn => FunctionSelector.fromNameAndParameters(fn));
Expand Down
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
12 changes: 10 additions & 2 deletions yarn-project/txe/src/util/txe_public_contract_data_source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AztecAddress, Fr, type FunctionSelector, unpackBytecode } from '@aztec/circuits.js';
import { type AztecAddress, Fr, FunctionSelector, PUBLIC_DISPATCH_SELECTOR } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { PrivateFunctionsTree } from '@aztec/pxe';
import {
Expand Down Expand Up @@ -31,11 +31,19 @@ export class TXEPublicContractDataSource implements ContractDataSource {
const tree = new PrivateFunctionsTree(artifact);
const privateFunctionsRoot = tree.getFunctionTreeRoot();

const publicFunctions: PublicFunction[] = [];
if (contractClass!.packedBytecode.length > 0) {
publicFunctions.push({
selector: FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR)),
bytecode: contractClass!.packedBytecode,
});
}

return {
id,
artifactHash: contractClass!.artifactHash,
packedBytecode: contractClass!.packedBytecode,
publicFunctions: unpackBytecode(contractClass!.packedBytecode),
publicFunctions: publicFunctions,
privateFunctionsRoot: new Fr(privateFunctionsRoot!.root),
version: contractClass!.version,
privateFunctions: [],
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
Loading