-
Notifications
You must be signed in to change notification settings - Fork 3
Reflect reviews #35
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
Reflect reviews #35
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04ea7dd
Reflect reviews from Mathias
mellowcroc d530b4b
fix: minor grammar errors
mellowcroc d356f11
Add link to reference implementation for public input AIR
mellowcroc 65682b4
fix: apply reviews
mellowcroc 4052c29
chore: update stwo and rust versions
mellowcroc 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 |
|---|---|---|
|
|
@@ -4,13 +4,13 @@ So now that we know how to create a self-contained AIR, the inevitable question | |
|
|
||
| Fortunately, Stwo provides an abstraction called **components** that allows us to create independent AIRs and compose them together. In other proving frontends, this is also commonly referred to as a _chip_, but the idea is the same. | ||
|
|
||
| One of the most common use cases of components is to separate frequently used functions (e.g. a hash function) from the main component into a separate component and reuse it, avoiding trace column bloat. Even if the function is not frequently used, it could be useful to separate it into a component to avoid the degree of the constraints becoming too high. This second point is possible because when we create a new component and connect it to the old component, we do it by using lookups, which means that the constraints of the new component are not added to the degree of the old component. | ||
| One of the most common use cases of components is to separate frequently used functions (e.g. a hash function) from the main component into a separate component and reuse it, avoiding trace column bloat. Even if the function is not frequently used, it can be useful to separate it into a component to avoid the degree of the constraints becoming too high. This second point is possible because when we create a new component and connect it to the old component, we do it by using lookups, which means that the constraints of the new component are not added to the degree of the old component. | ||
|
|
||
| ## Hash Function Example | ||
|
|
||
| To illustrate how to use components, we will create two components where the main component calls a hash function component. For simplicity, instead of an actual hash function, the second component will compute $x^5 + 1$ from an input $x$. This component will have in total three columns: [input, intermediate, output], which will correspond to the values $[x, x^3, x^5 + 1]$. Our main component, on the other hand, will have two columns, [input, output], which corresponds to the values $[x, x^5 + 1]$. | ||
| To illustrate how to use components, we will create two components where the main component calls a hash function component. For simplicity, instead of an actual hash function, the second component will compute $x^5 + 1$ from an input $x$. This component will have, in total, three columns: [input, intermediate, output], which will correspond to the values $[x, x^3, x^5 + 1]$. Our main component, on the other hand, will have two columns, [input, output], which corresponds to the values $[x, x^5 + 1]$. | ||
|
|
||
| We'll now refer to the main component as the **scheduling component** and the hash function component the **computing component**, as the main component is essentially _scheduling_ the hash function component to run its function with a given input and the hash function component _computes_ on the provided input. As can be seen in [Figure 1](#fig-component-example), the input and output of each component are connected by lookups. | ||
| We'll refer to the main component as the **scheduling component** and the hash function component as the **computing component**, since the main component is essentially _scheduling_ the hash function component to run its function with a given input and the hash function component _computes_ on the provided input. As can be seen in [Figure 1](#fig-component-example), the input and output of each component are connected by lookups. | ||
|
|
||
| <figure id="fig-component-example" style="text-align: center;"> | ||
| <img src="./component-example.png" width="100%" /> | ||
|
|
@@ -24,29 +24,45 @@ We'll now refer to the main component as the **scheduling component** and the ha | |
| <figcaption><center><span style="font-size: 0.9em">Figure 2: Traces of each component</span></center></figcaption> | ||
| </figure> | ||
|
|
||
| When we implement this in Stwo, the traces of each component will look like [Figure 2](#fig-component-trace) above. Each component has its own original and LogUp traces, and each the inputs and outputs of each component are connected by lookups. Since the scheduling component sets the LogUp value as a positive multiplicity and the computing component sets the same value as a negative multiplicity, the verifier can simply check that the sum of the two LogUp columns is zero. Note that we combine the input and output randomly (as $\dfrac{1}{Z - x \cdot \alpha^0 - (x^5+1) \cdot \alpha^1}$) to form a single lookup. This is because we want to ensure that each input is paired with the correct output. If we add the input and output as separate lookups (as $\dfrac{1}{Z - x} + \dfrac{1}{Z - (x^5+1)}$), a malicious prover can switch the output with a different row and still come up with a valid proof. For example, the following traces would be valid: | ||
| When we implement this in Stwo, the traces of each component will look like [Figure 2](#fig-component-trace) above. Each component has its own original and LogUp traces, and the inputs and outputs of each component are connected by lookups. Since the scheduling component sets the LogUp value as a positive multiplicity and the computing component sets the same value as a negative multiplicity, the verifier can simply check that the sum of the two LogUp columns is zero. Note that we combine the input and output randomly as | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nit, is the combination of the (input,output) tuple not just |
||
|
|
||
| Scheduling component | ||
| ------------ | ||
| | x | H(y) | | ||
| | y | H(x) | | ||
| ------------ | ||
| $$ | ||
| \frac{1}{Z - x \cdot \alpha^0 - (x^5+1) \cdot \alpha^1} | ||
| $$ | ||
|
|
||
| Computing component | ||
| ---------------------- | ||
| | x | x^5 + 1 | H(x) | | ||
| | y | y^5 + 1 | H(y) | | ||
| ---------------------- | ||
| to form a single lookup. This is because we want to ensure that each input is paired with the correct output. If we add the input and output as separate lookups as | ||
|
|
||
| ## Code | ||
| $$ | ||
| \frac{1}{Z - x} + \frac{1}{Z - (x^5+1)} | ||
| $$ | ||
|
|
||
| A malicious prover can switch the output with a different row and still come up with a valid proof. For example, the following scheduling component | ||
|
|
||
| | Input | Output | | ||
| | ----- | ------- | | ||
| | x | y^5 + 1 | | ||
| | y | x^5 + 1 | | ||
|
|
||
| And the following computing component | ||
|
|
||
| | Input | Intermediate | Output | | ||
| | ----- | ------------ | ------- | | ||
| | x | x^3 + 1 | x^5 + 1 | | ||
| | y | y^3 + 1 | y^5 + 1 | | ||
|
|
||
| would be valid. | ||
|
|
||
| ## Implementation | ||
|
|
||
| Let's move on to the implementation. | ||
|
|
||
| ```rust,ignore | ||
| {{#include ../../../stwo-examples/examples/components.rs:main_start}} | ||
| {{#include ../../../stwo-examples/examples/components.rs:main_prove}} | ||
| {{#include ../../../stwo-examples/examples/components.rs:main_end}} | ||
| ``` | ||
|
|
||
| The code above for proving the components should look pretty familiar by now. Since we need to do everything twice the amount of times, we create structs like `ComponentsStatement0`, `ComponentsStatement1`, `Components` and `ComponentsProof`, but the main logic is the same. | ||
| The code above for proving the components should look pretty familiar by now. Since we need to do everything twice as many times, we create structs like `ComponentsStatement0`, `ComponentsStatement1`, `Components`, and `ComponentsProof`, but the main logic is the same. | ||
|
|
||
| Let's take a closer look at how the LogUp columns are generated. | ||
|
|
||
|
|
@@ -60,7 +76,7 @@ Let's take a closer look at how the LogUp columns are generated. | |
| {{#include ../../../stwo-examples/examples/components.rs:gen_computing_logup_trace_end}} | ||
| ``` | ||
|
|
||
| As you can see, the LogUp values of the input and output columns of both the scheduling and computing components are batched together, but in the scheduling component, the output LogUp value is subtracted from the input LogUp value, while in the computing component, the input LogUp value is subtracted from the output LogUp value. This means that when the LogUp sums from both components are added together, they should cancel out and equal zero. | ||
| As you can see, the LogUp values of the input and output columns of both the scheduling and computing components are batched together, but in the scheduling component, the output LogUp value is subtracted from the input LogUp value, while in the computing component, the input LogUp value is subtracted from the output LogUp value. This means that when the LogUp sums from both components are added together, they should cancel out to zero. | ||
|
|
||
| Next, let's check how the constraints are created. | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice