Skip to content

Blog

The hax blog. Here you find announcement, development news, and more.

Core-models: modelling Rust dependencies in Rust

Verification has to be scoped: verifying a system amounts to explicitly stating what properties it assumes of its dependencies, what guarantees it provides to its users, and in between those two interfaces, checking that the system behaves accordingly. For code, dependencies can be external libraries, system calls, or the primitives of the language itself. The set of assumptions about the dependencies is referred to as a model.

Specifically for Rust, almost all crates depend on the functions and type definitions coming from core and alloc libraries. Any Rust verification pipeline, like hax, needs to model them in some way. While they are written in Rust, those libraries are highly optimized and intertwined with the compiler, making them a hassle to handle. A typical workaround is to exclude those libraries from the translation, and provide directly in the backend (Lean, F*, etc) a model of what they are doing. Such model is crucially part of the Trusted Computing Base (TCB).

Yet, writing backend specific models is time-consuming, error-prone, hard to review, test and extend. This post present hax's solution to this problem: we write the models of core and alloc directly in rust, providing an equivalent but simpler implementation. Being in Rust, this model can be heavily tested against the real core library, using a variety of Rust testing tools (see below) and can be reviewed at the rust level, improving the overall confidence. And it factors out the modeling effort by using the same model for all backends.

This post gives more details and motivation for the setup.

The issues with backend-specific models of core

For example, let us consider the following Rust code:

fn length(x: &[u8]) -> usize {
    x.len()
}

Open this code snippet in the playground

Extracts to the following F* code:

let length (x: t_Slice u8) : usize =
  Core_models.Slice.impl__len #u8 x

Here, the translation didn't do anything clever: the semantics of the rust program only depends on the definitions of t_Slice and Core_models.Slice.impl__len that are provided by the hax F* proof library.

Until about a year ago, we relied on standard library models written specifically for each backend. This approach had many issues: - The work has to be redone for each backend (Lean, F, ProVerif, etc), increasing the TCB. - Writing a semantically correct model for every possible corner case is hard, even just for bounded arithmetic (see this hax bug for instance). - The models have to be updated every time there is a change in the translation. Keeping up with upstream updates of core is challenging. - We extended the models on-demand*, when we stumbled upon missing entries in real proof campaigns, or when hax users reported missing definitions. The models were always somewhat incomplete.

One natural idea to address all these would be to directly extract the Rust core and alloc crates using the same toolchain. However, core is an extremely challenging rust crate: besides it sheer size, it uses nightly and hard-to-support Rust features, and is highly optimized.

Overall, even if hax could extract core, the resulting backend library would be hard to use in proofs and reason about. There is value in abstracting the content of core and only reason about simpler model of the library.

Presenting hax's core-models

The key idea of hax's approach to handling core is surprisingly simple: we provide a model of core written in Rust. This model depends on a small, well identified set of primitives (integers, sequences, strings, etc), called rust-primitives. This has several benefits:

  • Factoring backend-models. Extracting the models from rust effectively reduces the bakend-specific modeling problem from the whole of core to only the rust-primitives, factoring out the backend-agnostic part. This makes any improvement of coverage automatically multi-backend.
  • Rust level reviews. While reviewing models of core requires expertise in both Rust and the given backend, writing them directly in Rust makes the review process much easier.
  • Heavy testing. Detailed below.

We started using this about a year ago and this has helped us improve our coverage of Rust core with less maintenance burden. The models reproduce the behavior as simply as possible so they produce a proof library that is as good for proofs as the hand-written models were. We first replaced the hand-written F* library by it, and then started using it for the legacy Lean backend. More recently, we changed our Lean translation to use aeneas instead of the hax engine, and we also use an extraction of core-models as the basis for our proof library. This required adding a specific option to aeneas so that the calls to core it produces are compatible, but overall using core-models to get a proof library for a new backend is faster than writing it by hand.

Testing that core-models behaves as core

The current architecture has three main trusted components: - The Rust models - The hand-written model of rust-primitives for each backend - The translation process

We are trying to increase trust in those three parts, and having models in Rust gives us an advantage. Indeed, as the models are executable, it becomes much easier to test that the models behave like the functions from core that they specify. For this we use property-based testing: each model type implements a trait Inject that allows to take a value from the core type and convert it to its value in the corresponding core-models type. Then most tests are quite simple to write and look like: for any value x, core_f(x).inject() == core_models_f(x.inject()). We also added CI checks for test coverage with a requirement to cover all models with a property test. We use llvm-cov for this and require 100% coverage of lines, functions and regions. We have a handful of justified exclusions but this has helped strengthening testing where needed. We also run cargo-mutants once a week to get more subtle indications of where testing is insufficient. And this has helped us to improve the testing of functions like min, max and more Ord operations where it is easy to forget to test that the model returns the right element in case of equality.

