Debian Codegraph @

src/Bedrock/Field/Translation/README.md

raw symbols SCIP
1 # Bedrock2 translation
2
3 The code in this subdirectory translates field arithmetic functions in
4 fiat-crypto's internal language to bedrock2 code.
5
6 ## Structure
7
8 The top-level definition for the bedrock2 translation is `translate_func` from
9 `Func.v`; other files in this directory form the lower-level definitions on
10 which it depends.
11
12 `Parameters/` : helper tactics and default values relating to translation
13 parameters
14
15 `Proofs/` : proves that the functions in `Translation/` do their jobs
16 correctly. Generally, each `Translation/XYZ.v` has a `Translation/Proofs/XYZ.v`
17 that corresponds to it.
18
19 ## Method
20
21 The translation process mirrors bedrock2 abstraction levels (see bedrock2's
22 `Syntax.v`):
23
24 1. `expr`, for expressions that don't set local variables, store anything, or
25 otherwise change state
26 2. `cmd`, for expressions that do set local variables. In bedrock2 these are
27 also allowed to store data. However, in this fiat-crypto backend the `cmd`
28 level is not allowed to read from or write to memory.
29 3. `func`, the top level, which wraps the output of the `cmd` phase and
30 generates additional code to handle memory. The bedrock2 code produced by
31 this backend always loads input arrays into local variables immediately
32 after function invocation, and keeps everything in local variables until
33 it's ready to store any output arrays at the end.
34
35 The choice to not allow the `cmd` level to modify memory was a tradeoff that
36 makes sense for the specific domain in which fiat-crypto operates, because you
37 basically always need to access all the information in your input array and
38 gain nothing by modifying it in place. Doing the loads immediately and the
39 stores at the very end also greatly simplified the proofs for the `cmd` level,
40 which can now assume that memory doesn't change. And, since the inputs are
41 read-only, it allowed `translate_func_correct` to use multiple separation-logic
42 preconditions instead of one (one for each input, one for the output), which
43 means it's possible to e.g. add a bignum to itself and then store the result
44 in the same location, in place. The postcondition will be stated in terms of
45 the output precondition (so if you stored the result in one of the inputs, you
46 will of course lose information about that input).
47
48
49 Another thing to note about the translation is that it assumes all lists are
50 handled as arrays and passed as pointers, so if a function returns a list an
51 argument will be added to the bedrock2 implementation: a pointer in which to
52 store the output list. This happens automatically for all lists of integers in
53 the return type.
54
55 ## Restrictions
56
57 Input to the bedrock2 translation must:
58
59 - contain only the subset of operations found in `Translation/Expr.v`
60 - have only one size of integer (see rewriter pipeline options `widen_bytes`
61 and `widen_carry`)
62 - not have arithmetic oprations that return multiple values (for example, low
63 and high half of the output), as these are untranslatable to bedrock2\*
64 - not use types other than integers, lists of integers, and tuples, although
65 tuples can be arbitrarily nested
66 - not assign anything to lists
67
68 \* bedrock2 does support returning multiple values from a function, but
69 currently the compiler does not do function inlining so the relevant
70 substitution is peformed by the fiat-crypto rewriter instead. If bedrock2
71 started supporting multi-return arithmetic operations in the future the
72 translation has most of the code in place to handle that.