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(noir): added distinct keyword #1219

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ impl std::fmt::Display for AbiVisibility {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
/// Represents whether the return value should be encoded into witness indexes that are distinct
/// from the input parameters.
///
/// This is useful for apllication stacks that require an uniform abi across across multiple
/// circuits. When duplication is allowed, the compiler may indentify that a public input reaches
/// the output unaltered and is thus references directly, causing the input and output witness
/// indcies to overlap.
pub enum AbiDistinctness {
Distinct,
DuplicationAllowed,
}

impl std::fmt::Display for AbiDistinctness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AbiDistinctness::Distinct => write!(f, "distinct"),
AbiDistinctness::DuplicationAllowed => write!(f, "duplication-allowed"),
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Sign {
Expand Down
17 changes: 15 additions & 2 deletions crates/noirc_evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct Evaluator {
// and increasing as for `public_parameters`. We then use a `Vec` rather
// than a `BTreeSet` to preserve this order for the ABI.
return_values: Vec<Witness>,
// If true, indicates that the resulting ACIR should prevent input and output witness indicies
// from overlapping by having extra contraints if necessary.
return_is_distinct: bool,

opcodes: Vec<AcirOpcode>,
}
Expand Down Expand Up @@ -102,6 +105,11 @@ pub fn create_circuit(
}

impl Evaluator {
// Returns true if the `witnees_index` appears in the program's input parameters.
fn is_abi_input(&self, witness_index: Witness) -> bool {
witness_index.as_usize() <= self.num_witnesses_abi_len
}

// Returns true if the `witness_index`
// was created in the ABI as a private input.
//
Expand All @@ -111,11 +119,14 @@ impl Evaluator {
// If the `witness_index` is more than the `num_witnesses_abi_len`
// then it was created after the ABI was processed and is therefore
// an intermediate variable.
let is_intermediate_variable = witness_index.as_usize() > self.num_witnesses_abi_len;

let is_public_input = self.public_parameters.contains(&witness_index);

!is_intermediate_variable && !is_public_input
self.is_abi_input(witness_index) && !is_public_input
}

fn should_proxy_witness_for_abi_output(&self, witness_index: Witness) -> bool {
self.return_is_distinct && self.is_abi_input(witness_index)
}

// Creates a new Witness index
Expand All @@ -139,6 +150,8 @@ impl Evaluator {
enable_logging: bool,
show_output: bool,
) -> Result<(), RuntimeError> {
self.return_is_distinct =
program.return_distinctness == noirc_abi::AbiDistinctness::Distinct;
let mut ir_gen = IrGenerator::new(program);
self.parse_abi_alt(&mut ir_gen);

Expand Down
12 changes: 11 additions & 1 deletion crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use acvm::acir::native_types::Expression;

use crate::{
errors::RuntimeErrorKind,
ssa::{
Expand Down Expand Up @@ -46,7 +48,15 @@ pub(crate) fn evaluate(
"we do not allow private ABI inputs to be returned as public outputs",
)));
}
evaluator.return_values.push(witness);
// Check if the outputted witness needs separating from an occurrence in the program's
// input. This behaviour stems from usage of the `distinct` keyword.
let return_witness = if evaluator.should_proxy_witness_for_abi_output(witness) {
let proxy_constraint = Expression::from(witness);
evaluator.create_intermediate_variable(proxy_constraint)
} else {
witness
};
evaluator.return_values.push(return_witness);
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ pub struct FunctionDefinition {
pub span: Span,
pub return_type: UnresolvedType,
pub return_visibility: noirc_abi::AbiVisibility,
pub return_distinctness: noirc_abi::AbiDistinctness,
}

/// Describes the types of smart contract functions that are allowed.
Expand Down
14 changes: 14 additions & 0 deletions crates/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum ResolverError {
UnnecessaryPub { ident: Ident },
#[error("Required 'pub', main function must return public value")]
NecessaryPub { ident: Ident },
#[error("'distinct' keyword can only be used with main method")]
DistinctNotAllowed { ident: Ident },
#[error("Expected const value where non-constant value was used")]
ExpectedComptimeVariable { name: String, span: Span },
#[error("Missing expression for declared constant")]
Expand Down Expand Up @@ -176,6 +178,18 @@ impl From<ResolverError> for Diagnostic {
diag.add_note("The `pub` keyword is mandatory for the entry-point function return type because the verifier cannot retrieve private witness and thus the function will not be able to return a 'priv' value".to_owned());
diag
}
ResolverError::DistinctNotAllowed { ident } => {
let name = &ident.0.contents;

let mut diag = Diagnostic::simple_error(
format!("Invalid `distinct` keyword on return type of function {name}"),
"Invalid distinct on return type".to_string(),
ident.0.span(),
);

diag.add_note("The `distinct` keyword is only valid when used on the main function of a program as it's only purpose is to prevent a program's parameter and return witness indexes from overlapping".to_owned());
jfecher marked this conversation as resolved.
Show resolved Hide resolved
diag
}
ResolverError::ExpectedComptimeVariable { name, span } => Diagnostic::simple_error(
format!("expected constant variable where non-constant variable {name} was used"),
"expected const variable".to_string(),
Expand Down
16 changes: 16 additions & 0 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ impl<'a> Resolver<'a> {
self.push_err(ResolverError::NecessaryPub { ident: func.name_ident().clone() });
}

if !self.distinct_allowed(func)
&& func.def.return_distinctness != noirc_abi::AbiDistinctness::DuplicationAllowed
{
self.push_err(ResolverError::DistinctNotAllowed { ident: func.name_ident().clone() });
}

if attributes == Some(Attribute::Test) && !parameters.is_empty() {
self.push_err(ResolverError::TestFunctionHasParameters {
span: func.name_ident().span(),
Expand All @@ -661,6 +667,7 @@ impl<'a> Resolver<'a> {
typ,
parameters: parameters.into(),
return_visibility: func.def.return_visibility,
return_distinctness: func.def.return_distinctness,
has_body: !func.def.body.is_empty(),
}
}
Expand All @@ -674,6 +681,15 @@ impl<'a> Resolver<'a> {
}
}

/// True if the 'distinct' keyword is allowed on parameters in this function
jfecher marked this conversation as resolved.
Show resolved Hide resolved
fn distinct_allowed(&self, func: &NoirFunction) -> bool {
if self.in_contract() {
!func.def.is_unconstrained && !func.def.is_open
jfecher marked this conversation as resolved.
Show resolved Hide resolved
} else {
func.name() == "main"
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn handle_function_type(&mut self, func: &NoirFunction) -> Option<ContractFunctionType> {
if func.def.is_open {
if self.in_contract() {
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ mod test {
]
.into(),
return_visibility: noirc_abi::AbiVisibility::Private,
return_distinctness: noirc_abi::AbiDistinctness::DuplicationAllowed,
has_body: true,
};
interner.push_fn_meta(func_meta, func_id);
Expand Down
4 changes: 3 additions & 1 deletion crates/noirc_frontend/src/hir_def/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use iter_extended::vecmap;
use noirc_abi::{AbiParameter, AbiType, AbiVisibility};
use noirc_abi::{AbiDistinctness, AbiParameter, AbiType, AbiVisibility};
use noirc_errors::{Location, Span};

use super::expr::{HirBlockExpression, HirExpression, HirIdent};
Expand Down Expand Up @@ -131,6 +131,8 @@ pub struct FuncMeta {

pub return_visibility: AbiVisibility,

pub return_distinctness: AbiDistinctness,

/// The type of this function. Either a Type::Function
/// or a Type::Forall for generic functions.
pub typ: Type,
Expand Down
3 changes: 3 additions & 0 deletions crates/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ pub enum Keyword {
Contract,
Crate,
Dep,
Distinct,
Else,
Field,
Fn,
Expand Down Expand Up @@ -454,6 +455,7 @@ impl fmt::Display for Keyword {
Keyword::Contract => write!(f, "contract"),
Keyword::Crate => write!(f, "crate"),
Keyword::Dep => write!(f, "dep"),
Keyword::Distinct => write!(f, "distinct"),
Keyword::Else => write!(f, "else"),
Keyword::Field => write!(f, "Field"),
Keyword::Fn => write!(f, "fn"),
Expand Down Expand Up @@ -490,6 +492,7 @@ impl Keyword {
"contract" => Keyword::Contract,
"crate" => Keyword::Crate,
"dep" => Keyword::Dep,
"distinct" => Keyword::Distinct,
"else" => Keyword::Else,
"Field" => Keyword::Field,
"fn" => Keyword::Fn,
Expand Down
13 changes: 11 additions & 2 deletions crates/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,20 @@ impl Type {
pub struct Program {
pub functions: Vec<Function>,
pub main_function_signature: FunctionSignature,
/// Indicates whether the resulting ACIR should keep input and output witness indcies separate.
///
/// Note: this has no impact on monomorphisation, and is simply attached here for ease of
/// forwarding to the next phase.
pub return_distinctness: noirc_abi::AbiDistinctness,
}

impl Program {
pub fn new(functions: Vec<Function>, main_function_signature: FunctionSignature) -> Program {
Program { functions, main_function_signature }
pub fn new(
functions: Vec<Function>,
main_function_signature: FunctionSignature,
return_distinctness: noirc_abi::AbiDistinctness,
) -> Program {
Program { functions, main_function_signature, return_distinctness }
}

pub fn main(&self) -> &Function {
Expand Down
5 changes: 3 additions & 2 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::collections::{BTreeMap, HashMap, VecDeque};
use crate::{
hir_def::{
expr::*,
function::{Param, Parameters},
function::{FuncMeta, Param, Parameters},
stmt::{HirAssignStatement, HirLValue, HirLetStatement, HirPattern, HirStatement},
},
node_interner::{self, DefinitionKind, NodeInterner, StmtId},
Expand Down Expand Up @@ -88,7 +88,8 @@ pub fn monomorphize(main: node_interner::FuncId, interner: &NodeInterner) -> Pro
}

let functions = vecmap(monomorphizer.finished_functions, |(_, f)| f);
Program::new(functions, function_sig)
let FuncMeta { return_distinctness, .. } = interner.function_meta(&main);
Program::new(functions, function_sig, return_distinctness)
}

impl<'interner> Monomorphizer<'interner> {
Expand Down
26 changes: 22 additions & 4 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{

use chumsky::prelude::*;
use iter_extended::vecmap;
use noirc_abi::AbiVisibility;
use noirc_abi::{AbiDistinctness, AbiVisibility};
use noirc_errors::{CustomDiagnostic, Span, Spanned};

/// Entry function for the parser - also handles lexing internally.
Expand Down Expand Up @@ -162,7 +162,7 @@ fn function_definition(allow_self: bool) -> impl NoirParser<NoirFunction> {
|(
(
((((attribute, (is_unconstrained, is_open)), name), generics), parameters),
(return_visibility, return_type),
(return_visibility, return_distinctness, return_type),
),
body,
)| {
Expand All @@ -177,6 +177,7 @@ fn function_definition(allow_self: bool) -> impl NoirParser<NoirFunction> {
body,
return_type,
return_visibility,
return_distinctness,
}
.into()
},
Expand Down Expand Up @@ -235,12 +236,22 @@ fn lambda_return_type() -> impl NoirParser<UnresolvedType> {
.map(|ret| ret.unwrap_or(UnresolvedType::Unspecified))
}

fn function_return_type() -> impl NoirParser<(AbiVisibility, UnresolvedType)> {
fn function_return_type() -> impl NoirParser<(AbiVisibility, AbiDistinctness, UnresolvedType)> {
just(Token::Arrow)
.ignore_then(optional_visibility())
.then(optional_distinctness())
jfecher marked this conversation as resolved.
Show resolved Hide resolved
.then(parse_type())
.or_not()
.map(|ret| ret.unwrap_or((AbiVisibility::Private, UnresolvedType::Unit)))
.map(|ret| {
ret.map(|((visibility, distinctness), return_type)| {
(visibility, distinctness, return_type)
})
jfecher marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or((
AbiVisibility::Private,
AbiDistinctness::DuplicationAllowed,
UnresolvedType::Unit,
))
})
}

fn attribute() -> impl NoirParser<Attribute> {
Expand Down Expand Up @@ -554,6 +565,13 @@ fn optional_visibility() -> impl NoirParser<AbiVisibility> {
})
}

fn optional_distinctness() -> impl NoirParser<AbiDistinctness> {
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
keyword(Keyword::Distinct).or_not().map(|opt| match opt {
Some(_) => AbiDistinctness::Distinct,
None => AbiDistinctness::DuplicationAllowed,
})
}

fn maybe_comp_time() -> impl NoirParser<CompTime> {
keyword(Keyword::CompTime).or_not().map(|opt| match opt {
Some(_) => CompTime::Yes(None),
Expand Down