Skip to content

Commit

Permalink
Update function to load Noir artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Mar 6, 2024
1 parent a51b304 commit 6a49464
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 4 additions & 0 deletions yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export interface FunctionAbi {
* Whether the function is internal.
*/
isInternal: boolean;
/**
* Whether the function is flagged as an initializer.
*/
isInitializer: boolean;
/**
* Function parameters.
*/
Expand Down
32 changes: 29 additions & 3 deletions yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
FunctionType,
} from '@aztec/foundation/abi';

import { NoirCompiledContract } from '../noir/index.js';
import {
AZTEC_INITIALIZER_ATTRIBUTE,
AZTEC_INTERNAL_ATTRIBUTE,
AZTEC_PRIVATE_ATTRIBUTE,
AZTEC_PUBLIC_ATTRIBUTE,
NoirCompiledContract,
} from '../noir/index.js';
import { mockVerificationKey } from './mocked_keys.js';

/**
Expand Down Expand Up @@ -98,8 +104,14 @@ type NoirCompiledContractFunction = NoirCompiledContract['functions'][number];
* @returns Function artifact.
*/
function generateFunctionArtifact(fn: NoirCompiledContractFunction): FunctionArtifact {
const functionType = fn.function_type.toLowerCase() as FunctionType;
const isInternal = fn.is_internal;
if (fn.custom_attributes === undefined) {
throw new Error(
`No custom attributes found for contract function ${fn.name}. Try rebuilding the contract with the latest nargo version.`,
);
}
const functionType = getFunctionType(fn);
const isInternal = fn.custom_attributes.includes(AZTEC_INTERNAL_ATTRIBUTE);
const isInitializer = fn.custom_attributes.includes(AZTEC_INITIALIZER_ATTRIBUTE);

// If the function is not unconstrained, the first item is inputs or CallContext which we should omit
let parameters = fn.abi.parameters.map(generateFunctionParameter);
Expand All @@ -117,6 +129,7 @@ function generateFunctionArtifact(fn: NoirCompiledContractFunction): FunctionArt
name: fn.name,
functionType,
isInternal,
isInitializer,
parameters,
returnTypes,
bytecode: fn.bytecode,
Expand All @@ -125,6 +138,19 @@ function generateFunctionArtifact(fn: NoirCompiledContractFunction): FunctionArt
};
}

function getFunctionType(fn: NoirCompiledContractFunction): FunctionType {
if (fn.custom_attributes.includes(AZTEC_PRIVATE_ATTRIBUTE)) {
return FunctionType.SECRET;
} else if (fn.custom_attributes.includes(AZTEC_PUBLIC_ATTRIBUTE)) {
return FunctionType.OPEN;
} else if (fn.is_unconstrained) {
return FunctionType.UNCONSTRAINED;
} else {
// Default to a private function (see simple_macro_example_expanded for an example of this behavior)
return FunctionType.SECRET;
}
}

/**
* Returns true if the first parameter is kernel function inputs.
*
Expand Down
14 changes: 8 additions & 6 deletions yarn-project/types/src/noir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
EventAbi,
} from '@aztec/foundation/abi';

/** The Aztec.nr function types. */
type NoirFunctionType = 'Open' | 'Secret' | 'Unconstrained';
export const AZTEC_PRIVATE_ATTRIBUTE = 'aztec(private)';
export const AZTEC_PUBLIC_ATTRIBUTE = 'aztec(public)';
export const AZTEC_INTERNAL_ATTRIBUTE = 'aztec(internal)';
export const AZTEC_INITIALIZER_ATTRIBUTE = 'aztec(initializer)';

/** The witness indices of the parameters. */
type ParamWitnessIndices = { /** Start */ start: number; /** End */ end: number };
Expand Down Expand Up @@ -40,10 +42,10 @@ export interface NoirFunctionAbi {
export interface NoirFunctionEntry {
/** The name of the function. */
name: string;
/** The type of the function. */
function_type: NoirFunctionType;
/** Whether the function is internal. */
is_internal: boolean;
/** Whether the function is unconstrained. */
is_unconstrained: boolean;
/** Attributes injected via macros. */
custom_attributes: string[];
/** The ABI of the function. */
abi: NoirFunctionAbi;
/** The bytecode of the function in base64. */
Expand Down

0 comments on commit 6a49464

Please sign in to comment.