Skip to content

Commit 7b42e51

Browse files
RobinJadoulerik-3milabsColoCarletticdesaintguilhemgreptile-apps[bot]
authored
spec v0.2 (#370)
* spec: initial spec commit * spec: Basic chip data format and layout See the original yetanotherco/lambda_vm_spec #1 for more details, if it still exists. * Introduce `config` and "variables" * chip column-to-table rendering * restructuring * some basic interactions idea * Sample lt chip design * Update formatting * Interpret variable indexing * BRANCH draft * Fix indexing + render template * Render labels for references to constraints * Rendering chip assumptions * Add an editorconfig for consistency in indentation and trailing newlines * The constraint range index found its way back home * Finish (?) LT chip * Improve lisp rendering * support constraint group rendering * Support "^" type setting * dvrm * add dvrm assumptions * Rendering virtual column definitions and polynomials for arith constraints * ignore ebook.pdf * Split LT and BRANCH into groups * Nicer mutual recursion in expression formatting * Use negation instead of mult by -1 in lt * Format expr.typ * Simplify subtraction expression * Remove parentheses using precedence rules * fmt * improve dvrm readability * fix lt parentheses * move `extended_n_sub_r` def from constraint to var * Set div chip word types to HL * divrem fixes * more dvrm tweaks * Specify grammar * add docs * Drop chip files * Improve `chip` readability * minor fixes --------- Co-authored-by: Erik Takke <erik.takke@3milabs.tech> * spec: Fix some chip rendering pain points (#83) This fixes the following pain points: - Assumptions and constraints requiring a `ref` for rendering to succeed - The `desc` field of `arith` constraints not being rendered - The `constraint` field of an `arith` constraint using eval in code mode - Long tables (columns and constraints) didn't break across pages - Template constraints did not have conditions rendered - Constraint groups didn't get the proper prefix if specified - The default branch of expression rendering has missing arguments It also introduces a nice visual todo macro --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: support array-like types (#85) Support array-like variable types. Typed as: ```toml [[variables.auxiliary]] name = "var" type = ["Bit", 5] desc = "five bits" ``` --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Fixup wrong type sanity check for array types (#86) * Make precedence a lookup table instead of hardcoding it * Render type cast expressions * spec: Allow desc field on non-arith constraints as clarification * spec: Modify cast operator precedence * spec: improve definitions (#91) Updated definition rendering: * definitions are only allowed for virtual variables * definitions are now labelled with `def` rather than `poly` or `polys` * more flexible definitions possible for array-type virtuals. * spec: update table rendering (#93) * spec: update `description` printing * spec: update `polynomial constraint` printing * spec: introduce "condition" column type * spec: is_bit template * spec: CPU chip for RV64IMC (#88) * spec: Initial CPU version to handle RV64IMC * Address review comments * Add word_instr as input to SHIFT --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: improve multi-poly definition rendering (#98) * spec: BRANCH chip (#92) * spec: init BRANCH chip * Small cleanup * Clean up variable naming and generally address review comments * outdated comment --------- Co-authored-by: Erik Takke <erik.takke@3milabs.tech> * spec: conditionally render constraint table headers (#94) * spec: conditionally render constraint table headers * spec: simplify `selected_constraints` expression * spec: repurpose `selected_constraints` * spec: do not print index in assumption/constraint ref (#96) * spec: Make constraint numbering restart when displaying multiple chips in one document (#108) * spec: Introduce LT chip (#90) Co-authored-by: Erik Takke <erik.takke@3milabs.tech> * spec: Fix constraint group lookup (#105) * spec: `SHIFT` chip (#84) * spec: rough draft SHIFT chip * various minor fixes * implement right-limb shifting * Update rendering "polynomial constriant" in table * fix degree 4 issues * Further update to SHIFT chip * Clean up SHIFT * spec/shift: add assumption * spec/shift: Add lookup constraint * spec/shift: make extension virtual Kudos to Robin for uncovering this! * spec/shift: Simplify limb-situation Kudos to Robin for pointing this out! * spec/SHIFT: fix typo * Turn `limb_shift_x` into array * spec: support "sum" expression in math * Simplify limb-shifting constraint * spec: attempt at refactoring `shift` * spec: overhaul SHIFT * spec: SHIFT: rename `extensions` as `extension` Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: SHIFT: make `shift` of type Byte * spec: SHIFT: replace variable '0x' with constant 0x * spec: SHIFT: remove "cheaper" remark * spec: SHIFT: fix `shifted` description * spec: SHIFT: make output a DWordWL * spec: SHIFT * spec: SHIFT: introduce explanation; update some constraint elaborations * Apply suggestions from the code review Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: SHIFT: update `bits_shift` desc * spec: SHIFT: update `limb_shift` desc * spec: SHIFT: add missing IS_BIT constraint for limb_shift * spec: SHIFT: update description * spec: SHIFT: fix sum's expr-to-math * Minor language pass Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: `ADD` template (#97) * spec: ADD draft * spec: ADD: fix `carry` size * spec: ADD: clarify sum is mod 2^64 * spec: introduce `SUB` template notation. * Fix assumption indices Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * Fix typos Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: have column table subheaders repeat on page wrap (#121) * spec: drop `dot` when multiplying constant with one-letter variable. (#120) * spec: `MUL` chip (#122) * spec: support "sum" expression * spec: introduce "QuadHL" type * spec: introduce MUL chip * spec: Introduce QuadWL * spec: introduce B20[4] * spec: simplify MUL to 26 columns * spec: Fix expr-sum bug * spec: simplify MUL to 22 columns * spec: improve MUL readability * spec: MUL: fix indexing * spec: MUL: refactor * spec: drop B20 * spec: MUL: fix raw_product relation * spec: MUL: fix IS_B19 check range Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: MUL: add missing res range check assumption * spec: MUL: remove superfluous/invalid constraints * spec: MUL: leverage SIGN template * spec: MUL: fix index mistake * spec: MUL: update description * spec: permit non-constant exponents * spec: MUL: drop `limb_product` * spec: MUL: minor tweaks * spec: MUL: bump headers * spec: MUL: update description * spec: MUL: update to IS_B20 * spec: MUL: remove 'eloquent' * Apply suggestions from code review Thanks Robin! Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: MUL: define padding --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Add support for specifying padding values of columns (#133) Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: update range specifications to iters concept (#130) Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: `BITWISE` chip (#138) * spec: introduce BITWISE * spec: BITWISE: outline optimizations * spec: BITWISE: fix SLL naming mismatch * spec: BITWISE: fix length computation mistake * spec: drop `dot` in `expr_to_code` when multiplying constant with single-letter variable * spec: Initial inefficient MEMW chip (#104) Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: LOAD chip (#144) * fix CPU-CA41 typo (#189) * spec: `DECODE` (#143) * spec: DECODE: decode basics * spec: DECODE: update table + add *W instructions * spec: fix padding table for chips that don't have all types of variables * spec: introduce B49 * spec: DECODE: split-off decode uncompressed * spec: DECODE: overhaul decode * Apply suggestions from code review Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * Apply suggestion from @RobinJadoul Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * Fix `ADDI` flag mistakes * spec: DECODE: make `packed_encode` a `BaseField`; remove superfluous `B49` * spec: DECODE: set `mem_xB` when reading/writing _exactly_ `x` bytes * spec: DECODE: update `mp_selector` description. * Apply suggestions from code review * spec: DECODE: merge uncompressed page into decode.typ --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> *  spec: placeholder chapters for chips to come (#190) * fix(spec): Use a better precedence value for "idx" (#197) * fix(spec): Missing `write_register` multiplicity. (#196) * spec: Initial version of memory argument (#164) Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * fix(spec): Correct typo in spec README and align style (#210) * spec: CPU padding (#195) * spec: CPU fast path for x0 reads * Do not write/read pc when in a padding row * specify padding for the CPU * Apply suggestions from code review Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: DECODE: update padding row * spec: DECODE: explain 'one more instruction' * spec: CPU: fix c_type_instruction typo * Apply suggestions from code review Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: align `packed_decode` in `DECODE` and `CPU` * spec: DECODE: add `read_registerX` to `packed_decode` * spec: DECODE: specify `read_register1` and `2` * spec: DECODE: update pc padding value * spec: DECODE: several small fixes * spec: DECODE: fix ECALL's rs2 value Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: DECODE: minor rewording Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: DECODE: minor fix --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> Co-authored-by: Erik Takke <erik.takke@3milabs.tech> * spec: update `ECALL` signature (#244) * spec: update `ECALL` signature * spec: CPU/ECALL: cast rv1 to DWordWL * spec: Allow for cross referencing between different chapters, both in pdf and web mode (#225) * spec: Allow for cross referencing between different chapters, both in pdf and web mode * Improve PDF organization The PDF now no longer depends on shiroa trickery to compile, so errors are more clearly visible instead of being hidden behind layour iterations. Additionally, we can now have nice chapter headings and references to them. * Allow xref by specifying only the label * document strip-all * It does work, after all; with only ~7GB of RAM usage for the entire thing * small cleanup * Update spec/book.typ Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * Address some review comments * less repetition for file names --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: Update LT interaction signature so that it can be used properly for timestamps (#246) * spec: Update LT interaction signature so that it can be used properly for timestamps * fix(spec): add missing signed argument to LT from MEMW * Update spec/src/lt.toml Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * Update spec/src/lt.toml Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: `HALT` chip (#235) * spec: HALT: first draft * spec: HALT: add link to sys call number * spec: HALT: update ECALL signature * spec: HALT: minor update * spec: HALT: document cleanup verification alternative * adapt to new chapter format * spec: HALT: fix MEMW register indexing * spec: HALT: move halt.typ into ecall.typ --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: minor `MUL` fixes (#223) * spec: MUL: fix missing iters * spec: MUL: fix res slice in lookup contribution * spec: MU: split `res` into `lo` and `hi` * spec: MUL: replace `range` by `iter` * spec: MUL: update `lo` and `hi` types + introduce `res` as virtual * spec: MUL: add note on future optimization * spec: `SIGN` (#279) * spec: introduce SIGN template * Update spec/src/sign.toml Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * spec: drop `IsZero` template (#278) * spec: fix header levels (#264) * spec: offset headers in PDF * spec: decrement header levels for chip descriptions * spec: move heading offset to ebook.typ * spec: LOAD: fix LOAD-C9 signature (#284) * spec: `NEG` template (#270) * spec: tweak code-rendering "not" * spec: introduce NEG template * Update spec/book.typ Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * spec: update NEG * spec: NEG: refactor * spec: NEG: fix range-assumption on x * spec: NEG: update cond * spec: tweak math-rendering "not" Analogous to 801f5ee * spec: NEG: add non-zero x case distinction --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * spec: Introduce DVRM chip spec: DVRM: introduce `μ_sum` spec: DVRM: apply SIGN template spec: DVRM: fix `n_sub_r_is_negative` spec: DVRM: range check `n_sub_r` spec: DVRM: add missing LT constraint spec: DVRM: add missing abs_* range checks required by SUB calls. spec: DVRM: fix LT lookup spec: support variable labelling spec: DVRM: completely refactor DVRM chip spec: DVRM: make multiplicities binary spec: DVRM: spec padding spec: DVRM: remove superfluous TODOs spec: DVRM: drop msb lookup for `sign_r` spec: DVRM: replace `range=` by `iter=` spec: DVRM: replace range assumptions for q and r by constraints Apply suggestions from code review Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> spec: DVRM: drop bit checks for multiplicities spec:DVRM: complete refactor spec: DVRM: update padding spec: DVRM: fix minor discrepancy spec: DVRM: drop superfluous `q_if_overflow` spec: DVRM: fix typos spec: DVRM: fix casting spec: ZERO: expand lookup to B20 spec: DVRM: abandon `IsZero` and `IsEqual` templates spec: DVRM: fix typo spec: expr: update constant rendering in expr_to_math Update spec/bitwise.typ Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> spec: DVRM: replace [Half, x] by xHL spec: DVRM: use QuadHL-sub to constrain `extended_n_sub_r` spec: drop support variable labelling This reverts commit c8d6896 (and removes a bit more). spec: DVRM: fix dvrm:c:div_by_zero Update spec/dvrm.typ Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: signatures (#280) * spec: list all interaction signatures * Update spec/signatures.typ Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * spec: signatures: fix LOAD signature * spec: signatures: make IS_BIT's cond a BaseField * spec: signatures: make ECALL's syscallnr a DWordWL * spec: signatures: preemptively introduce NEG signature (see #270) * spec: signatures: fix DWordDL typo Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Leverage `NEG` in `DVRM` (#287) * spec: DVRM: use NEG template for abs_r and abs_d This saves 4 columns. * Apply suggestions from @RobinJadoul Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Add initial tooling to check data formats, prepare for more elaborate type checking (#271) * spec: Add initial tooling to check data formats, prepare for more elaborate type checking * Initial type checking * ruff format * Update some more typing mismatches * Move to range-based type checks * Avoid casting to more limbs by leveraging scalar-array mult and literal casts * toml fixes to pass type checks * Type check virtual definitions properly now * ruff format * Make typst compile by turning big range values to string * Switch some isinstance checks around to make both mypy and ty work * Fix issues after rebasing on spec/main * Address review comments * Review comments * lit -> const * spec: Introduce array expressions (#295) Closes #135 * spec: separate ALU path for STORE to enable byte representation of rv2 to exist in arg2 (#308) * spec: separate ALU path for STORE to enable byte representation of rv2 to exist in arg2 * Apply review suggestion Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * Update spec/src/cpu.toml Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: `COMMIT` chip (#283) * spec: update footnote numbering * spec: COMMIT: specify commit chip * spec: COMMIT: fix typos * Move footnote numbering to a more general spot and allow easy future updates * Update common-formatting location * spec: COMMIT: update citation links * spec: COMMIT: deal with committing 0 bytes * spec: COMMIT: list future improvement * Fix typos Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: COMMIT: rearrange CNB multiplicity * spec: COMMIT: update padding strategy permitting ADD and SUB constraints of lower degree * spec: COMMIT: list two possible optimizations --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Typecheck signatures and make all chips pass (#312) * spec: Typecheck signatures and make all chips pass * Apply suggestion from @RobinJadoul * Apply suggestion from @RobinJadoul * Apply suggestion from @RobinJadoul * s/IS_HALFWORD/IS_HALF * Ensure constants being casted fit into the first limb * spec: Variable category for constants (#327) Closes #303 * spec: Fix interaction signatures for COMMIT (#328) * spec: Cleanup, uniformize chapters, make colors work better on web. (#336) * spec: Cleanup, uniformize chapters, make colors work better on web. * Fix double scroll bar * Improve decode table * Remove `style` state and make aside box grey. Having multiple web themes makes the style approach almost always wrong, since we cannot rely on the scheme being dark or light, in contrast to a regular PDF. * Apply suggestions from code review Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * Update spec/cpu.typ Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> --------- Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> * spec: LogUp: Vanilla protocol description (#243) --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: Add a version and title/front pages (#367) * spec: Losing some MEMW weight (#398) * spec: Some fixes and improvements for SHIFT (#400) * spec: Some fixes for SHIFT Closes: #389 * spec: Merge HWSL with HWSLC, to simplify SHIFT Closes: #119 * typo Co-authored-by: Cyprien de Saint Guilhem <c.desaintguilhem@gmail.com> --------- Co-authored-by: Cyprien de Saint Guilhem <c.desaintguilhem@gmail.com> * Fix type checking for MEMW_A (#423) * Spec/memw update (#434) * spec/memw: read/write from/to -> read from/write to * spec/memw: rename add_limb_overflow as carry * spec/memw: minor var desc updates * spec/memw: remove superfluous minus symbol * spec/memw: update description * spec/memw_a: minor optimization * Apply suggestions from code review Co-authored-by: Robin Jadoul <robin.jadoul@gmail.com> * spec/MEMW: fix interaction typing * spec/MEMW: drop superfluous notes * spec/MEMW: update alignment requirement * spec/MEMW: intentionally separate carry's prose and .toml descriptions --------- Co-authored-by: Robin Jadoul <robin.jadoul@gmail.com> * spec/MEMW(_A): minor update (#459) * spec/memw: read/write from/to -> read from/write to * spec/memw: rename add_limb_overflow as carry * spec/memw: minor var desc updates * spec/memw: remove superfluous minus symbol * spec/memw: update description * spec/memw_a: minor optimization * Apply suggestions from code review Co-authored-by: Robin Jadoul <robin.jadoul@gmail.com> * spec/MEMW: fix interaction typing * spec/MEMW: drop superfluous notes * spec/MEMW: update alignment requirement * spec/MEMW: intentionally separate carry's prose and .toml descriptions * spec/MEMW: fix multiplicities * spec/MEMW_A: padding * spec/MEMW: padding * spec/MEMW: bit check multiplicities * spec/MEMW: simplify padding --------- Co-authored-by: Robin Jadoul <robin.jadoul@gmail.com> * spec/MEMW_R: register access fast path (#457) --------- Co-authored-by: Robin Jadoul <robin.jadoul@gmail.com> * spec: Fix CPU sign bit constraints for `word_instr` (#435) * spec: interaction counter (#469) * spec: recursively compute chip interaction count * spec: print interaction count per chip * spec: cleanup * spec/interaction-counter: add multi-dimensional iter support * spec/interaction-counter: count SUB interactions * spec/interaction-counter: drop silent lookup fails * spec/interaction-counter: remove superfluous code * spec/interaction_count: merge getter and setter * spec/interaction_counter: clean up * spec: run spec-tooling in CI (#440) * spec: have tooling exit(1) on error * spec: run spec tooling in CI * spec/tooling: verbosely state when no issues are found * spec/CI: ignore benchmarks for spec stuff * spec: update TOC (#478) * spec: add chapters * spec: clean up toc * spec: separate ecalls * Fixes for shiroa * Reformat ebook.typ a bit --------- Co-authored-by: Robin Jadoul <robin.jadoul@3milabs.tech> * spec: cleanup before v0.2 (#479) * spec: add backticks to section titles * spec: replace "columns" headers with "variables" * spec: auto count nr_variables * spec: standardize "optimizations"-section headers * spec/config: add missing spaces * spec/chip: rename *_column_table as *_variable_table * spec: drop table captions * spec/v0.2: turn note into aside * spec: place correctness arguments in separate section * spec/tooling: add default case in `build_signature` * spec/tooling: fix hidden global variable * spec/tooling: fix silent side effect * spec/tooling: context manage file reads * spec: fix precedence Swap precedence of ADD and SUB and treat the first subexpression of a SUB differently --------- Co-authored-by: Erik Takke <erik.takke@3milabs.tech> Co-authored-by: Erik <159244975+erik-3milabs@users.noreply.github.com> Co-authored-by: Joaquin Carletti <56092489+ColoCarletti@users.noreply.github.com> Co-authored-by: Cyprien de Saint Guilhem <c.desaintguilhem@gmail.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Co-authored-by: Diego K <43053772+diegokingston@users.noreply.github.com>
1 parent 19133a6 commit 7b42e51

57 files changed

Lines changed: 9615 additions & 0 deletions

Some content is hidden

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

.github/workflows/pr_spec.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Spec tests
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
- 'spec/**'
7+
push:
8+
branches: ["**"]
9+
paths: ["spec/**"]
10+
11+
permissions:
12+
contents: read
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
spec_structure:
20+
name: Spec structure test
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v6
24+
- uses: actions/setup-python@v6
25+
- run: python3 spec/tooling/chip.py spec/src/config.toml spec/src/signatures.toml spec/src/*.toml

spec/.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
8+
[*.typ]
9+
indent_style = space
10+
indent_size = 2

spec/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/*
2+
ebook.pdf

spec/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# LambdaVM specification
2+
This repository contains specification for [`LambdaVM`](https://github.com/yetanotherco/lambda_vm).
3+
The specification is written in [`Typst`](https://typst.app/) and can be rendered by [`shiroa`](https://myriad-dreamin.github.io/shiroa/) as either a file (pdf) or a wiki (html).
4+
5+
## Installation & Development setup
6+
1. [Install `Typst`](https://github.com/typst/typst?tab=readme-ov-file#installation).
7+
2. [Install `shiroa`](https://myriad-dreamin.github.io/shiroa/guide/installation.html).
8+
3. Clone this repository.
9+
4. Open the repository in a terminal and execute `shiroa serve`.
10+
11+
At this point, the wiki version is hosted locally and is actively updated as you modify the specification files.

spec/about_ecalls.typ

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#import "/book.typ": book-page, aside
2+
#import "/src.typ": load_config, load_chip
3+
#import "/chip.typ": (
4+
render_chip_variable_table,
5+
total_nr_variables,
6+
total_nr_instantiated_columns,
7+
render_constraint_table,
8+
render_chip_assumptions,
9+
render_chip_padding_table,
10+
)
11+
12+
#let config = load_config()
13+
14+
#show: book-page("about_ecalls.typ")
15+
16+
ECALLs provide system-level functionalities to the guest program.
17+
18+
When `ECALL` is executed, it is assumed that:
19+
- register `A7` contains the system call number
20+
#footnote([The RISC-V system call ABI; libriscv.no, #link("https://web.archive.org/web/20260128152107/https://libriscv.no/docs/concepts/syscalls/#the-risc-v-system-call-abi")[[src]]]),
21+
- the arguments are located in registers `A0`-`A6`, and
22+
- the return value is written to `A0`,
23+
where `A0`-`A7` are symbolic names for the registers `x10`-`x17`
24+
#footnote([RISC-V - Register sets; en.wikipedia.org, #link("https://web.archive.org/web/20260209053447/https://en.wikipedia.org/wiki/RISC-V#Register_sets")[[src]]]).

spec/add.typ

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#import "/book.typ": book-page, et
2+
#import "/src.typ": load_config, load_chip
3+
#import "/chip.typ": render_chip_variable_table, render_chip_assumptions, render_constraint_table, set_nr_interactions, compute_nr_interactions,
4+
5+
#let config = load_config()
6+
#let chip = load_chip("src/add.toml", config)
7+
8+
#show: book-page(chip.name)
9+
10+
#set_nr_interactions(chip, name: "SUB")
11+
#let nr_interactions = compute_nr_interactions(chip)
12+
13+
#let add = raw(chip.name)
14+
#let sub = raw("SUB")
15+
16+
#add is a constraint template that is used to assert that $#`sum` equiv #`lhs` + #`rhs` (mod 2^64)$, under the condition that `cond` is non-zero.
17+
For ease of notation, we moreover introduce the #sub constraint template
18+
$
19+
#`SUB<diff; lhs, rhs>` := #`ADD<lhs; rhs, diff>`,
20+
$
21+
in both conditional and unconditional versions.
22+
It constrains that $#`diff` equiv #`lhs` - #`rhs` (mod 2^64)$ when the expression `cond` is non-zero.
23+
24+
= Variables
25+
This template introduces #nr_interactions interaction(s).
26+
#render_chip_variable_table(chip, config)
27+
28+
= Assumptions
29+
#render_chip_assumptions(chip, config)
30+
31+
= Constraints
32+
This template introduces the following constraints
33+
#render_constraint_table(chip, config)

spec/bitwise.typ

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#import "/book.typ": book-page, rj
2+
#import "/src.typ": load_config, load_chip
3+
#import "/chip.typ": (
4+
render_chip_assumptions,
5+
render_chip_variable_table,
6+
total_nr_variables,
7+
total_nr_instantiated_columns,
8+
render_constraint_table,
9+
)
10+
11+
#let config = load_config()
12+
#let chip = load_chip("src/bitwise.toml", config)
13+
14+
#let bitwise = raw(chip.name)
15+
16+
#show: book-page(chip.name)
17+
#let bitwise = raw(chip.name)
18+
19+
The #bitwise chips deal with precomputed lookup tables for bitwise boolean operations
20+
and convenience functionalities over small domains.
21+
22+
= Variables
23+
#let nr_variables = total_nr_variables(chip)
24+
#let nr_columns = total_nr_instantiated_columns(chip, config)
25+
#let nr_precomputed = ("input", "output").map(c => chip.variables.at(c)).flatten().len()
26+
27+
The #bitwise chip is comprised of #nr_variables variables that are expressed using #nr_columns columns.
28+
Of these, the _input_ and _output_ variables (#nr_precomputed in total) are precomputed.
29+
#render_chip_variable_table(chip, config)
30+
31+
*Note*: This table contains one row for every possible value of `(X, Y, Z)`.
32+
As such, it has length $2^8 dot 2^8 dot 2^4 = 2^(20)$.
33+
34+
= Lookup
35+
This chip adds the following interactions to the lookup:
36+
#render_constraint_table(chip, config)
37+
38+
= Notes/Optimizations
39+
The following ideas may prove to be optimizations for the #bitwise chip:
40+
+ Extend `IS_BYTE[X]` to `ARE_BYTES[X, Y]`, such that two bytes are range checked at once.
41+
When only a single check is required, one can still execute `IS_BYTE[X] := ARE_BYTES[X, 0]`.
42+
+ Drop `MSB8` column, and instead define the `MSB8` lookup as `MSB8<X> := MSB16[256X]`.
43+
Note: currently, `MSB8` also implicity range checks the input `X` (the lookup fails if `X` is not a `Byte`).
44+
This optimization should only be executed when all chips leveraging `MSB8` do _not_ need this implicit range check.
45+
+ Place the 16-bit (`AND`, `OR`, `XOR`, `MSB16`, etc.) and 20-bit (`HWSL`, `IS_B20`, `ZERO`) lookups in separate tables.

spec/book.typ

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#import "@preview/shiroa:0.3.1": *
2+
#import "/templates/page.typ": project
3+
4+
#show: book
5+
6+
#let meta = (
7+
title: "Lambda VM specification",
8+
authors: ("3MI Labs", "Aligned"),
9+
version: "0.2",
10+
summary: (
11+
("PROOF SYSTEM", (
12+
("logup.typ", [`LogUp` argument], <logup>),
13+
("memory.typ", [Memory argument], <memory>),
14+
)),
15+
("OVERVIEW", (
16+
("variables.typ", [Variables], <vars>),
17+
("signatures.typ", [Signatures], <signatures>),
18+
)),
19+
("TEMPLATES", (
20+
("is_bit.typ", [`IS_BIT` template], <isbit>),
21+
("sign.typ", [`SIGN` template], <sign>),
22+
("add.typ", [`ADD`/`SUB` template], <add>),
23+
("neg.typ", [`NEG` template], <neg>),
24+
)),
25+
("MEMORY", (
26+
("memw.typ", [`MEMW` chip], <memw>),
27+
)),
28+
("CPU", (
29+
("decode.typ", [`DECODE` table], <decode>),
30+
("cpu.typ", [`CPU` chip], <cpu>),
31+
)),
32+
("ALU", (
33+
("shift.typ", [`SHIFT` chip], <shift>),
34+
("branch.typ", [`BRANCH` chip], <branch>),
35+
("lt.typ", [`LT` chip], <lt>),
36+
("mul.typ", [`MUL` chip], <mul>),
37+
("dvrm.typ", [`DVRM` chip], <dvrm>),
38+
("load.typ", [`LOAD` chip], <load>),
39+
("bitwise.typ", [`BITWISE` chips], <bitwise>),
40+
)),
41+
("ECALLS", (
42+
("about_ecalls.typ", [About `ECALL`], <ecall>),
43+
("halt.typ", [`HALT` chip], <halt>),
44+
("commit.typ", [`COMMIT` chip], <commit>),
45+
))
46+
)
47+
)
48+
#let meta_sections = meta.summary.map(m => m.at(1)).sum()
49+
#book-meta(
50+
title: meta.title,
51+
authors: meta.authors,
52+
summary: prefix-chapter("front.typ", meta.title)
53+
+ meta.summary.map(
54+
((title, sections)) => {
55+
heading(depth: 1, title) + sections.map(((ch, title, _ref)) => chapter(ch, title)).join()
56+
}
57+
).join()
58+
)
59+
60+
#let common-formatting(body) = {
61+
set footnote(numbering: "[1]")
62+
show raw.where(block: true): it => block(it, inset: 1em, width: 100%, radius: 5pt)
63+
body
64+
}
65+
66+
67+
#let todo(background: white, foreground: black, name: none, body) = block(fill: background, outset: 0.4em, radius: 20%, stroke: black)[
68+
#set text(fill: foreground)
69+
*TODO #if name != none { [(#name)] }*: #body
70+
]
71+
#let rj = todo.with(background: teal, name: "Robin")
72+
#let et = todo.with(background: rgb("d4aa3a"), name: "Erik")
73+
#let cdsg = todo.with(background: olive, name: "Cyprien")
74+
75+
#let aside(title, body) = context figure(
76+
block(inset: (left: 1em, right: 1em, bottom: 1em), stroke: luma(50%), breakable: false)[
77+
#block(inset: (left: 1em, right: 1em, top: .75em, bottom: .75em),
78+
width: 100% + 2em,
79+
fill: rgb("55aaff"),
80+
stroke: luma(50%),
81+
align(center, strong(text(fill: black, title))))
82+
#align(left, body)
83+
])
84+
85+
86+
#let is-shiroa = "x-target" in sys.inputs
87+
88+
// Strip styling to keep only "pure" content.
89+
// This is useful to avoid errors on the `set document(...)` in `project`
90+
// when invisibly including other chapters to resolve xrefs.
91+
#let strip-all(content) = {
92+
if repr(content.func()) == "sequence" {
93+
for c in content.children {
94+
strip-all(c)
95+
}
96+
} else if repr(content.func()) == "styled" {
97+
strip-all(content.child)
98+
} else {
99+
content
100+
}
101+
}
102+
103+
#let _toplevel = state("_toplevel", none)
104+
#let _xref-included = state("_xref-included", (:))
105+
106+
// Invisibly include another chapter, so that its labels can be resolved
107+
#let xref-include(f) = {
108+
context {
109+
place(hide(box(width: auto, height: 0%, strip-all(include "/" + f))))
110+
}
111+
}
112+
113+
// Generate a cross-link for references to other chapters.
114+
// Leaves the ref untouched if it can't be resolved or points to the current chapter.
115+
#let xref(rf) = {
116+
assert(is-shiroa, message: "xref should only be used when compiling for shiroa")
117+
let lbl = rf.target
118+
let found = meta_sections.find(((_, _, tag)) => str(lbl).starts-with(str(tag)))
119+
context if found != none and found.at(0) != _toplevel.final() {
120+
let (ch, title, ref) = found
121+
if ref == lbl {
122+
cross-link("/" + ch, [Chapter #(meta_sections.position(x => x == found) + 1)])
123+
} else {
124+
// Because shiroa does weird url escaping
125+
let shiroa-label = label(str(lbl).replace(":", "%3A"))
126+
context _xref-included.update(x => x + ((ch): true))
127+
// The ideal would be to use `rf` directly as content argument to `cross-link`,
128+
// as that would inherit any/all formatting of the ref we want or need.
129+
// Unfortunately the ref link seems to take precedence over the cross-link hyperlink
130+
// when clicking.
131+
// There may still be some way around it by messing with some html output
132+
let link-content = context {
133+
let fig = query(lbl).first()
134+
let counter = if fig.has("counter") {
135+
fig.counter
136+
} else {
137+
counter(fig.func())
138+
}
139+
140+
let supplement = if rf.supplement == auto {
141+
fig.fields().at("supplement", default: none)
142+
} else {
143+
rf.supplement
144+
}
145+
[#supplement#numbering(fig.numbering, ..counter.at(lbl))]
146+
}
147+
cross-link("/" + ch, reference: shiroa-label, link-content)
148+
}
149+
} else {
150+
rf
151+
}
152+
}
153+
154+
#let book-page(file, ..args) = {
155+
if not file.ends-with(".typ") {
156+
file = lower(file) + ".typ"
157+
}
158+
159+
assert(meta_sections.find(s => s.at(0) == file) != none, message: "Couldn't resolve typst source file " + file)
160+
161+
if is-shiroa {
162+
(body) => {
163+
show: common-formatting
164+
context _toplevel.update(s => {
165+
if s == none {
166+
file
167+
} else {
168+
s
169+
}
170+
})
171+
let cond() = _toplevel.final() == file
172+
project.with(..args, title: context meta_sections.find(x => x.at(0) == _toplevel.final()).at(1), cond: cond)([
173+
#show ref: it => context if _toplevel.final() == file {
174+
xref(it)
175+
}
176+
#context _xref-included.final().pairs().map(((key, value)) => context if value and cond() {
177+
xref-include(key)
178+
}).join()
179+
#body
180+
])
181+
}
182+
} else {
183+
body => body
184+
}
185+
}

spec/branch.typ

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#import "/book.typ": book-page, rj
2+
#import "/src.typ": load_config, load_chip
3+
#import "/chip.typ": (
4+
render_chip_assumptions,
5+
render_chip_variable_table,
6+
compute_nr_interactions,
7+
total_nr_variables,
8+
total_nr_instantiated_columns,
9+
render_constraint_table,
10+
render_chip_padding_table,
11+
)
12+
13+
#let config = load_config()
14+
#let chip = load_chip("src/branch.toml", config)
15+
16+
#show: book-page(chip.name)
17+
#let branch = raw(chip.name)
18+
19+
The #branch chip computes the target address of a branching instruction.
20+
21+
= Variables
22+
#let nr_variables = total_nr_variables(chip)
23+
#let nr_columns = total_nr_instantiated_columns(chip, config)
24+
#let nr_interactions = compute_nr_interactions(chip)
25+
26+
The #branch chip is comprised of #nr_variables variables that are expressed using #nr_columns columns and leverages #nr_interactions interaction(s):
27+
#render_chip_variable_table(chip, config)
28+
29+
= Assumptions
30+
31+
#render_chip_assumptions(chip, config)
32+
33+
= Constraints
34+
35+
We constrain `next_pc` to be $#`base_address` + #`offset`$,
36+
where `base_address` equals `pc` when $#`JALR` = 0$ and `register` otherwise.
37+
38+
The range checks on `unmasked_low_byte` and `next_pc_low[0]` are performed implicitly by the `AND_BYTE` lookup.
39+
#render_constraint_table(chip, config, groups: "all")
40+
41+
This chip contributes the following to the lookup argument.
42+
#render_constraint_table(chip, config, groups: "output")
43+
44+
= Padding
45+
46+
The table can be padded to the next power of two with the following value assignments:
47+
48+
#render_chip_padding_table(chip, config)

0 commit comments

Comments
 (0)