-
Notifications
You must be signed in to change notification settings - Fork 611
feat: ecadd op code #6906
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
feat: ecadd op code #6906
Changes from all commits
Commits
Show all changes
4 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
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
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 |
|---|---|---|
|
|
@@ -3520,6 +3520,113 @@ void AvmTraceBuilder::op_pedersen_hash(uint8_t indirect, | |
| write_slice_to_memory( | ||
| call_ptr, clk, output_offset, AvmMemoryTag::FF, AvmMemoryTag::FF, FF(internal_return_ptr), { output }); | ||
| } | ||
|
|
||
| void AvmTraceBuilder::op_ec_add(uint8_t indirect, | ||
| uint32_t lhs_x_offset, | ||
| uint32_t lhs_y_offset, | ||
| uint32_t lhs_is_inf_offset, | ||
| uint32_t rhs_x_offset, | ||
| uint32_t rhs_y_offset, | ||
| uint32_t rhs_is_inf_offset, | ||
| uint32_t output_offset) | ||
| { | ||
| auto clk = static_cast<uint32_t>(main_trace.size()) + 1; | ||
| // Load lhs point | ||
| auto lhs_x_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::IA, lhs_x_offset, AvmMemoryTag::FF, AvmMemoryTag::U0); | ||
| auto lhs_y_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::IB, lhs_y_offset, AvmMemoryTag::FF, AvmMemoryTag::U0); | ||
| // Load rhs point | ||
| auto rhs_x_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::IC, rhs_x_offset, AvmMemoryTag::FF, AvmMemoryTag::U0); | ||
| auto rhs_y_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::ID, rhs_y_offset, AvmMemoryTag::FF, AvmMemoryTag::U0); | ||
|
|
||
| // Save this clk time to line up with the gadget op. | ||
| auto ecc_clk = clk; | ||
| main_trace.push_back(Row{ | ||
| .avm_main_clk = clk, | ||
| .avm_main_ia = lhs_x_read.val, | ||
| .avm_main_ib = lhs_y_read.val, | ||
| .avm_main_ic = rhs_x_read.val, | ||
| .avm_main_id = rhs_y_read.val, | ||
| .avm_main_internal_return_ptr = FF(internal_return_ptr), | ||
| .avm_main_mem_idx_a = FF(lhs_x_offset), | ||
| .avm_main_mem_idx_b = FF(lhs_y_offset), | ||
| .avm_main_mem_idx_c = FF(rhs_x_offset), | ||
| .avm_main_mem_idx_d = FF(rhs_y_offset), | ||
| .avm_main_mem_op_a = FF(1), | ||
| .avm_main_mem_op_b = FF(1), | ||
| .avm_main_mem_op_c = FF(1), | ||
| .avm_main_mem_op_d = FF(1), | ||
| .avm_main_pc = FF(pc++), | ||
| .avm_main_r_in_tag = FF(static_cast<uint32_t>(AvmMemoryTag::FF)), | ||
| }); | ||
| clk++; | ||
| // Load the infinite bools separately since they have a different memory tag | ||
| auto lhs_is_inf_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::IA, lhs_is_inf_offset, AvmMemoryTag::U8, AvmMemoryTag::U0); | ||
| auto rhs_is_inf_read = mem_trace_builder.read_and_load_from_memory( | ||
| call_ptr, clk, IntermRegister::IB, rhs_is_inf_offset, AvmMemoryTag::U8, AvmMemoryTag::U0); | ||
|
|
||
| main_trace.push_back(Row{ | ||
| .avm_main_clk = clk, | ||
| .avm_main_ia = lhs_is_inf_read.val, | ||
| .avm_main_ib = rhs_is_inf_read.val, | ||
| .avm_main_internal_return_ptr = FF(internal_return_ptr), | ||
| .avm_main_mem_idx_a = FF(lhs_is_inf_offset), | ||
| .avm_main_mem_idx_b = FF(rhs_is_inf_offset), | ||
| .avm_main_mem_op_a = FF(1), | ||
| .avm_main_mem_op_b = FF(1), | ||
| .avm_main_pc = FF(pc), | ||
| .avm_main_r_in_tag = FF(static_cast<uint32_t>(AvmMemoryTag::U8)), | ||
| }); | ||
| clk++; | ||
| grumpkin::g1::affine_element lhs = uint8_t(lhs_is_inf_read.val) == 1 | ||
| ? grumpkin::g1::affine_element::infinity() | ||
| : grumpkin::g1::affine_element{ lhs_x_read.val, lhs_y_read.val }; | ||
| grumpkin::g1::affine_element rhs = uint8_t(rhs_is_inf_read.val) == 1 | ||
| ? grumpkin::g1::affine_element::infinity() | ||
| : grumpkin::g1::affine_element{ rhs_x_read.val, rhs_y_read.val }; | ||
| auto result = ecc_trace_builder.embedded_curve_add(lhs, rhs, ecc_clk); | ||
| // Write across two lines since we have different mem_tags | ||
| uint32_t direct_output_offset = output_offset; | ||
| bool indirect_flag_output = is_operand_indirect(indirect, 6); | ||
| if (indirect_flag_output) { | ||
| auto read_ind_output = | ||
| mem_trace_builder.indirect_read_and_load_from_memory(call_ptr, clk, IndirectRegister::IND_A, output_offset); | ||
| direct_output_offset = uint32_t(read_ind_output.val); | ||
| } | ||
|
|
||
| mem_trace_builder.write_into_memory( | ||
| call_ptr, clk, IntermRegister::IA, direct_output_offset, result.x, AvmMemoryTag::U0, AvmMemoryTag::FF); | ||
| mem_trace_builder.write_into_memory( | ||
| call_ptr, clk, IntermRegister::IB, direct_output_offset + 1, result.y, AvmMemoryTag::U0, AvmMemoryTag::FF); | ||
| main_trace.push_back(Row{ | ||
| .avm_main_clk = clk, | ||
| .avm_main_ia = result.x, | ||
| .avm_main_ib = result.y, | ||
| .avm_main_ind_a = indirect_flag_output ? FF(output_offset) : FF(0), | ||
| .avm_main_ind_op_a = FF(static_cast<uint32_t>(indirect_flag_output)), | ||
| .avm_main_internal_return_ptr = FF(internal_return_ptr), | ||
| .avm_main_mem_idx_a = FF(direct_output_offset), | ||
| .avm_main_mem_idx_b = FF(direct_output_offset + 1), | ||
| .avm_main_mem_op_a = FF(1), | ||
| .avm_main_mem_op_b = FF(1), | ||
| .avm_main_pc = FF(pc), | ||
| .avm_main_rwa = FF(1), | ||
| .avm_main_rwb = FF(1), | ||
| .avm_main_w_in_tag = FF(static_cast<uint32_t>(AvmMemoryTag::FF)), | ||
| }); | ||
| clk++; | ||
| write_slice_to_memory(call_ptr, | ||
|
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. You can directly perform an indirect write and consolidate the first slice writing with a direct write at direct_output_offset+1 |
||
| clk, | ||
| direct_output_offset + 2, | ||
| AvmMemoryTag::U8, | ||
| AvmMemoryTag::U8, | ||
| FF(internal_return_ptr), | ||
| { result.is_point_at_infinity() }); | ||
| } | ||
| // Finalise Lookup Counts | ||
| // | ||
| // For log derivative lookups, we require a column that contains the number of times each lookup is consumed | ||
|
|
||
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
33 changes: 33 additions & 0 deletions
33
barretenberg/cpp/src/barretenberg/vm/avm_trace/gadgets/avm_ecc.cpp
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,33 @@ | ||
| #include "barretenberg/vm/avm_trace/gadgets/avm_ecc.hpp" | ||
| #include "barretenberg/vm/avm_trace/avm_common.hpp" | ||
|
|
||
| namespace bb::avm_trace { | ||
| using element = grumpkin::g1::affine_element; | ||
|
|
||
|
IlyasRidhuan marked this conversation as resolved.
|
||
| AvmEccTraceBuilder::AvmEccTraceBuilder() | ||
| { | ||
| ecc_trace.reserve(AVM_TRACE_SIZE); | ||
| } | ||
|
|
||
| std::vector<AvmEccTraceBuilder::EccTraceEntry> AvmEccTraceBuilder::finalize() | ||
| { | ||
| return std::move(ecc_trace); | ||
| } | ||
|
|
||
| void AvmEccTraceBuilder::reset() | ||
| { | ||
| ecc_trace.clear(); | ||
| } | ||
|
|
||
| element AvmEccTraceBuilder::embedded_curve_add(element lhs, element rhs, uint32_t clk) | ||
| { | ||
| element result = lhs + rhs; | ||
| std::tuple<FF, FF, bool> p1 = { lhs.x, lhs.y, lhs.is_point_at_infinity() }; | ||
| std::tuple<FF, FF, bool> p2 = { rhs.x, rhs.y, rhs.is_point_at_infinity() }; | ||
| std::tuple<FF, FF, bool> result_tuple = { result.x, result.y, result.is_point_at_infinity() }; | ||
| ecc_trace.push_back({ clk, p1, p2, result_tuple }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| } // namespace bb::avm_trace | ||
29 changes: 29 additions & 0 deletions
29
barretenberg/cpp/src/barretenberg/vm/avm_trace/gadgets/avm_ecc.hpp
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,29 @@ | ||
| #pragma once | ||
|
|
||
| #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" | ||
| #include "barretenberg/ecc/groups/affine_element.hpp" | ||
| #include "barretenberg/vm/avm_trace/avm_common.hpp" | ||
|
|
||
| namespace bb::avm_trace { | ||
| class AvmEccTraceBuilder { | ||
| public: | ||
| struct EccTraceEntry { | ||
| uint32_t clk = 0; | ||
| std::tuple<FF, FF, bool> p1; // x, y, is_infinity | ||
| std::tuple<FF, FF, bool> p2; | ||
| std::tuple<FF, FF, bool> result; | ||
| }; | ||
|
|
||
| AvmEccTraceBuilder(); | ||
| void reset(); | ||
| // Finalize the trace | ||
| std::vector<EccTraceEntry> finalize(); | ||
| grumpkin::g1::affine_element embedded_curve_add(grumpkin::g1::affine_element lhs, | ||
| grumpkin::g1::affine_element rhs, | ||
| uint32_t clk); | ||
|
|
||
| private: | ||
| std::vector<EccTraceEntry> ecc_trace; | ||
| }; | ||
|
|
||
| } // namespace bb::avm_trace |
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.
Uh oh!
There was an error while loading. Please reload this page.