Skip to content

Commit 358d72a

Browse files
committed
update project files
Made-with: Cursor
1 parent 6ac129f commit 358d72a

1,250 files changed

Lines changed: 5827 additions & 1758 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: weekly
7+
open-pull-requests-limit: 5
8+
9+
- package-ecosystem: cargo
10+
directory: /rust
11+
schedule:
12+
interval: weekly
13+
open-pull-requests-limit: 5

.github/workflows/build-old.yml

Lines changed: 0 additions & 49 deletions
This file was deleted.

.github/workflows/formal-verify.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Organization-hosted remote checks (SentinelOps). Contributor-visible Lean/Rust
2+
# verification also runs in `.github/workflows/local-ci.yml`.
13
name: SentinelOps
24
on: [push, pull_request]
35

.github/workflows/local-ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Local CI (Lean + Rust)
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
workflow_dispatch:
9+
10+
jobs:
11+
verify:
12+
strategy:
13+
matrix:
14+
os: [ubuntu-22.04, macos-14]
15+
16+
runs-on: ${{ matrix.os }}
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Setup Lean
23+
uses: leanprover/lean-action@v1
24+
25+
- name: Setup Rust
26+
uses: dtolnay/rust-toolchain@stable
27+
with:
28+
components: clippy
29+
30+
- name: Sorry policy
31+
run: bash scripts/check_sorry.sh
32+
33+
- name: Lake build
34+
run: lake build
35+
36+
- name: Lake test
37+
run: lake test
38+
39+
- name: Regenerate Rust crate
40+
run: lake exe extract
41+
42+
- name: Cargo test and clippy
43+
working-directory: rust
44+
run: |
45+
cargo test
46+
cargo clippy --all-targets -- -D warnings

CI_MIGRATION_SUMMARY.md

Lines changed: 0 additions & 60 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
# Contributing
22

3-
Thank you for your interest in contributing to lean-toolchain!
3+
Thank you for your interest in contributing to lean-toolchain.
44

5-
## Getting Started
5+
## Getting started
66

77
- Fork the repository and clone your fork.
8-
- Install Lean 4 and Rust (see README for setup instructions).
9-
- Run `lake build` and `lake test` to ensure a clean build.
8+
- Install Lean 4 and Rust (see `README.md`).
9+
- From the repository root, run `lake build` and `lake test`.
10+
- If you touch the Rust generator or templates, run `lake exe extract` and `cargo test` / `cargo clippy --all-targets -- -D warnings` inside `rust/`.
1011

11-
## Coding Standards
12+
## Toolchain policy (Lean + mathlib)
1213

13-
- Follow the `.editorconfig` for whitespace and indentation.
14-
- For Lean: Use descriptive names, add docstrings, and prefer constructive proofs.
15-
- For Rust: No `unsafe` outside FFI modules; document all public APIs.
16-
- Write clear, minimal, and total code. Avoid `sorry` unless absolutely necessary.
14+
The Lean version is pinned in `lean-toolchain`. **mathlib** is required in `lakefile.lean` with a git reference that matches that Lean release (for example the `v4.21.0` tag line). When you bump Lean:
1715

18-
## Commit Messages
16+
1. Update `lean-toolchain`.
17+
2. Update the mathlib `require` line to the corresponding mathlib tag or revision.
18+
3. Run `lake update` and fix any breakage before opening a PR.
1919

