Skip to content

Commit

Permalink
4944: addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Aug 22, 2024
1 parent ddf527d commit 1f9f846
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 28 deletions.
4 changes: 4 additions & 0 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,14 @@ bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::
vinfo("circuit size: ", circuit_size);
vinfo("num of pub inputs: ", num_public_inputs);

// Each commitment (precomputed entity) is represented as 2 Fq field elements.
// Each Fq element is split into two limbs of Fr elements.
// We therefore need 2 (circuit_size, num_public_inputs) + 4 * NUM_PRECOMPUTED_ENTITIES fr elements.
ASSERT(vk_as_fields.size() == 4 * AvmFlavor::NUM_PRECOMPUTED_ENTITIES + 2);

std::array<Commitment, AvmFlavor::NUM_PRECOMPUTED_ENTITIES> precomputed_cmts;
for (size_t i = 0; i < AvmFlavor::NUM_PRECOMPUTED_ENTITIES; i++) {
// Start at offset 2 and adds 4 fr elements per commitment. Therefore, index = 4 * i + 2.
precomputed_cmts[i] = field_conversion::convert_from_bn254_frs<Commitment>(vk_span.subspan(4 * i + 2, 4));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AvmProver AvmComposer::create_prover(CircuitConstructor& circuit_constructor)
AvmVerifier AvmComposer::create_verifier(CircuitConstructor& circuit_constructor)
{
auto verification_key = compute_verification_key(circuit_constructor);
return AvmVerifier(verification_key);
return AvmVerifier(std::move(verification_key));
}

std::shared_ptr<Flavor::ProvingKey> AvmComposer::compute_proving_key(CircuitConstructor& circuit_constructor)
Expand Down
3 changes: 0 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@ class AvmFlavor {
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
VerificationKey() = default;
VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
: VerificationKey_(circuit_size, num_public_inputs)
{}

VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
: VerificationKey_(proving_key->circuit_size, proving_key->num_public_inputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace bb {

AvmVerifier::AvmVerifier(std::shared_ptr<Flavor::VerificationKey> verifier_key)
: key(verifier_key)
, pcs_verification_key(std::make_shared<VerifierCommitmentKey>())
: key(std::move(verifier_key))
{}

AvmVerifier::AvmVerifier(AvmVerifier&& other) noexcept
Expand All @@ -22,7 +21,7 @@ AvmVerifier::AvmVerifier(AvmVerifier&& other) noexcept
AvmVerifier& AvmVerifier::operator=(AvmVerifier&& other) noexcept
{
key = other.key;
pcs_verification_key = (std::move(other.pcs_verification_key));
pcs_verification_key = other.pcs_verification_key;
commitments.clear();
return *this;
}
Expand Down Expand Up @@ -146,11 +145,11 @@ bool AvmVerifier::verify_proof(const HonkProof& proof,
claimed_evaluations.get_unshifted(),
claimed_evaluations.get_shifted(),
multivariate_challenge,
pcs_verification_key->get_g1_identity(),
pcs_verification_key.get_g1_identity(),
transcript);

auto pairing_points = PCS::reduce_verify(opening_claim, transcript);
auto verified = pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);
auto verified = pcs_verification_key.pairing_check(pairing_points[0], pairing_points[1]);
return sumcheck_verified.value() && verified;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AvmVerifier {

std::shared_ptr<VerificationKey> key;
std::map<std::string, Commitment> commitments;
std::shared_ptr<VerifierCommitmentKey> pcs_verification_key;
VerifierCommitmentKey pcs_verification_key;
std::shared_ptr<Transcript> transcript;
};

Expand Down
8 changes: 1 addition & 7 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,7 @@ VmPublicInputs Execution::convert_public_inputs(std::vector<FF> const& public_in

bool Execution::verify(AvmFlavor::VerificationKey vk, HonkProof const& proof)
{
auto verification_key = std::make_shared<AvmFlavor::VerificationKey>(vk);
AvmVerifier verifier(verification_key);

// todo: not needed for now until we verify the PCS/pairing of the proof
// auto pcs_verification_key = std::make_unique<VerifierCommitmentKey>(verification_key->circuit_size,
// crs_factory_);
// output_state.pcs_verification_key = std::move(pcs_verification_key);
AvmVerifier verifier(std::make_shared<AvmFlavor::VerificationKey>(vk));

// Proof structure: public_inputs | calldata_size | calldata | returndata_size | returndata | raw proof
std::vector<FF> public_inputs_vec;
Expand Down
2 changes: 1 addition & 1 deletion bb-pilcom/bb-pil-backend/templates/composer.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void {{name}}Composer::compute_witness(CircuitConstructor& circuit)
{{name}}Verifier {{name}}Composer::create_verifier(CircuitConstructor& circuit_constructor)
{
auto verification_key = compute_verification_key(circuit_constructor);
return {{name}}Verifier(verification_key);
return {{name}}Verifier(std::move(verification_key));
}

std::shared_ptr<Flavor::ProvingKey> {{name}}Composer::compute_proving_key(CircuitConstructor& circuit_constructor)
Expand Down
3 changes: 0 additions & 3 deletions bb-pilcom/bb-pil-backend/templates/flavor.hpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ class {{name}}Flavor {
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
VerificationKey() = default;
VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
: VerificationKey_(circuit_size, num_public_inputs)
{}

VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
: VerificationKey_(proving_key->circuit_size, proving_key->num_public_inputs)
Expand Down
9 changes: 4 additions & 5 deletions bb-pilcom/bb-pil-backend/templates/verifier.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace bb {

{{name}}Verifier::{{name}}Verifier(std::shared_ptr<Flavor::VerificationKey> verifier_key)
: key(verifier_key)
, pcs_verification_key(std::make_shared<VerifierCommitmentKey>())
: key(std::move(verifier_key))
{}

{{name}}Verifier::{{name}}Verifier({{name}}Verifier&& other) noexcept
Expand All @@ -22,7 +21,7 @@ namespace bb {
{{name}}Verifier& {{name}}Verifier::operator=({{name}}Verifier&& other) noexcept
{
key = other.key;
pcs_verification_key = (std::move(other.pcs_verification_key));
pcs_verification_key = other.pcs_verification_key;
commitments.clear();
return *this;
}
Expand Down Expand Up @@ -125,11 +124,11 @@ bool {{name}}Verifier::verify_proof(const HonkProof& proof, [[maybe_unused]] con
claimed_evaluations.get_unshifted(),
claimed_evaluations.get_shifted(),
multivariate_challenge,
pcs_verification_key->get_g1_identity(),
pcs_verification_key.get_g1_identity(),
transcript);

auto pairing_points = PCS::reduce_verify(opening_claim, transcript);
auto verified = pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);
auto verified = pcs_verification_key.pairing_check(pairing_points[0], pairing_points[1]);
return sumcheck_verified.value() && verified;
}

Expand Down
2 changes: 1 addition & 1 deletion bb-pilcom/bb-pil-backend/templates/verifier.hpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class {{name}}Verifier {

std::shared_ptr<VerificationKey> key;
std::map<std::string, Commitment> commitments;
std::shared_ptr<VerifierCommitmentKey> pcs_verification_key;
VerifierCommitmentKey pcs_verification_key;
std::shared_ptr<Transcript> transcript;
};

Expand Down
3 changes: 2 additions & 1 deletion yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ const proveAndVerifyAvmTestContract = async (
// - 16 affine elements (curve base field fq) encoded as fr elements takes (16 * 4 * 32 bytes)
// 16 above refers to the constant AvmFlavor::NUM_PRECOMPUTED_ENTITIES
// Total number of bytes = 2112
expect(verificationKey.keyAsBytes).toHaveLength(2112);
const NUM_PRECOMPUTED_ENTITIES = 16;
expect(verificationKey.keyAsBytes).toHaveLength(NUM_PRECOMPUTED_ENTITIES * 4 * 32 + 2 * 32);

// Then we verify.
const rawVkPath = path.join(succeededRes.vkPath!, 'vk');
Expand Down

0 comments on commit 1f9f846

Please sign in to comment.