Skip to content

Commit

Permalink
serialisation and deserialisation work
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Jun 7, 2024
1 parent df3dcb8 commit 729c7fc
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ set -eu
VFLAG=${VERBOSE:+-v}

$BIN client_ivc_prove_output_all $VFLAG -c $CRS_PATH -b ./target/program.json
$BIN prove_tube -k vk -p proof -c $CRS_PATH $VFLAG
2 changes: 1 addition & 1 deletion barretenberg/cpp/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"displayName": "Debugging build with Clang-16",
"description": "Build with globally installed Clang-16 in debug mode",
"inherits": "clang16",
"binaryDir": "build-debug",
"binaryDir": "build",
"environment": {
"CMAKE_BUILD_TYPE": "Debug",
"CFLAGS": "-gdwarf-4",
Expand Down
52 changes: 28 additions & 24 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,29 +317,18 @@ void client_ivc_prove_output_all(const std::string& bytecodePath,
auto inst_vk = ivc.instance_vk;
auto eccvm_vk = std::make_shared<ECCVMVK>(ivc.goblin.get_eccvm_proving_key());
auto translator_vk = std::make_shared<TranslatorVK>(ivc.goblin.get_translator_proving_key());
// LONDONTODO: we can remove this

auto last_instance = std::make_shared<ClientIVC::VerifierInstance>(inst_vk);
info("ensure valid proof: ", ivc.verify(proof, { accumulator, last_instance }));
auto buffer_proof = proof.to_buffer();
write_file(proofPath, to_buffer(buffer_proof));

write_file(proofPath, to_buffer(proof));

write_file(vkPath, to_buffer(inst_vk)); // maybe dereference
auto buffer_acc = accumulator->to_buffer();
write_file(accPath, to_buffer(buffer_acc));
write_file(accPath, to_buffer(accumulator));

write_file(translatorVkPath, to_buffer(translator_vk));

write_file(eccVkPath, to_buffer(eccvm_vk));

std::string proofJson = to_json(buffer_proof);
write_file(proofFieldsPath, { proofJson.begin(), proofJson.end() });

auto inst_vk_as_fields = inst_vk->to_field_elements();
std::string vk_json = to_json(inst_vk_as_fields);
write_file(vkFieldsPath, { vk_json.begin(), vk_json.end() });

std::string acc_json = to_json(buffer_acc);
write_file(accFieldsPath, { acc_json.begin(), acc_json.end() });
}

/**
Expand All @@ -359,30 +348,41 @@ bool prove_tube(const std::string& outputPath)
using GoblinVerifierInput = ClientIVC::GoblinVerifierInput;
using VerifierInput = ClientIVC::VerifierInput;
using Builder = UltraCircuitBuilder;
using GrumpkinVk = bb::VerifierCommitmentKey<curve::Grumpkin>;

std::string vkPath = outputPath + "/vk"; // the vk of the last instance
std::string vkPath = outputPath + "/inst_vk"; // the vk of the last instance
std::string accPath = outputPath + "/pg_acc";
std::string proofPath = outputPath + "/proof";
std::string translatorVkPath = outputPath + "/translatorVk";
std::string eccVkPath = outputPath + "/eccVk";
std::string vkFieldsPath = outputPath + "/vk_fields.json";
std::string proofFieldsPath = outputPath + "/proof_fields.json";
std::string accFieldsPath = outputPath + "/pg_acc_fields.json";
std::string translatorVkPath = outputPath + "/translator_vk";
std::string eccVkPath = outputPath + "/ecc_vk";
// std::string vkFieldsPath = outputPath + "/vk_fields.json";
init_bn254_crs(1 << 20);
init_grumpkin_crs(1 << 16); // is this even enough?

auto proof = from_buffer<std::vector<bb::fr>>(read_file(proofPath));
auto proof = from_buffer<ClientIVC::Proof>(read_file(proofPath));
auto instance_vk = std::make_shared<InstanceFlavor::VerificationKey>(
from_buffer<InstanceFlavor::VerificationKey>(read_file(vkPath)));
auto verifier_accumulator = std::make_shared<NativeInstance>(from_buffer<NativeInstance>(read_file(accPath)));
auto translator_vk = std::make_shared<TranslatorVk>(from_buffer<TranslatorVk>(read_file(translatorVkPath)));
auto eccvm_vk = std::make_shared<ECCVMVk>(from_buffer<ECCVMVk>(read_file(eccVkPath)));
auto translator_vk = std::make_shared<TranslatorVk>(from_buffer<TranslatorVk>(translatorVkPath));

eccvm_vk->pcs_verification_key = std::make_shared<GrumpkinVk>(1 << 16);
FoldVerifierInput fold_verifier_input{ verifier_accumulator, { instance_vk } };
GoblinVerifierInput goblin_verifier_input{ eccvm_vk, translator_vk };
VerifierInput input{ fold_verifier_input, goblin_verifier_input };
auto builder = std::make_shared<Builder>();
ClientIVC verifier{ builder, input };

verifier.verify(proof);
info("num gates: ", builder->get_num_gates());
info("generating proof");
using Prover = UltraProver_<UltraFlavor>;
Prover prover{ *builder };
auto tube_proof = prover.construct_proof();
std::string tubeProofPath = outputPath + "/tube_proof";
write_file(tubeProofPath, to_buffer(tube_proof));

auto verification_key = std::make_shared<typename UltraFlavor::VerificationKey>(prover.key);

return true;
}

Expand Down Expand Up @@ -997,6 +997,10 @@ int main(int argc, char* argv[])
std::string output_path = get_option(args, "-o", "./proofs");
info(output_path);
client_ivc_prove_output_all(bytecode_path, witness_path, output_path);
} else if (command == "prove_tube") {
std::string output_path = get_option(args, "-o", "./proofs");
info(output_path);
prove_tube(output_path);
} else if (command == "gates") {
gateCount(bytecode_path, honk_recursion);
} else if (command == "verify") {
Expand Down
17 changes: 17 additions & 0 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ ClientIVC::Proof ClientIVC::prove()
* @param proof
* @return bool
*/
bool ClientIVC::verify_special(Proof& proof,
std::shared_ptr<ECCVMVerificationKey> eccvm_vkey,
std::shared_ptr<TranslatorVerificationKey> translator_vkey,
const std::vector<std::shared_ptr<VerifierInstance>>& verifier_instances)
{
// Goblin verification (merge, eccvm, translator)
GoblinVerifier goblin_verifier{ eccvm_vkey, translator_vkey };
bool goblin_verified = goblin_verifier.verify(proof.goblin_proof);

// Decider verification
ClientIVC::FoldingVerifier folding_verifier({ verifier_instances[0], verifier_instances[1] });
auto verifier_accumulator = folding_verifier.verify_folding_proof(proof.folding_proof);

ClientIVC::DeciderVerifier decider_verifier(verifier_accumulator);
bool decision = decider_verifier.verify_proof(proof.decider_proof);
return goblin_verified && decision;
}
bool ClientIVC::verify(Proof& proof, const std::vector<std::shared_ptr<VerifierInstance>>& verifier_instances)
{
// Goblin verification (merge, eccvm, translator)
Expand Down
34 changes: 20 additions & 14 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ class ClientIVC {
*
* @return std::vector<FF>
*/
std::vector<FF> to_buffer() const
{
size_t proof_size = folding_proof.size() + decider_proof.size() + goblin_proof.size();

std::vector<FF> result;
result.reserve(proof_size);
const auto insert = [&result](const std::vector<FF>& buf) {
result.insert(result.end(), buf.begin(), buf.end());
};
insert(folding_proof);
insert(decider_proof);
insert(goblin_proof.to_buffer());
return result;
}
// std::vector<FF> to_buffer() const
// {
// size_t proof_size = folding_proof.size() + decider_proof.size() + goblin_proof.size();

// std::vector<FF> result;
// result.reserve(proof_size);
// const auto insert = [&result](const std::vector<FF>& buf) {
// result.insert(result.end(), buf.begin(), buf.end());
// };
// insert(folding_proof);
// insert(decider_proof);
// insert(goblin_proof.to_buffer());
// return result;
// }

MSGPACK_FIELDS(folding_proof, decider_proof, goblin_proof);
};

private:
Expand Down Expand Up @@ -94,6 +96,10 @@ class ClientIVC {
Proof prove();

bool verify(Proof& proof, const std::vector<std::shared_ptr<VerifierInstance>>& verifier_instances);
bool verify_special(Proof& proof,
std::shared_ptr<ECCVMVerificationKey> eccvm_vkey,
std::shared_ptr<TranslatorVerificationKey> translator_vkey,
const std::vector<std::shared_ptr<VerifierInstance>>& verifier_instances);

bool prove_and_verify();

Expand Down
23 changes: 23 additions & 0 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "barretenberg/client_ivc/client_ivc.hpp"
#include "barretenberg/bb/file_io.hpp"
#include "barretenberg/goblin/goblin.hpp"
#include "barretenberg/goblin/mock_circuits.hpp"
#include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp"
Expand Down Expand Up @@ -213,3 +214,25 @@ TEST_F(ClientIVCTests, StructuredPrecomputedVKs)

EXPECT_TRUE(prove_and_verify(ivc));
};

TEST_F(ClientIVCTests, Proof)
{
ClientIVC ivc;

// Initialize the IVC with an arbitrary circuit
Builder circuit_0 = create_mock_circuit(ivc);
ivc.accumulate(circuit_0);

// Create another circuit and accumulate
Builder circuit_1 = create_mock_circuit(ivc);
ivc.accumulate(circuit_1);

auto proof = ivc.prove();
write_file("./proof", to_buffer(proof));
auto reconstructed_proof = from_buffer<ClientIVC::Proof>(read_file("./proof"));
EXPECT_EQ(proof.folding_proof.size(), reconstructed_proof.folding_proof.size());
EXPECT_EQ(proof.decider_proof.size(), reconstructed_proof.decider_proof.size());
EXPECT_EQ(proof.goblin_proof.size(), reconstructed_proof.goblin_proof.size());
auto verifier_inst = std::make_shared<VerifierInstance>(ivc.instance_vk);
EXPECT_TRUE(ivc.verify(reconstructed_proof, { ivc.verifier_accumulator, verifier_inst }));
}
10 changes: 8 additions & 2 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ class ECCVMFlavor {
*/
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)
{}
Expand All @@ -682,8 +683,13 @@ class ECCVMFlavor {
}
}

MSGPACK_FIELDS(
circuit_size, num_public_inputs, pub_inputs_offset, lagrange_first, lagrange_second, lagrange_last);
MSGPACK_FIELDS(circuit_size,
log_circuit_size,
num_public_inputs,
pub_inputs_offset,
lagrange_first,
lagrange_second,
lagrange_last);
};

