Skip to content
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

feat!: change backend width to 4 #5374

Merged
merged 36 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
831ed00
upgrade width to 4
guipublic Mar 21, 2024
31b0c56
update snapshot
guipublic Mar 21, 2024
c143f77
Merge branch 'master' into gd/width_4
guipublic Mar 25, 2024
2306d04
Merge branch 'master' into gd/width_4
TomAFrench Mar 26, 2024
868c8b8
enable acir gates of width 4 in bb
guipublic Mar 26, 2024
5dd0769
allow for non mul gates
guipublic Mar 26, 2024
9cea3c6
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
2f39695
backup to quad gate
guipublic Mar 26, 2024
4d3922f
remove dead code
guipublic Mar 26, 2024
c64b87e
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
7772468
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
a86c334
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
fa2a6ec
update test cases with quad_constraints
guipublic Mar 26, 2024
b1e82d8
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
6bf1d97
add missing quad_constraint for test case
guipublic Mar 26, 2024
54864c3
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
1af068e
Merge branch 'master' into gd/width_4
guipublic Mar 26, 2024
1c068fe
code review
guipublic Mar 26, 2024
21367a0
Merge branch 'master' into gd/width_4
guipublic Mar 27, 2024
d1d37b4
change default width to 4
guipublic Mar 27, 2024
22d3a2c
Merge branch 'master' into gd/width_4
guipublic Mar 27, 2024
29a3148
code review
guipublic Mar 27, 2024
1d5a889
Merge branch 'master' into gd/width_4
guipublic Mar 27, 2024
e6a5284
Merge branch 'master' into gd/width_4
guipublic Mar 27, 2024
d658c48
Merge branch 'master' into gd/width_4
guipublic Mar 28, 2024
895810f
Merge branch 'master' into gd/width_4
guipublic Mar 29, 2024
761f3dd
Merge branch 'master' into gd/width_4
guipublic Apr 15, 2024
b5c67fc
Merge branch 'master' into gd/width_4
guipublic Apr 15, 2024
92e1f5e
Merge branch 'master' into gd/width_4
TomAFrench Apr 15, 2024
538bda8
Merge branch 'master' into gd/width_4
guipublic Apr 16, 2024
239d70f
update instance deployer address
guipublic Apr 16, 2024
453a68d
Merge branch 'master' into gd/width_4
guipublic Apr 16, 2024
6e02f44
Merge branch 'master' into gd/width_4
guipublic Apr 16, 2024
a34516f
Merge branch 'master' into gd/width_4
guipublic Apr 16, 2024
720a84f
update instance deployer address
guipublic Apr 16, 2024
dce0800
Merge branch 'master' into gd/width_4
guipublic Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ void acvm_info(const std::string& output_path)
const char* jsonData = R"({
"language": {
"name" : "PLONK-CSAT",
"width" : 3
"width" : 4
}
})";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ void build_constraints(Builder& builder, AcirFormat const& constraint_system, bo
for (const auto& constraint : constraint_system.constraints) {
builder.create_poly_gate(constraint);
}
for (const auto& constraint : constraint_system.quad_constraints) {
builder.create_big_mul_gate(constraint);
}

// Add logic constraint
for (const auto& constraint : constraint_system.logic_constraints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct AcirFormat {
std::vector<poly_triple_<curve::BN254::ScalarField>,
ContainerSlabAllocator<poly_triple_<curve::BN254::ScalarField>>>
constraints;
guipublic marked this conversation as resolved.
Show resolved Hide resolved
std::vector<mul_quad_<curve::BN254::ScalarField>> quad_constraints;
guipublic marked this conversation as resolved.
Show resolved Hide resolved
std::vector<BlockConstraint> block_constraints;

// For serialization, update with any new fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = { constraint },
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -171,6 +172,7 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = { expr_a, expr_b, expr_c, expr_d },
.quad_constraints = {},
.block_constraints = {} };

uint256_t inverse_of_five = fr(5).invert();
Expand Down Expand Up @@ -248,6 +250,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass)
.q_o = 1,
.q_c = fr::neg_one(),
} },
.quad_constraints = {},
.block_constraints = {} };

std::string message_string = "tenletters";
Expand Down Expand Up @@ -343,6 +346,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange)
.q_o = 1,
.q_c = fr::neg_one(),
} },
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -448,6 +452,7 @@ TEST_F(AcirFormatTests, TestVarKeccak)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = { dummy },
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -495,6 +500,7 @@ TEST_F(AcirFormatTests, TestKeccakPermutation)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {} };

