From a5c39b29fed4ee2803a3e14df22ff336362831b6 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Wed, 20 Sep 2023 17:25:35 +0800 Subject: [PATCH] add ffi c edwards25519 --- ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml | 18 ++++++++ ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs | 51 +++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml create mode 100644 ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs diff --git a/ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml b/ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml new file mode 100644 index 0000000..c0b671c --- /dev/null +++ b/ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "wedpr_ffi_c_edwards25519" +version = "1.0.0" +authors = [ "WeDPR " ] +edition = "2018" + +[lib] +name = "ffi_c_edwards25519" +crate-type = [ "cdylib", "staticlib" ] + +[dependencies] +libc = "0.2.60" +ecc_edwards25519 = { path = "../../../crypto/ecc/ecc_edwards25519" } +wedpr_ffi_common = { path = "../../ffi_common" } + +# This is required to generate C/C++ header files. +[build-dependencies] +cbindgen = "0.9.0" diff --git a/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs b/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs new file mode 100644 index 0000000..9e20389 --- /dev/null +++ b/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs @@ -0,0 +1,51 @@ +//! Library of FFI of wedpr_crypto wrapper functions, targeting C/C++ +//! compatible architectures (including iOS), with fast binary interfaces. +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +use ecc_edwards25519::{hash_to_curve, point_scalar_multi, random_scalar}; + +use wedpr_ffi_common::utils::{ + c_read_raw_pointer, c_write_raw_pointer, CInputBuffer, COutputBuffer, + SUCCESS, +}; + +#[no_mangle] +/// C interface for 'wedpr_random_scalar'. +pub unsafe extern "C" fn wedpr_random_scalar( + output_ciphertext: &mut COutputBuffer, +) -> i8 { + let random_scalar = random_scalar(); + c_write_raw_pointer(&random_scalar, output_ciphertext); + SUCCESS +} + +#[no_mangle] +/// C interface for 'wedpr_hash_to_curve'. +pub unsafe extern "C" fn wedpr_hash_to_curve( + raw_message: &CInputBuffer, + output_ciphertext: &mut COutputBuffer, +) -> i8 { + let plaintext = c_read_raw_pointer(raw_message); + + let message = hash_to_curve(&plaintext); + std::mem::forget(raw_message); + + c_write_raw_pointer(&message, output_ciphertext); + SUCCESS +} + +#[no_mangle] +/// C interface for 'wedpr_point_scalar_multi'. +pub unsafe extern "C" fn wedpr_point_scalar_multi( + raw_point: &CInputBuffer, + raw_scalar: &CInputBuffer, + output_ciphertext: &mut COutputBuffer, +) -> i8 { + let num_point = c_read_raw_pointer(raw_point); + let num_scalar = c_read_raw_pointer(raw_scalar); + let result = point_scalar_multi(&num_point, &num_scalar); + std::mem::forget(raw_point); + std::mem::forget(raw_scalar); + c_write_raw_pointer(&result, output_ciphertext); + SUCCESS +}