From 44aedede4996cbc0b8229240a80cd5d499da9caa Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Wed, 26 Jul 2023 16:03:40 +0800 Subject: [PATCH] format code --- crypto/block_cipher/aes/src/lib.rs | 5 ++ crypto/block_cipher/sm4/src/lib.rs | 3 + .../oblivious_transfer/base_ot/src/ot_kv.rs | 43 +++++---- crypto/pairing/bls12_381/src/lib.rs | 28 +++--- crypto/pairing/bls12_381/src/peks.rs | 89 +++++++++++-------- ffi/ffi_c/ffi_c_equality/src/lib.rs | 42 ++++----- ffi/ffi_java/ffi_java_equality/src/lib.rs | 62 +++++++------ 7 files changed, 156 insertions(+), 116 deletions(-) diff --git a/crypto/block_cipher/aes/src/lib.rs b/crypto/block_cipher/aes/src/lib.rs index bd04a04..19da31e 100644 --- a/crypto/block_cipher/aes/src/lib.rs +++ b/crypto/block_cipher/aes/src/lib.rs @@ -99,6 +99,11 @@ mod tests { let msg = b"hello"; let ciphertext = aes256.encrypt(&msg.to_vec(), &key, &iv).unwrap(); + // let iv2 = aes256.generate_iv(); + let ciphertext2 = aes256.encrypt(&msg.to_vec(), &key, &iv).unwrap(); + assert_eq!(ciphertext, ciphertext2); + println!("{:?}", ciphertext); + println!("{:?}", ciphertext2); let decrypted_msg = aes256.decrypt(&ciphertext, &key, &iv).unwrap(); assert_eq!(decrypted_msg, msg); diff --git a/crypto/block_cipher/sm4/src/lib.rs b/crypto/block_cipher/sm4/src/lib.rs index 6c2d01f..b3f604c 100644 --- a/crypto/block_cipher/sm4/src/lib.rs +++ b/crypto/block_cipher/sm4/src/lib.rs @@ -87,6 +87,9 @@ mod tests { let msg = b"helloworld1 helloworld2 helloworld3 helloworld4"; let ciphertext = sm4.encrypt(&msg.to_vec(), &key, &iv).unwrap(); + let ciphertext2 = sm4.encrypt(&msg.to_vec(), &key, &iv).unwrap(); + assert_eq!(ciphertext, ciphertext2); + let decrypted_msg = sm4.decrypt(&ciphertext, &key, &iv).unwrap(); assert_eq!(decrypted_msg, msg); } diff --git a/crypto/oblivious_transfer/base_ot/src/ot_kv.rs b/crypto/oblivious_transfer/base_ot/src/ot_kv.rs index 0f7078e..a84ca7c 100644 --- a/crypto/oblivious_transfer/base_ot/src/ot_kv.rs +++ b/crypto/oblivious_transfer/base_ot/src/ot_kv.rs @@ -5,16 +5,21 @@ use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; +use rand::Rng; use sha3::Sha3_512; +use wedpr_l_crypto_block_cipher_aes; use wedpr_l_crypto_hash_sha3::WedprSha3_256; use wedpr_l_crypto_zkp_utils::{ get_random_scalar, point_to_bytes, BASEPOINT_G1, }; -use wedpr_l_utils::{error::WedprError, traits::Hash}; -use wedpr_l_crypto_block_cipher_aes; -use wedpr_l_utils::traits::BlockCipher; -use rand::Rng; -use wedpr_l_protos::generated::ot::{IdList, DataDict, OtReceiverCommitmentKOutOfN, OtCiphertextItemKOutOfN, OtCiphertextsKOutOfN, OtReceiverSecretKOutOfN}; +use wedpr_l_protos::generated::ot::{ + DataDict, IdList, OtCiphertextItemKOutOfN, OtCiphertextsKOutOfN, + OtReceiverCommitmentKOutOfN, OtReceiverSecretKOutOfN, +}; +use wedpr_l_utils::{ + error::WedprError, + traits::{BlockCipher, Hash}, +}; lazy_static! { static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); @@ -41,7 +46,6 @@ pub struct CiphertextItemKOutOfN { pub key_basepoint: RistrettoPoint, pub encrypted_message: Vec>, } -// // pub struct TwoDeepVector {} // // impl TwoDeepVector { @@ -109,13 +113,10 @@ pub struct CiphertextItemKOutOfN { pub struct OtKvKOutOfN {} impl OtKvKOutOfN { - // pub fn ot_make_choice(choice_list: &Vec>) -> IdList { // // } - - /// Generates an OT query based on receiver's choice of ids from /// choice_list. It returns ReceiverSecret and ReceiverCommitment. /// ReceiverSecret will be later used to decrypt the chosen message @@ -145,7 +146,8 @@ impl OtKvKOutOfN { .push(RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ *BASEPOINT_G1, ])); - // ot_receiver_commitment.point_z.push(RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ + // ot_receiver_commitment.point_z. + // push(RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ // *BASEPOINT_G1, // ]).compress().to_bytes().to_vec()); // point_z_list @@ -154,7 +156,6 @@ impl OtKvKOutOfN { // ]).compress().to_bytes().to_vec()); } - // ot_receiver_commitment.set_point_z(point_z_list); ( ReceiverSecretKOutOfN { @@ -223,8 +224,10 @@ impl OtKvKOutOfN { // let key1 = bytes_key[0..16].to_vec(); // let iv1 = bytes_key[16..32].to_vec(); - // let aes256 = wedpr_l_crypto_block_cipher_aes::WedprBlockCipherAes256::default(); - // let encrypted_message = match aes256.encrypt(message, &key1, &iv1) { + // let aes256 = + // wedpr_l_crypto_block_cipher_aes::WedprBlockCipherAes256::default(); + // let encrypted_message = match + // aes256.encrypt(message, &key1, &iv1) { // Ok(v) =>v, // Err(_) => { // wedpr_println!("aes256 encrypt failed"); @@ -266,15 +269,16 @@ impl OtKvKOutOfN { while bytes_key_cp.len() < encrypted_message.len() { bytes_key_cp.append(&mut bytes_key.clone()); - } let decrypted_message: Vec = encrypted_message .iter() .zip(bytes_key_cp.iter()) .map(|(&x1, &x2)| x1 ^ x2) .collect(); - // let aes256 = wedpr_l_crypto_block_cipher_aes::WedprBlockCipherAes256::default(); - // let decrypted_message = match aes256.encrypt(encrypted_message, &bytes_key, &bytes_key) { + // let aes256 = + // wedpr_l_crypto_block_cipher_aes::WedprBlockCipherAes256::default(); + // let decrypted_message = match + // aes256.encrypt(encrypted_message, &bytes_key, &bytes_key) { // Ok(v) =>v, // Err(_) => { // wedpr_println!("secp256k1 ECIES encrypt failed"); @@ -314,7 +318,8 @@ mod tests { let choice_list = vec![id0.as_bytes().to_vec(), id2.as_bytes().to_vec()]; let choice_list_bytes = TwoDeepVector::to_bytes(&choice_list); - let choice_list_recover = TwoDeepVector::from_bytes(&choice_list_bytes).unwrap(); + let choice_list_recover = + TwoDeepVector::from_bytes(&choice_list_bytes).unwrap(); let id_list = vec![ id0.as_bytes().to_vec(), id1.as_bytes().to_vec(), @@ -342,11 +347,11 @@ mod tests { ) .unwrap(); let message_bytes = TwoDeepVector::to_bytes(&message); - let message_bytes_recover = TwoDeepVector::from_bytes(&message_bytes).unwrap(); + let message_bytes_recover = + TwoDeepVector::from_bytes(&message_bytes).unwrap(); assert_eq!( vec![message0.as_bytes().to_vec(), message2.as_bytes().to_vec()], message_bytes_recover ); - } } diff --git a/crypto/pairing/bls12_381/src/lib.rs b/crypto/pairing/bls12_381/src/lib.rs index 70b772a..a296b23 100644 --- a/crypto/pairing/bls12_381/src/lib.rs +++ b/crypto/pairing/bls12_381/src/lib.rs @@ -7,10 +7,9 @@ use bls12_381::{ pairing, G1Affine, G1Projective, G2Affine, G2Projective, Scalar, }; use ff::Field; -use rand; +use rand::{self, Rng}; use sha2; use wedpr_l_utils::error::WedprError; -use rand::Rng; #[macro_use] extern crate wedpr_l_macros; @@ -211,9 +210,9 @@ mod tests { let web_hello1 = "a91c14d9111e95206e5be9e644f68dd6ddb5865da5bfab80d4c7fb7a1a1299db401e200bb401adc87da751283dd8cb2984820a100894e50583daec6261bd8728d6c80689fa02d8f3e7fae23329755229c98456db47f78071e03dd4344e7e8dc3029bf4809a8fc3198dcd4c6810465cbc42fc57329b1d980f70b2a383507c257e35f33f2851e0ab2a40bb24606900c152"; let web_bytes1 = hex::decode(web_hello1).unwrap(); - - // let web_hello2 = "8fa65c08f9d137934380af14cd659370b8c7e51e8df839f31fb3edaeb50d70a8aed3e7ef441927401e8f40691776292188adecd16201d2f868b2d862771bcb1ade33492db124a4b5329c32ae24971b4980f5649134eb9ac00615286e1dc0b3ae02d27938a92f7a49b9e830fd3857c4c7648f65879a2b018aef26dbfe253ef25e7e7c36dc92d5de4a2fce6e0c4d1c7803"; - // let web_bytes2 = hex::decode(web_hello2).unwrap(); + // let web_hello2 = + // "8fa65c08f9d137934380af14cd659370b8c7e51e8df839f31fb3edaeb50d70a8aed3e7ef441927401e8f40691776292188adecd16201d2f868b2d862771bcb1ade33492db124a4b5329c32ae24971b4980f5649134eb9ac00615286e1dc0b3ae02d27938a92f7a49b9e830fd3857c4c7648f65879a2b018aef26dbfe253ef25e7e7c36dc92d5de4a2fce6e0c4d1c7803" + // ; let web_bytes2 = hex::decode(web_hello2).unwrap(); // let cipher1_m2_recover = encrypt_message(message_hello); let cipher2_m1_recover = WedprBls128Cipher::from_bytes(&web_bytes1).unwrap(); @@ -221,31 +220,36 @@ mod tests { // WedprBls128Cipher::from_bytes(&web_bytes2).unwrap(); assert_eq!(equality_test(&cipher2_m1_recover, &cipher1_hello), true); - // let web_wrong1 = "ae80d2a0349fac71423365ba24a4ecd195f629e11b3a403de0ba4c81bda121e050508220a50e4da78ff0b4d3c0f4fafcab3c6080b96d3050ec0dcf271e3c00ce81b6e34de2628817a7f9312d4a64bac56207208abc7594c29fb427cd0c0372c80cb4e5e966471dcefedba86857f572554102075f560949b38ba1cb928360b9c865aafaabea691ea2bbc5863775f02f32"; - // let web_bytes1_wrong = hex::decode(web_wrong1).unwrap(); + // let web_wrong1 = + // "ae80d2a0349fac71423365ba24a4ecd195f629e11b3a403de0ba4c81bda121e050508220a50e4da78ff0b4d3c0f4fafcab3c6080b96d3050ec0dcf271e3c00ce81b6e34de2628817a7f9312d4a64bac56207208abc7594c29fb427cd0c0372c80cb4e5e966471dcefedba86857f572554102075f560949b38ba1cb928360b9c865aafaabea691ea2bbc5863775f02f32" + // ; let web_bytes1_wrong = hex::decode(web_wrong1).unwrap(); // // - // let web_wrong2 = "80770cfb268456382939b1182f25088c93def05becf439db9c7f1c98628627593ab6b75e6b4f849328bf7aacdd142b7394417f658689838b086fc27a4d649d3c5ec3fbaa9e8fc6de2390e07f8a28bba2308fcf9870e55cd4e723c5ac74202b8213f38e1b950e009894d1334ca47be89d42080aee0b3b942f5a3da3f3e02757b25f9b9ae529d5d1a342df5b708a919ab8"; - // let web_bytes2_wrong = hex::decode(web_wrong2).unwrap(); + // let web_wrong2 = + // "80770cfb268456382939b1182f25088c93def05becf439db9c7f1c98628627593ab6b75e6b4f849328bf7aacdd142b7394417f658689838b086fc27a4d649d3c5ec3fbaa9e8fc6de2390e07f8a28bba2308fcf9870e55cd4e723c5ac74202b8213f38e1b950e009894d1334ca47be89d42080aee0b3b942f5a3da3f3e02757b25f9b9ae529d5d1a342df5b708a919ab8" + // ; let web_bytes2_wrong = hex::decode(web_wrong2).unwrap(); // let cipher1_m2_recover = // WedprBls128Cipher::from_bytes(&web_bytes1_wrong).unwrap(); // let cipher2_m2_recover = // WedprBls128Cipher::from_bytes(&web_bytes2_wrong).unwrap(); - // // assert_eq!(equality_test(&cipher2_m1_recover, &cipher1_hello), true); + // // assert_eq!(equality_test(&cipher2_m1_recover, &cipher1_hello), + // true); // // assert_eq!( // equality_test(&cipher1_m1_recover, &cipher2_m1_recover), // true // ); // assert_eq!(equality_test(&cipher1_m1_recover, &cipher1_hello), true); - // assert_eq!(equality_test(&cipher1_m1_recover, &cipher1_wrong), false); + // assert_eq!(equality_test(&cipher1_m1_recover, &cipher1_wrong), + // false); // // assert_eq!( // equality_test(&cipher1_m2_recover, &cipher2_m2_recover), // true // ); // assert_eq!(equality_test(&cipher1_m2_recover, &cipher1_wrong), true); - // assert_eq!(equality_test(&cipher1_m2_recover, &cipher1_hello), false); + // assert_eq!(equality_test(&cipher1_m2_recover, &cipher1_hello), + // false); } #[test] diff --git a/crypto/pairing/bls12_381/src/peks.rs b/crypto/pairing/bls12_381/src/peks.rs index a2b5366..840d08a 100644 --- a/crypto/pairing/bls12_381/src/peks.rs +++ b/crypto/pairing/bls12_381/src/peks.rs @@ -4,9 +4,9 @@ use bls12_381::{ }; use ff::Field; use rand; +use std::convert::TryInto; use wedpr_l_crypto_hash_sha2::WedprSha2_256; use wedpr_l_utils::{error::WedprError, traits::Hash}; -use std::convert::TryInto; #[derive(Clone, Copy, Debug, Default)] pub struct PeksKeyPair { @@ -47,7 +47,9 @@ impl PeksKeyPair { G2Affine::from(self.pk).to_compressed().to_vec() } - pub fn recover_public_key(pk_bytes: &[u8]) -> Result { + pub fn recover_public_key( + pk_bytes: &[u8], + ) -> Result { if pk_bytes.len() != 96 { return Err(WedprError::FormatError); } @@ -65,7 +67,9 @@ impl PeksKeyPair { self.sk.to_bytes().to_vec() } - pub fn recover_secret_key(scalar_bytes: &[u8]) -> Result { + pub fn recover_secret_key( + scalar_bytes: &[u8], + ) -> Result { if scalar_bytes.len() != 32 { return Err(WedprError::FormatError); } @@ -141,14 +145,13 @@ impl TrapdoorCipher { } } - pub fn seed_to_scalar(seed: &[u8]) -> Result { let seed_vec = seed.to_vec(); if seed_vec.len() != 32 { return Err(WedprError::FormatError); } let seed_array: [u8; 32] = match seed_vec.try_into() { - Ok(v) =>v, + Ok(v) => v, Err(_) => return Err(WedprError::FormatError), }; let result = Scalar::from_bytes(&seed_array).unwrap_or(Scalar::zero()); @@ -173,17 +176,17 @@ pub fn generate_key() -> PeksKeyPair { generate_key_with_seed(&blinding).unwrap() } - - pub fn encrypt_message(message: &[u8], pk: &G2Projective) -> PeksCipher { let rng = rand::rngs::OsRng::default(); let blinding = Scalar::random(rng).to_bytes(); encrypt_message_with_seed(&blinding, message, pk).unwrap() } - -pub fn encrypt_message_with_seed(seed: &[u8], message: &[u8], pk: &G2Projective) -> Result { - +pub fn encrypt_message_with_seed( + seed: &[u8], + message: &[u8], + pk: &G2Projective, +) -> Result { let blinding = seed_to_scalar(seed)?; let message_g1 = message_to_g1_point(message); @@ -242,18 +245,21 @@ mod tests { let pk_bytes = key1.get_public_key(); // let cipher_id1 = encrypt_message(&id1, &key1.pk); - let cipher_id1 = encrypt_message(&id1, &PeksKeyPair::recover_public_key(&pk_bytes).unwrap()); + let cipher_id1 = encrypt_message( + &id1, + &PeksKeyPair::recover_public_key(&pk_bytes).unwrap(), + ); let cipher_id2 = encrypt_message(&id2, &key2.pk); let cipher_id3 = encrypt_message(&id3, &key3.pk); - // let cipher_id1 = encrypt_message(&vec![1, 2, 3, 4], &PeksKeyPair::recover_public_key(&pk_bytes).unwrap()); + // let cipher_id1 = encrypt_message(&vec![1, 2, 3, 4], + // &PeksKeyPair::recover_public_key(&pk_bytes).unwrap()); // wedpr_println!("cipher_id1:{:?}", cipher_id1.to_bytes()); - - let sk_bytes = key1.get_secret_key(); // let trapdoor1 = trapdoor(id1, &key1.sk); - let trapdoor1 = trapdoor(id1, &PeksKeyPair::recover_secret_key(&sk_bytes).unwrap()); + let trapdoor1 = + trapdoor(id1, &PeksKeyPair::recover_secret_key(&sk_bytes).unwrap()); assert_eq!(trapdoor_test(&cipher_id1, &trapdoor1), true); assert_eq!(trapdoor_test(&cipher_id2, &trapdoor1), false); assert_eq!(trapdoor_test(&cipher_id3, &trapdoor1), false); @@ -288,34 +294,29 @@ mod tests { let message_hello = hex::decode("01020304").unwrap(); let message_wrong = hex::decode("04030201").unwrap(); - let seed = hex::decode("0195f7500b825a152a42ed730df86de0331ee7b2579c944ee68f682a84e6004d").unwrap(); - + let seed = hex::decode( + "0195f7500b825a152a42ed730df86de0331ee7b2579c944ee68f682a84e6004d", + ) + .unwrap(); let cipher_message_str = "87af4f84f5cabefe4e4e52a98735a4aa7ac39ad56ca14f2d1fef6aea07dae2c42e948df92465940057329e241c13aa3213941282526cc735e6926fc9c4044b1a733e11c82fffcf4f516a9a3fb32dbafac76446270226e96f4a9a6d2537a68e4f2bdca94fd6d2ad2904196174ccb66a5cec3135f1b6310c942bfccae5a4386d34"; let cipher_message_bytes = hex::decode(cipher_message_str).unwrap(); - let cipher_message = PeksCipher::from_bytes(&cipher_message_bytes).unwrap(); - + let cipher_message = + PeksCipher::from_bytes(&cipher_message_bytes).unwrap(); let key1 = generate_key_with_seed(&seed).unwrap(); - let cipher_message_test = encrypt_message_with_seed(&seed, &message_hello, &key1.pk).unwrap(); - // wedpr_println!("cipher_message_test:{:?}", cipher_message_test.to_bytes()); - // wedpr_println!("cipher_message_bytes:{:?}", cipher_message_bytes); - + let cipher_message_test = + encrypt_message_with_seed(&seed, &message_hello, &key1.pk).unwrap(); + // wedpr_println!("cipher_message_test:{:?}", + // cipher_message_test.to_bytes()); wedpr_println!(" + // cipher_message_bytes:{:?}", cipher_message_bytes); let trapdoor1 = trapdoor(&message_hello, &key1.sk); let trapdoor2 = trapdoor(&message_wrong, &key1.sk); + assert_eq!(trapdoor_test(&cipher_message, &trapdoor1), true); - assert_eq!( - trapdoor_test(&cipher_message, &trapdoor1), - true - ); - - assert_eq!( - trapdoor_test(&cipher_message, &trapdoor2), - false - ); - + assert_eq!(trapdoor_test(&cipher_message, &trapdoor2), false); } #[test] @@ -330,14 +331,26 @@ mod tests { let cipher = encrypt_message(&message_hello, &pk); // wedpr_println!("cipher:{:?}", cipher.to_bytes()); - let pk_b: Vec = vec![166, 128, 102, 24, 26, 188, 251, 191, 70, 187, 221, 154, 94, 222, 132, 98, 247, 202, 88, 211, 23, 95, 6, 11, 218, 184, 14, 25, 137, 212, 231, 234, 79, 132, 33, 142, 12, 108, 128, 138, 42, 28, 32, 95, 28, 37, 192, 237, 9, 123, 245, 203, 141, 103, 203, 241, 14, 187, 150, 79, 172, 21, 11, 7, 250, 94, 86, 143, 233, 96, 246, 10, 133, 71, 226, 121, 202, 80, 119, 56, 95, 88, 23, 221, 119, 131, 109, 120, 55, 99, 132, 208, 237, 115, 51, 179]; - let seed_b: Vec = vec![1, 90, 160, 40, 152, 38, 133, 69, 16, 19, 78, 178, 73, 141, 154, 223, 51, 220, 69, 67, 206, 170, 49, 27, 74, 232, 77, 229, 212, 234, 4, 105]; + let pk_b: Vec = vec![ + 166, 128, 102, 24, 26, 188, 251, 191, 70, 187, 221, 154, 94, 222, + 132, 98, 247, 202, 88, 211, 23, 95, 6, 11, 218, 184, 14, 25, 137, + 212, 231, 234, 79, 132, 33, 142, 12, 108, 128, 138, 42, 28, 32, 95, + 28, 37, 192, 237, 9, 123, 245, 203, 141, 103, 203, 241, 14, 187, + 150, 79, 172, 21, 11, 7, 250, 94, 86, 143, 233, 96, 246, 10, 133, + 71, 226, 121, 202, 80, 119, 56, 95, 88, 23, 221, 119, 131, 109, + 120, 55, 99, 132, 208, 237, 115, 51, 179, + ]; + let seed_b: Vec = vec![ + 1, 90, 160, 40, 152, 38, 133, 69, 16, 19, 78, 178, 73, 141, 154, + 223, 51, 220, 69, 67, 206, 170, 49, 27, 74, 232, 77, 229, 212, 234, + 4, 105, + ]; let pk_b_new = PeksKeyPair::recover_public_key(&pk_b).unwrap(); - let cipher_new = encrypt_message_with_seed(&seed_b, &message_hello, &pk_b_new).unwrap(); + let cipher_new = + encrypt_message_with_seed(&seed_b, &message_hello, &pk_b_new) + .unwrap(); // wedpr_println!("cipher_new:{:?}", cipher_new.to_bytes()); - // assert_eq!(pk_bytes, pk.to_bytes()); - } } diff --git a/ffi/ffi_c/ffi_c_equality/src/lib.rs b/ffi/ffi_c/ffi_c_equality/src/lib.rs index 3488edf..5b38eb2 100644 --- a/ffi/ffi_c/ffi_c_equality/src/lib.rs +++ b/ffi/ffi_c/ffi_c_equality/src/lib.rs @@ -41,7 +41,7 @@ pub unsafe extern "C" fn wedpr_pairing_bls128_equality_test( Err(_) => { std::mem::forget(cipher1); std::mem::forget(cipher2); - return FAILURE + return FAILURE; }, }; let cipher2_struct = @@ -50,7 +50,7 @@ pub unsafe extern "C" fn wedpr_pairing_bls128_equality_test( Err(_) => { std::mem::forget(cipher1); std::mem::forget(cipher2); - return FAILURE + return FAILURE; }, }; std::mem::forget(cipher1); @@ -61,7 +61,6 @@ pub unsafe extern "C" fn wedpr_pairing_bls128_equality_test( FAILURE } - #[no_mangle] /// C interface for 'peks_test'. pub unsafe extern "C" fn wedpr_pairing_bls128_peks_test( @@ -70,24 +69,25 @@ pub unsafe extern "C" fn wedpr_pairing_bls128_peks_test( ) -> i8 { let peks_bytes = c_read_raw_pointer(peks_cipher); let trapdoor_bytes = c_read_raw_pointer(trapdoor_cipher); - let peks = - match wedpr_bls12_381::peks::PeksCipher::from_bytes(&peks_bytes) { - Ok(v) => v, - Err(_) => { - std::mem::forget(peks_bytes); - std::mem::forget(trapdoor_bytes); - return FAILURE - }, - }; - let trapdoor = - match wedpr_bls12_381::peks::TrapdoorCipher::from_bytes(&trapdoor_bytes) { - Ok(v) => v, - Err(_) => { - std::mem::forget(peks_bytes); - std::mem::forget(trapdoor_bytes); - return FAILURE - }, - }; + let peks = match wedpr_bls12_381::peks::PeksCipher::from_bytes(&peks_bytes) + { + Ok(v) => v, + Err(_) => { + std::mem::forget(peks_bytes); + std::mem::forget(trapdoor_bytes); + return FAILURE; + }, + }; + let trapdoor = match wedpr_bls12_381::peks::TrapdoorCipher::from_bytes( + &trapdoor_bytes, + ) { + Ok(v) => v, + Err(_) => { + std::mem::forget(peks_bytes); + std::mem::forget(trapdoor_bytes); + return FAILURE; + }, + }; std::mem::forget(peks_bytes); std::mem::forget(trapdoor_bytes); if wedpr_bls12_381::peks::trapdoor_test(&peks, &trapdoor) { diff --git a/ffi/ffi_java/ffi_java_equality/src/lib.rs b/ffi/ffi_java/ffi_java_equality/src/lib.rs index 9eff4e9..798d079 100644 --- a/ffi/ffi_java/ffi_java_equality/src/lib.rs +++ b/ffi/ffi_java/ffi_java_equality/src/lib.rs @@ -33,8 +33,7 @@ use wedpr_ffi_common::utils::{ #[allow(dead_code)] // Result class name is 'com.webank.wedpr.crypto.EqualityResult'. -const RESULT_EQUALITY_CLASS_NAME: &str = - "com/webank/webet/peks/CryptoResult"; +const RESULT_EQUALITY_CLASS_NAME: &str = "com/webank/webet/peks/CryptoResult"; #[allow(dead_code)] fn get_result_jobject<'a>(_env: &'a JNIEnv) -> JObject<'a> { @@ -117,8 +116,6 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_equalityTest( result_jobject.into_inner() } - - #[no_mangle] /// Java interface for /// 'com.webank.wedpr.crypto.NativeInterface->peksGenerateKeyPair'. @@ -128,7 +125,6 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_peksGenerateKe ) -> jobject { let result_jobject = get_result_jobject(&_env); - let result = wedpr_bls12_381::peks::generate_key(); java_safe_set_byte_array_field!( @@ -146,7 +142,6 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_peksGenerateKe result_jobject.into_inner() } - #[no_mangle] /// Java interface for /// 'com.webank.wedpr.crypto.NativeInterface->peksTrapdoor'. @@ -163,16 +158,20 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_peksTrapdoor( let secret_bytes = java_safe_jbytes_to_bytes!(_env, result_jobject, secret_jbyte_array); - let secret = match wedpr_bls12_381::peks::PeksKeyPair::recover_secret_key(&secret_bytes) { - Ok(v) =>v, - Err(_) => return java_set_error_field_and_extract_jobject( - &_env, - &result_jobject, - &format!( - "pkesTrapdoor recover failed, secret_bytes={:?}", - &secret_bytes - ), - ) + let secret = match wedpr_bls12_381::peks::PeksKeyPair::recover_secret_key( + &secret_bytes, + ) { + Ok(v) => v, + Err(_) => { + return java_set_error_field_and_extract_jobject( + &_env, + &result_jobject, + &format!( + "pkesTrapdoor recover failed, secret_bytes={:?}", + &secret_bytes + ), + ) + }, }; let result = wedpr_bls12_381::peks::trapdoor(&message_bytes, &secret); @@ -197,21 +196,28 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_peksTrapdoorTe ) -> jobject { let result_jobject = get_result_jobject(&_env); - let pkes_cipher_bytes = - java_safe_jbytes_to_bytes!(_env, result_jobject, pkes_cipher_jbyte_array); - let trapdoor_cipher_bytes = - java_safe_jbytes_to_bytes!(_env, result_jobject, trapdoor_cipher_jbyte_array); - + let pkes_cipher_bytes = java_safe_jbytes_to_bytes!( + _env, + result_jobject, + pkes_cipher_jbyte_array + ); + let trapdoor_cipher_bytes = java_safe_jbytes_to_bytes!( + _env, + result_jobject, + trapdoor_cipher_jbyte_array + ); let pkes_cipher = - match wedpr_bls12_381::peks::PeksCipher::from_bytes(&pkes_cipher_bytes) { + match wedpr_bls12_381::peks::PeksCipher::from_bytes(&pkes_cipher_bytes) + { Ok(v) => v, Err(_) => { return java_set_error_field_and_extract_jobject( &_env, &result_jobject, &format!( - "pkesTrapdoorTest recover failed, pkes_cipher_bytes={:?}", + "pkesTrapdoorTest recover failed, \ + pkes_cipher_bytes={:?}", &pkes_cipher_bytes ), ) @@ -219,20 +225,24 @@ pub extern "system" fn Java_com_webank_webet_peks_NativeInterface_peksTrapdoorTe }; let trapdoor_cipher = - match wedpr_bls12_381::peks::TrapdoorCipher::from_bytes(&trapdoor_cipher_bytes) { + match wedpr_bls12_381::peks::TrapdoorCipher::from_bytes( + &trapdoor_cipher_bytes, + ) { Ok(v) => v, Err(_) => { return java_set_error_field_and_extract_jobject( &_env, &result_jobject, &format!( - "pkesTrapdoorTest recover failed, trapdoor_cipher_bytes={:?}", + "pkesTrapdoorTest recover failed, \ + trapdoor_cipher_bytes={:?}", &trapdoor_cipher_bytes ), ) }, }; - let result = wedpr_bls12_381::peks::trapdoor_test(&pkes_cipher, &trapdoor_cipher); + let result = + wedpr_bls12_381::peks::trapdoor_test(&pkes_cipher, &trapdoor_cipher); java_safe_set_boolean_field!(_env, result_jobject, result, "booleanResult"); result_jobject.into_inner()