WitnessVector witness{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <iterator>

namespace acir_format {

using mul_quad = mul_quad_<bb::fr>;
/**
* @brief Construct a poly_tuple for a standard width-3 arithmetic gate from its acir representation
*
Expand Down Expand Up @@ -89,18 +89,106 @@ poly_triple serialize_arithmetic_gate(Program::Expression const& arg)
pt.q_o = selector_value;
c_set = true;
} else {
throw_or_abort("Cannot assign linear term to a constraint of width 3");
return poly_triple{
.a = 0,
.b = 0,
.c = 0,
.q_m = 0,
.q_l = 0,
.q_r = 0,
.q_o = 0,
.q_c = 0,
};
}
}

// Set constant value q_c
pt.q_c = uint256_t(arg.q_c);
return pt;
}
mul_quad serialize_mul_quad_gate(Program::Expression const& arg)
{
// TODO(https://github.com/AztecProtocol/barretenberg/issues/816): The initialization of the witness indices a,b,c
// to 0 is implicitly assuming that (builder.zero_idx == 0) which is no longer the case. Now, witness idx 0 in
// general will correspond to some non-zero value and some witnesses which are not explicitly set below will be
// erroneously populated with this value. This does not cause failures however because the corresponding selector
// will indeed be 0 so the gate will be satisfied. Still, its a bad idea to have erroneous wire values
// even if they dont break the relation. They'll still add cost in commitments, for example.
mul_quad quad{ .a = 0,
.b = 0,
.c = 0,
.d = 0,
.mul_scaling = 0,
.a_scaling = 0,
.b_scaling = 0,
.c_scaling = 0,
.d_scaling = 0,
.const_scaling = 0 };

// Flags indicating whether each witness index for the present poly_tuple has been set
guipublic marked this conversation as resolved.
Show resolved Hide resolved
bool a_set = false;
bool b_set = false;
bool c_set = false;
bool d_set = false;
ASSERT(arg.mul_terms.size() <= 1); // We can only accommodate 1 quadratic term
// Note: mul_terms are tuples of the form {selector_value, witness_idx_1, witness_idx_2}
if (!arg.mul_terms.empty()) {
const auto& mul_term = arg.mul_terms[0];
quad.mul_scaling = uint256_t(std::get<0>(mul_term));
quad.a = std::get<1>(mul_term).value;
quad.b = std::get<2>(mul_term).value;
a_set = true;
b_set = true;
}
// If necessary, set values for linears terms q_l * w_l, q_r * w_r and q_o * w_o
ASSERT(arg.linear_combinations.size() <= 4); // We can only accommodate 4 linear terms
for (const auto& linear_term : arg.linear_combinations) {
bb::fr selector_value(uint256_t(std::get<0>(linear_term)));
uint32_t witness_idx = std::get<1>(linear_term).value;

// If the witness index has not yet been set or if the corresponding linear term is active, set the witness
// index and the corresponding selector value.
// TODO(https://github.com/AztecProtocol/barretenberg/issues/816): May need to adjust the pt.a == witness_idx
guipublic marked this conversation as resolved.
Show resolved Hide resolved
// check (and the others like it) since we initialize a,b,c with 0 but 0 is a valid witness index once the
// +1 offset is removed from noir.
if (!a_set || quad.a == witness_idx) {
quad.a = witness_idx;
quad.a_scaling = selector_value;
a_set = true;
} else if (!b_set || quad.b == witness_idx) {
quad.b = witness_idx;
quad.b_scaling = selector_value;
b_set = true;
} else if (!c_set || quad.c == witness_idx) {
quad.c = witness_idx;
quad.c_scaling = selector_value;
c_set = true;
} else if (!d_set || quad.d == witness_idx) {
quad.d = witness_idx;
quad.d_scaling = selector_value;
d_set = true;
} else {
throw_or_abort("Cannot assign linear term to a constraint of width 4");
}
}

// Set constant value q_c
quad.const_scaling = uint256_t(arg.q_c);
return quad;
}

void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af)
{
af.constraints.push_back(serialize_arithmetic_gate(arg.value));
if (arg.value.linear_combinations.size() <= 3) {
poly_triple pt = serialize_arithmetic_gate(arg.value);
if (pt == poly_triple{ 0, 0, 0, 0, 0, 0, 0, 0 }) {
guipublic marked this conversation as resolved.
Show resolved Hide resolved
af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value));
} else {
af.constraints.push_back(pt);
}
} else {
af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value));
}
}

void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, AcirFormat& af)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};
apply_constraints(constraint_system, contraints);
Expand Down Expand Up @@ -260,6 +261,7 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple)
.bigint_to_le_bytes_constraints = { result2_to_le_bytes },
.bigint_operations = { add_constraint },
.constraints = {},
.quad_constraints = {},
.block_constraints = {},

};
Expand Down Expand Up @@ -313,6 +315,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};
apply_constraints(constraint_system, contraints);
Expand Down Expand Up @@ -370,6 +373,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};
apply_constraints(constraint_system, contraints);
Expand Down Expand Up @@ -448,6 +452,7 @@ TEST_F(BigIntTests, TestBigIntDIV)
.bigint_to_le_bytes_constraints = { result3_to_le_bytes },
.bigint_operations = { div_constraint },
.constraints = {},
.quad_constraints = {},
.block_constraints = {},

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = { block },
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ TEST_F(EcOperations, TestECOperations)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -163,6 +164,7 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -207,6 +209,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ TEST(ECDSASecp256r1, test_hardcoded)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -199,6 +200,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down Expand Up @@ -248,6 +250,7 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};
auto builder = create_circuit(constraint_system);
Expand Down Expand Up @@ -292,6 +295,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {} };

WitnessVector witness{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Builder create_inner_circuit()
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = { expr_a, expr_b, expr_c, expr_d },
.quad_constraints = {},
.block_constraints = {} };

uint256_t inverse_of_five = fr(5).invert();
Expand Down Expand Up @@ -263,6 +264,7 @@ Builder create_outer_circuit(std::vector<Builder>& inner_circuits)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {} };

auto outer_circuit = create_circuit(constraint_system, /*size_hint*/ 0, witness);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TEST_F(Sha256Tests, TestSha256Compression)
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.quad_constraints = {},
.block_constraints = {} };

WitnessVector witness{ 0,
Expand Down
Loading