Skip to content

Commit 683f782

Browse files
authored
chore: git subrepo pull (merge) noir (#4331)
subrepo: subdir: "noir" merged: "b38ffdd7a" upstream: origin: "https://github.com/noir-lang/noir" branch: "aztec-packages" commit: "b38ffdd7a" git-subrepo: version: "0.4.6" origin: "https://github.com/ingydotnet/git-subrepo" commit: "110b9eb"
1 parent aeb4cf0 commit 683f782

34 files changed

Lines changed: 341 additions & 261 deletions

File tree

noir/.gitrepo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[subrepo]
77
remote = https://github.com/noir-lang/noir
88
branch = aztec-packages
9-
commit = ddd94a2f7f620da14e4222c2325119737b91908d
10-
parent = 4ddf8f42d005d3a1b72fac6b9d40c475a3c4231d
9+
commit = 9944bb170691d4e7905793a978019ba9504b1139
10+
parent = 1faead5bf5e07417e2d4452a2e3ff096a273a41a
1111
method = merge
1212
cmdver = 0.4.6
File renamed without changes.

noir/acvm-repo/acvm/src/compiler/optimizers/redundant_range.rs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use acir::{
33
opcodes::{BlackBoxFuncCall, FunctionInput},
44
Circuit, Opcode,
55
},
6-
native_types::{Expression, Witness},
7-
FieldElement,
6+
native_types::Witness,
87
};
98
use std::collections::{BTreeMap, HashSet};
109

@@ -105,9 +104,11 @@ impl RangeOptimizer {
105104
let mut new_order_list = Vec::with_capacity(order_list.len());
106105
let mut optimized_opcodes = Vec::with_capacity(self.circuit.opcodes.len());
107106
for (idx, opcode) in self.circuit.opcodes.into_iter().enumerate() {
108-
let (witness, num_bits) = match extract_range_opcode(&opcode) {
109-
Some(range_opcode) => range_opcode,
110-
None => {
107+
let (witness, num_bits) = match &opcode {
108+
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE { input }) => {
109+
(input.witness, input.num_bits)
110+
}
111+
_ => {
111112
// If its not the range opcode, add it to the opcode
112113
// list and continue;
113114
optimized_opcodes.push(opcode);
@@ -131,44 +132,19 @@ impl RangeOptimizer {
131132
if is_lowest_bit_size {
132133
already_seen_witness.insert(witness);
133134
new_order_list.push(order_list[idx]);
134-
optimized_opcodes.push(optimized_range_opcode(witness, num_bits));
135+
optimized_opcodes.push(opcode);
135136
}
136137
}
137138

138139
(Circuit { opcodes: optimized_opcodes, ..self.circuit }, new_order_list)
139140
}
140141
}
141142

142-
/// Extract the range opcode from the `Opcode` enum
143-
/// Returns None, if `Opcode` is not the range opcode.
144-
fn extract_range_opcode(opcode: &Opcode) -> Option<(Witness, u32)> {
145-
match opcode {
146-
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE { input }) => {
147-
Some((input.witness, input.num_bits))
148-
}
149-
_ => None,
150-
}
151-
}
152-
153-
fn optimized_range_opcode(witness: Witness, num_bits: u32) -> Opcode {
154-
if num_bits == 1 {
155-
Opcode::AssertZero(Expression {
156-
mul_terms: vec![(FieldElement::one(), witness, witness)],
157-
linear_combinations: vec![(-FieldElement::one(), witness)],
158-
q_c: FieldElement::zero(),
159-
})
160-
} else {
161-
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
162-
input: FunctionInput { witness, num_bits },
163-
})
164-
}
165-
}
166-
167143
#[cfg(test)]
168144
mod tests {
169145
use std::collections::BTreeSet;
170146

171-
use crate::compiler::optimizers::redundant_range::{extract_range_opcode, RangeOptimizer};
147+
use crate::compiler::optimizers::redundant_range::RangeOptimizer;
172148
use acir::{
173149
circuit::{
174150
opcodes::{BlackBoxFuncCall, FunctionInput},
@@ -218,11 +194,12 @@ mod tests {
218194
let (optimized_circuit, _) = optimizer.replace_redundant_ranges(acir_opcode_positions);
219195
assert_eq!(optimized_circuit.opcodes.len(), 1);
220196

221-
let (witness, num_bits) =
222-
extract_range_opcode(&optimized_circuit.opcodes[0]).expect("expected one range opcode");
223-
224-
assert_eq!(witness, Witness(1));
225-
assert_eq!(num_bits, 16);
197+
assert_eq!(
198+
optimized_circuit.opcodes[0],
199+
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
200+
input: FunctionInput { witness: Witness(1), num_bits: 16 }
201+
})
202+
);
226203
}
227204

228205
#[test]
@@ -240,15 +217,18 @@ mod tests {
240217
let (optimized_circuit, _) = optimizer.replace_redundant_ranges(acir_opcode_positions);
241218
assert_eq!(optimized_circuit.opcodes.len(), 2);
242219

243-
let (witness_a, num_bits_a) =
244-
extract_range_opcode(&optimized_circuit.opcodes[0]).expect("expected two range opcode");
245-
let (witness_b, num_bits_b) =
246-
extract_range_opcode(&optimized_circuit.opcodes[1]).expect("expected two range opcode");
247-
248-
assert_eq!(witness_a, Witness(1));
249-
assert_eq!(witness_b, Witness(2));
250-
assert_eq!(num_bits_a, 16);
251-
assert_eq!(num_bits_b, 23);
220+
assert_eq!(
221+
optimized_circuit.opcodes[0],
222+
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
223+
input: FunctionInput { witness: Witness(1), num_bits: 16 }
224+
})
225+
);
226+
assert_eq!(
227+
optimized_circuit.opcodes[1],
228+
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
229+
input: FunctionInput { witness: Witness(2), num_bits: 23 }
230+
})
231+
);
252232
}
253233

