Skip to content

Conversation

@clementblaudeau
Copy link
Contributor

@clementblaudeau clementblaudeau commented Aug 4, 2025

This PR is a follow-up of #1590. It introduces two specific examples to showcase the lean backend in examples/lean_barrett and example/lean_chacha20. For both, the lean extraction can be obtained by running cargo hax into lean

Barrett

The Barrett reduction allows to compute remainders without using divisions. It showcases arithmetic operations, conversions between integer types (namely i32 and i64). Specifically, the Lean backend provides panicking arithmetic operations +?, -?, etc, that panic on overflows.

For the Lean extracted code, we prove panic freedom with regards to those arithmetic operations, and then we prove that the result is indeed the modulus (as long as the absolute value of the input is lower than the bound BARRETT_R). The proof is made via bit-blasting (using Lean's bv_decide). To limit the computation time, the bound BARRETT_R was lowered compared to the normal example in the barrett folder.

The proofs are backported in the rust code (in lean_barrett/src/lib.rs): doing cargo hax into lean extracts a valid lean file that contains the proof.

Chacha20

The Chacha20 example extracts to Lean, but requires a manual edit to be wellformed (will be removed via resugarings #1591). It showcases array, vector and slices accesses, as well as loops (with loop invariants).

For the Lean extracted code, we prove panic freedom. It is not backported in rust yet.

This edit and the proofs of panic freedom can be found in lean_chacha20/proofs/lean/extraction/lean_chacha20_manual_edit.lean.

@clementblaudeau clementblaudeau marked this pull request as ready for review August 5, 2025 13:02
@clementblaudeau clementblaudeau requested a review from a team as a code owner August 5, 2025 13:02
@clementblaudeau clementblaudeau changed the title Lean backend 2/3 - Examples Lean backend [M2] - 2/3 - Examples Aug 6, 2025
@clementblaudeau clementblaudeau force-pushed the lean-dev-examples branch 2 times, most recently from 5f850a6 to 85ae109 Compare August 6, 2025 16:56
Copy link
Contributor

@karthikbhargavan karthikbhargavan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We reviewed the proofs together last week and they look great.
This is mainly a Lean PR and shouldn't require much Rust review.

@clementblaudeau clementblaudeau requested review from W95Psp and removed request for maximebuyse August 14, 2025 10:06
@W95Psp
Copy link
Member

W95Psp commented Aug 14, 2025

I'm looking into the nix issue

Edit: I got no much time to look at it, let's postpone this.

Copy link
Member

@W95Psp W95Psp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've been the code, it looks nice to me!
I checked the lean_barrett example, it works fine on my laptop :)

@W95Psp
Copy link
Member

W95Psp commented Aug 20, 2025

🎉 CI is happy

@clementblaudeau
Copy link
Contributor Author

I've rebased to squash the commits about CI, and will merge as soon as it passes.

@W95Psp W95Psp enabled auto-merge August 21, 2025 08:29
@W95Psp W95Psp added this pull request to the merge queue Aug 21, 2025
Merged via the queue into main with commit 2005f85 Aug 21, 2025
17 checks passed
@W95Psp W95Psp deleted the lean-dev-examples branch August 21, 2025 09:23
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.

3 participants