4#include "barretenberg/bbapi/generated/bb_types.hpp"
10#include "msgpack/v3/sbuffer_decl.hpp"
11#include <gtest/gtest.h>
17template <
typename Cmd,
typename Resp>
struct WirePair {
18 using CommandType = Cmd;
19 using ResponseType = Resp;
28 WirePair<bbapi::wire::CircuitProve, bbapi::wire::CircuitProveResponse>,
29 WirePair<bbapi::wire::CircuitComputeVk, bbapi::wire::CircuitComputeVkResponse>,
30 WirePair<bbapi::wire::CircuitStats, bbapi::wire::CircuitInfoResponse>,
31 WirePair<bbapi::wire::CircuitVerify, bbapi::wire::CircuitVerifyResponse>,
32 WirePair<bbapi::wire::VkAsFields, bbapi::wire::VkAsFieldsResponse>,
33 WirePair<bbapi::wire::CircuitWriteSolidityVerifier, bbapi::wire::CircuitWriteSolidityVerifierResponse>,
34 WirePair<bbapi::wire::ChonkStart, bbapi::wire::ChonkStartResponse>,
35 WirePair<bbapi::wire::ChonkLoad, bbapi::wire::ChonkLoadResponse>,
36 WirePair<bbapi::wire::ChonkAccumulate, bbapi::wire::ChonkAccumulateResponse>,
37 WirePair<bbapi::wire::ChonkProve, bbapi::wire::ChonkProveResponse>,
38 WirePair<bbapi::wire::ChonkComputeVk, bbapi::wire::ChonkComputeVkResponse>,
39 WirePair<bbapi::wire::ChonkCheckPrecomputedVk, bbapi::wire::ChonkCheckPrecomputedVkResponse>,
40 WirePair<bbapi::wire::ChonkBatchVerify, bbapi::wire::ChonkBatchVerifyResponse>>;
42template <
typename T>
class BBApiMsgpack :
public ::testing::Test {};
48 typename TypeParam::CommandType command{};
50 EXPECT_EQ(actual_command, expected_command);
52 typename TypeParam::ResponseType response{};
54 EXPECT_EQ(actual_response, expected_response);
60TEST(BBApiInputValidation, NonCanonicalPublicInputRejected)
68 EXPECT_THROW(bbapi::concatenate_proof<Flavor>(bad_public_inputs, proof), std::runtime_error);
71TEST(BBApiInputValidation, NonCanonicalProofElementRejected)
79 EXPECT_THROW(bbapi::concatenate_proof<Flavor>(public_inputs, bad_proof), std::runtime_error);
82TEST(BBApiInputValidation, CanonicalValuesAccepted)
90 EXPECT_NO_THROW(bbapi::concatenate_proof<Flavor>(public_inputs, proof));
93TEST(BBApiInputValidation, TrailingBytesInBinaryInputRejected)
96 std::vector<uint8_t> buf(32 + 1, 0);
97 EXPECT_THROW(many_from_buffer_exact<uint256_t>(buf,
"test input"), std::runtime_error);
100TEST(BBApiInputValidation, ExactBinaryInputAccepted)
103 std::vector<uint8_t> buf(64, 0);
104 EXPECT_NO_THROW(many_from_buffer_exact<uint256_t>(buf,
"test input"));
105 auto result = many_from_buffer_exact<uint256_t>(buf,
"test input");
106 EXPECT_EQ(result.size(), 2UL);
109TEST(BBApiInputValidation, VkWithTrailingBytesRejectedOnProveSide)
112 const size_t expected_size = VK::calc_num_data_types() *
sizeof(
bb::fr);
114 std::vector<uint8_t> bad_vk(expected_size + 1, 0);
115 EXPECT_THROW(bbapi::validate_vk_size<VK>(bad_vk), std::runtime_error);
118TEST(BBApiInputValidation, VkWithCorrectSizeAccepted)
121 const size_t expected_size = VK::calc_num_data_types() *
sizeof(
bb::fr);
122 std::vector<uint8_t> good_vk(expected_size, 0);
123 EXPECT_NO_THROW(bbapi::validate_vk_size<VK>(good_vk));
126TEST(BBApiInputValidation, ChonkVerifyWrongVkSizeReturnsInvalid)
129 auto response = bbapi::handle_chonk_verify(request, bbapi::wire::ChonkVerify{ .proof = {}, .vk = { 0 } });
130 EXPECT_FALSE(response.valid);
133TEST(BBApiInputValidation, ChonkVerifyFromFieldsWrongVkSizeReturnsInvalid)
137 bbapi::handle_chonk_verify_from_fields(request, bbapi::wire::ChonkVerifyFromFields{ .proof = {}, .vk = { 0 } });
138 EXPECT_FALSE(response.valid);
141TEST(BBApiInputValidation, ChonkBatchVerifyWrongVkSizeReturnsInvalid)
144 auto response = bbapi::handle_chonk_batch_verify(request,
145 bbapi::wire::ChonkBatchVerify{
146 .proofs = { bbapi::wire::ChonkProof{} },
149 EXPECT_FALSE(response.valid);
156 std::stringstream ss;
157 msgpack::pack(ss, steps);
158 const std::string s = ss.str();
159 return { s.begin(), s.end() };
163TEST(BBApiInputValidation, MsgpackParseUncompressedAcceptsCleanInput)
166 .
bytecode = { 0xCA, 0xFE }, .witness = { 0xBE, 0xEF }, .vk = {}, .function_name =
"test_fn"
169 auto buf = pack_steps({ step });
172 ASSERT_EQ(result.size(), 1);
173 EXPECT_EQ(result[0].
bytecode, step.bytecode);
174 EXPECT_EQ(result[0].witness, step.witness);
175 EXPECT_EQ(result[0].function_name,
"test_fn");
178TEST(BBApiInputValidation, MsgpackParseUncompressedRejectsTrailingData)
182 auto buf = pack_steps({ step });
188TEST(BBApiInputValidation, MsgpackLoadAcceptsCleanFile)
192 auto buf = pack_steps({ step });
194 auto tmp = std::filesystem::temp_directory_path() /
"bb_test_clean.msgpack";
195 std::ofstream out(tmp, std::ios::binary);
196 out.write(
reinterpret_cast<const char*
>(buf.data()),
static_cast<std::streamsize>(buf.size()));
200 std::filesystem::remove(tmp);
202 ASSERT_EQ(result.size(), 1);
203 EXPECT_EQ(result[0].function_name,
"file_fn");
206TEST(BBApiInputValidation, MsgpackLoadRejectsTrailingData)
210 auto buf = pack_steps({ step });
213 auto tmp = std::filesystem::temp_directory_path() /
"bb_test_tailed.msgpack";
214 std::ofstream out(tmp, std::ios::binary);
215 out.write(
reinterpret_cast<const char*
>(buf.data()),
static_cast<std::streamsize>(buf.size()));
219 std::filesystem::remove(tmp);
226TEST(BBApiInputValidation, AesEncryptRejectsLengthMismatch)
229 bbapi::wire::AesEncrypt cmd{ .plaintext = std::vector<uint8_t>(16, 0), .iv = {}, .key = {}, .length = 32 };
233TEST(BBApiInputValidation, AesEncryptRejectsNonBlockAlignedLength)
236 bbapi::wire::AesEncrypt cmd{ .plaintext = std::vector<uint8_t>(17, 0), .iv = {}, .key = {}, .length = 17 };
240TEST(BBApiInputValidation, AesDecryptRejectsLengthMismatch)
243 bbapi::wire::AesDecrypt cmd{ .ciphertext = std::vector<uint8_t>(16, 0), .iv = {}, .key = {}, .length = 32 };
247TEST(BBApiInputValidation, AesDecryptRejectsNonBlockAlignedLength)
250 bbapi::wire::AesDecrypt cmd{ .ciphertext = std::vector<uint8_t>(17, 0), .iv = {}, .key = {}, .length = 17 };
#define EXPECT_THROW_OR_ABORT(statement, matcher)
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
::testing::Types< WirePair< bbapi::wire::CircuitProve, bbapi::wire::CircuitProveResponse >, WirePair< bbapi::wire::CircuitComputeVk, bbapi::wire::CircuitComputeVkResponse >, WirePair< bbapi::wire::CircuitStats, bbapi::wire::CircuitInfoResponse >, WirePair< bbapi::wire::CircuitVerify, bbapi::wire::CircuitVerifyResponse >, WirePair< bbapi::wire::VkAsFields, bbapi::wire::VkAsFieldsResponse >, WirePair< bbapi::wire::CircuitWriteSolidityVerifier, bbapi::wire::CircuitWriteSolidityVerifierResponse >, WirePair< bbapi::wire::ChonkStart, bbapi::wire::ChonkStartResponse >, WirePair< bbapi::wire::ChonkLoad, bbapi::wire::ChonkLoadResponse >, WirePair< bbapi::wire::ChonkAccumulate, bbapi::wire::ChonkAccumulateResponse >, WirePair< bbapi::wire::ChonkProve, bbapi::wire::ChonkProveResponse >, WirePair< bbapi::wire::ChonkComputeVk, bbapi::wire::ChonkComputeVkResponse >, WirePair< bbapi::wire::ChonkCheckPrecomputedVk, bbapi::wire::ChonkCheckPrecomputedVkResponse >, WirePair< bbapi::wire::ChonkBatchVerify, bbapi::wire::ChonkBatchVerifyResponse > > WirePairs
TEST(BBApiInputValidation, NonCanonicalPublicInputRejected)
TYPED_TEST_SUITE(BBApiMsgpack, WirePairs)
TYPED_TEST(BBApiMsgpack, DefaultConstructorRoundtrip)
Non-template handler declarations for the bb service.
Shared type definitions for the Barretenberg RPC API.
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
The verification key stores commitments to the precomputed (non-witness) polynomials used by the veri...
Entry point for Barretenberg command-line interface.
field< Bn254FrParams > fr
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
This is the msgpack encoding of the objects returned by the following typescript: const stepToStruct ...
std::vector< uint8_t > bytecode
static std::vector< PrivateExecutionStepRaw > parse_uncompressed(const std::vector< uint8_t > &buf)
static std::vector< PrivateExecutionStepRaw > load(const std::filesystem::path &input_path)
static constexpr uint256_t modulus
std::pair< T, T > msgpack_roundtrip(const T &object)