-
Notifications
You must be signed in to change notification settings - Fork 130
feat!: add support for ROM and RAM ACVM opcodes #417
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5313b11
*WIP* do not push
guipublic d440f1d
Generate constraints for dynamic memory
guipublic 993be33
fix unit test: add missing block_constraint
guipublic 7777bf2
add unit test for dynamic memory
guipublic d385150
missed one block constraint in ecdsa unit test
guipublic d99f1b8
trying a rebase
guipublic 4b92d66
remove comments
guipublic 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include "block_constraint.hpp" | ||
| #include "barretenberg/stdlib/primitives/memory/rom_table.hpp" | ||
| #include "barretenberg/stdlib/primitives/memory/ram_table.hpp" | ||
|
|
||
| using namespace proof_system::plonk; | ||
|
|
||
| namespace acir_format { | ||
| field_ct poly_to_field_ct(const poly_triple poly, Composer& composer) | ||
| { | ||
| ASSERT(poly.q_m == 0); | ||
| ASSERT(poly.q_r == 0); | ||
| ASSERT(poly.q_o == 0); | ||
| if (poly.q_l == 0) { | ||
| return field_ct(poly.q_c); | ||
| } | ||
| field_ct x = field_ct::from_witness_index(&composer, poly.a); | ||
| x.additive_constant = poly.q_c; | ||
| x.multiplicative_constant = poly.q_l; | ||
| return x; | ||
| } | ||
|
|
||
| void create_block_constraints(Composer& composer, const BlockConstraint constraint) | ||
| { | ||
| std::vector<field_ct> init; | ||
| for (auto i : constraint.init) { | ||
| field_ct value = poly_to_field_ct(i, composer); | ||
| init.push_back(value); | ||
| } | ||
|
|
||
| switch (constraint.type) { | ||
| case BlockType::ROM: { | ||
|
|
||
| rom_table_ct table(init); | ||
| for (auto& op : constraint.trace) { | ||
| ASSERT(op.access_type == 0); | ||
| field_ct value = poly_to_field_ct(op.value, composer); | ||
| field_ct index = poly_to_field_ct(op.index, composer); | ||
| value.assert_equal(table[index]); | ||
| } | ||
| } break; | ||
| case BlockType::RAM: { | ||
| ram_table_ct table(init); | ||
| for (auto& op : constraint.trace) { | ||
| field_ct value = poly_to_field_ct(op.value, composer); | ||
| field_ct index = poly_to_field_ct(op.index, composer); | ||
| if (op.access_type == 0) { | ||
| value.assert_equal(table.read(index)); | ||
| } else { | ||
| ASSERT(op.access_type == 1); | ||
| table.write(index, value); | ||
| } | ||
| } | ||
| } break; | ||
| default: | ||
| ASSERT(false); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } // namespace acir_format | ||
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,62 @@ | ||
| #pragma once | ||
| #include <cstdint> | ||
| #include <vector> | ||
| #include "barretenberg/plonk/composer/ultra_composer.hpp" | ||
| #include "barretenberg/stdlib/primitives/field/field.hpp" | ||
| #include "barretenberg/dsl/types.hpp" | ||
|
|
||
| namespace acir_format { | ||
|
|
||
| struct MemOp { | ||
| uint8_t access_type; | ||
| poly_triple index; | ||
| poly_triple value; | ||
| }; | ||
|
|
||
| enum BlockType { | ||
| ROM = 0, | ||
| RAM = 1, | ||
| }; | ||
|
|
||
| struct BlockConstraint { | ||
| std::vector<poly_triple> init; | ||
| std::vector<MemOp> trace; | ||
| BlockType type; | ||
| }; | ||
|
|
||
| void create_block_constraints(Composer& composer, const BlockConstraint constraint); | ||
|
|
||
| template <typename B> inline void read(B& buf, MemOp& mem_op) | ||
| { | ||
| using serialize::read; | ||
| read(buf, mem_op.access_type); | ||
| read(buf, mem_op.index); | ||
| read(buf, mem_op.value); | ||
| } | ||
|
|
||
| template <typename B> inline void write(B& buf, MemOp const& mem_op) | ||
| { | ||
| using serialize::write; | ||
| write(buf, mem_op.access_type); | ||
| write(buf, mem_op.index); | ||
| write(buf, mem_op.value); | ||
| } | ||
|
|
||
| template <typename B> inline void read(B& buf, BlockConstraint& constraint) | ||
| { | ||
| using serialize::read; | ||
| read(buf, constraint.init); | ||
| read(buf, constraint.trace); | ||
| uint8_t type; | ||
| read(buf, type); | ||
| constraint.type = static_cast<BlockType>(type); | ||
| } | ||
|
|
||
| template <typename B> inline void write(B& buf, BlockConstraint const& constraint) | ||
| { | ||
| using serialize::write; | ||
| write(buf, constraint.init); | ||
| write(buf, constraint.trace); | ||
| write(buf, static_cast<uint8_t>(constraint.type)); | ||
| } | ||
| } // namespace acir_format |
130 changes: 130 additions & 0 deletions
130
cpp/src/barretenberg/dsl/acir_format/block_constraint.test.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,130 @@ | ||
| #include "acir_format.hpp" | ||
| #include "block_constraint.hpp" | ||
| #include "barretenberg/plonk/proof_system/types/proof.hpp" | ||
| #include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <vector> | ||
|
|
||
| size_t generate_block_constraint(acir_format::BlockConstraint& constraint, std::vector<fr>& witness_values) | ||
| { | ||
| size_t witness_len = 1; | ||
| witness_values.emplace_back(1); | ||
| witness_len++; | ||
|
|
||
| fr two = fr::one() + fr::one(); | ||
| poly_triple a0 = poly_triple{ | ||
| .a = 1, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = two, | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = 0, | ||
| }; | ||
| fr three = fr::one() + two; | ||
| poly_triple a1 = poly_triple{ | ||
| .a = 0, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = 0, | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = three, | ||
| }; | ||
| poly_triple r1 = poly_triple{ | ||
| .a = 1, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = fr::one(), | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = fr::neg_one(), | ||
| }; | ||
| poly_triple r2 = poly_triple{ | ||
| .a = 1, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = two, | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = fr::neg_one(), | ||
| }; | ||
| poly_triple y = poly_triple{ | ||
| .a = 2, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = fr::one(), | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = 0, | ||
| }; | ||
| witness_values.emplace_back(2); | ||
| witness_len++; | ||
| poly_triple z = poly_triple{ | ||
| .a = 3, | ||
| .b = 0, | ||
| .c = 0, | ||
| .q_m = 0, | ||
| .q_l = fr::one(), | ||
| .q_r = 0, | ||
| .q_o = 0, | ||
| .q_c = 0, | ||
| }; | ||
| witness_values.emplace_back(3); | ||
| witness_len++; | ||
| acir_format::MemOp op1 = acir_format::MemOp{ | ||
| .access_type = 0, | ||
| .index = r1, | ||
| .value = y, | ||
| }; | ||
| acir_format::MemOp op2 = acir_format::MemOp{ | ||
| .access_type = 0, | ||
| .index = r2, | ||
| .value = z, | ||
| }; | ||
| constraint = acir_format::BlockConstraint{ | ||
| .init = { a0, a1 }, | ||
| .trace = { op1, op2 }, | ||
| .type = acir_format::BlockType::ROM, | ||
| }; | ||
|
|
||
| return witness_len; | ||
| } | ||
|
|
||
| TEST(up_ram, TestBlockConstraint) | ||
| { | ||
| acir_format::BlockConstraint block; | ||
| std::vector<fr> witness_values; | ||
| size_t num_variables = generate_block_constraint(block, witness_values); | ||
| acir_format::acir_format constraint_system{ | ||
| .varnum = static_cast<uint32_t>(num_variables), | ||
| .public_inputs = {}, | ||
| .fixed_base_scalar_mul_constraints = {}, | ||
| .logic_constraints = {}, | ||
| .range_constraints = {}, | ||
| .schnorr_constraints = {}, | ||
| .ecdsa_constraints = {}, | ||
| .sha256_constraints = {}, | ||
| .blake2s_constraints = {}, | ||
| .keccak_constraints = {}, | ||
| .hash_to_field_constraints = {}, | ||
| .pedersen_constraints = {}, | ||
| .compute_merkle_root_constraints = {}, | ||
| .block_constraints = { block }, | ||
| .constraints = {}, | ||
| }; | ||
|
|
||
| auto composer = acir_format::create_circuit_with_witness(constraint_system, witness_values); | ||
|
|
||
| auto prover = composer.create_prover(); | ||
|
|
||
| auto proof = prover.construct_proof(); | ||
| auto verifier = composer.create_verifier(); | ||
| EXPECT_EQ(verifier.verify_proof(proof), true); | ||
| } |
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.