Skip to content

Commit

Permalink
feat: add assert_eq builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Aug 7, 2023
1 parent a72cc96 commit 3aa708f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,13 @@ impl Context {
result_ids: &[ValueId],
) -> Result<Vec<AcirValue>, RuntimeError> {
match intrinsic {
Intrinsic::AssertEq => {
let lhs = self.convert_value(arguments[0], dfg).into_var()?;
let rhs = self.convert_value(arguments[1], dfg).into_var()?;

self.acir_context.assert_eq_var(lhs, rhs)?;
Ok(Vec::new())
}
Intrinsic::BlackBox(black_box) => {
let inputs = vecmap(arguments, |arg| self.convert_value(*arg, dfg));

Expand Down
3 changes: 3 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub(crate) enum Intrinsic {
SliceInsert,
SliceRemove,
Println,
AssertEq,
ToBits(Endian),
ToRadix(Endian),
BlackBox(BlackBoxFunc),
Expand All @@ -48,6 +49,7 @@ pub(crate) enum Intrinsic {
impl std::fmt::Display for Intrinsic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Intrinsic::AssertEq => write!(f, "assert_eq"),
Intrinsic::Println => write!(f, "println"),
Intrinsic::Sort => write!(f, "arraysort"),
Intrinsic::ArrayLen => write!(f, "array_len"),
Expand All @@ -71,6 +73,7 @@ impl Intrinsic {
/// If there is no such intrinsic by that name, None is returned.
pub(crate) fn lookup(name: &str) -> Option<Intrinsic> {
match name {
"assert_eq" => Some(Intrinsic::AssertEq),
"println" => Some(Intrinsic::Println),
"arraysort" => Some(Intrinsic::Sort),
"array_len" => Some(Intrinsic::ArrayLen),
Expand Down
11 changes: 11 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ pub(super) fn simplify_call(
arguments.iter().map(|value_id| dfg.get_numeric_constant(*value_id)).collect();

match intrinsic {
Intrinsic::AssertEq => {
if let Some(constant_args) = constant_args {
if constant_args[0] == constant_args[1] {
SimplifyResult::Remove
} else {
SimplifyResult::None
}
} else {
SimplifyResult::None
}
}
Intrinsic::ToBits(endian) => {
if let Some(constant_args) = constant_args {
let field = constant_args[0];
Expand Down
3 changes: 3 additions & 0 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ unconstrained fn println<T>(input: T) {

#[foreign(recursive_aggregation)]
fn verify_proof<N>(_verification_key : [Field], _proof : [Field], _public_inputs : [Field], _key_hash : Field, _input_aggregation_object : [Field; N]) -> [Field; N] {}

#[builtin(assert_eq)]
fn assert_eq<T>(_lhs: T, _rhs: T) {}

0 comments on commit 3aa708f

Please sign in to comment.