Expand description
§hax library
This crate contains helpers that can be used when writing Rust code that is proven through the hax toolchain.
⚠️ The code in this crate has no effect when compiled without --cfg hax. hax sets this cfg automatically when extracting a crate; a regular cargo build leaves it unset.
§Main features
#[hax_lib::requires(...)]and#[hax_lib::ensures(...)]: pre- and postconditions on functions.#[hax_lib::attributes]: enablesrequires/ensureson trait methods and refinements on struct fields.hax_lib::loop_invariant!: loop invariants.Propand the logical operatorsforall,exists,implies: propositions beyondbool.assume!,assert!,assert_prop!: assumptions and assertions for the backends.fstar!,coq!,proverif!, …: inline backend code.
§Example
/// The addition in `sum` does not overflow.
fn no_overflow(x: &[u32], y: &[u32]) -> hax_lib::Prop {
hax_lib::forall(|i: usize| {
hax_lib::implies(
i < x.len(),
x[i] as u64 + y[i] as u64 <= u32::MAX as u64,
)
})
}
#[hax_lib::requires(hax_lib::Prop::from(x.len() == y.len()) & no_overflow(&x, &y))]
#[hax_lib::ensures(|result| result.len() == x.len())]
fn sum(x: Vec<u32>, y: Vec<u32>) -> Vec<u32> {
hax_lib::assert!(x.len() == y.len());
hax_lib::assert_prop!(no_overflow(&x, &y));
x.into_iter().zip(y).map(|(x, y)| x + y).collect()
}Re-exports§
Modules§
- coq
- Procedular macros that have an effect only for the backend coq.
- fstar
- Procedular macros that have an effect only for the backend fstar.
- int
- legacy_
lean - Procedular macros that have an effect only for the backend legacy_lean.
- prop
- proverif
- Procedular macros that have an effect only for the backend proverif.
Macros§
- assert
- Proxy to
std::assert!. Compiled withhax, this is transformed into aassertin the backend. - assert_
prop - Assert a logical proposition
Prop: this exists only in the backends of hax. In Rust, this macro expands to an empty block{ }. - assume
- coq
- Embed coq expression inside a Rust expression. This macro takes only one argument: some raw coq code as a string literal.
- debug_
assert - Proxy to
std::debug_assert!. Compiled withhax, this disappears. - fstar
- Embed fstar expression inside a Rust expression. This macro takes only one argument: some raw fstar code as a string literal.
- legacy_
lean - Embed legacy_lean expression inside a Rust expression. This macro takes only one argument: some raw legacy_lean code as a string literal.
- loop_
decreases - Must be used to prove termination of while loops. This takes an expression that should be a usize that decreases at every iteration
- loop_
invariant - Add an invariant to a loop which deals with an index. The invariant cannot refer to any variable introduced within the loop. An invariant is a closure that takes one argument, the index, and returns a proposition.
- proverif
- Embed proverif expression inside a Rust expression. This macro takes only one argument: some raw proverif code as a string literal.
Traits§
- Abstraction
- Marks a type as abstractable: its values can be mapped to an idealized version of the type. For instance, machine integers, which have bounds, can be mapped to mathematical integers.
- Concretization
- Marks a type as abstract: its values can be lowered to concrete values. This might panic.
- Refine
As - A utilitary trait that provides a
into_checkedmethod on traits that have a refined counter part. This trait is parametrized by a typeTarget: a base type can be refined in multiple ways. - Refinement
- A type that implements
Refinementshould be a newtype for a typeT. The field holding the value of typeTshould be private, andRefinementshould be the only interface to the type.
Attribute Macros§
- attributes
- Enable the following attributes in the annotated item and sub-items.
- decreases
- Provide a measure for a function: this measure will be used once extracted in a backend for checking termination. The expression that decreases can be of any type. (TODO: this is probably as it is true only for F*, see #297)
- ensures
- Add a logical postcondition to a function. Note you can use the
forallandexistsoperators. - ensures_
ref - Same as
ensures, but the closure takes the result by reference: the binder has type&TwhereTis the function’s return type. - exclude
- Exclude this item from the Hax translation.
- impl_
fn_ decoration - Internal macro for dealing with function decorations
(
#[decreases(...)],#[ensures(...)],#[requires(...)]) onfnitems within animplblock. There is special handling since such functions might have aselfargument: in such cases, we rewrite function decorations as#[impl_fn_decoration(<KIND>, <GENERICS>, <WHERE CLAUSE>, <SELF TYPE> [as <TRAIT>], <BODY>)], where<TRAIT>is the trait implemented by the enclosingimplblock. - include
- Include this item in the Hax translation. This overrides any exclusion resulting of
-iflag. - lemma
- Mark a
Proof<{STATEMENT}>-returning function as a lemma, whereSTATEMENTis aPropexpression capturing any input variable. In the backends, this will generate a lemma with an empty proof. - opaque
- Mark an item opaque: the extraction will assume the type without revealing its definition.
- opaque_
type - Mark an item opaque: the extraction will assume the type without revealing its definition.
- process_
init - A marker indicating a
fnas a ProVerif process initialization. - process_
read - A marker indicating a
fnas a ProVerif process read. - process_
write - A marker indicating a
fnas a ProVerif process write. - protocol_
messages - A marker indicating an
enumas describing the protocol messages. - pv_
constructor - A marker indicating a
fnshould be automatically translated to a ProVerif constructor. - pv_
handwritten - A marker indicating a
fnrequires manual modelling in ProVerif. - refinement_
type - Marks a newtype
struct RefinedT(T);as a refinement type. The struct should have exactly one unnamed private field. - requires
- Add a logical precondition to a function.
In the case of a function that has one or more
&mutinputs, in theensuresclause, you can refer to such an&mutinputxasxfor its “past” value andfuture(x)for its “future” value. Where those future values sit relative to the result in the generated postcondition is backend-dependent, since each backend orders the tuple its functions return differently. - trait_
fn_ decoration - Internal macro for dealing with function decorations on
fnitems within atrait. Seeimpl_fn_decoration. - transparent
- Mark an item transparent: the extraction will not
make it opaque regardless of the
-iflag default.