/**
Expand Down
3 changes: 3 additions & 0 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ bool ECCVMVerifier::verify_proof(const HonkProof& proof)

for (auto [comm, label] : zip_view(commitments.get_wires(), commitment_labels.get_wires())) {
comm = transcript->template receive_from_prover<Commitment>(label);
if (!comm.on_curve()) {
comm.self_set_infinity();
}
}

// Get challenge for sorted list batching and wire four memory records
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ template <typename BF, typename FF> struct TranslationEvaluations_ {
BF op, Px, Py, z1, z2;
static constexpr uint32_t NUM_EVALUATIONS = 5;
static size_t size() { return field_conversion::calc_num_bn254_frs<BF>() * NUM_EVALUATIONS; }
std::vector<FF> to_buffer() const
{
std::vector<FF> result;
result.reserve(size());
const auto insert = [&result](const BF& elt) {
std::vector<FF> buf = field_conversion::convert_to_bn254_frs(elt);
result.insert(result.end(), buf.begin(), buf.end());
};
insert(op);
insert(Px);
insert(Py);
insert(z1);
insert(z2);
return result;
}
// std::vector<FF> to_buffer() const
// {
// std::vector<FF> result;
// result.reserve(size());
// const auto insert = [&result](const BF& elt) {
// std::vector<FF> buf = field_conversion::convert_to_bn254_frs(elt);
// result.insert(result.end(), buf.begin(), buf.end());
// };
// insert(op);
// insert(Px);
// insert(Py);
// insert(z1);
// insert(z2);
// return result;
// }

MSGPACK_FIELDS(op, Px, Py, z1, z2);
};
} // namespace bb
30 changes: 16 additions & 14 deletions barretenberg/cpp/src/barretenberg/goblin/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ struct GoblinProof {
return merge_proof.size() + eccvm_proof.size() + translator_proof.size() + TranslationEvaluations::size();
};

std::vector<FF> to_buffer() const
{
// ACIRHACK: so much copying and duplication added here and elsewhere
std::vector<FF> result;
result.reserve(size());
const auto insert = [&result](const std::vector<FF>& buf) {
result.insert(result.end(), buf.begin(), buf.end());
};
insert(merge_proof);
insert(eccvm_proof);
insert(translator_proof);
insert(translation_evaluations.to_buffer());
return result;
}
MSGPACK_FIELDS(merge_proof, eccvm_proof, translator_proof, translation_evaluations);

// std::vector<FF> to_buffer() const
// {
// // ACIRHACK: so much copying and duplication added here and elsewhere
// std::vector<FF> result;
// result.reserve(size());
// const auto insert = [&result](const std::vector<FF>& buf) {
// result.insert(result.end(), buf.begin(), buf.end());
// };
// insert(merge_proof);
// insert(eccvm_proof);
// insert(translator_proof);
// insert(translation_evaluations.to_buffer());
// return result;
// }
};
} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ClientIVCRecursiveVerifier {
using GoblinVerifier = GoblinRecursiveVerifier;

public:
using Proof = ClientIVC::Proof;
using FoldVerifierInput = FoldingVerifier::VerifierInput;
using GoblinVerifierInput = GoblinVerifier::VerifierInput;
struct VerifierInput {
Expand Down
Loading

0 comments on commit 729c7fc

Please sign in to comment.