Skip to content

Commit

Permalink
Merge pull request monero-project#6 from kayabaNerve/rust-proper-ffi
Browse files Browse the repository at this point in the history
Remove cxx for a proper FFI
  • Loading branch information
j-berman authored May 24, 2024
2 parents 92c0182 + eea6e23 commit 8dfc8f9
Show file tree
Hide file tree
Showing 14 changed files with 1,150 additions and 335 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,3 @@ nbproject
__pycache__/
*.pyc
*.log

Cargo.lock
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@
path = external/supercop
url = https://github.com/monero-project/supercop
branch = monero
[submodule "external/fcmp-plus-plus"]
path = external/fcmp-plus-plus
url = https://github.com/kayabaNerve/fcmp-plus-plus.git
4 changes: 2 additions & 2 deletions src/fcmp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_subdirectory(fcmp_rust)

monero_add_library_with_deps(
NAME fcmp
DEPENDS rust_cxx
DEPENDS fcmp_rust
SOURCES
${fcmp_sources}
${fcmp_headers})
Expand All @@ -46,5 +46,5 @@ target_link_libraries(fcmp
crypto
epee
PRIVATE
fcmp_rust
${CMAKE_CURRENT_BINARY_DIR}/fcmp_rust/libfcmp_rust.a
${EXTRA_LIBRARIES})
39 changes: 20 additions & 19 deletions src/fcmp/curve_trees.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright (c) 2024, The Monero Project
//
//
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
Expand Down Expand Up @@ -46,7 +46,7 @@ typename C::Point get_new_parent(const C &curve, const typename C::Chunk &new_ch
{
// New parent means no prior children, fill priors with 0
std::vector<typename C::Scalar> prior_children;
tower_cycle::extend_zeroes(curve, new_children.size(), prior_children);
tower_cycle::extend_zeroes(curve, new_children.len, prior_children);

return curve.hash_grow(
curve.m_hash_init_point,
Expand Down Expand Up @@ -76,17 +76,17 @@ static typename C::Point get_first_parent(const C &curve,
if (child_layer_last_hash_updated)
{
// If the last chunk has updated children in it, then we need to get the delta to the old children
prior_children.emplace_back(curve.clone(last_chunk_ptr->last_child));
prior_children.emplace_back(last_chunk_ptr->last_child);

// Extend prior children by zeroes for any additional new children, since they must be new
if (new_children.size() > 1)
tower_cycle::extend_zeroes(curve, new_children.size() - 1, prior_children);
if (new_children.len > 1)
tower_cycle::extend_zeroes(curve, new_children.len - 1, prior_children);
}
else if (offset > 0)
{
// If we're updating the parent hash and no children were updated, then we're just adding new children
// to the existing last chunk and can fill priors with 0
tower_cycle::extend_zeroes(curve, new_children.size(), prior_children);
tower_cycle::extend_zeroes(curve, new_children.len, prior_children);
}
else
{
Expand Down Expand Up @@ -190,8 +190,9 @@ static void hash_layer(const C &curve,
const auto chunk_start = child_scalars.data() + chunk_start_idx;
const typename C::Chunk chunk{chunk_start, chunk_size};

for (const auto &c : chunk)
MDEBUG("Hashing " << curve.to_string(c));
for (uint c = 0; c < chunk_size; ++c) {
MDEBUG("Hashing " << curve.to_string(chunk_start[c]));
}

// Hash the chunk of children
typename C::Point chunk_hash = chunk_start_idx == 0
Expand Down Expand Up @@ -264,9 +265,9 @@ typename CurveTrees<C1, C2>::TreeExtension CurveTrees<C1, C2>::get_tree_extensio
for (const auto &leaf : new_leaf_tuples)
{
tree_extension.leaves.tuples.emplace_back(LeafTuple{
.O_x = m_c2.clone(leaf.O_x),
.I_x = m_c2.clone(leaf.I_x),
.C_x = m_c2.clone(leaf.C_x)
.O_x = leaf.O_x,
.I_x = leaf.I_x,
.C_x = leaf.C_x
});
}

Expand Down Expand Up @@ -378,9 +379,9 @@ std::vector<typename C2::Scalar> CurveTrees<C1, C2>::flatten_leaves(const std::v
for (const auto &l : leaves)
{
// TODO: implement without cloning
flattened_leaves.emplace_back(m_c2.clone(l.O_x));
flattened_leaves.emplace_back(m_c2.clone(l.I_x));
flattened_leaves.emplace_back(m_c2.clone(l.C_x));
flattened_leaves.emplace_back(l.O_x);
flattened_leaves.emplace_back(l.I_x);
flattened_leaves.emplace_back(l.C_x);
}

return flattened_leaves;
Expand Down
4 changes: 4 additions & 0 deletions src/fcmp/fcmp_rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# If a developer runs cargo build inside this sub-directory to only work with
# the Rust side of things, they'll create this target directory which shouldn't
# be committed
target
31 changes: 11 additions & 20 deletions src/fcmp/fcmp_rust/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,28 @@ else ()
set(TARGET_DIR "release")
endif ()

set(FCMP_RUST_CXX "${CMAKE_CURRENT_BINARY_DIR}/fcmp_rust.cc")
set(FCMP_RUST_HEADER_DIR "${MONERO_GENERATED_HEADERS_DIR}/fcmp_rust")
set(FCMP_RUST_HEADER "${FCMP_RUST_HEADER_DIR}/fcmp_rust.h")
set(CXX_HEADER "${FCMP_RUST_HEADER_DIR}/cxx.h")
set(FCMP_RUST_HEADER "${FCMP_RUST_HEADER_DIR}/fcmp++.h")
set(FCMP_RUST_LIB "${CMAKE_CURRENT_BINARY_DIR}/libfcmp_rust.a")

# Removing OUTPUT files makes sure custom command runs every time
file(REMOVE_RECURSE "${FCMP_RUST_CXX}")
file(REMOVE_RECURSE "${FCMP_RUST_HEADER_DIR}")
file(MAKE_DIRECTORY "${FCMP_RUST_HEADER_DIR}")

file(REMOVE "${FCMP_RUST_LIB}")

add_custom_command(
COMMENT "Building rust fcmp lib"
OUTPUT ${FCMP_RUST_CXX} ${FCMP_RUST_HEADER} ${CXX_HEADER}
OUTPUT ${FCMP_RUST_HEADER}
OUTPUT ${FCMP_RUST_LIB}
COMMAND CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} ${CARGO_CMD}
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/fcmp_rust/src/lib.rs.cc ${FCMP_RUST_CXX}
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/fcmp_rust/src/lib.rs.h ${FCMP_RUST_HEADER}
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/rust/cxx.h ${CXX_HEADER}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/fcmp++.h ${FCMP_RUST_HEADER}
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libfcmp_rust.a ${FCMP_RUST_LIB}
COMMAND echo "Finished copying fcmp rust targets"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)

add_custom_target(rust_cxx ALL DEPENDS ${CXX_HEADER})

set(fcmp_rust_sources ${FCMP_RUST_CXX})

monero_find_all_headers(fcmp_rust_headers "${FCMP_RUST_HEAfDER_DIR}")

monero_add_library(fcmp_rust
${fcmp_rust_sources}
${fcmp_rust_headers})

set(FCMP_RUST_LIB "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libfcmp_rust.a")
target_link_libraries(fcmp_rust dl ${FCMP_RUST_LIB})
#monero_find_all_headers(fcmp_rust_headers "${FCMP_RUST_HEADER_DIR}")
add_custom_target(fcmp_rust DEPENDS ${FCMP_RUST_LIB})
#target_link_libraries(fcmp ${FCMP_RUST_LIB})
Loading

0 comments on commit 8dfc8f9

Please sign in to comment.