From 15e495068fa73a73b9c47f5a1c6b4678c68697ba Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Mon, 27 Mar 2023 13:48:43 +0100 Subject: [PATCH] Edit JSON Abi to include whether a function is open --- Cargo.lock | 1 + crates/noirc_driver/src/contract.rs | 26 +++------------------ crates/noirc_driver/src/lib.rs | 10 ++++---- crates/noirc_frontend/Cargo.toml | 3 ++- crates/noirc_frontend/src/ast/expression.rs | 23 ++++++++++++++---- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83884bbf57a..23b25c0b451 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2240,6 +2240,7 @@ dependencies = [ "noirc_abi", "noirc_errors", "rustc-hash", + "serde", "smol_str", "strum", "strum_macros", diff --git a/crates/noirc_driver/src/contract.rs b/crates/noirc_driver/src/contract.rs index 3f438355f3b..4e74f06a2e3 100644 --- a/crates/noirc_driver/src/contract.rs +++ b/crates/noirc_driver/src/contract.rs @@ -1,29 +1,9 @@ use std::collections::BTreeMap; +use noirc_frontend::ContractVisibility; + use crate::CompiledProgram; -/// ContractFunctionType describes the types -/// smart contract functions that are allowed. -/// -/// Note: -/// - All Noir programs in the non-contract context -/// can be seen as `Secret`. -/// - It may be possible to have `unconstrained` -/// functions in regular Noir programs. For now -/// we leave it as a property of only contract functions. -#[derive(serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum ContractFunctionType { - /// This function will be executed in a private - /// context. - Secret, - /// This function will be executed in a public - /// context. - Public, - /// A function which is non-deterministic - /// and does not require any constraints. - Unconstrained, -} /// Each function in the contract will be compiled /// as a separate noir program. /// @@ -32,7 +12,7 @@ pub enum ContractFunctionType { /// One of these being a function type. #[derive(serde::Serialize, serde::Deserialize)] pub struct ContractFunction { - pub func_type: ContractFunctionType, + pub func_type: ContractVisibility, pub function: CompiledProgram, } diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 789ea90ae10..7ed5974bec7 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -202,11 +202,11 @@ impl Driver { contract: Contract, options: &CompileOptions, ) -> Result { - let functions = try_btree_map(&contract.functions, |function| { - let function_name = self.function_name(*function).to_owned(); - let function = self.compile_no_check(options, *function)?; - let func_type = contract::ContractFunctionType::Secret; - // Note: currently we mark all of the contract methods as secret as we do not support public functions. + let functions = try_btree_map(&contract.functions, |function_id| { + let function_name = self.function_name(*function_id).to_owned(); + let function = self.compile_no_check(options, *function_id)?; + let func_type = + self.context.def_interner.function_meta(function_id).contract_visibility; Ok((function_name, ContractFunction { func_type, function })) })?; diff --git a/crates/noirc_frontend/Cargo.toml b/crates/noirc_frontend/Cargo.toml index b05601c8ab6..b5551d17f51 100644 --- a/crates/noirc_frontend/Cargo.toml +++ b/crates/noirc_frontend/Cargo.toml @@ -16,8 +16,9 @@ iter-extended.workspace = true chumsky.workspace = true thiserror.workspace = true smol_str.workspace = true +serde.workspace = true rustc-hash = "1.1.0" [dev-dependencies] strum = "0.24" -strum_macros = "0.24" \ No newline at end of file +strum_macros = "0.24" diff --git a/crates/noirc_frontend/src/ast/expression.rs b/crates/noirc_frontend/src/ast/expression.rs index 3ecb55b3882..e13dfecf0a8 100644 --- a/crates/noirc_frontend/src/ast/expression.rs +++ b/crates/noirc_frontend/src/ast/expression.rs @@ -319,13 +319,26 @@ pub struct FunctionDefinition { pub return_visibility: noirc_abi::AbiVisibility, } -/// The visibility a function has in the contract it is defined within. -/// If the function is not defined within a contract, this should always be 'Secret'. -/// Likewise, if a function does not specify its visibility, it defaults to 'Secret'. -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +/// ContractFunctionType describes the types +/// smart contract functions that are allowed. +/// +/// Note: +/// - All Noir programs in the non-contract context +/// can be seen as `Secret`. +/// - It may be possible to have `unconstrained` +/// functions in regular Noir programs. For now +/// we leave it as a property of only contract functions. +#[derive(serde::Serialize, serde::Deserialize, Debug, Copy, Clone, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] pub enum ContractVisibility { - Open, + /// This function will be executed in a private + /// context. Secret, + /// This function will be executed in a public + /// context. + Open, + /// A function which is non-deterministic + /// and does not require any constraints. Unconstrained, }