Skip to content

Commit

Permalink
fix: Add Result to acir gen (#1927)
Browse files Browse the repository at this point in the history
Add Result to acir gen
  • Loading branch information
jfecher authored Jul 13, 2023
1 parent 8df4f05 commit 1f8fd51
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 89 deletions.
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn optimize_into_acir(
program: Program,
allow_log_ops: bool,
print_ssa_passes: bool,
) -> GeneratedAcir {
) -> Result<GeneratedAcir, RuntimeError> {
let abi_distinctness = program.return_distinctness;
let mut ssa = ssa_gen::generate_ssa(program)
.print(print_ssa_passes, "Initial SSA:")
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn experimental_create_circuit(
) -> Result<(Circuit, DebugInfo, Abi), RuntimeError> {
let func_sig = program.main_function_signature.clone();
let GeneratedAcir { current_witness_index, opcodes, return_witnesses, locations, .. } =
optimize_into_acir(program, show_output, enable_logging);
optimize_into_acir(program, show_output, enable_logging)?;

let abi = gen_abi(func_sig, return_witnesses.clone());
let public_abi = abi.clone().public_abi();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl AcirContext {
self.add_data(var_data)
}

pub(crate) fn get_location(&mut self) -> Option<Location> {
self.acir_ir.current_location
}

pub(crate) fn set_location(&mut self, location: Option<Location>) {
self.acir_ir.current_location = location;
}
Expand Down Expand Up @@ -271,7 +275,11 @@ impl AcirContext {
Ok(())
} else {
// Constraint is always false - this program is unprovable
Err(AcirGenError::BadConstantEquality { lhs: *lhs_const, rhs: *rhs_const })
Err(AcirGenError::BadConstantEquality {
lhs: *lhs_const,
rhs: *rhs_const,
location: self.get_location(),
})
}
} else {
self.acir_ir.assert_is_zero(
Expand Down
55 changes: 47 additions & 8 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/errors.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
use acvm::FieldElement;
use noirc_errors::Location;

use crate::errors::{RuntimeError, RuntimeErrorKind};

#[derive(Debug, PartialEq, Eq, Clone)]
pub(crate) enum AcirGenError {
InvalidRangeConstraint { num_bits: u32 },
IndexOutOfBounds { index: usize, array_size: usize },
UnsupportedIntegerSize { num_bits: u32, max_num_bits: u32 },
BadConstantEquality { lhs: FieldElement, rhs: FieldElement },
InvalidRangeConstraint { num_bits: u32, location: Option<Location> },
IndexOutOfBounds { index: usize, array_size: usize, location: Option<Location> },
UnsupportedIntegerSize { num_bits: u32, max_num_bits: u32, location: Option<Location> },
BadConstantEquality { lhs: FieldElement, rhs: FieldElement, location: Option<Location> },
}

impl AcirGenError {
pub(crate) fn message(&self) -> String {
match self {
AcirGenError::InvalidRangeConstraint { num_bits } => {
AcirGenError::InvalidRangeConstraint { num_bits, .. } => {
// Don't apply any constraints if the range is for the maximum number of bits or more.
format!(
"All Witnesses are by default u{num_bits} Applying this type does not apply any constraints.\n We also currently do not allow integers of size more than {num_bits}, this will be handled by BigIntegers.")
}
AcirGenError::IndexOutOfBounds { index, array_size } => {
AcirGenError::IndexOutOfBounds { index, array_size, .. } => {
format!("Index out of bounds, array has size {array_size}, but index was {index}")
}
AcirGenError::UnsupportedIntegerSize { num_bits, max_num_bits } => {
AcirGenError::UnsupportedIntegerSize { num_bits, max_num_bits, .. } => {
format!("Integer sized {num_bits} is over the max supported size of {max_num_bits}")
}
AcirGenError::BadConstantEquality { lhs, rhs } => {
AcirGenError::BadConstantEquality { lhs, rhs, .. } => {
format!("{lhs} and {rhs} constrained to be equal though they never can be")
}
}
}
}

impl From<AcirGenError> for RuntimeError {
fn from(error: AcirGenError) -> Self {
match error {
AcirGenError::InvalidRangeConstraint { num_bits, location } => {
let kind = RuntimeErrorKind::UnstructuredError {
message: format!(
"Failed range constraint when constraining to {num_bits} bits"
),
};
RuntimeError::new(kind, location)
}
AcirGenError::IndexOutOfBounds { index, array_size, location } => {
let kind = RuntimeErrorKind::ArrayOutOfBounds {
index: index as u128,
bound: array_size as u128,
};
RuntimeError::new(kind, location)
}
AcirGenError::UnsupportedIntegerSize { num_bits, max_num_bits, location } => {
let kind = RuntimeErrorKind::UnstructuredError {
message: format!("Unsupported integer size of {num_bits} bits. The maximum supported size is {max_num_bits} bits.")
};
RuntimeError::new(kind, location)
}
AcirGenError::BadConstantEquality { lhs: _, rhs: _, location } => {
// We avoid showing the actual lhs and rhs since most of the time they are just 0
// and 1 respectively. This would confuse users if a constraint such as
// assert(foo < bar) fails with "failed constraint: 0 = 1."
let kind =
RuntimeErrorKind::UnstructuredError { message: "Failed constraint".into() };
RuntimeError::new(kind, location)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ impl GeneratedAcir {
if num_bits >= FieldElement::max_num_bits() {
return Err(AcirGenError::InvalidRangeConstraint {
num_bits: FieldElement::max_num_bits(),
location: self.current_location,
});
};

Expand Down
Loading

0 comments on commit 1f8fd51

Please sign in to comment.