diff --git a/crypto/ecc/ecc_edwards25519/src/lib.rs b/crypto/ecc/ecc_edwards25519/src/lib.rs index 7eef815..753cc6e 100644 --- a/crypto/ecc/ecc_edwards25519/src/lib.rs +++ b/crypto/ecc/ecc_edwards25519/src/lib.rs @@ -1,8 +1,8 @@ -use sha2::Sha512; -// use sha2::Digest; +use curve25519_dalek::{ + ristretto::CompressedRistretto, RistrettoPoint, Scalar, +}; use rand::rngs::ThreadRng; -// use rand::RngCore; -use curve25519_dalek::{edwards::CompressedEdwardsY, Scalar}; +use sha2::Sha512; const SCALAR_SIZE: usize = 32; const POINT_SIZE: usize = 32; @@ -19,13 +19,8 @@ pub fn random_scalar() -> Vec { } pub fn hash_to_curve(message: &[u8]) -> Vec { - let hash_scalar = Scalar::hash_from_bytes::(message).to_bytes(); - let opt_point = match CompressedEdwardsY::from_slice(&hash_scalar) { - Ok(v) => v, - Err(_) => return Vec::new(), - }; - - return opt_point.to_bytes().to_vec(); + let hash_point = RistrettoPoint::hash_from_bytes::(message); + return hash_point.compress().to_bytes().to_vec(); } pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { @@ -34,10 +29,12 @@ pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { return Vec::new(); // 如果大小不正确,返回空的 Vec } - // 将输入 &[u8] 转换成 CompressedEdwardsY 表示的点 - let mut point_bytes = [0u8; POINT_SIZE]; - point_bytes.copy_from_slice(point); - let compressed_point = match CompressedEdwardsY(point_bytes).decompress() { + // 将输入 &[u8] 转换成 CompressedRistretto 表示的点 + let compressed_point = match CompressedRistretto::from_slice(&point) { + Ok(point) => point, + Err(_) => return Vec::new(), // 解析点失败,返回空的 Vec + }; + let point = match compressed_point.decompress() { Some(point) => point, None => return Vec::new(), // 解析点失败,返回空的 Vec }; @@ -48,7 +45,7 @@ pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { let scalar = Scalar::from_bytes_mod_order(scalar_bytes); // 执行点乘操作 - let result_point = compressed_point * scalar; + let result_point = point * scalar; // 将结果转换成压缩格式的点 let compressed_result = result_point.compress(); @@ -56,3 +53,34 @@ pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { // 将结果转换成 &[u8] compressed_result.as_bytes().to_vec() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_edwards25519() { + let message = "hello world".as_bytes(); + // let key = random_scalar(); + let key = [ + 125, 185, 175, 119, 127, 247, 65, 76, 146, 163, 142, 178, 247, 149, + 185, 187, 132, 19, 67, 161, 231, 145, 164, 133, 64, 64, 220, 138, + 248, 231, 43, 7, + ]; + let point = hash_to_curve(&message); + let result = point_scalar_multi(&point, &key); + let expected: Vec = [ + 74, 210, 113, 116, 176, 64, 232, 75, 240, 244, 198, 94, 19, 27, + 194, 225, 169, 80, 205, 176, 169, 190, 206, 56, 52, 218, 142, 79, + 28, 132, 70, 16, + ] + .to_vec(); + assert_eq!(expected, result); + } + + #[test] + fn test_empty_vector() { + let test_vector: Vec = Vec::new(); + assert_eq!(test_vector.is_empty(), true); + } +} diff --git a/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs b/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs index af2c31b..d4ef012 100644 --- a/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs +++ b/ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs @@ -4,10 +4,7 @@ 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, -}; +use wedpr_ffi_common::utils::{c_read_raw_pointer, c_write_raw_pointer, CInputBuffer, COutputBuffer, FAILURE, SUCCESS}; #[no_mangle] /// C interface for 'wedpr_random_scalar'. @@ -44,6 +41,9 @@ pub unsafe extern "C" fn wedpr_point_scalar_multi( let result = point_scalar_multi(&num_point, &num_scalar); std::mem::forget(num_point); std::mem::forget(num_scalar); + if result.is_empty() { + return FAILURE; + } c_write_raw_pointer(&result, output_ciphertext); SUCCESS }