254234
#[test]

noir/aztec_macros/src/lib.rs

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ impl MacroProcessor for AztecMacro {
2626
transform(ast, crate_id, context)
2727
}
2828

29-
fn process_typed_ast(&self, crate_id: &CrateId, context: &mut HirContext) {
30-
transform_hir(crate_id, context)
29+
fn process_typed_ast(
30+
&self,
31+
crate_id: &CrateId,
32+
context: &mut HirContext,
33+
) -> Result<(), (MacroError, FileId)> {
34+
transform_hir(crate_id, context).map_err(|(err, file_id)| (err.into(), file_id))
3135
}
3236
}
3337

@@ -41,6 +45,7 @@ pub enum AztecMacroError {
4145
ContractHasTooManyFunctions { span: Span },
4246
ContractConstructorMissing { span: Span },
4347
UnsupportedFunctionArgumentType { span: Span, typ: UnresolvedTypeData },
48+
EventError { span: Span, message: String },
4449
}
4550

4651
impl From<AztecMacroError> for MacroError {
@@ -71,6 +76,11 @@ impl From<AztecMacroError> for MacroError {
7176
secondary_message: None,
7277
span: Some(span),
7378
},
79+
AztecMacroError::EventError { span, message } => MacroError {
80+
primary_message: message,
81+
secondary_message: None,
82+
span: Some(span),
83+
},
7484
}
7585
}
7686
}
@@ -237,8 +247,11 @@ fn transform(
237247
//
238248

239249
/// Completes the Hir with data gathered from type resolution
240-
fn transform_hir(crate_id: &CrateId, context: &mut HirContext) {
241-
transform_events(crate_id, context);
250+
fn transform_hir(
251+
crate_id: &CrateId,
252+
context: &mut HirContext,
253+
) -> Result<(), (AztecMacroError, FileId)> {
254+
transform_events(crate_id, context)
242255
}
243256

244257
/// Includes an import to the aztec library if it has not been included yet
@@ -472,19 +485,30 @@ fn collect_crate_structs(crate_id: &CrateId, context: &HirContext) -> Vec<Struct
472485
}
473486

474487
/// Substitutes the signature literal that was introduced in the selector method previously with the actual signature.
475-
fn transform_event(struct_id: StructId, interner: &mut NodeInterner) {
488+
fn transform_event(
489+
struct_id: StructId,
490+
interner: &mut NodeInterner,
491+
) -> Result<(), (AztecMacroError, FileId)> {
476492
let struct_type = interner.get_struct(struct_id);
477493
let selector_id = interner
478-
.lookup_method(&Type::Struct(struct_type, vec![]), struct_id, "selector", false)
479-
.expect("Selector method not found");
494+
.lookup_method(&Type::Struct(struct_type.clone(), vec![]), struct_id, "selector", false)
495+
.ok_or_else(|| {
496+
let error = AztecMacroError::EventError {
497+
span: struct_type.borrow().location.span,
498+
message: "Selector method not found".to_owned(),
499+
};
500+
(error, struct_type.borrow().location.file)
501+
})?;
480502
let selector_function = interner.function(&selector_id);
481503

482504
let compute_selector_statement = interner.statement(
483-
selector_function
484-
.block(interner)
485-
.statements()
486-
.first()
487-
.expect("Compute selector statement not found"),
505+
selector_function.block(interner).statements().first().ok_or_else(|| {
506+
let error = AztecMacroError::EventError {
507+
span: struct_type.borrow().location.span,
508+
message: "Compute selector statement not found".to_owned(),
509+
};
510+
(error, struct_type.borrow().location.file)
511+
})?,
488512
);
489513

490514
let compute_selector_expression = match compute_selector_statement {
@@ -494,12 +518,21 @@ fn transform_event(struct_id: StructId, interner: &mut NodeInterner) {
494518
},
495519
_ => None,
496520
}
497-
.expect("Compute selector statement is not a call expression");
498-
499-
let first_arg_id = compute_selector_expression
500-
.arguments
501-
.first()
502-
.expect("Missing argument for compute selector");
521+
.ok_or_else(|| {
522+
let error = AztecMacroError::EventError {
523+
span: struct_type.borrow().location.span,
524+
message: "Compute selector statement is not a call expression".to_owned(),
525+
};
526+
(error, struct_type.borrow().location.file)
527+
})?;
528+
529+
let first_arg_id = compute_selector_expression.arguments.first().ok_or_else(|| {
530+
let error = AztecMacroError::EventError {
531+
span: struct_type.borrow().location.span,
532+
message: "Compute selector statement is not a call expression".to_owned(),
533+
};
534+
(error, struct_type.borrow().location.file)
535+
})?;
503536

504537
match interner.expression(first_arg_id) {
505538
HirExpression::Literal(HirLiteral::Str(signature))
@@ -518,18 +551,29 @@ fn transform_event(struct_id: StructId, interner: &mut NodeInterner) {
518551
selector_literal_id,
519552
Type::String(Box::new(Type::Constant(signature.len() as u64))),
520553
);
554+
Ok(())
521555
}
522-
_ => unreachable!("Signature placeholder literal does not match"),
556+
_ => Err((
557+
AztecMacroError::EventError {
558+
span: struct_type.borrow().location.span,
559+
message: "Signature placeholder literal does not match".to_owned(),
560+
},
561+
struct_type.borrow().location.file,
562+
)),
523563
}
524564
}
525565

526-
fn transform_events(crate_id: &CrateId, context: &mut HirContext) {
566+
fn transform_events(
567+
crate_id: &CrateId,
568+
context: &mut HirContext,
569+
) -> Result<(), (AztecMacroError, FileId)> {
527570
for struct_id in collect_crate_structs(crate_id, context) {
528571
let attributes = context.def_interner.struct_attributes(&struct_id);
529572
if attributes.iter().any(|attr| matches!(attr, SecondaryAttribute::Event)) {
530-
transform_event(struct_id, &mut context.def_interner);
573+
transform_event(struct_id, &mut context.def_interner)?;
531574
}
532575
}
576+
Ok(())
533577
}
534578

535579
const SIGNATURE_PLACEHOLDER: &str = "SIGNATURE_PLACEHOLDER";

noir/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,20 +1441,22 @@ impl AcirContext {
14411441
inputs: Vec<AcirValue>,
14421442
outputs: Vec<AcirType>,
14431443
attempt_execution: bool,
1444-
) -> Result<Vec<AcirValue>, InternalError> {
1445-
let b_inputs = try_vecmap(inputs, |i| match i {
1446-
AcirValue::Var(var, _) => Ok(BrilligInputs::Single(self.var_to_expression(var)?)),
1447-
AcirValue::Array(vars) => {
1448-
let mut var_expressions: Vec<Expression> = Vec::new();
1449-
for var in vars {
1450-
self.brillig_array_input(&mut var_expressions, var)?;
1444+
) -> Result<Vec<AcirValue>, RuntimeError> {
1445+
let b_inputs = try_vecmap(inputs, |i| -> Result<_, InternalError> {
1446+
match i {
1447+
AcirValue::Var(var, _) => Ok(BrilligInputs::Single(self.var_to_expression(var)?)),
1448+
AcirValue::Array(vars) => {
1449+
let mut var_expressions: Vec<Expression> = Vec::new();
1450+
for var in vars {
1451+
self.brillig_array_input(&mut var_expressions, var)?;
1452+
}
1453+
Ok(BrilligInputs::Array(var_expressions))
1454+
}
1455+
AcirValue::DynamicArray(_) => {
1456+
let mut var_expressions = Vec::new();
1457+
self.brillig_array_input(&mut var_expressions, i)?;
1458+
Ok(BrilligInputs::Array(var_expressions))
14511459
}
1452-
Ok(BrilligInputs::Array(var_expressions))
1453-
}
1454-
AcirValue::DynamicArray(_) => {
1455-
let mut var_expressions = Vec::new();
1456-
self.brillig_array_input(&mut var_expressions, i)?;
1457-
Ok(BrilligInputs::Array(var_expressions))
14581460
}
14591461
})?;
14601462

@@ -1489,6 +1491,34 @@ impl AcirContext {
14891491
let predicate = self.var_to_expression(predicate)?;
14901492
self.acir_ir.brillig(Some(predicate), generated_brillig, b_inputs, b_outputs);
14911493

1494+
fn range_constraint_value(
1495+
context: &mut AcirContext,
1496+
value: &AcirValue,
1497+
) -> Result<(), RuntimeError> {
1498+
match value {
1499+
AcirValue::Var(var, typ) => {
1500+
let numeric_type = match typ {
1501+
AcirType::NumericType(numeric_type) => numeric_type,
1502+
_ => unreachable!("`AcirValue::Var` may only hold primitive values"),
1503+
};
1504+
context.range_constrain_var(*var, numeric_type, None)?;
1505+
}
1506+
AcirValue::Array(values) => {
1507+
for value in values {
1508+
range_constraint_value(context, value)?;
1509+
}
1510+
}
1511+
AcirValue::DynamicArray(_) => {
1512+
unreachable!("Brillig opcodes cannot return dynamic arrays")
1513+
}
1514+
}
1515+
Ok(())
1516+
}
1517+
1518+
for output_var in &outputs_var {
1519+
range_constraint_value(self, output_var)?;
1520+
}
1521+
14921522
Ok(outputs_var)
14931523
}
14941524

noir/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::hir::resolution::{
1414
use crate::hir::type_check::{type_check_func, TypeCheckError, TypeChecker};
1515
use crate::hir::Context;
1616

17-
use crate::macros_api::MacroProcessor;
17+
use crate::macros_api::{MacroError, MacroProcessor};
1818
use crate::node_interner::{FuncId, NodeInterner, StmtId, StructId, TraitId, TypeAliasId};
1919

2020
use crate::parser::{ParserError, SortedModule};
@@ -155,6 +155,12 @@ impl From<CompilationError> for CustomDiagnostic {
155155
}
156156
}
157157

158+
impl From<MacroError> for CompilationError {
159+
fn from(value: MacroError) -> Self {
160+
CompilationError::DefinitionError(DefCollectorErrorKind::MacroError(value))
161+
}
162+
}
163+
158164
impl From<ParserError> for CompilationError {
159165
fn from(value: ParserError) -> Self {
160166
CompilationError::ParseError(value)
@@ -359,7 +365,11 @@ impl DefCollector {
359365
errors.extend(resolved_globals.errors);
360366

361367
for macro_processor in macro_processors {
362-
macro_processor.process_typed_ast(&crate_id, context);
368+
macro_processor.process_typed_ast(&crate_id, context).unwrap_or_else(
369+
|(macro_err, file_id)| {
370+
errors.push((macro_err.into(), file_id));
371+
},
372+
);
363373
}
364374
errors.extend(type_check_globals(&mut context.def_interner, resolved_globals.globals));
365375

0 commit comments

Comments
 (0)