20-
- Use [Conventional Commits](https://www.conventionalcommits.org/).
20+
## `sorry` policy
2121

22-
## Pull Requests
22+
Production code under `LeanToolchain/` is expected to contain **no** `sorry` placeholders. CI runs `scripts/check_sorry.sh`, which fails on any occurrence except files explicitly listed in `scripts/sorry_allowlist.txt` (whole-file waivers only, one repo-relative path per line; use sparingly and justify in the PR).
2323

24-
- Ensure all tests pass (`lake test`).
25-
- Add/Update documentation as needed.
26-
- Link related issues in your PR description.
24+
## Coding standards
2725

28-
## Code of Conduct
26+
- Follow `.editorconfig` for whitespace and indentation.
27+
- For Lean: descriptive names, docstrings where they aid readers, and constructive proofs.
28+
- For generated Rust: `pub unsafe extern "C"` entrypoints are used for FFI-shaped APIs; keep `unsafe` blocks minimal and obvious.
29+
30+
## Commit messages
31+
32+
- Prefer [Conventional Commits](https://www.conventionalcommits.org/).
33+
34+
## Pull requests
35+
36+
- Ensure `lake build`, `lake test`, and (if applicable) the `rust/` checks above succeed.
37+
- Link related issues in the PR description.
38+
39+
## Code of conduct
2940

3041
- Be respectful and inclusive.
3142

32-
See the full documentation for more details.
43+
## Documentation
44+
45+
- **MkDocs**: configuration lives in [`docs/mkdocs.yml`](docs/mkdocs.yml); start from [`docs/index.md`](docs/index.md) for the full map.
46+
- **CI details**: [`docs/development/ci.md`](docs/development/ci.md).
47+
- **Rust generator**: [`docs/development/extraction.md`](docs/development/extraction.md).
48+
- **Security scope**: [`SECURITY.md`](SECURITY.md).
49+
50+
## CI
51+
52+
Pushes and pull requests run a local GitHub Actions workflow (see `.github/workflows/local-ci.yml`) that builds Lean, runs tests, regenerates `rust/` with `lake exe extract`, runs `cargo test` / `cargo clippy`, and enforces the sorry policy. Additional organization checks may run via SentinelOps (`formal-verify.yml`).
53+
54+
For a concise overview of how those pieces fit together, see [`docs/development/ci.md`](docs/development/ci.md).

LeanToolchain/Crypto/HMAC.lean

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ import LeanToolchain.Crypto.Utils
44
/-!
55
# HMAC-SHA256 Implementation
66
7-
This module provides a formally verified implementation of HMAC-SHA256.
8-
HMAC (Hash-based Message Authentication Code) provides message authentication
9-
using a cryptographic hash function combined with a secret key.
7+
This module provides an HMAC-SHA256 construction built directly on the in-repo
8+
`sha256` function, together with structural lemmas about key preparation and
9+
determinism.
10+
11+
## Key schedule (important for interoperability)
12+
13+
RFC 2104 describes hashing over-long keys, then **zero-padding the digest to the
14+
block size** before XOR with `ipad` / `opad`. The definition here instead follows
15+
the common “short key padded to 64 bytes; **long key replaced by a 32-byte
16+
digest** (no trailing zero pad before XOR)” shape used by several libraries and
17+
matches the emitted Rust generator for parity tests.
18+
19+
For keys whose byte length is at most `sha256BlockSize` (64), behavior matches
20+
standard test vectors (for example RFC 4231 cases exercised in
21+
`LeanToolchain.Crypto.Tests`). For longer keys, treat the MAC as **Lean-defined**
22+
when comparing against other implementations.
1023
-/
1124

1225
namespace LeanToolchain.Crypto
@@ -30,8 +43,8 @@ def hmacPrepareKey (key : ByteArray) : ByteArray :=
3043
sha256 key
3144
else if key.size < sha256BlockSize then
3245
-- If key is shorter than block size, pad with zeros
33-
let padding := List.replicate (sha256BlockSize - key.size) 0
34-
key ++ ByteArray.mk (Array.mk padding) -- Lean 4 idiom: Array.mk for List -> Array
46+
let pad := (List.replicate (sha256BlockSize - key.size) 0).toArray
47+
ByteArray.mk (key.data ++ pad)
3548
else
3649
-- Key is exactly block size
3750
key
@@ -84,29 +97,32 @@ The following properties should be formally proven:
8497
## HMAC Properties and Lemmas
8598
-/
8699

87-
/-- HMAC key preparation preserves block size -/
88-
theorem hmacPrepareKey_size (key : ByteArray) : (hmacPrepareKey key).size = sha256BlockSize := by
89-
simp [hmacPrepareKey, sha256BlockSize]
90-
-- This follows from the construction in hmacPrepareKey
91-
sorry
100+
/-- After preparation, short keys are padded to the block size; long keys are hashed to the digest size. -/
101+
theorem hmacPrepareKey_size_cases (key : ByteArray) :
102+
(key.size ≤ sha256BlockSize → (hmacPrepareKey key).size = sha256BlockSize) ∧
103+
(sha256BlockSize < key.size → (hmacPrepareKey key).size = sha256OutputSize) := by
104+
constructor
105+
· intro hle
106+
rcases Nat.lt_or_eq_of_le hle with hlt | heq
107+
· have hg1 : ¬ key.size > sha256BlockSize := by intro h'; omega
108+
have hg1' : ¬ key.data.size > sha256BlockSize := by simpa [ByteArray.size] using hg1
109+
have hlt' : key.data.size < sha256BlockSize := by
110+
simpa [ByteArray.size, sha256BlockSize] using hlt
111+
unfold hmacPrepareKey
112+
simp only [hg1', hlt', ↓reduceIte, ByteArray.size, Array.size_append, List.size_toArray,
113+
List.length_replicate]
114+
rw [Nat.add_sub_of_le (Nat.le_of_lt hlt')]
115+
· have h64 : key.size = 64 := by simpa [sha256BlockSize] using heq
116+
unfold hmacPrepareKey
117+
simp [h64, sha256BlockSize]
118+
· intro hgt
119+
simp [hmacPrepareKey, sha256OutputSize, hgt, sha256_size]
92120

93121
/-- HMAC is deterministic -/
94122
theorem hmacSha256_deterministic (key message : ByteArray) :
95123
hmacSha256 key message = hmacSha256 key message := by
96124
rfl
97125

98-
/-- HMAC with different keys produces different outputs -/
99-
theorem hmacSha256_key_dependent (key1 key2 message : ByteArray) (h : key1 ≠ key2) :
100-
hmacSha256 key1 message ≠ hmacSha256 key2 message := by
101-
-- This requires cryptographic assumptions about SHA-256
102-
sorry
103-
104-
/-- HMAC with different messages produces different outputs -/
105-
theorem hmacSha256_message_dependent (key message1 message2 : ByteArray) (h : message1 ≠ message2) :
106-
hmacSha256 key message1 ≠ hmacSha256 key message2 := by
107-
-- This requires cryptographic assumptions about SHA-256
108-
sorry
109-
110126
/-- HMAC verification is correct -/
111127
theorem hmacSha256_verification_correct (key message : ByteArray) :
112128
hmacSha256Verify key message (bytesToHex (hmacSha256 key message)) = true := by
@@ -116,8 +132,8 @@ theorem hmacSha256_verification_correct (key message : ByteArray) :
116132
theorem hmacSha256_verification_rejects_incorrect (key message : ByteArray) (wrongSignature : String) :
117133
wrongSignature ≠ bytesToHex (hmacSha256 key message) →
118134
hmacSha256Verify key message wrongSignature = false := by
119-
simp [hmacSha256Verify]
120-
intro h
121-
exact h
135+
intro hne
136+
have h := Ne.symm hne
137+
simp [hmacSha256Verify, h]
122138

123139
end LeanToolchain.Crypto

0 commit comments

Comments
 (0)