But this doesn't catch bugs in the primitives (or in the translation), so we added a second kind of testing: Rust equivalence tests. The idea is to check that a certain call to a core function still returns the same value after translation. For this we made sure that all the models are executable in Lean after translation. Then we wrote a series of test of the form core_f(x0) == y0, core_f(x1) == y1, etc. These tests are then translated to Lean and re-checked on the Lean side, executing through the translated core_models and hand-written Lean for rust primitives. These equivalence tests are in the Lean backend only for now, and we don't check coverage so we have room to improve.

This process is quite new so we still need to evaluate how effective it is to prevent soundness bugs. We perform regular AI audits to track inconsistencies, and we would like to encourage contributors to report bugs, or give feedback about how we can increase trust.

Increasing the coverage of core

We like the extensibility and trust that this approach brings, however we still cover a limited (even if increased) subset of the Rust core library. We added some generated measurements of the coverage we provide (for the modules we model) to track the progress. As I write this blogpost, we cover 35 out of 55 modules of core, and in these modules we cover about 20% of the items (8% of 11 modules for alloc, with 4 more modules to add). We hope to make progress on these metrics in the coming months. With the testing in place, and the clear specification for the models, we believe that AI agents can be effective for this, which has the potential to speed up progress. The core-models are also a great place to welcome external contributions as they are quite self-contained, and hax users can help improve the support for the core functions they need for their projects. If you want to contribute, learn more details and check the guidelines on github.

hax 0.4: easier to install and configure, and a new Lean backend

We are happy to announce hax 0.4. The release brings a one-command installation for the Lean backend, proof scenarios configured in a hax.toml file, a new Lean backend built on Aeneas, and new capabilities for the specification macros. This post walks through these highlights. The changelog lists everything else, including many fixes and improvements to the engine, the proof libraries, and the core models.

Installing hax

Until now, installing hax meant setting up an OCaml toolchain for the hax engine or using Nix or Docker. The new Lean backend does not use the hax engine. Instead, the cargo-hax command line tool drives Charon and Aeneas, which translate Rust into Lean. Installing cargo-hax and a Lean toolchain is therefore enough, given a C compiler and rustup, which Charon uses at extraction time. hax 0.4 publishes cargo-hax as a pre-built binary for Linux (x86_64 and aarch64) and macOS (aarch64). The binary needs glibc 2.35 or newer on Linux, and macOS 11 or newer. With cargo-binstall available, one command installs it:

cargo binstall cargo-hax

cargo install --locked cargo-hax builds the same binary from crates.io and covers systems the pre-built binary does not support. Charon and Aeneas need no manual installation. hax downloads them on first use, verifies them against a manifest shipped with the release, and caches them.

cargo-hax and hax-lib are released together and must match. A 0.4.2 binary accepts only hax-lib 0.4.2 and otherwise aborts with instructions on how to resolve the mismatch.

The F*, Rocq, ProVerif, and other backends still run through the hax engine and need the full installation via the setup script, Nix, or Docker, as described in the README.

Configuring a project

Before 0.4, a cargo hax invocation carried its configuration as flags: item selection, backend options, extra tool arguments. With 0.4, a project stores this configuration as proof scenarios in a hax.toml next to its Cargo.toml. The barrett example's hax.toml declares two scenarios, one for Lean and one for F*:

[scenario.barrett]
backend = "lean"

[scenario.barrett-fstar]
backend = "fstar"
z3rlimit = 100

cargo hax extract runs every scenario of the project, and cargo hax extract barrett runs a single scenario. Each scenario writes to proofs/<scenario>/<backend>/, so several extractions of one crate coexist. Scenarios support item selection (currently for the Lean backend only), cargo features, environment variables, and backend-specific options. The manual documents the keys.

Charon, Aeneas, the Lean toolchain, and the hax Lean library have default versions shipped and tested with each hax release. Most projects should use these defaults. If needed, hax.toml can override them: [tools] pins Charon and Aeneas, while [versions] pins the Lean toolchain and the hax Lean library, which models Rust core. This is an advanced option: other combinations are untested and are your responsibility to maintain across hax releases.

The new cargo hax tools subcommands let you inspect the versions in use, install them ahead of time for CI or offline work, and clean the tool cache.

