Skip to content

Commit

Permalink
recusive v circuit prf verifies w no check circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Nov 30, 2023
1 parent 5383459 commit a2aa9ac
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ template <typename FF_> class UltraHonk {
SelectorType& q_elliptic() { return selectors[8]; };
SelectorType& q_aux() { return selectors[9]; };
SelectorType& q_lookup_type() { return selectors[10]; };
SelectorType& q_busread() { return this->selectors[11]; };
SelectorType& q_busread() { return selectors[11]; };

const auto& get() const { return selectors; };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ template <typename FF> void GoblinUltraCircuitBuilder_<FF>::add_gates_to_ensure_
this->w_l.emplace_back(public_calldata[read_idx]); // populate with value of calldata at read index
this->w_r.emplace_back(this->add_variable(FF(read_idx))); // populate with read index as witness
calldata_read_counts[read_idx]++; // increment read count at read index
q_busread.emplace_back(1); // read selector on
q_busread().emplace_back(1); // read selector on

// populate all other components with zero
this->w_o.emplace_back(this->zero_idx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ template <typename FF> class GoblinUltraCircuitBuilder_ : public UltraCircuitBui
WireVector& ecc_op_wire_3 = std::get<2>(ecc_op_wires);
WireVector& ecc_op_wire_4 = std::get<3>(ecc_op_wires);

SelectorVector& q_busread = this->selectors.q_busread();
SelectorVector& q_busread() { return this->selectors.q_busread(); };

// DataBus call/return data arrays
std::vector<uint32_t> public_calldata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ void UltraCircuitBuilder_<Arithmetization>::add_gates_to_ensure_all_polys_are_no
q_elliptic.emplace_back(1);
q_aux.emplace_back(1);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;

info("in ultra builder method.");
info("q_arith size = ", q_arith.size());
info("w_l size = ", w_l.size());
info("w_r size = ", w_r.size());

// Some relations depend on wire shifts so we add another gate with
// wires set to 0 to ensure corresponding constraints are satisfied
create_poly_gate({ this->zero_idx, this->zero_idx, this->zero_idx, 0, 0, 0, 0, 0 });
Expand Down Expand Up @@ -140,6 +146,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_add_gate(const add_triple_<FF
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -172,6 +179,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_big_add_gate(const add_quad_<
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -266,6 +274,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_big_mul_gate(const mul_quad_<
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand All @@ -292,6 +301,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_balanced_add_gate(const add_q
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
// Why 3? TODO: return to this
// The purpose of this gate is to do enable lazy 32-bit addition.
Expand Down Expand Up @@ -334,6 +344,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_mul_gate(const mul_triple_<FF
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}
/**
Expand Down Expand Up @@ -363,6 +374,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_bool_gate(const uint32_t vari
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -394,6 +406,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_poly_gate(const poly_triple_<
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -448,6 +461,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_ecc_add_gate(const ecc_add_ga
q_elliptic.emplace_back(1);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}
w_l.emplace_back(in.x2);
Expand All @@ -466,6 +480,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_ecc_add_gate(const ecc_add_ga
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -512,6 +527,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_ecc_dbl_gate(const ecc_dbl_ga
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand All @@ -531,6 +547,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_ecc_dbl_gate(const ecc_dbl_ga
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -561,6 +578,7 @@ void UltraCircuitBuilder_<Arithmetization>::fix_witness(const uint32_t witness_i
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}

Expand Down Expand Up @@ -636,6 +654,7 @@ plookup::ReadData<uint32_t> UltraCircuitBuilder_<Arithmetization>::create_gates_
q_elliptic.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
++this->num_gates;
}
return read_data;
Expand Down Expand Up @@ -945,6 +964,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint(const std::ve
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}
// dummy gate needed because of sort widget's check of next row
w_l.emplace_back(variable_index[variable_index.size() - 1]);
Expand All @@ -964,6 +984,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint(const std::ve
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}

// useful to put variables in the witness that aren't already used - e.g. the dummy variables of the range constraint in
Expand Down Expand Up @@ -998,6 +1019,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_dummy_constraints(const std::
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}
}

Expand Down Expand Up @@ -1029,6 +1051,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint_with_edges(
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
// enforce range check for middle rows
for (size_t i = gate_width; i < variable_index.size() - gate_width; i += gate_width) {

Expand All @@ -1049,6 +1072,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint_with_edges(
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}
// enforce range checks of last row and ending at end
if (variable_index.size() > gate_width) {
Expand All @@ -1069,6 +1093,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint_with_edges(
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}

// dummy gate needed because of sort widget's check of next row
Expand All @@ -1090,6 +1115,7 @@ void UltraCircuitBuilder_<Arithmetization>::create_sort_constraint_with_edges(
q_lookup_type.emplace_back(0);
q_aux.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
}

// range constraint a value by decomposing it into limbs whose size should be the default range constraint size
Expand Down Expand Up @@ -1206,6 +1232,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::LIMB_ACCUMULATE_2: {
Expand All @@ -1217,6 +1244,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::NON_NATIVE_FIELD_1: {
Expand All @@ -1228,6 +1256,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::NON_NATIVE_FIELD_2: {
Expand All @@ -1239,6 +1268,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::NON_NATIVE_FIELD_3: {
Expand All @@ -1250,6 +1280,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::ROM_CONSISTENCY_CHECK: {
Expand All @@ -1265,6 +1296,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::RAM_CONSISTENCY_CHECK: {
Expand All @@ -1281,6 +1313,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(1);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::RAM_TIMESTAMP_CHECK: {
Expand All @@ -1294,6 +1327,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::ROM_READ: {
Expand All @@ -1308,6 +1342,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0); // read/write flag stored in q_c
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::RAM_READ: {
Expand All @@ -1322,6 +1357,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0); // read/write flag stored in q_c
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
case AUX_SELECTORS::RAM_WRITE: {
Expand All @@ -1336,6 +1372,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(1); // read/write flag stored in q_c
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
default: {
Expand All @@ -1347,6 +1384,7 @@ void UltraCircuitBuilder_<Arithmetization>::apply_aux_selectors(const AUX_SELECT
q_c.emplace_back(0);
q_arith.emplace_back(0);
selectors.pad_additional();
check_selector_length_consistency();
break;
}
}
Expand Down Expand Up @@ -1869,6 +1907,7 @@ std::array<uint32_t, 5> UltraCircuitBuilder_<Arithmetization>::evaluate_non_nati
q_aux.emplace_back(0);
selectors.pad_additional();
}
check_selector_length_consistency();

this->num_gates += 4;
return std::array<uint32_t, 5>{
Expand Down Expand Up @@ -1991,6 +2030,7 @@ std::array<uint32_t, 5> UltraCircuitBuilder_<Arithmetization>::evaluate_non_nati
q_aux.emplace_back(0);
selectors.pad_additional();
}
check_selector_length_consistency();

this->num_gates += 4;
return std::array<uint32_t, 5>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,23 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase<typename Arithmetization:
};
~UltraCircuitBuilder_() override = default;

void check_selector_length_consistency()
{
bool sizes_consistent = true;
size_t nominal_size = selectors.get()[0].size();
// for (auto& selector : selectors.get()) {
for (size_t idx = 0; idx < selectors.get().size(); ++idx) {
sizes_consistent = sizes_consistent && (selectors.get()[idx].size() == nominal_size);
}
if (!sizes_consistent) {
for (size_t idx = 0; idx < selectors.get().size(); ++idx) {
info("idx = ", idx);
info(selectors.get()[idx].size());
}
}
ASSERT(sizes_consistent);
}

void finalize_circuit();

void add_gates_to_ensure_all_polys_are_non_zero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,27 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te

// Check the recursive verifier circuit
EXPECT_EQ(outer_circuit.failed(), false) << outer_circuit.err();
EXPECT_TRUE(outer_circuit.check_circuit());

// { // Construct and verify a proof of the recursive verifier circuit
// auto composer = get_composer<OuterBuilder>();
// auto instance = composer.create_instance(outer_circuit);
// auto prover = composer.create_prover(instance);
// auto verifier = composer.create_verifier(instance);
// auto proof = prover.construct_proof();
// bool verified = verifier.verify_proof(proof);
// ASSERT(verified);
// }
// EXPECT_TRUE(outer_circuit.check_circuit());

{ // Construct and verify a proof of the recursive verifier circuit
auto composer = get_composer<OuterBuilder>();
auto instance = composer.create_instance(outer_circuit);
auto prover = composer.create_prover(instance);
auto verifier = composer.create_verifier(instance);
auto proof = prover.construct_proof();
bool verified = verifier.verify_proof(proof);
(void)verified;

auto prover_manifest = prover.transcript.get_manifest();
auto verifier_manifest = verifier.transcript.get_manifest();
// prover_manifest.print();
// verifier_manifest.print();
for (size_t i = 0; i < prover_manifest.size(); ++i) {
EXPECT_EQ(prover_manifest[i], verifier_manifest[i]);
}

ASSERT(verified);
}

// Additional check 1: Perform native verification then perform the pairing on the outputs of the recursive
// verifier and check that the result agrees.
Expand Down
Loading

0 comments on commit a2aa9ac

Please sign in to comment.