-
Notifications
You must be signed in to change notification settings - Fork 599
feat(avm)!: ecc add error handling #15781
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
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -39,9 +39,11 @@ include "execution.pil"; | |
| * - guaranteed by this gadget to be U1. | ||
| * | ||
| * ERROR HANDLING: | ||
| * A single error needs to be handled as part of this trace | ||
| * Two errors needs to be handled as part of this trace, | ||
| * (1) DST_OUT_OF_BOUNDS_ACCESS: If the writes would access a memory address outside | ||
| * of the max AVM memory address (AVM_HIGHEST_MEM_ADDRESS). | ||
| * (2) POINT_NOT_ON_CURVE: If either of the inputs (embedded curve points) do not | ||
| * satisfy the grumpkin curve equation (SW form: Y^2 = X^3 − 17) | ||
| * | ||
| * N.B This subtrace writes the values within a single row (i.e. 3 output columns) | ||
| * | ||
|
|
@@ -86,48 +88,45 @@ namespace ecc_add_mem; | |
| gt.sel { gt.input_a, gt.input_b, gt.res }; | ||
|
|
||
| //////////////////////////////////////////////// | ||
| // Dispatch from execution trace to ECC Add | ||
| // Error Handling - Check Points are on curve | ||
| //////////////////////////////////////////////// | ||
| #[DISPATCH_EXEC_ECC_ADD] | ||
| execution.sel_execute_ecc_add { | ||
| precomputed.clk, | ||
| execution.context_id, | ||
| // Point P | ||
| execution.register[0], | ||
| execution.register[1], | ||
| execution.register[2], | ||
| // Point Q | ||
| execution.register[3], | ||
| execution.register[4], | ||
| execution.register[5], | ||
| // Dst address | ||
| execution.rop[6], | ||
| // Error | ||
| execution.sel_opcode_error | ||
| } is | ||
| sel { | ||
| execution_clk, | ||
| space_id, | ||
| // Point P | ||
| p_x, | ||
| p_y, | ||
| p_is_inf, | ||
| // Point Q | ||
| q_x, | ||
| q_y, | ||
| q_is_inf, | ||
| // Dst address | ||
| dst_addr[0], | ||
| // Error | ||
| sel_dst_out_of_range_err | ||
| }; | ||
| pol commit sel_p_not_on_curve_err; | ||
| sel_p_not_on_curve_err * (1 - sel_p_not_on_curve_err) = 0; | ||
|
|
||
| pol commit sel_q_not_on_curve_err; | ||
| sel_q_not_on_curve_err * (1 - sel_q_not_on_curve_err) = 0; | ||
|
|
||
| // Y^2 = X^3 − 17, re-formulate to Y^2 - (X^3 - 17) = 0 | ||
| pol commit p_is_on_curve_eqn; // Needs to be committed to reduce relation degrees | ||
| pol P_X3 = p_x * p_x * p_x; | ||
| pol P_Y2 = p_y * p_y; | ||
| #[P_CURVE_EQN] | ||
| p_is_on_curve_eqn = sel * (P_Y2 - (P_X3 - 17)) * (1 - p_is_inf); // Infinity considered as on curve | ||
| // If p_is_on_curve_eqn != 0, sel_p_not_on_curve_err = 1 | ||
| pol commit p_is_on_curve_eqn_inv; | ||
| #[P_ON_CURVE_CHECK] | ||
| sel * (p_is_on_curve_eqn * ((1 - sel_p_not_on_curve_err) * (1 - p_is_on_curve_eqn_inv) + p_is_on_curve_eqn_inv) - sel_p_not_on_curve_err) = 0; | ||
|
|
||
| // Y^2 = X^3 − 17, re-formulate to Y^2 - (X^3 - 17) = 0 | ||
| pol commit q_is_on_curve_eqn; // Needs to be committed to reduce relation degrees | ||
| pol Q_X3 = q_x * q_x * q_x; | ||
| pol Q_Y2 = q_y * q_y; | ||
| #[Q_CURVE_EQN] | ||
| q_is_on_curve_eqn = sel * (Q_Y2 - (Q_X3 - 17)) * (1 - q_is_inf); // Infinity considered as on curve | ||
|
Contributor
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. Same here |
||
| // If q_is_on_curve_eqn != 0, sel_q_not_on_curve_err = 1 | ||
| pol commit q_is_on_curve_eqn_inv; | ||
| #[Q_ON_CURVE_CHECK] | ||
| sel * (q_is_on_curve_eqn * ((1 - sel_q_not_on_curve_err) * (1 - q_is_on_curve_eqn_inv) + q_is_on_curve_eqn_inv) - sel_q_not_on_curve_err) = 0; | ||
|
|
||
| pol commit err; // Consolidate errors | ||
| err = 1 - (1 - sel_dst_out_of_range_err) * (1 - sel_p_not_on_curve_err) * (1 - sel_q_not_on_curve_err); | ||
|
|
||
| /////////////////////////////////////////////////////////////////////// | ||
| // Dispatch inputs to ecc add and retrieve outputs | ||
| /////////////////////////////////////////////////////////////////////// | ||
| pol commit res_x, res_y, res_is_inf; | ||
| pol commit sel_should_exec; | ||
| sel_should_exec = sel * (1 - sel_dst_out_of_range_err); | ||
| sel_should_exec = sel * (1 - err); | ||
|
|
||
| #[INPUT_OUTPUT_ECC_ADD] | ||
| sel_should_exec { | ||
|
|
@@ -180,3 +179,41 @@ namespace ecc_add_mem; | |
| memory.value, memory.tag, | ||
| memory.space_id, memory.rw | ||
| }; | ||
|
|
||
| //////////////////////////////////////////////// | ||
| // Dispatch from execution trace to ECC Add | ||
| //////////////////////////////////////////////// | ||
| #[DISPATCH_EXEC_ECC_ADD] | ||
| execution.sel_execute_ecc_add { | ||
| precomputed.clk, | ||
| execution.context_id, | ||
| // Point P | ||
| execution.register[0], | ||
| execution.register[1], | ||
| execution.register[2], | ||
| // Point Q | ||
| execution.register[3], | ||
| execution.register[4], | ||
| execution.register[5], | ||
| // Dst address | ||
| execution.rop[6], | ||
| // Error | ||
| execution.sel_opcode_error | ||
| } is | ||
| sel { | ||
| execution_clk, | ||
| space_id, | ||
| // Point P | ||
| p_x, | ||
| p_y, | ||
| p_is_inf, | ||
| // Point Q | ||
| q_x, | ||
| q_y, | ||
| q_is_inf, | ||
| // Dst address | ||
| dst_addr[0], | ||
| // Error | ||
| err | ||
| }; | ||
|
|
||
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.
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.
"Infinity considered as on curve", but if
p_is_inf = 1, we getp_is_on_curve_eqn = 0There 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.
yep that is intended. In short, the curve equation is
y^2 = x^3 - 17, so it gets re-arranged top_is_on_curve_eqn = y^2 - (x^3 - 17)and to be on the curve we wantp_is_on_curve_eqn = 0.Now a quirk is that the point at infinity actually doesnt satisfy the equation. So im multiplifying it by zero essentially to ensure we get
p_is_on_curve_eqn = 0.@MirandaWood is actually mathematically qualified so please tell me if this is correct.
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.
Wait so
p_is_on_curve== "p is NOT on the curve"?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.
Sounds correct to me!
Looks like we want
p_is_on_curve_eqn = 0iffPis on the curve, which is true wheny^2 - (x^3 - 17) = 0or we have the point at infinity. So LGTM 🎉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.
@dbanks12 it's basically a commited column to represent an evaluation of the "point is on curve" equation. It can have values between [0, p - 1] (basically any field value). If this value !=0 we raise the "not_on_curve_err"
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.
I see.... A bit confusing because the variable name sounds like a boolean, but I guess the
_eqnsuffix is meant to signal otherwise.