Skip to content

Commit

Permalink
Merge branch 'master' into 7380-nargo-exec-fwd
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh authored Feb 28, 2025
2 parents 5fe2c77 + fdd2fe7 commit 7c7f931
Show file tree
Hide file tree
Showing 57 changed files with 1,189 additions and 971 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ codegen

**/cspell.json
!./cspell.json

mutants.out
mutants.out.old
47 changes: 46 additions & 1 deletion acvm-repo/acvm/src/pwg/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,52 @@ mod tests {
use acir::FieldElement;

#[test]
fn expression_solver_smoke_test() {
fn solves_simple_assignment() {
let a = Witness(0);

// a - 1 == 0;
let opcode_a = Expression {
mul_terms: vec![],
linear_combinations: vec![(FieldElement::one(), a)],
q_c: -FieldElement::one(),
};

let mut values = WitnessMap::new();
assert_eq!(ExpressionSolver::solve(&mut values, &opcode_a), Ok(()));

assert_eq!(values.get(&a).unwrap(), &FieldElement::from(1_i128));
}

#[test]
fn solves_unknown_in_mul_term() {
let a = Witness(0);
let b = Witness(1);
let c = Witness(2);
let d = Witness(3);

// a * b - b - c - d == 0;
let opcode_a = Expression {
mul_terms: vec![(FieldElement::one(), a, b)],
linear_combinations: vec![
(-FieldElement::one(), b),
(-FieldElement::one(), c),
(-FieldElement::one(), d),
],
q_c: FieldElement::zero(),
};

let mut values = WitnessMap::new();
values.insert(b, FieldElement::from(2_i128));
values.insert(c, FieldElement::from(1_i128));
values.insert(d, FieldElement::from(1_i128));

assert_eq!(ExpressionSolver::solve(&mut values, &opcode_a), Ok(()));

assert_eq!(values.get(&a).unwrap(), &FieldElement::from(2_i128));
}

#[test]
fn solves_unknown_in_linear_term() {
let a = Witness(0);
let b = Witness(1);
let c = Witness(2);
Expand Down
9 changes: 4 additions & 5 deletions acvm-repo/acvm/src/pwg/blackbox/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ pub(crate) fn solve_poseidon2_permutation_opcode<F: AcirField>(
}

// Read witness assignments
let mut state = Vec::new();
for input in inputs.iter() {
let witness_assignment = input_to_value(initial_witness, *input, false)?;
state.push(witness_assignment);
}
let state: Vec<F> = inputs
.iter()
.map(|input| input_to_value(initial_witness, *input, false))
.collect::<Result<_, _>>()?;

let state = backend.poseidon2_permutation(&state, len)?;

Expand Down
33 changes: 33 additions & 0 deletions acvm-repo/acvm/src/pwg/blackbox/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,36 @@ pub(crate) fn solve_range_opcode<F: AcirField>(
}
Ok(())
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use acir::{
FieldElement,
circuit::opcodes::FunctionInput,
native_types::{Witness, WitnessMap},
};

use crate::pwg::blackbox::solve_range_opcode;

#[test]
fn rejects_too_large_inputs() {
let witness_map =
WitnessMap::from(BTreeMap::from([(Witness(0), FieldElement::from(256u32))]));
let input: FunctionInput<FieldElement> = FunctionInput::witness(Witness(0), 8);
assert!(solve_range_opcode(&witness_map, &input, false).is_err());
}

#[test]
fn accepts_valid_inputs() {
let values: [u32; 4] = [0, 1, 8, 255];

for value in values {
let witness_map =
WitnessMap::from(BTreeMap::from([(Witness(0), FieldElement::from(value))]));
let input: FunctionInput<FieldElement> = FunctionInput::witness(Witness(0), 8);
assert!(solve_range_opcode(&witness_map, &input, false).is_ok());
}
}
}
4 changes: 2 additions & 2 deletions acvm-repo/blackbox_solver/src/curve_specific_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub trait BlackBoxFunctionSolver<F> {
) -> Result<(F, F, F), BlackBoxResolutionError>;
fn poseidon2_permutation(
&self,
_inputs: &[F],
_len: u32,
inputs: &[F],
len: u32,
) -> Result<Vec<F>, BlackBoxResolutionError>;
}

Expand Down
52 changes: 23 additions & 29 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::Args;
use fm::{FileId, FileManager};
use iter_extended::vecmap;
use noirc_abi::{AbiParameter, AbiType, AbiValue};
use noirc_errors::{CustomDiagnostic, DiagnosticKind, FileDiagnostic};
use noirc_errors::{CustomDiagnostic, DiagnosticKind};
use noirc_evaluator::brillig::BrilligOptions;
use noirc_evaluator::create_program;
use noirc_evaluator::errors::RuntimeError;
Expand Down Expand Up @@ -227,8 +227,8 @@ impl From<RuntimeError> for CompileError {
}
}

impl From<CompileError> for FileDiagnostic {
fn from(error: CompileError) -> FileDiagnostic {
impl From<CompileError> for CustomDiagnostic {
fn from(error: CompileError) -> CustomDiagnostic {
match error {
CompileError::RuntimeError(err) => err.into(),
CompileError::MonomorphizationError(err) => err.into(),
Expand All @@ -237,10 +237,10 @@ impl From<CompileError> for FileDiagnostic {
}

/// Helper type used to signify where only warnings are expected in file diagnostics
pub type Warnings = Vec<FileDiagnostic>;
pub type Warnings = Vec<CustomDiagnostic>;

/// Helper type used to signify where errors or warnings are expected in file diagnostics
pub type ErrorsAndWarnings = Vec<FileDiagnostic>;
pub type ErrorsAndWarnings = Vec<CustomDiagnostic>;

/// Helper type for connecting a compilation artifact to the errors or warnings which were produced during compilation.
pub type CompilationResult<T> = Result<(T, Warnings), ErrorsAndWarnings>;
Expand Down Expand Up @@ -350,20 +350,16 @@ pub fn check_crate(
) -> CompilationResult<()> {
let diagnostics = CrateDefMap::collect_defs(crate_id, context, options.frontend_options());
let crate_files = context.crate_files(&crate_id);
let warnings_and_errors: Vec<FileDiagnostic> = diagnostics
.into_iter()
.map(|error| {
let location = error.location();
let diagnostic = CustomDiagnostic::from(&error);
diagnostic.in_file(location.file)
})
let warnings_and_errors: Vec<CustomDiagnostic> = diagnostics
.iter()
.map(CustomDiagnostic::from)
.filter(|diagnostic| {
// We filter out any warnings if they're going to be ignored later on to free up memory.
!options.silence_warnings || diagnostic.diagnostic.kind != DiagnosticKind::Warning
!options.silence_warnings || diagnostic.kind != DiagnosticKind::Warning
})
.filter(|error| {
// Only keep warnings from the crate we are checking
if error.diagnostic.is_warning() { crate_files.contains(&error.file_id) } else { true }
if error.is_warning() { crate_files.contains(&error.file) } else { true }
})
.collect();

Expand Down Expand Up @@ -401,16 +397,16 @@ pub fn compile_main(
// TODO(#2155): This error might be a better to exist in Nargo
let err = CustomDiagnostic::from_message(
"cannot compile crate into a program as it does not contain a `main` function",
)
.in_file(FileId::default());
FileId::default(),
);
vec![err]
})?;

let compiled_program =
compile_no_check(context, options, main, cached_program, options.force_compile)
.map_err(FileDiagnostic::from)?;
.map_err(|error| vec![CustomDiagnostic::from(error)])?;

let compilation_warnings = vecmap(compiled_program.warnings.clone(), FileDiagnostic::from);
let compilation_warnings = vecmap(compiled_program.warnings.clone(), CustomDiagnostic::from);
if options.deny_warnings && !compilation_warnings.is_empty() {
return Err(compilation_warnings);
}
Expand Down Expand Up @@ -439,14 +435,16 @@ pub fn compile_contract(
let mut errors = warnings;

if contracts.len() > 1 {
let err = CustomDiagnostic::from_message("Packages are limited to a single contract")
.in_file(FileId::default());
let err = CustomDiagnostic::from_message(
"Packages are limited to a single contract",
FileId::default(),
);
return Err(vec![err]);
} else if contracts.is_empty() {
let err = CustomDiagnostic::from_message(
"cannot compile crate into a contract as it does not contain any contracts",
)
.in_file(FileId::default());
FileId::default(),
);
return Err(vec![err]);
};

Expand Down Expand Up @@ -483,12 +481,8 @@ pub fn compile_contract(
}

/// True if there are (non-warning) errors present and we should halt compilation
fn has_errors(errors: &[FileDiagnostic], deny_warnings: bool) -> bool {
if deny_warnings {
!errors.is_empty()
} else {
errors.iter().any(|error| error.diagnostic.is_error())
}
fn has_errors(errors: &[CustomDiagnostic], deny_warnings: bool) -> bool {
if deny_warnings { !errors.is_empty() } else { errors.iter().any(|error| error.is_error()) }
}

/// Compile all of the functions associated with a Noir contract.
Expand Down Expand Up @@ -525,7 +519,7 @@ fn compile_contract_inner(
let function = match compile_no_check(context, &options, function_id, None, true) {
Ok(function) => function,
Err(new_error) => {
errors.push(FileDiagnostic::from(new_error));
errors.push(new_error.into());
continue;
}
};
Expand Down
8 changes: 4 additions & 4 deletions compiler/noirc_driver/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ contract Bar {}";

assert_eq!(
errors,
vec![
CustomDiagnostic::from_message("Packages are limited to a single contract")
.in_file(FileId::default())
],
vec![CustomDiagnostic::from_message(
"Packages are limited to a single contract",
FileId::default()
)],
"stdlib is producing warnings"
);

Expand Down
18 changes: 0 additions & 18 deletions compiler/noirc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,3 @@ mod position;
pub mod reporter;
pub use position::{Located, Location, Position, Span, Spanned};
pub use reporter::{CustomDiagnostic, DiagnosticKind};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiagnostic {
pub file_id: fm::FileId,
pub diagnostic: CustomDiagnostic,
}

impl FileDiagnostic {
pub fn new(file_id: fm::FileId, diagnostic: CustomDiagnostic) -> FileDiagnostic {
FileDiagnostic { file_id, diagnostic }
}
}

impl From<FileDiagnostic> for Vec<FileDiagnostic> {
fn from(value: FileDiagnostic) -> Self {
vec![value]
}
}
22 changes: 11 additions & 11 deletions compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::io::IsTerminal;

use crate::{FileDiagnostic, Location, Span};
use crate::{Location, Span};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::Files;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomDiagnostic {
pub file: fm::FileId,
pub message: String,
pub secondaries: Vec<CustomLabel>,
pub notes: Vec<String>,
Expand Down Expand Up @@ -35,8 +36,9 @@ pub struct ReportedErrors {
}

impl CustomDiagnostic {
pub fn from_message(msg: &str) -> CustomDiagnostic {
pub fn from_message(msg: &str, file: fm::FileId) -> CustomDiagnostic {
Self {
file,
message: msg.to_owned(),
secondaries: Vec::new(),
notes: Vec::new(),
Expand All @@ -54,6 +56,7 @@ impl CustomDiagnostic {
kind: DiagnosticKind,
) -> CustomDiagnostic {
CustomDiagnostic {
file: secondary_location.file,
message: primary_message,
secondaries: vec![CustomLabel::new(secondary_message, secondary_location)],
notes: Vec::new(),
Expand Down Expand Up @@ -109,6 +112,7 @@ impl CustomDiagnostic {
secondary_location: Location,
) -> CustomDiagnostic {
CustomDiagnostic {
file: secondary_location.file,
message: primary_message,
secondaries: vec![CustomLabel::new(secondary_message, secondary_location)],
notes: Vec::new(),
Expand All @@ -119,10 +123,6 @@ impl CustomDiagnostic {
}
}

pub fn in_file(self, file_id: fm::FileId) -> FileDiagnostic {
FileDiagnostic::new(file_id, self)
}

pub fn with_call_stack(mut self, call_stack: Vec<Location>) -> Self {
self.call_stack = call_stack;
self
Expand Down Expand Up @@ -185,16 +185,16 @@ impl CustomLabel {
/// of diagnostics that were errors.
pub fn report_all<'files>(
files: &'files impl Files<'files, FileId = fm::FileId>,
diagnostics: &[FileDiagnostic],
diagnostics: &[CustomDiagnostic],
deny_warnings: bool,
silence_warnings: bool,
) -> ReportedErrors {
// Report warnings before any errors
let (warnings_and_bugs, mut errors): (Vec<_>, _) =
diagnostics.iter().partition(|item| !item.diagnostic.is_error());
diagnostics.iter().partition(|item| !item.is_error());

let (warnings, mut bugs): (Vec<_>, _) =
warnings_and_bugs.iter().partition(|item| item.diagnostic.is_warning());
warnings_and_bugs.iter().partition(|item| item.is_warning());
let mut diagnostics = if silence_warnings { Vec::new() } else { warnings };
diagnostics.append(&mut bugs);
diagnostics.append(&mut errors);
Expand All @@ -205,14 +205,14 @@ pub fn report_all<'files>(
ReportedErrors { error_count }
}

impl FileDiagnostic {
impl CustomDiagnostic {
/// Print the report; return true if it was an error.
pub fn report<'files>(
&self,
files: &'files impl Files<'files, FileId = fm::FileId>,
deny_warnings: bool,
) -> bool {
report(files, &self.diagnostic, deny_warnings)
report(files, self, deny_warnings)
}
}

Expand Down
Loading

0 comments on commit 7c7f931

Please sign in to comment.