Debian Codegraph @

README.md

raw symbols SCIP
1 Fiat-Crypto: Synthesizing Correct-by-Construction Code for Cryptographic Primitives
2 ===================================================================================
3
4 Building
5 --------
6 [![CI (Coq, Docker)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-docker.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-docker.yml?query=branch%3Amaster)
7 [![CI (Coq, Debian)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-debian.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-debian.yml?query=branch%3Amaster)
8 [![CI (Coq, Alpine)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-alpine.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-alpine.yml?query=branch%3Amaster)
9 [![CI (Coq, Arch Linux)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-archlinux.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-archlinux.yml?query=branch%3Amaster)
10 [![CI (Coq, Windows)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-windows.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-windows.yml?query=branch%3Amaster)
11 [![CI (Coq, MacOS)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-macos.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-macos.yml?query=branch%3Amaster)
12 [![CI (opam)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-opam-package.yml/badge.svg?branch=master)](https://github.com/mit-plv/fiat-crypto/actions/workflows/coq-opam-package.yml?query=branch%3Amaster)
13
14 [![Release][release-shield]][release-link]
15 [![Zulip][zulip-shield]][zulip-link]
16 [![Rust Crate][crate-shield]][crate-link]
17 [![Go Reference][pkg.go-shield]][pkg.go-link]
18
19 [release-shield]: https://img.shields.io/github/v/release/mit-plv/fiat-crypto.svg?logo=github&include_prereleases
20 [release-link]: https://github.com/mit-plv/fiat-crypto/releases
21
22 [zulip-shield]: https://img.shields.io/badge/chat-on%20zulip-informational.svg
23 [zulip-link]: https://coq.zulipchat.com/#narrow/stream/247791-fiat-crypto
24
25 [crate-shield]: https://img.shields.io/crates/v/fiat-crypto.svg
26 [crate-link]: https://crates.io/crates/fiat-crypto
27
28 [pkg.go-shield]: https://pkg.go.dev/badge/github.com/mit-plv/fiat-crypto/fiat-go.svg
29 [pkg.go-link]: https://pkg.go.dev/github.com/mit-plv/fiat-crypto/fiat-go
30
31 🌐 Try out synthesis [on the web](https://mit-plv.github.io/fiat-crypto/)!
32 (Supported by [`js_of_ocaml`](https://ocsigen.org/js_of_ocaml/latest/manual/overview) compilation.)
33
34 This repository requires [Coq](https://coq.inria.fr/) 8.20 or later, including OCaml libraries.
35 If you install Coq from Ubuntu aptitude packages, you need `libcoq-ocaml-dev` in addition to `coq`.
36 Note that in some cases (such as installing Coq via homebrew on Mac), you may also need to install `ocaml-findlib` (for `ocamlfind`).
37 The extracted OCaml code for the standalone binaries requires [OCaml](https://ocaml.org/) [4.08](https://ocaml.org/p/ocaml/4.08.0) or later.
38 We suggest downloading [the latest version of Coq](https://github.com/coq/coq/wiki#coq-installation).
39 Generation of JSON code via the Makefile also requires `jq`.
40
41 Alternatively, choose your package-manager to install dependencies:
42
43 Package Manager | Command Line Invocation |
44 --|--|
45 Aptitude (Ubuntu / Debian) | `apt install coq ocaml-findlib libcoq-ocaml-dev jq` |
46 Homebrew (OS X) | `brew install coq ocaml-findlib coreutils jq` |
47 Pacman (Archlinux) | `pacman -S coq ocaml-findlib ocaml-zarith jq` |
48 APK (Alpine) | `apk add --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing ocaml ocaml-findlib coq ocaml-zarith jq make` |
49
50 You can clone this repository with
51
52 git clone --recursive https://github.com/mit-plv/fiat-crypto.git
53
54 Git submodules are used for some dependencies. If you did not clone with `--recursive`, run
55
56 git submodule update --init --recursive
57
58 from inside the repository.
59 Then run:
60
61 make
62
63 You can check out [our CI](https://github.com/mit-plv/fiat-crypto/actions?query=branch%3Amaster+workflow%3A%22CI+%28Coq%29%22) to see how long the build should take; as of the last update to this line in the README, it takes about 1h10m to run `make -j2` on Coq 8.11.1.
64
65 If you want to build only the command-line binaries used for generating code, you can save a bit of time by making only the `standalone-unified-ocaml` target with
66
67 make standalone-unified-ocaml
68
69 Usage (Generating C Files)
70 --------------------------
71
72 The Coq development builds binary compilers that generate code using some implementation strategy.
73 The parameters (modulus, hardware multiplication input bitwidth, etc.) are specified on the command line of the compiler.
74 The generated C code is written to standard output.
75
76 A collection of C files for popular curves can be made with
77
78 make c-files
79
80 The C files will appear in [`fiat-c/src/`](./fiat-c/src/).
81
82 Just the compilers generating these C files can be made with
83
84 make standalone-ocaml
85
86 or `make standalone-haskell` for compiler binaries generated with Haskell, or `make standalone` for both the Haskell and OCaml compiler binaries.
87 The binaries are located in `src/ExtractionOcaml/` and `src/ExtractionHaskell/` respectively.
88
89 There is one compiler binary for all implementation strategies:
90
91 - `fiat_crypto`
92
93 This binary takes arguments for the strategy:
94
95 - `saturated-solinas`
96 - `unsaturated-solinas`
97 - `word-by-word-montgomery`
98 - `dettman-multiplication`
99 - `solinas-reduction`
100 - `base-conversion`
101
102 Passing no arguments, or passing `-h` or `--help` (or any other invalid arguments) will result in a usage message being printed. These binaries output C code on stdout.
103
104 Here are some examples of ways to invoke the binaries (from the directories that they live in):
105
106 # Generate code for 2^255-19
107 ./fiat_crypto unsaturated-solinas '25519' '64' '5' '2^255 - 19' carry_mul carry_square carry_scmul121666 carry add sub opp selectznz to_bytes from_bytes > curve25519_64.c # 1
108 ./fiat_crypto unsaturated-solinas '25519' '32' '10' '2^255 - 19' carry_mul carry_square carry_scmul121666 carry add sub opp selectznz to_bytes from_bytes > curve25519_32.c # 2
109
110 # Generate code for NIST-P256 (2^256 - 2^224 + 2^192 + 2^96 - 1)
111 ./fiat_crypto word-by-word-montgomery 'p256' '32' '2^256 - 2^224 + 2^192 + 2^96 - 1' > p256_32.c # 3
112 ./fiat_crypto word-by-word-montgomery 'p256' '64' '2^256 - 2^224 + 2^192 + 2^96 - 1' > p256_64.c # 4
113
114 Try out the above on the web [🌐<sub>1</sub>][web-1-link] [🌐<sub>2</sub>][web-2-link] [🌐<sub>3</sub>][web-3-link] [🌐<sub>4</sub>][web-4-link].
115 You can find more examples in the [`Makefile`](./Makefile).
116
117 [web-1-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%2225519%22%2C%2264%22%2C%225%22%2C%222%5E255-19%22%2C%22carry_mul%22%2C%22carry_square%22%2C%22carry_scmul121666%22%2C%22carry%22%2C%22add%22%2C%22sub%22%2C%22opp%22%2C%22selectznz%22%2C%22to_bytes%22%2C%22from_bytes%22%5D&interactive
118 [web-2-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%2225519%22%2C%2232%22%2C%2210%22%2C%222%5E255-19%22%2C%22carry_mul%22%2C%22carry_square%22%2C%22carry_scmul121666%22%2C%22carry%22%2C%22add%22%2C%22sub%22%2C%22opp%22%2C%22selectznz%22%2C%22to_bytes%22%2C%22from_bytes%22%5D&interactive
119 [web-3-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22word-by-word-montgomery%22%2C%22p256%22%2C%2232%22%2C%222%5E256-2%5E224%2B2%5E192%2B2%5E96-1%22%5D&interactive
120 [web-4-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22word-by-word-montgomery%22%2C%22p256%22%2C%2264%22%2C%222%5E256-2%5E224%2B2%5E192%2B2%5E96-1%22%5D&interactive
121
122 Note that for large primes, you may need to increase the stack size to avoid stack overflows. For example:
123
124 ulimit -S -s 1048576; ./fiat_crypto word-by-word-montgomery --static gost_512_paramSetB 32 '2^511 + 111'
125
126 This sets the stack size to 1 GB (= 1024 MB = 1024 * 1024 KB = 1048576 KB) before running the command.
127 As of the last edit of this line, this command takes about an hour to run, but does in fact complete successfully.
128 Without a sufficiently large stack-size, it instead stack overflows.
129
130 Usage (Generating Bedrock2 Files)
131 ---------------------------------
132 Output is supported to the research language [bedrock2](https://github.com/mit-plv/bedrock2).
133 The Coq development builds binary compilers that generate code using some implementation strategy.
134 The parameters (modulus, hardware multiplication input bitwidth, etc.) are specified on the command line of the compiler.
135 The generated bedrock2 code is then written to standard output using the bedrock2 C backend.
136
137 A collection of bedrock2/C files for popular curves can be made with
138
139 make bedrock2-files
140
141 The bedrock2/C files will appear in [`fiat-bedrock2/src/`](./fiat-bedrock2/src/).
142
143 Just the compilers generating these bedrock2/C files can be made with
144
145 make standalone-ocaml
146
147 or `make standalone-haskell` for binaries generated with Haskell, or `make standalone` for both the Haskell and OCaml binaries.
148 The binaries are located in `src/ExtractionOcaml/` and `src/ExtractionHaskell/` respectively.
149
150 There is one compiler binary for all implementation strategies:
151
152 - `bedrock2_fiat_crypto`
153
154 This binary takes arguments for the strategy:
155
156 - `saturated-solinas`
157 - `unsaturated-solinas`
158 - `word-by-word-montgomery`
159 - `dettman-multiplication`
160 - `solinas-reduction`
161 - `base-conversion`
162
163 Passing no arguments, or passing `-h` or `--help` (or any other invalid arguments) will result in a usage message being printed. These binaries output bedrock2/C code on stdout.
164
165 Here are some examples of ways to invoke the binaries (from the directories that they live in):
166
167 # Generate code for 2^255-19
168 ./bedrock2_fiat_crypto unsaturated-solinas --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select '25519' '64' '5' '2^255 - 19' carry_mul carry_square carry_scmul121666 carry add sub opp selectznz to_bytes from_bytes > curve25519_64.c # 1
169 ./bedrock2_fiat_crypto unsaturated-solinas --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select '25519' '32' '10' '2^255 - 19' carry_mul carry_square carry_scmul121666 carry add sub opp selectznz to_bytes from_bytes > curve25519_32.c # 2
170
171 # Generate code for NIST-P256 (2^256 - 2^224 + 2^192 + 2^96 - 1)
172 ./bedrock2_fiat_crypto word-by-word-montgomery --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select 'p256' '32' '2^256 - 2^224 + 2^192 + 2^96 - 1' > p256_32.c # 3
173 ./bedrock2_fiat_crypto word-by-word-montgomery --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select 'p256' '64' '2^256 - 2^224 + 2^192 + 2^96 - 1' > p256_64.c # 4
174
175 # Generate code for 2^130 - 5
176 ./bedrock2_fiat_crypto unsaturated-solinas --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select 'poly1305' '64' '3' '2^130 - 5' > poly1305_64.c # 5
177 ./bedrock2_fiat_crypto unsaturated-solinas --no-wide-int --widen-carry --widen-bytes --split-multiret --no-select 'poly1305' '32' '5' '2^130 - 5' > poly1305_32.c # 6
178
179 Try out the above on the web [🌐<sub>1</sub>][web-bedrock2-1-link] [🌐<sub>2</sub>][web-bedrock2-2-link] [🌐<sub>3</sub>][web-bedrock2-3-link] [🌐<sub>4</sub>][web-bedrock2-4-link] [🌐<sub>5</sub>][web-bedrock2-5-link] [🌐<sub>6</sub>][web-bedrock2-6-link].
180 You can find more examples in [`Makefile.examples`](./Makefile.examples).
181
182 [web-bedrock2-1-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%2225519%22%2C%2264%22%2C%225%22%2C%222%5E255-19%22%2C%22carry_mul%22%2C%22carry_square%22%2C%22carry_scmul121666%22%2C%22carry%22%2C%22add%22%2C%22sub%22%2C%22opp%22%2C%22selectznz%22%2C%22to_bytes%22%2C%22from_bytes%22%5D&interactive
183 [web-bedrock2-2-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%2225519%22%2C%2232%22%2C%2210%22%2C%222%5E255-19%22%2C%22carry_mul%22%2C%22carry_square%22%2C%22carry_scmul121666%22%2C%22carry%22%2C%22add%22%2C%22sub%22%2C%22opp%22%2C%22selectznz%22%2C%22to_bytes%22%2C%22from_bytes%22%5D&interactive
184 [web-bedrock2-3-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22word-by-word-montgomery%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%22p256%22%2C%2232%22%2C%222%5E256-2%5E224%2B2%5E192%2B2%5E96-1%22%5D&interactive
185 [web-bedrock2-4-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22word-by-word-montgomery%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%22p256%22%2C%2264%22%2C%222%5E256-2%5E224%2B2%5E192%2B2%5E96-1%22%5D&interactive
186 [web-bedrock2-5-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%22poly1305%22%2C%2264%22%2C%223%22%2C%222%5E130-5%22%5D&interactive
187 [web-bedrock2-6-link]: https://mit-plv.github.io/fiat-crypto/?argv=%5B%22unsaturated-solinas%22%2C%22--lang%22%2C%22bedrock2%22%2C%22--no-wide-int%22%2C%22--widen-carry%22%2C%22--widen-bytes%22%2C%22--split-multiret%22%2C%22--no-select%22%2C%22poly1305%22%2C%2232%22%2C%225%22%2C%222%5E130-5%22%5D&interactive
188
189 License
190 -------
191
192 Fiat-Crypto is distributed under the terms of the MIT License, the Apache License (Version 2.0), and the BSD 1-Clause License; users may pick which license to apply.
193
194 See [`COPYRIGHT`](./COPYRIGHT), [`LICENSE-MIT`](./LICENSE-MIT), [`LICENSE-APACHE`](./LICENSE-APACHE), and [`LICENSE-BSD-1`](./LICENSE-BSD-1) for details.
195
196
197 Reading About The Code
198 ----------------------
199
200 - [Andres Erbsen, Jade Philipoom, Jason Gross, Robert Sloan, Adam Chlipala. Simple High-Level Code For Cryptographic Arithmetic -- With Proofs, Without Compromises. To Appear in Proceedings of the IEEE Symposium on Security & Privacy 2019 (S&P'19). May 2019.](http://adam.chlipala.net/papers/FiatCryptoSP19/FiatCryptoSP19.pdf). This paper describes multiple field arithmetic implementations, and an older version of the compilation pipeline (preserved [here](https://github.com/mit-plv/fiat-crypto/tree/sp2019latest)). It is somewhat space-constrained, so some details are best read about in theses below.
201 - [Jade Philipoom. Correct-by-Construction Finite Field Arithmetic in Coq. MIT Master's Thesis. February 2018.](http://adam.chlipala.net/theses/jadep_meng.pdf) Chapters 3 and 4 contain a detailed walkthrough of the field arithmetic implementations (again, targeting the previous compilation pipeline).
202 - [Andres Erbsen. Crafting Certified Elliptic Curve Cryptography Implementations in Coq. MIT Master's Thesis. June 2017.](
203 http://adam.chlipala.net/theses/andreser_meng.pdf) Section 3 contains a whirlwind introduction to synthesizing field arithmetic code using coq, without assuming Coq skills, but covering a tiny fraction of the overall library. Sections 5 and 6 contain the only write-up on the elliptic-curve library in this repository.
204 - [Jason Gross. Performance Engineering of Proof-Based Software Systems at Scale. MIT Doctoral Thesis. February 2021.](http://adam.chlipala.net/theses/jgross.pdf) Chapters 4 and 5 describe the reflective program transformation framework at the center of the newest compilation pipeline.
205 - The newest compilation pipeline as a whole does not have a separate document yet, but this README does go over it in some detail.
206
207
208 Status of Backends
209 ------------------
210
211 Fiat Cryptography contains a number of backends; the final step of the pipeline is transforming from straight-line C-like code to expressions in whatever language is being targeted.
212 The Bedrock2 backend comes with proofs that the Bedrock2 AST matches the semantics of our internal AST, but none of the other backends have any proofs about them.
213 While the transformations are not particularly involved, and our proofs ensure that we have picked integer sizes large enough to store values at each operation, there is no verification that the particular integer size casts that we emit are sufficient to ensure that gcc, clang, or whatever compiler is used on the code correctly selects integer sizes for expressions correctly.
214 Note that even the C code printed by the Bedrock2 backend does not have proofs that the conversion to strings is correct.
215
216 Hence we provide here a table of the extent to which the various backends are maintained, tested, and proven.
217 A :heavy_check_mark: in "maintainer" means that the Fiat Cryptography maintainers are fully maintaining and testing the backend; :white_check_mark: means maintenance by external contributors.
218 We do not provide any quality guarantees for code generated by the backends.
219
220 Backend | Maintainer / Person of Contact | Build Checked by CI | Generated Code Tested | Internal AST Proven Correct
221 :-- | :-- | :-- | :-- | :--
222 C | :heavy_check_mark: @JasonGross / entire team | :heavy_check_mark: | :heavy_check_mark: (BoringSSL test-suite) |
223 Bedrock2/C | :heavy_check_mark: @andres-erbsen / entire team | :heavy_check_mark: | :heavy_check_mark: (BoringSSL test-suite) | :heavy_check_mark:
224 Go | :white_check_mark: @mdempsky | :heavy_check_mark: | |
225 Java | :x: Unmaintained | :heavy_check_mark: | :x: Known Buggy [#707](https://github.com/mit-plv/fiat-crypto/issues/707#issuecomment-763098543) |
226 JSON | Experimental | :white_check_mark: (only syntax) | |
227 Rust | :white_check_mark: | :heavy_check_mark: | :heavy_check_mark: ([Dalek Cryptography test-suite](https://github.com/dalek-cryptography/curve25519-dalek)) |
228 Zig | :white_check_mark: @jedisct1 | :heavy_check_mark: | :white_check_mark: ([Zig standard library](https://github.com/ziglang/zig/tree/master/lib/std/crypto/pcurves) (generated file [here](https://github.com/ziglang/zig/blob/master/lib/std/crypto/pcurves/p256/p256_64.zig)) (main CI [here](https://dev.azure.com/ziglang/zig/_build?definitionId=1&_a=summary&repositoryFilter=1&branchFilter=5%2C5%2C5%2C5%2C5%2C5%2C5%2C5))) |
229
230 ### Contributing a new backend
231
232 We weclome new backends (as long as you're willing to maintain them).
233 We hope that the process of contributing a new backend is not too painful, and welcome feedback on how the process could be streamlined.
234 To contribute a new backend, follow the following steps (perhaps using, for example, [#936](https://github.com/mit-plv/fiat-crypto/pull/936), [#660](https://github.com/mit-plv/fiat-crypto/pull/660), [#638](https://github.com/mit-plv/fiat-crypto/pull/638), or [#570](https://github.com/mit-plv/fiat-crypto/pull/570) as examples):
235 - Add a new file to [`src/Stringification/`](./src/Stringification/) for your language, modeled after the existing file of your choice
236 - Run `git add` on your new file and then `make update-_CoqProject` to have the build system track your file
237 - Update [`src/CLI.v`](./src/CLI.v) to `Require Import` your file and add your language to [the list `default_supported_languages`](https://github.com/mit-plv/fiat-crypto/blob/8944697db11055abeeba252782d2bfd1e2b844cd/src/CLI.v#L212-L219) so that it can be passed to the binaries as an argument to `--lang`
238 - Update the `Makefile` in the following ways:
239 - Consider adding a variable near `CARGO_BUILD` for the build invocation
240 - Add targets to `.PHONY` analogous to `c-files`, `lite-c-files`, `test-c-files`, `only-test-c-files`
241 - Add a variables analogous to `C_DIR`, `ALL_C_FILES`, and `LITE_C_FILES` for your language's generated files
242 - Add targets analogous to `c-files` and `lite-c-files` and make `generated-files` and `lite-generated-files` depend on those targets respectively
243 - Add build rules for `ALL_<YOUR-LANGUAGE>_FILES`
244 - Add targets for `test-<your-language>-files` and `only-test-<your-language>-files`; both targets should have the same build rule, but `test-<your-language>-files` should depend on all the generated files of your language, while `only-test-<your-language>-files` should not have any build rule dependencies.
245 - If you are developing a package, you can look for uses of `COPY_TO_FIAT_RUST` or `COPY_TO_FIAT_GO` to see how license files are copied
246 - Run `make` to generate all the relevant files of your language, and add the generated files to git
247 - Update `.gitignore` to ignore any compiled files generated by the compiler of your language (analogous to `.o` and `.a` for C)
248 - Create a file in [`.github/workflows/`](./.github/workflows/) like [`c.yml`](./.github/workflows/c.yml), [`go.yml`](./.github/workflows/go.yml), [`rust.yml`](./.github/workflows/rust.yml), etc, to test at least that the generated code compiles.
249 - Optionally, also test the built code against some external project's crypto unit tests
250 - Update [`.gitattributes`](./.gitattributes) to mark your language's generated files as `text` and to mark the paths of the generated files as `linguist-generated` so that diffs don't clog up PR displays.
251 - Add your language to the table in preceeding section of the README indicating the status of your backend and marking your GitHub username as the maintainer.
252
253
254 Reading The Code
255 ----------------
256
257 ### Demo of Synthesis
258
259 The idea of the synthesis process is demoed in [`src/Demo.v`](./src/Demo.v).
260 We strongly recommend reading this before studying the full-scale system.
261
262 ### Proofs About Elliptic Curves
263
264 We have some about elliptic curves, for example:
265
266 - [`src/Curves/Edwards/AffineProofs.v`](./src/Curves/Edwards/AffineProofs.v),
267 - [`src/Curves/Edwards/XYZT/Basic.v`](./src/Curves/Edwards/XYZT/Basic.v),
268 - [`src/Curves/Montgomery/AffineProofs.v`](./src/Curves/Montgomery/AffineProofs.v),
269 - [`src/Curves/Montgomery/XZProofs.v`](src/Curves/Montgomery/XZProofs.v).
270
271 ### Actual Synthesis Pipeline
272
273 The entry point for clients of the PHOAS expressions we use is
274 `Language/API.v`. Refer to comments in that file for an explanation
275 of the interface; the following text describes how the expressions are
276 generated, not how to interact with them.
277
278 The ordering of files (eliding `*Proofs.v` files) is:
279
280 ```
281 Language/*.v
282 ↑
283 ├────────────────────────────────┬───────────────────────┬───────────────────────┐
284 AbstractInterpretation/*.v MiscCompilerPasses.v Rewriter/*.v PushButtonSynthesis/ReificationCache.v Arithmetic.v
285 ↑ ↑ ↑ ↑ ↑
286 Stringification/*.v │ │ │ COperationSpecifications.v
287 ↑ │ │ │ ↑
288 └────────────┬───────────────────┴───────────────────────┴────────┬──────────────┘ │
289 BoundsPipeline.v CompilersTestCases.v │
290 ↑ │
291 └────────────┬──────────────────────────────────────────────────────────────────────────────────────────┘
292 PushButtonSynthesis/*.v
293 ↑
294 ┌──────────┴────────────────┐
295 CLI.v SlowPrimeSynthesisExamples.v
296 ↑
297 ┌──────────┴────────────────┐
298 StandaloneHaskellMain.v StandaloneOCamlMain.v
299 ↑ ↑
300 ExtractionHaskell.v ExtractionOCaml.v
301 ```
302
303 Within each directory, the dependency graphs (again eliding `*Proofs.v` and related files) are:
304
305 Within `Language/`:
306 ```
307 Pre.v ←──────────────────────────────────────────────────────────────────────── IdentifierParameters.v
308 ↑ ↑
309 Language.v ←── IdentifiersBasicLibrary.v ←──── IdentifiersBasicGenerate.v ←── IdentifiersBasicGENERATED.v ←───────────────────────────── API.v
310 ↑ ↑ ↑
311 ├────────────────┐ └────────────────────────────┐ │
312 UnderLets.v IdentifiersLibrary.v ←──────────── IdentifiersGenerate.v ←─────── IdentifiersGENERATED.v
313 ↑ ↑ ↑
314 IdentifiersLibraryProofs.v ←─── IdentifiersGenerateProofs.v ←─ IdentifersGENERATEDProofs.v
315 ```
316
317 Within `Stringification/`:
318 ```
319 Language.v
320 ↑
321 IR.v
322 ↑
323 ┌──┴───────┐
324 C.v Rust.v
325 ```
326
327 We will come back to the `Rewriter/*` files shortly.
328
329 The files contain:
330
331 - `Arithmetic.v`: All of the high-level field arithmetic stuff
332
333 - `COperationSpecifications.v`: The specifications for the various
334 operations to be synthesized.
335 TODO: This file should probably be renamed.
336
337 - `AbstractInterpretation/*.v`: type-code-based ZRange definitions, abstract
338 interpretation of identifiers (which does let-lifting, for historical reasons,
339 and the dependency on UnderLets should probably be removed), defines the
340 passes:
341 + PartialEvaluateWithBounds
342 + PartialEvaluateWithListInfoFromBounds
343 + CheckPartialEvaluateWithBounds
344
345 - `MiscCompilerPasses.v`: Defines the passes:
346 + EliminateDead (dead code elimination)
347 + Subst01 (substitute let-binders used 0 or 1 times)
348
349 - `Rewriter/*.v`: rewrite rules, rewriting. See below for actual structure
350 of files. Defines the passes:
351 + RewriteNBE
352 + RewriteArith
353 + RewriteArithWithCasts
354 + RewriteStripLiteralCasts
355 + RewriteToFancy
356 + RewriteToFancyWithCasts
357 + PartialEvaluate (which is just a synonym for RewriteNBE)
358
359 - Inside `Language/`:
360
361 + `Pre.v`: A few definitions which are used in writing out rewrite
362 rules and the interpretations of PHOAS identifiers, e.g.,
363 `ident.cast`, `ident.eagerly`, `Thunked.list_rect`, etc
364
365 + `Language.v`: Defines parts of the PHOAS basic infrastructure
366 parameterized over base types and identifiers including:
367 * PHOAS
368 * reification
369 * denotation/intepretation
370 * utilities for inverting PHOAS exprs
371 * default/dummy values of PHOAS exprs
372 * default instantiation of generic PHOAS types
373 * Gallina reification of ground terms
374 * Flat/indexed syntax trees, and conversions to and from PHOAS
375
376 Defines the passes:
377 * ToFlat
378 * FromFlat
379 * GeneralizeVar
380
381 + `API.v`: Specializes the type of PHOAS expressions to the
382 particular identifiers we're using, and defines convenience
383 notations, tactics, and definitions for some of the specialized
384 versions.
385
386 + `IdentifierParameters.v`: Defines a couple of definitions
387 determining the identifiers and types used by the language. These
388 are used as input for the generation of identifier definitions.
389
390 + `IdentifiersBasicLibrary.v`: Defines the package type holding basic
391 identifier definitions.
392
393 + `IdentifiersBasicGenerate.v`: Defines the tactics that generate
394 all of the identifier-list-specific definitions used by the PHOAS
395 machinery, in addition to defining the tactics that do reification
396 based on the generated package.
397
398 + `IdentifiersBasicGENERATED.v`: Basically autogenerated file that
399 defines the inductives of base type codes and identifier codes
400 (the first hand-written because it's short; the latter copy-pasted
401 from a tactic that prints out the inductive), and calls the
402 package-generation-tactic from `IdentifiersBasicGenerate.v`.
403
404 + `UnderLets.v`: the UnderLets monad, a pass that does substitution
405 of var-like things, a pass that inserts let-binders in the
406 next-to-last line of code, substituting away var-like things (this
407 is used to ensure that when we output C code, aliasing the input
408 and the output arrays doesn't cause issues).
409 Defines the passes:
410 * SubstVar
411 * SubstVarLike
412 * SubstVarOrIdent
413
414 The following files in `Language/` are used only by the rewriter:
415
416 + `IdentifiersLibrary.v`: Some definitions about identifiers and
417 pattern identifiers and raw identifiers. Some of these
418 definitions take generated definitions as arguments. Also defines
419 a package record to hold all of the generated definitions.
420
421 + `IdentifiersGenerate.v`: Tactics to generate definitions about
422 untyped and pattern versions of identifiers for the rewriter.
423 Culminates in a tactic which inhabits the package type defined in
424 `IdentifiersLibrary.v`
425
426 + `IdentifiersLibraryProofs.v`: proofs about definitions in
427 IdentifiersLibrary. Also defines a package to hold generated
428 proofs that require destructing inductives not yet defined in this
429 file.
430
431 + `IdentifiersGenerateProofs.v`: tactics to prove lemmas to inhabit
432 the package defined in `IdentifiersLibraryProofs.v`
433
434 + `IdentifiersGENERATE.v`: identifiers / inductives and definitions
435 generated by IdentifiersGenerate.
436
437 + `IdentifiersGENERATEProofs.v`: proofs generated by
438 IdentifiersGenerateProofs, about definitions in
439 IdentifiersGENERATE
440
441 - Inside `Stringification/`:
442
443 + `Language.v`: defines a printer (Show instance) for the PHOAS
444 language, defines some common language-independent utilities for
445 conversion to output code, and defines the spec/API of conversion
446 from PHOAS to code in a language as strings. (Depends on
447 `AbstractInterpretation.v` for ZRange utilities.) Defines the
448 passes:
449 * ToString.LinesToString
450 * ToString.ToFunctionLines
451
452 + `IR.v`: Defines a common IR for C and Rust (and maybe eventually
453 other languages), and builds most of the infrastructure necessary
454 for instantiating the LanguageSpecification API for a language
455 with pointers and function calls
456
457 + `C.v`: conversion to C code as strings. Instantiates the API
458 defined in `Stringification.Language`.
459
460 + `Rust.v`: conversion to Rust code as strings. Instantiates the
461 API defined in `Stringification.Language`.
462
463 - `CompilersTestCases.v`: Various test cases to ensure everything is working
464
465 - `BoundsPipeline.v`: Assemble the various compiler passes together into
466 a composed pipeline. It is the final interface for the compiler.
467 Also contains some tactics for applying the BoundsPipeline
468 correctness lemma.
469
470 - `PushButtonSynthesis/ReificationCache.v`: Defines the cache that
471 holds reified versions of operations, as well as the tactics that
472 reify and apply things from the cache.
473
474 - `PushButtonSynthesis/*`: Reifies the various operations from
475 `Arithmetic.v`, defines the compositions of the BoundsPipeline with
476 these operations, proves that their interpretations satisfies the
477 specs from `COperationSpecifications.v`, assembles the reified
478 post-bounds operations into synthesis targets. These are the files
479 that `CLI.v` depends on:
480 + `ReificationCache.v`:
481 Defines the cache of pre-reified terms. Splitting up
482 reification from uses of the pipeline allows us to not have to
483 re-reify big terms every time we change the pipeline or
484 intermediate stages thereof.
485 + `InvertHighLow.v`:
486 Defines some common definitions, around splitting apart high and
487 low bits of things, for Barrett and FancyMontgomeryReduction.
488 + `Primitives.v`:
489 Specializes the pipeline to basic "primitive" operations such as
490 cmovznz, addcarryx, subborrowx, etc.
491 + `SmallExamples.v`:
492 Some small examples of using the pipeline. Nothing depends on
493 this file; it is for demonstration purposes only.
494 + `*ReificationCache.v`:
495 Holds the reified versions of the definitions used in the
496 corresponding file.
497 + `BarrettReduction.v`, `FancyMontgomeryReduction.v`,
498 `SaturatedSolinas.v`, `UnsaturatedSolinas.v`, `WordByWordMontgomery.v`:
499 Holds the instantiation of the pipeline to the corresponding
500 implementation choice, as well as any relevant correctness
501 proofs (such as that things assemble into a ring).
502
503 - `SlowPrimeSynthesisExamples.v`: Additional uses of the pipeline for
504 primes that are kind-of slow, which I don't want extraction blocking
505 on. Also contains some debugging examples.
506
507 - `CLI.v`: Setting up all of the language-independent parts of extraction; relies
508 on having a list of strings-or-error-messages for each pipeline, and on the
509 arguments to that pipeline, and builds a parser for command line arguments for
510 that.
511
512 - `StandaloneHaskellMain.v`, `StandaloneOCamlMain.v`, `ExtractionHaskell.v`,
513 `ExtractionOCaml.v`: Extraction of pipeline to various languages
514
515
516 The files defined in `Rewriter/` are split up into the following
517 dependency graph (including some files from `Language/` at the top):
518 ```
519 IdentifiersLibrary.v ←───────────────────────── IdentifiersGenerate.v ←──────────────────── IdentifiersGENERATED.v
520 ↑ ↑ ↑ ↑
521 │ └──────────────── IdentifiersLibraryProofs.v ←──────┴─ IdentifiersGenerateProofs.v ←─ IdentifersGENERATEDProofs.v
522 │ ↑ ↑
523 │ │ │
524 │ │ │
525 │ │ │
526 │ │ │
527 Rewriter.v ←────────────────────── ProofsCommon.v ←──────────────────── ProofsCommonTactics.v │
528 ↑ ↗ ↖ ↑ │
529 Reify.v ←──────────────┐ Wf.v InterpProofs.v │ │
530 │ ↖ ↗ │ │
531 Rules.v └──────────── AllTactics.v ──────────────────────────────┘ │
532 ↑ ↑ ┌───────────────────────────────────────────────┘
533 RulesProofs.v AllTacticsExtra.v
534 ↑ ↑
535 ├────────┬─────────────┬───────────────┴────────┬─────────────────────────────┐
536 │ Passes/NBE.v Passes/Arith.v Passes/ArithWithCasts.v Passes/StripLiteralCasts.v
537 │ ↑ ↑ ↑ ↑
538 │ └─────────────┴────────────────────────┴─────────────────────────────┴─────────────┐
539 │ │
540 └────────┬──────────────────────────┐ │
541 Passes/ToFancy.v Passes/ToFancyWithCasts.v │
542 ↑ ↑ │
543 └───────┬──────────────────┴───────────────────────────────────────────────────────┘
544 │
545 All.v
546 ```
547
548 - `Rules.v`: Defines the list of types of the rewrite rules that
549 will be reified. Largely independent of the expression language.
550
551 - `RulesProofs.v`: Proves all of the Gallina versions of the
552 rewrite rules correct.
553
554 - `Rewriter.v`: Defines the rewriter machinery. In particular, all of
555 the rewriter definitions that have non-rewrite-rule-specific proofs
556 about them are found in this file.
557
558 - `RewrierReify.v`: Defines reification of rewrite rules, continuing on
559 from `Rewriter.v`, and culminates in the tactic
560 `RewriteRules.Tactic.Build_RewriterT` and the tactic notation
561 `make_Rewriter` which define a package of type
562 `RewriteRules.GoalType.RewriterT`. The `Build_*` tactic returns a
563 `constr`, while the `make_*` tactic notation refines that `constr`
564 in the goal. Both tactics take two arguments: first a boolean
565 `include_interp` which specifies whether (`true`) or not (`false`)
566 to prefix the list of rewrite rules with the reduction-to-literal
567 rewrite rules; and second a list of `bool * Prop` which is the list
568 of rewrite rule types to reify, each paired with a boolean saying
569 whether or not to try rewriting again in the output of the
570 replacement for that rule.
571
572 - `ProofsCommon.v`: Defines the notion of interp-goodness and wf-goodness
573 for rewrite rules, defines tactics to prove these notions, and
574 contains a semi-arbitrary collection of proofs and definitions that
575 are mostly shared between the wf proofs and the interp proofs.
576 Importantly, this file defines everything needed to state and prove
577 that specific rewrite rules are correct. Additionally defines a
578 package `RewriteRules.GoalType.VerifiedRewriter` which describes the
579 type of the overall specialized rewriter together with its `Wf` and
580 `Interp` proofs. (This package should perhaps move to another file?)
581
582 - `ProofsCommonTactics.v`: Defines the actual tactics used to prove that
583 specific rewrite rules are correct, and to inhabit the packages
584 defined in `ProofsCommon.v`.
585
586 - `Wf.v`: Proves wf-preservation of the generic rewriter,
587 taking in wf-goodness of rewrite rules as a hypothesis.
588
589 - `InterpProofs.v`: Proves interp-correctness of the generic
590 rewriter, taking in interp-goodness of rewrite rules as a
591 hypothesis.
592
593 - `AllTactics.v`: Defines the tactic
594 `RewriteRules.Tactic.make_rewriter` (and a similar tactic notation)
595 which build the entire `VerifiedRewriter`. They take in the
596 `include_interp` as in `Rewriter.v` tactics, as well as an hlist of
597 proofs of rewrite rules indexed over a `list (bool * Prop)` of
598 rewrite rule types. This is the primary interface for defining a
599 rewriter from a list of rewrite rules.
600
601 - `AllTacticsExtra.v`: Specializes `AllTactics.v` to
602 what's defined in `Identifier.v`
603
604 - `{NBE, Arith, ArithWithCasts, StripLiteralCasts, ToFancy,
605 ToFancyWithCasts}.v`: Use the tactic from `AllTactics.v`
606 together with the proven list of rewrite rules from
607 `RulesProofs.v` to reify and reduce the corresponding pass
608 and generate a rewriter.
609
610 - `All.v`: `Definition`less file that `Export`s the rewriters
611 defined in `Rewriter/*.v`
612
613
614 Proofs files:
615 For `Language.v`, there is a semi-arbitrary split between two files
616 `Language.Inversion` and `Language.Wf`.
617 - `Inversion.v`:
618 + classifies equality of type codes and exprs
619 + type codes have decidable equality
620 + correctness of the various type-transport definitions
621 + correctness lemmas for the various `expr.invert_*` definitions
622 + correctness lemmas for the various `reify_*` definitions in `Language.v`
623 + `inversion_type`, which inverts equality of type codes
624 + `type_beq_to_eq`, which converts boolean equality of types to
625 Leibniz equality
626 + `rewrite_type_transport_correct`, which rewrites with the
627 correctness lemmas of the various type-transport definitions
628 + `type.invert_one e` which does case analysis on any inductive type
629 indexed over type codes, in a way that preserves information
630 about the type of `e`, and generally works even when the goal is
631 dependently typed over `e` and/or its type
632 + `ident.invert`, which does case-analysis on idents whose type has
633 structure (i.e., is not a var)
634 + `ident.invert_match`, which does case-analysis on idents appearing as
635 the discriminee of a `match` in the goal or in any hypothesis
636 + `expr.invert`, which does case-analysis on exprs whose type has
637 structure (i.e., is not a var)
638 + `expr.invert_match`, which does case-analysis on exprs appearing as
639 the discriminee of a `match` in the goal or in any hypothesis
640 + `expr.invert_subst`, which does case-analysis on exprs which show up
641 in hypotheses of the form `expr.invert_* _ = Some _`
642 + `expr.inversion_expr`, which inverts equalities of exprs
643
644 - `Wf.v`: Depends on `Inversion.v`
645 Defines:
646 + expr.wf, expr.Wf, expr.wf3, expr.wf4, expr.Wf3, expr.Wf4
647 + GeneralizeVar.Flat.wf
648 + `expr.inversion_wf` (and variants), which invert `wf` hypotheses
649 + `expr.wf_t` (and variants wf_unsafe_t and wf_safe_t) which make
650 progress on `wf` goals; `wf_safe_t` should never turn a provable
651 goal into an unprovable one, while `wf_unsafe_t` might.
652 + `expr.interp_t` (and variants), which should make progress on
653 equivalence-of-interp hypotheses and goals, but is not used much
654 (mainly because I forgot I had defined it)
655 + `prove_Wf`, which proves wf goals on concrete syntax trees in a more
656 optimized way than `repeat constructor`
657 Proves:
658 + funext → (type.eqv ↔ Logic.eq) (`eqv_iff_eq_of_funext`)
659 + type.related and type.eqv are PERs
660 + Proper instances for `type.app_curried`, `type.and_for_each_lhs_of_arrow`
661 + `type.is_not_higher_order` → Reflexive (type.and_for_each_lhs_of_arrow type.eqv)
662 + iff between `type.related{,_hetero}` and related of `type.app_curried`
663 + various properties of `type.and{,b_bool}for_each_lhs_of_arrow`
664 + various properties of `type.eqv` and `ident.{gen_,}interp`
665 + various properties of `ident.cast`
666 + various properties of `expr.wf` (particularly of things defined in `Language.v`)
667 + interp and wf proofs for the passes to/from Flat
668
669 - `UnderLetsProofs.v`: wf and interp lemmas for the various passes defined in `UnderLets.v`
670 - `MiscCompilerPassesProofs.v`: wf and interp lemmas for the various passes defined in `MiscCompilerPasses.v`
671 - `AbstractInterpretation/ZRangeProofs.v`: Proves correctness lemmas of the per-operation zrange-bounds-analysis functions
672 - `AbstractInterpretation/Wf.v`: wf lemmas for the AbstractInterpretation pass
673 - `AbstractInterpretation/Proofs.v`: interp lemmas for the
674 AbstractInterpretation pass, and also correctness lemmas that
675 combine Wf and interp