Skip to content

Commit

Permalink
Add abort extern
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdweightman committed Nov 12, 2024
1 parent 8566f02 commit 6fba7e7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions zirgen/Conversions/Typing/BuiltinComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ function Div(lhs: Val, rhs: Val) {
}
extern Log(message: String, vals: Val...);
extern Abort();
extern Assert(x: Val, message: String);
)";
Expand Down
2 changes: 2 additions & 0 deletions zirgen/Main/RunTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ struct TestExternHandler : public zirgen::Zll::ExternHandler {
valPtrs[i] = &vals[i];
}
results = zirgen::Zll::ExternHandler::doExtern("log", message, valPtrs, outCount, failed);
} else if (name == "Abort") {
*failed = true;
} else if (name == "Assert") {
auto condition = args[0]->getBaseFieldVal();
llvm::StringRef message = args[1]->getAttr<mlir::StringAttr>().getValue();
Expand Down
13 changes: 13 additions & 0 deletions zirgen/circuit/keccak/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ impl<'a> CpuExecContext<'a> {
tracing::trace!("Read returns {val:?}");
Ok(val)
}

pub fn abort(&self) -> Result<()> {
Err(anyhow!("circuit aborted proving"))
}

pub fn assert(&self, condition: Val, message: &str) -> Result<()> {
if condition == Val::ZERO {
Err(anyhow!(message.to_owned()))
} else {
Ok(())
}
}

pub fn log(&self, message: &str, x: impl AsRef<[Val]>) -> Result<()> {
risc0_zirgen_dsl::codegen::default_log(message, x.as_ref())
}
Expand Down
7 changes: 7 additions & 0 deletions zirgen/dsl/test/abort.zir
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: not zirgen --test %s 2>&1 | FileCheck %s

test {
// CHECK: [0] Abort() -> ()
// CHECK-NEXT: error: Evaluation error occured
Abort();
}

0 comments on commit 6fba7e7

Please sign in to comment.