Skip to content

Commit

Permalink
Compile target and rust lib as library for cpp_test
Browse files Browse the repository at this point in the history
  • Loading branch information
neithanmo committed Mar 8, 2024
1 parent 52d03e9 commit 9914cc9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 48 deletions.
37 changes: 30 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ file(GLOB_RECURSE LIB_SRC
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
####
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand All @@ -132,26 +132,49 @@ target_include_directories(app_lib PUBLIC
###
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref

)

##############################################################
## Rust library
## Rust library for CPP tests????
set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust")
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/aarch64-apple-darwin/release")

# Determine the Rust target triple based on the host system
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(RUST_TARGET_TRIPLE "aarch64-unknown-linux-gnu")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
set(RUST_TARGET_TRIPLE "x86_64-unknown-linux-gnu")
else()
message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(RUST_TARGET_TRIPLE "aarch64-apple-darwin")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
set(RUST_TARGET_TRIPLE "x86_64-apple-darwin")
else()
message(FATAL_ERROR "Unsupported processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
else()
message(FATAL_ERROR "Unsupported host system: ${CMAKE_HOST_SYSTEM_NAME}")
endif()

# Use debug mode for debugging tests
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/${RUST_TARGET_TRIPLE}/debug")

# Custom target for the Rust library
add_custom_target(RustLibClean
COMMAND cargo clean
WORKING_DIRECTORY ${RUST_LIB_DIR}
)

add_custom_target(RustLibBuild
COMMAND cargo build --release --target=aarch64-apple-darwin
COMMAND cargo build --target ${RUST_TARGET_TRIPLE} --features cpp_tests
WORKING_DIRECTORY ${RUST_LIB_DIR}
DEPENDS RustLibClean
)

# Assuming the Rust library outputs a file named librslib.a
set(RUST_LIB "${RUST_TARGET_DIR}/librslib.a")

Expand All @@ -177,7 +200,7 @@ target_include_directories(unittests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
)

target_link_libraries(unittests PRIVATE
Expand Down
80 changes: 39 additions & 41 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@
* limitations under the License.
********************************************************************************/
#![no_std]
#![no_main]
#![no_builtins]
#![macro_use]
#![allow(dead_code, unused_imports)]
#![deny(unused_crate_dependencies)]

// extern crate core;
// #[cfg(test)]
// #[macro_use]
extern crate no_std_compat as std;

// use core::convert::TryInto;
// use core::mem;
use core::panic::PanicInfo;
// use core::slice::{from_raw_parts, from_raw_parts_mut};

use constants::{SPENDING_KEY_GENERATOR};
use constants::SPENDING_KEY_GENERATOR;
mod constants;

use jubjub::{Fr, Fq, AffinePoint, ExtendedPoint, SubgroupPoint};
use jubjub::{AffinePoint, ExtendedPoint, Fq, Fr, SubgroupPoint};
// use group::GroupEncoding;


// use crate::pedersen::extended_to_bytes;

#[cfg(not(any(test, fuzzing, feature = "cpp_tests")))]
use core::panic::PanicInfo;

#[cfg(not(any(test, fuzzing, feature = "cpp_tests")))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

fn debug(_msg: &str) {}

// ParserError should mirror parser_error_t from parser_common.
Expand All @@ -60,7 +65,6 @@ pub extern "C" fn from_bytes_wide(input: &[u8; 64], output: &mut [u8; 32]) -> Pa
ParserError::ParserOk
}


#[no_mangle]
pub extern "C" fn scalar_multiplication2(scalar_ptr: *const [u8; 32], output_ptr: *mut [u8; 32]) {

Expand All @@ -74,7 +78,11 @@ pub extern "C" fn scalar_multiplication2(scalar_ptr: *const [u8; 32], output_ptr
}

#[no_mangle]
pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, output: *mut [u8; 32]) -> ParserError {
pub extern "C" fn scalar_multiplication(
input: &[u8; 32],
key: ConstantKey,
output: *mut [u8; 32],
) -> ParserError {
let key_point = match key {
ConstantKey::SpendingKeyGenerator => &constants::SPENDING_KEY_GENERATOR,
ConstantKey::ProofGenerationKeyGenerator => &constants::PROOF_GENERATION_KEY_GENERATOR,
Expand All @@ -91,9 +99,10 @@ pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, outp
let subgroup2 = SubgroupPoint::from_raw_unchecked(result.get_v(), result.get_u());
let mut isEqual = false;
// let tmp_u = result.get_u() + result.get_v();
if (subgroup == subgroup2) {
isEqual == true;
// result_bytes.fill(0);
if subgroup == subgroup2 {
// isEqual == true;
isEqual = true; // ?
// result_bytes.fill(0);
}

unsafe {
Expand All @@ -120,23 +129,22 @@ pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, outp
// let scalar = Fq::from_bytes(input);
// let tmp = *SPENDING_KEY_GENERATOR * scalar;

// #if 0
// let mut scalarbytes = [0u8; 32];
// scalarbytes.copy_from_slice(input);
// let result = constants::SPENDING_KEY_GENERATOR.multiply_bits(&scalarbytes);
// let result_bytes = AffinePoint::from(result).to_bytes();

// // output.copy_from_slice(&AffinePoint::from(result).to_bytes());
// // Ensure safety and convert the raw pointer to a mutable slice
// unsafe {
// // Convert the raw pointer to a mutable slice of the appropriate length.
// // This is safe within the context of this function assuming the output pointer is valid
// // and points to a memory area of at least 32 bytes.
// let output_slice = &mut *output;
// output_slice.copy_from_slice(&result_bytes);
// }
// #endif

// #if 0
// let mut scalarbytes = [0u8; 32];
// scalarbytes.copy_from_slice(input);
// let result = constants::SPENDING_KEY_GENERATOR.multiply_bits(&scalarbytes);
// let result_bytes = AffinePoint::from(result).to_bytes();

// // output.copy_from_slice(&AffinePoint::from(result).to_bytes());
// // Ensure safety and convert the raw pointer to a mutable slice
// unsafe {
// // Convert the raw pointer to a mutable slice of the appropriate length.
// // This is safe within the context of this function assuming the output pointer is valid
// // and points to a memory area of at least 32 bytes.
// let output_slice = &mut *output;
// output_slice.copy_from_slice(&result_bytes);
// }
// #endif

// let outputUnsafe = unsafe { &mut *output };
// let result = AffinePoint::from(key_point.multiply_bits(input)).to_bytes();
Expand Down Expand Up @@ -170,13 +178,6 @@ pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, outp
ParserError::ParserOk
}

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}


#[cfg(test)]
mod tests {
// use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
Expand All @@ -201,7 +202,4 @@ mod tests {
// .0;
// pk.copy_from_slice(&pubkey);
// }



}

0 comments on commit 9914cc9

Please sign in to comment.