Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Format msgpack serialization in bberg #467

Merged
merged 1 commit into from
May 22, 2023
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
137 changes: 70 additions & 67 deletions cpp/src/barretenberg/common/fuzzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,80 +84,83 @@ class FastRandom {
*
* @tparam T
*/
template <typename T>
concept SimpleRng = requires(T a) {
{
a.next()
} -> std::convertible_to<uint32_t>;
};
template <typename T> concept SimpleRng = requires(T a)
{
{
a.next()
}
->std::convertible_to<uint32_t>;
};
/**
* @brief Concept for forcing ArgumentSizes to be size_t
*
* @tparam T
*/
template <typename T>
concept InstructionArgumentSizes = requires {
{
std::make_tuple(T::CONSTANT,
T::WITNESS,
T::CONSTANT_WITNESS,
T::ADD,
T::SUBTRACT,
T::MULTIPLY,
T::DIVIDE,
T::ADD_TWO,
T::MADD,
T::MULT_MADD,
T::MSUB_DIV,
T::SQR,
T::SQR_ADD,
T::SUBTRACT_WITH_CONSTRAINT,
T::DIVIDE_WITH_CONSTRAINTS,
T::SLICE,
T::ASSERT_ZERO,
T::ASSERT_NOT_ZERO)
} -> std::same_as<std::tuple<size_t>>;
};
template <typename T> concept InstructionArgumentSizes = requires
{
{
std::make_tuple(T::CONSTANT,
T::WITNESS,
T::CONSTANT_WITNESS,
T::ADD,
T::SUBTRACT,
T::MULTIPLY,
T::DIVIDE,
T::ADD_TWO,
T::MADD,
T::MULT_MADD,
T::MSUB_DIV,
T::SQR,
T::SQR_ADD,
T::SUBTRACT_WITH_CONSTRAINT,
T::DIVIDE_WITH_CONSTRAINTS,
T::SLICE,
T::ASSERT_ZERO,
T::ASSERT_NOT_ZERO)
}
->std::same_as<std::tuple<size_t>>;
};

/**
* @brief Concept for Havoc Configurations
*
* @tparam T
*/
template <typename T>
concept HavocConfigConstraint =
requires {
{
std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY)
} -> std::same_as<std::tuple<size_t>>;
T::GEN_MUTATION_COUNT_LOG <= 7;
};
template <typename T> concept HavocConfigConstraint = requires
{
{
std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY)
}
->std::same_as<std::tuple<size_t>>;
T::GEN_MUTATION_COUNT_LOG <= 7;
};
/**
* @brief Concept specifying the class used by the fuzzer
*
* @tparam T
*/
template <typename T>
concept ArithmeticFuzzHelperConstraint = requires {
typename T::ArgSizes;
typename T::Instruction;
typename T::ExecutionState;
typename T::ExecutionHandler;
InstructionArgumentSizes<typename T::ArgSizes>;
// HavocConfigConstraint<typename T::HavocConfig>;
};
template <typename T> concept ArithmeticFuzzHelperConstraint = requires
{
typename T::ArgSizes;
typename T::Instruction;
typename T::ExecutionState;
typename T::ExecutionHandler;
InstructionArgumentSizes<typename T::ArgSizes>;
// HavocConfigConstraint<typename T::HavocConfig>;
};

/**
* @brief Fuzzer uses only composers with check_circuit function
*
* @tparam T
*/
template <typename T>
concept CheckableComposer = requires(T a) {
{
a.check_circuit()
} -> std::same_as<bool>;
};
template <typename T> concept CheckableComposer = requires(T a)
{
{
a.check_circuit()
}
->std::same_as<bool>;
};

/**
* @brief The fuzzer can use a postprocessing function that is specific to the type being fuzzed
Expand All @@ -167,31 +170,31 @@ concept CheckableComposer = requires(T a) {
* @tparam Context The class containing the full context
*/
template <typename T, typename Composer, typename Context>
concept PostProcessingEnabled = requires(Composer composer, Context context) {
{
T::postProcess(&composer, context)
} -> std::same_as<bool>;
};
concept PostProcessingEnabled = requires(Composer composer, Context context)
{
{
T::postProcess(&composer, context)
}
->std::same_as<bool>;
};