The hax version itself can be pinned alongside the hax-lib version in Cargo.toml with cargo-run-bin:

[package.metadata.bin]
cargo-hax = { version = "0.4.2", bins = ["cargo-hax"], locked = true }

cargo-run-bin installs the pinned version on demand, and after cargo bin --sync-aliases, the usual cargo hax invocation uses that version.

Finally, cargo hax now exits with a non-zero status when an extraction fails. This lets a scenario run serve as a CI check, but CI scripts that relied on a zero exit status need adjusting. cargo hax extract followed by git diff --exit-code detects when the committed extraction output is out of date.

The Lean/Aeneas backend

In the new Lean backend, Charon compiles Rust into an intermediate representation, and Aeneas translates it into pure functional Lean code. On top of this pipeline, hax extracts hax_lib pre- and postconditions as Lean specifications, provides the hax Lean library, and generates a complete Lean package that builds with lake build. The previous Lean backend, based on the hax engine, remains available as legacy-lean.

Take a slightly simplified version of the Barrett reduction from the barrett example:

#[hax_lib::requires((i64::from(value) >= -BARRETT_R && i64::from(value) <= BARRETT_R))]
#[hax_lib::ensures(|result| result > -FIELD_MODULUS && result < FIELD_MODULUS &&
                       result.rem_euclid(FIELD_MODULUS) == value.rem_euclid(FIELD_MODULUS))]
pub fn barrett_reduce(value: FieldElement) -> FieldElement {
    let t = i64::from(value) * BARRETT_MULTIPLIER;
    let t = t + (BARRETT_R >> 1);
    let quotient = t >> BARRETT_SHIFT;
    let quotient = quotient as i32;
    let sub = quotient * FIELD_MODULUS;
    value - sub
}

Running the barrett scenario turns the function into a definition in the RustM monad, which models failures such as panics and integer overflow. The requires and ensures attributes become barrett_reduce.pre and barrett_reduce.post, and hax combines them into a specification:

def barrett_reduce.spec (value : Std.I32) : Prop :=
  (barrett_reduce.pre value).holds →
  ⦃ ⌜ True ⌝ ⦄
  barrett_reduce value
  ⦃ ⇓ res => ⌜ (barrett_reduce.post value res).holds ⌝ ⦄

The ⦃ ... ⦄ brackets enclose a Hoare triple around the call, with a postcondition on the result res. The triple itself has the trivial precondition True, and the actual precondition is the implication in front of it. The specification states that, if the precondition holds, the function returns without failing and its result satisfies the postcondition.

hax also generates a theorem for this specification, whose proof is sorry, Lean's placeholder for a missing proof. The theorem lives in a Verification folder that hax never overwrites. The barrett example contains the finished proof, which uses the hax_mvcgen tactic and grind. The Lean quick start shows the workflow, the Lean tutorial walks through proving panic freedom and functional properties, and the examples in the repository provide larger cases.

Specification updates

hax_lib::ensures takes the return value by value, which does not work for non-Copy types. hax 0.4 adds hax_lib::ensures_ref, which takes it by reference:

#[hax_lib::ensures_ref(|result| result.len() == 2)]
pub fn pair(x: u64) -> Vec<u64> {
    vec![x, x]
}

requires and ensures now also work under cfg_attr in impl blocks and on methods whose signatures mention associated types. Cases that are still unsupported produce an explicit error instead of silently dropping the specification.

hax can now extract specifications written with anodized, a crate that aims to provide one specification syntax for several Rust verification tools. hax extracts anodized's #[spec(requires: .., maintains: .., ensures: ..)] attributes as pre- and postconditions, where a maintains clause becomes both. hax_lib::requires and hax_lib::ensures remain the recommended way to write specifications for hax. The anodized example shows both side by side.

Thanks and what's next

Thanks to everyone who contributed to this release:

Our main goal for the next release is robustness. We want hax to work on almost any crate with all backends. We will expand our test suite to track progress towards this goal. We also plan to improve proof ergonomics, making it easier to go from an extraction to a finished proof.

As always, bug reports and questions are welcome on the issue tracker and on Zulip.

This Month in Hax: August 2026

In August, we successfully merged 46 pull requests!

We extended our CLI functionality significantly: hax can now be installed via cargo binstall cargo-hax, and it can automatically download and manage its aeenas and charon dependencies. Moreover, users can declare proof scenarios in a hax.toml file to specify exactly which options they want to use for a particular project, and then run those scenarios via a new cargo hax extract command.

