Skip to content

Commit

Permalink
fix error hash to curve
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Oct 10, 2023
1 parent 34d9485 commit c569186
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
60 changes: 44 additions & 16 deletions crypto/ecc/ecc_edwards25519/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,13 +19,8 @@ pub fn random_scalar() -> Vec<u8> {
}

pub fn hash_to_curve(message: &[u8]) -> Vec<u8> {
let hash_scalar = Scalar::hash_from_bytes::<Sha512>(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::<Sha512>(message);
return hash_point.compress().to_bytes().to_vec();
}

pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec<u8> {
Expand All @@ -34,10 +29,12 @@ pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec<u8> {
return Vec::new(); // 如果大小不正确,返回空的 Vec<u8>
}

// 将输入 &[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<u8>
};
let point = match compressed_point.decompress() {
Some(point) => point,
None => return Vec::new(), // 解析点失败,返回空的 Vec<u8>
};
Expand All @@ -48,11 +45,42 @@ pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec<u8> {
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();

// 将结果转换成 &[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<u8> = [
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<u8> = Vec::new();
assert_eq!(test_vector.is_empty(), true);
}
}
8 changes: 4 additions & 4 deletions ffi/ffi_c/ffi_c_ecc_edwards25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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
}

0 comments on commit c569186

Please sign in to comment.