/**
* @brief This concept is used when we want to limit the number of executions of certain instructions (for example,
* divisions and multiplications in bigfield start to bog down the fuzzer)
*
* @tparam T
*/
template <typename T>
concept InstructionWeightsEnabled = requires {
typename T::InstructionWeights;
T::InstructionWeights::_LIMIT;
};
template <typename T> concept InstructionWeightsEnabled = requires
{
typename T::InstructionWeights;
T::InstructionWeights::_LIMIT;
};
/**
* @brief A templated class containing most of the fuzzing logic for a generic Arithmetic class
*
* @tparam T
*/
template <typename T>
requires ArithmeticFuzzHelperConstraint<T>
class ArithmeticFuzzHelper {
template <typename T> requires ArithmeticFuzzHelperConstraint<T> class ArithmeticFuzzHelper {
private:
/**
* @brief Mutator swapping two instructions together
Expand Down Expand Up @@ -494,8 +497,8 @@ class ArithmeticFuzzHelper {
* @param instructions
*/
template <typename Composer>
inline static void executeInstructions(std::vector<typename T::Instruction>& instructions)
requires CheckableComposer<Composer>
inline static void executeInstructions(
std::vector<typename T::Instruction>& instructions) requires CheckableComposer<Composer>
{
typename T::ExecutionState state;
Composer composer = Composer();
Expand Down
12 changes: 7 additions & 5 deletions cpp/src/barretenberg/common/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"

#define GET_COMPOSER_NAME_STRING(composer) \
(typeid(composer) == typeid(plonk::StandardComposer) ? "StandardPlonk" \
: typeid(composer) == typeid(plonk::TurboComposer) ? "TurboPlonk" \
: typeid(composer) == typeid(plonk::UltraComposer) ? "UltraPlonk" \
: typeid(composer) == typeid(honk::StandardHonkComposer) ? "StandardHonk" \
: "NULLPlonk")
(typeid(composer) == typeid(plonk::StandardComposer) \
? "StandardPlonk" \
: typeid(composer) == typeid(plonk::TurboComposer) \
? "TurboPlonk" \
: typeid(composer) == typeid(plonk::UltraComposer) \
? "UltraPlonk" \
: typeid(composer) == typeid(honk::StandardHonkComposer) ? "StandardHonk" : "NULLPlonk")

namespace {

Expand Down
4 changes: 2 additions & 2 deletions cpp/src/barretenberg/crypto/hmac/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ std::array<uint8_t, Hash::OUTPUT_SIZE> hmac(const MessageContainer& message, con
* @return Fr output field element as uint512_t( H(10...0 || HMAC(k,m)) || H(00...0 || HMAC(k,m)) ) % r
*/
template <typename Hash, typename Fr, typename MessageContainer, typename KeyContainer>
Fr get_unbiased_field_from_hmac(const MessageContainer& message, const KeyContainer& key)
requires(Hash::OUTPUT_SIZE == 32)
Fr get_unbiased_field_from_hmac(const MessageContainer& message,
const KeyContainer& key) requires(Hash::OUTPUT_SIZE == 32)
{
// Strong assumption that works for now with our suite of Hashers
static_assert(Hash::BLOCK_SIZE > Hash::OUTPUT_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ecdsa_secp256k1.hpp"
TEST(acir_format, msgpack_logic_constraint)
{
auto [actual, expected] = msgpack_roundtrip(acir_format::LogicConstraint {});
auto [actual, expected] = msgpack_roundtrip(acir_format::LogicConstraint{});
EXPECT_EQ(actual, expected);
}
TEST(acir_format, test_logic_gate_from_noir_circuit)
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/ecc/curves/bn254/fq.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace barretenberg;

TEST(fq, msgpack)
{
auto [actual, expected] = msgpack_roundtrip(barretenberg::fq{1ull, 2ull, 3ull, 4ull});
auto [actual, expected] = msgpack_roundtrip(barretenberg::fq{ 1ull, 2ull, 3ull, 4ull });
EXPECT_EQ(actual, expected);
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace barretenberg;

TEST(fr, msgpack)
{
auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{1ull, 2ull, 3ull, 4ull});
auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{ 1ull, 2ull, 3ull, 4ull });
EXPECT_EQ(actual, expected);
}

Expand Down
20 changes: 10 additions & 10 deletions cpp/src/barretenberg/ecc/fields/field_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,16 @@ template <class T> constexpr field<T> field<T>::multiplicative_generator() noexc
return target;
}

// This function is used to serialize a field. It matches the old serialization format by first
// converting the field from Montgomery form, which is a special representation used for efficient
// This function is used to serialize a field. It matches the old serialization format by first
// converting the field from Montgomery form, which is a special representation used for efficient
// modular arithmetic.
template <class Params> void field<Params>::msgpack_pack(auto& packer) const
{
// The field is first converted from Montgomery form, similar to how the old format did it.
auto adjusted = from_montgomery_form();

// The data is then converted to big endian format using htonll, which stands for "host to network long long".
// This is necessary because the data will be written to a raw msgpack buffer, which requires big endian format.
// The data is then converted to big endian format using htonll, which stands for "host to network long long".
// This is necessary because the data will be written to a raw msgpack buffer, which requires big endian format.
uint64_t bin_data[4] = {
htonll(adjusted.data[3]), htonll(adjusted.data[2]), htonll(adjusted.data[1]), htonll(adjusted.data[0])
};
Expand All @@ -625,22 +625,22 @@ template <class Params> void field<Params>::msgpack_pack(auto& packer) const
packer.pack_bin_body((const char*)bin_data, sizeof(bin_data));
}

// This function is used to deserialize a field. It also matches the old deserialization format by
// reading the binary data as big endian uint64_t's, correcting them to the host endianness, and
// then converting the field back to Montgomery form.
// This function is used to deserialize a field. It also matches the old deserialization format by
// reading the binary data as big endian uint64_t's, correcting them to the host endianness, and
// then converting the field back to Montgomery form.
template <class Params> void field<Params>::msgpack_unpack(auto o)
{
// The binary data is first extracted from the msgpack object.
std::array<uint8_t, sizeof(data)> raw_data = o;

// The binary data is then read as big endian uint64_t's. This is done by casting the raw data to uint64_t* and then
// The binary data is then read as big endian uint64_t's. This is done by casting the raw data to uint64_t* and then
// using ntohll ("network to host long long") to correct the endianness to the host's endianness.
uint64_t* cast_data = (uint64_t*)&raw_data[0];
uint64_t reversed[] = {ntohll(cast_data[3]), ntohll(cast_data[2]), ntohll(cast_data[1]), ntohll(cast_data[0])};
uint64_t reversed[] = { ntohll(cast_data[3]), ntohll(cast_data[2]), ntohll(cast_data[1]), ntohll(cast_data[0]) };

// The corrected data is then copied back into the field's data array.
for (int i = 0; i < 4; i++) {
data[i] = reversed[i];
data[i] = reversed[i];
}

// Finally, the field is converted back to Montgomery form, just like in the old format.
Expand Down
Loading