We extended our models of Rust core further and improved its testing infrastructure to make sure that it is an accurate model of Rust core. These models benefit both the Lean and the F* backend.

For the new Lean backend, we added support for hax_lib::requires, hax_lib::ensures, and other attributes and for the hax_lib::prop and hax_lib::int specification helpers.

This was our final sprint towards a release candidate for hax v0.4.0. The release candidate was published yesterday. Now, we are testing it, fixing bugs, and finalizing its documentation.

Thanks to the many external contributors we had this month!

Full list of PRs

Contributors

This Month in Hax: July 2026

In July, we successfully merged 31 pull requests!

We are preparing for the release of hax 0.4.0. The main change is that the Lean backend now uses aeneas, the Lean backend relying on the hax engine has been renamed legacy-lean. The integration with aeneas has been improved and is still one of our main focuses.

This includes improvements and fixes to the core-models library. The rust models have been reintegrated in the hax repo, and now extract to both Lean, through the new Lean backend, and F*. We also fixed issues with the hax-lib annotation macros.

Finally, we worked on the documentation and updating the examples to show the latest workflow with our main backends (Lean and F*).

Full list of PRs

Contributors

This Month in Hax: June 2026

In June, we successfully merged 6 pull requests!

Our main focus right now is the integration of aeneas into hax, which we will release in July. Besides that, we worked on improving our testing infrastructure and on our Rust core models.

Full list of PRs

Contributors

This Month in Hax: May 2026

In May, we successfully merged 6 pull requests!

We fixed various bugs and finally released hax 0.3.7! Our work to integrate Aeneas in the hax engine is still ongoing. Some of it is in our new external repo for the core models (https://github.com/cryspen/rust-core-models). And the changes to hax will be published later.

Full list of PRs

Contributors

This Month in Hax: April 2026

In April, we successfully merged 11 pull requests!

Besides various bug fixes, we improved the rendering of ellipsis in pattern-matching and refactored the mvcgen registration of the RustM monad to support partial correctness lemmas. Thanks to the external contributors @MavenRain, @niooss-ledger, and @remix7531!

Towards the end of the month, we participated in the Software Verification in Lean workshop and hackathon organized by the Beneficial AI Foundation and the Lean FRO. We came away with lots of new ideas for our Lean backend.

For a couple of weeks, we have been working on merging the Aeneas engine into Hax. We haven't made any public contributions to Hax in that direction yet, but we have created a new repo containing our Rust core models with an Aeneas-compatible extraction: https://github.com/cryspen/rust-core-models

Full list of PRs

Contributors

This Month in Hax: March 2026

In March, we successfully merged 8 pull requests!

This was a rather quiet month with some of us away for Real World Crypto and HACS in Taipei. But we continued to improve the Lean library with bugfixes and new additions like support for u128 and i128.

Special thanks to @JuanCoRo for improving and extending the treatment of binops in the Lean backend. And thank you @redshiftzero for your first PR!

Full list of PRs

Contributors

This Month in Hax: February 2026

In February, we successfully merged 32 pull requests!

This month, the Lean backend made headway by getting new Rust proof attributes. With the new attributes, Users can choose between two proof methods, one based on symbolic reasoning (grind) and one based on bit-blasting (bv_decide). This new setup is illustrated in our updated Tutorial and the Chacha20 example. Moreover, the Lean backend produces prettier output by opening namespaces, handles for-loops more reliably, and its library contains more of our Rust core models.

We also made great progress on our new THIR importer, implemented in Rust. Currenlty, it can be activated using --experimental-full-def, and we intend to make it the default soon.

Special thanks to @JuanCoRo, @klausnat, and @rusch95 for their contributions this month!

Full list of PRs

Contributors

This Month in Hax: January 2026

In January, we successfully merged 29 pull requests!

The Lean backend continues to get improvements, mostly thanks to @abentkamp! We now have more specs in the proof library (for while loops, negation and more), and improved tactics to reason with our annotations of pre/post-conditions (#1888). Some naming fixes, and improvements of the handling of associated types make us closer to reaching parity with the F* backend!

The other significant improvements are on the core models side. Thanks to some new models, the F* proof library is now entirely extracted from Rust core models. The Rust primitives models will remain hand-written in each backend, but they have been designed to be as small as possible, and are intended to be modelled in a very backend-specific way. The Lean library is now also partly extracted from core models! Some modules are excluded for now as they rely on Rust features that we don't support in the Lean backend for now. We will prioritize these missing features in the next few months to extend the Lean library using extraction from core models.

Full list of PRs

Contributors