
Bithoven is a smart contract language for composing powerful and secure instruments on Bitcoin. LR(1) parser with static analysis for compile-time safety. Formal verification paper: https://arxiv.org/abs/2601.01436
Formal Safety for Expressive Bitcoin Smart Contracts
Bithoven is a high-level, imperative language for Bitcoin smart contracts. It compiles to native Bitcoin Script for legacy (P2SH), segwit (P2WSH) and taproot (P2TR) outputs. You write readable contracts with if/else, named and typed witness inputs, and built-in timelock and signature primitives, instead of juggling a stack by hand.
What sets Bithoven apart is the analysis behind the syntax. Before emitting a single opcode, the compiler rejects any contract that could reach an undefined state, break a consensus encoding rule, consume a witness item other than exactly once and in stack order, leave a path without an explicit return, or let a path succeed without a valid signature. It also rejects any contract it can prove no witness could ever redeem.
if/else, verify and return instead of mental stack juggling.number, bool, string, signature).checksig results; numbers, byte strings, locktimes and public keys are range- and encoding-checked at compile time.legacy, segwit or taproot; multisig becomes OP_CHECKMULTISIG or OP_CHECKSIGADD as the target requires.libbitcoinconsensus.rust-miniscript.v0.1.0 is a major revision of the analyzer and back end, accompanying the paper.
signature item, and only the literal shape !checksig(..) was flagged. v0.1.0 decides, for every path, whether it can succeed while every checksig fails. Rewrites that 0.0.x compiled into anyone-can-spend scripts, such as checksig(..) == false, checksig(..) || flag or an inverted if checksig(..), are now rejected. !!checksig(..), which 0.0.x wrongly rejected, now compiles.DeadPath instead of compiling to an unspendable output.if needs an else, there is exactly one stack declaration per spending path, and paths that split at a branch must agree on the witness items consumed before it.sig; 0.0.x rejected this with VariableConsumed.verify, return and if operands must be bool; byte strings are capped at the 520-byte push limit; key literals must use the target's encoding.OP_NUMEQUAL, so an overflowing sum can no longer compare equal byte-wise, and long byte-string literals that could crash the 0.0.x compiler now compile.tests/README.md).Upgrading from 0.0.x? Contracts that relied on the looser rules may stop compiling. Give every
ifanelse(NoReturn), write exactly one declaration per spending path (DeclarationPathMismatch), and use 32-byte x-only keys undertaprootor 33-byte compressed keys underlegacy/segwit(MalformedPubkey).
# For CLI user
cargo install bithoven
# For rust user
cargo add bithoven
# For js user
npm install bithoven
Bithoven contracts are defined with a .bithoven extension. Below is an implementation of a standard Hashed Time-Locked Contract (HTLC), demonstrating how Bithoven simplifies conditional spending paths.
htlc.bithoven
pragma bithoven version 0.0.1;
pragma bithoven target segwit;
/*
* Stack input declarations: one per spending path, in the order the
* paths' returns appear below. Each lists its items in the order the path
* consumes them, so the first item (here the branch condition) is the top
* of the witness stack.
*/
(condition: bool, sig_alice: signature)
(condition: bool, preimage: string, sig_bob: signature)
{
// If 'condition' is true, we enter the Refund Path (Alice)
if condition {
// Enforce relative timelock of 1000 blocks
older 1000;
// If timelock is satisfied, Alice can spend with her signature
return checksig(sig_alice, "0245a6b3f8eeab8e88501a9a25391318dce9bf35e24c377ee82799543606bf5212");
} else {
// Redeem Path (Bob)
// Bob must reveal the secret preimage that hashes to the expected value
verify sha256(sha256(preimage)) == "53de742e2e323e3290234052a702458589c30d2c813bf9f866bef1b651c4e45f";
// If hash matches, Bob can spend with his signature
return checksig(sig_bob, "0345a6b3f8eeab8e88501a9a25391318dce9bf35e24c377ee82799543606bf5212");
}
}
When compiled, Bithoven translates the high-level imperative logic into the equivalent Bitcoin Script opcodes, handling the control flow and stack management automatically.
Command:
bithoven compile htlc.bithoven --format asm
Without --format, the CLI writes the ASM, hex and bytes to htlc.bithoven.json; --format hex and --format json print to stdout instead.
Generated Bitcoin Script (ASM, formatted for readability):
OP_IF
<0xe803> OP_CHECKSEQUENCEVERIFY OP_DROP
<pubkey_alice> OP_CHECKSIG
OP_ELSE
OP_HASH256 OP_TOALTSTACK <hash_digest> OP_FROMALTSTACK OP_SWAP OP_EQUALVERIFY
<pubkey_bob> OP_CHECKSIG
OP_ENDIF
Every check runs at compile time, and each rejection comes with a diagnostic pointing at the offending line. This contract looks signature-gated, but its only path succeeds exactly when the signature check fails:
pragma bithoven version 0.0.1;
pragma bithoven target segwit;
(sig_alice: signature)
{
return checksig(sig_alice, "0245a6b3f8eeab8e88501a9a25391318dce9bf35e24c377ee82799543606bf5212") == false;
}
Error at line 6:5: UselessSig("A checksig is present on the top-level path but it does not gate the spend: the return at line 6:5 can succeed without a valid signature.")
The signature-necessity check reasons about what each path computes, not about how it is spelled, so equivalent rewrites get the same verdict:
Contract body (.. elides the signature and key) | Verdict |
|---|---|
return !checksig(..); | ❌ UselessSig |
return checksig(..) == false; or != true; | ❌ UselessSig |
return checksig(..) || flag; | ❌ UselessSig |
if checksig(..) { return false; } else { return true; } | ❌ UselessSig |
if flag { return true; } else { return checksig(..); } | ❌ NoSigRequired |
verify false; return checksig(..); | ❌ DeadPath |
return checksig(..) == true; or return !!checksig(..); | ✅ compiles |