Skip to content

Commit

Permalink
Added macro for throw/assert of marshalling status #316
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Aug 23, 2024
1 parent 53f63c6 commit e9dff5d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 47 deletions.
5 changes: 2 additions & 3 deletions libs/hash/include/nil/crypto3/hash/pedersen.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
Expand All @@ -26,8 +27,6 @@
#ifndef CRYPTO3_HASH_PEDERSEN_HPP
#define CRYPTO3_HASH_PEDERSEN_HPP

#include <tuple>

#include <nil/crypto3/hash/accumulators/hash.hpp>
#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/hash_state.hpp>
Expand Down Expand Up @@ -241,9 +240,9 @@ namespace nil {
static inline result_type process(internal_accumulator_type &acc) {
auto result_point = nil::crypto3::accumulators::extract::hash<base_hash_type>(acc);
nil::marshalling::status_type status;
// TODO: check status
result_type result =
nil::marshalling::pack<typename construction::params_type::digest_endian>(result_point, status);
THROW_IF_ERROR_STATUS(status, "Pedersen hash processing");
return result;
}
};
Expand Down
11 changes: 4 additions & 7 deletions libs/hash/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ set(TESTS_NAMES

if(CRYPTO3_HASH_PEDERSEN)
list(APPEND TESTS_NAMES
find_group_hash
h2c
h2f)

if(CRYPTO3_HASH_PEDERSEN)
list(APPEND TESTS_NAMES pedersen)
endif()
pedersen
find_group_hash
h2c
h2f)
endif()

foreach(TEST_NAME ${TESTS_NAMES})
Expand Down
36 changes: 0 additions & 36 deletions libs/hash/test/pedersen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,6 @@
using namespace nil::crypto3;
using namespace nil::crypto3::algebra;

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp<FieldParams> &e) {
std::cout << e.data << std::endl;
}

template<typename CurveParams, typename Form>
void print_curve_point(std::ostream &os,
const curves::detail::curve_element<CurveParams, Form, curves::coordinates::affine> &p) {
os << "( X: [";
print_field_element(os, p.X);
os << "], Y: [";
print_field_element(os, p.Y);
os << "] )" << std::endl;
}

namespace boost {
namespace test_tools {
namespace tt_detail {
template<typename CurveParams, typename Form, typename Coordinates>
struct print_log_value<curves::detail::curve_element<CurveParams, Form, Coordinates>> {
void operator()(std::ostream &os,
const curves::detail::curve_element<CurveParams, Form, Coordinates> &p) {
print_curve_point(os, p);
}
};

template<template<typename, typename> class P, typename K, typename V>
struct print_log_value<P<K, V>> {
void operator()(std::ostream &, P<K, V> const &) {
}
};

} // namespace tt_detail
} // namespace test_tools
} // namespace boost

BOOST_AUTO_TEST_SUITE(hash_pedersen_manual_test_suite)

BOOST_AUTO_TEST_CASE(hash_pedersen_jubjub_sha256_default_params_manual_test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ namespace nil {
using group_type = typename GroupAffineElement::group_type;
using group_affine_value_type = GroupAffineElement;

// TODO: throw catchable error, for example return status
if (y_int >= base_field_type::modulus) {
return nil::marshalling::status_type::invalid_msg_data;
}
Expand Down
8 changes: 8 additions & 0 deletions libs/marshalling/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ include(CMSetupVersion)

option(BUILD_WITH_NO_WARNINGS "Build threading warnings as errors" FALSE)

# When set, the code always throws on invalid data.
# If not set, the code is using BOOST_VERIFY - noop in release and stop in debug
option(CRYPTO3_MARSHALLING_THROWS "Throw exceptions when marshalling invalid data" FALSE)

if(CRYPTO3_MARSHALLING_THROWS)
add_definitions(-DCRYPTO3_MARSHALLING_THROWS)
endif()

cm_setup_version(VERSION 0.1.0 PREFIX ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME})

add_library(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE)
Expand Down
14 changes: 14 additions & 0 deletions libs/marshalling/core/include/nil/marshalling/status_type.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2017-2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2020-2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
Expand Down Expand Up @@ -114,5 +115,18 @@ inline std::error_code make_error_code(nil::marshalling::status_type e)
return {static_cast<int>(e), category};
}

#if defined(CRYPTO3_MARSHALLING_THROWS)

#define THROW_IF_ERROR_STATUS(status, message) \
if (nil::marshalling::status_type::success != status) { \
std::stringstream os; os << std::string(message) << " " << make_error_code(status); \
throw std::invalid_argument(os); \
}

#else

#define THROW_IF_ERROR_STATUS(status, message) \
BOOST_VERIFY_MSG(nil::marshalling::status_type::success == status, message)
#endif

#endif // MARSHALLING_STATUS_TYPE_HPP

0 comments on commit e9dff5d

Please sign in to comment.