Skip to content

Commit

Permalink
Support assert statement (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
curryrasul authored Jun 13, 2024
1 parent 0334baa commit 8d41881
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This library enables the creation of arithmetic circuits from circom programs.
| | `UnderscoreSubstitution` ||
| | `ConstraintEquality` ||
| | `LogCall` ||
| | `Assert` | |
| | `Assert` | |
| **Expressions** | `Call` ||
| | `InfixOp` ||
| | `Number` ||
Expand Down
15 changes: 14 additions & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::compiler::{AGateType, Compiler};
use crate::program::ProgramError;
use crate::runtime::{
generate_u32, increment_indices, u32_to_access, Context, DataAccess, DataType, NestedValue,
Runtime, Signal, SubAccess, RETURN_VAR,
Runtime, RuntimeError, Signal, SubAccess, RETURN_VAR,
};
use circom_circom_algebra::num_traits::ToPrimitive;
use circom_program_structure::ast::{
Expand Down Expand Up @@ -170,6 +170,19 @@ pub fn process_statement(

Ok(())
}
Statement::Assert { arg, .. } => {
let access = process_expression(ac, runtime, program_archive, arg)?;
let result = runtime
.current_context()?
.get_variable_value(&access)?
.ok_or(ProgramError::EmptyDataItem)?;

if result == 0 {
return Err(ProgramError::RuntimeError(RuntimeError::AssertionFailed));
}

Ok(())
}
_ => Err(ProgramError::StatementNotImplemented),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,8 @@ pub enum RuntimeError {
NotAValue,
#[error("Unsupported data type")]
UnsupportedDataType,
#[error("Assertion failed")]
AssertionFailed,
}

impl From<RuntimeError> for ProgramError {
Expand Down

0 comments on commit 8d41881

Please sign in to comment.