-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add support for ROM and RAM ACVM opcodes (#417)
* *WIP* do not push * Generate constraints for dynamic memory * fix unit test: add missing block_constraint * add unit test for dynamic memory * missed one block constraint in ecdsa unit test * trying a rebase * remove comments
- Loading branch information
Showing
8 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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.