Skip to content

verify: ACSL contracts and Frama-C WP/Eva proofs of the hashes and mont - #16

Open
maximmenshikov wants to merge 1 commit into
mainfrom
verify/acsl-wp
Open

maximmenshikov wants to merge 1 commit into
mainfrom
verify/acsl-wp

Conversation

@maximmenshikov

Copy link
Copy Markdown
Collaborator

This adds formal checks with Frama-C, as bflat-riscv64 has for its modules (check_acsl.py, run_wp_all.sh, the eh proof). It covers the hashes, the Montgomery arithmetic and the C version of the u256 delegation.

What runs

Script What it proves
verify/check_acsl.py Every file in verify/files.txt parses under Frama-C, and every function it defines has an ACSL contract. This includes the static inline functions of the headers.
verify/run_wp.sh WP with RTE guards proves every goal: the contracts, the loop invariants, and that there are no out-of-bounds accesses, signed overflows, bad shift amounts or other UB.
verify/run_eva.sh Eva reports no alarm on the harnesses of verify/eva/. They cover what WP cannot follow.

Each file is analysed twice, once with -DAIRBENDER and once as the linux build. The target is rv32im/ilp32 (verify/machdep_rv32.yaml), generated from the macros that clang predefines for the target: 32-bit size_t and unsigned char.

File airbender linux
sha256 435/435 435/435
keccak 98/98 180/180
ripemd160 191/191 191/191
blake2f 244/244 244/244
blake2s 21/21 + Eva 187/187 + Eva
mont (with the inline functions of mont.h) 323/323 323/323
u256_soft 136/136

The full run takes about 10 minutes on 6 cores. The new CI job "Verify (Frama-C WP + Eva)" runs it in framac/frama-c:30.0, pinned by digest.

Delegations

The asm blocks stay as they are for the compiler. Under Frama-C, each one is replaced by a declaration whose contract states what the delegation reads and writes.

  • The contract of u256_op asks for two distinct operands (\separated(a, b)) and an operation the delegation knows, and gives a flag of 0 or 1. So WP checks every call in mont:
    • no call passes the same operand twice, which is why mont_add and mont_mul copy to hi;
    • the borrow used as the next operation (u256_op(r, &ctx->n, borrow)) is U256_ADD.
  • On linux, u256_soft.c proves the same contract.
  • The BLAKE2s delegation is a declaration that writes State from State and Block.

Check that the proofs catch bugs: two bugs were injected by hand, and both make the run fail.

  • tail[tailLen - i] in sha256 (a write past the buffer) leaves an RTE goal unproved.
  • Without the copy for r = 2r in mont_add, the precondition of u256_op is not proved.

Exceptions, with the reason in the file

  • memcpy/memset: they have the contracts of our runtime's, in zkvm.h. With n = 0 they touch no memory. C11 and the libc of Frama-C want valid pointers even then. But zkvm_sha256, zkvm_ripemd160 and zkvm_keccak256 pass a pointer just past the message when its length is a whole number of blocks.
  • blake2s.opts: load_block writes bytes into the uint32_t words of Block, and the final copy reads State as bytes. The typed memory model of WP cannot follow this. Both entry points are covered by Eva instead (verify/eva/blake2s.c): every length from 0 to 257 bytes, every alignment of input and output, any bytes.
  • mont.opts: the +cast model. Frama-C turns r == b (u256d * against const u256d *) into a comparison of void * casts, which the typed model rejects. mont reads and writes memory only through u256d and uint32_t.

Not proved:

  • that the operands of the delegation are in RAM and 32-byte aligned (an ACSL contract cannot state this);
  • that the loop in mont_init that finds R mod n terminates. That needs the arithmetic of the delegation, so mont_init has terminates \false.

Source changes

None of them change the generated code. I compiled every src/*.c for both platforms before and after (clang, the flags of build/build.sh) and compared the disassembly. It is identical except mont.o, where only the offsets of the static values inside .bss change.

  • sha256.c: #pragma clang loop unroll(full) becomes UNROLL_FULL (_Pragma in zkvm.h), because Frama-C cannot parse the pragma.
  • mont.c: the static scratch values move from the function bodies to file scope (OnePlain, PowTable/PowAcc, Init*), so that the contracts can name them.
  • u256.h, blake2s.c, zkvm.h: #if defined(AIRBENDER) && !defined(__FRAMAC__) around the asm; under Frama-C, __builtin_assume_aligned and __builtin_memcpy are mapped to plain C.

Next

  • mpt.c and modexp.c: they parse untrusted input (witness RLP, lengths).
  • Then ec, ecc, bn254, bls12 (absence of runtime errors only) and platform_airbender.c. One thing to check there: airbender_keccak256 reads the last input word whole (w[i] & mask), up to 3 bytes past len.

🤖 Generated with Claude Code

…d mont

As bflat-riscv64 does for its modules: verify/check_acsl.py checks that every function of the files in
verify/files.txt has a contract, verify/run_wp.sh proves every goal with WP and RTE guards, and verify/run_eva.sh
runs Eva on the harnesses for what WP cannot follow. Each file is analysed on both platforms (-DAIRBENDER and the
linux build) for rv32im/ilp32 (verify/machdep_rv32.yaml, from the macros clang predefines for the target).

The files: sha256, keccak, ripemd160, blake2f, blake2s, mont (with the inline operations of mont.h) and u256_soft.

Where Airbender runs a delegation, Frama-C sees a declaration whose contract states what it reads and writes; the
asm blocks stay as they are for the compiler. The contract of u256_op asks for two distinct operands and an
operation the delegation knows, and gives a flag of 0 or 1, so WP checks every call of mont for them; on linux,
u256_soft.c proves the contract. memcpy and memset have the contracts of the runtime's: with n = 0 they touch no
memory, as the hashes need when the message is a whole number of blocks.

blake2s writes bytes into the words of its delegation buffers, which the typed model of WP cannot follow: its two
entry points are covered by Eva, for every length up to four blocks and a byte, at every alignment. mont compares
pointers of differently qualified types, which needs the +cast model. The README lists what is not proved.

Source changes for Frama-C, none of them in the generated code: the clang loop pragma of sha256 is a macro,
UNROLL_FULL, that is empty under Frama-C; the scratch values of mont.c are static at file scope rather than in each
function, so that the contracts can name them (only their layout in .bss changes).

A Verify job in CI runs the three scripts in the framac/frama-c:30.0 image.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant