Repository navigation
verify: ACSL contracts and Frama-C WP/Eva proofs of the hashes and mont - #16
Open
maximmenshikov wants to merge 1 commit into
Open
maximmenshikov wants to merge 1 commit into
maximmenshikov wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds formal checks with Frama-C, as bflat-riscv64 has for its modules (
check_acsl.py,run_wp_all.sh, theehproof). It covers the hashes, the Montgomery arithmetic and the C version of the u256 delegation.What runs
verify/check_acsl.pyverify/files.txtparses under Frama-C, and every function it defines has an ACSL contract. This includes the static inline functions of the headers.verify/run_wp.shverify/run_eva.shverify/eva/. They cover what WP cannot follow.Each file is analysed twice, once with
-DAIRBENDERand 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-bitsize_tand unsignedchar.mont.h)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.
u256_opasks 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 inmont:mont_addandmont_mulcopy tohi;u256_op(r, &ctx->n, borrow)) isU256_ADD.u256_soft.cproves the same contract.StatefromStateandBlock.Check that the proofs catch bugs: two bugs were injected by hand, and both make the run fail.
tail[tailLen - i]insha256(a write past the buffer) leaves an RTE goal unproved.r = 2rinmont_add, the precondition ofu256_opis not proved.Exceptions, with the reason in the file
memcpy/memset: they have the contracts of our runtime's, inzkvm.h. With n = 0 they touch no memory. C11 and the libc of Frama-C want valid pointers even then. Butzkvm_sha256,zkvm_ripemd160andzkvm_keccak256pass a pointer just past the message when its length is a whole number of blocks.blake2s.opts:load_blockwrites bytes into theuint32_twords ofBlock, and the final copy readsStateas 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+castmodel. Frama-C turnsr == b(u256d *againstconst u256d *) into a comparison ofvoid *casts, which the typed model rejects.montreads and writes memory only throughu256danduint32_t.Not proved:
mont_initthat finds R mod n terminates. That needs the arithmetic of the delegation, somont_inithasterminates \false.Source changes
None of them change the generated code. I compiled every
src/*.cfor both platforms before and after (clang, the flags ofbuild/build.sh) and compared the disassembly. It is identical exceptmont.o, where only the offsets of the static values inside.bsschange.sha256.c:#pragma clang loop unroll(full)becomesUNROLL_FULL(_Pragmainzkvm.h), because Frama-C cannot parse the pragma.mont.c: thestaticscratch 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_alignedand__builtin_memcpyare mapped to plain C.Next
mpt.candmodexp.c: they parse untrusted input (witness RLP, lengths).ec,ecc,bn254,bls12(absence of runtime errors only) andplatform_airbender.c. One thing to check there:airbender_keccak256reads the last input word whole (w[i] & mask), up to 3 bytes pastlen.🤖 Generated with Claude Code