From 322a038d2c9b29b9e7487015fac7a1718312985b Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Wed, 10 Aug 2022 00:13:44 +0800 Subject: [PATCH] support use sm-algorithm to generate signature for acv when using sm feature --- .github/workflows/workflow.yml | 24 +++++++++++++++++++ ffi/ffi_java/ffi_java_acv/Cargo.toml | 8 +++++-- protos/Cargo.toml | 12 ++++++++-- protos/src/config.rs | 20 +++++++++++++--- .../anonymous_ciphertext_voting/Cargo.toml | 7 +++--- .../anonymous_ciphertext_voting/src/config.rs | 14 ----------- .../anonymous_ciphertext_voting/src/lib.rs | 16 ++++++------- .../anonymous_ciphertext_voting/src/main.rs | 5 ++-- 8 files changed, 72 insertions(+), 34 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 1a06543..db4a678 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -64,6 +64,14 @@ jobs: with: name: libffi_c_hdk.dylib.tar.gz path: target/release/libffi_c_hdk.dylib + - name: build sm-acv + run: cd ffi/ffi_java/ffi_java_acv && cargo clean && cargo build --release --no-default-features --features sm + - name: rename libffi_java_acv + run: mv target/release/libffi_java_acv.dylib target/release/libffi_java_sm_acv.dylib + - uses: actions/upload-artifact@v2 + with: + name: libffi_java_sm_acv.dylib.tar.gz + path: target/release/libffi_java_sm_acv.dylib build_with_centos: name: build_with_centos @@ -116,6 +124,14 @@ jobs: with: name: libffi_c_hdk.so.tar.gz path: target/release/libffi_c_hdk.so + - name: build sm-acv + run: cd ffi/ffi_java/ffi_java_acv && cargo clean && cargo build --release --no-default-features --features sm + - name: rename libffi_java_acv + run: mv target/release/libffi_java_acv.so target/release/libffi_java_sm_acv.so + - uses: actions/upload-artifact@v2 + with: + name: libffi_java_sm_acv.so.tar.gz + path: target/release/libffi_java_sm_acv.so build_with_windows: name: build_with_windows runs-on: ${{ matrix.os }} @@ -161,3 +177,11 @@ jobs: with: name: ffi_c_hdk.dll.zip path: D:\a\WeDPR-Lab-Core\WeDPR-Lab-Core\target\release\ffi_c_hdk.dll + - name: build sm-acv + run: cd ffi/ffi_java/ffi_java_acv && cargo clean && cargo build --release --no-default-features --features sm + - name: rename libffi_java_acv + run: mv D:\a\WeDPR-Lab-Core\WeDPR-Lab-Core\target\release\ffi_java_acv.dll D:\a\WeDPR-Lab-Core\WeDPR-Lab-Core\target\release\ffi_java_sm_acv.dll + - uses: actions/upload-artifact@v2 + with: + name: ffi_java_sm_acv.dll.zip + path: D:\a\WeDPR-Lab-Core\WeDPR-Lab-Core\target\release\ffi_java_sm_acv.dll diff --git a/ffi/ffi_java/ffi_java_acv/Cargo.toml b/ffi/ffi_java/ffi_java_acv/Cargo.toml index bc5d9a8..fa3ea6d 100644 --- a/ffi/ffi_java/ffi_java_acv/Cargo.toml +++ b/ffi/ffi_java/ffi_java_acv/Cargo.toml @@ -9,6 +9,10 @@ edition = "2018" name = "ffi_java_acv" crate-type = [ "cdylib", "staticlib" ] +[features] +default = ["wedpr_s_protos/wedpr_f_secp256k1_signature"] +sm = ["wedpr_s_protos/wedpr_f_sm_signature"] + [dependencies] jni = "0.13.0" protobuf = "2.22.1" @@ -16,8 +20,8 @@ wedpr_ffi_common = "1.1.0" wedpr_ffi_macros = "1.1.0" wedpr_l_crypto_zkp_utils = {version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"} -wedpr_s_protos = { path = "../../../protos" } -wedpr_s_anonymous_ciphertext_voting = { path = "../../../solution/anonymous_ciphertext_voting" } +wedpr_s_protos = { path = "../../../protos", default-features = false} +wedpr_s_anonymous_ciphertext_voting = { path = "../../../solution/anonymous_ciphertext_voting", default-features = false} # This is required to generate C/C++ header files. [build-dependencies] diff --git a/protos/Cargo.toml b/protos/Cargo.toml index 3a58817..4151aa2 100644 --- a/protos/Cargo.toml +++ b/protos/Cargo.toml @@ -6,6 +6,12 @@ edition = "2018" license = "Apache-2.0" description = "Library of WeDPR protobuf definitions and their generated code." +[features] +default = ["wedpr_f_secp256k1_signature"] +sm = ["wedpr_f_sm_signature"] +wedpr_f_sm_signature = ["wedpr_l_crypto_hash_sm3", "wedpr_l_crypto_signature_sm2"] +wedpr_f_secp256k1_signature = ["wedpr_l_crypto_hash_keccak256", "wedpr_l_crypto_signature_secp256k1"] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -14,5 +20,7 @@ protobuf = "2.22.1" protoc-rust = "2.22.1" wedpr_l_utils = "1.1.0" wedpr_l_crypto_zkp_utils = {version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"} -wedpr_l_crypto_hash_keccak256 = "1.1.0" -wedpr_l_crypto_signature_secp256k1 = "1.1.0" +wedpr_l_crypto_hash_sm3 = {version = "1.1.0", optional = true} +wedpr_l_crypto_signature_sm2 = {version = "1.1.0", optional = true} +wedpr_l_crypto_hash_keccak256 = {version = "1.1.0", optional = true} +wedpr_l_crypto_signature_secp256k1 = {version = "1.1.0", optional = true} diff --git a/protos/src/config.rs b/protos/src/config.rs index aa9604a..cc4b550 100644 --- a/protos/src/config.rs +++ b/protos/src/config.rs @@ -1,14 +1,28 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. //! Config of anonymous ciphertext voting (ACV) solution. +#[cfg(feature = "wedpr_f_sm_signature")] +use wedpr_l_crypto_hash_sm3::WedprSm3; +#[cfg(feature = "wedpr_f_sm_signature")] +use wedpr_l_crypto_signature_sm2::WedprSm2p256v1; +#[cfg(feature = "wedpr_f_sm_signature")] +lazy_static! { + /// Shared signature algorithm reference for quick implementation replacement. + pub static ref SIGNATURE: WedprSm2p256v1 = + WedprSm2p256v1::default(); + /// Shared hash algorithm reference for quick implementation replacement. + pub static ref HASH: WedprSm3 = WedprSm3::default(); +} + +#[cfg(feature = "wedpr_f_secp256k1_signature")] use wedpr_l_crypto_hash_keccak256::WedprKeccak256; +#[cfg(feature = "wedpr_f_secp256k1_signature")] use wedpr_l_crypto_signature_secp256k1::WedprSecp256k1Recover; - -// TODO: support sm-crypto +#[cfg(feature = "wedpr_f_secp256k1_signature")] lazy_static! { /// Shared signature algorithm reference for quick implementation replacement. pub static ref SIGNATURE: WedprSecp256k1Recover = - WedprSecp256k1Recover::default(); + WedprSecp256k1Recover::default(); /// Shared hash algorithm reference for quick implementation replacement. pub static ref HASH: WedprKeccak256 = WedprKeccak256::default(); } diff --git a/solution/anonymous_ciphertext_voting/Cargo.toml b/solution/anonymous_ciphertext_voting/Cargo.toml index 720f278..ba6f784 100644 --- a/solution/anonymous_ciphertext_voting/Cargo.toml +++ b/solution/anonymous_ciphertext_voting/Cargo.toml @@ -7,16 +7,17 @@ license = "Apache-2.0" description = "Library of anonymous ciphertext voting (ACV) solution." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["wedpr_s_protos/wedpr_f_secp256k1_signature"] +sm = ["wedpr_s_protos/wedpr_f_sm_signature"] [dependencies] curve25519-dalek = { version = "1", features = [ "serde" ] } lazy_static = "1.4.0" -wedpr_l_crypto_hash_keccak256 = "1.1.0" -wedpr_l_crypto_signature_secp256k1 = "1.1.0" wedpr_l_crypto_zkp_discrete_logarithm_proof = {version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"} wedpr_l_crypto_zkp_range_proof = "1.2.0" wedpr_l_crypto_zkp_utils = {version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"} wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" -wedpr_s_protos = { path = "../../protos" } +wedpr_s_protos = { path = "../../protos", default-features = false} colored = "1.8" diff --git a/solution/anonymous_ciphertext_voting/src/config.rs b/solution/anonymous_ciphertext_voting/src/config.rs index 75c4cb8..1d1a666 100644 --- a/solution/anonymous_ciphertext_voting/src/config.rs +++ b/solution/anonymous_ciphertext_voting/src/config.rs @@ -1,17 +1,3 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - -//! Config of anonymous ciphertext voting (ACV) solution. - -use wedpr_l_crypto_hash_keccak256::WedprKeccak256; -use wedpr_l_crypto_signature_secp256k1::WedprSecp256k1Recover; - -lazy_static! { - /// Shared signature algorithm reference for quick implementation replacement. - pub static ref SIGNATURE: WedprSecp256k1Recover = - WedprSecp256k1Recover::default(); - /// Shared hash algorithm reference for quick implementation replacement. - pub static ref HASH: WedprKeccak256 = WedprKeccak256::default(); -} - /// Poll result key for total ballots. pub const POLL_RESULT_KEY_TOTAL_BALLOTS: &str = "Wedpr_voting_total_ballots"; diff --git a/solution/anonymous_ciphertext_voting/src/lib.rs b/solution/anonymous_ciphertext_voting/src/lib.rs index ef254a8..939e605 100644 --- a/solution/anonymous_ciphertext_voting/src/lib.rs +++ b/solution/anonymous_ciphertext_voting/src/lib.rs @@ -18,18 +18,18 @@ pub mod voter; #[cfg(test)] mod tests { use super::*; - use crate::{ - config::{POLL_RESULT_KEY_TOTAL_BALLOTS, SIGNATURE}, - coordinator, - }; + use crate::{config::POLL_RESULT_KEY_TOTAL_BALLOTS, coordinator}; use wedpr_l_crypto_zkp_utils::{ bytes_to_point, get_random_scalar, scalar_to_bytes, }; use wedpr_l_utils::traits::Signature; - use wedpr_s_protos::generated::acv::{ - CandidateList, CounterParametersStorage, CounterSecret, - DecryptedResultPartStorage, UnlistedVoteChoice, VoteChoice, - VoteChoices, VoteStorage, VoterSecret, + use wedpr_s_protos::{ + config::SIGNATURE, + generated::acv::{ + CandidateList, CounterParametersStorage, CounterSecret, + DecryptedResultPartStorage, UnlistedVoteChoice, VoteChoice, + VoteChoices, VoteStorage, VoterSecret, + }, }; #[test] diff --git a/solution/anonymous_ciphertext_voting/src/main.rs b/solution/anonymous_ciphertext_voting/src/main.rs index ee8e4c8..900b6f1 100644 --- a/solution/anonymous_ciphertext_voting/src/main.rs +++ b/solution/anonymous_ciphertext_voting/src/main.rs @@ -4,6 +4,7 @@ use wedpr_l_crypto_zkp_utils::bytes_to_point; use wedpr_l_utils::traits::Signature; +use wedpr_s_protos::config::SIGNATURE; use wedpr_s_protos::generated::acv::{ CandidateList, CounterParametersStorage, CounterSecret, @@ -13,8 +14,8 @@ use wedpr_s_protos::generated::acv::{ extern crate wedpr_s_anonymous_ciphertext_voting; use colored::Colorize; use wedpr_s_anonymous_ciphertext_voting::{ - config::{POLL_RESULT_KEY_TOTAL_BALLOTS, SIGNATURE}, - coordinator, counter, verifier, voter, + config::POLL_RESULT_KEY_TOTAL_BALLOTS, coordinator, counter, verifier, + voter, }; fn main() {