Skip to content

Commit

Permalink
feat: Sync from noir (#7444)
Browse files Browse the repository at this point in the history
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
chore: redo typo PR by omahs
(noir-lang/noir#5487)
fix: Run macro processors in the elaborator
(noir-lang/noir#5472)
feat: let LSP always work in a Noir workspace if there's any
(noir-lang/noir#5461)
fix: Move BigInt modulus checks to runtime in brillig
(noir-lang/noir#5374)
chore: keccak256 in Noir (noir-lang/noir#5316)
END_COMMIT_OVERRIDE

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
  • Loading branch information
3 people authored Jul 11, 2024
1 parent 3760c64 commit 8403e84
Show file tree
Hide file tree
Showing 33 changed files with 613 additions and 252 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e59ff8c6a12978407be4f9f474d5208bdabb8c29
90b636e71333cfe46c5e3062ded34b583fcb64d5
4 changes: 2 additions & 2 deletions noir/noir-repo/acvm-repo/blackbox_solver/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BigIntSolver {
}

impl BigIntSolver {
pub(crate) fn get_bigint(
pub fn get_bigint(
&self,
id: u32,
func: BlackBoxFunc,
Expand All @@ -32,7 +32,7 @@ impl BigIntSolver {
.cloned()
}

pub(crate) fn get_modulus(
pub fn get_modulus(
&self,
id: u32,
func: BlackBoxFunc,
Expand Down
8 changes: 8 additions & 0 deletions noir/noir-repo/acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ impl BrilligBigintSolver {
rhs: u32,
func: BlackBoxFunc,
) -> Result<u32, BlackBoxResolutionError> {
let modulus_lhs = self.bigint_solver.get_modulus(lhs, func)?;
let modulus_rhs = self.bigint_solver.get_modulus(rhs, func)?;
if modulus_lhs != modulus_rhs {
return Err(BlackBoxResolutionError::Failed(
func,
"moduli should be identical in BigInt operation".to_string(),
));
}
let id = self.create_bigint_id();
self.bigint_solver.bigint_op(lhs, rhs, id, func)?;
Ok(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use acvm::{
use crate::brillig::brillig_ir::{
brillig_variable::{BrilligVariable, BrilligVector, SingleAddrVariable},
debug_show::DebugToString,
BrilligBinaryOp, BrilligContext,
BrilligContext,
};

/// Transforms SSA's black box function calls into the corresponding brillig instructions
Expand Down Expand Up @@ -239,11 +239,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
BlackBoxFunc::RecursiveAggregation => {}
BlackBoxFunc::BigIntAdd => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntAdd {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -257,11 +256,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntSub => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntSub {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -275,11 +273,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntMul => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntMul {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -293,11 +290,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntDiv => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntDiv {
lhs: lhs.address,
rhs: rhs.address,
Expand Down Expand Up @@ -416,27 +412,3 @@ fn convert_array_or_vector<F: AcirField + DebugToString>(
),
}
}

fn prepare_bigint_output<F: AcirField + DebugToString>(
brillig_context: &mut BrilligContext<F>,
lhs_modulus: &SingleAddrVariable,
rhs_modulus: &SingleAddrVariable,
modulus_id: &SingleAddrVariable,
) {
// Check moduli
let condition = brillig_context.allocate_register();
let condition_adr = SingleAddrVariable { address: condition, bit_size: 1 };
brillig_context.binary_instruction(
*lhs_modulus,
*rhs_modulus,
condition_adr,
BrilligBinaryOp::Equals,
);
brillig_context.codegen_constrain(
condition_adr,
Some("moduli should be identical in BigInt operation".to_string()),
);
brillig_context.deallocate_register(condition);

brillig_context.mov_instruction(modulus_id.address, lhs_modulus.address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
HirStatement, Ident, IndexExpression, Literal, MemberAccessExpression,
MethodCallExpression, PrefixExpression,
},
node_interner::{DefinitionKind, ExprId, FuncId, ReferenceId, TraitMethodId},
node_interner::{DefinitionKind, ExprId, FuncId, TraitMethodId},
token::Tokens,
QuotedType, Shared, StructType, Type,
};
Expand Down Expand Up @@ -429,9 +429,9 @@ impl<'context> Elaborator<'context> {
struct_generics,
});

let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(Location::new(span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let struct_id = struct_type.borrow().id;
let reference_location = Location::new(span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

(expr, Type::Struct(struct_type, generics))
}
Expand Down Expand Up @@ -485,11 +485,11 @@ impl<'context> Elaborator<'context> {
}

if let Some(expected_index) = expected_index {
let struct_id = struct_type.borrow().id;
let referenced = ReferenceId::StructMember(struct_id, expected_index);
let reference =
ReferenceId::Reference(Location::new(field_name.span(), self.file), false);
self.interner.add_reference(referenced, reference);
self.interner.add_struct_member_reference(
struct_type.borrow().id,
expected_index,
Location::new(field_name.span(), self.file),
);
}

ret.push((field_name, resolved));
Expand Down
6 changes: 2 additions & 4 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,13 +1471,11 @@ impl<'context> Elaborator<'context> {

if let Some(trait_id) = trait_id {
let trait_name = trait_impl.trait_path.last_segment();

let referenced = ReferenceId::Trait(trait_id);
let reference = ReferenceId::Reference(
self.interner.add_trait_reference(
trait_id,
Location::new(trait_name.span(), trait_impl.file_id),
trait_name.is_self_type_name(),
);
self.interner.add_reference(referenced, reference);
}
}
}
Expand Down
31 changes: 9 additions & 22 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use crate::{
stmt::HirPattern,
},
macros_api::{HirExpression, Ident, Path, Pattern},
node_interner::{
DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, ReferenceId, TraitImplKind,
},
node_interner::{DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, TraitImplKind},
Shared, StructType, Type, TypeBindings,
};

Expand Down Expand Up @@ -204,14 +202,12 @@ impl<'context> Elaborator<'context> {

let struct_id = struct_type.borrow().id;

let referenced = ReferenceId::Struct(struct_id);
let reference = ReferenceId::Reference(Location::new(name_span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(name_span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

for (field_index, field) in fields.iter().enumerate() {
let referenced = ReferenceId::StructMember(struct_id, field_index);
let reference = ReferenceId::Reference(Location::new(field.0.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(field.0.span(), self.file);
self.interner.add_struct_member_reference(struct_id, field_index, reference_location);
}

HirPattern::Struct(expected_type, fields, location)
Expand Down Expand Up @@ -494,7 +490,6 @@ impl<'context> Elaborator<'context> {
// This lookup allows support of such statements: let x = foo::bar::SOME_GLOBAL + 10;
// If the expression is a singular indent, we search the resolver's current scope as normal.
let span = path.span();
let is_self_type_name = path.last_segment().is_self_type_name();
let (hir_ident, var_scope_index) = self.get_ident_from_path(path);

if hir_ident.id != DefinitionId::dummy_id() {
Expand All @@ -504,10 +499,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_function_dependency(current_item, func_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let function = ReferenceId::Function(func_id);
self.interner.add_reference(function, variable);
self.interner.add_function_reference(func_id, hir_ident.location);
}
DefinitionKind::Global(global_id) => {
if let Some(global) = self.unresolved_globals.remove(&global_id) {
Expand All @@ -517,10 +509,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, global_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let global = ReferenceId::Global(global_id);
self.interner.add_reference(global, variable);
self.interner.add_global_reference(global_id, hir_ident.location);
}
DefinitionKind::GenericType(_) => {
// Initialize numeric generics to a polymorphic integer type in case
Expand All @@ -536,10 +525,8 @@ impl<'context> Elaborator<'context> {
// only local variables can be captured by closures.
self.resolve_local_variable(hir_ident.clone(), var_scope_index);

let referenced = ReferenceId::Local(hir_ident.id);
let reference =
ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(hir_ident.id, reference_location);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl<'context> Elaborator<'context> {
resolver.resolve(self.def_maps, path.clone(), &mut Some(&mut references))?;

for (referenced, ident) in references.iter().zip(path.segments) {
let reference = ReferenceId::Reference(
self.interner.add_reference(
*referenced,
Location::new(ident.span(), self.file),
ident.is_self_type_name(),
);
self.interner.add_reference(*referenced, reference);
}
} else {
path_resolution = resolver.resolve(self.def_maps, path, &mut None)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
macros_api::{
ForLoopStatement, ForRange, HirStatement, LetStatement, Path, Statement, StatementKind,
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, ReferenceId, StmtId},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
Type,
};

Expand Down Expand Up @@ -255,9 +255,8 @@ impl<'context> Elaborator<'context> {
typ.follow_bindings()
};

let referenced = ReferenceId::Local(ident.id);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(ident.id, reference_location);

(HirLValue::Ident(ident.clone(), typ.clone()), typ, mutable)
}
Expand Down Expand Up @@ -380,9 +379,8 @@ impl<'context> Elaborator<'context> {
Type::Struct(s, args) => {
let s = s.borrow();
if let Some((field, index)) = s.get_field(field_name, args) {
let referenced = ReferenceId::StructMember(s.id, index);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_struct_member_reference(s.id, index, reference_location);

return Some((field, index));
}
Expand Down
30 changes: 10 additions & 20 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ use crate::{
UnaryOp, UnresolvedType, UnresolvedTypeData,
},
node_interner::{
DefinitionKind, DependencyId, ExprId, GlobalId, ReferenceId, TraitId, TraitImplKind,
TraitMethodId,
DefinitionKind, DependencyId, ExprId, GlobalId, TraitId, TraitImplKind, TraitMethodId,
},
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeVariable, TypeVariableKind,
};
Expand Down Expand Up @@ -154,30 +153,23 @@ impl<'context> Elaborator<'context> {
};

if let Some(unresolved_span) = typ.span {
let location = Location::new(unresolved_span, self.file);

match resolved_type {
Type::Struct(ref struct_type, _) => {
// Record the location of the type reference
self.interner.push_type_ref_location(
resolved_type.clone(),
Location::new(unresolved_span, self.file),
);
self.interner.push_type_ref_location(resolved_type.clone(), location);

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
self.interner.add_struct_reference(
struct_type.borrow().id,
location,
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
}
Type::Alias(ref alias_type, _) => {
let referenced = ReferenceId::Alias(alias_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
self.interner.add_alias_reference(alias_type.borrow().id, location);
}
_ => (),
}
Expand Down Expand Up @@ -369,10 +361,8 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, id);
}

let referenced = ReferenceId::Global(id);
let reference =
ReferenceId::Reference(Location::new(path.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(path.span(), self.file);
self.interner.add_global_reference(id, reference_location);

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
}
Expand Down
Loading

0 comments on commit 8403e84

Please sign in to comment.