diff --git a/src/fcmp/curve_trees.cpp b/src/fcmp/curve_trees.cpp index eaac83288..52071bd31 100644 --- a/src/fcmp/curve_trees.cpp +++ b/src/fcmp/curve_trees.cpp @@ -716,7 +716,7 @@ typename CurveTrees::TreeExtension CurveTrees::get_tree_extensio template std::vector CurveTrees::get_trim_instructions( const std::size_t old_n_leaf_tuples, - const std::size_t trim_n_leaf_tuples) + const std::size_t trim_n_leaf_tuples) const { CHECK_AND_ASSERT_THROW_MES(old_n_leaf_tuples > trim_n_leaf_tuples, "cannot trim more leaves than exist"); CHECK_AND_ASSERT_THROW_MES(trim_n_leaf_tuples > 0, "must be trimming some leaves"); diff --git a/src/fcmp/curve_trees.h b/src/fcmp/curve_trees.h index b4c3f10d1..77bd7bd58 100644 --- a/src/fcmp/curve_trees.h +++ b/src/fcmp/curve_trees.h @@ -217,7 +217,7 @@ public: // Get instructions useful for trimming all existing layers in the tree std::vector get_trim_instructions( const std::size_t old_n_leaf_tuples, - const std::size_t trim_n_leaf_tuples); + const std::size_t trim_n_leaf_tuples) const; // Take in the instructions useful for trimming all existing layers in the tree, all children to be trimmed from // each last chunk, and the existing last hashes in what will become the new last parent of each layer, and return diff --git a/tests/unit_tests/curve_trees.cpp b/tests/unit_tests/curve_trees.cpp index 7dbdf14b8..d552344d4 100644 --- a/tests/unit_tests/curve_trees.cpp +++ b/tests/unit_tests/curve_trees.cpp @@ -77,6 +77,60 @@ static bool validate_layer(const C &curve, return true; } //---------------------------------------------------------------------------------------------------------------------- +template +static std::vector get_last_chunk_children_to_trim(const C_CHILD &c_child, + const fcmp::curve_trees::TrimLayerInstructions &trim_instructions, + const CurveTreesGlobalTree::Layer &child_layer) +{ + std::vector children_to_trim_out; + + const std::size_t new_total_children = trim_instructions.new_total_children; + const std::size_t old_total_children = trim_instructions.old_total_children; + const std::size_t new_total_parents = trim_instructions.new_total_parents; + const std::size_t parent_chunk_width = trim_instructions.parent_chunk_width; + const std::size_t new_offset = trim_instructions.new_offset; + + CHECK_AND_ASSERT_THROW_MES(new_total_children > 0, "expected some new children"); + CHECK_AND_ASSERT_THROW_MES(new_total_children >= new_offset, "expected more children than offset"); + CHECK_AND_ASSERT_THROW_MES(new_total_parents > 0, "expected some new parents"); + + if (trim_instructions.need_last_chunk_children_to_trim) + { + std::size_t idx = ((new_total_parents - 1) * parent_chunk_width) + new_offset; + MDEBUG("Start trim from idx: " << idx); + do + { + // TODO: consolidate do while inner logic with below + CHECK_AND_ASSERT_THROW_MES(child_layer.size() > idx, "idx too high"); + const auto &child_point = child_layer[idx]; + + auto child_scalar = c_child.point_to_cycle_scalar(child_point); + children_to_trim_out.push_back(std::move(child_scalar)); + + ++idx; + } + while ((idx < old_total_children) && (idx % parent_chunk_width != 0)); + } + else if (trim_instructions.need_last_chunk_remaining_children && new_offset > 0) + { + std::size_t idx = new_total_children - new_offset; + MDEBUG("Start grow remaining from idx: " << idx); + do + { + CHECK_AND_ASSERT_THROW_MES(child_layer.size() > idx, "idx too high"); + const auto &child_point = child_layer[idx]; + + auto child_scalar = c_child.point_to_cycle_scalar(child_point); + children_to_trim_out.push_back(std::move(child_scalar)); + + ++idx; + } + while ((idx < new_total_children) && (idx % parent_chunk_width != 0)); + } + + return children_to_trim_out; +} +//---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- // CurveTreesGlobalTree implementations //---------------------------------------------------------------------------------------------------------------------- @@ -308,60 +362,6 @@ void CurveTreesGlobalTree::reduce_tree(const CurveTreesV1::TreeReduction &tree_r m_tree.c2_layers.shrink_to_fit(); } //---------------------------------------------------------------------------------------------------------------------- -template -static std::vector get_last_chunk_children_to_trim(const C_CHILD &c_child, - const fcmp::curve_trees::TrimLayerInstructions &trim_instructions, - const CurveTreesGlobalTree::Layer &child_layer) -{ - std::vector children_to_trim_out; - - const std::size_t new_total_children = trim_instructions.new_total_children; - const std::size_t old_total_children = trim_instructions.old_total_children; - const std::size_t new_total_parents = trim_instructions.new_total_parents; - const std::size_t parent_chunk_width = trim_instructions.parent_chunk_width; - const std::size_t new_offset = trim_instructions.new_offset; - - CHECK_AND_ASSERT_THROW_MES(new_total_children > 0, "expected some new children"); - CHECK_AND_ASSERT_THROW_MES(new_total_children >= new_offset, "expected more children than offset"); - CHECK_AND_ASSERT_THROW_MES(new_total_parents > 0, "expected some new parents"); - - if (trim_instructions.need_last_chunk_children_to_trim) - { - std::size_t idx = ((new_total_parents - 1) * parent_chunk_width) + new_offset; - MDEBUG("Start trim from idx: " << idx); - do - { - // TODO: consolidate do while inner logic with below - CHECK_AND_ASSERT_THROW_MES(child_layer.size() > idx, "idx too high"); - const auto &child_point = child_layer[idx]; - - auto child_scalar = c_child.point_to_cycle_scalar(child_point); - children_to_trim_out.push_back(std::move(child_scalar)); - - ++idx; - } - while ((idx < old_total_children) && (idx % parent_chunk_width != 0)); - } - else if (trim_instructions.need_last_chunk_remaining_children && new_offset > 0) - { - std::size_t idx = new_total_children - new_offset; - MDEBUG("Start grow remaining from idx: " << idx); - do - { - CHECK_AND_ASSERT_THROW_MES(child_layer.size() > idx, "idx too high"); - const auto &child_point = child_layer[idx]; - - auto child_scalar = c_child.point_to_cycle_scalar(child_point); - children_to_trim_out.push_back(std::move(child_scalar)); - - ++idx; - } - while ((idx < new_total_children) && (idx % parent_chunk_width != 0)); - } - - return children_to_trim_out; -} -//---------------------------------------------------------------------------------------------------------------------- // TODO: template CurveTreesV1::LastChunkChildrenToTrim CurveTreesGlobalTree::get_all_last_chunk_children_to_trim( const std::vector &trim_instructions)