Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,38 @@ class ClientIVC {
return buffer;
}

/**
* @brief Very quirky method to convert a msgpack buffer to a "heap" buffer
* @details This method results in a buffer that is double-size-prefixed with the buffer size. This is to mimmic
* the original bb.js behavior which did a *out_proof = to_heap_buffer(to_buffer(proof));
*
* @return uint8_t* Double size-prefixed msgpack buffer
*/
uint8_t* to_msgpack_heap_buffer() const
{
msgpack::sbuffer buffer = to_msgpack_buffer();

std::vector<uint8_t> buf(buffer.data(), buffer.data() + buffer.size());
return to_heap_buffer(buf);
}

class DeserializationError : public std::runtime_error {
public:
DeserializationError(const std::string& msg)
: std::runtime_error(std::string("Client IVC Proof deserialization error: ") + msg)
{}
};

static Proof from_msgpack_buffer(uint8_t const*& buffer)
{
auto uint8_buffer = from_buffer<std::vector<uint8_t>>(buffer);

msgpack::sbuffer sbuf;
sbuf.write(reinterpret_cast<char*>(uint8_buffer.data()), uint8_buffer.size());

return from_msgpack_buffer(sbuf);
}

static Proof from_msgpack_buffer(const msgpack::sbuffer& buffer)
{
msgpack::object_handle oh = msgpack::unpack(buffer.data(), buffer.size());
Expand Down
28 changes: 28 additions & 0 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,34 @@ TEST_F(ClientIVCTests, MsgpackProofFromFile)
EXPECT_TRUE(ivc.verify(proof_deserialized));
};

/**
* @brief Test methods for serializing and deserializing a proof to/from a "heap" buffer in msgpack format
*
*/
TEST_F(ClientIVCTests, MsgpackProofFromBuffer)
{
ClientIVC ivc;

ClientIVCMockCircuitProducer circuit_producer;

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

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

const auto proof = ivc.prove();

// Serialize/deserialize proof to/from a heap buffer, check that it verifies
uint8_t const* buffer = proof.to_msgpack_heap_buffer();
auto uint8_buffer = from_buffer<std::vector<uint8_t>>(buffer);
uint8_t const* uint8_ptr = uint8_buffer.data();
auto proof_deserialized = ClientIVC::Proof::from_msgpack_buffer(uint8_ptr);
EXPECT_TRUE(ivc.verify(proof_deserialized));
};

/**
* @brief Check that a CIVC proof can be serialized and deserialized via msgpack and that attempting to deserialize a
* random buffer of bytes fails gracefully with a type error
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ WASM_EXPORT void acir_prove_aztec_client(uint8_t const* acir_stack,
vinfo("time to construct, accumulate, prove all circuits: ", diff.count());

start = std::chrono::steady_clock::now();
*out_proof = to_heap_buffer(to_buffer(proof));
*out_proof = proof.to_msgpack_heap_buffer();
end = std::chrono::steady_clock::now();
diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
vinfo("time to serialize proof: ", diff.count());
Expand All @@ -290,7 +290,7 @@ WASM_EXPORT void acir_prove_aztec_client(uint8_t const* acir_stack,

WASM_EXPORT void acir_verify_aztec_client(uint8_t const* proof_buf, uint8_t const* vk_buf, bool* result)
{
const auto proof = from_buffer<ClientIVC::Proof>(from_buffer<std::vector<uint8_t>>(proof_buf));
const auto proof = ClientIVC::Proof::from_msgpack_buffer(proof_buf);
const auto vk = from_buffer<ClientIVC::VerificationKey>(from_buffer<std::vector<uint8_t>>(vk_buf));

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1335): Should be able to remove this.
Expand Down