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: include oracles in ABI #6412

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
21 changes: 19 additions & 2 deletions compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use acvm::acir::circuit::ErrorSelector;
use acvm::AcirField;
use iter_extended::vecmap;
use noirc_abi::{
Abi, AbiErrorType, AbiParameter, AbiReturnType, AbiType, AbiValue, AbiVisibility, Sign,
Abi, AbiErrorType, AbiParameter, AbiReturnType, AbiType, AbiValue, AbiVisibility, Oracle,
OracleParameter, Sign,
};
use noirc_frontend::ast::{Signedness, Visibility};
use noirc_frontend::TypeBinding;
Expand Down Expand Up @@ -36,7 +37,8 @@ pub(super) fn gen_abi(
.into_iter()
.map(|(selector, typ)| (selector, build_abi_error_type(context, &typ)))
.collect();
Abi { parameters, return_type, error_types }
let oracles = compute_oracles(context);
Abi { parameters, return_type, error_types, oracles }
}

fn build_abi_error_type(context: &Context, typ: &Type) -> AbiErrorType {
Expand Down Expand Up @@ -141,6 +143,21 @@ pub(super) fn compute_function_abi(
(parameters, return_type)
}

fn compute_oracles(context: &Context) -> Vec<Oracle> {
context
.def_interner
.oracle_functions()
.map(|(func_id, oracle_name)| {
let (parameters, return_type) = compute_function_abi(context, func_id);
let parameters = vecmap(parameters, |parameter| OracleParameter {
name: parameter.name,
typ: parameter.typ,
});
Oracle { name: oracle_name.to_string(), parameters, return_type }
})
.collect()
}

/// Attempts to retrieve the name of this parameter. Returns None
/// if this parameter is a tuple or struct pattern.
fn get_param_name<'a>(pattern: &HirPattern, interner: &'a NodeInterner) -> Option<&'a str> {
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@
interned_statement_kinds: noirc_arena::Arena<StatementKind>,

// Interned `UnresolvedTypeData`s during comptime code.
interned_unresolved_type_datas: noirc_arena::Arena<UnresolvedTypeData>,

Check warning on line 223 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)

// Interned `Pattern`s during comptime code.
interned_patterns: noirc_arena::Arena<Pattern>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 228 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -670,7 +670,7 @@
quoted_types: Default::default(),
interned_expression_kinds: Default::default(),
interned_statement_kinds: Default::default(),
interned_unresolved_type_datas: Default::default(),

Check warning on line 673 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
interned_patterns: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
Expand Down Expand Up @@ -2206,11 +2206,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2209 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

pub fn get_unresolved_type_data(&self, id: InternedUnresolvedTypeData) -> &UnresolvedTypeData {
&self.interned_unresolved_type_datas[id.0]

Check warning on line 2213 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

/// Returns the type of an operator (which is always a function), along with its return type.
Expand Down Expand Up @@ -2326,6 +2326,12 @@
) -> &std::collections::HashMap<ModuleId, std::collections::HashMap<Ident, UnusedItem>> {
self.usage_tracker.unused_items()
}

pub fn oracle_functions(&self) -> impl Iterator<Item = (&FuncId, &String)> {
self.function_modifiers.iter().filter_map(|(func_id, modifiers)| {
Some((func_id, modifiers.attributes.function.as_ref()?.oracle()?))
})
}
}

impl Methods {
Expand Down
6 changes: 5 additions & 1 deletion tooling/noirc_abi/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

fn arb_primitive_abi_type() -> SBoxedStrategy<AbiType> {
const MAX_STRING_LEN: u32 = 1000;
proptest::prop_oneof![

Check warning on line 85 in tooling/noirc_abi/src/arbitrary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
Just(AbiType::Field),
Just(AbiType::Boolean),
any::<(Sign, u32)>().prop_map(|(sign, width)| {
Expand All @@ -103,7 +103,7 @@
256, // Shoot for maximum size of 256 nodes
10, // We put up to 10 items per collection
|inner| {
prop_oneof![

Check warning on line 106 in tooling/noirc_abi/src/arbitrary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
(1..10u32, inner.clone())
.prop_map(|(length, typ)| { AbiType::Array { length, typ: Box::new(typ) } })
.boxed(),
Expand All @@ -121,6 +121,10 @@
.boxed()
}

pub(super) fn optional_arb_abi_type() -> BoxedStrategy<Option<AbiType>> {
prop_oneof![Just(None), arb_abi_type().prop_map(Some)].boxed()

Check warning on line 125 in tooling/noirc_abi/src/arbitrary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
}

fn arb_abi_param_and_value() -> BoxedStrategy<(AbiParameter, InputValue)> {
arb_abi_type()
.prop_flat_map(|typ| {
Expand All @@ -147,6 +151,6 @@
let parameters = vecmap(&parameters_with_values, |(param, _)| param.clone());
let input_map = btree_map(parameters_with_values, |(param, value)| (param.name, value));

(Abi { parameters, return_type, error_types: BTreeMap::default() }, input_map)
(Abi { parameters, return_type, error_types: BTreeMap::default(), oracles: Vec::default() }, input_map)
}
}
1 change: 1 addition & 0 deletions tooling/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ mod serialization_tests {
visibility: AbiVisibility::Public,
}),
error_types: Default::default(),
oracles: vec![],
};

let input_map: BTreeMap<String, InputValue> = BTreeMap::from([
Expand Down
18 changes: 18 additions & 0 deletions tooling/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ pub struct AbiReturnType {
pub visibility: AbiVisibility,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub struct OracleParameter {
pub name: String,
#[cfg_attr(test, proptest(strategy = "arbitrary::arb_abi_type()"))]
pub typ: AbiType,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub struct Oracle {
pub name: String,
pub parameters: Vec<OracleParameter>,
#[cfg_attr(test, proptest(strategy = "arbitrary::optional_arb_abi_type()"))]
pub return_type: Option<AbiType>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub struct Abi {
Expand All @@ -179,6 +196,7 @@ pub struct Abi {
pub return_type: Option<AbiReturnType>,
#[cfg_attr(test, proptest(strategy = "proptest::prelude::Just(BTreeMap::from([]))"))]
pub error_types: BTreeMap<ErrorSelector, AbiErrorType>,
pub oracles: Vec<Oracle>,
}

impl Abi {
Expand Down
Loading