-
Notifications
You must be signed in to change notification settings - Fork 48
Lean backend [M2] - 2/3 - Examples #1593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
90d077e
Update Readme
clementblaudeau eaf198d
Add rewriting lemmas
clementblaudeau 8eca698
Add the Lean Barrett example
clementblaudeau 1ef0334
Add the Lean Chacha20 example
clementblaudeau 64737f0
Update README
clementblaudeau 5fe0411
Turn the proof-libs/lean into a proper lean library package
clementblaudeau 4daaeae
Extract pure facts out of preconditions on specification theorems
clementblaudeau f80e1f2
Finish panic freedom proof of Chacha20
clementblaudeau 899a794
Update lib import for lean barrett
clementblaudeau a33b7fd
Add run instructions
clementblaudeau 040452b
Update makefiles in examples
clementblaudeau 6c36477
proof-lib/lean Add Xor + fix bug in gt/ge
clementblaudeau ee782a3
proof-lib/lean Move scoped attributes into a shared namespace
clementblaudeau b438ad6
Lean_chacha20 add proof of hacspec helpers
clementblaudeau 19afead
Update Readme
clementblaudeau 2aa10c6
Update testcase
clementblaudeau a0f0196
Update Readme and lakefile
clementblaudeau 65f3961
Add Apache Standard License Header
clementblaudeau 2eff826
Changed relative path to hax library
clementblaudeau 70cb738
Merge branch 'main' into lean-dev-examples
clementblaudeau 0705e68
Update examples/README.md
clementblaudeau 0287b2d
Merge branch 'main' into lean-dev-examples
clementblaudeau 1d60924
Merge remote-tracking branch 'origin/main' into lean-dev-examples
clementblaudeau e1686c6
Update CI to install lean before running the examples
clementblaudeau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "lean_barrett" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| hax-lib.workspace = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .PHONY: default lean clean | ||
| default: | ||
| cargo hax into lean | ||
| cd proofs/lean/extraction && lake build | ||
|
|
||
| clean: | ||
| rm -f proofs/lean/extraction/lean_barrett.lean | ||
| cd proofs/lean/extraction && lake clean |
10 changes: 10 additions & 0 deletions
10
examples/lean_barrett/proofs/lean/extraction/lakefile.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| name = "lean_barrett" | ||
| version = "0.1.0" | ||
| defaultTargets = ["lean_barrett"] | ||
|
|
||
| [[lean_lib]] | ||
| name = "lean_barrett" | ||
|
|
||
| [[require]] | ||
| name = "Hax" | ||
| path = "../../../../../hax-lib/proof-libs/lean" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use hax_lib as hax; | ||
| use hax_lib::lean; | ||
|
|
||
| /// Values having this type hold a representative 'x' of the Kyber field. | ||
| /// We use 'fe' as a shorthand for this type. | ||
| pub(crate) type FieldElement = i32; | ||
|
|
||
| #[hax_lib::lean::before("@[simp, spec]")] | ||
| const BARRETT_R: i64 = 0x400000; // is 0x4000000 in the normal barrett example | ||
|
|
||
| #[hax_lib::lean::before("@[simp, spec]")] | ||
| const BARRETT_SHIFT: i64 = 26; | ||
|
|
||
| #[hax_lib::lean::before("@[simp, spec]")] | ||
| const BARRETT_MULTIPLIER: i64 = 20159; | ||
|
|
||
| #[hax_lib::lean::before("@[simp, spec]")] | ||
| pub(crate) const FIELD_MODULUS: i32 = 3329; | ||
|
|
||
| /// Signed Barrett Reduction | ||
| /// | ||
| /// Given an input `value`, `barrett_reduce` outputs a representative `result` | ||
| /// such that: | ||
| /// | ||
| /// - result ≡ value (mod FIELD_MODULUS) | ||
| /// - the absolute value of `result` is bound as follows: | ||
| /// | ||
| /// `|result| ≤ FIELD_MODULUS / 2 · (|value|/BARRETT_R + 1) | ||
| /// | ||
| /// In particular, if `|value| < BARRETT_R`, then `|result| < FIELD_MODULUS`. | ||
| #[hax::requires((i64::from(value) >= -BARRETT_R && i64::from(value) <= BARRETT_R))] | ||
| #[hax::ensures(|result| { | ||
| let valid_result = value % FIELD_MODULUS; | ||
| result > -FIELD_MODULUS | ||
| && result < FIELD_MODULUS | ||
| && (result == valid_result || | ||
| result == valid_result + FIELD_MODULUS || | ||
| result == valid_result - FIELD_MODULUS) | ||
| })] | ||
| #[hax_lib::lean::before("@[simp, spec]")] | ||
| #[hax_lib::lean::after(" | ||
| theorem barrett_spec (value: i32) : | ||
| ⦃ __requires (value) = pure true ⦄ | ||
| (barrett_reduce value) | ||
| ⦃ ⇓ result => __ensures value result = pure true ⦄ | ||
| := by | ||
| mvcgen [__requires, __ensures] | ||
| hax_bv_decide | ||
| simp [__requires, __ensures] at * | ||
| rw [Int32.HaxRem_spec_bv_rw] ; simp ; | ||
| rw [Int32.HaxAdd_spec_bv_rw] ; simp ; | ||
| rw [Int32.HaxSub_spec_bv_rw] ; simp | ||
| hax_bv_decide | ||
| expose_names | ||
| have ⟨ h1, h2 ⟩ := h; clear h | ||
| simp [Int32.eq_iff_toBitVec_eq, | ||
| Int32.lt_iff_toBitVec_slt, | ||
| Int32.le_iff_toBitVec_sle, | ||
| Int64.eq_iff_toBitVec_eq, | ||
| Int64.lt_iff_toBitVec_slt, | ||
| Int64.le_iff_toBitVec_sle, | ||
| ] at * | ||
| generalize Int32.toBitVec value = bv_value at * ; clear value | ||
| bv_decide (config := {timeout := 120}) | ||
| ")] | ||
| 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 | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "lean_chacha20" | ||
| version = "0.1.0" | ||
| authors = ["Clement Blaudeau <[email protected]>"] | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| hax-lib.workspace = true | ||
| hax-bounded-integers.workspace = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .PHONY: default clean | ||
| default: | ||
| cargo hax into lean | ||
| cd proofs/lean/extraction && lake build | ||
|
|
||
| clean: | ||
| rm -f proofs/lean/extraction/lean_chacha20.lean |
13 changes: 13 additions & 0 deletions
13
examples/lean_chacha20/proofs/lean/extraction/lakefile.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| name = "lean_chacha20" | ||
| version = "0.1.0" | ||
| defaultTargets = ["lean_chacha20_manual_edit"] | ||
|
|
||
| [[lean_lib]] | ||
| name = "lean_chacha20" | ||
|
|
||
| [[lean_lib]] | ||
| name = "lean_chacha20_manual_edit" | ||
|
|
||
| [[require]] | ||
| name = "Hax" | ||
| path = "../../../../../hax-lib/proof-libs/lean" |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.