-
Notifications
You must be signed in to change notification settings - Fork 0
spec: ADD template
#97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e3f0920
spec: ADD draft
erik-3milabs e6a2545
spec: ADD: fix `carry` size
erik-3milabs af5c41d
spec: ADD: clarify sum is mod 2^64
erik-3milabs 00ba51e
Merge branch 'spec/main' into spec/ADD
erik-3milabs 424d4db
spec: introduce `SUB` template notation.
erik-3milabs ddb9183
Fix assumption indices
erik-3milabs 9de2bb2
Fix typos
erik-3milabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #import "/book.typ": book-page, et | ||
| #import "/src.typ": load_config, load_chip | ||
| #import "/chip.typ": render_chip_column_table, render_chip_assumptions, render_constraint_table | ||
|
|
||
| #show: book-page.with(title: "ADD/SUB") | ||
|
|
||
| #let config = load_config() | ||
| #let chip = load_chip("src/add.toml", config) | ||
|
|
||
| #let add = raw(chip.name) | ||
|
|
||
| #let highlighted_code(code) = { | ||
| box( | ||
| inset: (left: 4pt, right: 4pt), | ||
| outset: (top: 4pt, bottom: 4pt), | ||
| radius: 2pt, | ||
| fill: luma(230), | ||
| raw(code)) | ||
| } | ||
|
|
||
| = #add template | ||
| #add is a constraint template that is used to assert that $#`sum` = #`lhs` + #`rhs` mod 2^64$, under the condition that `cond` is non-zero. | ||
|
|
||
| == Notation | ||
| The #add constraint template has the following interface: | ||
| #block(radius: 5pt, width: 100%, inset: 1.5em, fill: luma(230), raw("cond => ADD<sum; lhs, rhs>")) | ||
| where `cond` is any value described by an expression _of degree at most $1$_. | ||
| #highlighted_code("ADD<sum; lhs, rhs>") can be used to denote the _unconditional_ application of the #add template to `lhs`, `rhs`, and `sum`. | ||
|
|
||
| #let sub = raw("SUB") | ||
| === #sub | ||
| For ease of notation, we moreover introduce the #sub constraint template. | ||
| Its interface | ||
| #block(radius: 5pt, width: 100%, inset: 1.5em, fill: luma(230), raw("cond => SUB<diff; lhs, rhs>")) | ||
| maps onto the #add template as | ||
| #block(radius: 5pt, width: 100%, inset: 1.5em, fill: luma(230), raw("cond => ADD<lhs; rhs, diff>")) | ||
| It constrains that $#`diff` = #`lhs` - #`rhs` mod 2^64$ when the expression `cond` is non-zero. | ||
| As with #add, #highlighted_code("SUB<diff; lhs, rhs>") can be used to denote the _unconditional_ application of the template. | ||
|
|
||
| == Variables | ||
| #render_chip_column_table(chip, config) | ||
|
|
||
| == Assumptions | ||
| #render_chip_assumptions(chip, config) | ||
|
|
||
| == Constraints | ||
| This template introduces the following constraints | ||
| #render_constraint_table(chip, config) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| name = "ADD" | ||
|
|
||
| # Variables | ||
|
|
||
| [[variables.condition]] | ||
| name = "cond" | ||
| type = "BaseField" | ||
| desc = "Whether the relation should be enforced ($eq.not 0$) or not ($0$)." | ||
|
|
||
| [[variables.input]] | ||
| name = "lhs" | ||
| type = "DWordWL" | ||
| desc = "left-hand operator" | ||
|
|
||
| [[variables.input]] | ||
| name = "rhs" | ||
| type = "DWordWL" | ||
| desc = "right-hand operator" | ||
|
|
||
| [[variables.output]] | ||
| name = "sum" | ||
| type = "DWordWL" | ||
| desc = "$#`lhs` + #`rhs`$" | ||
|
|
||
| [[variables.virtual]] | ||
| name = "carry" | ||
| desc = "Carry values used to constrain the addition" | ||
| type = ["Bit", 2] | ||
| def = {idx="i", polys=[ | ||
| {range=0, poly=["*", ["^", 2, -32], ["-", ["+", ["idx", "lhs", 0], ["idx", "rhs", 0]], ["idx", "sum", 0]]]}, | ||
| {range=1, poly=["*", ["^", 2, -32], ["-", ["+", ["idx", "lhs", 1], ["idx", "rhs", 1], ["idx", "carry", 0]], ["idx", "sum", 1]]]}, | ||
| ]} | ||
|
|
||
|
|
||
| # Assumptions | ||
|
|
||
| [[assumptions]] | ||
| desc = "`IS_WORD[lhs[i]]`" | ||
| range = ["i", 0, 1] | ||
| ref = "add:a:lhs" | ||
|
|
||
| [[assumptions]] | ||
| desc = "`IS_WORD[rhs[i]]`" | ||
| range = ["i", 0, 1] | ||
| ref = "add:a:rhs" | ||
|
|
||
| [[assumptions]] | ||
| desc = "`IS_WORD[sum[i]]`" | ||
| range = ["i", 0, 1] | ||
| ref = "add:a:sum" | ||
|
|
||
| # Constraints | ||
|
|
||
| [[constraint_groups]] | ||
| name = "all" | ||
|
|
||
| [[constraints.all]] | ||
| kind = "template" | ||
| tag = "IS_BIT" | ||
| input = [["idx", "carry", "i"]] | ||
| range = ["i", 0, 1] | ||
| cond = "cond" | ||
| ref = "add:c:carry" | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.