Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Jul 26, 2023
1 parent 098820d commit 44aeded
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 116 deletions.
5 changes: 5 additions & 0 deletions crypto/block_cipher/aes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions crypto/block_cipher/sm4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
43 changes: 24 additions & 19 deletions crypto/oblivious_transfer/base_ot/src/ot_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -41,7 +46,6 @@ pub struct CiphertextItemKOutOfN {
pub key_basepoint: RistrettoPoint,
pub encrypted_message: Vec<Vec<u8>>,
}
//
// pub struct TwoDeepVector {}
//
// impl TwoDeepVector {
Expand Down Expand Up @@ -109,13 +113,10 @@ pub struct CiphertextItemKOutOfN {
pub struct OtKvKOutOfN {}

impl OtKvKOutOfN {

// pub fn ot_make_choice(choice_list: &Vec<Vec<u8>>) -> 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
Expand Down Expand Up @@ -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
Expand All @@ -154,7 +156,6 @@ impl OtKvKOutOfN {
// ]).compress().to_bytes().to_vec());
}


// ot_receiver_commitment.set_point_z(point_z_list);
(
ReceiverSecretKOutOfN {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<u8> = 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");
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
);

}
}
28 changes: 16 additions & 12 deletions crypto/pairing/bls12_381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -211,41 +210,46 @@ 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();
// let cipher2_m1_recover =
// 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]
Expand Down
89 changes: 51 additions & 38 deletions crypto/pairing/bls12_381/src/peks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,7 +47,9 @@ impl PeksKeyPair {
G2Affine::from(self.pk).to_compressed().to_vec()
}

pub fn recover_public_key(pk_bytes: &[u8]) -> Result<G2Projective, WedprError> {
pub fn recover_public_key(
pk_bytes: &[u8],
) -> Result<G2Projective, WedprError> {
if pk_bytes.len() != 96 {
return Err(WedprError::FormatError);
}
Expand All @@ -65,7 +67,9 @@ impl PeksKeyPair {
self.sk.to_bytes().to_vec()
}

pub fn recover_secret_key(scalar_bytes: &[u8]) -> Result<Scalar, WedprError> {
pub fn recover_secret_key(
scalar_bytes: &[u8],
) -> Result<Scalar, WedprError> {
if scalar_bytes.len() != 32 {
return Err(WedprError::FormatError);
}
Expand Down Expand Up @@ -141,14 +145,13 @@ impl TrapdoorCipher {
}
}


pub fn seed_to_scalar(seed: &[u8]) -> Result<Scalar, WedprError> {
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());
Expand All @@ -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<PeksCipher, WedprError> {

pub fn encrypt_message_with_seed(
seed: &[u8],
message: &[u8],
pk: &G2Projective,
) -> Result<PeksCipher, WedprError> {
let blinding = seed_to_scalar(seed)?;

let message_g1 = message_to_g1_point(message);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand All @@ -330,14 +331,26 @@ mod tests {
let cipher = encrypt_message(&message_hello, &pk);
// wedpr_println!("cipher:{:?}", cipher.to_bytes());

let pk_b: Vec<u8> = 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<u8> = 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<u8> = 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<u8> = 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());

}
}
Loading

0 comments on commit 44aeded

Please sign in to comment.