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
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/api/api_ultra_honk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ bool UltraHonkAPI::verify(const Flags& flags,
if (auto json = try_parse_json(public_inputs_content)) {
public_inputs = PublicInputsJson::parse(*json);
} else {
public_inputs = many_from_buffer<uint256_t>(public_inputs_content);
public_inputs = many_from_buffer_exact<uint256_t>(public_inputs_content, "UltraHonk public inputs file");
}

auto proof_content = read_file(proof_path);
if (auto json = try_parse_json(proof_content)) {
proof = ProofJson::parse(*json);
} else {
proof = many_from_buffer<uint256_t>(proof_content);
proof = many_from_buffer_exact<uint256_t>(proof_content, "UltraHonk proof file");
}

auto vk_content = read_file(vk_path);
Expand Down
12 changes: 12 additions & 0 deletions barretenberg/cpp/src/barretenberg/api/file_io.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "barretenberg/common/log.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/common/try_catch_shim.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include <cstdint>
Expand All @@ -9,6 +10,7 @@
#include <fstream>
#include <ios>
#include <sstream>
#include <string_view>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
Expand Down Expand Up @@ -153,6 +155,16 @@ inline std::vector<uint8_t> read_vk_file(const std::filesystem::path& vk_path)
}
}

template <typename T>
inline std::vector<T> many_from_buffer_exact(const std::vector<uint8_t>& buffer, std::string_view object_name)
{
if (buffer.size() % sizeof(T) != 0) {
THROW std::runtime_error(std::string(object_name) + " size must be a multiple of " + std::to_string(sizeof(T)) +
" bytes, got " + std::to_string(buffer.size()));
}
return ::many_from_buffer<T>(buffer);
}

// On Windows, std::filesystem::path uses wide strings (wchar_t) and doesn't implicitly convert
// to std::string. On Linux/macOS (libstdc++), the conversion is implicit so these aren't needed.
#ifdef _WIN32
Expand Down
25 changes: 25 additions & 0 deletions barretenberg/cpp/src/barretenberg/api/file_io.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "barretenberg/api/file_io.hpp"
#include "barretenberg/common/assert.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include <gtest/gtest.h>

using namespace bb;

TEST(APIFileIO, ManyFromBufferExactRejectsTrailingBytes)
{
std::vector<uint8_t> bytes(sizeof(uint256_t) + 1, 0);

EXPECT_THROW_WITH_MESSAGE((many_from_buffer_exact<uint256_t>(bytes, "UltraHonk proof file")),
"UltraHonk proof file size must be a multiple of 32 bytes, got 33");
}

TEST(APIFileIO, ManyFromBufferExactAcceptsAlignedBuffers)
{
std::vector<uint256_t> expected{ uint256_t(1), uint256_t(2) };
auto bytes = to_buffer(expected);

auto parsed = many_from_buffer_exact<uint256_t>(bytes, "UltraHonk proof file");

EXPECT_EQ(parsed, expected);
}
Loading