-
Notifications
You must be signed in to change notification settings - Fork 0
spec: NEG template
#270
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
spec: NEG template
#270
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
801f5ee
spec: tweak code-rendering "not"
erik-3milabs 70a8a2c
spec: introduce NEG template
erik-3milabs 8bd3260
Update spec/book.typ
erik-3milabs f53f6c9
spec: update NEG
erik-3milabs ac9d65d
spec: NEG: refactor
erik-3milabs b072055
spec: NEG: fix range-assumption on x
erik-3milabs a545557
spec: NEG: update cond
erik-3milabs e4eb03d
spec: tweak math-rendering "not"
erik-3milabs dfa328c
spec: NEG: add non-zero x case distinction
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
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,78 @@ | ||
| #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 | ||
|
|
||
| #let config = load_config() | ||
| #let chip = load_chip("src/neg.toml", config) | ||
| #show: book-page(chip.name) | ||
|
|
||
| #let neg = 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)) | ||
| } | ||
|
|
||
| #neg is a constraint template that is used to assert that $#`neg` = -#`x`$, under the condition that `cond` is non-zero. | ||
|
|
||
| == Notation | ||
| The #neg constraint template has the following interface: | ||
| #block(radius: 5pt, width: 100%, inset: 1.5em, fill: luma(230), raw("cond => NEG<neg; x>")) | ||
| where `cond` is a bit value (i.e., lies in ${0, 1}$) described by an expression _of degree at most $1$_. | ||
| #highlighted_code("NEG<neg; x>") can be used to denote the _unconditional_ application of the #neg template to `x` and `neg` (which is equivalent to $#`cond` = 1$). | ||
|
|
||
| == Variables | ||
| #render_chip_column_table(chip, config) | ||
|
|
||
| == Assumptions | ||
| #render_chip_assumptions(chip, config) | ||
|
|
||
| == Constraints | ||
| We constrain this equality using two constraints: | ||
| #render_constraint_table(chip, config) | ||
| The constraints force the `carry` values to be fixed. | ||
| Writing `carry`'s definition, we then find that | ||
| $ | ||
| #`neg`_0 &= 2^32 dot #`carry`_0 - (#`x as DWordWL`)_0 | ||
| = cases( | ||
| 2^32 - (#`x as DWordWL`)_0 & "if" (#`x as DWordWL`)_0 != 0, | ||
| 0 & "if" (#`x as DWordWL`)_0 = 0 | ||
| ),\ | ||
| #`neg`_1 &= 2^32 dot #`carry`_1 - (#`x as DWordWL`)_1 - #`carry`_0 = cases( | ||
| 2^32 - (#`x as DWordWL`)_1 - 1 & "if" #`x` != 0, | ||
| 0 & "if" #`x` = 0 | ||
| ) | ||
| $ | ||
| Clearly, $#`neg` = 0$ when $#`x` = 0$ (and `cond` is set). | ||
| For non-zero `x`, we distinguish two cases. | ||
| When $(#`x as DWordWL`)_0 = 0$, | ||
| $ | ||
| #`neg` | ||
| &= 2^32 dot #`neg`_1 + #`neg`_0\ | ||
| &= 2^32 dot (2^32 - (#`x as DWordWL`)_1) + 0\ | ||
| &= 2^32 dot (2^32 - (#`x as DWordWL`)_1) + (#`x as DWordWL`)_0\ | ||
| &= 2^64 - (2^32 dot (#`x as DWordWL`)_1 + (#`x as DWordWL`)_0)\ | ||
| &= 2^64 - #`x`\ | ||
| &equiv -x mod 2^64, | ||
| $ | ||
| while when $(#`x as DWordWL`)_0 != 0$, | ||
| $ | ||
| #`neg` | ||
| &= 2^32 dot #`neg`_1 + #`neg`_0\ | ||
| &= 2^32 dot (2^32 - (#`x as DWordWL`)_1 - 1) + (2^32 - (#`x as DWordWL`)_0) \ | ||
| &= 2^64 - 2^32 dot (#`x as DWordWL`)_1 - 2^32 + 2^32 - (#`x as DWordWL`)_0 \ | ||
| &= 2^64 - ((#`x as DWordWL`)_0 + 2^32 dot (#`x as DWordWL`)_1) \ | ||
| &= 2^64 - #`x`\ | ||
| &equiv -x mod 2^64 | ||
| $ | ||
| when `cond` is set. | ||
| When `cond` is not set, the two lookups are not executed, allowing `neg` to take any value in either case. | ||
|
|
||
| == Note | ||
| It is worth noting that this construction does _not_ require the limbs of `neg` to be range checked, | ||
| thus allowing it be represented by the unrangecheckable `DWordWL` rather than a `DWordHL`. | ||
| The input value `x` is still assumed to be range-checked, however. |
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,53 @@ | ||
| name = "NEG" | ||
|
|
||
| [[variables.condition]] | ||
| name = "cond" | ||
| type = "Bit" | ||
| desc = "condition on whether to negate x" | ||
|
|
||
| [[variables.input]] | ||
| name = "x" | ||
| type = "DWordHL" | ||
| desc = "value to compute negation of" | ||
|
|
||
| [[variables.output]] | ||
| name = "neg" | ||
| type = "DWordWL" | ||
| desc = "negation of `x` if $#`cond` != 0$; unconstrained otherwise." | ||
|
|
||
| [[variables.virtual]] | ||
| name = "carry" | ||
| type = ["Bit", 2] | ||
| desc = "carries of the addition $#`neg` + #`x`$." | ||
| def = {idx="i", polys=[ | ||
| {iter=0, poly=["*", ["^", 2, -32], ["+", ["idx", ["cast", "x", "DWordWL"], 0], ["idx", "neg", 0]]]}, | ||
| {iter=1, poly=["*", ["^", 2, -32], ["+", ["idx", ["cast", "x", "DWordWL"], 1], ["idx", "neg", 1], ["idx", "carry", 0]]]} | ||
| ]} | ||
|
|
||
|
|
||
| [[assumptions]] | ||
| desc = "`IS_HALF[x[i]]`" | ||
| iter = ["i", 0, 3] | ||
|
|
||
| [[assumptions]] | ||
| desc = "`IS_BIT<cond>`" | ||
|
RobinJadoul marked this conversation as resolved.
|
||
|
|
||
|
|
||
| [[constraint_groups]] | ||
| name = "all" | ||
|
|
||
| [[constraints.all]] | ||
| kind = "interaction" | ||
| tag = "ZERO" | ||
| input = [["+", ["idx", "x", 0], ["idx", "x", 1]]] | ||
|
erik-3milabs marked this conversation as resolved.
erik-3milabs marked this conversation as resolved.
|
||
| output = ["not", ["idx", "carry", 0]] | ||
| multiplicity = "cond" | ||
|
erik-3milabs marked this conversation as resolved.
|
||
| ref = "neg:c:carry_0" | ||
|
|
||
| [[constraints.all]] | ||
| kind = "interaction" | ||
| tag = "ZERO" | ||
| input = [["+", ["idx", "x", 0], ["idx", "x", 1], ["idx", "x", 2], ["idx", "x", 3]]] | ||
| output = ["not", ["idx", "carry", 1]] | ||
| multiplicity = "cond" | ||
| ref = "neg:c:carry_1" | ||
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.