Skip to content

Commit

Permalink
fix mini-program bls
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Jan 3, 2023
1 parent f9a6466 commit 098820d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 71 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crypto/pairing/bls12_381/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ wedpr_l_crypto_hash_sha2 = { path = "../../hash/sha2" }
[dev-dependencies]
criterion = "0.3"
hex = "0.4.2"
wedpr_l_common_coder_base64 = { path = "../../../common/coder/base64" }

[[bench]]
name = "pairing"
Expand Down
93 changes: 40 additions & 53 deletions crypto/pairing/bls12_381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,64 +201,51 @@ mod tests {

#[test]
fn test_from_web() {
let message_hello = b"hello";
let message_wrong = b"wrong";
let message_hello = b"abc";
// let message_hello:Vec<u8> = vec![96,97,98,0,0,0,0,0];
wedpr_println!("message_hello:{:?}", message_hello);
// let message_wrong = b"wrong";
let cipher1_hello = encrypt_message(message_hello);
let cipher1_wrong = encrypt_message(message_wrong);
// let cipher1_wrong = encrypt_message(message_wrong);

let web_hello1 = "912caa3f6fb385af33cc9059ba87523a5ab2ff0112fd21d239ec1ea93a767ae68068a7da29d45dd1665740c32593461f868a81a830ca7db6943dc56512f9507373b35beeec8a4a2f77fe03a72ba6ec0b94bb79de3ff9f24c0bc39e4a75e35c2816c33de0310b2194a48d0eb69cfc86b76e67238a94cea87459c0359451362c8ea6321d9d57dc03d55b219fd20e1188a9";
let web_hello1 = "a91c14d9111e95206e5be9e644f68dd6ddb5865da5bfab80d4c7fb7a1a1299db401e200bb401adc87da751283dd8cb2984820a100894e50583daec6261bd8728d6c80689fa02d8f3e7fae23329755229c98456db47f78071e03dd4344e7e8dc3029bf4809a8fc3198dcd4c6810465cbc42fc57329b1d980f70b2a383507c257e35f33f2851e0ab2a40bb24606900c152";
let web_bytes1 = hex::decode(web_hello1).unwrap();

// let web_bytes1 =
// [178,100,235,129,182,124,252,216,28,9,219,125,168,153,128,192,138,
// 216,173,172,141,46,235,167,172,72,82,241,145,129,208,150,80,80,214,
// 237,229,158,116,93,141,67,101,166,116,228,108,0,175,152,239,234,129,
// 64,178,132,188,240,52,31,118,115,145,215,203,27,19,162,231,180,136,
// 185,63,110,117,174,203,105,51,56,13,138,252,231,179,40,235,128,6,39,
// 120,220,191,62,60,37,16,61,3,197,40,175,205,130,189,125,62,134,80,
// 249,131,21,43,60,171,164,147,6,72,214,246,66,219,97,37,203,31,211,33,
// 202,115,242,164,224,125,110,238,238,95,158,248,74,24,124];
let web_hello2 = "8fa65c08f9d137934380af14cd659370b8c7e51e8df839f31fb3edaeb50d70a8aed3e7ef441927401e8f40691776292188adecd16201d2f868b2d862771bcb1ade33492db124a4b5329c32ae24971b4980f5649134eb9ac00615286e1dc0b3ae02d27938a92f7a49b9e830fd3857c4c7648f65879a2b018aef26dbfe253ef25e7e7c36dc92d5de4a2fce6e0c4d1c7803";
let web_bytes2 = hex::decode(web_hello2).unwrap();
let cipher1_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_bytes1 =
// [178,100,235,129,182,124,252,216,28,9,219,125,168,153,128,192,138,
// 216,173,172,141,46,235,167,172,72,82,241,145,129,208,150,80,80,214,
// 237,229,158,116,93,141,67,101,166,116,228,108,0,175,152,239,234,129,
// 64,178,132,188,240,52,31,118,115,145,215,203,27,19,162,231,180,136,
// 185,63,110,117,174,203,105,51,56,13,138,252,231,179,40,235,128,6,39,
// 120,220,191,62,60,37,16,61,3,197,40,175,205,130,189,125,62,134,80,
// 249,131,21,43,60,171,164,147,6,72,214,246,66,219,97,37,203,31,211,33,
// 202,115,242,164,224,125,110,238,238,95,158,248,74,24,124];
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(&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_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);
// 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_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(&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_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);
}

#[test]
Expand Down
29 changes: 27 additions & 2 deletions crypto/pairing/bls12_381/src/peks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ pub fn trapdoor_test(
#[cfg(test)]
mod tests {
use crate::peks::*;
use wedpr_l_common_coder_base64::WedprBase64;
use wedpr_l_utils::traits::Coder;

#[test]
fn test_peks() {
Expand Down Expand Up @@ -296,8 +298,8 @@ mod tests {

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);
// 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);
Expand All @@ -315,4 +317,27 @@ mod tests {
);

}

#[test]
fn test_base64_decode() {
let base64_pk = "ifC8LWz8S67q2zmg4Jnt9RoPtjj4HPBq2Ga47aQlSGdz+V3ySo6yQiMSvpOUudZ2Brv8u4rViiFfC7yroiFNcVAvFf/f6ftcqDZQiRodvDqJG5KqlYa8IJHzxm00ZW5I";
let base64 = WedprBase64::default();
let pk_bytes = base64.decode(base64_pk).unwrap();
// wedpr_println!("pk_bytes:{:?}", pk_bytes);
let pk = PeksKeyPair::recover_public_key(&pk_bytes).unwrap();
// wedpr_println!("pk_bytes:{:?}", pk_bytes);
let message_hello = hex::decode("01020304").unwrap();
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_new = PeksKeyPair::recover_public_key(&pk_b).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());

}
}
4 changes: 2 additions & 2 deletions protos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "Library of WeDPR protobuf definitions and their generated code."
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
protobuf = "2.22.1"
protoc-rust = "2.22.1"
protobuf = "2.28.0"
protoc-rust = "2.28.0"
wedpr_l_utils = "1.1.0"

4 changes: 2 additions & 2 deletions protos/src/generated/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.22.1. Do not edit
// This file is generated by rust-protobuf 2.28.0. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_28_0;

#[derive(PartialEq,Clone,Default)]
pub struct Keypair {
Expand Down
4 changes: 2 additions & 2 deletions protos/src/generated/ot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.22.1. Do not edit
// This file is generated by rust-protobuf 2.28.0. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_28_0;

#[derive(PartialEq,Clone,Default)]
pub struct OtReceiverSecretKOutOfN {
Expand Down
4 changes: 2 additions & 2 deletions protos/src/generated/zkp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.22.1. Do not edit
// This file is generated by rust-protobuf 2.28.0. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_28_0;

#[derive(PartialEq,Clone,Default)]
pub struct BalanceProof {
Expand Down

0 comments on commit 098820d

Please sign in to comment.