From 098820d88bcf44de89cef1a891bea7ea62235129 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 3 Jan 2023 18:26:53 +0800 Subject: [PATCH] fix mini-program bls --- Cargo.lock | 17 ++--- crypto/pairing/bls12_381/Cargo.toml | 1 + crypto/pairing/bls12_381/src/lib.rs | 93 ++++++++++++---------------- crypto/pairing/bls12_381/src/peks.rs | 29 ++++++++- protos/Cargo.toml | 4 +- protos/src/generated/common.rs | 4 +- protos/src/generated/ot.rs | 4 +- protos/src/generated/zkp.rs | 4 +- 8 files changed, 85 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4200f54..34a0911 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,24 +1311,24 @@ dependencies = [ [[package]] name = "protobuf" -version = "2.22.1" +version = "2.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b7f4a129bb3754c25a4e04032a90173c68f85168f77118ac4cb4936e7f06f92" +checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94" [[package]] name = "protobuf-codegen" -version = "2.22.1" +version = "2.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d2fa3a461857508103b914da60dd7b489c1a834967c2e214ecc1496f0c486a" +checksum = "033460afb75cf755fcfc16dfaed20b86468082a2ea24e05ac35ab4a099a017d6" dependencies = [ "protobuf", ] [[package]] name = "protoc" -version = "2.22.1" +version = "2.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6653d384a260fedff0a466e894e05c5b8d75e261a14e9f93e81e43ef86cad23" +checksum = "a0218039c514f9e14a5060742ecd50427f8ac4f85a6dc58f2ddb806e318c55ee" dependencies = [ "log", "which", @@ -1336,9 +1336,9 @@ dependencies = [ [[package]] name = "protoc-rust" -version = "2.22.1" +version = "2.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5198afa8fca3f419b36db9a70ede51ff845938ef0386b49f4b02a5a322015a6" +checksum = "22f8a182bb17c485f20bdc4274a8c39000a61024cfe461c799b50fec77267838" dependencies = [ "protobuf", "protobuf-codegen", @@ -2145,6 +2145,7 @@ dependencies = [ "hex", "rand 0.8.3", "sha2 0.9.5", + "wedpr_l_common_coder_base64 1.1.0", "wedpr_l_crypto_hash_sha2", "wedpr_l_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/crypto/pairing/bls12_381/Cargo.toml b/crypto/pairing/bls12_381/Cargo.toml index 3e5e653..6da82e2 100644 --- a/crypto/pairing/bls12_381/Cargo.toml +++ b/crypto/pairing/bls12_381/Cargo.toml @@ -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" diff --git a/crypto/pairing/bls12_381/src/lib.rs b/crypto/pairing/bls12_381/src/lib.rs index 5be1dcc..70b772a 100644 --- a/crypto/pairing/bls12_381/src/lib.rs +++ b/crypto/pairing/bls12_381/src/lib.rs @@ -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 = 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] diff --git a/crypto/pairing/bls12_381/src/peks.rs b/crypto/pairing/bls12_381/src/peks.rs index fb19c4c..a2b5366 100644 --- a/crypto/pairing/bls12_381/src/peks.rs +++ b/crypto/pairing/bls12_381/src/peks.rs @@ -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() { @@ -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); @@ -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 = 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(); + // wedpr_println!("cipher_new:{:?}", cipher_new.to_bytes()); + + + // assert_eq!(pk_bytes, pk.to_bytes()); + + } } diff --git a/protos/Cargo.toml b/protos/Cargo.toml index 397c4b9..9e9e3a6 100644 --- a/protos/Cargo.toml +++ b/protos/Cargo.toml @@ -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" diff --git a/protos/src/generated/common.rs b/protos/src/generated/common.rs index d7be59b..a3ea76b 100644 --- a/protos/src/generated/common.rs +++ b/protos/src/generated/common.rs @@ -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 @@ -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 { diff --git a/protos/src/generated/ot.rs b/protos/src/generated/ot.rs index c26a01c..62f0963 100644 --- a/protos/src/generated/ot.rs +++ b/protos/src/generated/ot.rs @@ -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 @@ -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 { diff --git a/protos/src/generated/zkp.rs b/protos/src/generated/zkp.rs index 5c5e370..fe8accd 100644 --- a/protos/src/generated/zkp.rs +++ b/protos/src/generated/zkp.rs @@ -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 @@ -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 {