Skip to content

Commit

Permalink
from witness indices method for UH rec VK
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Sep 10, 2024
1 parent e5a4f0b commit 9a6f57a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace {
void create_dummy_vkey_and_proof(Builder& builder,
size_t proof_size,
size_t public_inputs_size,
const std::vector<field_ct>& key_fields,
const std::vector<uint32_t>& key_indices,
const std::vector<field_ct>& proof_fields)
{
using Flavor = UltraFlavor;
Expand All @@ -50,29 +50,29 @@ void create_dummy_vkey_and_proof(Builder& builder,
Flavor::NUM_ALL_ENTITIES * num_frs_fr - 2 * num_frs_comm) /
(num_frs_comm + num_frs_fr * Flavor::BATCHED_RELATION_PARTIAL_LENGTH);
// First key field is circuit size
builder.assert_equal(builder.add_variable(1 << log_circuit_size), key_fields[0].witness_index);
builder.assert_equal(builder.add_variable(1 << log_circuit_size), key_indices[0]);
// Second key field is number of public inputs
builder.assert_equal(builder.add_variable(public_inputs_size), key_fields[1].witness_index);
builder.assert_equal(builder.add_variable(public_inputs_size), key_indices[1]);
// Third key field is the pub inputs offset
builder.assert_equal(builder.add_variable(Flavor::has_zero_row ? 1 : 0), key_fields[2].witness_index);
builder.assert_equal(builder.add_variable(Flavor::has_zero_row ? 1 : 0), key_indices[2]);
// Fourth key field is the whether the proof contains an aggregation object.
builder.assert_equal(builder.add_variable(1), key_fields[3].witness_index);
builder.assert_equal(builder.add_variable(1), key_indices[3]);
uint32_t offset = 4;
size_t num_inner_public_inputs = public_inputs_size - bb::AGGREGATION_OBJECT_SIZE;

// We are making the assumption that the aggregation object are behind all the inner public inputs
for (size_t i = 0; i < bb::AGGREGATION_OBJECT_SIZE; i++) {
builder.assert_equal(builder.add_variable(num_inner_public_inputs + i), key_fields[offset].witness_index);
builder.assert_equal(builder.add_variable(num_inner_public_inputs + i), key_indices[offset]);
offset++;
}

for (size_t i = 0; i < Flavor::NUM_PRECOMPUTED_ENTITIES; ++i) {
auto comm = curve::BN254::AffineElement::one() * fr::random_element();
auto frs = field_conversion::convert_to_bn254_frs(comm);
builder.assert_equal(builder.add_variable(frs[0]), key_fields[offset].witness_index);
builder.assert_equal(builder.add_variable(frs[1]), key_fields[offset + 1].witness_index);
builder.assert_equal(builder.add_variable(frs[2]), key_fields[offset + 2].witness_index);
builder.assert_equal(builder.add_variable(frs[3]), key_fields[offset + 3].witness_index);
builder.assert_equal(builder.add_variable(frs[0]), key_indices[offset]);
builder.assert_equal(builder.add_variable(frs[1]), key_indices[offset + 1]);
builder.assert_equal(builder.add_variable(frs[2]), key_indices[offset + 2]);
builder.assert_equal(builder.add_variable(frs[3]), key_indices[offset + 3]);
offset += 4;
}

Expand Down Expand Up @@ -168,12 +168,8 @@ AggregationObjectIndices create_honk_recursion_constraints(Builder& builder,
// Construct an in-circuit representation of the verification key.
// For now, the v-key is a circuit constant and is fixed for the circuit.
// (We may need a separate recursion opcode for this to vary, or add more config witnesses to this opcode)
std::vector<field_ct> key_fields;
key_fields.reserve(input.key.size());
for (const auto& idx : input.key) {
auto field = field_ct::from_witness_index(&builder, idx);
key_fields.emplace_back(field);
}
auto vkey =
std::make_shared<RecursiveVerificationKey>(RecursiveVerificationKey::from_witness_indices(builder, input.key));

std::vector<field_ct> proof_fields;

Expand All @@ -193,11 +189,10 @@ AggregationObjectIndices create_honk_recursion_constraints(Builder& builder,
size_t size_of_proof_with_no_pub_inputs = input.proof.size() - bb::AGGREGATION_OBJECT_SIZE;
size_t total_num_public_inputs = input.public_inputs.size() + bb::AGGREGATION_OBJECT_SIZE;
create_dummy_vkey_and_proof(
builder, size_of_proof_with_no_pub_inputs, total_num_public_inputs, key_fields, proof_fields);
builder, size_of_proof_with_no_pub_inputs, total_num_public_inputs, input.key, proof_fields);
}

// Recursively verify the proof
auto vkey = std::make_shared<RecursiveVerificationKey>(builder, key_fields);
RecursiveVerifier verifier(&builder, vkey);
aggregation_state_ct input_agg_obj = bb::stdlib::recursion::convert_witness_indices_to_agg_obj<Builder, bn254>(
builder, input_aggregation_object_indices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {
commitment = deserialize_from_frs<Commitment>(builder, elements, num_frs_read);
}
}

/**
* @brief Construct a VerificationKey from a set of corresponding witness indices
*
* @param builder
* @param witness_indices
* @return VerificationKey
*/
static VerificationKey from_witness_indices(CircuitBuilder& builder,
const std::span<const uint32_t>& witness_indices)
{
std::vector<FF> vkey_fields;
vkey_fields.reserve(witness_indices.size());
for (const auto& idx : witness_indices) {
vkey_fields.emplace_back(FF::from_witness_index(&builder, idx));
}
return VerificationKey(builder, vkey_fields);
}
};

/**
Expand Down

0 comments on commit 9a6f57a

Please sign in to comment.