Skip to content

Commit

Permalink
add ffi c edwards25519
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Sep 20, 2023
1 parent 6b6a338 commit a5c39b2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ffi/ffi_c/ffi_c_ecc_edwards25519/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "wedpr_ffi_c_edwards25519"
version = "1.0.0"
authors = [ "WeDPR <[email protected]>" ]
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"
51 changes: 51 additions & 0 deletions ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a5c39b2

Please sign in to comment.