Skip to content

Commit

Permalink
Replaced asserts with exceptions so they are thrown at release #126
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Dec 6, 2024
1 parent 850312c commit 33ee1b3
Show file tree
Hide file tree
Showing 32 changed files with 319 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,16 @@ namespace nil {
std::is_same<std::uint8_t,
typename std::iterator_traits<typename ValueType::iterator>::value_type>::value,
bool>::type = true>
ValueType
make_merkle_node_value(const typename merkle_node_value<nil::marshalling::field_type<Endianness>,
ValueType>::type &filled_node_value) {
ValueType make_merkle_node_value(
const typename merkle_node_value<nil::marshalling::field_type<Endianness>, ValueType>::type &filled_node_value)
{
ValueType node_value;
BOOST_ASSERT(node_value.size() == filled_node_value.value().size());
if (node_value.size() != filled_node_value.value().size()) {
throw std::invalid_argument(
std::string("Invalid number of elements for merkle tree nodes. Expected: ") +
std::to_string(node_value.size()) + " got: " +
std::to_string(filled_node_value.value().size()));
}
for (std::size_t i = 0; i < filled_node_value.value().size(); ++i) {
node_value.at(i) = filled_node_value.value().at(i).value();
}
Expand All @@ -187,7 +192,8 @@ namespace nil {
GroupElementType
>::value, bool>::type = true>
GroupElementType make_merkle_node_value(const typename merkle_node_value<
nil::marshalling::field_type<Endianness>, GroupElementType>::type &filled_node_value) {
nil::marshalling::field_type<Endianness>, GroupElementType>::type &filled_node_value)
{
return filled_node_value.value();
}

Expand All @@ -198,11 +204,12 @@ namespace nil {
nil::crypto3::containers::merkle_proof<typename MerkleProof::hash_type,
MerkleProof::arity>>::value,
bool>::type = true>
typename MerkleProof::value_type
make_merkle_node_value(const typename merkle_node_value<nil::marshalling::field_type<Endianness>,
MerkleProof>::type &filled_node_value) {
typename MerkleProof::value_type make_merkle_node_value(
const typename merkle_node_value<nil::marshalling::field_type<Endianness>, MerkleProof>::type &filled_node_value)
{
return make_merkle_node_value<typename MerkleProof::value_type, Endianness>(filled_node_value);
}

template<typename MerkleTree,
typename Endianness,
typename std::enable_if<
Expand All @@ -211,9 +218,9 @@ namespace nil {
nil::crypto3::containers::merkle_tree<typename MerkleTree::hash_type,
MerkleTree::arity>>::value,
bool>::type = true>
typename MerkleTree::value_type
make_merkle_node_value(const typename merkle_node_value<nil::marshalling::field_type<Endianness>,
MerkleTree>::type &filled_node_value) {
typename MerkleTree::value_type make_merkle_node_value(
const typename merkle_node_value<nil::marshalling::field_type<Endianness>, MerkleTree>::type &filled_node_value)
{
return make_merkle_node_value<typename MerkleTree::value_type, Endianness>(filled_node_value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ namespace nil {

template<typename MerkleProof, typename Endianness>
typename MerkleProof::path_element_type make_merkle_proof_path_element(
const typename merkle_proof_path_element<nil::marshalling::field_type<Endianness>,
MerkleProof>::type &filled_proof_path_element) {
const typename merkle_proof_path_element<nil::marshalling::field_type<Endianness>, MerkleProof>::type &filled_proof_path_element)
{
typename MerkleProof::path_element_type proof_path_element;
proof_path_element._position = std::get<0>(filled_proof_path_element.value()).value();
proof_path_element._hash =
Expand All @@ -133,9 +133,9 @@ namespace nil {
}

template<typename MerkleProof, typename Endianness>
typename MerkleProof::layer_type
make_merkle_proof_layer(const typename merkle_proof_layer<nil::marshalling::field_type<Endianness>,
MerkleProof>::type &filled_proof_layer) {
typename MerkleProof::layer_type make_merkle_proof_layer(
const typename merkle_proof_layer<nil::marshalling::field_type<Endianness>, MerkleProof>::type &filled_proof_layer)
{
typename MerkleProof::layer_type proof_layer;
for (std::size_t i = 0; i < filled_proof_layer.value().size(); ++i) {
proof_layer.at(i) =
Expand All @@ -158,9 +158,9 @@ namespace nil {
}

template<typename MerkleProof, typename Endianness>
typename MerkleProof::path_type
make_merkle_proof_path(const typename merkle_proof_path<nil::marshalling::field_type<Endianness>,
MerkleProof>::type &filled_proof_path) {
typename MerkleProof::path_type make_merkle_proof_path(
const typename merkle_proof_path<nil::marshalling::field_type<Endianness>, MerkleProof>::type &filled_proof_path)
{
typename MerkleProof::path_type proof_path;
proof_path.reserve(filled_proof_path.value().size());
for (std::size_t i = 0; i < filled_proof_path.value().size(); ++i) {
Expand Down Expand Up @@ -189,8 +189,8 @@ namespace nil {

template<typename MerkleProof, typename Endianness>
MerkleProof make_merkle_proof(
const merkle_proof<nil::marshalling::field_type<Endianness>, MerkleProof> &filled_merkle_proof) {

const merkle_proof<nil::marshalling::field_type<Endianness>, MerkleProof> &filled_merkle_proof)
{
MerkleProof mp(
std::get<0>(filled_merkle_proof.value()).value(), // mp._li
make_merkle_node_value<MerkleProof, Endianness>(std::get<1>(filled_merkle_proof.value())), // mp._root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ namespace nil {
}

template<typename MerkleTree, typename Endianness>
MerkleTree make_merkle_tree(const merkle_tree<
nil::marshalling::field_type<Endianness>, MerkleTree> &filled_merkle_tree) {
MerkleTree make_merkle_tree(
const merkle_tree<nil::marshalling::field_type<Endianness>, MerkleTree> &filled_merkle_tree)
{
typename MerkleTree::container_type hashes;
for (std::size_t i = 0; i < filled_merkle_tree.value().size(); ++i) {
hashes.push_back(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,11 @@ namespace nil {

template<typename TFieldBase, typename TElement, typename TMarshalledElement>
std::vector<TElement> make_standard_array_list(
const standard_array_list<TFieldBase, TMarshalledElement>& filled_array,
std::function<TElement(const TMarshalledElement&)> element_de_marshalling) {
const standard_array_list<TFieldBase, TMarshalledElement>& filled_array,
std::function<TElement(const TMarshalledElement&)> element_de_marshalling)
{
std::vector<TElement> result;
result.reserve(filled_array.value().size());
for (const auto& v: filled_array.value()) {
result.push_back(element_de_marshalling(v));
}
Expand Down Expand Up @@ -470,8 +472,11 @@ namespace nil {
const standard_array_list<TFieldBase, TMarshalledKey>& filled_keys,
const standard_array_list<TFieldBase, TMarshalledValue>& filled_values,
std::function<TKey(const TMarshalledKey&)> key_de_marshalling,
std::function<TValue(const TMarshalledValue&)> value_de_marshalling) {
assert(filled_keys.value().size() == filled_values.value().size());
std::function<TValue(const TMarshalledValue&)> value_de_marshalling)
{
if (filled_keys.value().size() != filled_values.value().size()) {
throw std::invalid_argument("Number of values and keys do not match");;
}

std::map<TKey, TValue> result;
for (std::size_t i = 0; i < filled_keys.value().size(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ namespace nil {
}

template<typename Endianness>
math::flat_pow_operation
make_power_operation(const typename flat_pow_operation<nil::marshalling::field_type<Endianness>>::type& filled_power_op) {
math::flat_pow_operation make_power_operation(
const typename flat_pow_operation<nil::marshalling::field_type<Endianness>>::type& filled_power_op)
{
math::flat_pow_operation power_op;
power_op.power = std::get<0>(filled_power_op.value()).value();
power_op.type = static_cast<math::flat_node_type>(std::get<1>(filled_power_op.value()).value());
Expand All @@ -190,8 +191,9 @@ namespace nil {
}

template<typename Endianness, typename ArithmeticOperatorType>
math::flat_binary_arithmetic_operation<ArithmeticOperatorType>
make_binary_operation(const typename flat_binary_arithmetic_operation<nil::marshalling::field_type<Endianness>>::type& filled_power_op) {
math::flat_binary_arithmetic_operation<ArithmeticOperatorType> make_binary_operation(
const typename flat_binary_arithmetic_operation<nil::marshalling::field_type<Endianness>>::type& filled_power_op)
{
math::flat_binary_arithmetic_operation<ArithmeticOperatorType> bin_op;
bin_op.op = static_cast<ArithmeticOperatorType>(std::get<0>(filled_power_op.value()).value());
bin_op.left_type = static_cast<math::flat_node_type>(std::get<1>(filled_power_op.value()).value());
Expand All @@ -203,9 +205,8 @@ namespace nil {

template<typename ExpressionType, typename Endianness>
ExpressionType make_expression(
const typename expression<nil::marshalling::field_type<Endianness>,
ExpressionType>::type &filled_expr) {

const typename expression<nil::marshalling::field_type<Endianness>, ExpressionType>::type &filled_expr)
{
using ArithmeticOperatorType = typename ExpressionType::binary_arithmetic_operation_type::ArithmeticOperatorType;
math::flat_expression<ExpressionType> flat_expr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ namespace nil {
}

template<typename Endianness, typename PolynomialType>
PolynomialType
make_polynomial(
PolynomialType make_polynomial(
const typename polynomial<
nil::marshalling::field_type<Endianness>,
PolynomialType,
Expand Down Expand Up @@ -121,12 +120,12 @@ namespace nil {
}

template<typename Endianness, typename PolynomialDFSType>
PolynomialDFSType
make_polynomial(const typename polynomial<
PolynomialDFSType make_polynomial(const typename polynomial<
nil::marshalling::field_type<Endianness>,
PolynomialDFSType,
std::enable_if_t<nil::crypto3::math::is_polynomial_dfs<PolynomialDFSType>::value
>>::type &filled_polynomial) {
>>::type &filled_polynomial)
{
auto val = nil::crypto3::marshalling::types::make_field_element_vector<
typename PolynomialDFSType::value_type,
Endianness>(std::get<1>(filled_polynomial.value()));
Expand Down Expand Up @@ -155,7 +154,8 @@ namespace nil {

template<typename Endianness, typename PolynomialType>
std::vector<PolynomialType> make_polynomial_vector(
const polynomial_vector<nil::marshalling::field_type<Endianness>, PolynomialType> &filled_polynomial_vector) {
const polynomial_vector<nil::marshalling::field_type<Endianness>, PolynomialType> &filled_polynomial_vector)
{
std::vector<PolynomialType> result;
result.reserve(filled_polynomial_vector.value().size());
for (std::size_t i = 0; i < filled_polynomial_vector.value().size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ namespace nil {
}

template<typename Endianness, typename NonLinearTerm>
NonLinearTerm
make_term(const typename term<nil::marshalling::field_type<Endianness>,
NonLinearTerm>::type &filled_term) {
NonLinearTerm make_term(
const typename term<nil::marshalling::field_type<Endianness>, NonLinearTerm>::type &filled_term)
{
std::vector<typename NonLinearTerm::variable_type> vars;
auto coeff = std::get<0>(filled_term.value()).value();
vars.reserve(std::get<1>(filled_term.value()).value().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,16 @@ namespace nil {
nil::marshalling::types::standard_array_list<
nil::marshalling::field_type<Endianness>,
integral<nil::marshalling::field_type<Endianness>, IntegralContainer>>
integral_vector) {

std::vector<IntegralContainer> result;
std::vector<integral<nil::marshalling::field_type<Endianness>, IntegralContainer>> &values =
const& integral_vector)
{
std::vector<integral<nil::marshalling::field_type<Endianness>, IntegralContainer>> const& values =
integral_vector.value();
std::size_t size = values.size();
std::vector<IntegralContainer> result;
result.reserve(size);

for (std::size_t i = 0; i < size; i++) {
result.push_back(values[i].value());
result.emplace_back(values[i].value());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ namespace nil {

template<typename Endianness, typename IntegerType>
std::vector<IntegerType>
make_integer_vector(const nil::marshalling::types::standard_size_t_array_list<
nil::marshalling::field_type<Endianness>
>& filled_vector) {
make_integer_vector(
const nil::marshalling::types::standard_size_t_array_list<nil::marshalling::field_type<Endianness> >& filled_vector)
{
std::vector<IntegerType> result;
for( std::size_t i = 0; i < filled_vector.value().size(); i++){
result.push_back(filled_vector.value()[i].value());
result.reserve(filled_vector.value().size());
for (std::size_t i = 0; i < filled_vector.value().size(); i++) {
result.emplace_back(filled_vector.value()[i].value());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,41 @@ namespace nil {

template<typename Endianness, typename EvalStorage>
EvalStorage make_eval_storage(
const eval_storage<nil::marshalling::field_type<Endianness>, EvalStorage> &filled_storage
){
const eval_storage<nil::marshalling::field_type<Endianness>, EvalStorage> &filled_storage)
{
EvalStorage z;
typename nil::crypto3::marshalling::types::batch_info_type batch_info;
std::vector<std::uint8_t> eval_points_num;

auto filled_batch_info = std::get<1>(filled_storage.value()).value();
if (filled_batch_info.size() % 2 != 0) {
throw std::invalid_argument("Wrong length of batch info");
}
for( std::size_t i = 0; i < filled_batch_info.size(); i+=2 ){
batch_info[filled_batch_info[i].value()] = filled_batch_info[i+1].value();
z.set_batch_size(filled_batch_info[i].value(), filled_batch_info[i+1].value());
}

auto filled_eval_points_num = std::get<2>(filled_storage.value()).value();
std::size_t cur = 0;
for( const auto &it:batch_info){
for( std::size_t i = 0; i < it.second; i++ ){
for (const auto &it: batch_info){
for (std::size_t i = 0; i < it.second; i++ ) {
if (cur >= filled_eval_points_num.size()) {
throw std::invalid_argument("Not enough eval points");
}
z.set_poly_points_number(it.first, i, filled_eval_points_num[cur].value());
cur++;
}
}

auto filled_z = std::get<0>(filled_storage.value()).value();
cur = 0;
for( const auto &it:batch_info){
for( std::size_t i = 0; i < it.second; i++ ){
for( std::size_t j = 0; j < z.get_poly_points_number(it.first, i); j++ ){
for (const auto &it: batch_info) {
for (std::size_t i = 0; i < it.second; i++ ) {
for (std::size_t j = 0; j < z.get_poly_points_number(it.first, i); j++ ) {
if (cur >= filled_z.size()) {
throw std::invalid_argument("Not enough values for Z");
}
z.set(it.first, i, j, filled_z[cur].value());
cur++;
}
Expand Down
Loading

0 comments on commit 33ee1b3

Please sign in to comment.