From cdc153f00ca2ad6de5d754b532ba6946c127fe81 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Thu, 27 May 2021 19:44:25 +0800 Subject: [PATCH 01/17] update vote zkp --- .../zkp/discrete_logarithm_proof/Cargo.toml | 4 +- .../zkp/discrete_logarithm_proof/src/lib.rs | 684 +++++++++++++++++- crypto/zkp/range_proof/Cargo.toml | 2 +- .../ffi_java_fisco_bcos_sdk/src/lib.rs | 3 +- .../ffi_java_fisco_bcos_sdk/src/vrf.rs | 10 +- protos/crypto/zkp.proto | 3 + protos/src/generated/common.rs | 2 - protos/src/generated/zkp.rs | 136 +++- release_note.txt | 2 +- third_party/fisco_bcos_java_sdk/src/lib.rs | 11 +- 10 files changed, 837 insertions(+), 20 deletions(-) diff --git a/crypto/zkp/discrete_logarithm_proof/Cargo.toml b/crypto/zkp/discrete_logarithm_proof/Cargo.toml index a6cde7a..8c55f9f 100644 --- a/crypto/zkp/discrete_logarithm_proof/Cargo.toml +++ b/crypto/zkp/discrete_logarithm_proof/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wedpr_l_crypto_zkp_discrete_logarithm_proof" -version = "1.1.0" +version = "1.2.0" authors = [ "WeDPR " ] edition = "2018" license = "Apache-2.0" @@ -12,7 +12,7 @@ description = "Library of WeDPR shared zkp Function implement discrete logarithm curve25519-dalek = { version = "1", features = [ "serde" ] } wedpr_l_crypto_zkp_utils = "1.1.0" wedpr_l_macros = "1.0.0" -wedpr_l_protos = "1.1.0" +wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" rand = "0.6" diff --git a/crypto/zkp/discrete_logarithm_proof/src/lib.rs b/crypto/zkp/discrete_logarithm_proof/src/lib.rs index babc00b..d06dadb 100644 --- a/crypto/zkp/discrete_logarithm_proof/src/lib.rs +++ b/crypto/zkp/discrete_logarithm_proof/src/lib.rs @@ -13,6 +13,471 @@ use wedpr_l_crypto_zkp_utils::{ use wedpr_l_protos::generated::zkp::{BalanceProof, EqualityProof}; use wedpr_l_utils::error::WedprError; +/// Proves three commitments satisfying either or equality relationships, i.e. +/// the values embedded in c1_point = c1_value * c_basepoint + c1_blinding * +/// blinding_basepoint c2_point = c2_value * c_basepoint + c2_blinding * +/// blinding_basepoint c3_point = c3_blinding * blinding_basepoint +/// where c1_value = c2_value or 0, +/// It returns a proof for the above equality relationship. +pub fn prove_either_equality_relationship_proof( + c1_value: u64, + c2_value: u64, + c1_blinding: &Scalar, + c2_blinding: &Scalar, + c3_blinding: &Scalar, + c_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> BalanceProof { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let blinding_c = get_random_scalar(); + let blinding_d = get_random_scalar(); + let blinding_e = get_random_scalar(); + let blinding_f = get_random_scalar(); + let blinding_w = get_random_scalar(); + let c1_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c1_value), *c1_blinding], + &[*c_basepoint, *blinding_basepoint], + ); + let c2_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c2_value), *c2_blinding], + &[*c_basepoint, *blinding_basepoint], + ); + let c3_point = c3_blinding * blinding_basepoint; + + let (check1, check2, m1, m2, m3, m4, m5, m6) = if c1_value == c2_value { + let t1_p = + RistrettoPoint::multiscalar_mul(&[blinding_a, blinding_b], &[ + *c_basepoint, + *blinding_basepoint, + ]); + let t2_p = + RistrettoPoint::multiscalar_mul(&[blinding_a, blinding_c], &[ + *c_basepoint, + *blinding_basepoint, + ]); + let t3_p = RistrettoPoint::multiscalar_mul( + &[blinding_w, blinding_d, blinding_e], + &[c3_point, *c_basepoint, *blinding_basepoint], + ); + let t4_p = RistrettoPoint::multiscalar_mul( + &[blinding_w, blinding_d, blinding_f], + &[c1_point, *c_basepoint, *blinding_basepoint], + ); + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&t2_p)); + hash_vec.append(&mut point_to_bytes(&t3_p)); + hash_vec.append(&mut point_to_bytes(&t4_p)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(&c3_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + + let check = hash_to_scalar(&hash_vec) - blinding_w; + ( + check, + blinding_w, + blinding_a - (check * Scalar::from(c2_value)), + blinding_b - (check * c2_blinding), + blinding_c - (check * c1_blinding), + blinding_d, + blinding_e, + blinding_f, + ) + } else if c1_value == 0 { + let t1_p = RistrettoPoint::multiscalar_mul( + &[blinding_w, blinding_a, blinding_b], + &[c2_point, *c_basepoint, *blinding_basepoint], + ); + let t2_p = RistrettoPoint::multiscalar_mul( + &[blinding_w, blinding_a, blinding_c], + &[c1_point, *c_basepoint, *blinding_basepoint], + ); + let t3_p = + RistrettoPoint::multiscalar_mul(&[blinding_d, blinding_e], &[ + *c_basepoint, + *blinding_basepoint, + ]); + let t4_p = + RistrettoPoint::multiscalar_mul(&[blinding_d, blinding_f], &[ + *c_basepoint, + *blinding_basepoint, + ]); + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&t2_p)); + hash_vec.append(&mut point_to_bytes(&t3_p)); + hash_vec.append(&mut point_to_bytes(&t4_p)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(&c3_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + + let check = hash_to_scalar(&hash_vec) - blinding_w; + ( + blinding_w, + check, + blinding_a, + blinding_b, + blinding_c, + blinding_d, + blinding_e - (check * c3_blinding), + blinding_f - (check * c1_blinding), + ) + } else { + return BalanceProof::new(); + }; + + let mut proof = BalanceProof::new(); + proof.set_check1(scalar_to_bytes(&check1)); + proof.set_check2(scalar_to_bytes(&check2)); + proof.set_m1(scalar_to_bytes(&m1)); + proof.set_m2(scalar_to_bytes(&m2)); + proof.set_m3(scalar_to_bytes(&m3)); + proof.set_m4(scalar_to_bytes(&m4)); + proof.set_m5(scalar_to_bytes(&m5)); + proof.set_m6(scalar_to_bytes(&m6)); + proof +} + +/// Verifies owner know a commitment's secret value c_value and c_blinding, i.e. +/// the values embedded in c_point = c_value * c_basepoint + c_blinding * +/// blinding_basepoint +pub fn verify_either_equality_relationship_proof( + c1_point: &RistrettoPoint, + c2_point: &RistrettoPoint, + c3_point: &RistrettoPoint, + proof: &BalanceProof, + c_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> Result { + let check1 = bytes_to_scalar(proof.get_check1())?; + let check2 = bytes_to_scalar(proof.get_check2())?; + let m1 = bytes_to_scalar(proof.get_m1())?; + let m2 = bytes_to_scalar(proof.get_m2())?; + let m3 = bytes_to_scalar(proof.get_m3())?; + let m4 = bytes_to_scalar(proof.get_m4())?; + let m5 = bytes_to_scalar(proof.get_m5())?; + let m6 = bytes_to_scalar(proof.get_m6())?; + + let t1_v = RistrettoPoint::multiscalar_mul(&[check1, m1, m2], &[ + *c2_point, + *c_basepoint, + *blinding_basepoint, + ]); + let t2_v = RistrettoPoint::multiscalar_mul(&[check1, m1, m3], &[ + *c1_point, + *c_basepoint, + *blinding_basepoint, + ]); + let t3_v = RistrettoPoint::multiscalar_mul(&[check2, m4, m5], &[ + *c3_point, + *c_basepoint, + *blinding_basepoint, + ]); + let t4_v = RistrettoPoint::multiscalar_mul(&[check2, m4, m6], &[ + *c1_point, + *c_basepoint, + *blinding_basepoint, + ]); + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_v)); + hash_vec.append(&mut point_to_bytes(&t2_v)); + hash_vec.append(&mut point_to_bytes(&t3_v)); + hash_vec.append(&mut point_to_bytes(&t4_v)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(&c3_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + let check = hash_to_scalar(&hash_vec); + + if check == (check1 + check2) { + return Ok(true); + } + Ok(false) +} + +/// Proves owner know a commitment's secret value c_value and c_blinding, i.e. +/// the values embedded in c_point = c_value * c_basepoint + c_blinding * +/// blinding_basepoint It returns a proof for the above balance relationship. +pub fn prove_knowledge_proof( + c_value: u64, + c_blinding: &Scalar, + c_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> BalanceProof { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let t1_p = RistrettoPoint::multiscalar_mul(&[blinding_a, blinding_b], &[ + *c_basepoint, + *blinding_basepoint, + ]); + let c_scalar_value = Scalar::from(c_value); + let c_point = + RistrettoPoint::multiscalar_mul(&[c_scalar_value, *c_blinding], &[ + *c_basepoint, + *blinding_basepoint, + ]); + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&c_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + + let check = hash_to_scalar(&hash_vec); + let m1 = blinding_a - (check * c_scalar_value); + let m2 = blinding_b - (check * c_blinding); + let mut proof = BalanceProof::new(); + proof.set_t1(point_to_bytes(&t1_p)); + proof.set_m1(scalar_to_bytes(&m1)); + proof.set_m2(scalar_to_bytes(&m2)); + proof +} + +/// Verifies owner know a commitment's secret value c_value and c_blinding, i.e. +/// the values embedded in c_point = c_value * c_basepoint + c_blinding * +/// blinding_basepoint +pub fn verify_knowledge_proof( + c_point: &RistrettoPoint, + proof: &BalanceProof, + c_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> Result { + let t1_p = bytes_to_point(proof.get_t1())?; + let m1 = bytes_to_scalar(proof.get_m1())?; + let m2 = bytes_to_scalar(proof.get_m2())?; + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&c_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + let check = hash_to_scalar(&hash_vec); + let t1_v = RistrettoPoint::multiscalar_mul(&[check, m1, m2], &[ + *c_point, + *c_basepoint, + *blinding_basepoint, + ]); + + if t1_v == t1_p { + return Ok(true); + } + Ok(false) +} + +/// Verifies all commitment pairs satisfying knowledge relationships, +/// where each commitment pair contains one commitment points, +/// c_point = c_point_list[i], +/// the values embedded in c_point = c_value * c_basepoint + c_blinding * +/// blinding_basepoint +pub fn verify_knowledge_proof_in_batch( + c_point_list: &Vec, + proof_list: &Vec, + c_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> Result { + if c_point_list.len() != proof_list.len() { + return Err(WedprError::FormatError); + } + let mut t1_sum_expected: RistrettoPoint = Default::default(); + let mut c1_c_expected: RistrettoPoint = Default::default(); + let mut m1_expected: Scalar = Scalar::zero(); + let mut m2_expected: Scalar = Scalar::zero(); + + for i in 0..c_point_list.len() { + // 8 bit random scalar + let random_scalar = get_random_u8(); + let blinding_factor = Scalar::from(random_scalar); + let m1 = bytes_to_scalar(proof_list[i].get_m1())?; + let m2 = bytes_to_scalar(proof_list[i].get_m2())?; + let t1_p = bytes_to_point(proof_list[i].get_t1())?; + let c_point = c_point_list[i]; + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&c_point)); + hash_vec.append(&mut point_to_bytes(c_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + let check = hash_to_scalar(&hash_vec); + + let c_factor = blinding_factor * check; + m1_expected += blinding_factor * m1; + m2_expected += blinding_factor * m2; + t1_sum_expected += small_scalar_point_mul(random_scalar, t1_p); + c1_c_expected += c_factor * c_point; + } + let t1_compute_sum_final = m1_expected * c_basepoint + + m2_expected * blinding_basepoint + + c1_c_expected; + + if t1_compute_sum_final == t1_sum_expected { + return Ok(true); + } + Ok(false) +} + +/// Proves two commitments satisfying an equality relationship, i.e. +/// the values embedded in c1_point and c2_point satisfying c1_blinding = +/// c2_blinding, where c1_point = c1_value * c1_basepoint + c1_blinding * +/// blinding_basepoint, c2_point = c2_blinding * c2_basepoint. It returns a +/// proof for the above equality relationship. +pub fn prove_format_proof( + c1_value: u64, + c_blinding: &Scalar, + c1_basepoint: &RistrettoPoint, + c2_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> BalanceProof { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let t1_p = RistrettoPoint::multiscalar_mul(&[blinding_a, blinding_b], &[ + *c1_basepoint, + *blinding_basepoint, + ]); + let t2_p = c2_basepoint * blinding_b; + let c1_scalar_value = Scalar::from(c1_value); + let c1_point = + RistrettoPoint::multiscalar_mul(&[c1_scalar_value, *c_blinding], &[ + *c1_basepoint, + *blinding_basepoint, + ]); + let c2_point = c_blinding * c2_basepoint; + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&t2_p)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(c1_basepoint)); + hash_vec.append(&mut point_to_bytes(c2_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + + let check = hash_to_scalar(&hash_vec); + let m1 = blinding_a - (check * c1_scalar_value); + let m2 = blinding_b - (check * c_blinding); + let mut proof = BalanceProof::new(); + proof.set_t1(point_to_bytes(&t1_p)); + proof.set_t2(point_to_bytes(&t2_p)); + proof.set_m1(scalar_to_bytes(&m1)); + proof.set_m2(scalar_to_bytes(&m2)); + proof +} + +/// Verifies two commitments satisfying an equality relationship, i.e. +/// the values embedded in c1_point and c2_point satisfying c1_blinding = +/// c2_blinding, where c1_point = c1_value * c1_basepoint + c1_blinding * +/// blinding_basepoint, c2_point = c2_blinding * c2_basepoint. +pub fn verify_format_proof( + c1_point: &RistrettoPoint, + c2_point: &RistrettoPoint, + proof: &BalanceProof, + c1_basepoint: &RistrettoPoint, + c2_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> Result { + let t1_p = bytes_to_point(proof.get_t1())?; + let t2_p = bytes_to_point(proof.get_t2())?; + let m1 = bytes_to_scalar(proof.get_m1())?; + let m2 = bytes_to_scalar(proof.get_m2())?; + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&t2_p)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(c1_basepoint)); + hash_vec.append(&mut point_to_bytes(c2_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + let check = hash_to_scalar(&hash_vec); + let t1_v = RistrettoPoint::multiscalar_mul(&[check, m1, m2], &[ + *c1_point, + *c1_basepoint, + *blinding_basepoint, + ]); + let t2_v = RistrettoPoint::multiscalar_mul(&[check, m2], &[ + *c2_point, + *c2_basepoint, + ]); + + if t1_v == t1_p && t2_v == t2_p { + return Ok(true); + } + Ok(false) +} + +/// Verifies all commitment pairs satisfying equality relationships, +/// where each commitment pair contains two commitment points, +/// c1_point = c1_point_list[i], c2_point = c2_point_list[i], +/// and the values embedded in c1_point, c2_point satisfying +/// c1_blinding = c2_blinding. +pub fn verify_format_proof_in_batch( + c1_point_list: &Vec, + c2_point_list: &Vec, + proof_list: &Vec, + c1_basepoint: &RistrettoPoint, + c2_basepoint: &RistrettoPoint, + blinding_basepoint: &RistrettoPoint, +) -> Result { + if c1_point_list.len() != c1_point_list.len() + || c1_point_list.len() != proof_list.len() + { + return Err(WedprError::FormatError); + } + let mut t1_sum_expected: RistrettoPoint = Default::default(); + let mut t2_sum_expected: RistrettoPoint = Default::default(); + let mut c1_c_expected: RistrettoPoint = Default::default(); + let mut c2_c_expected: RistrettoPoint = Default::default(); + let mut m1_expected: Scalar = Scalar::zero(); + let mut m2_expected: Scalar = Scalar::zero(); + + for i in 0..c1_point_list.len() { + // 8 bit random scalar + let random_scalar = get_random_u8(); + let blinding_factor = Scalar::from(random_scalar); + let m1 = bytes_to_scalar(proof_list[i].get_m1())?; + let m2 = bytes_to_scalar(proof_list[i].get_m2())?; + let t1_p = bytes_to_point(proof_list[i].get_t1())?; + let t2_p = bytes_to_point(proof_list[i].get_t2())?; + let c1_point = c1_point_list[i]; + let c2_point = c2_point_list[i]; + + let mut hash_vec = Vec::new(); + hash_vec.append(&mut point_to_bytes(&t1_p)); + hash_vec.append(&mut point_to_bytes(&t2_p)); + hash_vec.append(&mut point_to_bytes(&c1_point)); + hash_vec.append(&mut point_to_bytes(&c2_point)); + hash_vec.append(&mut point_to_bytes(c1_basepoint)); + hash_vec.append(&mut point_to_bytes(c2_basepoint)); + hash_vec.append(&mut point_to_bytes(blinding_basepoint)); + let check = hash_to_scalar(&hash_vec); + + let c_factor = blinding_factor * check; + m1_expected += blinding_factor * m1; + m2_expected += blinding_factor * m2; + t1_sum_expected += small_scalar_point_mul(random_scalar, t1_p); + t2_sum_expected += small_scalar_point_mul(random_scalar, t2_p); + c1_c_expected += c_factor * c1_point; + c2_c_expected += c_factor * c2_point; + } + let t1_compute_sum_final = m1_expected * c1_basepoint + + m2_expected * blinding_basepoint + + c1_c_expected; + let t2_compute_sum_final = m2_expected * c2_basepoint + c2_c_expected; + + if t1_compute_sum_final == t1_sum_expected + && t2_compute_sum_final == t2_sum_expected + { + return Ok(true); + } + Ok(false) +} + /// Proves three commitments satisfying a sum relationship, i.e. /// the values embedded in them satisfying c1_value + c2_value = c3_value. /// c3_value is not in the argument list, and will be directly computed from @@ -620,10 +1085,227 @@ pub fn get_random_u8() -> u8 { #[cfg(test)] mod tests { use super::*; - use wedpr_l_crypto_zkp_utils::{BASEPOINT_G1, BASEPOINT_G2}; + use wedpr_l_crypto_zkp_utils::{ + get_random_u32, BASEPOINT_G1, BASEPOINT_G2, + }; const BATCH_SIZE: usize = 10; + #[test] + fn test_either_equality() { + let c1_value = 100u64; + let c1_blinding = get_random_scalar(); + let c2_blinding = get_random_scalar(); + let c3_blinding = get_random_scalar(); + let c2_value = c1_value; + let c_basepoint = *BASEPOINT_G1; + let blinding_basepoint = *BASEPOINT_G2; + let c1_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c1_value), c1_blinding], + &[c_basepoint, blinding_basepoint], + ); + let c2_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c2_value), c2_blinding], + &[c_basepoint, blinding_basepoint], + ); + let c3_point = + RistrettoPoint::multiscalar_mul(&[Scalar::zero(), c3_blinding], &[ + c_basepoint, + blinding_basepoint, + ]); + + let proof = prove_either_equality_relationship_proof( + c1_value, + c2_value, + &c1_blinding, + &c2_blinding, + &c3_blinding, + &c_basepoint, + &blinding_basepoint, + ); + assert_eq!( + true, + verify_either_equality_relationship_proof( + &c1_point, + &c2_point, + &c3_point, + &proof, + &c_basepoint, + &blinding_basepoint, + ) + .unwrap() + ); + + let zero_c1_point = c1_blinding * blinding_basepoint; + + let proof_zero = prove_either_equality_relationship_proof( + 0, + c2_value, + &c1_blinding, + &c2_blinding, + &c3_blinding, + &c_basepoint, + &blinding_basepoint, + ); + assert_eq!( + true, + verify_either_equality_relationship_proof( + &zero_c1_point, + &c2_point, + &c3_point, + &proof_zero, + &c_basepoint, + &blinding_basepoint, + ) + .unwrap() + ); + + let invalid_c1_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(101u64), c1_blinding], + &[c_basepoint, blinding_basepoint], + ); + + assert_eq!( + false, + verify_either_equality_relationship_proof( + &invalid_c1_point, + &c2_point, + &c3_point, + &proof, + &c_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + } + + #[test] + fn test_knowledge_proof_in_batch() { + let mut proofs: Vec = vec![]; + let mut c1_points: Vec = vec![]; + let c1_basepoint = *BASEPOINT_G1; + let blinding_basepoint = *BASEPOINT_G2 * get_random_scalar(); + for _ in 0..BATCH_SIZE { + let c1_value = get_random_u32() as u64; + let c1_blinding = get_random_scalar(); + + let proof = prove_knowledge_proof( + c1_value, + &c1_blinding, + &c1_basepoint, + &blinding_basepoint, + ); + let c1_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c1_value), c1_blinding], + &[c1_basepoint, blinding_basepoint], + ); + + assert_eq!( + true, + verify_knowledge_proof( + &c1_point, + &proof, + &c1_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + proofs.push(proof); + c1_points.push(c1_point); + } + assert_eq!( + true, + verify_knowledge_proof_in_batch( + &c1_points, + &proofs, + &c1_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + // Setting the wrong point should cause proof verification failure. + c1_points[BATCH_SIZE - 2] = c1_points[BATCH_SIZE - 1]; + assert_eq!( + false, + verify_knowledge_proof_in_batch( + &c1_points, + &proofs, + &c1_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + } + + #[test] + fn test_format_proof_in_batch() { + let mut proofs: Vec = vec![]; + let mut c1_points: Vec = vec![]; + let mut c2_points: Vec = vec![]; + let c1_basepoint = *BASEPOINT_G1; + let c2_basepoint = *BASEPOINT_G2; + let blinding_basepoint = *BASEPOINT_G2 * get_random_scalar(); + for _ in 0..BATCH_SIZE { + let c1_value = get_random_u32() as u64; + let c1_blinding = get_random_scalar(); + + let proof = prove_format_proof( + c1_value, + &c1_blinding, + &c1_basepoint, + &c2_basepoint, + &blinding_basepoint, + ); + let c1_point = RistrettoPoint::multiscalar_mul( + &[Scalar::from(c1_value), c1_blinding], + &[c1_basepoint, blinding_basepoint], + ); + let c2_point = c1_blinding * c2_basepoint; + + assert_eq!( + true, + verify_format_proof( + &c1_point, + &c2_point, + &proof, + &c1_basepoint, + &c2_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + proofs.push(proof); + c1_points.push(c1_point); + c2_points.push(c2_point); + } + assert_eq!( + true, + verify_format_proof_in_batch( + &c1_points, + &c2_points, + &proofs, + &c1_basepoint, + &c2_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + // Setting the wrong point should cause proof verification failure. + c1_points[BATCH_SIZE - 2] = c2_points[BATCH_SIZE - 1]; + assert_eq!( + false, + verify_format_proof_in_batch( + &c1_points, + &c2_points, + &proofs, + &c1_basepoint, + &c2_basepoint, + &blinding_basepoint + ) + .unwrap() + ); + } + #[test] fn test_sum_relationship_proof() { let c1_value = 30u64; diff --git a/crypto/zkp/range_proof/Cargo.toml b/crypto/zkp/range_proof/Cargo.toml index 7d2d8d9..5863502 100644 --- a/crypto/zkp/range_proof/Cargo.toml +++ b/crypto/zkp/range_proof/Cargo.toml @@ -9,7 +9,7 @@ description = "Library of WeDPR shared zkp Function implement range proof." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bulletproofs = "1.0.4" +bulletproofs = { git = "https://github.com/WeDPR-Team/bulletproofs.git" } curve25519-dalek = { version = "1", features = [ "serde" ] } merlin = "1" wedpr_l_crypto_zkp_utils = "1.1.0" diff --git a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs index 835ce16..9b3c70c 100644 --- a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs +++ b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs @@ -13,7 +13,8 @@ extern crate wedpr_ffi_macros; pub mod vrf; -const RESULT_JAVA_SDK_CLASS_NAME: &str = "com/webank/fisco/bcos/wedpr/sdk/SdkResult"; +const RESULT_JAVA_SDK_CLASS_NAME: &str = + "com/webank/fisco/bcos/wedpr/sdk/SdkResult"; use jni::{objects::JObject, JNIEnv}; use wedpr_ffi_common::utils::java_new_jobject; diff --git a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs index 42fd753..a5ea4da 100644 --- a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs +++ b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs @@ -10,7 +10,9 @@ use jni::{ sys::jobject, JNIEnv, }; -use wedpr_ffi_common::utils::{java_set_error_field_and_extract_jobject, java_jstring_to_string}; +use wedpr_ffi_common::utils::{ + java_jstring_to_string, java_set_error_field_and_extract_jobject, +}; use wedpr_third_party_fisco_bcos_java_sdk; // Curve25519 implementation. @@ -120,7 +122,8 @@ pub extern "system" fn Java_com_webank_fisco_bcos_wedpr_sdk_NativeInterface_curv #[no_mangle] /// Java interface for -/// 'com.webank.fisco.bcos.wedpr.sdk.NativeInterface->curve25519VrfDerivePublicKey'. +/// 'com.webank.fisco.bcos.wedpr.sdk. +/// NativeInterface->curve25519VrfDerivePublicKey'. pub extern "system" fn Java_com_webank_fisco_bcos_wedpr_sdk_NativeInterface_curve25519VrfDerivePublicKey( _env: JNIEnv, _class: JClass, @@ -194,7 +197,8 @@ pub extern "system" fn Java_com_webank_fisco_bcos_wedpr_sdk_NativeInterface_curv #[no_mangle] /// Java interface for -/// 'com.webank.fisco.bcos.wedpr.sdk.NativeInterface->curve25519VrfIsValidPublicKey'. +/// 'com.webank.fisco.bcos.wedpr.sdk. +/// NativeInterface->curve25519VrfIsValidPublicKey'. pub extern "system" fn Java_com_webank_fisco_bcos_wedpr_sdk_NativeInterface_curve25519VrfIsValidPublicKey( _env: JNIEnv, _class: JClass, diff --git a/protos/crypto/zkp.proto b/protos/crypto/zkp.proto index df394b3..c4935ad 100644 --- a/protos/crypto/zkp.proto +++ b/protos/crypto/zkp.proto @@ -18,6 +18,9 @@ message BalanceProof { bytes m3 = 6; bytes m4 = 7; bytes m5 = 8; + bytes m6 = 9; + bytes check1 = 10; + bytes check2 = 11; } // ZKP data to verify the equality relationship among value commitments. diff --git a/protos/src/generated/common.rs b/protos/src/generated/common.rs index d2b6da7..d7be59b 100644 --- a/protos/src/generated/common.rs +++ b/protos/src/generated/common.rs @@ -1,5 +1,3 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - // This file is generated by rust-protobuf 2.22.1. Do not edit // @generated diff --git a/protos/src/generated/zkp.rs b/protos/src/generated/zkp.rs index 4f11896..5c5e370 100644 --- a/protos/src/generated/zkp.rs +++ b/protos/src/generated/zkp.rs @@ -1,5 +1,3 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - // This file is generated by rust-protobuf 2.22.1. Do not edit // @generated @@ -36,6 +34,9 @@ pub struct BalanceProof { pub m3: ::std::vec::Vec, pub m4: ::std::vec::Vec, pub m5: ::std::vec::Vec, + pub m6: ::std::vec::Vec, + pub check1: ::std::vec::Vec, + pub check2: ::std::vec::Vec, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, @@ -259,6 +260,84 @@ impl BalanceProof { pub fn take_m5(&mut self) -> ::std::vec::Vec { ::std::mem::replace(&mut self.m5, ::std::vec::Vec::new()) } + + // bytes m6 = 9; + + + pub fn get_m6(&self) -> &[u8] { + &self.m6 + } + pub fn clear_m6(&mut self) { + self.m6.clear(); + } + + // Param is passed by value, moved + pub fn set_m6(&mut self, v: ::std::vec::Vec) { + self.m6 = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_m6(&mut self) -> &mut ::std::vec::Vec { + &mut self.m6 + } + + // Take field + pub fn take_m6(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.m6, ::std::vec::Vec::new()) + } + + // bytes check1 = 10; + + + pub fn get_check1(&self) -> &[u8] { + &self.check1 + } + pub fn clear_check1(&mut self) { + self.check1.clear(); + } + + // Param is passed by value, moved + pub fn set_check1(&mut self, v: ::std::vec::Vec) { + self.check1 = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_check1(&mut self) -> &mut ::std::vec::Vec { + &mut self.check1 + } + + // Take field + pub fn take_check1(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.check1, ::std::vec::Vec::new()) + } + + // bytes check2 = 11; + + + pub fn get_check2(&self) -> &[u8] { + &self.check2 + } + pub fn clear_check2(&mut self) { + self.check2.clear(); + } + + // Param is passed by value, moved + pub fn set_check2(&mut self, v: ::std::vec::Vec) { + self.check2 = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_check2(&mut self) -> &mut ::std::vec::Vec { + &mut self.check2 + } + + // Take field + pub fn take_check2(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.check2, ::std::vec::Vec::new()) + } } impl ::protobuf::Message for BalanceProof { @@ -294,6 +373,15 @@ impl ::protobuf::Message for BalanceProof { 8 => { ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.m5)?; }, + 9 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.m6)?; + }, + 10 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.check1)?; + }, + 11 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.check2)?; + }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; }, @@ -330,6 +418,15 @@ impl ::protobuf::Message for BalanceProof { if !self.m5.is_empty() { my_size += ::protobuf::rt::bytes_size(8, &self.m5); } + if !self.m6.is_empty() { + my_size += ::protobuf::rt::bytes_size(9, &self.m6); + } + if !self.check1.is_empty() { + my_size += ::protobuf::rt::bytes_size(10, &self.check1); + } + if !self.check2.is_empty() { + my_size += ::protobuf::rt::bytes_size(11, &self.check2); + } my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); self.cached_size.set(my_size); my_size @@ -360,6 +457,15 @@ impl ::protobuf::Message for BalanceProof { if !self.m5.is_empty() { os.write_bytes(8, &self.m5)?; } + if !self.m6.is_empty() { + os.write_bytes(9, &self.m6)?; + } + if !self.check1.is_empty() { + os.write_bytes(10, &self.check1)?; + } + if !self.check2.is_empty() { + os.write_bytes(11, &self.check2)?; + } os.write_unknown_fields(self.get_unknown_fields())?; ::std::result::Result::Ok(()) } @@ -438,6 +544,21 @@ impl ::protobuf::Message for BalanceProof { |m: &BalanceProof| { &m.m5 }, |m: &mut BalanceProof| { &mut m.m5 }, )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "m6", + |m: &BalanceProof| { &m.m6 }, + |m: &mut BalanceProof| { &mut m.m6 }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "check1", + |m: &BalanceProof| { &m.check1 }, + |m: &mut BalanceProof| { &mut m.check1 }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "check2", + |m: &BalanceProof| { &m.check2 }, + |m: &mut BalanceProof| { &mut m.check2 }, + )); ::protobuf::reflect::MessageDescriptor::new_pb_name::( "BalanceProof", fields, @@ -462,6 +583,9 @@ impl ::protobuf::Clear for BalanceProof { self.m3.clear(); self.m4.clear(); self.m5.clear(); + self.m6.clear(); + self.check1.clear(); + self.check2.clear(); self.unknown_fields.clear(); } } @@ -722,14 +846,16 @@ impl ::protobuf::reflect::ProtobufValue for EqualityProof { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\x10crypto/zkp.proto\x12\x1dcom.webank.wedpr.crypto.proto\"\x8e\x01\n\ + \n\x10crypto/zkp.proto\x12\x1dcom.webank.wedpr.crypto.proto\"\xce\x01\n\ \x0cBalanceProof\x12\x0e\n\x02t1\x18\x01\x20\x01(\x0cR\x02t1\x12\x0e\n\ \x02t2\x18\x02\x20\x01(\x0cR\x02t2\x12\x0e\n\x02t3\x18\x03\x20\x01(\x0cR\ \x02t3\x12\x0e\n\x02m1\x18\x04\x20\x01(\x0cR\x02m1\x12\x0e\n\x02m2\x18\ \x05\x20\x01(\x0cR\x02m2\x12\x0e\n\x02m3\x18\x06\x20\x01(\x0cR\x02m3\x12\ \x0e\n\x02m4\x18\x07\x20\x01(\x0cR\x02m4\x12\x0e\n\x02m5\x18\x08\x20\x01\ - (\x0cR\x02m5\"?\n\rEqualityProof\x12\x0e\n\x02m1\x18\x01\x20\x01(\x0cR\ - \x02m1\x12\x0e\n\x02t1\x18\x02\x20\x01(\x0cR\x02t1\x12\x0e\n\x02t2\x18\ + (\x0cR\x02m5\x12\x0e\n\x02m6\x18\t\x20\x01(\x0cR\x02m6\x12\x16\n\x06chec\ + k1\x18\n\x20\x01(\x0cR\x06check1\x12\x16\n\x06check2\x18\x0b\x20\x01(\ + \x0cR\x06check2\"?\n\rEqualityProof\x12\x0e\n\x02m1\x18\x01\x20\x01(\x0c\ + R\x02m1\x12\x0e\n\x02t1\x18\x02\x20\x01(\x0cR\x02t1\x12\x0e\n\x02t2\x18\ \x03\x20\x01(\x0cR\x02t2B!\n\x1dcom.webank.wedpr.crypto.protoP\x01b\x06p\ roto3\ "; diff --git a/release_note.txt b/release_note.txt index 992977a..0408c30 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1 +1 @@ -v1.1.0 \ No newline at end of file +v1.2.0 \ No newline at end of file diff --git a/third_party/fisco_bcos_java_sdk/src/lib.rs b/third_party/fisco_bcos_java_sdk/src/lib.rs index 53fdcc2..3571c08 100644 --- a/third_party/fisco_bcos_java_sdk/src/lib.rs +++ b/third_party/fisco_bcos_java_sdk/src/lib.rs @@ -1,15 +1,18 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. //! FISCO BCOS Java Sdk specific functions. +#![allow(non_camel_case_types)] use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar}; use rand::thread_rng; use sha3::Sha3_512; +use wedpr_l_common_coder_hex::WedprHex; use wedpr_l_crypto_hash_keccak256::{self, WedprKeccak256}; use wedpr_l_crypto_zkp_utils::BASEPOINT_G1; -use wedpr_l_utils::{error::WedprError, traits::Hash}; -use wedpr_l_common_coder_hex::WedprHex; -use wedpr_l_utils::traits::Coder; +use wedpr_l_utils::{ + error::WedprError, + traits::{Coder, Hash}, +}; mod tools; @@ -160,7 +163,7 @@ mod tests { let encode = proof.encode(); println!("encode = {}", encode); - let decode = vrf_proof::decode(&encode).unwrap(); + // let decode = vrf_proof::decode(&encode).unwrap(); let result = curve25519_vrf_verify(&y, alpha, &proof); println!("result = {}", result); } From 6d5bf2234bd76dd24838af0715b85cf3b55ae9db Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Thu, 27 May 2021 21:19:39 +0800 Subject: [PATCH 02/17] skip ffi coverage --- ffi/ffi_c/ffi_c_common/src/lib.rs | 3 +++ ffi/ffi_c/ffi_c_crypto/src/block_cipher.rs | 1 + ffi/ffi_c/ffi_c_crypto/src/config.rs | 1 + ffi/ffi_c/ffi_c_crypto/src/ecies.rs | 1 + ffi/ffi_c/ffi_c_crypto/src/hash.rs | 1 + ffi/ffi_c/ffi_c_crypto/src/signature.rs | 1 + ffi/ffi_c/ffi_c_crypto/src/vrf.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/block_cipher.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/config.rs | 2 ++ ffi/ffi_c/ffi_c_crypto_binary/src/ecies.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/hash.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/signature.rs | 1 + ffi/ffi_c/ffi_c_crypto_binary/src/vrf.rs | 1 + ffi/ffi_c/ffi_c_fisco_bcos/src/bn128.rs | 2 ++ ffi/ffi_c/ffi_c_fisco_bcos/src/lib.rs | 1 + ffi/ffi_common/src/utils.rs | 2 ++ ffi/ffi_java/ffi_java_crypto/src/block_cipher.rs | 1 + ffi/ffi_java/ffi_java_crypto/src/config.rs | 2 ++ ffi/ffi_java/ffi_java_crypto/src/ecies.rs | 2 ++ ffi/ffi_java/ffi_java_crypto/src/hash.rs | 2 ++ ffi/ffi_java/ffi_java_crypto/src/signature.rs | 1 + ffi/ffi_java/ffi_java_crypto/src/vrf.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/block_cipher.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/config.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/ecies.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/hash.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/signature.rs | 2 ++ ffi/ffi_java/ffi_java_crypto_binary/src/vrf.rs | 2 ++ ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs | 1 + ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs | 2 ++ ffi/ffi_macros/src/binary.rs | 2 ++ ffi/ffi_macros/src/lib.rs | 1 + 33 files changed, 50 insertions(+) diff --git a/ffi/ffi_c/ffi_c_common/src/lib.rs b/ffi/ffi_c/ffi_c_common/src/lib.rs index c5aa542..d7e1966 100644 --- a/ffi/ffi_c/ffi_c_common/src/lib.rs +++ b/ffi/ffi_c/ffi_c_common/src/lib.rs @@ -1,10 +1,13 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Library of shared utilities for FFI targeting C/C++ //! compatible architectures (including iOS). /// Patch code to fix the missing 'backtrace_*' related functions errors during /// cross compilation. Do not modify them if you are not sure. + #[allow(non_camel_case_types)] pub mod backtrace_patch { extern crate libc; diff --git a/ffi/ffi_c/ffi_c_crypto/src/block_cipher.rs b/ffi/ffi_c/ffi_c_crypto/src/block_cipher.rs index cd90747..33f33b6 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/block_cipher.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/block_cipher.rs @@ -2,6 +2,7 @@ //! Block Cipher function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_crypto_block_cipher_aes", feature = "wedpr_f_crypto_block_cipher_sm4" diff --git a/ffi/ffi_c/ffi_c_crypto/src/config.rs b/ffi/ffi_c/ffi_c_crypto/src/config.rs index d3defa0..b3ba960 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/config.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/config.rs @@ -4,6 +4,7 @@ // ECIES section. +#![cfg(not(tarpaulin_include))] #[cfg(feature = "wedpr_f_ecies_secp256k1")] use wedpr_l_crypto_ecies_secp256k1::WedprSecp256k1Ecies; diff --git a/ffi/ffi_c/ffi_c_crypto/src/ecies.rs b/ffi/ffi_c/ffi_c_crypto/src/ecies.rs index 076d867..5507dd9 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/ecies.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/ecies.rs @@ -2,6 +2,7 @@ //! ECIES function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(feature = "wedpr_f_ecies_secp256k1")] use wedpr_l_utils::traits::Ecies; diff --git a/ffi/ffi_c/ffi_c_crypto/src/hash.rs b/ffi/ffi_c/ffi_c_crypto/src/hash.rs index 4f59c02..ca24435 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/hash.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/hash.rs @@ -2,6 +2,7 @@ //! Hash function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_hash_keccak256", feature = "wedpr_f_hash_sm3", diff --git a/ffi/ffi_c/ffi_c_crypto/src/signature.rs b/ffi/ffi_c/ffi_c_crypto/src/signature.rs index 3d21813..a9a6ca7 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/signature.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/signature.rs @@ -2,6 +2,7 @@ //! Signature function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_signature_secp256k1", feature = "wedpr_f_signature_sm2", diff --git a/ffi/ffi_c/ffi_c_crypto/src/vrf.rs b/ffi/ffi_c/ffi_c_crypto/src/vrf.rs index 43d1453..b63b86d 100644 --- a/ffi/ffi_c/ffi_c_crypto/src/vrf.rs +++ b/ffi/ffi_c/ffi_c_crypto/src/vrf.rs @@ -2,6 +2,7 @@ //! VRF function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(feature = "wedpr_f_vrf_curve25519")] use libc::c_char; diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/block_cipher.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/block_cipher.rs index b0925d3..6e283ed 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/block_cipher.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/block_cipher.rs @@ -2,6 +2,7 @@ //! Block Cipher function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_crypto_block_cipher_aes", feature = "wedpr_f_crypto_block_cipher_sm4" diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/config.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/config.rs index d3defa0..055051b 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/config.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/config.rs @@ -2,6 +2,8 @@ //! Shared config for wedpr_ffi_c_crypto. +#![cfg(not(tarpaulin_include))] + // ECIES section. #[cfg(feature = "wedpr_f_ecies_secp256k1")] diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/ecies.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/ecies.rs index e3137d8..c63b41e 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/ecies.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/ecies.rs @@ -2,6 +2,7 @@ //! ECIES function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(feature = "wedpr_f_ecies_secp256k1")] use wedpr_l_utils::traits::Ecies; diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/hash.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/hash.rs index 723117a..8a4345e 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/hash.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/hash.rs @@ -2,6 +2,7 @@ //! Hash function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_hash_keccak256", feature = "wedpr_f_hash_sm3", diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs index 5bb11dd..c20ff4f 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs @@ -3,6 +3,7 @@ //! Library of FFI of wedpr_crypto wrapper functions, targeting C/C++ //! compatible architectures (including iOS), with fast binary interfaces. +#![cfg(not(tarpaulin_include))] #[macro_use] extern crate wedpr_ffi_macros; diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/signature.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/signature.rs index af7d449..e4b9d81 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/signature.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/signature.rs @@ -2,6 +2,7 @@ //! Signature function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_signature_secp256k1", feature = "wedpr_f_signature_sm2", diff --git a/ffi/ffi_c/ffi_c_crypto_binary/src/vrf.rs b/ffi/ffi_c/ffi_c_crypto_binary/src/vrf.rs index f9b7bd9..106cd05 100644 --- a/ffi/ffi_c/ffi_c_crypto_binary/src/vrf.rs +++ b/ffi/ffi_c/ffi_c_crypto_binary/src/vrf.rs @@ -2,6 +2,7 @@ //! VRF function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(feature = "wedpr_f_vrf_curve25519")] use wedpr_ffi_common::utils::{ diff --git a/ffi/ffi_c/ffi_c_fisco_bcos/src/bn128.rs b/ffi/ffi_c/ffi_c_fisco_bcos/src/bn128.rs index a3c025f..95a9077 100644 --- a/ffi/ffi_c/ffi_c_fisco_bcos/src/bn128.rs +++ b/ffi/ffi_c/ffi_c_fisco_bcos/src/bn128.rs @@ -2,6 +2,8 @@ //! BN128 curve function wrappers. +#![cfg(not(tarpaulin_include))] + use wedpr_ffi_common::utils::{ c_read_raw_pointer, c_write_raw_pointer, CInputBuffer, COutputBuffer, FAILURE, SUCCESS, diff --git a/ffi/ffi_c/ffi_c_fisco_bcos/src/lib.rs b/ffi/ffi_c/ffi_c_fisco_bcos/src/lib.rs index 695c55d..07ef1cb 100644 --- a/ffi/ffi_c/ffi_c_fisco_bcos/src/lib.rs +++ b/ffi/ffi_c/ffi_c_fisco_bcos/src/lib.rs @@ -3,4 +3,5 @@ //! Library of FFI of wedpr_third_party_fisco_bcos wrapper functions, targeting //! C/C++ compatible architectures (including iOS). +#![cfg(not(tarpaulin_include))] pub mod bn128; diff --git a/ffi/ffi_common/src/utils.rs b/ffi/ffi_common/src/utils.rs index bf79de3..c72dcba 100644 --- a/ffi/ffi_common/src/utils.rs +++ b/ffi/ffi_common/src/utils.rs @@ -2,6 +2,8 @@ //! Common utility functions for FFI. +#![cfg(not(tarpaulin_include))] + #[cfg(all(feature = "wedpr_f_base64", feature = "wedpr_f_hex"))] compile_error!( "Feature wedpr_f_base64 and wedpr_f_hex can not be enabled at same time!" diff --git a/ffi/ffi_java/ffi_java_crypto/src/block_cipher.rs b/ffi/ffi_java/ffi_java_crypto/src/block_cipher.rs index f7afb4a..1ec58a7 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/block_cipher.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/block_cipher.rs @@ -2,6 +2,7 @@ //! Block cipher function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_crypto_block_cipher_aes", feature = "wedpr_f_crypto_block_cipher_sm4" diff --git a/ffi/ffi_java/ffi_java_crypto/src/config.rs b/ffi/ffi_java/ffi_java_crypto/src/config.rs index 13e1fd1..491405d 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/config.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/config.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Shared config for wedpr_ffi_java_crypto. // ECIES section. diff --git a/ffi/ffi_java/ffi_java_crypto/src/ecies.rs b/ffi/ffi_java/ffi_java_crypto/src/ecies.rs index 27198d2..90f094e 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/ecies.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/ecies.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! ECIES function wrappers. #![cfg(feature = "wedpr_f_ecies_secp256k1")] diff --git a/ffi/ffi_java/ffi_java_crypto/src/hash.rs b/ffi/ffi_java/ffi_java_crypto/src/hash.rs index 14c376b..9588381 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/hash.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/hash.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Hash function wrappers. #![cfg(any( diff --git a/ffi/ffi_java/ffi_java_crypto/src/signature.rs b/ffi/ffi_java/ffi_java_crypto/src/signature.rs index 4fcd0e8..64d0603 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/signature.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/signature.rs @@ -2,6 +2,7 @@ //! Signature function wrappers. +#![cfg(not(tarpaulin_include))] #![cfg(any( feature = "wedpr_f_signature_secp256k1", feature = "wedpr_f_signature_sm2", diff --git a/ffi/ffi_java/ffi_java_crypto/src/vrf.rs b/ffi/ffi_java/ffi_java_crypto/src/vrf.rs index 8f70f4d..7de6041 100644 --- a/ffi/ffi_java/ffi_java_crypto/src/vrf.rs +++ b/ffi/ffi_java/ffi_java_crypto/src/vrf.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! VRF function wrappers. #![cfg(feature = "wedpr_f_vrf_curve25519")] diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/block_cipher.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/block_cipher.rs index d05c94b..bdb936e 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/block_cipher.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/block_cipher.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Block cipher function wrappers. #![cfg(any( diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/config.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/config.rs index 13e1fd1..491405d 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/config.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/config.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Shared config for wedpr_ffi_java_crypto. // ECIES section. diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/ecies.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/ecies.rs index f52c7eb..8fd42ed 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/ecies.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/ecies.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! ECIES function wrappers. #![cfg(feature = "wedpr_f_ecies_secp256k1")] diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/hash.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/hash.rs index ad7bb37..17ad770 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/hash.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/hash.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Hash function wrappers. #![cfg(any( diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/signature.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/signature.rs index 6cca72e..c868bd2 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/signature.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/signature.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Signature function wrappers. #![cfg(any( diff --git a/ffi/ffi_java/ffi_java_crypto_binary/src/vrf.rs b/ffi/ffi_java/ffi_java_crypto_binary/src/vrf.rs index 146efe3..d126b0c 100644 --- a/ffi/ffi_java/ffi_java_crypto_binary/src/vrf.rs +++ b/ffi/ffi_java/ffi_java_crypto_binary/src/vrf.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! VRF function wrappers. #![cfg(feature = "wedpr_f_vrf_curve25519")] diff --git a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs index 9b3c70c..0026233 100644 --- a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs +++ b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/lib.rs @@ -4,6 +4,7 @@ //! targeting Java-compatible architectures (including Android), with fast //! binary interfaces. +#![cfg(not(tarpaulin_include))] extern crate jni; #[allow(unused_imports)] #[macro_use] diff --git a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs index a5ea4da..6fda840 100644 --- a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs +++ b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs @@ -1,5 +1,7 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +#![cfg(not(tarpaulin_include))] + //! Library of FFI of wedpr_third_party_fisco_bcos_java_sdk wrapper functions, //! targeting Java-compatible architectures (including Android), with fast //! binary interfaces. diff --git a/ffi/ffi_macros/src/binary.rs b/ffi/ffi_macros/src/binary.rs index 0623388..a79c6d0 100644 --- a/ffi/ffi_macros/src/binary.rs +++ b/ffi/ffi_macros/src/binary.rs @@ -1,5 +1,7 @@ // Field setting section. +#![cfg(not(tarpaulin_include))] + /// Sets a field of bytes type, and returns an error object if failed. #[macro_export] macro_rules! java_safe_set_byte_array_field { diff --git a/ffi/ffi_macros/src/lib.rs b/ffi/ffi_macros/src/lib.rs index d7ecdf9..fe6e9e2 100644 --- a/ffi/ffi_macros/src/lib.rs +++ b/ffi/ffi_macros/src/lib.rs @@ -6,6 +6,7 @@ // Type conversion section. +#![cfg(not(tarpaulin_include))] pub mod binary; // TODO: Extract Proto-related macros to proto.rs. From 284fdcc666c16ce19fec12c0c766c39f81dc44f2 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 1 Jun 2021 14:19:35 +0800 Subject: [PATCH 03/17] update proto to bytes --- .rustfmt.toml | 2 +- protos/Cargo.toml | 4 +++- protos/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index de39635..cfb9516 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -5,7 +5,7 @@ format_strings = true indent_style = "Block" match_block_trailing_comma = true max_width = 80 -merge_imports = true +imports_granularity = "Crate" normalize_comments = true normalize_doc_attributes = true overflow_delimited_expr = true diff --git a/protos/Cargo.toml b/protos/Cargo.toml index b7b456b..8fb426e 100644 --- a/protos/Cargo.toml +++ b/protos/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wedpr_l_protos" -version = "1.1.0" +version = "1.1.1" authors = [ "WeDPR " ] edition = "2018" license = "Apache-2.0" @@ -11,3 +11,5 @@ description = "Library of WeDPR protobuf definitions and their generated code." [dependencies] protobuf = "2.22.1" protoc-rust = "2.22.1" +wedpr_l_utils = "1.1.0" + diff --git a/protos/src/lib.rs b/protos/src/lib.rs index 9711d66..6dd3089 100644 --- a/protos/src/lib.rs +++ b/protos/src/lib.rs @@ -2,5 +2,39 @@ //! Library of protobuf definitions and their generated code. +use protobuf::Message; + #[cfg(not(tarpaulin_include))] pub mod generated; + +use wedpr_l_utils::error::WedprError; + +pub fn proto_to_bytes(proto: &T) -> Result, WedprError> { + return match proto.write_to_bytes() { + Ok(v) => Ok(v), + Err(_) => Err(WedprError::DecodeError), + }; +} + +pub fn bytes_to_proto(proto_bytes: &[u8]) -> Result { + return match T::parse_from_bytes(proto_bytes) { + Ok(v) => Ok(v), + Err(_) => Err(WedprError::DecodeError), + }; +} + +#[cfg(test)] +mod tests { + use super::*; + use generated::zkp::BalanceProof; + + #[test] + fn test_parser() { + let mut proof = BalanceProof::new(); + proof.set_check1("test1".as_bytes().to_vec()); + proof.set_check2("test2".as_bytes().to_vec()); + let bytes = proto_to_bytes(&proof).unwrap(); + let proof_parser = bytes_to_proto::(&bytes).unwrap(); + assert_eq!(proof_parser, proof); + } +} From 0e906c056fa3e8070c59cfa12d48019c7966a32d Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 7 Jun 2021 17:06:36 +0800 Subject: [PATCH 04/17] add base ot --- Cargo.toml | 1 + crypto/oblivious_transfer/base_ot/Cargo.toml | 23 + .../base_ot/benches/base_ot.rs | 69 + crypto/oblivious_transfer/base_ot/src/lib.rs | 204 +++ protos/crypto/ot.proto | 38 + protos/src/generated/mod.rs | 1 + protos/src/generated/ot.rs | 1273 +++++++++++++++++ protos/src/main.rs | 2 +- 8 files changed, 1610 insertions(+), 1 deletion(-) create mode 100644 crypto/oblivious_transfer/base_ot/Cargo.toml create mode 100644 crypto/oblivious_transfer/base_ot/benches/base_ot.rs create mode 100644 crypto/oblivious_transfer/base_ot/src/lib.rs create mode 100644 protos/crypto/ot.proto create mode 100644 protos/src/generated/ot.rs diff --git a/Cargo.toml b/Cargo.toml index a558606..6a3a24e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "crypto/hash/ripemd160", "crypto/hash/sha3", "crypto/hash/sm3", + "crypto/oblivious_transfer/base_ot", "crypto/signature/ed25519", "crypto/signature/secp256k1", "crypto/signature/sm2", diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml new file mode 100644 index 0000000..8186365 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "base_ot" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +curve25519-dalek = { version = "1", features = [ "serde" ] } +wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } +wedpr_l_crypto_hash_sha3 = { version = "1.0.0" } +lazy_static = "1.4.0" +wedpr_l_protos = { path = "../../../protos"} +wedpr_l_utils = "1.1.0" +sha3 = "0.8.2" + +[dev-dependencies] +criterion = "0.3" +rand = "0.6.0" + +[[bench]] +name = "base_ot" +harness = false \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs new file mode 100644 index 0000000..c5f7556 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -0,0 +1,69 @@ +#[macro_use] +extern crate criterion; +use base_ot::{receiver_decrypt, receiver_init, sender_init}; +use criterion::Criterion; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use wedpr_l_protos::generated::ot::{SenderData, SenderDataPair}; + +fn create_base_ot_helper(c: &mut Criterion, message_count: u64, str_len: u64) { + let label = format!( + "create_base_ot_helper, message_count = {}, str_len = {}", + message_count, str_len + ); + let mut sender_data = SenderData::default(); + for _ in 0..message_count { + let random_id: String = + thread_rng().sample_iter(&Alphanumeric).take(18).collect(); + let random_message: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(str_len as usize) + .collect(); + sender_data.mut_pair().push(SenderDataPair { + id: random_id.as_bytes().to_vec(), + message: random_message.as_bytes().to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let use_data = sender_data.clone(); + let choose_id = + sender_data.get_pair()[sender_data.get_pair().len() / 2].get_id(); + let true_message = + sender_data.get_pair()[sender_data.get_pair().len() / 2].get_message(); + c.bench_function(&label, move |b| { + b.iter(|| { + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&use_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + assert_eq!(message.as_slice(), true_message); + }) + }); +} + +fn create_base_ot_10_10(c: &mut Criterion) { + create_base_ot_helper(c, 10, 10); +} + +fn create_base_ot_100_10(c: &mut Criterion) { + create_base_ot_helper(c, 100, 10); +} + +fn create_base_ot_1000_10(c: &mut Criterion) { + create_base_ot_helper(c, 1000, 10); +} + +fn create_base_ot_10000_10(c: &mut Criterion) { + create_base_ot_helper(c, 10000, 10); +} + +criterion_group! { + name = init_base_ot_test; + config = Criterion::default().sample_size(10); +targets = +create_base_ot_10_10, +create_base_ot_100_10, +create_base_ot_1000_10, +create_base_ot_10000_10, +} + +criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs new file mode 100644 index 0000000..2136142 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -0,0 +1,204 @@ +#[macro_use] +extern crate lazy_static; + +use curve25519_dalek::scalar::Scalar; +use sha3::Sha3_512; +use wedpr_l_crypto_hash_sha3::WedprSha3_256; +use wedpr_l_crypto_zkp_utils::{ + bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, + scalar_to_bytes, BASEPOINT_G1, +}; +use wedpr_l_protos::generated::ot::{ + ReceiverPublic, ReceiverSecret, SenderData, SenderDataPair, SenderPublic, + SenderPublicPair, +}; +use wedpr_l_utils::{error::WedprError, traits::Hash}; + +lazy_static! { + pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); +} + +pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { + let id_scalar = Scalar::hash_from_bytes::(id); + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let c_id = blinding_a * blinding_b; + let point_x = blinding_a * *BASEPOINT_G1; + let point_y = blinding_b * *BASEPOINT_G1; + let point_z = (c_id - id_scalar) * *BASEPOINT_G1; + ( + ReceiverSecret { + scalar_a: scalar_to_bytes(&blinding_a), + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ReceiverPublic { + point_x: point_to_bytes(&point_x), + point_y: point_to_bytes(&point_y), + point_z: point_to_bytes(&point_z), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ) +} + +pub fn sender_init( + data: &SenderData, + r_public: &ReceiverPublic, +) -> Result { + let mut sender_public = SenderPublic::default(); + let point_x = bytes_to_point(r_public.get_point_x())?; + let point_y = bytes_to_point(r_public.get_point_y())?; + let point_z = bytes_to_point(r_public.get_point_z())?; + for data_pair in data.get_pair() { + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let point_w = blinding_s * point_x + blinding_r * *BASEPOINT_G1; + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + let point_key = blinding_s * point_z + + blinding_s * id_scalar * *BASEPOINT_G1 + + blinding_r * point_y; + let mut bytes_key = point_to_bytes(&point_key); + while message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let encrypt_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + // println!("encrypt_message = {:?}", encrypt_message.clone()); + sender_public.mut_pair().push(SenderPublicPair { + figure_print: HASH_SHA3_256.hash(message), + point_w: point_to_bytes(&point_w), + encrypt_message, + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + Ok(sender_public) +} + +pub fn receiver_decrypt( + secret: &ReceiverSecret, + // id: &[u8], + encrypt_message: &SenderPublic, +) -> Result, WedprError> { + let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; + for pair in encrypt_message.get_pair() { + // if id != pair.get_id() { + // continue; + // } + let point_w = bytes_to_point(pair.get_point_w())?; + let encrypt_message = pair.get_encrypt_message(); + let point_key = blinding_b * point_w; + let mut bytes_key = point_to_bytes(&point_key); + while encrypt_message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let decrypt_message: Vec = encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { + return Ok(decrypt_message); + } + } + Err(WedprError::ArgumentError) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_base_ot() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ("10000".as_bytes(), "wedpr test1".as_bytes()), + ("10086".as_bytes(), "wedpr test2".as_bytes()), + ("10010".as_bytes(), "wedpr test3".as_bytes()), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = + // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); + receiver_decrypt(&r_secret, &s_public).unwrap(); + assert_eq!(message, "wedpr test2".as_bytes()); + } + + #[test] + fn test_base_ot_long() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ( + "10000".as_bytes(), + "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10086".as_bytes(), + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10010".as_bytes(), + "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); + let message_str = String::from_utf8(message.clone()).unwrap(); + // println!("message = {}", message_str); + assert_eq!( + message_str, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .to_string() + ); + assert_eq!( + message, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes() + ); + } +} diff --git a/protos/crypto/ot.proto b/protos/crypto/ot.proto new file mode 100644 index 0000000..766dcc5 --- /dev/null +++ b/protos/crypto/ot.proto @@ -0,0 +1,38 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +syntax = "proto3"; + +package com.webank.wedpr.crypto.proto; +option java_package = "com.webank.wedpr.crypto.proto"; +option java_multiple_files = true; + +// ot key pair containing a public key and a private key. +message ReceiverSecret { + bytes scalar_a = 1; + bytes scalar_b = 2; +} + +message ReceiverPublic { + bytes point_x = 1; + bytes point_y = 2; + bytes point_z = 3; +} + +message SenderPublicPair { + bytes figure_print = 1; + bytes point_w = 2; + bytes encrypt_message = 3; +} + +message SenderPublic { + repeated SenderPublicPair pair = 1; +} + +message SenderDataPair { + bytes id = 1; + bytes message = 2; +} + +message SenderData { + repeated SenderDataPair pair = 1; +} diff --git a/protos/src/generated/mod.rs b/protos/src/generated/mod.rs index 16d353e..4276416 100644 --- a/protos/src/generated/mod.rs +++ b/protos/src/generated/mod.rs @@ -2,3 +2,4 @@ pub mod common; pub mod zkp; +pub mod ot; diff --git a/protos/src/generated/ot.rs b/protos/src/generated/ot.rs new file mode 100644 index 0000000..dc40d4f --- /dev/null +++ b/protos/src/generated/ot.rs @@ -0,0 +1,1273 @@ +// This file is generated by rust-protobuf 2.22.1. Do not edit +// @generated + +// https://github.com/rust-lang/rust-clippy/issues/702 +#![allow(unknown_lints)] +#![allow(clippy::all)] + +#![allow(unused_attributes)] +#![cfg_attr(rustfmt, rustfmt::skip)] + +#![allow(box_pointers)] +#![allow(dead_code)] +#![allow(missing_docs)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(trivial_casts)] +#![allow(unused_imports)] +#![allow(unused_results)] +//! Generated file from `crypto/ot.proto` + +/// Generated files are compatible only with the same version +/// of protobuf runtime. +// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1; + +#[derive(PartialEq,Clone,Default)] +pub struct ReceiverSecret { + // message fields + pub scalar_a: ::std::vec::Vec, + pub scalar_b: ::std::vec::Vec, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a ReceiverSecret { + fn default() -> &'a ReceiverSecret { + ::default_instance() + } +} + +impl ReceiverSecret { + pub fn new() -> ReceiverSecret { + ::std::default::Default::default() + } + + // bytes scalar_a = 1; + + + pub fn get_scalar_a(&self) -> &[u8] { + &self.scalar_a + } + pub fn clear_scalar_a(&mut self) { + self.scalar_a.clear(); + } + + // Param is passed by value, moved + pub fn set_scalar_a(&mut self, v: ::std::vec::Vec) { + self.scalar_a = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_scalar_a(&mut self) -> &mut ::std::vec::Vec { + &mut self.scalar_a + } + + // Take field + pub fn take_scalar_a(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.scalar_a, ::std::vec::Vec::new()) + } + + // bytes scalar_b = 2; + + + pub fn get_scalar_b(&self) -> &[u8] { + &self.scalar_b + } + pub fn clear_scalar_b(&mut self) { + self.scalar_b.clear(); + } + + // Param is passed by value, moved + pub fn set_scalar_b(&mut self, v: ::std::vec::Vec) { + self.scalar_b = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_scalar_b(&mut self) -> &mut ::std::vec::Vec { + &mut self.scalar_b + } + + // Take field + pub fn take_scalar_b(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.scalar_b, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for ReceiverSecret { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.scalar_a)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.scalar_b)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.scalar_a.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.scalar_a); + } + if !self.scalar_b.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.scalar_b); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.scalar_a.is_empty() { + os.write_bytes(1, &self.scalar_a)?; + } + if !self.scalar_b.is_empty() { + os.write_bytes(2, &self.scalar_b)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> ReceiverSecret { + ReceiverSecret::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "scalar_a", + |m: &ReceiverSecret| { &m.scalar_a }, + |m: &mut ReceiverSecret| { &mut m.scalar_a }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "scalar_b", + |m: &ReceiverSecret| { &m.scalar_b }, + |m: &mut ReceiverSecret| { &mut m.scalar_b }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "ReceiverSecret", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static ReceiverSecret { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(ReceiverSecret::new) + } +} + +impl ::protobuf::Clear for ReceiverSecret { + fn clear(&mut self) { + self.scalar_a.clear(); + self.scalar_b.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for ReceiverSecret { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for ReceiverSecret { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct ReceiverPublic { + // message fields + pub point_x: ::std::vec::Vec, + pub point_y: ::std::vec::Vec, + pub point_z: ::std::vec::Vec, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a ReceiverPublic { + fn default() -> &'a ReceiverPublic { + ::default_instance() + } +} + +impl ReceiverPublic { + pub fn new() -> ReceiverPublic { + ::std::default::Default::default() + } + + // bytes point_x = 1; + + + pub fn get_point_x(&self) -> &[u8] { + &self.point_x + } + pub fn clear_point_x(&mut self) { + self.point_x.clear(); + } + + // Param is passed by value, moved + pub fn set_point_x(&mut self, v: ::std::vec::Vec) { + self.point_x = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_x(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_x + } + + // Take field + pub fn take_point_x(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_x, ::std::vec::Vec::new()) + } + + // bytes point_y = 2; + + + pub fn get_point_y(&self) -> &[u8] { + &self.point_y + } + pub fn clear_point_y(&mut self) { + self.point_y.clear(); + } + + // Param is passed by value, moved + pub fn set_point_y(&mut self, v: ::std::vec::Vec) { + self.point_y = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_y(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_y + } + + // Take field + pub fn take_point_y(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_y, ::std::vec::Vec::new()) + } + + // bytes point_z = 3; + + + pub fn get_point_z(&self) -> &[u8] { + &self.point_z + } + pub fn clear_point_z(&mut self) { + self.point_z.clear(); + } + + // Param is passed by value, moved + pub fn set_point_z(&mut self, v: ::std::vec::Vec) { + self.point_z = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_z(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_z + } + + // Take field + pub fn take_point_z(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_z, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for ReceiverPublic { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_x)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_y)?; + }, + 3 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_z)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.point_x.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.point_x); + } + if !self.point_y.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.point_y); + } + if !self.point_z.is_empty() { + my_size += ::protobuf::rt::bytes_size(3, &self.point_z); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.point_x.is_empty() { + os.write_bytes(1, &self.point_x)?; + } + if !self.point_y.is_empty() { + os.write_bytes(2, &self.point_y)?; + } + if !self.point_z.is_empty() { + os.write_bytes(3, &self.point_z)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> ReceiverPublic { + ReceiverPublic::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_x", + |m: &ReceiverPublic| { &m.point_x }, + |m: &mut ReceiverPublic| { &mut m.point_x }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_y", + |m: &ReceiverPublic| { &m.point_y }, + |m: &mut ReceiverPublic| { &mut m.point_y }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_z", + |m: &ReceiverPublic| { &m.point_z }, + |m: &mut ReceiverPublic| { &mut m.point_z }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "ReceiverPublic", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static ReceiverPublic { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(ReceiverPublic::new) + } +} + +impl ::protobuf::Clear for ReceiverPublic { + fn clear(&mut self) { + self.point_x.clear(); + self.point_y.clear(); + self.point_z.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for ReceiverPublic { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for ReceiverPublic { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct SenderPublicPair { + // message fields + pub figure_print: ::std::vec::Vec, + pub point_w: ::std::vec::Vec, + pub encrypt_message: ::std::vec::Vec, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a SenderPublicPair { + fn default() -> &'a SenderPublicPair { + ::default_instance() + } +} + +impl SenderPublicPair { + pub fn new() -> SenderPublicPair { + ::std::default::Default::default() + } + + // bytes figure_print = 1; + + + pub fn get_figure_print(&self) -> &[u8] { + &self.figure_print + } + pub fn clear_figure_print(&mut self) { + self.figure_print.clear(); + } + + // Param is passed by value, moved + pub fn set_figure_print(&mut self, v: ::std::vec::Vec) { + self.figure_print = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_figure_print(&mut self) -> &mut ::std::vec::Vec { + &mut self.figure_print + } + + // Take field + pub fn take_figure_print(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.figure_print, ::std::vec::Vec::new()) + } + + // bytes point_w = 2; + + + pub fn get_point_w(&self) -> &[u8] { + &self.point_w + } + pub fn clear_point_w(&mut self) { + self.point_w.clear(); + } + + // Param is passed by value, moved + pub fn set_point_w(&mut self, v: ::std::vec::Vec) { + self.point_w = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_w(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_w + } + + // Take field + pub fn take_point_w(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_w, ::std::vec::Vec::new()) + } + + // bytes encrypt_message = 3; + + + pub fn get_encrypt_message(&self) -> &[u8] { + &self.encrypt_message + } + pub fn clear_encrypt_message(&mut self) { + self.encrypt_message.clear(); + } + + // Param is passed by value, moved + pub fn set_encrypt_message(&mut self, v: ::std::vec::Vec) { + self.encrypt_message = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_encrypt_message(&mut self) -> &mut ::std::vec::Vec { + &mut self.encrypt_message + } + + // Take field + pub fn take_encrypt_message(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.encrypt_message, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for SenderPublicPair { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.figure_print)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_w)?; + }, + 3 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.encrypt_message)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.figure_print.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.figure_print); + } + if !self.point_w.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.point_w); + } + if !self.encrypt_message.is_empty() { + my_size += ::protobuf::rt::bytes_size(3, &self.encrypt_message); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.figure_print.is_empty() { + os.write_bytes(1, &self.figure_print)?; + } + if !self.point_w.is_empty() { + os.write_bytes(2, &self.point_w)?; + } + if !self.encrypt_message.is_empty() { + os.write_bytes(3, &self.encrypt_message)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> SenderPublicPair { + SenderPublicPair::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "figure_print", + |m: &SenderPublicPair| { &m.figure_print }, + |m: &mut SenderPublicPair| { &mut m.figure_print }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_w", + |m: &SenderPublicPair| { &m.point_w }, + |m: &mut SenderPublicPair| { &mut m.point_w }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "encrypt_message", + |m: &SenderPublicPair| { &m.encrypt_message }, + |m: &mut SenderPublicPair| { &mut m.encrypt_message }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderPublicPair", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static SenderPublicPair { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderPublicPair::new) + } +} + +impl ::protobuf::Clear for SenderPublicPair { + fn clear(&mut self) { + self.figure_print.clear(); + self.point_w.clear(); + self.encrypt_message.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for SenderPublicPair { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for SenderPublicPair { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct SenderPublic { + // message fields + pub pair: ::protobuf::RepeatedField, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a SenderPublic { + fn default() -> &'a SenderPublic { + ::default_instance() + } +} + +impl SenderPublic { + pub fn new() -> SenderPublic { + ::std::default::Default::default() + } + + // repeated .com.webank.wedpr.crypto.proto.SenderPublicPair pair = 1; + + + pub fn get_pair(&self) -> &[SenderPublicPair] { + &self.pair + } + pub fn clear_pair(&mut self) { + self.pair.clear(); + } + + // Param is passed by value, moved + pub fn set_pair(&mut self, v: ::protobuf::RepeatedField) { + self.pair = v; + } + + // Mutable pointer to the field. + pub fn mut_pair(&mut self) -> &mut ::protobuf::RepeatedField { + &mut self.pair + } + + // Take field + pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { + ::std::mem::replace(&mut self.pair, ::protobuf::RepeatedField::new()) + } +} + +impl ::protobuf::Message for SenderPublic { + fn is_initialized(&self) -> bool { + for v in &self.pair { + if !v.is_initialized() { + return false; + } + }; + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pair)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + for value in &self.pair { + let len = value.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; + }; + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + for v in &self.pair { + os.write_tag(1, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; + }; + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> SenderPublic { + SenderPublic::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( + "pair", + |m: &SenderPublic| { &m.pair }, + |m: &mut SenderPublic| { &mut m.pair }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderPublic", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static SenderPublic { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderPublic::new) + } +} + +impl ::protobuf::Clear for SenderPublic { + fn clear(&mut self) { + self.pair.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for SenderPublic { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for SenderPublic { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct SenderDataPair { + // message fields + pub id: ::std::vec::Vec, + pub message: ::std::vec::Vec, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a SenderDataPair { + fn default() -> &'a SenderDataPair { + ::default_instance() + } +} + +impl SenderDataPair { + pub fn new() -> SenderDataPair { + ::std::default::Default::default() + } + + // bytes id = 1; + + + pub fn get_id(&self) -> &[u8] { + &self.id + } + pub fn clear_id(&mut self) { + self.id.clear(); + } + + // Param is passed by value, moved + pub fn set_id(&mut self, v: ::std::vec::Vec) { + self.id = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_id(&mut self) -> &mut ::std::vec::Vec { + &mut self.id + } + + // Take field + pub fn take_id(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.id, ::std::vec::Vec::new()) + } + + // bytes message = 2; + + + pub fn get_message(&self) -> &[u8] { + &self.message + } + pub fn clear_message(&mut self) { + self.message.clear(); + } + + // Param is passed by value, moved + pub fn set_message(&mut self, v: ::std::vec::Vec) { + self.message = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_message(&mut self) -> &mut ::std::vec::Vec { + &mut self.message + } + + // Take field + pub fn take_message(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.message, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for SenderDataPair { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.id)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.message)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.id.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.id); + } + if !self.message.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.message); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.id.is_empty() { + os.write_bytes(1, &self.id)?; + } + if !self.message.is_empty() { + os.write_bytes(2, &self.message)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> SenderDataPair { + SenderDataPair::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "id", + |m: &SenderDataPair| { &m.id }, + |m: &mut SenderDataPair| { &mut m.id }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "message", + |m: &SenderDataPair| { &m.message }, + |m: &mut SenderDataPair| { &mut m.message }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderDataPair", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static SenderDataPair { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderDataPair::new) + } +} + +impl ::protobuf::Clear for SenderDataPair { + fn clear(&mut self) { + self.id.clear(); + self.message.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for SenderDataPair { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for SenderDataPair { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct SenderData { + // message fields + pub pair: ::protobuf::RepeatedField, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a SenderData { + fn default() -> &'a SenderData { + ::default_instance() + } +} + +impl SenderData { + pub fn new() -> SenderData { + ::std::default::Default::default() + } + + // repeated .com.webank.wedpr.crypto.proto.SenderDataPair pair = 1; + + + pub fn get_pair(&self) -> &[SenderDataPair] { + &self.pair + } + pub fn clear_pair(&mut self) { + self.pair.clear(); + } + + // Param is passed by value, moved + pub fn set_pair(&mut self, v: ::protobuf::RepeatedField) { + self.pair = v; + } + + // Mutable pointer to the field. + pub fn mut_pair(&mut self) -> &mut ::protobuf::RepeatedField { + &mut self.pair + } + + // Take field + pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { + ::std::mem::replace(&mut self.pair, ::protobuf::RepeatedField::new()) + } +} + +impl ::protobuf::Message for SenderData { + fn is_initialized(&self) -> bool { + for v in &self.pair { + if !v.is_initialized() { + return false; + } + }; + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pair)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + for value in &self.pair { + let len = value.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; + }; + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + for v in &self.pair { + os.write_tag(1, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; + }; + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> SenderData { + SenderData::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( + "pair", + |m: &SenderData| { &m.pair }, + |m: &mut SenderData| { &mut m.pair }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderData", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static SenderData { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderData::new) + } +} + +impl ::protobuf::Clear for SenderData { + fn clear(&mut self) { + self.pair.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for SenderData { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for SenderData { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +static file_descriptor_proto_data: &'static [u8] = b"\ + \n\x0fcrypto/ot.proto\x12\x1dcom.webank.wedpr.crypto.proto\"F\n\x0eRecei\ + verSecret\x12\x19\n\x08scalar_a\x18\x01\x20\x01(\x0cR\x07scalarA\x12\x19\ + \n\x08scalar_b\x18\x02\x20\x01(\x0cR\x07scalarB\"[\n\x0eReceiverPublic\ + \x12\x17\n\x07point_x\x18\x01\x20\x01(\x0cR\x06pointX\x12\x17\n\x07point\ + _y\x18\x02\x20\x01(\x0cR\x06pointY\x12\x17\n\x07point_z\x18\x03\x20\x01(\ + \x0cR\x06pointZ\"w\n\x10SenderPublicPair\x12!\n\x0cfigure_print\x18\x01\ + \x20\x01(\x0cR\x0bfigurePrint\x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\ + \x06pointW\x12'\n\x0fencrypt_message\x18\x03\x20\x01(\x0cR\x0eencryptMes\ + sage\"S\n\x0cSenderPublic\x12C\n\x04pair\x18\x01\x20\x03(\x0b2/.com.weba\ + nk.wedpr.crypto.proto.SenderPublicPairR\x04pair\":\n\x0eSenderDataPair\ + \x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x18\n\x07message\x18\ + \x02\x20\x01(\x0cR\x07message\"O\n\nSenderData\x12A\n\x04pair\x18\x01\ + \x20\x03(\x0b2-.com.webank.wedpr.crypto.proto.SenderDataPairR\x04pairB!\ + \n\x1dcom.webank.wedpr.crypto.protoP\x01b\x06proto3\ +"; + +static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; + +fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto { + ::protobuf::Message::parse_from_bytes(file_descriptor_proto_data).unwrap() +} + +pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto { + file_descriptor_proto_lazy.get(|| { + parse_descriptor_proto() + }) +} diff --git a/protos/src/main.rs b/protos/src/main.rs index 6236b88..174043e 100644 --- a/protos/src/main.rs +++ b/protos/src/main.rs @@ -27,7 +27,7 @@ fn generate_proto_for_all() { .includes(&["."]) // List all used proto files here. // You can remove any proto files that are not used by your project. - .inputs(&["crypto/zkp.proto", "crypto/common.proto"]) + .inputs(&["crypto/zkp.proto", "crypto/common.proto", "crypto/ot.proto"]) .customize(Customize { ..Default::default() }) From 2653bbea110ec2e1ecec0ebf186f0ad2052e48c9 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 7 Jun 2021 17:44:00 +0800 Subject: [PATCH 05/17] update point multi --- .../base_ot/benches/base_ot.rs | 24 +++++++++++++++ crypto/oblivious_transfer/base_ot/src/lib.rs | 29 +++++++++++++------ protos/src/generated/mod.rs | 2 +- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs index c5f7556..6564790 100644 --- a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -30,6 +30,10 @@ fn create_base_ot_helper(c: &mut Criterion, message_count: u64, str_len: u64) { sender_data.get_pair()[sender_data.get_pair().len() / 2].get_id(); let true_message = sender_data.get_pair()[sender_data.get_pair().len() / 2].get_message(); + // let choose_id = + // sender_data.get_pair()[0].get_id(); + // let true_message = + // sender_data.get_pair()[0].get_message(); c.bench_function(&label, move |b| { b.iter(|| { let (r_secret, r_public) = receiver_init(choose_id); @@ -56,6 +60,22 @@ fn create_base_ot_10000_10(c: &mut Criterion) { create_base_ot_helper(c, 10000, 10); } +fn create_base_ot_300_10(c: &mut Criterion) { + create_base_ot_helper(c, 300, 10); +} + +fn create_base_ot_3000_10(c: &mut Criterion) { + create_base_ot_helper(c, 3000, 10); +} + +fn create_base_ot_30000_10(c: &mut Criterion) { + create_base_ot_helper(c, 30000, 10); +} + +fn create_base_ot_300000_10(c: &mut Criterion) { + create_base_ot_helper(c, 300000, 10); +} + criterion_group! { name = init_base_ot_test; config = Criterion::default().sample_size(10); @@ -64,6 +84,10 @@ create_base_ot_10_10, create_base_ot_100_10, create_base_ot_1000_10, create_base_ot_10000_10, + create_base_ot_300_10, + create_base_ot_3000_10, + create_base_ot_30000_10, + // create_base_ot_300000_10, } criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index 2136142..bab1592 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -1,7 +1,9 @@ #[macro_use] extern crate lazy_static; -use curve25519_dalek::scalar::Scalar; +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; use sha3::Sha3_512; use wedpr_l_crypto_hash_sha3::WedprSha3_256; use wedpr_l_crypto_zkp_utils::{ @@ -23,9 +25,12 @@ pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { let blinding_a = get_random_scalar(); let blinding_b = get_random_scalar(); let c_id = blinding_a * blinding_b; - let point_x = blinding_a * *BASEPOINT_G1; - let point_y = blinding_b * *BASEPOINT_G1; - let point_z = (c_id - id_scalar) * *BASEPOINT_G1; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + let point_z = + RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); ( ReceiverSecret { scalar_a: scalar_to_bytes(&blinding_a), @@ -54,13 +59,18 @@ pub fn sender_init( for data_pair in data.get_pair() { let blinding_r = get_random_scalar(); let blinding_s = get_random_scalar(); - let point_w = blinding_s * point_x + blinding_r * *BASEPOINT_G1; + let point_w = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); let message = data_pair.get_message(); let id = data_pair.get_id(); let id_scalar = Scalar::hash_from_bytes::(id); - let point_key = blinding_s * point_z - + blinding_s * id_scalar * *BASEPOINT_G1 - + blinding_r * point_y; + let point_key = RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + ); let mut bytes_key = point_to_bytes(&point_key); while message.len() > bytes_key.len() { for key_bytes in bytes_key.clone() { @@ -96,7 +106,8 @@ pub fn receiver_decrypt( // } let point_w = bytes_to_point(pair.get_point_w())?; let encrypt_message = pair.get_encrypt_message(); - let point_key = blinding_b * point_w; + let point_key = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); let mut bytes_key = point_to_bytes(&point_key); while encrypt_message.len() > bytes_key.len() { for key_bytes in bytes_key.clone() { diff --git a/protos/src/generated/mod.rs b/protos/src/generated/mod.rs index 4276416..b79093b 100644 --- a/protos/src/generated/mod.rs +++ b/protos/src/generated/mod.rs @@ -1,5 +1,5 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. pub mod common; -pub mod zkp; pub mod ot; +pub mod zkp; From 370896f4830939c46df1d635d99c3170d02b1222 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 8 Jun 2021 10:54:30 +0800 Subject: [PATCH 06/17] update k out of n --- crypto/oblivious_transfer/base_ot/Cargo.toml | 1 + .../base_ot/benches/base_ot.rs | 94 ++- crypto/oblivious_transfer/base_ot/src/lib.rs | 155 +++- protos/crypto/ot.proto | 17 + protos/src/generated/ot.rs | 793 ++++++++++++++++-- 5 files changed, 996 insertions(+), 64 deletions(-) diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml index 8186365..8bc1518 100644 --- a/crypto/oblivious_transfer/base_ot/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -13,6 +13,7 @@ lazy_static = "1.4.0" wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" sha3 = "0.8.2" +#rayon = "1.5" [dev-dependencies] criterion = "0.3" diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs index 6564790..1bbe46a 100644 --- a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -1,9 +1,60 @@ #[macro_use] extern crate criterion; -use base_ot::{receiver_decrypt, receiver_init, sender_init}; +use base_ot::{ + receiver_decrypt, receiver_decrypt_k_out_of_n, receiver_init, + receiver_init_k_out_of_n, sender_init, sender_init_k_out_of_n, +}; use criterion::Criterion; use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use wedpr_l_protos::generated::ot::{SenderData, SenderDataPair}; +use wedpr_l_protos::generated::ot::{IdList, SenderData, SenderDataPair}; + +fn create_base_ot_k_out_of_n_helper( + c: &mut Criterion, + k_choose_count: u64, + n_message_count: u64, + str_len: u64, +) { + let label = format!( + "create_base_ot_k_out_of_n_helper, k_choose_count = {}, \ + n_message_count = {}, str_len = {}", + k_choose_count, n_message_count, str_len + ); + let mut sender_data = SenderData::default(); + let mut expect: Vec> = vec![]; + for _ in 0..n_message_count { + let random_id: String = + thread_rng().sample_iter(&Alphanumeric).take(18).collect(); + let random_message: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(str_len as usize) + .collect(); + sender_data.mut_pair().push(SenderDataPair { + id: random_id.as_bytes().to_vec(), + message: random_message.as_bytes().to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let mut choose_id_list = IdList::default(); + for i in 0..k_choose_count { + choose_id_list + .mut_id() + .push(sender_data.get_pair()[i as usize].get_id().to_vec()); + expect.push(sender_data.get_pair()[i as usize].get_message().to_vec()) + } + let use_data = sender_data.clone(); + c.bench_function(&label, move |b| { + b.iter(|| { + let (r_secret, r_public) = + receiver_init_k_out_of_n(&choose_id_list); + let s_public = + sender_init_k_out_of_n(&use_data, &r_public).unwrap(); + let message = + receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); + assert_eq!(message, expect); + }) + }); +} fn create_base_ot_helper(c: &mut Criterion, message_count: u64, str_len: u64) { let label = format!( @@ -76,17 +127,42 @@ fn create_base_ot_300000_10(c: &mut Criterion) { create_base_ot_helper(c, 300000, 10); } +fn create_base_ot_k_out_of_n_1_300_10(c: &mut Criterion) { + create_base_ot_k_out_of_n_helper(c, 1, 300, 10); +} + +fn create_base_ot_k_out_of_n_3_300_10(c: &mut Criterion) { + create_base_ot_k_out_of_n_helper(c, 3, 300, 10); +} + +fn create_base_ot_k_out_of_n_15_300_10(c: &mut Criterion) { + create_base_ot_k_out_of_n_helper(c, 15, 300, 10); +} + +fn create_base_ot_k_out_of_n_30_300_10(c: &mut Criterion) { + create_base_ot_k_out_of_n_helper(c, 30, 300, 10); +} + +fn create_base_ot_k_out_of_n_60_300_10(c: &mut Criterion) { + create_base_ot_k_out_of_n_helper(c, 60, 300, 10); +} + criterion_group! { name = init_base_ot_test; config = Criterion::default().sample_size(10); targets = -create_base_ot_10_10, -create_base_ot_100_10, -create_base_ot_1000_10, -create_base_ot_10000_10, - create_base_ot_300_10, - create_base_ot_3000_10, - create_base_ot_30000_10, +// create_base_ot_10_10, +// create_base_ot_100_10, +// create_base_ot_1000_10, +// create_base_ot_10000_10, +// create_base_ot_300_10, +// create_base_ot_3000_10, +// create_base_ot_30000_10, + create_base_ot_k_out_of_n_1_300_10, + create_base_ot_k_out_of_n_3_300_10, + create_base_ot_k_out_of_n_15_300_10, + create_base_ot_k_out_of_n_30_300_10, + create_base_ot_k_out_of_n_60_300_10, // create_base_ot_300000_10, } diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index bab1592..e94ec04 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -11,15 +11,129 @@ use wedpr_l_crypto_zkp_utils::{ scalar_to_bytes, BASEPOINT_G1, }; use wedpr_l_protos::generated::ot::{ - ReceiverPublic, ReceiverSecret, SenderData, SenderDataPair, SenderPublic, - SenderPublicPair, + IdList, ReceiverPublic, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, + SenderDataPair, SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, }; use wedpr_l_utils::{error::WedprError, traits::Hash}; +// use rayon::prelude::*; lazy_static! { pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); } +pub fn receiver_init_k_out_of_n( + id_list: &IdList, +) -> (ReceiverSecret, ReceiverPublicKOutOfN) { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let mut r_public = ReceiverPublicKOutOfN::default(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + r_public.set_point_x(point_to_bytes(&point_x)); + r_public.set_point_y(point_to_bytes(&point_y)); + for id in id_list.get_id() { + let id_scalar = Scalar::hash_from_bytes::(id); + let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ + *BASEPOINT_G1, + ]); + r_public.mut_point_z().push(point_to_bytes(&point_z)); + } + ( + ReceiverSecret { + scalar_a: scalar_to_bytes(&blinding_a), + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + r_public, + ) +} + +pub fn sender_init_k_out_of_n( + data: &SenderData, + r_public: &ReceiverPublicKOutOfN, +) -> Result { + let mut sender_public = SenderPublic::default(); + let point_x = bytes_to_point(r_public.get_point_x())?; + let point_y = bytes_to_point(r_public.get_point_y())?; + + for data_pair in data.get_pair() { + let mut pair = SenderPublicPairKOutOfN::default(); + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let point_w = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + pair.set_figure_print(HASH_SHA3_256.hash(message)); + pair.set_point_w(point_to_bytes(&point_w)); + for k_point_z in r_public.get_point_z() { + let point_z = bytes_to_point(k_point_z)?; + let point_key = RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + ); + let mut bytes_key = point_to_bytes(&point_key); + while message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let encrypt_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + pair.mut_encrypt_message().push(encrypt_message); + } + sender_public.mut_pairKN().push(pair) + } + Ok(sender_public) +} + +pub fn receiver_decrypt_k_out_of_n( + secret: &ReceiverSecret, + sender_public: &SenderPublic, +) -> Result>, WedprError> { + let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; + let mut result: Vec> = vec![]; + let mut skip_vector: Vec<&[u8]> = vec![]; + for pair in sender_public.get_pairKN() { + let point_w = bytes_to_point(pair.get_point_w())?; + if skip_vector.contains(&pair.get_figure_print()) { + continue; + } + let point_key = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); + let mut bytes_key = point_to_bytes(&point_key); + for encrypt_message in pair.get_encrypt_message() { + while encrypt_message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let decrypt_message: Vec = encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() + { + result.push(decrypt_message); + skip_vector.push(pair.get_figure_print()); + } + } + } + Ok(result) +} + pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { let id_scalar = Scalar::hash_from_bytes::(id); let blinding_a = get_random_scalar(); @@ -82,7 +196,6 @@ pub fn sender_init( .zip(bytes_key.iter()) .map(|(&x1, &x2)| x1 ^ x2) .collect(); - // println!("encrypt_message = {:?}", encrypt_message.clone()); sender_public.mut_pair().push(SenderPublicPair { figure_print: HASH_SHA3_256.hash(message), point_w: point_to_bytes(&point_w), @@ -97,10 +210,10 @@ pub fn sender_init( pub fn receiver_decrypt( secret: &ReceiverSecret, // id: &[u8], - encrypt_message: &SenderPublic, + sender_public: &SenderPublic, ) -> Result, WedprError> { let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - for pair in encrypt_message.get_pair() { + for pair in sender_public.get_pair() { // if id != pair.get_id() { // continue; // } @@ -130,6 +243,34 @@ pub fn receiver_decrypt( mod tests { use super::*; + #[test] + fn test_base_ot_k_out_of_n() { + let mut choose_id_pb = IdList::default(); + let mut expect: Vec> = vec![]; + choose_id_pb.mut_id().push("10086".as_bytes().to_vec()); + choose_id_pb.mut_id().push("10010".as_bytes().to_vec()); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ("10000".as_bytes(), "wedpr test1".as_bytes()), + ("10086".as_bytes(), "wedpr test2".as_bytes()), + ("10010".as_bytes(), "wedpr test3".as_bytes()), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init_k_out_of_n(&choose_id_pb); + let s_public = sender_init_k_out_of_n(&sender_data, &r_public).unwrap(); + let message = + receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); + expect.push("wedpr test2".as_bytes().to_vec()); + expect.push("wedpr test3".as_bytes().to_vec()); + assert_eq!(message, expect); + } + #[test] fn test_base_ot() { let choose_id = "10086".as_bytes(); @@ -148,9 +289,7 @@ mod tests { } let (r_secret, r_public) = receiver_init(choose_id); let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = - // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); - receiver_decrypt(&r_secret, &s_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); assert_eq!(message, "wedpr test2".as_bytes()); } diff --git a/protos/crypto/ot.proto b/protos/crypto/ot.proto index 766dcc5..ff30e98 100644 --- a/protos/crypto/ot.proto +++ b/protos/crypto/ot.proto @@ -18,14 +18,27 @@ message ReceiverPublic { bytes point_z = 3; } +message ReceiverPublicKOutOfN { + bytes point_x = 1; + bytes point_y = 2; + repeated bytes point_z = 3; +} + message SenderPublicPair { bytes figure_print = 1; bytes point_w = 2; bytes encrypt_message = 3; } +message SenderPublicPairKOutOfN { + bytes figure_print = 1; + bytes point_w = 2; + repeated bytes encrypt_message = 3; +} + message SenderPublic { repeated SenderPublicPair pair = 1; + repeated SenderPublicPairKOutOfN pairKN = 2; } message SenderDataPair { @@ -36,3 +49,7 @@ message SenderDataPair { message SenderData { repeated SenderDataPair pair = 1; } + +message IdList { + repeated bytes id = 1; +} diff --git a/protos/src/generated/ot.rs b/protos/src/generated/ot.rs index dc40d4f..17caac0 100644 --- a/protos/src/generated/ot.rs +++ b/protos/src/generated/ot.rs @@ -467,25 +467,510 @@ impl ::protobuf::reflect::ProtobufValue for ReceiverPublic { } } +#[derive(PartialEq,Clone,Default)] +pub struct ReceiverPublicKOutOfN { + // message fields + pub point_x: ::std::vec::Vec, + pub point_y: ::std::vec::Vec, + pub point_z: ::protobuf::RepeatedField<::std::vec::Vec>, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a ReceiverPublicKOutOfN { + fn default() -> &'a ReceiverPublicKOutOfN { + ::default_instance() + } +} + +impl ReceiverPublicKOutOfN { + pub fn new() -> ReceiverPublicKOutOfN { + ::std::default::Default::default() + } + + // bytes point_x = 1; + + + pub fn get_point_x(&self) -> &[u8] { + &self.point_x + } + pub fn clear_point_x(&mut self) { + self.point_x.clear(); + } + + // Param is passed by value, moved + pub fn set_point_x(&mut self, v: ::std::vec::Vec) { + self.point_x = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_x(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_x + } + + // Take field + pub fn take_point_x(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_x, ::std::vec::Vec::new()) + } + + // bytes point_y = 2; + + + pub fn get_point_y(&self) -> &[u8] { + &self.point_y + } + pub fn clear_point_y(&mut self) { + self.point_y.clear(); + } + + // Param is passed by value, moved + pub fn set_point_y(&mut self, v: ::std::vec::Vec) { + self.point_y = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_y(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_y + } + + // Take field + pub fn take_point_y(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_y, ::std::vec::Vec::new()) + } + + // repeated bytes point_z = 3; + + + pub fn get_point_z(&self) -> &[::std::vec::Vec] { + &self.point_z + } + pub fn clear_point_z(&mut self) { + self.point_z.clear(); + } + + // Param is passed by value, moved + pub fn set_point_z(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec>) { + self.point_z = v; + } + + // Mutable pointer to the field. + pub fn mut_point_z(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec> { + &mut self.point_z + } + + // Take field + pub fn take_point_z(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec> { + ::std::mem::replace(&mut self.point_z, ::protobuf::RepeatedField::new()) + } +} + +impl ::protobuf::Message for ReceiverPublicKOutOfN { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_x)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_y)?; + }, + 3 => { + ::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.point_z)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.point_x.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.point_x); + } + if !self.point_y.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.point_y); + } + for value in &self.point_z { + my_size += ::protobuf::rt::bytes_size(3, &value); + }; + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.point_x.is_empty() { + os.write_bytes(1, &self.point_x)?; + } + if !self.point_y.is_empty() { + os.write_bytes(2, &self.point_y)?; + } + for v in &self.point_z { + os.write_bytes(3, &v)?; + }; + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> ReceiverPublicKOutOfN { + ReceiverPublicKOutOfN::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_x", + |m: &ReceiverPublicKOutOfN| { &m.point_x }, + |m: &mut ReceiverPublicKOutOfN| { &mut m.point_x }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_y", + |m: &ReceiverPublicKOutOfN| { &m.point_y }, + |m: &mut ReceiverPublicKOutOfN| { &mut m.point_y }, + )); + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_z", + |m: &ReceiverPublicKOutOfN| { &m.point_z }, + |m: &mut ReceiverPublicKOutOfN| { &mut m.point_z }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "ReceiverPublicKOutOfN", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static ReceiverPublicKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(ReceiverPublicKOutOfN::new) + } +} + +impl ::protobuf::Clear for ReceiverPublicKOutOfN { + fn clear(&mut self) { + self.point_x.clear(); + self.point_y.clear(); + self.point_z.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for ReceiverPublicKOutOfN { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for ReceiverPublicKOutOfN { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + #[derive(PartialEq,Clone,Default)] pub struct SenderPublicPair { // message fields pub figure_print: ::std::vec::Vec, pub point_w: ::std::vec::Vec, - pub encrypt_message: ::std::vec::Vec, + pub encrypt_message: ::std::vec::Vec, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a SenderPublicPair { + fn default() -> &'a SenderPublicPair { + ::default_instance() + } +} + +impl SenderPublicPair { + pub fn new() -> SenderPublicPair { + ::std::default::Default::default() + } + + // bytes figure_print = 1; + + + pub fn get_figure_print(&self) -> &[u8] { + &self.figure_print + } + pub fn clear_figure_print(&mut self) { + self.figure_print.clear(); + } + + // Param is passed by value, moved + pub fn set_figure_print(&mut self, v: ::std::vec::Vec) { + self.figure_print = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_figure_print(&mut self) -> &mut ::std::vec::Vec { + &mut self.figure_print + } + + // Take field + pub fn take_figure_print(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.figure_print, ::std::vec::Vec::new()) + } + + // bytes point_w = 2; + + + pub fn get_point_w(&self) -> &[u8] { + &self.point_w + } + pub fn clear_point_w(&mut self) { + self.point_w.clear(); + } + + // Param is passed by value, moved + pub fn set_point_w(&mut self, v: ::std::vec::Vec) { + self.point_w = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_point_w(&mut self) -> &mut ::std::vec::Vec { + &mut self.point_w + } + + // Take field + pub fn take_point_w(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.point_w, ::std::vec::Vec::new()) + } + + // bytes encrypt_message = 3; + + + pub fn get_encrypt_message(&self) -> &[u8] { + &self.encrypt_message + } + pub fn clear_encrypt_message(&mut self) { + self.encrypt_message.clear(); + } + + // Param is passed by value, moved + pub fn set_encrypt_message(&mut self, v: ::std::vec::Vec) { + self.encrypt_message = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_encrypt_message(&mut self) -> &mut ::std::vec::Vec { + &mut self.encrypt_message + } + + // Take field + pub fn take_encrypt_message(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.encrypt_message, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for SenderPublicPair { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.figure_print)?; + }, + 2 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_w)?; + }, + 3 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.encrypt_message)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.figure_print.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.figure_print); + } + if !self.point_w.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.point_w); + } + if !self.encrypt_message.is_empty() { + my_size += ::protobuf::rt::bytes_size(3, &self.encrypt_message); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.figure_print.is_empty() { + os.write_bytes(1, &self.figure_print)?; + } + if !self.point_w.is_empty() { + os.write_bytes(2, &self.point_w)?; + } + if !self.encrypt_message.is_empty() { + os.write_bytes(3, &self.encrypt_message)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> SenderPublicPair { + SenderPublicPair::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "figure_print", + |m: &SenderPublicPair| { &m.figure_print }, + |m: &mut SenderPublicPair| { &mut m.figure_print }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "point_w", + |m: &SenderPublicPair| { &m.point_w }, + |m: &mut SenderPublicPair| { &mut m.point_w }, + )); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "encrypt_message", + |m: &SenderPublicPair| { &m.encrypt_message }, + |m: &mut SenderPublicPair| { &mut m.encrypt_message }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderPublicPair", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static SenderPublicPair { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderPublicPair::new) + } +} + +impl ::protobuf::Clear for SenderPublicPair { + fn clear(&mut self) { + self.figure_print.clear(); + self.point_w.clear(); + self.encrypt_message.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for SenderPublicPair { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for SenderPublicPair { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct SenderPublicPairKOutOfN { + // message fields + pub figure_print: ::std::vec::Vec, + pub point_w: ::std::vec::Vec, + pub encrypt_message: ::protobuf::RepeatedField<::std::vec::Vec>, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a SenderPublicPair { - fn default() -> &'a SenderPublicPair { - ::default_instance() +impl<'a> ::std::default::Default for &'a SenderPublicPairKOutOfN { + fn default() -> &'a SenderPublicPairKOutOfN { + ::default_instance() } } -impl SenderPublicPair { - pub fn new() -> SenderPublicPair { +impl SenderPublicPairKOutOfN { + pub fn new() -> SenderPublicPairKOutOfN { ::std::default::Default::default() } @@ -541,10 +1026,10 @@ impl SenderPublicPair { ::std::mem::replace(&mut self.point_w, ::std::vec::Vec::new()) } - // bytes encrypt_message = 3; + // repeated bytes encrypt_message = 3; - pub fn get_encrypt_message(&self) -> &[u8] { + pub fn get_encrypt_message(&self) -> &[::std::vec::Vec] { &self.encrypt_message } pub fn clear_encrypt_message(&mut self) { @@ -552,23 +1037,22 @@ impl SenderPublicPair { } // Param is passed by value, moved - pub fn set_encrypt_message(&mut self, v: ::std::vec::Vec) { + pub fn set_encrypt_message(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec>) { self.encrypt_message = v; } // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_encrypt_message(&mut self) -> &mut ::std::vec::Vec { + pub fn mut_encrypt_message(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec> { &mut self.encrypt_message } // Take field - pub fn take_encrypt_message(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.encrypt_message, ::std::vec::Vec::new()) + pub fn take_encrypt_message(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec> { + ::std::mem::replace(&mut self.encrypt_message, ::protobuf::RepeatedField::new()) } } -impl ::protobuf::Message for SenderPublicPair { +impl ::protobuf::Message for SenderPublicPairKOutOfN { fn is_initialized(&self) -> bool { true } @@ -584,7 +1068,7 @@ impl ::protobuf::Message for SenderPublicPair { ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_w)?; }, 3 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.encrypt_message)?; + ::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.encrypt_message)?; }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; @@ -604,9 +1088,9 @@ impl ::protobuf::Message for SenderPublicPair { if !self.point_w.is_empty() { my_size += ::protobuf::rt::bytes_size(2, &self.point_w); } - if !self.encrypt_message.is_empty() { - my_size += ::protobuf::rt::bytes_size(3, &self.encrypt_message); - } + for value in &self.encrypt_message { + my_size += ::protobuf::rt::bytes_size(3, &value); + }; my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); self.cached_size.set(my_size); my_size @@ -619,9 +1103,9 @@ impl ::protobuf::Message for SenderPublicPair { if !self.point_w.is_empty() { os.write_bytes(2, &self.point_w)?; } - if !self.encrypt_message.is_empty() { - os.write_bytes(3, &self.encrypt_message)?; - } + for v in &self.encrypt_message { + os.write_bytes(3, &v)?; + }; os.write_unknown_fields(self.get_unknown_fields())?; ::std::result::Result::Ok(()) } @@ -652,8 +1136,8 @@ impl ::protobuf::Message for SenderPublicPair { Self::descriptor_static() } - fn new() -> SenderPublicPair { - SenderPublicPair::new() + fn new() -> SenderPublicPairKOutOfN { + SenderPublicPairKOutOfN::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { @@ -662,34 +1146,34 @@ impl ::protobuf::Message for SenderPublicPair { let mut fields = ::std::vec::Vec::new(); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "figure_print", - |m: &SenderPublicPair| { &m.figure_print }, - |m: &mut SenderPublicPair| { &mut m.figure_print }, + |m: &SenderPublicPairKOutOfN| { &m.figure_print }, + |m: &mut SenderPublicPairKOutOfN| { &mut m.figure_print }, )); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "point_w", - |m: &SenderPublicPair| { &m.point_w }, - |m: &mut SenderPublicPair| { &mut m.point_w }, + |m: &SenderPublicPairKOutOfN| { &m.point_w }, + |m: &mut SenderPublicPairKOutOfN| { &mut m.point_w }, )); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "encrypt_message", - |m: &SenderPublicPair| { &m.encrypt_message }, - |m: &mut SenderPublicPair| { &mut m.encrypt_message }, + |m: &SenderPublicPairKOutOfN| { &m.encrypt_message }, + |m: &mut SenderPublicPairKOutOfN| { &mut m.encrypt_message }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderPublicPair", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "SenderPublicPairKOutOfN", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static SenderPublicPair { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderPublicPair::new) + fn default_instance() -> &'static SenderPublicPairKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(SenderPublicPairKOutOfN::new) } } -impl ::protobuf::Clear for SenderPublicPair { +impl ::protobuf::Clear for SenderPublicPairKOutOfN { fn clear(&mut self) { self.figure_print.clear(); self.point_w.clear(); @@ -698,13 +1182,13 @@ impl ::protobuf::Clear for SenderPublicPair { } } -impl ::std::fmt::Debug for SenderPublicPair { +impl ::std::fmt::Debug for SenderPublicPairKOutOfN { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderPublicPair { +impl ::protobuf::reflect::ProtobufValue for SenderPublicPairKOutOfN { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } @@ -714,6 +1198,7 @@ impl ::protobuf::reflect::ProtobufValue for SenderPublicPair { pub struct SenderPublic { // message fields pub pair: ::protobuf::RepeatedField, + pub pairKN: ::protobuf::RepeatedField, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, @@ -754,6 +1239,31 @@ impl SenderPublic { pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { ::std::mem::replace(&mut self.pair, ::protobuf::RepeatedField::new()) } + + // repeated .com.webank.wedpr.crypto.proto.SenderPublicPairKOutOfN pairKN = 2; + + + pub fn get_pairKN(&self) -> &[SenderPublicPairKOutOfN] { + &self.pairKN + } + pub fn clear_pairKN(&mut self) { + self.pairKN.clear(); + } + + // Param is passed by value, moved + pub fn set_pairKN(&mut self, v: ::protobuf::RepeatedField) { + self.pairKN = v; + } + + // Mutable pointer to the field. + pub fn mut_pairKN(&mut self) -> &mut ::protobuf::RepeatedField { + &mut self.pairKN + } + + // Take field + pub fn take_pairKN(&mut self) -> ::protobuf::RepeatedField { + ::std::mem::replace(&mut self.pairKN, ::protobuf::RepeatedField::new()) + } } impl ::protobuf::Message for SenderPublic { @@ -763,6 +1273,11 @@ impl ::protobuf::Message for SenderPublic { return false; } }; + for v in &self.pairKN { + if !v.is_initialized() { + return false; + } + }; true } @@ -773,6 +1288,9 @@ impl ::protobuf::Message for SenderPublic { 1 => { ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pair)?; }, + 2 => { + ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pairKN)?; + }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; }, @@ -789,6 +1307,10 @@ impl ::protobuf::Message for SenderPublic { let len = value.compute_size(); my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; }; + for value in &self.pairKN { + let len = value.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; + }; my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); self.cached_size.set(my_size); my_size @@ -800,6 +1322,11 @@ impl ::protobuf::Message for SenderPublic { os.write_raw_varint32(v.get_cached_size())?; v.write_to_with_cached_sizes(os)?; }; + for v in &self.pairKN { + os.write_tag(2, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; + }; os.write_unknown_fields(self.get_unknown_fields())?; ::std::result::Result::Ok(()) } @@ -843,6 +1370,11 @@ impl ::protobuf::Message for SenderPublic { |m: &SenderPublic| { &m.pair }, |m: &mut SenderPublic| { &mut m.pair }, )); + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( + "pairKN", + |m: &SenderPublic| { &m.pairKN }, + |m: &mut SenderPublic| { &mut m.pairKN }, + )); ::protobuf::reflect::MessageDescriptor::new_pb_name::( "SenderPublic", fields, @@ -860,6 +1392,7 @@ impl ::protobuf::Message for SenderPublic { impl ::protobuf::Clear for SenderPublic { fn clear(&mut self) { self.pair.clear(); + self.pairKN.clear(); self.unknown_fields.clear(); } } @@ -1243,21 +1776,187 @@ impl ::protobuf::reflect::ProtobufValue for SenderData { } } +#[derive(PartialEq,Clone,Default)] +pub struct IdList { + // message fields + pub id: ::protobuf::RepeatedField<::std::vec::Vec>, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a IdList { + fn default() -> &'a IdList { + ::default_instance() + } +} + +impl IdList { + pub fn new() -> IdList { + ::std::default::Default::default() + } + + // repeated bytes id = 1; + + + pub fn get_id(&self) -> &[::std::vec::Vec] { + &self.id + } + pub fn clear_id(&mut self) { + self.id.clear(); + } + + // Param is passed by value, moved + pub fn set_id(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec>) { + self.id = v; + } + + // Mutable pointer to the field. + pub fn mut_id(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec> { + &mut self.id + } + + // Take field + pub fn take_id(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec> { + ::std::mem::replace(&mut self.id, ::protobuf::RepeatedField::new()) + } +} + +impl ::protobuf::Message for IdList { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.id)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + for value in &self.id { + my_size += ::protobuf::rt::bytes_size(1, &value); + }; + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + for v in &self.id { + os.write_bytes(1, &v)?; + }; + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> IdList { + IdList::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "id", + |m: &IdList| { &m.id }, + |m: &mut IdList| { &mut m.id }, + )); + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "IdList", + fields, + file_descriptor_proto() + ) + }) + } + + fn default_instance() -> &'static IdList { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(IdList::new) + } +} + +impl ::protobuf::Clear for IdList { + fn clear(&mut self) { + self.id.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for IdList { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for IdList { + fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { + ::protobuf::reflect::ReflectValueRef::Message(self) + } +} + static file_descriptor_proto_data: &'static [u8] = b"\ \n\x0fcrypto/ot.proto\x12\x1dcom.webank.wedpr.crypto.proto\"F\n\x0eRecei\ verSecret\x12\x19\n\x08scalar_a\x18\x01\x20\x01(\x0cR\x07scalarA\x12\x19\ \n\x08scalar_b\x18\x02\x20\x01(\x0cR\x07scalarB\"[\n\x0eReceiverPublic\ \x12\x17\n\x07point_x\x18\x01\x20\x01(\x0cR\x06pointX\x12\x17\n\x07point\ _y\x18\x02\x20\x01(\x0cR\x06pointY\x12\x17\n\x07point_z\x18\x03\x20\x01(\ - \x0cR\x06pointZ\"w\n\x10SenderPublicPair\x12!\n\x0cfigure_print\x18\x01\ - \x20\x01(\x0cR\x0bfigurePrint\x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\ - \x06pointW\x12'\n\x0fencrypt_message\x18\x03\x20\x01(\x0cR\x0eencryptMes\ - sage\"S\n\x0cSenderPublic\x12C\n\x04pair\x18\x01\x20\x03(\x0b2/.com.weba\ - nk.wedpr.crypto.proto.SenderPublicPairR\x04pair\":\n\x0eSenderDataPair\ - \x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x18\n\x07message\x18\ - \x02\x20\x01(\x0cR\x07message\"O\n\nSenderData\x12A\n\x04pair\x18\x01\ - \x20\x03(\x0b2-.com.webank.wedpr.crypto.proto.SenderDataPairR\x04pairB!\ - \n\x1dcom.webank.wedpr.crypto.protoP\x01b\x06proto3\ + \x0cR\x06pointZ\"b\n\x15ReceiverPublicKOutOfN\x12\x17\n\x07point_x\x18\ + \x01\x20\x01(\x0cR\x06pointX\x12\x17\n\x07point_y\x18\x02\x20\x01(\x0cR\ + \x06pointY\x12\x17\n\x07point_z\x18\x03\x20\x03(\x0cR\x06pointZ\"w\n\x10\ + SenderPublicPair\x12!\n\x0cfigure_print\x18\x01\x20\x01(\x0cR\x0bfigureP\ + rint\x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\x06pointW\x12'\n\x0fencr\ + ypt_message\x18\x03\x20\x01(\x0cR\x0eencryptMessage\"~\n\x17SenderPublic\ + PairKOutOfN\x12!\n\x0cfigure_print\x18\x01\x20\x01(\x0cR\x0bfigurePrint\ + \x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\x06pointW\x12'\n\x0fencrypt_\ + message\x18\x03\x20\x03(\x0cR\x0eencryptMessage\"\xa3\x01\n\x0cSenderPub\ + lic\x12C\n\x04pair\x18\x01\x20\x03(\x0b2/.com.webank.wedpr.crypto.proto.\ + SenderPublicPairR\x04pair\x12N\n\x06pairKN\x18\x02\x20\x03(\x0b26.com.we\ + bank.wedpr.crypto.proto.SenderPublicPairKOutOfNR\x06pairKN\":\n\x0eSende\ + rDataPair\x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x18\n\x07messa\ + ge\x18\x02\x20\x01(\x0cR\x07message\"O\n\nSenderData\x12A\n\x04pair\x18\ + \x01\x20\x03(\x0b2-.com.webank.wedpr.crypto.proto.SenderDataPairR\x04pai\ + r\"\x18\n\x06IdList\x12\x0e\n\x02id\x18\x01\x20\x03(\x0cR\x02idB!\n\x1dc\ + om.webank.wedpr.crypto.protoP\x01b\x06proto3\ "; static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; From 8d92bb268abc2722a5590a357284efa0b4ca417b Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Wed, 9 Jun 2021 12:28:47 +0800 Subject: [PATCH 07/17] add 1-2 base ot --- .../base_ot/benches/base_ot.rs | 58 ++++++- crypto/oblivious_transfer/base_ot/src/lib.rs | 2 + .../base_ot/src/one_out_of_two.rs | 159 ++++++++++++++++++ 3 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs index 1bbe46a..d83b184 100644 --- a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -1,6 +1,10 @@ #[macro_use] extern crate criterion; use base_ot::{ + one_out_of_two::{ + receiver_decrypt_1_out_of_2, receiver_init_1_out_of_2, + sender_init_1_out_of_2, DataOneOutOfTwo, + }, receiver_decrypt, receiver_decrypt_k_out_of_n, receiver_init, receiver_init_k_out_of_n, sender_init, sender_init_k_out_of_n, }; @@ -8,6 +12,41 @@ use criterion::Criterion; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use wedpr_l_protos::generated::ot::{IdList, SenderData, SenderDataPair}; +fn create_base_ot_1_out_of_2_helper(c: &mut Criterion, str_len: u64) { + let label = + format!("create_base_ot_1_out_of_2_helper, str_len = {}", str_len); + let random_message0: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(str_len as usize) + .collect(); + let random_message1: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(str_len as usize) + .collect(); + let data = DataOneOutOfTwo { + data0: random_message0.as_bytes().to_vec(), + data1: random_message1.as_bytes().to_vec(), + }; + + let choice = false; + let true_message = random_message0.as_bytes().to_vec(); + + c.bench_function(&label, move |b| { + b.iter(|| { + let (blinding_b, point_x, point_y, point_z) = + receiver_init_1_out_of_2(choice); + let sender_public = + sender_init_1_out_of_2(&data, &point_x, &point_y, &point_z); + let decrypt_message = receiver_decrypt_1_out_of_2( + choice, + &blinding_b, + &sender_public, + ); + assert_eq!(decrypt_message, true_message); + }) + }); +} + fn create_base_ot_k_out_of_n_helper( c: &mut Criterion, k_choose_count: u64, @@ -147,17 +186,22 @@ fn create_base_ot_k_out_of_n_60_300_10(c: &mut Criterion) { create_base_ot_k_out_of_n_helper(c, 60, 300, 10); } +fn create_base_ot_1_out_of_2_10(c: &mut Criterion) { + create_base_ot_1_out_of_2_helper(c, 10); +} + criterion_group! { name = init_base_ot_test; config = Criterion::default().sample_size(10); targets = -// create_base_ot_10_10, -// create_base_ot_100_10, -// create_base_ot_1000_10, -// create_base_ot_10000_10, -// create_base_ot_300_10, -// create_base_ot_3000_10, -// create_base_ot_30000_10, + create_base_ot_1_out_of_2_10, +create_base_ot_10_10, +create_base_ot_100_10, +create_base_ot_1000_10, +create_base_ot_10000_10, + create_base_ot_300_10, + create_base_ot_3000_10, + create_base_ot_30000_10, create_base_ot_k_out_of_n_1_300_10, create_base_ot_k_out_of_n_3_300_10, create_base_ot_k_out_of_n_15_300_10, diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index e94ec04..00d5c2b 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -17,6 +17,8 @@ use wedpr_l_protos::generated::ot::{ use wedpr_l_utils::{error::WedprError, traits::Hash}; // use rayon::prelude::*; +pub mod one_out_of_two; + lazy_static! { pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); } diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs new file mode 100644 index 0000000..cfcbc79 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs @@ -0,0 +1,159 @@ +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use wedpr_l_crypto_zkp_utils::{ + get_random_scalar, point_to_bytes, BASEPOINT_G1, +}; + +#[derive(Default, Debug, Clone)] +pub struct DataOneOutOfTwo { + pub data0: Vec, + pub data1: Vec, +} + +#[derive(Default, Debug, Clone)] +pub struct EncryptPairOneOutOfTwo { + pub point_w: RistrettoPoint, + pub encrypt_message: Vec, +} + +#[derive(Default, Debug, Clone)] +pub struct EncryptOneOutOfTwo { + pub encrypt0: EncryptPairOneOutOfTwo, + pub encrypt1: EncryptPairOneOutOfTwo, +} + +pub fn receiver_init_1_out_of_2( + choice: bool, +) -> (Scalar, RistrettoPoint, RistrettoPoint, RistrettoPoint) { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + let point_z: RistrettoPoint; + if choice { + point_z = RistrettoPoint::multiscalar_mul(&[c_id - Scalar::one()], &[ + *BASEPOINT_G1, + ]); + } else { + point_z = RistrettoPoint::multiscalar_mul(&[c_id], &[*BASEPOINT_G1]); + } + (blinding_b, point_x, point_y, point_z) +} + +pub fn sender_init_1_out_of_2( + data: &DataOneOutOfTwo, + point_x: &RistrettoPoint, + point_y: &RistrettoPoint, + point_z: &RistrettoPoint, +) -> EncryptOneOutOfTwo { + let point_z1 = point_z + *BASEPOINT_G1; + + // compute zero ot + let blinding_r0 = get_random_scalar(); + let blinding_s0 = get_random_scalar(); + let point_w0 = + RistrettoPoint::multiscalar_mul(&[blinding_s0, blinding_r0], &[ + *point_x, + *BASEPOINT_G1, + ]); + let point_key0 = + RistrettoPoint::multiscalar_mul(&[blinding_s0, blinding_r0], [ + point_z, point_y, + ]); + let bytes_key0 = point_to_bytes(&point_key0); + let encrypt_message0: Vec = data + .data0 + .iter() + .zip(bytes_key0.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + + // compute one ot + let blinding_r1 = get_random_scalar(); + let blinding_s1 = get_random_scalar(); + let point_w1 = + RistrettoPoint::multiscalar_mul(&[blinding_s1, blinding_r1], &[ + *point_x, + *BASEPOINT_G1, + ]); + let point_key1 = + RistrettoPoint::multiscalar_mul(&[blinding_s1, blinding_r1], &[ + point_z1, *point_y, + ]); + let bytes_key1 = point_to_bytes(&point_key1); + let encrypt_message1: Vec = data + .data1 + .iter() + .zip(bytes_key1.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + EncryptOneOutOfTwo { + encrypt0: EncryptPairOneOutOfTwo { + point_w: point_w0, + encrypt_message: encrypt_message0, + }, + encrypt1: EncryptPairOneOutOfTwo { + point_w: point_w1, + encrypt_message: encrypt_message1, + }, + } +} + +pub fn receiver_decrypt_1_out_of_2( + choice: bool, + blinding_b: &Scalar, + sender_public: &EncryptOneOutOfTwo, +) -> Vec { + if choice { + let point_key = + RistrettoPoint::multiscalar_mul([blinding_b], [sender_public + .encrypt1 + .point_w]); + let bytes_key = point_to_bytes(&point_key); + let decrypt_message: Vec = sender_public + .encrypt1 + .encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + return decrypt_message; + } + let point_key = + RistrettoPoint::multiscalar_mul([blinding_b], [sender_public + .encrypt0 + .point_w]); + let bytes_key = point_to_bytes(&point_key); + let decrypt_message: Vec = sender_public + .encrypt0 + .encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + return decrypt_message; +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_base_ot_1_out_of_2() { + let data = DataOneOutOfTwo { + data0: "wedpr test1".as_bytes().to_vec(), + data1: "wedpr test2".as_bytes().to_vec(), + }; + let choice = true; + let (blinding_b, point_x, point_y, point_z) = + receiver_init_1_out_of_2(choice); + let sender_public = + sender_init_1_out_of_2(&data, &point_x, &point_y, &point_z); + let decrypt_message = + receiver_decrypt_1_out_of_2(choice, &blinding_b, &sender_public); + assert_eq!(decrypt_message, "wedpr test2".as_bytes().to_vec()); + } +} From 1795c67fda6946925b2a10e52fdced3723bbabd1 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Wed, 9 Jun 2021 15:30:46 +0800 Subject: [PATCH 08/17] add mod --- Cargo.toml | 1 + crypto/oblivious_transfer/base_ot/Cargo.toml | 5 +++-- crypto/oblivious_transfer/base_ot/src/lib.rs | 11 ++++++----- .../oblivious_transfer/base_ot/src/utils.rs | 7 +++++++ .../oblivious_transfer/base_ot_mod/Cargo.toml | 12 ++++++++++++ .../base_ot_mod/src/constant.rs | 9 +++++++++ .../oblivious_transfer/base_ot_mod/src/lib.rs | 19 +++++++++++++++++++ crypto/zkp/utils/Cargo.toml | 5 +++-- crypto/zkp/utils/src/lib.rs | 10 +++++++--- 9 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 crypto/oblivious_transfer/base_ot/src/utils.rs create mode 100644 crypto/oblivious_transfer/base_ot_mod/Cargo.toml create mode 100644 crypto/oblivious_transfer/base_ot_mod/src/constant.rs create mode 100644 crypto/oblivious_transfer/base_ot_mod/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 6a3a24e..6038f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "crypto/hash/sha3", "crypto/hash/sm3", "crypto/oblivious_transfer/base_ot", + "crypto/oblivious_transfer/base_ot_mod", "crypto/signature/ed25519", "crypto/signature/secp256k1", "crypto/signature/sm2", diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml index 8bc1518..96e9b12 100644 --- a/crypto/oblivious_transfer/base_ot/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -6,13 +6,14 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -curve25519-dalek = { version = "1", features = [ "serde" ] } +curve25519-dalek = { version = "3", features = [ "serde" ] } wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } wedpr_l_crypto_hash_sha3 = { version = "1.0.0" } lazy_static = "1.4.0" wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" -sha3 = "0.8.2" +sha2 = "0.9" +rand = "0.6" #rayon = "1.5" [dev-dependencies] diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index 00d5c2b..216b624 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -4,7 +4,7 @@ extern crate lazy_static; use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; -use sha3::Sha3_512; +use sha2::Sha512; use wedpr_l_crypto_hash_sha3::WedprSha3_256; use wedpr_l_crypto_zkp_utils::{ bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, @@ -18,6 +18,7 @@ use wedpr_l_utils::{error::WedprError, traits::Hash}; // use rayon::prelude::*; pub mod one_out_of_two; +// pub mod utils; lazy_static! { pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); @@ -37,7 +38,7 @@ pub fn receiver_init_k_out_of_n( r_public.set_point_x(point_to_bytes(&point_x)); r_public.set_point_y(point_to_bytes(&point_y)); for id in id_list.get_id() { - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ *BASEPOINT_G1, ]); @@ -73,7 +74,7 @@ pub fn sender_init_k_out_of_n( ]); let message = data_pair.get_message(); let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); pair.set_figure_print(HASH_SHA3_256.hash(message)); pair.set_point_w(point_to_bytes(&point_w)); for k_point_z in r_public.get_point_z() { @@ -137,7 +138,7 @@ pub fn receiver_decrypt_k_out_of_n( } pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let blinding_a = get_random_scalar(); let blinding_b = get_random_scalar(); let c_id = blinding_a * blinding_b; @@ -182,7 +183,7 @@ pub fn sender_init( ]); let message = data_pair.get_message(); let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let point_key = RistrettoPoint::multiscalar_mul( &[blinding_s, blinding_s * id_scalar, blinding_r], &[point_z, *BASEPOINT_G1, point_y], diff --git a/crypto/oblivious_transfer/base_ot/src/utils.rs b/crypto/oblivious_transfer/base_ot/src/utils.rs new file mode 100644 index 0000000..0869954 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/utils.rs @@ -0,0 +1,7 @@ +// use curve25519_dalek::scalar::Scalar; +// +// pub fn get_random_scalar() -> Scalar { +// Scalar::random(&mut rand::thread_rng()) +// } +// + diff --git a/crypto/oblivious_transfer/base_ot_mod/Cargo.toml b/crypto/oblivious_transfer/base_ot_mod/Cargo.toml new file mode 100644 index 0000000..5de11df --- /dev/null +++ b/crypto/oblivious_transfer/base_ot_mod/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "base_ot_mod" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num-integer = "0.1.44" +rand = "0.8" +num-bigint = { version = "0.4", features = ["rand"] } +lazy_static = "1.4.0" diff --git a/crypto/oblivious_transfer/base_ot_mod/src/constant.rs b/crypto/oblivious_transfer/base_ot_mod/src/constant.rs new file mode 100644 index 0000000..d780744 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot_mod/src/constant.rs @@ -0,0 +1,9 @@ +use num_bigint::BigUint; + +lazy_static! { + /// A base mod big number used by various crypto algorithms. + pub static ref BASE_MOD: BigUint = ; + +} + +22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427 \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot_mod/src/lib.rs b/crypto/oblivious_transfer/base_ot_mod/src/lib.rs new file mode 100644 index 0000000..e35ed16 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot_mod/src/lib.rs @@ -0,0 +1,19 @@ +#[macro_use] +extern crate lazy_static; + +// pub mod constant; + +#[cfg(test)] +mod tests { + use super::*; + use num_bigint::BigUint; + use std::str::FromStr; + + #[test] + fn it_works() { + let prime_mod = BigUint::from_str("22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427").unwrap(); + let prime_gen = BigUint::from_str("21967776453576425418401845975004568131328756948517171056887978357803037829568879921671582562682834869268458693220178940296762093275708539056500850220701423436253549516692322294566575394724161597988209569075339356219938032314547111217962796852472235866809694733568073434189840033571185928562157743032806061986802973509384288857210981073943845944525063097134620867285393169672223797738231250393852177901783342394915102874707020281747941442896665787806091821561888468560319383163287935077324049516667817847046679134650369734057189968145858211980242981359451590399925956110049309589712838467090785843798902998395786695163").unwrap(); + println!("prime_mod = {:?}", prime_mod); + println!("prime_gen = {:?}", prime_gen); + } +} diff --git a/crypto/zkp/utils/Cargo.toml b/crypto/zkp/utils/Cargo.toml index bb833a4..4305d49 100644 --- a/crypto/zkp/utils/Cargo.toml +++ b/crypto/zkp/utils/Cargo.toml @@ -9,10 +9,11 @@ description = "Library of WeDPR shared zkp function utils." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -curve25519-dalek = { version = "1", features = [ "serde" ] } +curve25519-dalek = { version = "3", features = [ "serde" ] } lazy_static = "1.4.0" rand = "0.6" -sha3 = "0.8.0" +rand_core = { version = "0.5", features = ["getrandom"]} +sha2 = "0.9" wedpr_l_crypto_hash_keccak256 = "1.1.0" wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/utils/src/lib.rs b/crypto/zkp/utils/src/lib.rs index 3d27e8a..6fbdd31 100644 --- a/crypto/zkp/utils/src/lib.rs +++ b/crypto/zkp/utils/src/lib.rs @@ -19,7 +19,9 @@ extern crate lazy_static; mod config; use config::HASH; use rand::Rng; -use sha3::Sha3_512; +use rand_core::OsRng; +// use sha3::Sha3_512; +use sha2::Sha512; use std::convert::TryInto; use wedpr_l_utils::{error::WedprError, traits::Hash}; @@ -28,7 +30,7 @@ lazy_static! { pub static ref BASEPOINT_G1: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; /// Another base point used by various crypto algorithms. pub static ref BASEPOINT_G2: RistrettoPoint = - RistrettoPoint::hash_from_bytes::( + RistrettoPoint::hash_from_bytes::( RISTRETTO_BASEPOINT_COMPRESSED.as_bytes() ); } @@ -38,7 +40,9 @@ const RISTRETTO_POINT_SIZE_IN_BYTES: usize = 32; /// Gets a random Scalar. pub fn get_random_scalar() -> Scalar { - Scalar::random(&mut rand::thread_rng()) + let mut csprng = OsRng; + Scalar::random(&mut csprng) + // Scalar::random(&mut rand::thread_rng()) } /// Converts an arbitrary string to Scalar. From d5dde3f36f64f1b0c7a707b0b23ef3f1402ca016 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 29 Jun 2021 16:39:07 +0800 Subject: [PATCH 09/17] add base_ot_mod/benches --- Cargo.toml | 3 +- crypto/oblivious_transfer/base_ot/Cargo.toml | 3 +- crypto/oblivious_transfer/base_ot/src/lib.rs | 1 + .../base_ot/src/one_out_of_n.rs | 6 ++ .../oblivious_transfer/base_ot/src/utils.rs | 20 +++++-- .../oblivious_transfer/base_ot_mod/Cargo.toml | 7 +++ .../base_ot_mod/benches/base_ot.rs | 58 +++++++++++++++++++ .../base_ot_mod/src/constant.rs | 6 +- .../oblivious_transfer/base_ot_mod/src/lib.rs | 7 ++- .../base_ot_mod/src/utils.rs | 6 ++ crypto/oblivious_transfer/kkrt/Cargo.toml | 11 ++++ crypto/oblivious_transfer/kkrt/src/lib.rs | 8 +++ .../oblivious_transfer/kkrt/src/m_out_of_n.rs | 3 + 13 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs create mode 100644 crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs create mode 100644 crypto/oblivious_transfer/base_ot_mod/src/utils.rs create mode 100644 crypto/oblivious_transfer/kkrt/Cargo.toml create mode 100644 crypto/oblivious_transfer/kkrt/src/lib.rs create mode 100644 crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs diff --git a/Cargo.toml b/Cargo.toml index 6038f91..9abc82e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,8 @@ members = [ "crypto/hash/sha3", "crypto/hash/sm3", "crypto/oblivious_transfer/base_ot", - "crypto/oblivious_transfer/base_ot_mod", + "crypto/oblivious_transfer/kkrt", +# "crypto/oblivious_transfer/base_ot_mod", "crypto/signature/ed25519", "crypto/signature/secp256k1", "crypto/signature/sm2", diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml index 96e9b12..88d9df0 100644 --- a/crypto/oblivious_transfer/base_ot/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "base_ot" +name = "wedpr_l_crypto_ot_base_ot" version = "0.1.0" edition = "2018" @@ -14,6 +14,7 @@ wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" sha2 = "0.9" rand = "0.6" +rand_core = { version = "0.5", features = ["getrandom"]} #rayon = "1.5" [dev-dependencies] diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index 216b624..d6c2faf 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -18,6 +18,7 @@ use wedpr_l_utils::{error::WedprError, traits::Hash}; // use rayon::prelude::*; pub mod one_out_of_two; +// pub mod one_out_of_n; // pub mod utils; lazy_static! { diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs new file mode 100644 index 0000000..7ff808c --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs @@ -0,0 +1,6 @@ +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use wedpr_l_crypto_zkp_utils::{ + get_random_scalar, point_to_bytes, BASEPOINT_G1, +}; \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot/src/utils.rs b/crypto/oblivious_transfer/base_ot/src/utils.rs index 0869954..5911591 100644 --- a/crypto/oblivious_transfer/base_ot/src/utils.rs +++ b/crypto/oblivious_transfer/base_ot/src/utils.rs @@ -1,7 +1,15 @@ -// use curve25519_dalek::scalar::Scalar; -// -// pub fn get_random_scalar() -> Scalar { -// Scalar::random(&mut rand::thread_rng()) -// } -// +use curve25519_dalek::scalar::Scalar; +use rand_core::OsRng; + +lazy_static! { + /// A base point used by various crypto algorithms. + pub static ref BASEPOINT_G1: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; +} + +pub fn get_random_scalar() -> Scalar { + let mut csprng = OsRng; + Scalar::random(&mut csprng) + // Scalar::random(&mut rand::thread_rng()) +} + diff --git a/crypto/oblivious_transfer/base_ot_mod/Cargo.toml b/crypto/oblivious_transfer/base_ot_mod/Cargo.toml index 5de11df..b6345cf 100644 --- a/crypto/oblivious_transfer/base_ot_mod/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot_mod/Cargo.toml @@ -10,3 +10,10 @@ num-integer = "0.1.44" rand = "0.8" num-bigint = { version = "0.4", features = ["rand"] } lazy_static = "1.4.0" + +[dev-dependencies] +criterion = "0.3" + +[[bench]] +name = "base_ot" +harness = false \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs new file mode 100644 index 0000000..4d5ce4c --- /dev/null +++ b/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs @@ -0,0 +1,58 @@ +#[macro_use] +extern crate criterion; +use num_bigint::BigUint; +use criterion::Criterion; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use base_ot_mod::utils::get_random_biguint; +use base_ot_mod::constant::{G_GENERATOR, N_MOD}; +use std::ops::Add; +use num_integer::Integer; + +fn create_base_ot_mod(c: &mut Criterion) { + let label = + format!("create_base_ot_mod"); + let random_number = get_random_biguint(); + + c.bench_function(&label, move |b| { + b.iter(|| { + let _ = G_GENERATOR.modpow(&random_number, &N_MOD); + }) + }); +} + +fn create_base_ot_mul(c: &mut Criterion) { + let label = + format!("create_base_ot_mul"); + let random_number1 = get_random_biguint(); + let random_number2 = get_random_biguint(); + + c.bench_function(&label, move |b| { + b.iter(|| { + // let _ = (random_number1 * random_number2) % *N_MOD; + }) + }); +} +// +// fn create_base_ot_add(c: &mut Criterion) { +// let label = +// format!("create_base_ot_add"); +// let random_number1 = get_random_biguint(); +// let random_number2 = get_random_biguint(); +// +// c.bench_function(&label, move |b| { +// b.iter(|| { +// let _ = random_number1.add(&random_number2).mod_floor(&N_MOD); +// }) +// }); +// } + +criterion_group! { + name = init_base_ot_test; + config = Criterion::default().sample_size(10); +targets = + // create_base_ot_add, + // create_base_ot_mul, + create_base_ot_mod, +} + +criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot_mod/src/constant.rs b/crypto/oblivious_transfer/base_ot_mod/src/constant.rs index d780744..e24db92 100644 --- a/crypto/oblivious_transfer/base_ot_mod/src/constant.rs +++ b/crypto/oblivious_transfer/base_ot_mod/src/constant.rs @@ -1,9 +1,9 @@ use num_bigint::BigUint; +use std::str::FromStr; lazy_static! { /// A base mod big number used by various crypto algorithms. - pub static ref BASE_MOD: BigUint = ; - + pub static ref N_MOD: BigUint = BigUint::from_str("22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427").unwrap(); + pub static ref G_GENERATOR: BigUint = BigUint::from_str("21967776453576425418401845975004568131328756948517171056887978357803037829568879921671582562682834869268458693220178940296762093275708539056500850220701423436253549516692322294566575394724161597988209569075339356219938032314547111217962796852472235866809694733568073434189840033571185928562157743032806061986802973509384288857210981073943845944525063097134620867285393169672223797738231250393852177901783342394915102874707020281747941442896665787806091821561888468560319383163287935077324049516667817847046679134650369734057189968145858211980242981359451590399925956110049309589712838467090785843798902998395786695163").unwrap(); } -22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427 \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot_mod/src/lib.rs b/crypto/oblivious_transfer/base_ot_mod/src/lib.rs index e35ed16..17aad66 100644 --- a/crypto/oblivious_transfer/base_ot_mod/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot_mod/src/lib.rs @@ -1,7 +1,8 @@ #[macro_use] extern crate lazy_static; -// pub mod constant; +pub mod constant; +pub mod utils; #[cfg(test)] mod tests { @@ -15,5 +16,9 @@ mod tests { let prime_gen = BigUint::from_str("21967776453576425418401845975004568131328756948517171056887978357803037829568879921671582562682834869268458693220178940296762093275708539056500850220701423436253549516692322294566575394724161597988209569075339356219938032314547111217962796852472235866809694733568073434189840033571185928562157743032806061986802973509384288857210981073943845944525063097134620867285393169672223797738231250393852177901783342394915102874707020281747941442896665787806091821561888468560319383163287935077324049516667817847046679134650369734057189968145858211980242981359451590399925956110049309589712838467090785843798902998395786695163").unwrap(); println!("prime_mod = {:?}", prime_mod); println!("prime_gen = {:?}", prime_gen); + let random_biguint = utils::get_random_biguint(); + println!("random_biguint = {:?}", random_biguint); + + } } diff --git a/crypto/oblivious_transfer/base_ot_mod/src/utils.rs b/crypto/oblivious_transfer/base_ot_mod/src/utils.rs new file mode 100644 index 0000000..ede3b4b --- /dev/null +++ b/crypto/oblivious_transfer/base_ot_mod/src/utils.rs @@ -0,0 +1,6 @@ +use num_bigint::{BigUint, RandBigInt}; +use rand; + +pub fn get_random_biguint() -> BigUint { + rand::thread_rng().gen_biguint(2047) +} \ No newline at end of file diff --git a/crypto/oblivious_transfer/kkrt/Cargo.toml b/crypto/oblivious_transfer/kkrt/Cargo.toml new file mode 100644 index 0000000..f0428c0 --- /dev/null +++ b/crypto/oblivious_transfer/kkrt/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "kkrt" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +wedpr_l_crypto_ot_base_ot = { path = "../base_ot"} +curve25519-dalek = { version = "3", features = [ "serde" ] } +wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } diff --git a/crypto/oblivious_transfer/kkrt/src/lib.rs b/crypto/oblivious_transfer/kkrt/src/lib.rs new file mode 100644 index 0000000..257ce45 --- /dev/null +++ b/crypto/oblivious_transfer/kkrt/src/lib.rs @@ -0,0 +1,8 @@ +pub mod m_out_of_n; +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs b/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs new file mode 100644 index 0000000..02b34ca --- /dev/null +++ b/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs @@ -0,0 +1,3 @@ +use curve25519_dalek::ristretto::RistrettoPoint; + +pub fn receiver_init_matrix(k_size: u64, n_size: u64) -> Vec> \ No newline at end of file From 8c4aa81d249b253ac708bb024ddf0de2417e3760 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 30 Aug 2021 11:09:52 +0800 Subject: [PATCH 10/17] update utils version --- Cargo.toml | 4 ++-- crypto/zkp/discrete_logarithm_proof/Cargo.toml | 2 +- crypto/zkp/range_proof/Cargo.toml | 2 +- crypto/zkp/utils/Cargo.toml | 5 +++-- crypto/zkp/utils/src/lib.rs | 18 +++++++++++------- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9abc82e..2fbd058 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,8 @@ members = [ "crypto/hash/ripemd160", "crypto/hash/sha3", "crypto/hash/sm3", - "crypto/oblivious_transfer/base_ot", - "crypto/oblivious_transfer/kkrt", +# "crypto/oblivious_transfer/base_ot", +# "crypto/oblivious_transfer/kkrt", # "crypto/oblivious_transfer/base_ot_mod", "crypto/signature/ed25519", "crypto/signature/secp256k1", diff --git a/crypto/zkp/discrete_logarithm_proof/Cargo.toml b/crypto/zkp/discrete_logarithm_proof/Cargo.toml index 8c55f9f..c82108c 100644 --- a/crypto/zkp/discrete_logarithm_proof/Cargo.toml +++ b/crypto/zkp/discrete_logarithm_proof/Cargo.toml @@ -10,7 +10,7 @@ description = "Library of WeDPR shared zkp Function implement discrete logarithm [dependencies] curve25519-dalek = { version = "1", features = [ "serde" ] } -wedpr_l_crypto_zkp_utils = "1.1.0" +wedpr_l_crypto_zkp_utils = { path = "../utils"} wedpr_l_macros = "1.0.0" wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/range_proof/Cargo.toml b/crypto/zkp/range_proof/Cargo.toml index 5863502..8313390 100644 --- a/crypto/zkp/range_proof/Cargo.toml +++ b/crypto/zkp/range_proof/Cargo.toml @@ -12,6 +12,6 @@ description = "Library of WeDPR shared zkp Function implement range proof." bulletproofs = { git = "https://github.com/WeDPR-Team/bulletproofs.git" } curve25519-dalek = { version = "1", features = [ "serde" ] } merlin = "1" -wedpr_l_crypto_zkp_utils = "1.1.0" +wedpr_l_crypto_zkp_utils = { path = "../utils"} wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/utils/Cargo.toml b/crypto/zkp/utils/Cargo.toml index 4305d49..0ba812e 100644 --- a/crypto/zkp/utils/Cargo.toml +++ b/crypto/zkp/utils/Cargo.toml @@ -9,11 +9,12 @@ description = "Library of WeDPR shared zkp function utils." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -curve25519-dalek = { version = "3", features = [ "serde" ] } +curve25519-dalek = { version = "1", features = [ "serde" ] } lazy_static = "1.4.0" rand = "0.6" rand_core = { version = "0.5", features = ["getrandom"]} -sha2 = "0.9" +#sha2 = "0.9" +sha3 = "0.8" wedpr_l_crypto_hash_keccak256 = "1.1.0" wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/utils/src/lib.rs b/crypto/zkp/utils/src/lib.rs index 6fbdd31..7e6eaf3 100644 --- a/crypto/zkp/utils/src/lib.rs +++ b/crypto/zkp/utils/src/lib.rs @@ -19,9 +19,9 @@ extern crate lazy_static; mod config; use config::HASH; use rand::Rng; -use rand_core::OsRng; -// use sha3::Sha3_512; -use sha2::Sha512; +// use rand_core::OsRng; +use sha3::Sha3_512; +// use sha2::Sha512; use std::convert::TryInto; use wedpr_l_utils::{error::WedprError, traits::Hash}; @@ -29,8 +29,12 @@ lazy_static! { /// A base point used by various crypto algorithms. pub static ref BASEPOINT_G1: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; /// Another base point used by various crypto algorithms. + // pub static ref BASEPOINT_G2: RistrettoPoint = + // RistrettoPoint::hash_from_bytes::( + // RISTRETTO_BASEPOINT_COMPRESSED.as_bytes() + // ); pub static ref BASEPOINT_G2: RistrettoPoint = - RistrettoPoint::hash_from_bytes::( + RistrettoPoint::hash_from_bytes::( RISTRETTO_BASEPOINT_COMPRESSED.as_bytes() ); } @@ -40,9 +44,9 @@ const RISTRETTO_POINT_SIZE_IN_BYTES: usize = 32; /// Gets a random Scalar. pub fn get_random_scalar() -> Scalar { - let mut csprng = OsRng; - Scalar::random(&mut csprng) - // Scalar::random(&mut rand::thread_rng()) + // let mut csprng = OsRng; + // Scalar::random(&mut csprng) + Scalar::random(&mut rand::thread_rng()) } /// Converts an arbitrary string to Scalar. From 26f3136755b105d8f61ef86df1c28979890d5888 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 6 Sep 2021 11:03:41 +0800 Subject: [PATCH 11/17] remove useless ot --- Cargo.toml | 4 +- crypto/oblivious_transfer/base_ot/Cargo.toml | 5 +- crypto/oblivious_transfer/base_ot/src/lib.rs | 16 +++-- .../base_ot/src/one_out_of_n.rs | 6 -- .../oblivious_transfer/base_ot/src/utils.rs | 15 ----- .../oblivious_transfer/base_ot_mod/Cargo.toml | 19 ------ .../base_ot_mod/benches/base_ot.rs | 58 ------------------- .../base_ot_mod/src/constant.rs | 9 --- .../oblivious_transfer/base_ot_mod/src/lib.rs | 24 -------- .../base_ot_mod/src/utils.rs | 6 -- crypto/oblivious_transfer/kkrt/Cargo.toml | 11 ---- crypto/oblivious_transfer/kkrt/src/lib.rs | 8 --- .../oblivious_transfer/kkrt/src/m_out_of_n.rs | 3 - 13 files changed, 10 insertions(+), 174 deletions(-) delete mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs delete mode 100644 crypto/oblivious_transfer/base_ot/src/utils.rs delete mode 100644 crypto/oblivious_transfer/base_ot_mod/Cargo.toml delete mode 100644 crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs delete mode 100644 crypto/oblivious_transfer/base_ot_mod/src/constant.rs delete mode 100644 crypto/oblivious_transfer/base_ot_mod/src/lib.rs delete mode 100644 crypto/oblivious_transfer/base_ot_mod/src/utils.rs delete mode 100644 crypto/oblivious_transfer/kkrt/Cargo.toml delete mode 100644 crypto/oblivious_transfer/kkrt/src/lib.rs delete mode 100644 crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs diff --git a/Cargo.toml b/Cargo.toml index 2fbd058..6a3a24e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,7 @@ members = [ "crypto/hash/ripemd160", "crypto/hash/sha3", "crypto/hash/sm3", -# "crypto/oblivious_transfer/base_ot", -# "crypto/oblivious_transfer/kkrt", -# "crypto/oblivious_transfer/base_ot_mod", + "crypto/oblivious_transfer/base_ot", "crypto/signature/ed25519", "crypto/signature/secp256k1", "crypto/signature/sm2", diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml index 88d9df0..1a61e3f 100644 --- a/crypto/oblivious_transfer/base_ot/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -6,16 +6,15 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -curve25519-dalek = { version = "3", features = [ "serde" ] } +curve25519-dalek = { version = "1", features = [ "serde" ] } wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } wedpr_l_crypto_hash_sha3 = { version = "1.0.0" } lazy_static = "1.4.0" wedpr_l_protos = { path = "../../../protos"} wedpr_l_utils = "1.1.0" -sha2 = "0.9" +sha3 = "0.8" rand = "0.6" rand_core = { version = "0.5", features = ["getrandom"]} -#rayon = "1.5" [dev-dependencies] criterion = "0.3" diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index d6c2faf..a2c0192 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -4,7 +4,7 @@ extern crate lazy_static; use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; -use sha2::Sha512; +use sha3::Sha3_512; use wedpr_l_crypto_hash_sha3::WedprSha3_256; use wedpr_l_crypto_zkp_utils::{ bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, @@ -12,14 +12,11 @@ use wedpr_l_crypto_zkp_utils::{ }; use wedpr_l_protos::generated::ot::{ IdList, ReceiverPublic, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, - SenderDataPair, SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, + SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, }; use wedpr_l_utils::{error::WedprError, traits::Hash}; -// use rayon::prelude::*; pub mod one_out_of_two; -// pub mod one_out_of_n; -// pub mod utils; lazy_static! { pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); @@ -39,7 +36,7 @@ pub fn receiver_init_k_out_of_n( r_public.set_point_x(point_to_bytes(&point_x)); r_public.set_point_y(point_to_bytes(&point_y)); for id in id_list.get_id() { - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ *BASEPOINT_G1, ]); @@ -75,7 +72,7 @@ pub fn sender_init_k_out_of_n( ]); let message = data_pair.get_message(); let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); pair.set_figure_print(HASH_SHA3_256.hash(message)); pair.set_point_w(point_to_bytes(&point_w)); for k_point_z in r_public.get_point_z() { @@ -139,7 +136,7 @@ pub fn receiver_decrypt_k_out_of_n( } pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let blinding_a = get_random_scalar(); let blinding_b = get_random_scalar(); let c_id = blinding_a * blinding_b; @@ -184,7 +181,7 @@ pub fn sender_init( ]); let message = data_pair.get_message(); let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); + let id_scalar = Scalar::hash_from_bytes::(id); let point_key = RistrettoPoint::multiscalar_mul( &[blinding_s, blinding_s * id_scalar, blinding_r], &[point_z, *BASEPOINT_G1, point_y], @@ -246,6 +243,7 @@ pub fn receiver_decrypt( #[cfg(test)] mod tests { use super::*; + use wedpr_l_protos::generated::ot::SenderDataPair; #[test] fn test_base_ot_k_out_of_n() { diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs deleted file mode 100644 index 7ff808c..0000000 --- a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs +++ /dev/null @@ -1,6 +0,0 @@ -use curve25519_dalek::{ - ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, -}; -use wedpr_l_crypto_zkp_utils::{ - get_random_scalar, point_to_bytes, BASEPOINT_G1, -}; \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot/src/utils.rs b/crypto/oblivious_transfer/base_ot/src/utils.rs deleted file mode 100644 index 5911591..0000000 --- a/crypto/oblivious_transfer/base_ot/src/utils.rs +++ /dev/null @@ -1,15 +0,0 @@ -use curve25519_dalek::scalar::Scalar; -use rand_core::OsRng; - -lazy_static! { - /// A base point used by various crypto algorithms. - pub static ref BASEPOINT_G1: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; -} - -pub fn get_random_scalar() -> Scalar { - let mut csprng = OsRng; - Scalar::random(&mut csprng) - // Scalar::random(&mut rand::thread_rng()) -} - - diff --git a/crypto/oblivious_transfer/base_ot_mod/Cargo.toml b/crypto/oblivious_transfer/base_ot_mod/Cargo.toml deleted file mode 100644 index b6345cf..0000000 --- a/crypto/oblivious_transfer/base_ot_mod/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "base_ot_mod" -version = "0.1.0" -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -num-integer = "0.1.44" -rand = "0.8" -num-bigint = { version = "0.4", features = ["rand"] } -lazy_static = "1.4.0" - -[dev-dependencies] -criterion = "0.3" - -[[bench]] -name = "base_ot" -harness = false \ No newline at end of file diff --git a/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs deleted file mode 100644 index 4d5ce4c..0000000 --- a/crypto/oblivious_transfer/base_ot_mod/benches/base_ot.rs +++ /dev/null @@ -1,58 +0,0 @@ -#[macro_use] -extern crate criterion; -use num_bigint::BigUint; -use criterion::Criterion; -use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use base_ot_mod::utils::get_random_biguint; -use base_ot_mod::constant::{G_GENERATOR, N_MOD}; -use std::ops::Add; -use num_integer::Integer; - -fn create_base_ot_mod(c: &mut Criterion) { - let label = - format!("create_base_ot_mod"); - let random_number = get_random_biguint(); - - c.bench_function(&label, move |b| { - b.iter(|| { - let _ = G_GENERATOR.modpow(&random_number, &N_MOD); - }) - }); -} - -fn create_base_ot_mul(c: &mut Criterion) { - let label = - format!("create_base_ot_mul"); - let random_number1 = get_random_biguint(); - let random_number2 = get_random_biguint(); - - c.bench_function(&label, move |b| { - b.iter(|| { - // let _ = (random_number1 * random_number2) % *N_MOD; - }) - }); -} -// -// fn create_base_ot_add(c: &mut Criterion) { -// let label = -// format!("create_base_ot_add"); -// let random_number1 = get_random_biguint(); -// let random_number2 = get_random_biguint(); -// -// c.bench_function(&label, move |b| { -// b.iter(|| { -// let _ = random_number1.add(&random_number2).mod_floor(&N_MOD); -// }) -// }); -// } - -criterion_group! { - name = init_base_ot_test; - config = Criterion::default().sample_size(10); -targets = - // create_base_ot_add, - // create_base_ot_mul, - create_base_ot_mod, -} - -criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot_mod/src/constant.rs b/crypto/oblivious_transfer/base_ot_mod/src/constant.rs deleted file mode 100644 index e24db92..0000000 --- a/crypto/oblivious_transfer/base_ot_mod/src/constant.rs +++ /dev/null @@ -1,9 +0,0 @@ -use num_bigint::BigUint; -use std::str::FromStr; - -lazy_static! { - /// A base mod big number used by various crypto algorithms. - pub static ref N_MOD: BigUint = BigUint::from_str("22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427").unwrap(); - pub static ref G_GENERATOR: BigUint = BigUint::from_str("21967776453576425418401845975004568131328756948517171056887978357803037829568879921671582562682834869268458693220178940296762093275708539056500850220701423436253549516692322294566575394724161597988209569075339356219938032314547111217962796852472235866809694733568073434189840033571185928562157743032806061986802973509384288857210981073943845944525063097134620867285393169672223797738231250393852177901783342394915102874707020281747941442896665787806091821561888468560319383163287935077324049516667817847046679134650369734057189968145858211980242981359451590399925956110049309589712838467090785843798902998395786695163").unwrap(); -} - diff --git a/crypto/oblivious_transfer/base_ot_mod/src/lib.rs b/crypto/oblivious_transfer/base_ot_mod/src/lib.rs deleted file mode 100644 index 17aad66..0000000 --- a/crypto/oblivious_transfer/base_ot_mod/src/lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -#[macro_use] -extern crate lazy_static; - -pub mod constant; -pub mod utils; - -#[cfg(test)] -mod tests { - use super::*; - use num_bigint::BigUint; - use std::str::FromStr; - - #[test] - fn it_works() { - let prime_mod = BigUint::from_str("22066536338414323739865000721479941688019446300001727050033863123404508527360057382124431592487175552430214339019932977294438440994766853956747138992782925456829948399227941849596640104139709789897368750691388960480472054706025487529749292052027891171033028716913786485193581436348261797311173683194607566365759850535255366649852649228282281522574257602608653522735179675714896962913724066743735611355498831010057144313259386573625020235737194132968653752006026401256180969976174021974937728920791787186442242767314122749675344165399072583396362702154987714935195101739518213021236819473535057784900656398711233040427").unwrap(); - let prime_gen = BigUint::from_str("21967776453576425418401845975004568131328756948517171056887978357803037829568879921671582562682834869268458693220178940296762093275708539056500850220701423436253549516692322294566575394724161597988209569075339356219938032314547111217962796852472235866809694733568073434189840033571185928562157743032806061986802973509384288857210981073943845944525063097134620867285393169672223797738231250393852177901783342394915102874707020281747941442896665787806091821561888468560319383163287935077324049516667817847046679134650369734057189968145858211980242981359451590399925956110049309589712838467090785843798902998395786695163").unwrap(); - println!("prime_mod = {:?}", prime_mod); - println!("prime_gen = {:?}", prime_gen); - let random_biguint = utils::get_random_biguint(); - println!("random_biguint = {:?}", random_biguint); - - - } -} diff --git a/crypto/oblivious_transfer/base_ot_mod/src/utils.rs b/crypto/oblivious_transfer/base_ot_mod/src/utils.rs deleted file mode 100644 index ede3b4b..0000000 --- a/crypto/oblivious_transfer/base_ot_mod/src/utils.rs +++ /dev/null @@ -1,6 +0,0 @@ -use num_bigint::{BigUint, RandBigInt}; -use rand; - -pub fn get_random_biguint() -> BigUint { - rand::thread_rng().gen_biguint(2047) -} \ No newline at end of file diff --git a/crypto/oblivious_transfer/kkrt/Cargo.toml b/crypto/oblivious_transfer/kkrt/Cargo.toml deleted file mode 100644 index f0428c0..0000000 --- a/crypto/oblivious_transfer/kkrt/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "kkrt" -version = "0.1.0" -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -wedpr_l_crypto_ot_base_ot = { path = "../base_ot"} -curve25519-dalek = { version = "3", features = [ "serde" ] } -wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } diff --git a/crypto/oblivious_transfer/kkrt/src/lib.rs b/crypto/oblivious_transfer/kkrt/src/lib.rs deleted file mode 100644 index 257ce45..0000000 --- a/crypto/oblivious_transfer/kkrt/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod m_out_of_n; -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} diff --git a/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs b/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs deleted file mode 100644 index 02b34ca..0000000 --- a/crypto/oblivious_transfer/kkrt/src/m_out_of_n.rs +++ /dev/null @@ -1,3 +0,0 @@ -use curve25519_dalek::ristretto::RistrettoPoint; - -pub fn receiver_init_matrix(k_size: u64, n_size: u64) -> Vec> \ No newline at end of file From ef4dce26e72d33da00ee3cee8db1e06cb1a8f2e5 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 7 Sep 2021 10:55:44 +0800 Subject: [PATCH 12/17] add ot copyright --- .../base_ot/src/k_out_of_n.rs | 355 +++++++++++++++++ crypto/oblivious_transfer/base_ot/src/lib.rs | 357 +----------------- .../base_ot/src/one_out_of_two.rs | 4 + 3 files changed, 364 insertions(+), 352 deletions(-) create mode 100644 crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs diff --git a/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs new file mode 100644 index 0000000..db4298f --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs @@ -0,0 +1,355 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! K/N Oblivious transfer (OT) functions. + +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use sha3::Sha3_512; +use wedpr_l_crypto_hash_sha3::WedprSha3_256; +use wedpr_l_crypto_zkp_utils::{ + bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, + scalar_to_bytes, BASEPOINT_G1, +}; +use wedpr_l_protos::generated::ot::{ + IdList, ReceiverPublic, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, + SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, +}; +use wedpr_l_utils::{error::WedprError, traits::Hash}; + +lazy_static! { + pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); +} + +pub fn receiver_init_k_out_of_n( + id_list: &IdList, +) -> (ReceiverSecret, ReceiverPublicKOutOfN) { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let mut r_public = ReceiverPublicKOutOfN::default(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + r_public.set_point_x(point_to_bytes(&point_x)); + r_public.set_point_y(point_to_bytes(&point_y)); + for id in id_list.get_id() { + let id_scalar = Scalar::hash_from_bytes::(id); + let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ + *BASEPOINT_G1, + ]); + r_public.mut_point_z().push(point_to_bytes(&point_z)); + } + ( + ReceiverSecret { + scalar_a: scalar_to_bytes(&blinding_a), + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + r_public, + ) +} + +pub fn sender_init_k_out_of_n( + data: &SenderData, + r_public: &ReceiverPublicKOutOfN, +) -> Result { + let mut sender_public = SenderPublic::default(); + let point_x = bytes_to_point(r_public.get_point_x())?; + let point_y = bytes_to_point(r_public.get_point_y())?; + + for data_pair in data.get_pair() { + let mut pair = SenderPublicPairKOutOfN::default(); + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let point_w = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + pair.set_figure_print(HASH_SHA3_256.hash(message)); + pair.set_point_w(point_to_bytes(&point_w)); + for k_point_z in r_public.get_point_z() { + let point_z = bytes_to_point(k_point_z)?; + let point_key = RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + ); + let mut bytes_key = point_to_bytes(&point_key); + while message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let encrypt_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + pair.mut_encrypt_message().push(encrypt_message); + } + sender_public.mut_pairKN().push(pair) + } + Ok(sender_public) +} + +pub fn receiver_decrypt_k_out_of_n( + secret: &ReceiverSecret, + sender_public: &SenderPublic, +) -> Result>, WedprError> { + let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; + let mut result: Vec> = vec![]; + let mut skip_vector: Vec<&[u8]> = vec![]; + for pair in sender_public.get_pairKN() { + let point_w = bytes_to_point(pair.get_point_w())?; + if skip_vector.contains(&pair.get_figure_print()) { + continue; + } + let point_key = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); + let mut bytes_key = point_to_bytes(&point_key); + for encrypt_message in pair.get_encrypt_message() { + while encrypt_message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let decrypt_message: Vec = encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() + { + result.push(decrypt_message); + skip_vector.push(pair.get_figure_print()); + } + } + } + Ok(result) +} + +pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { + let id_scalar = Scalar::hash_from_bytes::(id); + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + let point_z = + RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); + ( + ReceiverSecret { + scalar_a: scalar_to_bytes(&blinding_a), + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ReceiverPublic { + point_x: point_to_bytes(&point_x), + point_y: point_to_bytes(&point_y), + point_z: point_to_bytes(&point_z), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ) +} + +pub fn sender_init( + data: &SenderData, + r_public: &ReceiverPublic, +) -> Result { + let mut sender_public = SenderPublic::default(); + let point_x = bytes_to_point(r_public.get_point_x())?; + let point_y = bytes_to_point(r_public.get_point_y())?; + let point_z = bytes_to_point(r_public.get_point_z())?; + for data_pair in data.get_pair() { + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let point_w = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + let point_key = RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + ); + let mut bytes_key = point_to_bytes(&point_key); + while message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let encrypt_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + sender_public.mut_pair().push(SenderPublicPair { + figure_print: HASH_SHA3_256.hash(message), + point_w: point_to_bytes(&point_w), + encrypt_message, + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + Ok(sender_public) +} + +pub fn receiver_decrypt( + secret: &ReceiverSecret, + // id: &[u8], + sender_public: &SenderPublic, +) -> Result, WedprError> { + let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; + for pair in sender_public.get_pair() { + // if id != pair.get_id() { + // continue; + // } + let point_w = bytes_to_point(pair.get_point_w())?; + let encrypt_message = pair.get_encrypt_message(); + let point_key = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); + let mut bytes_key = point_to_bytes(&point_key); + while encrypt_message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let decrypt_message: Vec = encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { + return Ok(decrypt_message); + } + } + Err(WedprError::ArgumentError) +} + +#[cfg(test)] +mod tests { + use super::*; + use wedpr_l_protos::generated::ot::SenderDataPair; + + #[test] + fn test_base_ot_k_out_of_n() { + let mut choose_id_pb = IdList::default(); + let mut expect: Vec> = vec![]; + choose_id_pb.mut_id().push("10086".as_bytes().to_vec()); + choose_id_pb.mut_id().push("10010".as_bytes().to_vec()); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ("10000".as_bytes(), "wedpr test1".as_bytes()), + ("10086".as_bytes(), "wedpr test2".as_bytes()), + ("10010".as_bytes(), "wedpr test3".as_bytes()), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init_k_out_of_n(&choose_id_pb); + let s_public = sender_init_k_out_of_n(&sender_data, &r_public).unwrap(); + let message = + receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); + expect.push("wedpr test2".as_bytes().to_vec()); + expect.push("wedpr test3".as_bytes().to_vec()); + assert_eq!(message, expect); + } + + #[test] + fn test_base_ot() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ("10000".as_bytes(), "wedpr test1".as_bytes()), + ("10086".as_bytes(), "wedpr test2".as_bytes()), + ("10010".as_bytes(), "wedpr test3".as_bytes()), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + assert_eq!(message, "wedpr test2".as_bytes()); + } + + #[test] + fn test_base_ot_long() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ( + "10000".as_bytes(), + "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10086".as_bytes(), + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10010".as_bytes(), + "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); + let message_str = String::from_utf8(message.clone()).unwrap(); + // println!("message = {}", message_str); + assert_eq!( + message_str, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .to_string() + ); + assert_eq!( + message, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes() + ); + } +} diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index a2c0192..e424927 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -1,356 +1,9 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! Oblivious transfer (OT) functions. + #[macro_use] extern crate lazy_static; -use curve25519_dalek::{ - ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, -}; -use sha3::Sha3_512; -use wedpr_l_crypto_hash_sha3::WedprSha3_256; -use wedpr_l_crypto_zkp_utils::{ - bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, - scalar_to_bytes, BASEPOINT_G1, -}; -use wedpr_l_protos::generated::ot::{ - IdList, ReceiverPublic, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, - SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, -}; -use wedpr_l_utils::{error::WedprError, traits::Hash}; - +pub mod k_out_of_n; pub mod one_out_of_two; - -lazy_static! { - pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); -} - -pub fn receiver_init_k_out_of_n( - id_list: &IdList, -) -> (ReceiverSecret, ReceiverPublicKOutOfN) { - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let mut r_public = ReceiverPublicKOutOfN::default(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - r_public.set_point_x(point_to_bytes(&point_x)); - r_public.set_point_y(point_to_bytes(&point_y)); - for id in id_list.get_id() { - let id_scalar = Scalar::hash_from_bytes::(id); - let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ - *BASEPOINT_G1, - ]); - r_public.mut_point_z().push(point_to_bytes(&point_z)); - } - ( - ReceiverSecret { - scalar_a: scalar_to_bytes(&blinding_a), - scalar_b: scalar_to_bytes(&blinding_b), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - r_public, - ) -} - -pub fn sender_init_k_out_of_n( - data: &SenderData, - r_public: &ReceiverPublicKOutOfN, -) -> Result { - let mut sender_public = SenderPublic::default(); - let point_x = bytes_to_point(r_public.get_point_x())?; - let point_y = bytes_to_point(r_public.get_point_y())?; - - for data_pair in data.get_pair() { - let mut pair = SenderPublicPairKOutOfN::default(); - let blinding_r = get_random_scalar(); - let blinding_s = get_random_scalar(); - let point_w = - RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ - point_x, - *BASEPOINT_G1, - ]); - let message = data_pair.get_message(); - let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); - pair.set_figure_print(HASH_SHA3_256.hash(message)); - pair.set_point_w(point_to_bytes(&point_w)); - for k_point_z in r_public.get_point_z() { - let point_z = bytes_to_point(k_point_z)?; - let point_key = RistrettoPoint::multiscalar_mul( - &[blinding_s, blinding_s * id_scalar, blinding_r], - &[point_z, *BASEPOINT_G1, point_y], - ); - let mut bytes_key = point_to_bytes(&point_key); - while message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let encrypt_message: Vec = message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - pair.mut_encrypt_message().push(encrypt_message); - } - sender_public.mut_pairKN().push(pair) - } - Ok(sender_public) -} - -pub fn receiver_decrypt_k_out_of_n( - secret: &ReceiverSecret, - sender_public: &SenderPublic, -) -> Result>, WedprError> { - let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - let mut result: Vec> = vec![]; - let mut skip_vector: Vec<&[u8]> = vec![]; - for pair in sender_public.get_pairKN() { - let point_w = bytes_to_point(pair.get_point_w())?; - if skip_vector.contains(&pair.get_figure_print()) { - continue; - } - let point_key = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); - let mut bytes_key = point_to_bytes(&point_key); - for encrypt_message in pair.get_encrypt_message() { - while encrypt_message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let decrypt_message: Vec = encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() - { - result.push(decrypt_message); - skip_vector.push(pair.get_figure_print()); - } - } - } - Ok(result) -} - -pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { - let id_scalar = Scalar::hash_from_bytes::(id); - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - let point_z = - RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); - ( - ReceiverSecret { - scalar_a: scalar_to_bytes(&blinding_a), - scalar_b: scalar_to_bytes(&blinding_b), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ReceiverPublic { - point_x: point_to_bytes(&point_x), - point_y: point_to_bytes(&point_y), - point_z: point_to_bytes(&point_z), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ) -} - -pub fn sender_init( - data: &SenderData, - r_public: &ReceiverPublic, -) -> Result { - let mut sender_public = SenderPublic::default(); - let point_x = bytes_to_point(r_public.get_point_x())?; - let point_y = bytes_to_point(r_public.get_point_y())?; - let point_z = bytes_to_point(r_public.get_point_z())?; - for data_pair in data.get_pair() { - let blinding_r = get_random_scalar(); - let blinding_s = get_random_scalar(); - let point_w = - RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ - point_x, - *BASEPOINT_G1, - ]); - let message = data_pair.get_message(); - let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); - let point_key = RistrettoPoint::multiscalar_mul( - &[blinding_s, blinding_s * id_scalar, blinding_r], - &[point_z, *BASEPOINT_G1, point_y], - ); - let mut bytes_key = point_to_bytes(&point_key); - while message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let encrypt_message: Vec = message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - sender_public.mut_pair().push(SenderPublicPair { - figure_print: HASH_SHA3_256.hash(message), - point_w: point_to_bytes(&point_w), - encrypt_message, - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - Ok(sender_public) -} - -pub fn receiver_decrypt( - secret: &ReceiverSecret, - // id: &[u8], - sender_public: &SenderPublic, -) -> Result, WedprError> { - let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - for pair in sender_public.get_pair() { - // if id != pair.get_id() { - // continue; - // } - let point_w = bytes_to_point(pair.get_point_w())?; - let encrypt_message = pair.get_encrypt_message(); - let point_key = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); - let mut bytes_key = point_to_bytes(&point_key); - while encrypt_message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let decrypt_message: Vec = encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { - return Ok(decrypt_message); - } - } - Err(WedprError::ArgumentError) -} - -#[cfg(test)] -mod tests { - use super::*; - use wedpr_l_protos::generated::ot::SenderDataPair; - - #[test] - fn test_base_ot_k_out_of_n() { - let mut choose_id_pb = IdList::default(); - let mut expect: Vec> = vec![]; - choose_id_pb.mut_id().push("10086".as_bytes().to_vec()); - choose_id_pb.mut_id().push("10010".as_bytes().to_vec()); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ("10000".as_bytes(), "wedpr test1".as_bytes()), - ("10086".as_bytes(), "wedpr test2".as_bytes()), - ("10010".as_bytes(), "wedpr test3".as_bytes()), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init_k_out_of_n(&choose_id_pb); - let s_public = sender_init_k_out_of_n(&sender_data, &r_public).unwrap(); - let message = - receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); - expect.push("wedpr test2".as_bytes().to_vec()); - expect.push("wedpr test3".as_bytes().to_vec()); - assert_eq!(message, expect); - } - - #[test] - fn test_base_ot() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ("10000".as_bytes(), "wedpr test1".as_bytes()), - ("10086".as_bytes(), "wedpr test2".as_bytes()), - ("10010".as_bytes(), "wedpr test3".as_bytes()), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - assert_eq!(message, "wedpr test2".as_bytes()); - } - - #[test] - fn test_base_ot_long() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ( - "10000".as_bytes(), - "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10086".as_bytes(), - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10010".as_bytes(), - "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); - let message_str = String::from_utf8(message.clone()).unwrap(); - // println!("message = {}", message_str); - assert_eq!( - message_str, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .to_string() - ); - assert_eq!( - message, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes() - ); - } -} diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs index cfcbc79..3dc7bb4 100644 --- a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs +++ b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs @@ -1,3 +1,7 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! 1/2 Oblivious transfer (OT) functions. + use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; From b45b6b469215db6ec2c752d1b360eaf4dec5ab3a Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Tue, 7 Sep 2021 14:23:03 +0800 Subject: [PATCH 13/17] update range proofs --- crypto/zkp/range_proof/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/zkp/range_proof/Cargo.toml b/crypto/zkp/range_proof/Cargo.toml index 8313390..03de656 100644 --- a/crypto/zkp/range_proof/Cargo.toml +++ b/crypto/zkp/range_proof/Cargo.toml @@ -9,7 +9,7 @@ description = "Library of WeDPR shared zkp Function implement range proof." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bulletproofs = { git = "https://github.com/WeDPR-Team/bulletproofs.git" } +bulletproofs = { package = "wedpr_l_bulletproofs", version = "1.0.5" } curve25519-dalek = { version = "1", features = [ "serde" ] } merlin = "1" wedpr_l_crypto_zkp_utils = { path = "../utils"} From 2414682ddf5a8d0d20830d8b359676b6e7ae78b5 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 13 Sep 2021 09:40:34 +0800 Subject: [PATCH 14/17] add ot comments --- .github/workflows/default_test.yml | 4 + .../base_ot/benches/base_ot.rs | 18 +- .../base_ot/src/k_out_of_n.rs | 222 +++------------- crypto/oblivious_transfer/base_ot/src/lib.rs | 1 + .../base_ot/src/one_out_of_n.rs | 241 ++++++++++++++++++ .../base_ot/src/one_out_of_two.rs | 20 +- protos/crypto/ot.proto | 10 + 7 files changed, 316 insertions(+), 200 deletions(-) create mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs diff --git a/.github/workflows/default_test.yml b/.github/workflows/default_test.yml index 64ea897..4b2cfd0 100644 --- a/.github/workflows/default_test.yml +++ b/.github/workflows/default_test.yml @@ -13,7 +13,11 @@ jobs: - uses: actions/checkout@v1 - name: Nightly default run: rustup default nightly + - name: Generate proto + run: cd protos && cargo run && cd ../ - name: Build run: cargo build --verbose - name: Run tests run: cargo test --verbose + - name: Run benches + run: cargo bench --verbose diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs index d83b184..edb5a03 100644 --- a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -1,15 +1,18 @@ #[macro_use] extern crate criterion; -use base_ot::{ +use criterion::Criterion; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use wedpr_l_crypto_ot_base_ot::{ + k_out_of_n::{ + receiver_decrypt_k_out_of_n, receiver_init_k_out_of_n, + sender_init_k_out_of_n, + }, + one_out_of_n::{receiver_decrypt, receiver_init, sender_init}, one_out_of_two::{ receiver_decrypt_1_out_of_2, receiver_init_1_out_of_2, sender_init_1_out_of_2, DataOneOutOfTwo, }, - receiver_decrypt, receiver_decrypt_k_out_of_n, receiver_init, - receiver_init_k_out_of_n, sender_init, sender_init_k_out_of_n, }; -use criterion::Criterion; -use rand::{distributions::Alphanumeric, thread_rng, Rng}; use wedpr_l_protos::generated::ot::{IdList, SenderData, SenderDataPair}; fn create_base_ot_1_out_of_2_helper(c: &mut Criterion, str_len: u64) { @@ -162,10 +165,6 @@ fn create_base_ot_30000_10(c: &mut Criterion) { create_base_ot_helper(c, 30000, 10); } -fn create_base_ot_300000_10(c: &mut Criterion) { - create_base_ot_helper(c, 300000, 10); -} - fn create_base_ot_k_out_of_n_1_300_10(c: &mut Criterion) { create_base_ot_k_out_of_n_helper(c, 1, 300, 10); } @@ -207,7 +206,6 @@ create_base_ot_10000_10, create_base_ot_k_out_of_n_15_300_10, create_base_ot_k_out_of_n_30_300_10, create_base_ot_k_out_of_n_60_300_10, - // create_base_ot_300000_10, } criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs index db4298f..1d5e559 100644 --- a/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs +++ b/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs @@ -1,7 +1,10 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. //! K/N Oblivious transfer (OT) functions. - +/// Sender has n data records, the format of each record is (id, message), +/// K/N Oblivious transfer (OT) can help receiver to get k messages using k +/// ids one time but does't disclose receiver's k ids, and help sender keep +/// the other n-1 messages privacy. use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; @@ -12,8 +15,8 @@ use wedpr_l_crypto_zkp_utils::{ scalar_to_bytes, BASEPOINT_G1, }; use wedpr_l_protos::generated::ot::{ - IdList, ReceiverPublic, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, - SenderPublic, SenderPublicPair, SenderPublicPairKOutOfN, + IdList, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, SenderPublic, + SenderPublicPairKOutOfN, }; use wedpr_l_utils::{error::WedprError, traits::Hash}; @@ -21,6 +24,11 @@ lazy_static! { pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); } +// Generates a request based on receiver's id list and private key, where the +// id list contains k messages' id receiver want to inquery, the +// private key used to get the message inquired is kept secretly by the +// receiver, and the request contains three public keys being sent to sender to +// encrypt the his messages. pub fn receiver_init_k_out_of_n( id_list: &IdList, ) -> (ReceiverSecret, ReceiverPublicKOutOfN) { @@ -52,6 +60,17 @@ pub fn receiver_init_k_out_of_n( ) } +// Computes sender's public responce(n pairs data for n messages), where each +// pair contains a symmetric ciphertext, a asymmetric ciphertext and a hash for +// each message(n messages in total). +// - The symmetric ciphertext is computed by encrypting the message using the +// symmetric +// key randomly generated for this message. +// - The asymmetric ciphertext is computed by encrypting the symmetric +// key using three public keys from receiver. +// - The hash is hash of the symmetric key, in order to help the receiver +// identify +// whether a certain message is the message he inquired. pub fn sender_init_k_out_of_n( data: &SenderData, r_public: &ReceiverPublicKOutOfN, @@ -98,6 +117,17 @@ pub fn sender_init_k_out_of_n( Ok(sender_public) } +// Recovers k messages receiver inquired, specifically for each pair data from +// sender, receiver +// step1. uses receiver's private key to decrypt the asymmetric +// ciphertext to obtain the symmetric key. +// step2. +// calculates the hash of the symmetric key and compare it with the received +// hash. If it matches, specify that the corresponding symmetric ciphertext is +// the ciphertext of message inquired, otherwise, perform the calculation of the +// next pair data. +// step3. uses the symmetric key in step1 to decrypt the +// symmetric ciphertext identified in step2 to obtain the message inquired. pub fn receiver_decrypt_k_out_of_n( secret: &ReceiverSecret, sender_public: &SenderPublic, @@ -134,111 +164,6 @@ pub fn receiver_decrypt_k_out_of_n( Ok(result) } -pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { - let id_scalar = Scalar::hash_from_bytes::(id); - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - let point_z = - RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); - ( - ReceiverSecret { - scalar_a: scalar_to_bytes(&blinding_a), - scalar_b: scalar_to_bytes(&blinding_b), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ReceiverPublic { - point_x: point_to_bytes(&point_x), - point_y: point_to_bytes(&point_y), - point_z: point_to_bytes(&point_z), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ) -} - -pub fn sender_init( - data: &SenderData, - r_public: &ReceiverPublic, -) -> Result { - let mut sender_public = SenderPublic::default(); - let point_x = bytes_to_point(r_public.get_point_x())?; - let point_y = bytes_to_point(r_public.get_point_y())?; - let point_z = bytes_to_point(r_public.get_point_z())?; - for data_pair in data.get_pair() { - let blinding_r = get_random_scalar(); - let blinding_s = get_random_scalar(); - let point_w = - RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ - point_x, - *BASEPOINT_G1, - ]); - let message = data_pair.get_message(); - let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); - let point_key = RistrettoPoint::multiscalar_mul( - &[blinding_s, blinding_s * id_scalar, blinding_r], - &[point_z, *BASEPOINT_G1, point_y], - ); - let mut bytes_key = point_to_bytes(&point_key); - while message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let encrypt_message: Vec = message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - sender_public.mut_pair().push(SenderPublicPair { - figure_print: HASH_SHA3_256.hash(message), - point_w: point_to_bytes(&point_w), - encrypt_message, - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - Ok(sender_public) -} - -pub fn receiver_decrypt( - secret: &ReceiverSecret, - // id: &[u8], - sender_public: &SenderPublic, -) -> Result, WedprError> { - let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - for pair in sender_public.get_pair() { - // if id != pair.get_id() { - // continue; - // } - let point_w = bytes_to_point(pair.get_point_w())?; - let encrypt_message = pair.get_encrypt_message(); - let point_key = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); - let mut bytes_key = point_to_bytes(&point_key); - while encrypt_message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let decrypt_message: Vec = encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { - return Ok(decrypt_message); - } - } - Err(WedprError::ArgumentError) -} - #[cfg(test)] mod tests { use super::*; @@ -271,85 +196,4 @@ mod tests { expect.push("wedpr test3".as_bytes().to_vec()); assert_eq!(message, expect); } - - #[test] - fn test_base_ot() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ("10000".as_bytes(), "wedpr test1".as_bytes()), - ("10086".as_bytes(), "wedpr test2".as_bytes()), - ("10010".as_bytes(), "wedpr test3".as_bytes()), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - assert_eq!(message, "wedpr test2".as_bytes()); - } - - #[test] - fn test_base_ot_long() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ( - "10000".as_bytes(), - "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10086".as_bytes(), - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10010".as_bytes(), - "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); - let message_str = String::from_utf8(message.clone()).unwrap(); - // println!("message = {}", message_str); - assert_eq!( - message_str, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .to_string() - ); - assert_eq!( - message, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes() - ); - } } diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index e424927..6f864a8 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -6,4 +6,5 @@ extern crate lazy_static; pub mod k_out_of_n; +pub mod one_out_of_n; pub mod one_out_of_two; diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs new file mode 100644 index 0000000..814ed48 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs @@ -0,0 +1,241 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! 1/N Oblivious transfer (OT) functions. +/// Sender has n data records, the format of each record is (id, message), +/// K/N Oblivious transfer (OT) can help receiver to get one message using +/// a id but does't disclose receiver's id and help sender keep the other +/// n-1 messages privacy. +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use sha3::Sha3_512; +use wedpr_l_crypto_hash_sha3::WedprSha3_256; +use wedpr_l_crypto_zkp_utils::{ + bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, + scalar_to_bytes, BASEPOINT_G1, +}; +use wedpr_l_protos::generated::ot::{ + ReceiverPublic, ReceiverSecret, SenderData, SenderPublic, SenderPublicPair, +}; +use wedpr_l_utils::{error::WedprError, traits::Hash}; + +lazy_static! { + pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); +} + +// Generates the private key and three public keys based on the id receiver +// inquired, where the private key used to get the message inquired is kept +// secretly by the receiver, the public key will be sent to sender to encrypt +// the his messages. +pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { + let id_scalar = Scalar::hash_from_bytes::(id); + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + let point_z = + RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); + ( + ReceiverSecret { + scalar_a: scalar_to_bytes(&blinding_a), + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ReceiverPublic { + point_x: point_to_bytes(&point_x), + point_y: point_to_bytes(&point_y), + point_z: point_to_bytes(&point_z), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + ) +} + +// Computes sender's public responce(n pairs data for n messages), where each +// pair contains a symmetric ciphertext, a asymmetric ciphertext and a hash for +// each message(n messages in total). +// - The symmetric ciphertext is computed by encrypting the message using the +// symmetric +// key randomly generated for this message. +// - The asymmetric ciphertext is computed by encrypting the symmetric +// key using three public keys from receiver. +// - The hash is hash of the symmetric key, in order to help the receiver +// identify +// whether a certain message is the message he inquired. +pub fn sender_init( + data: &SenderData, + r_public: &ReceiverPublic, +) -> Result { + let mut sender_public = SenderPublic::default(); + let point_x = bytes_to_point(r_public.get_point_x())?; + let point_y = bytes_to_point(r_public.get_point_y())?; + let point_z = bytes_to_point(r_public.get_point_z())?; + for data_pair in data.get_pair() { + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let point_w = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + let point_key = RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + ); + let mut bytes_key = point_to_bytes(&point_key); + while message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let encrypt_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + sender_public.mut_pair().push(SenderPublicPair { + figure_print: HASH_SHA3_256.hash(message), + point_w: point_to_bytes(&point_w), + encrypt_message, + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + Ok(sender_public) +} + +// Recovers the message receiver inquired, specifically for each pair data from +// sender, receiver +// step1. uses receiver's private key to decrypt the asymmetric +// ciphertext to obtain the symmetric key. +// step2. calculates the hash of the symmetric key and compare it with the +// received hash. If it matches, specify that the corresponding symmetric +// ciphertext is the ciphertext of message inquired, otherwise, perform the +// calculation of the next pair data. +// step3. uses the symmetric key in step1 to decrypt the +// symmetric ciphertext identified in step2 to obtain the message inquired. +pub fn receiver_decrypt( + secret: &ReceiverSecret, + // id: &[u8], + sender_public: &SenderPublic, +) -> Result, WedprError> { + let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; + for pair in sender_public.get_pair() { + // if id != pair.get_id() { + // continue; + // } + let point_w = bytes_to_point(pair.get_point_w())?; + let encrypt_message = pair.get_encrypt_message(); + let point_key = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); + let mut bytes_key = point_to_bytes(&point_key); + while encrypt_message.len() > bytes_key.len() { + for key_bytes in bytes_key.clone() { + bytes_key.push(key_bytes); + } + } + let decrypt_message: Vec = encrypt_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { + return Ok(decrypt_message); + } + } + Err(WedprError::ArgumentError) +} + +#[cfg(test)] +mod tests { + use super::*; + use wedpr_l_protos::generated::ot::SenderDataPair; + + #[test] + fn test_base_ot_1_out_of_n() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ("10000".as_bytes(), "wedpr test1".as_bytes()), + ("10086".as_bytes(), "wedpr test2".as_bytes()), + ("10010".as_bytes(), "wedpr test3".as_bytes()), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + assert_eq!(message, "wedpr test2".as_bytes()); + } + + #[test] + fn test_base_ot_long() { + let choose_id = "10086".as_bytes(); + let mut sender_data = SenderData::default(); + for (id, message) in vec![ + ( + "10000".as_bytes(), + "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10086".as_bytes(), + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ( + "10010".as_bytes(), + "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、\ + 更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes(), + ), + ] { + sender_data.mut_pair().push(SenderDataPair { + id: id.to_vec(), + message: message.to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + let (r_secret, r_public) = receiver_init(choose_id); + let s_public = sender_init(&sender_data, &r_public).unwrap(); + let message = receiver_decrypt(&r_secret, &s_public).unwrap(); + // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); + let message_str = String::from_utf8(message.clone()).unwrap(); + // println!("message = {}", message_str); + assert_eq!( + message_str, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .to_string() + ); + assert_eq!( + message, + "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ + 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ + WeDPR-Lab就是这一系列开源的核心算法组件的集合" + .as_bytes() + ); + } +} diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs index 3dc7bb4..0726d0e 100644 --- a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs +++ b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs @@ -1,7 +1,10 @@ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. //! 1/2 Oblivious transfer (OT) functions. - +/// Sender has two data records, the format of record is (index, message), +/// index = 0 or 1, 1/2 Oblivious transfer (OT) can help receiver to get +/// the messages he want to query but does't disclose receiver's choice and +/// sender's another message. use curve25519_dalek::{ ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, }; @@ -27,6 +30,12 @@ pub struct EncryptOneOutOfTwo { pub encrypt1: EncryptPairOneOutOfTwo, } +// Generates a query based on receiver's choice, where receiver's +// choice is 0 or 1, separately means the index of sender's two messages. +// Specificly, the query contains a private key and three public keys, the +// private key used to get the message inquired is kept secretly by the +// receiver, the public key will be sent to sender to encrypt the his two +// messages. pub fn receiver_init_1_out_of_2( choice: bool, ) -> (Scalar, RistrettoPoint, RistrettoPoint, RistrettoPoint) { @@ -48,6 +57,9 @@ pub fn receiver_init_1_out_of_2( (blinding_b, point_x, point_y, point_z) } +// Computes two ciphertexts by using three public keys from receiver to +// encrypt his two messages, where the ciphertexts will be sent to receiver to +// decrypt the message he inquired. pub fn sender_init_1_out_of_2( data: &DataOneOutOfTwo, point_x: &RistrettoPoint, @@ -107,6 +119,12 @@ pub fn sender_init_1_out_of_2( } } +// Decrypts the two ciphertexts from sender to get message he inquired. +// Since the public key is calculated using the receiver's choice, and the +// private key has a binding relationship with the public key, the receiver can +// use the private key to decrypt two ciphertexts respectively to get the +// message he chose. For the ciphertext of another message, the receiver can +// only get a string of random numbers after decryption. pub fn receiver_decrypt_1_out_of_2( choice: bool, blinding_b: &Scalar, diff --git a/protos/crypto/ot.proto b/protos/crypto/ot.proto index ff30e98..6218e1f 100644 --- a/protos/crypto/ot.proto +++ b/protos/crypto/ot.proto @@ -7,49 +7,59 @@ option java_package = "com.webank.wedpr.crypto.proto"; option java_multiple_files = true; // ot key pair containing a public key and a private key. + +// Private key of receiver. message ReceiverSecret { bytes scalar_a = 1; bytes scalar_b = 2; } +// Public key of receiver for 1/n ot. message ReceiverPublic { bytes point_x = 1; bytes point_y = 2; bytes point_z = 3; } +// Request of receiver for k/n ot. message ReceiverPublicKOutOfN { bytes point_x = 1; bytes point_y = 2; repeated bytes point_z = 3; } +// Record of sender's messages with one out of n OT. message SenderPublicPair { bytes figure_print = 1; bytes point_w = 2; bytes encrypt_message = 3; } +// Record of sender's messages with k out of n OT. message SenderPublicPairKOutOfN { bytes figure_print = 1; bytes point_w = 2; repeated bytes encrypt_message = 3; } +// Ciphertext of sender's each message. message SenderPublic { repeated SenderPublicPair pair = 1; repeated SenderPublicPairKOutOfN pairKN = 2; } +// Record of sender's message. message SenderDataPair { bytes id = 1; bytes message = 2; } +// Record of sender's each message. message SenderData { repeated SenderDataPair pair = 1; } +// List of ids to be inqueried. message IdList { repeated bytes id = 1; } From d594829c8b19822d61d5a01050a90b976f0f7750 Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Mon, 13 Sep 2021 09:55:54 +0800 Subject: [PATCH 15/17] skip generate protos --- .github/workflows/default_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/default_test.yml b/.github/workflows/default_test.yml index 4b2cfd0..ad96d1b 100644 --- a/.github/workflows/default_test.yml +++ b/.github/workflows/default_test.yml @@ -13,8 +13,8 @@ jobs: - uses: actions/checkout@v1 - name: Nightly default run: rustup default nightly - - name: Generate proto - run: cd protos && cargo run && cd ../ +# - name: Generate proto +# run: cd protos && cargo run && cd ../ - name: Build run: cargo build --verbose - name: Run tests From 0d0f46880f1e1af872527245db57ae9a69722c22 Mon Sep 17 00:00:00 2001 From: qyan-dev Date: Tue, 14 Sep 2021 18:11:09 +0800 Subject: [PATCH 16/17] v1.2 revise --- .github/workflows/default_test.yml | 4 +- .../base_ot/benches/base_ot.rs | 198 +--- .../base_ot/src/k_out_of_n.rs | 199 ---- crypto/oblivious_transfer/base_ot/src/lib.rs | 5 +- .../base_ot/src/one_out_of_n.rs | 241 ---- .../base_ot/src/one_out_of_two.rs | 181 --- .../oblivious_transfer/base_ot/src/ot_kv.rs | 239 ++++ .../base_ot/src/ot_message.rs | 266 +++++ .../discrete_logarithm_proof/benches/dlp.rs | 2 + crypto/zkp/utils/Cargo.toml | 3 +- crypto/zkp/utils/src/lib.rs | 8 - .../ffi_java_fisco_bcos_sdk/src/vrf.rs | 2 +- protos/crypto/ot.proto | 56 +- protos/src/generated/common.rs | 2 + protos/src/generated/ot.rs | 1026 ++++------------- protos/src/generated/zkp.rs | 2 + third_party/fisco_bcos_java_sdk/src/lib.rs | 1 - 17 files changed, 814 insertions(+), 1621 deletions(-) delete mode 100644 crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs delete mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs delete mode 100644 crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs create mode 100644 crypto/oblivious_transfer/base_ot/src/ot_kv.rs create mode 100644 crypto/oblivious_transfer/base_ot/src/ot_message.rs diff --git a/.github/workflows/default_test.yml b/.github/workflows/default_test.yml index ad96d1b..021e1b3 100644 --- a/.github/workflows/default_test.yml +++ b/.github/workflows/default_test.yml @@ -16,8 +16,6 @@ jobs: # - name: Generate proto # run: cd protos && cargo run && cd ../ - name: Build - run: cargo build --verbose + run: cargo build --all --all-targets --verbose - name: Run tests run: cargo test --verbose - - name: Run benches - run: cargo bench --verbose diff --git a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs index edb5a03..df93dca 100644 --- a/crypto/oblivious_transfer/base_ot/benches/base_ot.rs +++ b/crypto/oblivious_transfer/base_ot/benches/base_ot.rs @@ -1,23 +1,20 @@ +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. + #[macro_use] extern crate criterion; use criterion::Criterion; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use wedpr_l_crypto_ot_base_ot::{ - k_out_of_n::{ - receiver_decrypt_k_out_of_n, receiver_init_k_out_of_n, - sender_init_k_out_of_n, - }, - one_out_of_n::{receiver_decrypt, receiver_init, sender_init}, - one_out_of_two::{ - receiver_decrypt_1_out_of_2, receiver_init_1_out_of_2, - sender_init_1_out_of_2, DataOneOutOfTwo, - }, + ot_kv::OtKvKOutOfN, + ot_message::{make_two_ot_messages, OtMessages1OutOf2}, }; -use wedpr_l_protos::generated::ot::{IdList, SenderData, SenderDataPair}; +use wedpr_l_protos::generated::ot::{BytesToBytesPair, DataDict, IdList}; -fn create_base_ot_1_out_of_2_helper(c: &mut Criterion, str_len: u64) { - let label = - format!("create_base_ot_1_out_of_2_helper, str_len = {}", str_len); +fn create_base_ot_message_1_out_of_2_helper(c: &mut Criterion, str_len: u64) { + let label = format!( + "create_base_ot_message_1_out_of_2_helper, str_len = {}", + str_len + ); let random_message0: String = thread_rng() .sample_iter(&Alphanumeric) .take(str_len as usize) @@ -26,43 +23,41 @@ fn create_base_ot_1_out_of_2_helper(c: &mut Criterion, str_len: u64) { .sample_iter(&Alphanumeric) .take(str_len as usize) .collect(); - let data = DataOneOutOfTwo { - data0: random_message0.as_bytes().to_vec(), - data1: random_message1.as_bytes().to_vec(), - }; + let data = make_two_ot_messages(&random_message0, &random_message1); - let choice = false; - let true_message = random_message0.as_bytes().to_vec(); + let choose_zero = true; + let expected_message = random_message0.as_bytes().to_vec(); + let ot = OtMessages1OutOf2::default(); c.bench_function(&label, move |b| { b.iter(|| { - let (blinding_b, point_x, point_y, point_z) = - receiver_init_1_out_of_2(choice); - let sender_public = - sender_init_1_out_of_2(&data, &point_x, &point_y, &point_z); - let decrypt_message = receiver_decrypt_1_out_of_2( - choice, - &blinding_b, - &sender_public, + let (receiver_secret, receiver_commitment) = + ot.receiver_init(choose_zero); + let sender_ciphertexts = + ot.sender_init(&data, &receiver_commitment).unwrap(); + let decrypt_message = ot.receiver_decrypt( + choose_zero, + &receiver_secret, + &sender_ciphertexts, ); - assert_eq!(decrypt_message, true_message); + assert_eq!(decrypt_message, expected_message); }) }); } -fn create_base_ot_k_out_of_n_helper( +fn create_base_ot_kv_k_out_of_n_helper( c: &mut Criterion, k_choose_count: u64, n_message_count: u64, str_len: u64, ) { let label = format!( - "create_base_ot_k_out_of_n_helper, k_choose_count = {}, \ + "create_base_ot_kv_k_out_of_n_helper, k_choose_count = {}, \ n_message_count = {}, str_len = {}", k_choose_count, n_message_count, str_len ); - let mut sender_data = SenderData::default(); - let mut expect: Vec> = vec![]; + let mut data = DataDict::default(); + let mut expected: Vec> = vec![]; for _ in 0..n_message_count { let random_id: String = thread_rng().sample_iter(&Alphanumeric).take(18).collect(); @@ -70,142 +65,63 @@ fn create_base_ot_k_out_of_n_helper( .sample_iter(&Alphanumeric) .take(str_len as usize) .collect(); - sender_data.mut_pair().push(SenderDataPair { + data.mut_pair().push(BytesToBytesPair { id: random_id.as_bytes().to_vec(), message: random_message.as_bytes().to_vec(), unknown_fields: Default::default(), cached_size: Default::default(), }) } - let mut choose_id_list = IdList::default(); + let mut choice_list = IdList::default(); for i in 0..k_choose_count { - choose_id_list - .mut_id() - .push(sender_data.get_pair()[i as usize].get_id().to_vec()); - expect.push(sender_data.get_pair()[i as usize].get_message().to_vec()) + let pair = &data.get_pair()[i as usize]; + choice_list.mut_id().push(pair.get_id().to_vec()); + expected.push(pair.get_message().to_vec()) } - let use_data = sender_data.clone(); - c.bench_function(&label, move |b| { - b.iter(|| { - let (r_secret, r_public) = - receiver_init_k_out_of_n(&choose_id_list); - let s_public = - sender_init_k_out_of_n(&use_data, &r_public).unwrap(); - let message = - receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); - assert_eq!(message, expect); - }) - }); -} + let ot = OtKvKOutOfN::default(); -fn create_base_ot_helper(c: &mut Criterion, message_count: u64, str_len: u64) { - let label = format!( - "create_base_ot_helper, message_count = {}, str_len = {}", - message_count, str_len - ); - let mut sender_data = SenderData::default(); - for _ in 0..message_count { - let random_id: String = - thread_rng().sample_iter(&Alphanumeric).take(18).collect(); - let random_message: String = thread_rng() - .sample_iter(&Alphanumeric) - .take(str_len as usize) - .collect(); - sender_data.mut_pair().push(SenderDataPair { - id: random_id.as_bytes().to_vec(), - message: random_message.as_bytes().to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let use_data = sender_data.clone(); - let choose_id = - sender_data.get_pair()[sender_data.get_pair().len() / 2].get_id(); - let true_message = - sender_data.get_pair()[sender_data.get_pair().len() / 2].get_message(); - // let choose_id = - // sender_data.get_pair()[0].get_id(); - // let true_message = - // sender_data.get_pair()[0].get_message(); c.bench_function(&label, move |b| { b.iter(|| { - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&use_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - assert_eq!(message.as_slice(), true_message); + let (receiver_secret, receiver_commitment) = + ot.receiver_init(&choice_list); + let sender_ciphertexts = + ot.sender_init(&data, &receiver_commitment).unwrap(); + let message = ot + .receiver_decrypt( + &receiver_secret, + &sender_ciphertexts, + k_choose_count as usize, + ) + .unwrap(); + assert_eq!(message, expected); }) }); } -fn create_base_ot_10_10(c: &mut Criterion) { - create_base_ot_helper(c, 10, 10); -} - -fn create_base_ot_100_10(c: &mut Criterion) { - create_base_ot_helper(c, 100, 10); -} - -fn create_base_ot_1000_10(c: &mut Criterion) { - create_base_ot_helper(c, 1000, 10); -} - -fn create_base_ot_10000_10(c: &mut Criterion) { - create_base_ot_helper(c, 10000, 10); -} - -fn create_base_ot_300_10(c: &mut Criterion) { - create_base_ot_helper(c, 300, 10); -} - -fn create_base_ot_3000_10(c: &mut Criterion) { - create_base_ot_helper(c, 3000, 10); -} - -fn create_base_ot_30000_10(c: &mut Criterion) { - create_base_ot_helper(c, 30000, 10); -} - -fn create_base_ot_k_out_of_n_1_300_10(c: &mut Criterion) { - create_base_ot_k_out_of_n_helper(c, 1, 300, 10); -} - -fn create_base_ot_k_out_of_n_3_300_10(c: &mut Criterion) { - create_base_ot_k_out_of_n_helper(c, 3, 300, 10); -} - -fn create_base_ot_k_out_of_n_15_300_10(c: &mut Criterion) { - create_base_ot_k_out_of_n_helper(c, 15, 300, 10); +fn create_base_ot_kv_k_out_of_n_1_300_10(c: &mut Criterion) { + create_base_ot_kv_k_out_of_n_helper(c, 1, 300, 10); } -fn create_base_ot_k_out_of_n_30_300_10(c: &mut Criterion) { - create_base_ot_k_out_of_n_helper(c, 30, 300, 10); +fn create_base_ot_kv_k_out_of_n_3_300_10(c: &mut Criterion) { + create_base_ot_kv_k_out_of_n_helper(c, 3, 300, 10); } -fn create_base_ot_k_out_of_n_60_300_10(c: &mut Criterion) { - create_base_ot_k_out_of_n_helper(c, 60, 300, 10); +fn create_base_ot_kv_k_out_of_n_10_300_10(c: &mut Criterion) { + create_base_ot_kv_k_out_of_n_helper(c, 10, 300, 10); } -fn create_base_ot_1_out_of_2_10(c: &mut Criterion) { - create_base_ot_1_out_of_2_helper(c, 10); +fn create_base_ot_message_1_out_of_2_10(c: &mut Criterion) { + create_base_ot_message_1_out_of_2_helper(c, 10); } criterion_group! { name = init_base_ot_test; config = Criterion::default().sample_size(10); targets = - create_base_ot_1_out_of_2_10, -create_base_ot_10_10, -create_base_ot_100_10, -create_base_ot_1000_10, -create_base_ot_10000_10, - create_base_ot_300_10, - create_base_ot_3000_10, - create_base_ot_30000_10, - create_base_ot_k_out_of_n_1_300_10, - create_base_ot_k_out_of_n_3_300_10, - create_base_ot_k_out_of_n_15_300_10, - create_base_ot_k_out_of_n_30_300_10, - create_base_ot_k_out_of_n_60_300_10, + create_base_ot_message_1_out_of_2_10, + create_base_ot_kv_k_out_of_n_1_300_10, + create_base_ot_kv_k_out_of_n_3_300_10, + create_base_ot_kv_k_out_of_n_10_300_10, } criterion_main!(init_base_ot_test); diff --git a/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs deleted file mode 100644 index 1d5e559..0000000 --- a/crypto/oblivious_transfer/base_ot/src/k_out_of_n.rs +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - -//! K/N Oblivious transfer (OT) functions. -/// Sender has n data records, the format of each record is (id, message), -/// K/N Oblivious transfer (OT) can help receiver to get k messages using k -/// ids one time but does't disclose receiver's k ids, and help sender keep -/// the other n-1 messages privacy. -use curve25519_dalek::{ - ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, -}; -use sha3::Sha3_512; -use wedpr_l_crypto_hash_sha3::WedprSha3_256; -use wedpr_l_crypto_zkp_utils::{ - bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, - scalar_to_bytes, BASEPOINT_G1, -}; -use wedpr_l_protos::generated::ot::{ - IdList, ReceiverPublicKOutOfN, ReceiverSecret, SenderData, SenderPublic, - SenderPublicPairKOutOfN, -}; -use wedpr_l_utils::{error::WedprError, traits::Hash}; - -lazy_static! { - pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); -} - -// Generates a request based on receiver's id list and private key, where the -// id list contains k messages' id receiver want to inquery, the -// private key used to get the message inquired is kept secretly by the -// receiver, and the request contains three public keys being sent to sender to -// encrypt the his messages. -pub fn receiver_init_k_out_of_n( - id_list: &IdList, -) -> (ReceiverSecret, ReceiverPublicKOutOfN) { - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let mut r_public = ReceiverPublicKOutOfN::default(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - r_public.set_point_x(point_to_bytes(&point_x)); - r_public.set_point_y(point_to_bytes(&point_y)); - for id in id_list.get_id() { - let id_scalar = Scalar::hash_from_bytes::(id); - let point_z = RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ - *BASEPOINT_G1, - ]); - r_public.mut_point_z().push(point_to_bytes(&point_z)); - } - ( - ReceiverSecret { - scalar_a: scalar_to_bytes(&blinding_a), - scalar_b: scalar_to_bytes(&blinding_b), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - r_public, - ) -} - -// Computes sender's public responce(n pairs data for n messages), where each -// pair contains a symmetric ciphertext, a asymmetric ciphertext and a hash for -// each message(n messages in total). -// - The symmetric ciphertext is computed by encrypting the message using the -// symmetric -// key randomly generated for this message. -// - The asymmetric ciphertext is computed by encrypting the symmetric -// key using three public keys from receiver. -// - The hash is hash of the symmetric key, in order to help the receiver -// identify -// whether a certain message is the message he inquired. -pub fn sender_init_k_out_of_n( - data: &SenderData, - r_public: &ReceiverPublicKOutOfN, -) -> Result { - let mut sender_public = SenderPublic::default(); - let point_x = bytes_to_point(r_public.get_point_x())?; - let point_y = bytes_to_point(r_public.get_point_y())?; - - for data_pair in data.get_pair() { - let mut pair = SenderPublicPairKOutOfN::default(); - let blinding_r = get_random_scalar(); - let blinding_s = get_random_scalar(); - let point_w = - RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ - point_x, - *BASEPOINT_G1, - ]); - let message = data_pair.get_message(); - let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); - pair.set_figure_print(HASH_SHA3_256.hash(message)); - pair.set_point_w(point_to_bytes(&point_w)); - for k_point_z in r_public.get_point_z() { - let point_z = bytes_to_point(k_point_z)?; - let point_key = RistrettoPoint::multiscalar_mul( - &[blinding_s, blinding_s * id_scalar, blinding_r], - &[point_z, *BASEPOINT_G1, point_y], - ); - let mut bytes_key = point_to_bytes(&point_key); - while message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let encrypt_message: Vec = message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - pair.mut_encrypt_message().push(encrypt_message); - } - sender_public.mut_pairKN().push(pair) - } - Ok(sender_public) -} - -// Recovers k messages receiver inquired, specifically for each pair data from -// sender, receiver -// step1. uses receiver's private key to decrypt the asymmetric -// ciphertext to obtain the symmetric key. -// step2. -// calculates the hash of the symmetric key and compare it with the received -// hash. If it matches, specify that the corresponding symmetric ciphertext is -// the ciphertext of message inquired, otherwise, perform the calculation of the -// next pair data. -// step3. uses the symmetric key in step1 to decrypt the -// symmetric ciphertext identified in step2 to obtain the message inquired. -pub fn receiver_decrypt_k_out_of_n( - secret: &ReceiverSecret, - sender_public: &SenderPublic, -) -> Result>, WedprError> { - let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - let mut result: Vec> = vec![]; - let mut skip_vector: Vec<&[u8]> = vec![]; - for pair in sender_public.get_pairKN() { - let point_w = bytes_to_point(pair.get_point_w())?; - if skip_vector.contains(&pair.get_figure_print()) { - continue; - } - let point_key = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); - let mut bytes_key = point_to_bytes(&point_key); - for encrypt_message in pair.get_encrypt_message() { - while encrypt_message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let decrypt_message: Vec = encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() - { - result.push(decrypt_message); - skip_vector.push(pair.get_figure_print()); - } - } - } - Ok(result) -} - -#[cfg(test)] -mod tests { - use super::*; - use wedpr_l_protos::generated::ot::SenderDataPair; - - #[test] - fn test_base_ot_k_out_of_n() { - let mut choose_id_pb = IdList::default(); - let mut expect: Vec> = vec![]; - choose_id_pb.mut_id().push("10086".as_bytes().to_vec()); - choose_id_pb.mut_id().push("10010".as_bytes().to_vec()); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ("10000".as_bytes(), "wedpr test1".as_bytes()), - ("10086".as_bytes(), "wedpr test2".as_bytes()), - ("10010".as_bytes(), "wedpr test3".as_bytes()), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init_k_out_of_n(&choose_id_pb); - let s_public = sender_init_k_out_of_n(&sender_data, &r_public).unwrap(); - let message = - receiver_decrypt_k_out_of_n(&r_secret, &s_public).unwrap(); - expect.push("wedpr test2".as_bytes().to_vec()); - expect.push("wedpr test3".as_bytes().to_vec()); - assert_eq!(message, expect); - } -} diff --git a/crypto/oblivious_transfer/base_ot/src/lib.rs b/crypto/oblivious_transfer/base_ot/src/lib.rs index 6f864a8..65da8d0 100644 --- a/crypto/oblivious_transfer/base_ot/src/lib.rs +++ b/crypto/oblivious_transfer/base_ot/src/lib.rs @@ -5,6 +5,5 @@ #[macro_use] extern crate lazy_static; -pub mod k_out_of_n; -pub mod one_out_of_n; -pub mod one_out_of_two; +pub mod ot_kv; +pub mod ot_message; diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs deleted file mode 100644 index 814ed48..0000000 --- a/crypto/oblivious_transfer/base_ot/src/one_out_of_n.rs +++ /dev/null @@ -1,241 +0,0 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - -//! 1/N Oblivious transfer (OT) functions. -/// Sender has n data records, the format of each record is (id, message), -/// K/N Oblivious transfer (OT) can help receiver to get one message using -/// a id but does't disclose receiver's id and help sender keep the other -/// n-1 messages privacy. -use curve25519_dalek::{ - ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, -}; -use sha3::Sha3_512; -use wedpr_l_crypto_hash_sha3::WedprSha3_256; -use wedpr_l_crypto_zkp_utils::{ - bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, - scalar_to_bytes, BASEPOINT_G1, -}; -use wedpr_l_protos::generated::ot::{ - ReceiverPublic, ReceiverSecret, SenderData, SenderPublic, SenderPublicPair, -}; -use wedpr_l_utils::{error::WedprError, traits::Hash}; - -lazy_static! { - pub static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); -} - -// Generates the private key and three public keys based on the id receiver -// inquired, where the private key used to get the message inquired is kept -// secretly by the receiver, the public key will be sent to sender to encrypt -// the his messages. -pub fn receiver_init(id: &[u8]) -> (ReceiverSecret, ReceiverPublic) { - let id_scalar = Scalar::hash_from_bytes::(id); - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - let point_z = - RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[*BASEPOINT_G1]); - ( - ReceiverSecret { - scalar_a: scalar_to_bytes(&blinding_a), - scalar_b: scalar_to_bytes(&blinding_b), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ReceiverPublic { - point_x: point_to_bytes(&point_x), - point_y: point_to_bytes(&point_y), - point_z: point_to_bytes(&point_z), - unknown_fields: Default::default(), - cached_size: Default::default(), - }, - ) -} - -// Computes sender's public responce(n pairs data for n messages), where each -// pair contains a symmetric ciphertext, a asymmetric ciphertext and a hash for -// each message(n messages in total). -// - The symmetric ciphertext is computed by encrypting the message using the -// symmetric -// key randomly generated for this message. -// - The asymmetric ciphertext is computed by encrypting the symmetric -// key using three public keys from receiver. -// - The hash is hash of the symmetric key, in order to help the receiver -// identify -// whether a certain message is the message he inquired. -pub fn sender_init( - data: &SenderData, - r_public: &ReceiverPublic, -) -> Result { - let mut sender_public = SenderPublic::default(); - let point_x = bytes_to_point(r_public.get_point_x())?; - let point_y = bytes_to_point(r_public.get_point_y())?; - let point_z = bytes_to_point(r_public.get_point_z())?; - for data_pair in data.get_pair() { - let blinding_r = get_random_scalar(); - let blinding_s = get_random_scalar(); - let point_w = - RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ - point_x, - *BASEPOINT_G1, - ]); - let message = data_pair.get_message(); - let id = data_pair.get_id(); - let id_scalar = Scalar::hash_from_bytes::(id); - let point_key = RistrettoPoint::multiscalar_mul( - &[blinding_s, blinding_s * id_scalar, blinding_r], - &[point_z, *BASEPOINT_G1, point_y], - ); - let mut bytes_key = point_to_bytes(&point_key); - while message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let encrypt_message: Vec = message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - sender_public.mut_pair().push(SenderPublicPair { - figure_print: HASH_SHA3_256.hash(message), - point_w: point_to_bytes(&point_w), - encrypt_message, - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - Ok(sender_public) -} - -// Recovers the message receiver inquired, specifically for each pair data from -// sender, receiver -// step1. uses receiver's private key to decrypt the asymmetric -// ciphertext to obtain the symmetric key. -// step2. calculates the hash of the symmetric key and compare it with the -// received hash. If it matches, specify that the corresponding symmetric -// ciphertext is the ciphertext of message inquired, otherwise, perform the -// calculation of the next pair data. -// step3. uses the symmetric key in step1 to decrypt the -// symmetric ciphertext identified in step2 to obtain the message inquired. -pub fn receiver_decrypt( - secret: &ReceiverSecret, - // id: &[u8], - sender_public: &SenderPublic, -) -> Result, WedprError> { - let blinding_b = bytes_to_scalar(secret.get_scalar_b())?; - for pair in sender_public.get_pair() { - // if id != pair.get_id() { - // continue; - // } - let point_w = bytes_to_point(pair.get_point_w())?; - let encrypt_message = pair.get_encrypt_message(); - let point_key = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[point_w]); - let mut bytes_key = point_to_bytes(&point_key); - while encrypt_message.len() > bytes_key.len() { - for key_bytes in bytes_key.clone() { - bytes_key.push(key_bytes); - } - } - let decrypt_message: Vec = encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - if &HASH_SHA3_256.hash(&decrypt_message) == pair.get_figure_print() { - return Ok(decrypt_message); - } - } - Err(WedprError::ArgumentError) -} - -#[cfg(test)] -mod tests { - use super::*; - use wedpr_l_protos::generated::ot::SenderDataPair; - - #[test] - fn test_base_ot_1_out_of_n() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ("10000".as_bytes(), "wedpr test1".as_bytes()), - ("10086".as_bytes(), "wedpr test2".as_bytes()), - ("10010".as_bytes(), "wedpr test3".as_bytes()), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - assert_eq!(message, "wedpr test2".as_bytes()); - } - - #[test] - fn test_base_ot_long() { - let choose_id = "10086".as_bytes(); - let mut sender_data = SenderData::default(); - for (id, message) in vec![ - ( - "10000".as_bytes(), - "1-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10086".as_bytes(), - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ( - "10010".as_bytes(), - "3-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、\ - 更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes(), - ), - ] { - sender_data.mut_pair().push(SenderDataPair { - id: id.to_vec(), - message: message.to_vec(), - unknown_fields: Default::default(), - cached_size: Default::default(), - }) - } - let (r_secret, r_public) = receiver_init(choose_id); - let s_public = sender_init(&sender_data, &r_public).unwrap(); - let message = receiver_decrypt(&r_secret, &s_public).unwrap(); - // receiver_decrypt(&r_secret, choose_id, &s_public).unwrap(); - let message_str = String::from_utf8(message.clone()).unwrap(); - // println!("message = {}", message_str); - assert_eq!( - message_str, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .to_string() - ); - assert_eq!( - message, - "2-WeDPR全面拥抱开放,将陆续开源一系列核心算法组件,\ - 进一步提升系统安全性的透明度,提供更透明、更可信的隐私保护效果。\ - WeDPR-Lab就是这一系列开源的核心算法组件的集合" - .as_bytes() - ); - } -} diff --git a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs b/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs deleted file mode 100644 index 0726d0e..0000000 --- a/crypto/oblivious_transfer/base_ot/src/one_out_of_two.rs +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. - -//! 1/2 Oblivious transfer (OT) functions. -/// Sender has two data records, the format of record is (index, message), -/// index = 0 or 1, 1/2 Oblivious transfer (OT) can help receiver to get -/// the messages he want to query but does't disclose receiver's choice and -/// sender's another message. -use curve25519_dalek::{ - ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, -}; -use wedpr_l_crypto_zkp_utils::{ - get_random_scalar, point_to_bytes, BASEPOINT_G1, -}; - -#[derive(Default, Debug, Clone)] -pub struct DataOneOutOfTwo { - pub data0: Vec, - pub data1: Vec, -} - -#[derive(Default, Debug, Clone)] -pub struct EncryptPairOneOutOfTwo { - pub point_w: RistrettoPoint, - pub encrypt_message: Vec, -} - -#[derive(Default, Debug, Clone)] -pub struct EncryptOneOutOfTwo { - pub encrypt0: EncryptPairOneOutOfTwo, - pub encrypt1: EncryptPairOneOutOfTwo, -} - -// Generates a query based on receiver's choice, where receiver's -// choice is 0 or 1, separately means the index of sender's two messages. -// Specificly, the query contains a private key and three public keys, the -// private key used to get the message inquired is kept secretly by the -// receiver, the public key will be sent to sender to encrypt the his two -// messages. -pub fn receiver_init_1_out_of_2( - choice: bool, -) -> (Scalar, RistrettoPoint, RistrettoPoint, RistrettoPoint) { - let blinding_a = get_random_scalar(); - let blinding_b = get_random_scalar(); - let c_id = blinding_a * blinding_b; - let point_x = - RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); - let point_y = - RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); - let point_z: RistrettoPoint; - if choice { - point_z = RistrettoPoint::multiscalar_mul(&[c_id - Scalar::one()], &[ - *BASEPOINT_G1, - ]); - } else { - point_z = RistrettoPoint::multiscalar_mul(&[c_id], &[*BASEPOINT_G1]); - } - (blinding_b, point_x, point_y, point_z) -} - -// Computes two ciphertexts by using three public keys from receiver to -// encrypt his two messages, where the ciphertexts will be sent to receiver to -// decrypt the message he inquired. -pub fn sender_init_1_out_of_2( - data: &DataOneOutOfTwo, - point_x: &RistrettoPoint, - point_y: &RistrettoPoint, - point_z: &RistrettoPoint, -) -> EncryptOneOutOfTwo { - let point_z1 = point_z + *BASEPOINT_G1; - - // compute zero ot - let blinding_r0 = get_random_scalar(); - let blinding_s0 = get_random_scalar(); - let point_w0 = - RistrettoPoint::multiscalar_mul(&[blinding_s0, blinding_r0], &[ - *point_x, - *BASEPOINT_G1, - ]); - let point_key0 = - RistrettoPoint::multiscalar_mul(&[blinding_s0, blinding_r0], [ - point_z, point_y, - ]); - let bytes_key0 = point_to_bytes(&point_key0); - let encrypt_message0: Vec = data - .data0 - .iter() - .zip(bytes_key0.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - - // compute one ot - let blinding_r1 = get_random_scalar(); - let blinding_s1 = get_random_scalar(); - let point_w1 = - RistrettoPoint::multiscalar_mul(&[blinding_s1, blinding_r1], &[ - *point_x, - *BASEPOINT_G1, - ]); - let point_key1 = - RistrettoPoint::multiscalar_mul(&[blinding_s1, blinding_r1], &[ - point_z1, *point_y, - ]); - let bytes_key1 = point_to_bytes(&point_key1); - let encrypt_message1: Vec = data - .data1 - .iter() - .zip(bytes_key1.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - EncryptOneOutOfTwo { - encrypt0: EncryptPairOneOutOfTwo { - point_w: point_w0, - encrypt_message: encrypt_message0, - }, - encrypt1: EncryptPairOneOutOfTwo { - point_w: point_w1, - encrypt_message: encrypt_message1, - }, - } -} - -// Decrypts the two ciphertexts from sender to get message he inquired. -// Since the public key is calculated using the receiver's choice, and the -// private key has a binding relationship with the public key, the receiver can -// use the private key to decrypt two ciphertexts respectively to get the -// message he chose. For the ciphertext of another message, the receiver can -// only get a string of random numbers after decryption. -pub fn receiver_decrypt_1_out_of_2( - choice: bool, - blinding_b: &Scalar, - sender_public: &EncryptOneOutOfTwo, -) -> Vec { - if choice { - let point_key = - RistrettoPoint::multiscalar_mul([blinding_b], [sender_public - .encrypt1 - .point_w]); - let bytes_key = point_to_bytes(&point_key); - let decrypt_message: Vec = sender_public - .encrypt1 - .encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - return decrypt_message; - } - let point_key = - RistrettoPoint::multiscalar_mul([blinding_b], [sender_public - .encrypt0 - .point_w]); - let bytes_key = point_to_bytes(&point_key); - let decrypt_message: Vec = sender_public - .encrypt0 - .encrypt_message - .iter() - .zip(bytes_key.iter()) - .map(|(&x1, &x2)| x1 ^ x2) - .collect(); - return decrypt_message; -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn test_base_ot_1_out_of_2() { - let data = DataOneOutOfTwo { - data0: "wedpr test1".as_bytes().to_vec(), - data1: "wedpr test2".as_bytes().to_vec(), - }; - let choice = true; - let (blinding_b, point_x, point_y, point_z) = - receiver_init_1_out_of_2(choice); - let sender_public = - sender_init_1_out_of_2(&data, &point_x, &point_y, &point_z); - let decrypt_message = - receiver_decrypt_1_out_of_2(choice, &blinding_b, &sender_public); - assert_eq!(decrypt_message, "wedpr test2".as_bytes().to_vec()); - } -} diff --git a/crypto/oblivious_transfer/base_ot/src/ot_kv.rs b/crypto/oblivious_transfer/base_ot/src/ot_kv.rs new file mode 100644 index 0000000..be6a1e5 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/ot_kv.rs @@ -0,0 +1,239 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! KV oblivious transfer (OT) functions. + +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use sha3::Sha3_512; +use wedpr_l_crypto_hash_sha3::WedprSha3_256; +use wedpr_l_crypto_zkp_utils::{ + bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes, + scalar_to_bytes, BASEPOINT_G1, +}; +use wedpr_l_protos::generated::ot::{ + BytesToBytesPair, DataDict, IdList, OtCiphertextItemKOutOfN, + OtCiphertextsKOutOfN, ReceiverCommitmentKOutOfN, ReceiverSecretKOutOfN, +}; +use wedpr_l_utils::{error::WedprError, traits::Hash}; + +lazy_static! { + static ref HASH_SHA3_256: WedprSha3_256 = WedprSha3_256::default(); +} + +/// Implements a k-out-of-n KV OT instance. +/// Sender has n KV pairs, where each pair is (id, message), +/// k-out-of-n KV OT can help receiver to get exactly k messages from k matching +/// ids without disclosing receiver's actual choice and the rest of sender's +/// messages. +#[derive(Default, Debug, Clone)] +pub struct OtKvKOutOfN {} + +impl OtKvKOutOfN { + /// 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 + /// which should by kept secretly by receiver. ReceiverCommitment is the + /// actual query to be sent to sender for generating OT response. + pub fn receiver_init( + &self, + choice_list: &IdList, + ) -> (ReceiverSecretKOutOfN, ReceiverCommitmentKOutOfN) { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let mut receiver_commitment = ReceiverCommitmentKOutOfN::default(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + receiver_commitment.set_point_x(point_to_bytes(&point_x)); + receiver_commitment.set_point_y(point_to_bytes(&point_y)); + for id in choice_list.get_id() { + let id_scalar = Scalar::hash_from_bytes::(id); + let point_z = + RistrettoPoint::multiscalar_mul(&[c_id - id_scalar], &[ + *BASEPOINT_G1, + ]); + receiver_commitment + .mut_point_z() + .push(point_to_bytes(&point_z)); + } + ( + ReceiverSecretKOutOfN { + scalar_b: scalar_to_bytes(&blinding_b), + unknown_fields: Default::default(), + cached_size: Default::default(), + }, + receiver_commitment, + ) + } + + /// Computes OT ciphertexts based on the ReceiverCommitment from receiver. + /// It returns ciphertext OT response for all available KV pairs. It will + /// raise error if a plaintext message is too long. + pub fn sender_init( + &self, + data: &DataDict, + receiver_commitment: &ReceiverCommitmentKOutOfN, + ) -> Result { + let point_x = bytes_to_point(receiver_commitment.get_point_x())?; + let point_y = bytes_to_point(receiver_commitment.get_point_y())?; + + let mut sender_ciphertexts = OtCiphertextsKOutOfN::default(); + for data_pair in data.get_pair() { + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let key_basepoint = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + point_x, + *BASEPOINT_G1, + ]); + let message = data_pair.get_message(); + let id = data_pair.get_id(); + let id_scalar = Scalar::hash_from_bytes::(id); + + let mut ciphertext = OtCiphertextItemKOutOfN::default(); + ciphertext.set_fingerprint(HASH_SHA3_256.hash(message)); + ciphertext.set_key_basepoint(point_to_bytes(&key_basepoint)); + for k_point_z in receiver_commitment.get_point_z() { + // TODO: Extract common OT computation to utility. + let point_z = bytes_to_point(k_point_z)?; + let bytes_key = + point_to_bytes(&RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_s * id_scalar, blinding_r], + &[point_z, *BASEPOINT_G1, point_y], + )); + // TODO: Add KDF function to extend the key size for long + // message. + if bytes_key.len() < message.len() { + return Err(WedprError::ArgumentError); + } + let encrypted_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + ciphertext.mut_encrypted_message().push(encrypted_message); + } + sender_ciphertexts.mut_ciphertext().push(ciphertext) + } + Ok(sender_ciphertexts) + } + + /// Decrypts the ciphertext OT response based on receiver's choice and + /// ReceiverSecret. Receiver can only decrypt exactly choice_count OT + /// messages. It returns the list of decrypted message bytes. + pub fn receiver_decrypt( + &self, + receiver_secret: &ReceiverSecretKOutOfN, + sender_ciphertexts: &OtCiphertextsKOutOfN, + choice_count: usize, + ) -> Result>, WedprError> { + let blinding_b = bytes_to_scalar(receiver_secret.get_scalar_b())?; + let mut result: Vec> = vec![]; + for ciphertext in sender_ciphertexts.get_ciphertext() { + // Get all messages already. + if result.len() == choice_count { + break; + } + + let key_basepoint = bytes_to_point(ciphertext.get_key_basepoint())?; + let bytes_key = point_to_bytes(&RistrettoPoint::multiscalar_mul( + &[blinding_b], + &[key_basepoint], + )); + // TODO: Add KDF function to extend the key size for long message. + for encrypted_message in ciphertext.get_encrypted_message() { + let decrypted_message: Vec = encrypted_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + if &HASH_SHA3_256.hash(&decrypted_message) + == ciphertext.get_fingerprint() + { + result.push(decrypted_message); + break; + } + } + } + Ok(result) + } +} + +/// Creates OT bytes choice list from an id list. +pub fn make_choice_list(choice_list: Vec<&str>) -> IdList { + let mut id_list = IdList::default(); + for id in choice_list { + id_list.mut_id().push(id.as_bytes().to_vec()); + } + id_list +} + +/// Creates OT bytes KV collection from string lists. +pub fn make_data_dict(id_list: Vec<&str>, message_list: Vec<&str>) -> DataDict { + // TODO: Change to Result return type if necessary. + assert_eq!(id_list.len(), message_list.len()); + let mut data_dict = DataDict::default(); + for (id, message) in id_list.iter().zip(message_list.iter()) { + data_dict.mut_pair().push(BytesToBytesPair { + id: (*id.as_bytes()).to_vec(), + message: (*message.as_bytes()).to_vec(), + unknown_fields: Default::default(), + cached_size: Default::default(), + }) + } + data_dict +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_ot_kv_k_out_of_n() { + let id0 = "10-11"; + let id1 = "10#12"; + let id2 = "20-x3"; + let id3 = "30-w*"; + + let message0 = "wedpr test 0"; + let message1 = "wedpr test 1"; + let message2 = "wedpr test 2"; + let message3 = "wedpr test 3"; + + // Pick values of id0 and id2. + let choice_list = make_choice_list(vec![id0, id2]); + let data = make_data_dict(vec![id0, id1, id2, id3], vec![ + message0, message1, message2, message3, + ]); + let ot = OtKvKOutOfN::default(); + + let (receiver_secret, receiver_commitment) = + ot.receiver_init(&choice_list); + let sender_ciphertexts = + ot.sender_init(&data, &receiver_commitment).unwrap(); + let message = ot + .receiver_decrypt( + &receiver_secret, + &sender_ciphertexts, + choice_list.get_id().len(), + ) + .unwrap(); + assert_eq!( + vec![message0.as_bytes().to_vec(), message2.as_bytes().to_vec()], + message + ); + + // Test error condition when an input message is too long. + let too_long_message = "message123message123message123message123message123message123message123message123"; + let too_long_data = + make_data_dict(vec![id0, id1], vec![message0, too_long_message]); + assert_eq!( + ot.sender_init(&too_long_data, &receiver_commitment) + .unwrap_err(), + WedprError::ArgumentError + ); + } +} diff --git a/crypto/oblivious_transfer/base_ot/src/ot_message.rs b/crypto/oblivious_transfer/base_ot/src/ot_message.rs new file mode 100644 index 0000000..1fe7df3 --- /dev/null +++ b/crypto/oblivious_transfer/base_ot/src/ot_message.rs @@ -0,0 +1,266 @@ +// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. + +//! Message oblivious transfer (OT) functions. + +use curve25519_dalek::{ + ristretto::RistrettoPoint, scalar::Scalar, traits::MultiscalarMul, +}; +use wedpr_l_crypto_zkp_utils::{ + get_random_scalar, point_to_bytes, BASEPOINT_G1, +}; +use wedpr_l_utils::error::WedprError; + +// TODO: Move the following structs to protos if necessary. + +/// Sender's plaintext bytes collection of 1-out-of-2 OT. +#[derive(Default, Debug, Clone)] +pub struct TwoOtMessages { + pub message0: Vec, + pub message1: Vec, +} + +/// Sender's ciphertext item for a single encrypted message of 1-out-of-2 OT. +#[derive(Default, Debug, Clone)] +pub struct TwoOtCiphertextItem { + pub key_basepoint: RistrettoPoint, + pub encrypted_message: Vec, +} + +/// Sender's ciphertext collection of 1-out-of-2 OT. +#[derive(Default, Debug, Clone)] +pub struct TwoOtCiphertexts { + pub ciphertext0: TwoOtCiphertextItem, + pub ciphertext1: TwoOtCiphertextItem, +} + +/// Receiver's secret to decrypt the chosen messages during 1-out-of-2 OT. +#[derive(Default, Debug, Clone)] +pub struct ReceiverSecret1OutOf2 { + pub scalar_b: Scalar, +} + +/// Receiver's commitment for the chosen messages during 1-out-of-2 OT. +#[derive(Default, Debug, Clone)] +pub struct ReceiverCommitment1OutOf2 { + pub point_x: RistrettoPoint, + pub point_y: RistrettoPoint, + pub point_z: RistrettoPoint, +} + +/// Implements a 1-out-of-2 message OT instance. +/// Sender has two data messages, message0 and message1, +/// 1-out-of-2 message OT can help receiver to get +/// exactly one chosen message without disclosing receiver's actual choice and +/// the rest of sender's messages. +#[derive(Default, Debug, Clone)] +pub struct OtMessages1OutOf2 {} + +impl OtMessages1OutOf2 { + /// Generates an OT query based on receiver's choice. It will pick message0 + /// if choose_zero is true otherwise message1. + /// It returns ReceiverSecret and ReceiverCommitment. ReceiverSecret will be + /// later used to decrypt the chosen message which should by kept secretly + /// by receiver. ReceiverCommitment is the actual query to be sent to + /// sender for generating OT response. + pub fn receiver_init( + &self, + choose_zero: bool, + ) -> (ReceiverSecret1OutOf2, ReceiverCommitment1OutOf2) { + let blinding_a = get_random_scalar(); + let blinding_b = get_random_scalar(); + let c_id = blinding_a * blinding_b; + let point_x = + RistrettoPoint::multiscalar_mul(&[blinding_a], &[*BASEPOINT_G1]); + let point_y = + RistrettoPoint::multiscalar_mul(&[blinding_b], &[*BASEPOINT_G1]); + let point_z: RistrettoPoint; + if choose_zero { + point_z = + RistrettoPoint::multiscalar_mul(&[c_id], &[*BASEPOINT_G1]); + } else { + point_z = + RistrettoPoint::multiscalar_mul(&[c_id - Scalar::one()], &[ + *BASEPOINT_G1, + ]); + } + ( + ReceiverSecret1OutOf2 { + scalar_b: blinding_b, + }, + ReceiverCommitment1OutOf2 { + point_x, + point_y, + point_z, + }, + ) + } + + /// Computes OT ciphertexts based on the ReceiverCommitment from receiver. + /// It returns ciphertext OT response for all available messages. It will + /// raise error if a plaintext message is too long. + pub fn sender_init( + &self, + data: &TwoOtMessages, + receiver_commitment: &ReceiverCommitment1OutOf2, + ) -> Result { + let ReceiverCommitment1OutOf2 { + point_x, + point_y, + point_z, + } = receiver_commitment; + + // Encrypt message 0. + let (key_basepoint0, encrypted_message0) = + OtMessages1OutOf2::sender_encrypt_message( + &data.message0, + point_x, + point_y, + point_z, + )?; + // Encrypt message 1. + let point_z1 = point_z + *BASEPOINT_G1; + let (key_basepoint1, encrypted_message1) = + OtMessages1OutOf2::sender_encrypt_message( + &data.message1, + point_x, + point_y, + &point_z1, + )?; + + Ok(TwoOtCiphertexts { + ciphertext0: TwoOtCiphertextItem { + key_basepoint: key_basepoint0, + encrypted_message: encrypted_message0, + }, + ciphertext1: TwoOtCiphertextItem { + key_basepoint: key_basepoint1, + encrypted_message: encrypted_message1, + }, + }) + } + + /// Decrypts the ciphertext OT response based on receiver's choice and + /// ReceiverSecret. Receiver can only decrypt exactly one OT message. It + /// returns the decrypted message bytes. + pub fn receiver_decrypt( + &self, + choose_zero: bool, + receiver_secret: &ReceiverSecret1OutOf2, + sender_ciphertexts: &TwoOtCiphertexts, + ) -> Vec { + let blinding_b = receiver_secret.scalar_b; + if choose_zero { + OtMessages1OutOf2::receiver_decrypt_message( + &sender_ciphertexts.ciphertext0, + &blinding_b, + ) + } else { + OtMessages1OutOf2::receiver_decrypt_message( + &sender_ciphertexts.ciphertext1, + &blinding_b, + ) + } + } + + fn sender_encrypt_message( + message: &Vec, + point_x: &RistrettoPoint, + point_y: &RistrettoPoint, + point_z: &RistrettoPoint, + ) -> Result<(RistrettoPoint, Vec), WedprError> { + let blinding_r = get_random_scalar(); + let blinding_s = get_random_scalar(); + let key_basepoint = + RistrettoPoint::multiscalar_mul(&[blinding_s, blinding_r], &[ + *point_x, + *BASEPOINT_G1, + ]); + let bytes_key = point_to_bytes(&RistrettoPoint::multiscalar_mul( + &[blinding_s, blinding_r], + &[*point_z, *point_y], + )); + // TODO: Add KDF function to extend the key size for long message. + if bytes_key.len() < message.len() { + return Err(WedprError::ArgumentError); + } + let encrypted_message: Vec = message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect(); + Ok((key_basepoint, encrypted_message)) + } + + fn receiver_decrypt_message( + ciphertext: &TwoOtCiphertextItem, + blinding_b: &Scalar, + ) -> Vec { + let bytes_key = + point_to_bytes(&RistrettoPoint::multiscalar_mul([blinding_b], [ + ciphertext.key_basepoint, + ])); + // TODO: Add KDF function to extend the key size for long message. + ciphertext + .encrypted_message + .iter() + .zip(bytes_key.iter()) + .map(|(&x1, &x2)| x1 ^ x2) + .collect() + } +} + +/// Creates OT bytes message collection from strings. +pub fn make_two_ot_messages(message0: &str, message1: &str) -> TwoOtMessages { + TwoOtMessages { + message0: message0.as_bytes().to_vec(), + message1: message1.as_bytes().to_vec(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_ot_message_1_out_of_2() { + let message0 = "wedpr test 0"; + let message1 = "wedpr test 1"; + let ot = OtMessages1OutOf2::default(); + let data = make_two_ot_messages(message0, message1); + + // Pick the first (index = 0) message. + let choose_zero = true; + let (receiver_secret, receiver_commitment) = + ot.receiver_init(choose_zero); + let sender_ciphertexts = + ot.sender_init(&data, &receiver_commitment).unwrap(); + let decrypted_message = ot.receiver_decrypt( + choose_zero, + &receiver_secret, + &sender_ciphertexts, + ); + assert_eq!(String::from_utf8(decrypted_message).unwrap(), message0); + + // Pick the second (index = 1) message. + let choose_zero = false; + let (receiver_secret, receiver_commitment) = + ot.receiver_init(choose_zero); + let sender_ciphertexts = + ot.sender_init(&data, &receiver_commitment).unwrap(); + let decrypted_message = ot.receiver_decrypt( + choose_zero, + &receiver_secret, + &sender_ciphertexts, + ); + assert_eq!(String::from_utf8(decrypted_message).unwrap(), message1); + + // Test error condition when an input message is too long. + let too_long_data = make_two_ot_messages( + message0, "message123message123message123message123message123message123message123message123"); + assert_eq!( + ot.sender_init(&too_long_data, &receiver_commitment) + .unwrap_err(), + WedprError::ArgumentError + ); + } +} diff --git a/crypto/zkp/discrete_logarithm_proof/benches/dlp.rs b/crypto/zkp/discrete_logarithm_proof/benches/dlp.rs index 53c1653..83e1ec8 100644 --- a/crypto/zkp/discrete_logarithm_proof/benches/dlp.rs +++ b/crypto/zkp/discrete_logarithm_proof/benches/dlp.rs @@ -1,3 +1,5 @@ +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. + #[macro_use] extern crate criterion; use criterion::Criterion; diff --git a/crypto/zkp/utils/Cargo.toml b/crypto/zkp/utils/Cargo.toml index 0ba812e..eb6c6d5 100644 --- a/crypto/zkp/utils/Cargo.toml +++ b/crypto/zkp/utils/Cargo.toml @@ -12,8 +12,7 @@ description = "Library of WeDPR shared zkp function utils." curve25519-dalek = { version = "1", features = [ "serde" ] } lazy_static = "1.4.0" rand = "0.6" -rand_core = { version = "0.5", features = ["getrandom"]} -#sha2 = "0.9" +rand_core = { version = "0.5", features = ["getrandom"] } sha3 = "0.8" wedpr_l_crypto_hash_keccak256 = "1.1.0" wedpr_l_macros = "1.0.0" diff --git a/crypto/zkp/utils/src/lib.rs b/crypto/zkp/utils/src/lib.rs index 7e6eaf3..3d27e8a 100644 --- a/crypto/zkp/utils/src/lib.rs +++ b/crypto/zkp/utils/src/lib.rs @@ -19,9 +19,7 @@ extern crate lazy_static; mod config; use config::HASH; use rand::Rng; -// use rand_core::OsRng; use sha3::Sha3_512; -// use sha2::Sha512; use std::convert::TryInto; use wedpr_l_utils::{error::WedprError, traits::Hash}; @@ -29,10 +27,6 @@ lazy_static! { /// A base point used by various crypto algorithms. pub static ref BASEPOINT_G1: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; /// Another base point used by various crypto algorithms. - // pub static ref BASEPOINT_G2: RistrettoPoint = - // RistrettoPoint::hash_from_bytes::( - // RISTRETTO_BASEPOINT_COMPRESSED.as_bytes() - // ); pub static ref BASEPOINT_G2: RistrettoPoint = RistrettoPoint::hash_from_bytes::( RISTRETTO_BASEPOINT_COMPRESSED.as_bytes() @@ -44,8 +38,6 @@ const RISTRETTO_POINT_SIZE_IN_BYTES: usize = 32; /// Gets a random Scalar. pub fn get_random_scalar() -> Scalar { - // let mut csprng = OsRng; - // Scalar::random(&mut csprng) Scalar::random(&mut rand::thread_rng()) } diff --git a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs index 6fda840..d37ecda 100644 --- a/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs +++ b/ffi/ffi_java/ffi_java_fisco_bcos_sdk/src/vrf.rs @@ -21,7 +21,7 @@ use wedpr_third_party_fisco_bcos_java_sdk; #[no_mangle] /// Java interface for -/// 'xxx.NativeInterface->curve25519VrfProveUtf8'. +/// 'com.webank.fisco.bcos.wedpr.sdk.NativeInterface->curve25519VrfProveUtf8'. pub extern "system" fn Java_com_webank_fisco_bcos_wedpr_sdk_NativeInterface_curve25519VrfProveUtf8( _env: JNIEnv, _class: JClass, diff --git a/protos/crypto/ot.proto b/protos/crypto/ot.proto index 6218e1f..4c40ec3 100644 --- a/protos/crypto/ot.proto +++ b/protos/crypto/ot.proto @@ -1,4 +1,4 @@ -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. syntax = "proto3"; @@ -6,60 +6,42 @@ package com.webank.wedpr.crypto.proto; option java_package = "com.webank.wedpr.crypto.proto"; option java_multiple_files = true; -// ot key pair containing a public key and a private key. - -// Private key of receiver. -message ReceiverSecret { - bytes scalar_a = 1; +// Receiver's secret to decrypt the chosen messages during k-out-of-n OT. +message ReceiverSecretKOutOfN { bytes scalar_b = 2; } -// Public key of receiver for 1/n ot. -message ReceiverPublic { - bytes point_x = 1; - bytes point_y = 2; - bytes point_z = 3; -} - -// Request of receiver for k/n ot. -message ReceiverPublicKOutOfN { +// Receiver's commitment for the chosen messages during k-out-of-n OT. +message ReceiverCommitmentKOutOfN { bytes point_x = 1; bytes point_y = 2; repeated bytes point_z = 3; } -// Record of sender's messages with one out of n OT. -message SenderPublicPair { - bytes figure_print = 1; - bytes point_w = 2; - bytes encrypt_message = 3; -} - -// Record of sender's messages with k out of n OT. -message SenderPublicPairKOutOfN { - bytes figure_print = 1; - bytes point_w = 2; - repeated bytes encrypt_message = 3; +// Sender's ciphertext item for a single encrypted message of k-out-of-n OT. +message OtCiphertextItemKOutOfN { + bytes fingerprint = 1; + bytes key_basepoint = 2; + repeated bytes encrypted_message = 3; } -// Ciphertext of sender's each message. -message SenderPublic { - repeated SenderPublicPair pair = 1; - repeated SenderPublicPairKOutOfN pairKN = 2; +// Sender's ciphertext collection of k-out-of-n OT. +message OtCiphertextsKOutOfN { + repeated OtCiphertextItemKOutOfN ciphertext = 1; } -// Record of sender's message. -message SenderDataPair { +// Pair of id and message bytes. +message BytesToBytesPair { bytes id = 1; bytes message = 2; } -// Record of sender's each message. -message SenderData { - repeated SenderDataPair pair = 1; +// Dict of id and message bytes. +message DataDict { + repeated BytesToBytesPair pair = 1; } -// List of ids to be inqueried. +// List of ids. message IdList { repeated bytes id = 1; } diff --git a/protos/src/generated/common.rs b/protos/src/generated/common.rs index d7be59b..098d694 100644 --- a/protos/src/generated/common.rs +++ b/protos/src/generated/common.rs @@ -1,3 +1,5 @@ +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. + // This file is generated by rust-protobuf 2.22.1. Do not edit // @generated diff --git a/protos/src/generated/ot.rs b/protos/src/generated/ot.rs index 17caac0..38e8333 100644 --- a/protos/src/generated/ot.rs +++ b/protos/src/generated/ot.rs @@ -1,3 +1,5 @@ +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. + // This file is generated by rust-protobuf 2.22.1. Do not edit // @generated @@ -24,52 +26,25 @@ // const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1; #[derive(PartialEq,Clone,Default)] -pub struct ReceiverSecret { +pub struct ReceiverSecretKOutOfN { // message fields - pub scalar_a: ::std::vec::Vec, pub scalar_b: ::std::vec::Vec, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a ReceiverSecret { - fn default() -> &'a ReceiverSecret { - ::default_instance() +impl<'a> ::std::default::Default for &'a ReceiverSecretKOutOfN { + fn default() -> &'a ReceiverSecretKOutOfN { + ::default_instance() } } -impl ReceiverSecret { - pub fn new() -> ReceiverSecret { +impl ReceiverSecretKOutOfN { + pub fn new() -> ReceiverSecretKOutOfN { ::std::default::Default::default() } - // bytes scalar_a = 1; - - - pub fn get_scalar_a(&self) -> &[u8] { - &self.scalar_a - } - pub fn clear_scalar_a(&mut self) { - self.scalar_a.clear(); - } - - // Param is passed by value, moved - pub fn set_scalar_a(&mut self, v: ::std::vec::Vec) { - self.scalar_a = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_scalar_a(&mut self) -> &mut ::std::vec::Vec { - &mut self.scalar_a - } - - // Take field - pub fn take_scalar_a(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.scalar_a, ::std::vec::Vec::new()) - } - // bytes scalar_b = 2; @@ -97,7 +72,7 @@ impl ReceiverSecret { } } -impl ::protobuf::Message for ReceiverSecret { +impl ::protobuf::Message for ReceiverSecretKOutOfN { fn is_initialized(&self) -> bool { true } @@ -106,9 +81,6 @@ impl ::protobuf::Message for ReceiverSecret { while !is.eof()? { let (field_number, wire_type) = is.read_tag_unpack()?; match field_number { - 1 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.scalar_a)?; - }, 2 => { ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.scalar_b)?; }, @@ -124,9 +96,6 @@ impl ::protobuf::Message for ReceiverSecret { #[allow(unused_variables)] fn compute_size(&self) -> u32 { let mut my_size = 0; - if !self.scalar_a.is_empty() { - my_size += ::protobuf::rt::bytes_size(1, &self.scalar_a); - } if !self.scalar_b.is_empty() { my_size += ::protobuf::rt::bytes_size(2, &self.scalar_b); } @@ -136,9 +105,6 @@ impl ::protobuf::Message for ReceiverSecret { } fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - if !self.scalar_a.is_empty() { - os.write_bytes(1, &self.scalar_a)?; - } if !self.scalar_b.is_empty() { os.write_bytes(2, &self.scalar_b)?; } @@ -172,303 +138,54 @@ impl ::protobuf::Message for ReceiverSecret { Self::descriptor_static() } - fn new() -> ReceiverSecret { - ReceiverSecret::new() + fn new() -> ReceiverSecretKOutOfN { + ReceiverSecretKOutOfN::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; descriptor.get(|| { let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "scalar_a", - |m: &ReceiverSecret| { &m.scalar_a }, - |m: &mut ReceiverSecret| { &mut m.scalar_a }, - )); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "scalar_b", - |m: &ReceiverSecret| { &m.scalar_b }, - |m: &mut ReceiverSecret| { &mut m.scalar_b }, + |m: &ReceiverSecretKOutOfN| { &m.scalar_b }, + |m: &mut ReceiverSecretKOutOfN| { &mut m.scalar_b }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "ReceiverSecret", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "ReceiverSecretKOutOfN", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static ReceiverSecret { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(ReceiverSecret::new) + fn default_instance() -> &'static ReceiverSecretKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(ReceiverSecretKOutOfN::new) } } -impl ::protobuf::Clear for ReceiverSecret { +impl ::protobuf::Clear for ReceiverSecretKOutOfN { fn clear(&mut self) { - self.scalar_a.clear(); self.scalar_b.clear(); self.unknown_fields.clear(); } } -impl ::std::fmt::Debug for ReceiverSecret { - fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - ::protobuf::text_format::fmt(self, f) - } -} - -impl ::protobuf::reflect::ProtobufValue for ReceiverSecret { - fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { - ::protobuf::reflect::ReflectValueRef::Message(self) - } -} - -#[derive(PartialEq,Clone,Default)] -pub struct ReceiverPublic { - // message fields - pub point_x: ::std::vec::Vec, - pub point_y: ::std::vec::Vec, - pub point_z: ::std::vec::Vec, - // special fields - pub unknown_fields: ::protobuf::UnknownFields, - pub cached_size: ::protobuf::CachedSize, -} - -impl<'a> ::std::default::Default for &'a ReceiverPublic { - fn default() -> &'a ReceiverPublic { - ::default_instance() - } -} - -impl ReceiverPublic { - pub fn new() -> ReceiverPublic { - ::std::default::Default::default() - } - - // bytes point_x = 1; - - - pub fn get_point_x(&self) -> &[u8] { - &self.point_x - } - pub fn clear_point_x(&mut self) { - self.point_x.clear(); - } - - // Param is passed by value, moved - pub fn set_point_x(&mut self, v: ::std::vec::Vec) { - self.point_x = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_point_x(&mut self) -> &mut ::std::vec::Vec { - &mut self.point_x - } - - // Take field - pub fn take_point_x(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.point_x, ::std::vec::Vec::new()) - } - - // bytes point_y = 2; - - - pub fn get_point_y(&self) -> &[u8] { - &self.point_y - } - pub fn clear_point_y(&mut self) { - self.point_y.clear(); - } - - // Param is passed by value, moved - pub fn set_point_y(&mut self, v: ::std::vec::Vec) { - self.point_y = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_point_y(&mut self) -> &mut ::std::vec::Vec { - &mut self.point_y - } - - // Take field - pub fn take_point_y(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.point_y, ::std::vec::Vec::new()) - } - - // bytes point_z = 3; - - - pub fn get_point_z(&self) -> &[u8] { - &self.point_z - } - pub fn clear_point_z(&mut self) { - self.point_z.clear(); - } - - // Param is passed by value, moved - pub fn set_point_z(&mut self, v: ::std::vec::Vec) { - self.point_z = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_point_z(&mut self) -> &mut ::std::vec::Vec { - &mut self.point_z - } - - // Take field - pub fn take_point_z(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.point_z, ::std::vec::Vec::new()) - } -} - -impl ::protobuf::Message for ReceiverPublic { - fn is_initialized(&self) -> bool { - true - } - - fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { - while !is.eof()? { - let (field_number, wire_type) = is.read_tag_unpack()?; - match field_number { - 1 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_x)?; - }, - 2 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_y)?; - }, - 3 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_z)?; - }, - _ => { - ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; - }, - }; - } - ::std::result::Result::Ok(()) - } - - // Compute sizes of nested messages - #[allow(unused_variables)] - fn compute_size(&self) -> u32 { - let mut my_size = 0; - if !self.point_x.is_empty() { - my_size += ::protobuf::rt::bytes_size(1, &self.point_x); - } - if !self.point_y.is_empty() { - my_size += ::protobuf::rt::bytes_size(2, &self.point_y); - } - if !self.point_z.is_empty() { - my_size += ::protobuf::rt::bytes_size(3, &self.point_z); - } - my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); - self.cached_size.set(my_size); - my_size - } - - fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - if !self.point_x.is_empty() { - os.write_bytes(1, &self.point_x)?; - } - if !self.point_y.is_empty() { - os.write_bytes(2, &self.point_y)?; - } - if !self.point_z.is_empty() { - os.write_bytes(3, &self.point_z)?; - } - os.write_unknown_fields(self.get_unknown_fields())?; - ::std::result::Result::Ok(()) - } - - fn get_cached_size(&self) -> u32 { - self.cached_size.get() - } - - fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { - &self.unknown_fields - } - - fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { - &mut self.unknown_fields - } - - fn as_any(&self) -> &dyn (::std::any::Any) { - self as &dyn (::std::any::Any) - } - fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { - self as &mut dyn (::std::any::Any) - } - fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { - self - } - - fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { - Self::descriptor_static() - } - - fn new() -> ReceiverPublic { - ReceiverPublic::new() - } - - fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { - static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; - descriptor.get(|| { - let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "point_x", - |m: &ReceiverPublic| { &m.point_x }, - |m: &mut ReceiverPublic| { &mut m.point_x }, - )); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "point_y", - |m: &ReceiverPublic| { &m.point_y }, - |m: &mut ReceiverPublic| { &mut m.point_y }, - )); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "point_z", - |m: &ReceiverPublic| { &m.point_z }, - |m: &mut ReceiverPublic| { &mut m.point_z }, - )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "ReceiverPublic", - fields, - file_descriptor_proto() - ) - }) - } - - fn default_instance() -> &'static ReceiverPublic { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(ReceiverPublic::new) - } -} - -impl ::protobuf::Clear for ReceiverPublic { - fn clear(&mut self) { - self.point_x.clear(); - self.point_y.clear(); - self.point_z.clear(); - self.unknown_fields.clear(); - } -} - -impl ::std::fmt::Debug for ReceiverPublic { +impl ::std::fmt::Debug for ReceiverSecretKOutOfN { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for ReceiverPublic { +impl ::protobuf::reflect::ProtobufValue for ReceiverSecretKOutOfN { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } } #[derive(PartialEq,Clone,Default)] -pub struct ReceiverPublicKOutOfN { +pub struct ReceiverCommitmentKOutOfN { // message fields pub point_x: ::std::vec::Vec, pub point_y: ::std::vec::Vec, @@ -478,14 +195,14 @@ pub struct ReceiverPublicKOutOfN { pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a ReceiverPublicKOutOfN { - fn default() -> &'a ReceiverPublicKOutOfN { - ::default_instance() +impl<'a> ::std::default::Default for &'a ReceiverCommitmentKOutOfN { + fn default() -> &'a ReceiverCommitmentKOutOfN { + ::default_instance() } } -impl ReceiverPublicKOutOfN { - pub fn new() -> ReceiverPublicKOutOfN { +impl ReceiverCommitmentKOutOfN { + pub fn new() -> ReceiverCommitmentKOutOfN { ::std::default::Default::default() } @@ -567,7 +284,7 @@ impl ReceiverPublicKOutOfN { } } -impl ::protobuf::Message for ReceiverPublicKOutOfN { +impl ::protobuf::Message for ReceiverCommitmentKOutOfN { fn is_initialized(&self) -> bool { true } @@ -651,8 +368,8 @@ impl ::protobuf::Message for ReceiverPublicKOutOfN { Self::descriptor_static() } - fn new() -> ReceiverPublicKOutOfN { - ReceiverPublicKOutOfN::new() + fn new() -> ReceiverCommitmentKOutOfN { + ReceiverCommitmentKOutOfN::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { @@ -661,34 +378,34 @@ impl ::protobuf::Message for ReceiverPublicKOutOfN { let mut fields = ::std::vec::Vec::new(); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "point_x", - |m: &ReceiverPublicKOutOfN| { &m.point_x }, - |m: &mut ReceiverPublicKOutOfN| { &mut m.point_x }, + |m: &ReceiverCommitmentKOutOfN| { &m.point_x }, + |m: &mut ReceiverCommitmentKOutOfN| { &mut m.point_x }, )); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "point_y", - |m: &ReceiverPublicKOutOfN| { &m.point_y }, - |m: &mut ReceiverPublicKOutOfN| { &mut m.point_y }, + |m: &ReceiverCommitmentKOutOfN| { &m.point_y }, + |m: &mut ReceiverCommitmentKOutOfN| { &mut m.point_y }, )); fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "point_z", - |m: &ReceiverPublicKOutOfN| { &m.point_z }, - |m: &mut ReceiverPublicKOutOfN| { &mut m.point_z }, + |m: &ReceiverCommitmentKOutOfN| { &m.point_z }, + |m: &mut ReceiverCommitmentKOutOfN| { &mut m.point_z }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "ReceiverPublicKOutOfN", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "ReceiverCommitmentKOutOfN", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static ReceiverPublicKOutOfN { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(ReceiverPublicKOutOfN::new) + fn default_instance() -> &'static ReceiverCommitmentKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(ReceiverCommitmentKOutOfN::new) } } -impl ::protobuf::Clear for ReceiverPublicKOutOfN { +impl ::protobuf::Clear for ReceiverCommitmentKOutOfN { fn clear(&mut self) { self.point_x.clear(); self.point_y.clear(); @@ -697,362 +414,119 @@ impl ::protobuf::Clear for ReceiverPublicKOutOfN { } } -impl ::std::fmt::Debug for ReceiverPublicKOutOfN { - fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - ::protobuf::text_format::fmt(self, f) - } -} - -impl ::protobuf::reflect::ProtobufValue for ReceiverPublicKOutOfN { - fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { - ::protobuf::reflect::ReflectValueRef::Message(self) - } -} - -#[derive(PartialEq,Clone,Default)] -pub struct SenderPublicPair { - // message fields - pub figure_print: ::std::vec::Vec, - pub point_w: ::std::vec::Vec, - pub encrypt_message: ::std::vec::Vec, - // special fields - pub unknown_fields: ::protobuf::UnknownFields, - pub cached_size: ::protobuf::CachedSize, -} - -impl<'a> ::std::default::Default for &'a SenderPublicPair { - fn default() -> &'a SenderPublicPair { - ::default_instance() - } -} - -impl SenderPublicPair { - pub fn new() -> SenderPublicPair { - ::std::default::Default::default() - } - - // bytes figure_print = 1; - - - pub fn get_figure_print(&self) -> &[u8] { - &self.figure_print - } - pub fn clear_figure_print(&mut self) { - self.figure_print.clear(); - } - - // Param is passed by value, moved - pub fn set_figure_print(&mut self, v: ::std::vec::Vec) { - self.figure_print = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_figure_print(&mut self) -> &mut ::std::vec::Vec { - &mut self.figure_print - } - - // Take field - pub fn take_figure_print(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.figure_print, ::std::vec::Vec::new()) - } - - // bytes point_w = 2; - - - pub fn get_point_w(&self) -> &[u8] { - &self.point_w - } - pub fn clear_point_w(&mut self) { - self.point_w.clear(); - } - - // Param is passed by value, moved - pub fn set_point_w(&mut self, v: ::std::vec::Vec) { - self.point_w = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_point_w(&mut self) -> &mut ::std::vec::Vec { - &mut self.point_w - } - - // Take field - pub fn take_point_w(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.point_w, ::std::vec::Vec::new()) - } - - // bytes encrypt_message = 3; - - - pub fn get_encrypt_message(&self) -> &[u8] { - &self.encrypt_message - } - pub fn clear_encrypt_message(&mut self) { - self.encrypt_message.clear(); - } - - // Param is passed by value, moved - pub fn set_encrypt_message(&mut self, v: ::std::vec::Vec) { - self.encrypt_message = v; - } - - // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_encrypt_message(&mut self) -> &mut ::std::vec::Vec { - &mut self.encrypt_message - } - - // Take field - pub fn take_encrypt_message(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.encrypt_message, ::std::vec::Vec::new()) - } -} - -impl ::protobuf::Message for SenderPublicPair { - fn is_initialized(&self) -> bool { - true - } - - fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { - while !is.eof()? { - let (field_number, wire_type) = is.read_tag_unpack()?; - match field_number { - 1 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.figure_print)?; - }, - 2 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_w)?; - }, - 3 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.encrypt_message)?; - }, - _ => { - ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; - }, - }; - } - ::std::result::Result::Ok(()) - } - - // Compute sizes of nested messages - #[allow(unused_variables)] - fn compute_size(&self) -> u32 { - let mut my_size = 0; - if !self.figure_print.is_empty() { - my_size += ::protobuf::rt::bytes_size(1, &self.figure_print); - } - if !self.point_w.is_empty() { - my_size += ::protobuf::rt::bytes_size(2, &self.point_w); - } - if !self.encrypt_message.is_empty() { - my_size += ::protobuf::rt::bytes_size(3, &self.encrypt_message); - } - my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); - self.cached_size.set(my_size); - my_size - } - - fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - if !self.figure_print.is_empty() { - os.write_bytes(1, &self.figure_print)?; - } - if !self.point_w.is_empty() { - os.write_bytes(2, &self.point_w)?; - } - if !self.encrypt_message.is_empty() { - os.write_bytes(3, &self.encrypt_message)?; - } - os.write_unknown_fields(self.get_unknown_fields())?; - ::std::result::Result::Ok(()) - } - - fn get_cached_size(&self) -> u32 { - self.cached_size.get() - } - - fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { - &self.unknown_fields - } - - fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { - &mut self.unknown_fields - } - - fn as_any(&self) -> &dyn (::std::any::Any) { - self as &dyn (::std::any::Any) - } - fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { - self as &mut dyn (::std::any::Any) - } - fn into_any(self: ::std::boxed::Box) -> ::std::boxed::Box { - self - } - - fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { - Self::descriptor_static() - } - - fn new() -> SenderPublicPair { - SenderPublicPair::new() - } - - fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { - static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; - descriptor.get(|| { - let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "figure_print", - |m: &SenderPublicPair| { &m.figure_print }, - |m: &mut SenderPublicPair| { &mut m.figure_print }, - )); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "point_w", - |m: &SenderPublicPair| { &m.point_w }, - |m: &mut SenderPublicPair| { &mut m.point_w }, - )); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "encrypt_message", - |m: &SenderPublicPair| { &m.encrypt_message }, - |m: &mut SenderPublicPair| { &mut m.encrypt_message }, - )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderPublicPair", - fields, - file_descriptor_proto() - ) - }) - } - - fn default_instance() -> &'static SenderPublicPair { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderPublicPair::new) - } -} - -impl ::protobuf::Clear for SenderPublicPair { - fn clear(&mut self) { - self.figure_print.clear(); - self.point_w.clear(); - self.encrypt_message.clear(); - self.unknown_fields.clear(); - } -} - -impl ::std::fmt::Debug for SenderPublicPair { +impl ::std::fmt::Debug for ReceiverCommitmentKOutOfN { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderPublicPair { +impl ::protobuf::reflect::ProtobufValue for ReceiverCommitmentKOutOfN { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } } #[derive(PartialEq,Clone,Default)] -pub struct SenderPublicPairKOutOfN { +pub struct OtCiphertextItemKOutOfN { // message fields - pub figure_print: ::std::vec::Vec, - pub point_w: ::std::vec::Vec, - pub encrypt_message: ::protobuf::RepeatedField<::std::vec::Vec>, + pub fingerprint: ::std::vec::Vec, + pub key_basepoint: ::std::vec::Vec, + pub encrypted_message: ::protobuf::RepeatedField<::std::vec::Vec>, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a SenderPublicPairKOutOfN { - fn default() -> &'a SenderPublicPairKOutOfN { - ::default_instance() +impl<'a> ::std::default::Default for &'a OtCiphertextItemKOutOfN { + fn default() -> &'a OtCiphertextItemKOutOfN { + ::default_instance() } } -impl SenderPublicPairKOutOfN { - pub fn new() -> SenderPublicPairKOutOfN { +impl OtCiphertextItemKOutOfN { + pub fn new() -> OtCiphertextItemKOutOfN { ::std::default::Default::default() } - // bytes figure_print = 1; + // bytes fingerprint = 1; - pub fn get_figure_print(&self) -> &[u8] { - &self.figure_print + pub fn get_fingerprint(&self) -> &[u8] { + &self.fingerprint } - pub fn clear_figure_print(&mut self) { - self.figure_print.clear(); + pub fn clear_fingerprint(&mut self) { + self.fingerprint.clear(); } // Param is passed by value, moved - pub fn set_figure_print(&mut self, v: ::std::vec::Vec) { - self.figure_print = v; + pub fn set_fingerprint(&mut self, v: ::std::vec::Vec) { + self.fingerprint = v; } // Mutable pointer to the field. // If field is not initialized, it is initialized with default value first. - pub fn mut_figure_print(&mut self) -> &mut ::std::vec::Vec { - &mut self.figure_print + pub fn mut_fingerprint(&mut self) -> &mut ::std::vec::Vec { + &mut self.fingerprint } // Take field - pub fn take_figure_print(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.figure_print, ::std::vec::Vec::new()) + pub fn take_fingerprint(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.fingerprint, ::std::vec::Vec::new()) } - // bytes point_w = 2; + // bytes key_basepoint = 2; - pub fn get_point_w(&self) -> &[u8] { - &self.point_w + pub fn get_key_basepoint(&self) -> &[u8] { + &self.key_basepoint } - pub fn clear_point_w(&mut self) { - self.point_w.clear(); + pub fn clear_key_basepoint(&mut self) { + self.key_basepoint.clear(); } // Param is passed by value, moved - pub fn set_point_w(&mut self, v: ::std::vec::Vec) { - self.point_w = v; + pub fn set_key_basepoint(&mut self, v: ::std::vec::Vec) { + self.key_basepoint = v; } // Mutable pointer to the field. // If field is not initialized, it is initialized with default value first. - pub fn mut_point_w(&mut self) -> &mut ::std::vec::Vec { - &mut self.point_w + pub fn mut_key_basepoint(&mut self) -> &mut ::std::vec::Vec { + &mut self.key_basepoint } // Take field - pub fn take_point_w(&mut self) -> ::std::vec::Vec { - ::std::mem::replace(&mut self.point_w, ::std::vec::Vec::new()) + pub fn take_key_basepoint(&mut self) -> ::std::vec::Vec { + ::std::mem::replace(&mut self.key_basepoint, ::std::vec::Vec::new()) } - // repeated bytes encrypt_message = 3; + // repeated bytes encrypted_message = 3; - pub fn get_encrypt_message(&self) -> &[::std::vec::Vec] { - &self.encrypt_message + pub fn get_encrypted_message(&self) -> &[::std::vec::Vec] { + &self.encrypted_message } - pub fn clear_encrypt_message(&mut self) { - self.encrypt_message.clear(); + pub fn clear_encrypted_message(&mut self) { + self.encrypted_message.clear(); } // Param is passed by value, moved - pub fn set_encrypt_message(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec>) { - self.encrypt_message = v; + pub fn set_encrypted_message(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec>) { + self.encrypted_message = v; } // Mutable pointer to the field. - pub fn mut_encrypt_message(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec> { - &mut self.encrypt_message + pub fn mut_encrypted_message(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec> { + &mut self.encrypted_message } // Take field - pub fn take_encrypt_message(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec> { - ::std::mem::replace(&mut self.encrypt_message, ::protobuf::RepeatedField::new()) + pub fn take_encrypted_message(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec> { + ::std::mem::replace(&mut self.encrypted_message, ::protobuf::RepeatedField::new()) } } -impl ::protobuf::Message for SenderPublicPairKOutOfN { +impl ::protobuf::Message for OtCiphertextItemKOutOfN { fn is_initialized(&self) -> bool { true } @@ -1062,13 +536,13 @@ impl ::protobuf::Message for SenderPublicPairKOutOfN { let (field_number, wire_type) = is.read_tag_unpack()?; match field_number { 1 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.figure_print)?; + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.fingerprint)?; }, 2 => { - ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.point_w)?; + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.key_basepoint)?; }, 3 => { - ::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.encrypt_message)?; + ::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.encrypted_message)?; }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; @@ -1082,13 +556,13 @@ impl ::protobuf::Message for SenderPublicPairKOutOfN { #[allow(unused_variables)] fn compute_size(&self) -> u32 { let mut my_size = 0; - if !self.figure_print.is_empty() { - my_size += ::protobuf::rt::bytes_size(1, &self.figure_print); + if !self.fingerprint.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.fingerprint); } - if !self.point_w.is_empty() { - my_size += ::protobuf::rt::bytes_size(2, &self.point_w); + if !self.key_basepoint.is_empty() { + my_size += ::protobuf::rt::bytes_size(2, &self.key_basepoint); } - for value in &self.encrypt_message { + for value in &self.encrypted_message { my_size += ::protobuf::rt::bytes_size(3, &value); }; my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); @@ -1097,13 +571,13 @@ impl ::protobuf::Message for SenderPublicPairKOutOfN { } fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - if !self.figure_print.is_empty() { - os.write_bytes(1, &self.figure_print)?; + if !self.fingerprint.is_empty() { + os.write_bytes(1, &self.fingerprint)?; } - if !self.point_w.is_empty() { - os.write_bytes(2, &self.point_w)?; + if !self.key_basepoint.is_empty() { + os.write_bytes(2, &self.key_basepoint)?; } - for v in &self.encrypt_message { + for v in &self.encrypted_message { os.write_bytes(3, &v)?; }; os.write_unknown_fields(self.get_unknown_fields())?; @@ -1136,8 +610,8 @@ impl ::protobuf::Message for SenderPublicPairKOutOfN { Self::descriptor_static() } - fn new() -> SenderPublicPairKOutOfN { - SenderPublicPairKOutOfN::new() + fn new() -> OtCiphertextItemKOutOfN { + OtCiphertextItemKOutOfN::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { @@ -1145,135 +619,104 @@ impl ::protobuf::Message for SenderPublicPairKOutOfN { descriptor.get(|| { let mut fields = ::std::vec::Vec::new(); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "figure_print", - |m: &SenderPublicPairKOutOfN| { &m.figure_print }, - |m: &mut SenderPublicPairKOutOfN| { &mut m.figure_print }, + "fingerprint", + |m: &OtCiphertextItemKOutOfN| { &m.fingerprint }, + |m: &mut OtCiphertextItemKOutOfN| { &mut m.fingerprint }, )); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "point_w", - |m: &SenderPublicPairKOutOfN| { &m.point_w }, - |m: &mut SenderPublicPairKOutOfN| { &mut m.point_w }, + "key_basepoint", + |m: &OtCiphertextItemKOutOfN| { &m.key_basepoint }, + |m: &mut OtCiphertextItemKOutOfN| { &mut m.key_basepoint }, )); fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( - "encrypt_message", - |m: &SenderPublicPairKOutOfN| { &m.encrypt_message }, - |m: &mut SenderPublicPairKOutOfN| { &mut m.encrypt_message }, + "encrypted_message", + |m: &OtCiphertextItemKOutOfN| { &m.encrypted_message }, + |m: &mut OtCiphertextItemKOutOfN| { &mut m.encrypted_message }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderPublicPairKOutOfN", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "OtCiphertextItemKOutOfN", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static SenderPublicPairKOutOfN { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderPublicPairKOutOfN::new) + fn default_instance() -> &'static OtCiphertextItemKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(OtCiphertextItemKOutOfN::new) } } -impl ::protobuf::Clear for SenderPublicPairKOutOfN { +impl ::protobuf::Clear for OtCiphertextItemKOutOfN { fn clear(&mut self) { - self.figure_print.clear(); - self.point_w.clear(); - self.encrypt_message.clear(); + self.fingerprint.clear(); + self.key_basepoint.clear(); + self.encrypted_message.clear(); self.unknown_fields.clear(); } } -impl ::std::fmt::Debug for SenderPublicPairKOutOfN { +impl ::std::fmt::Debug for OtCiphertextItemKOutOfN { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderPublicPairKOutOfN { +impl ::protobuf::reflect::ProtobufValue for OtCiphertextItemKOutOfN { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } } #[derive(PartialEq,Clone,Default)] -pub struct SenderPublic { +pub struct OtCiphertextsKOutOfN { // message fields - pub pair: ::protobuf::RepeatedField, - pub pairKN: ::protobuf::RepeatedField, + pub ciphertext: ::protobuf::RepeatedField, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a SenderPublic { - fn default() -> &'a SenderPublic { - ::default_instance() +impl<'a> ::std::default::Default for &'a OtCiphertextsKOutOfN { + fn default() -> &'a OtCiphertextsKOutOfN { + ::default_instance() } } -impl SenderPublic { - pub fn new() -> SenderPublic { +impl OtCiphertextsKOutOfN { + pub fn new() -> OtCiphertextsKOutOfN { ::std::default::Default::default() } - // repeated .com.webank.wedpr.crypto.proto.SenderPublicPair pair = 1; - - - pub fn get_pair(&self) -> &[SenderPublicPair] { - &self.pair - } - pub fn clear_pair(&mut self) { - self.pair.clear(); - } - - // Param is passed by value, moved - pub fn set_pair(&mut self, v: ::protobuf::RepeatedField) { - self.pair = v; - } - - // Mutable pointer to the field. - pub fn mut_pair(&mut self) -> &mut ::protobuf::RepeatedField { - &mut self.pair - } - - // Take field - pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { - ::std::mem::replace(&mut self.pair, ::protobuf::RepeatedField::new()) - } + // repeated .com.webank.wedpr.crypto.proto.OtCiphertextItemKOutOfN ciphertext = 1; - // repeated .com.webank.wedpr.crypto.proto.SenderPublicPairKOutOfN pairKN = 2; - - pub fn get_pairKN(&self) -> &[SenderPublicPairKOutOfN] { - &self.pairKN + pub fn get_ciphertext(&self) -> &[OtCiphertextItemKOutOfN] { + &self.ciphertext } - pub fn clear_pairKN(&mut self) { - self.pairKN.clear(); + pub fn clear_ciphertext(&mut self) { + self.ciphertext.clear(); } // Param is passed by value, moved - pub fn set_pairKN(&mut self, v: ::protobuf::RepeatedField) { - self.pairKN = v; + pub fn set_ciphertext(&mut self, v: ::protobuf::RepeatedField) { + self.ciphertext = v; } // Mutable pointer to the field. - pub fn mut_pairKN(&mut self) -> &mut ::protobuf::RepeatedField { - &mut self.pairKN + pub fn mut_ciphertext(&mut self) -> &mut ::protobuf::RepeatedField { + &mut self.ciphertext } // Take field - pub fn take_pairKN(&mut self) -> ::protobuf::RepeatedField { - ::std::mem::replace(&mut self.pairKN, ::protobuf::RepeatedField::new()) + pub fn take_ciphertext(&mut self) -> ::protobuf::RepeatedField { + ::std::mem::replace(&mut self.ciphertext, ::protobuf::RepeatedField::new()) } } -impl ::protobuf::Message for SenderPublic { +impl ::protobuf::Message for OtCiphertextsKOutOfN { fn is_initialized(&self) -> bool { - for v in &self.pair { - if !v.is_initialized() { - return false; - } - }; - for v in &self.pairKN { + for v in &self.ciphertext { if !v.is_initialized() { return false; } @@ -1286,10 +729,7 @@ impl ::protobuf::Message for SenderPublic { let (field_number, wire_type) = is.read_tag_unpack()?; match field_number { 1 => { - ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pair)?; - }, - 2 => { - ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.pairKN)?; + ::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.ciphertext)?; }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; @@ -1303,11 +743,7 @@ impl ::protobuf::Message for SenderPublic { #[allow(unused_variables)] fn compute_size(&self) -> u32 { let mut my_size = 0; - for value in &self.pair { - let len = value.compute_size(); - my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; - }; - for value in &self.pairKN { + for value in &self.ciphertext { let len = value.compute_size(); my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; }; @@ -1317,16 +753,11 @@ impl ::protobuf::Message for SenderPublic { } fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - for v in &self.pair { + for v in &self.ciphertext { os.write_tag(1, ::protobuf::wire_format::WireTypeLengthDelimited)?; os.write_raw_varint32(v.get_cached_size())?; v.write_to_with_cached_sizes(os)?; }; - for v in &self.pairKN { - os.write_tag(2, ::protobuf::wire_format::WireTypeLengthDelimited)?; - os.write_raw_varint32(v.get_cached_size())?; - v.write_to_with_cached_sizes(os)?; - }; os.write_unknown_fields(self.get_unknown_fields())?; ::std::result::Result::Ok(()) } @@ -1357,60 +788,54 @@ impl ::protobuf::Message for SenderPublic { Self::descriptor_static() } - fn new() -> SenderPublic { - SenderPublic::new() + fn new() -> OtCiphertextsKOutOfN { + OtCiphertextsKOutOfN::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; descriptor.get(|| { let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( - "pair", - |m: &SenderPublic| { &m.pair }, - |m: &mut SenderPublic| { &mut m.pair }, - )); - fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( - "pairKN", - |m: &SenderPublic| { &m.pairKN }, - |m: &mut SenderPublic| { &mut m.pairKN }, + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( + "ciphertext", + |m: &OtCiphertextsKOutOfN| { &m.ciphertext }, + |m: &mut OtCiphertextsKOutOfN| { &mut m.ciphertext }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderPublic", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "OtCiphertextsKOutOfN", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static SenderPublic { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderPublic::new) + fn default_instance() -> &'static OtCiphertextsKOutOfN { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(OtCiphertextsKOutOfN::new) } } -impl ::protobuf::Clear for SenderPublic { +impl ::protobuf::Clear for OtCiphertextsKOutOfN { fn clear(&mut self) { - self.pair.clear(); - self.pairKN.clear(); + self.ciphertext.clear(); self.unknown_fields.clear(); } } -impl ::std::fmt::Debug for SenderPublic { +impl ::std::fmt::Debug for OtCiphertextsKOutOfN { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderPublic { +impl ::protobuf::reflect::ProtobufValue for OtCiphertextsKOutOfN { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } } #[derive(PartialEq,Clone,Default)] -pub struct SenderDataPair { +pub struct BytesToBytesPair { // message fields pub id: ::std::vec::Vec, pub message: ::std::vec::Vec, @@ -1419,14 +844,14 @@ pub struct SenderDataPair { pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a SenderDataPair { - fn default() -> &'a SenderDataPair { - ::default_instance() +impl<'a> ::std::default::Default for &'a BytesToBytesPair { + fn default() -> &'a BytesToBytesPair { + ::default_instance() } } -impl SenderDataPair { - pub fn new() -> SenderDataPair { +impl BytesToBytesPair { + pub fn new() -> BytesToBytesPair { ::std::default::Default::default() } @@ -1483,7 +908,7 @@ impl SenderDataPair { } } -impl ::protobuf::Message for SenderDataPair { +impl ::protobuf::Message for BytesToBytesPair { fn is_initialized(&self) -> bool { true } @@ -1558,8 +983,8 @@ impl ::protobuf::Message for SenderDataPair { Self::descriptor_static() } - fn new() -> SenderDataPair { - SenderDataPair::new() + fn new() -> BytesToBytesPair { + BytesToBytesPair::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { @@ -1568,29 +993,29 @@ impl ::protobuf::Message for SenderDataPair { let mut fields = ::std::vec::Vec::new(); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "id", - |m: &SenderDataPair| { &m.id }, - |m: &mut SenderDataPair| { &mut m.id }, + |m: &BytesToBytesPair| { &m.id }, + |m: &mut BytesToBytesPair| { &mut m.id }, )); fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( "message", - |m: &SenderDataPair| { &m.message }, - |m: &mut SenderDataPair| { &mut m.message }, + |m: &BytesToBytesPair| { &m.message }, + |m: &mut BytesToBytesPair| { &mut m.message }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderDataPair", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "BytesToBytesPair", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static SenderDataPair { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderDataPair::new) + fn default_instance() -> &'static BytesToBytesPair { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(BytesToBytesPair::new) } } -impl ::protobuf::Clear for SenderDataPair { +impl ::protobuf::Clear for BytesToBytesPair { fn clear(&mut self) { self.id.clear(); self.message.clear(); @@ -1598,42 +1023,42 @@ impl ::protobuf::Clear for SenderDataPair { } } -impl ::std::fmt::Debug for SenderDataPair { +impl ::std::fmt::Debug for BytesToBytesPair { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderDataPair { +impl ::protobuf::reflect::ProtobufValue for BytesToBytesPair { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } } #[derive(PartialEq,Clone,Default)] -pub struct SenderData { +pub struct DataDict { // message fields - pub pair: ::protobuf::RepeatedField, + pub pair: ::protobuf::RepeatedField, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, } -impl<'a> ::std::default::Default for &'a SenderData { - fn default() -> &'a SenderData { - ::default_instance() +impl<'a> ::std::default::Default for &'a DataDict { + fn default() -> &'a DataDict { + ::default_instance() } } -impl SenderData { - pub fn new() -> SenderData { +impl DataDict { + pub fn new() -> DataDict { ::std::default::Default::default() } - // repeated .com.webank.wedpr.crypto.proto.SenderDataPair pair = 1; + // repeated .com.webank.wedpr.crypto.proto.BytesToBytesPair pair = 1; - pub fn get_pair(&self) -> &[SenderDataPair] { + pub fn get_pair(&self) -> &[BytesToBytesPair] { &self.pair } pub fn clear_pair(&mut self) { @@ -1641,22 +1066,22 @@ impl SenderData { } // Param is passed by value, moved - pub fn set_pair(&mut self, v: ::protobuf::RepeatedField) { + pub fn set_pair(&mut self, v: ::protobuf::RepeatedField) { self.pair = v; } // Mutable pointer to the field. - pub fn mut_pair(&mut self) -> &mut ::protobuf::RepeatedField { + pub fn mut_pair(&mut self) -> &mut ::protobuf::RepeatedField { &mut self.pair } // Take field - pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { + pub fn take_pair(&mut self) -> ::protobuf::RepeatedField { ::std::mem::replace(&mut self.pair, ::protobuf::RepeatedField::new()) } } -impl ::protobuf::Message for SenderData { +impl ::protobuf::Message for DataDict { fn is_initialized(&self) -> bool { for v in &self.pair { if !v.is_initialized() { @@ -1730,47 +1155,47 @@ impl ::protobuf::Message for SenderData { Self::descriptor_static() } - fn new() -> SenderData { - SenderData::new() + fn new() -> DataDict { + DataDict::new() } fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT; descriptor.get(|| { let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( + fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage>( "pair", - |m: &SenderData| { &m.pair }, - |m: &mut SenderData| { &mut m.pair }, + |m: &DataDict| { &m.pair }, + |m: &mut DataDict| { &mut m.pair }, )); - ::protobuf::reflect::MessageDescriptor::new_pb_name::( - "SenderData", + ::protobuf::reflect::MessageDescriptor::new_pb_name::( + "DataDict", fields, file_descriptor_proto() ) }) } - fn default_instance() -> &'static SenderData { - static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; - instance.get(SenderData::new) + fn default_instance() -> &'static DataDict { + static instance: ::protobuf::rt::LazyV2 = ::protobuf::rt::LazyV2::INIT; + instance.get(DataDict::new) } } -impl ::protobuf::Clear for SenderData { +impl ::protobuf::Clear for DataDict { fn clear(&mut self) { self.pair.clear(); self.unknown_fields.clear(); } } -impl ::std::fmt::Debug for SenderData { +impl ::std::fmt::Debug for DataDict { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { ::protobuf::text_format::fmt(self, f) } } -impl ::protobuf::reflect::ProtobufValue for SenderData { +impl ::protobuf::reflect::ProtobufValue for DataDict { fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef { ::protobuf::reflect::ReflectValueRef::Message(self) } @@ -1935,28 +1360,21 @@ impl ::protobuf::reflect::ProtobufValue for IdList { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\x0fcrypto/ot.proto\x12\x1dcom.webank.wedpr.crypto.proto\"F\n\x0eRecei\ - verSecret\x12\x19\n\x08scalar_a\x18\x01\x20\x01(\x0cR\x07scalarA\x12\x19\ - \n\x08scalar_b\x18\x02\x20\x01(\x0cR\x07scalarB\"[\n\x0eReceiverPublic\ - \x12\x17\n\x07point_x\x18\x01\x20\x01(\x0cR\x06pointX\x12\x17\n\x07point\ - _y\x18\x02\x20\x01(\x0cR\x06pointY\x12\x17\n\x07point_z\x18\x03\x20\x01(\ - \x0cR\x06pointZ\"b\n\x15ReceiverPublicKOutOfN\x12\x17\n\x07point_x\x18\ - \x01\x20\x01(\x0cR\x06pointX\x12\x17\n\x07point_y\x18\x02\x20\x01(\x0cR\ - \x06pointY\x12\x17\n\x07point_z\x18\x03\x20\x03(\x0cR\x06pointZ\"w\n\x10\ - SenderPublicPair\x12!\n\x0cfigure_print\x18\x01\x20\x01(\x0cR\x0bfigureP\ - rint\x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\x06pointW\x12'\n\x0fencr\ - ypt_message\x18\x03\x20\x01(\x0cR\x0eencryptMessage\"~\n\x17SenderPublic\ - PairKOutOfN\x12!\n\x0cfigure_print\x18\x01\x20\x01(\x0cR\x0bfigurePrint\ - \x12\x17\n\x07point_w\x18\x02\x20\x01(\x0cR\x06pointW\x12'\n\x0fencrypt_\ - message\x18\x03\x20\x03(\x0cR\x0eencryptMessage\"\xa3\x01\n\x0cSenderPub\ - lic\x12C\n\x04pair\x18\x01\x20\x03(\x0b2/.com.webank.wedpr.crypto.proto.\ - SenderPublicPairR\x04pair\x12N\n\x06pairKN\x18\x02\x20\x03(\x0b26.com.we\ - bank.wedpr.crypto.proto.SenderPublicPairKOutOfNR\x06pairKN\":\n\x0eSende\ - rDataPair\x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x18\n\x07messa\ - ge\x18\x02\x20\x01(\x0cR\x07message\"O\n\nSenderData\x12A\n\x04pair\x18\ - \x01\x20\x03(\x0b2-.com.webank.wedpr.crypto.proto.SenderDataPairR\x04pai\ - r\"\x18\n\x06IdList\x12\x0e\n\x02id\x18\x01\x20\x03(\x0cR\x02idB!\n\x1dc\ - om.webank.wedpr.crypto.protoP\x01b\x06proto3\ + \n\x0fcrypto/ot.proto\x12\x1dcom.webank.wedpr.crypto.proto\"2\n\x15Recei\ + verSecretKOutOfN\x12\x19\n\x08scalar_b\x18\x02\x20\x01(\x0cR\x07scalarB\ + \"f\n\x19ReceiverCommitmentKOutOfN\x12\x17\n\x07point_x\x18\x01\x20\x01(\ + \x0cR\x06pointX\x12\x17\n\x07point_y\x18\x02\x20\x01(\x0cR\x06pointY\x12\ + \x17\n\x07point_z\x18\x03\x20\x03(\x0cR\x06pointZ\"\x8d\x01\n\x17OtCiphe\ + rtextItemKOutOfN\x12\x20\n\x0bfingerprint\x18\x01\x20\x01(\x0cR\x0bfinge\ + rprint\x12#\n\rkey_basepoint\x18\x02\x20\x01(\x0cR\x0ckeyBasepoint\x12+\ + \n\x11encrypted_message\x18\x03\x20\x03(\x0cR\x10encryptedMessage\"n\n\ + \x14OtCiphertextsKOutOfN\x12V\n\nciphertext\x18\x01\x20\x03(\x0b26.com.w\ + ebank.wedpr.crypto.proto.OtCiphertextItemKOutOfNR\nciphertext\"<\n\x10By\ + tesToBytesPair\x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x18\n\x07\ + message\x18\x02\x20\x01(\x0cR\x07message\"O\n\x08DataDict\x12C\n\x04pair\ + \x18\x01\x20\x03(\x0b2/.com.webank.wedpr.crypto.proto.BytesToBytesPairR\ + \x04pair\"\x18\n\x06IdList\x12\x0e\n\x02id\x18\x01\x20\x03(\x0cR\x02idB!\ + \n\x1dcom.webank.wedpr.crypto.protoP\x01b\x06proto3\ "; static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; diff --git a/protos/src/generated/zkp.rs b/protos/src/generated/zkp.rs index 5c5e370..5a57230 100644 --- a/protos/src/generated/zkp.rs +++ b/protos/src/generated/zkp.rs @@ -1,3 +1,5 @@ +// Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. + // This file is generated by rust-protobuf 2.22.1. Do not edit // @generated diff --git a/third_party/fisco_bcos_java_sdk/src/lib.rs b/third_party/fisco_bcos_java_sdk/src/lib.rs index 3571c08..55ff9de 100644 --- a/third_party/fisco_bcos_java_sdk/src/lib.rs +++ b/third_party/fisco_bcos_java_sdk/src/lib.rs @@ -163,7 +163,6 @@ mod tests { let encode = proof.encode(); println!("encode = {}", encode); - // let decode = vrf_proof::decode(&encode).unwrap(); let result = curve25519_vrf_verify(&y, alpha, &proof); println!("result = {}", result); } From 0bb4d0a18cd4cbd4a082c7ec7ac5f35c217fdbca Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Wed, 29 Sep 2021 09:51:25 +0800 Subject: [PATCH 17/17] update crate --- crypto/oblivious_transfer/base_ot/Cargo.toml | 6 ++++-- crypto/vrf/curve25519/Cargo.toml | 2 +- crypto/zkp/discrete_logarithm_proof/Cargo.toml | 4 ++-- crypto/zkp/range_proof/Cargo.toml | 4 ++-- crypto/zkp/utils/Cargo.toml | 2 +- protos/Cargo.toml | 2 +- third_party/fisco_bcos_java_sdk/Cargo.toml | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/crypto/oblivious_transfer/base_ot/Cargo.toml b/crypto/oblivious_transfer/base_ot/Cargo.toml index 1a61e3f..d6380b7 100644 --- a/crypto/oblivious_transfer/base_ot/Cargo.toml +++ b/crypto/oblivious_transfer/base_ot/Cargo.toml @@ -2,15 +2,17 @@ name = "wedpr_l_crypto_ot_base_ot" version = "0.1.0" edition = "2018" +license = "Apache-2.0" +description = "Library of WeDPR shared zkp Function implement base ot." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] curve25519-dalek = { version = "1", features = [ "serde" ] } -wedpr_l_crypto_zkp_utils = { path = "../../zkp/utils" } +wedpr_l_crypto_zkp_utils = "1.2.0" wedpr_l_crypto_hash_sha3 = { version = "1.0.0" } lazy_static = "1.4.0" -wedpr_l_protos = { path = "../../../protos"} +wedpr_l_protos = "1.2.0" wedpr_l_utils = "1.1.0" sha3 = "0.8" rand = "0.6" diff --git a/crypto/vrf/curve25519/Cargo.toml b/crypto/vrf/curve25519/Cargo.toml index b44542f..4b2663c 100644 --- a/crypto/vrf/curve25519/Cargo.toml +++ b/crypto/vrf/curve25519/Cargo.toml @@ -13,6 +13,6 @@ curve25519-dalek = { version = "1", features = [ "serde" ] } rand = "0.6" sha3 = "0.8.2" wedpr_l_crypto_hash_keccak256 = "1.1.0" -wedpr_l_crypto_zkp_utils = "1.1.0" +wedpr_l_crypto_zkp_utils = "1.2.0" wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/discrete_logarithm_proof/Cargo.toml b/crypto/zkp/discrete_logarithm_proof/Cargo.toml index c82108c..cfe4b6b 100644 --- a/crypto/zkp/discrete_logarithm_proof/Cargo.toml +++ b/crypto/zkp/discrete_logarithm_proof/Cargo.toml @@ -10,9 +10,9 @@ description = "Library of WeDPR shared zkp Function implement discrete logarithm [dependencies] curve25519-dalek = { version = "1", features = [ "serde" ] } -wedpr_l_crypto_zkp_utils = { path = "../utils"} +wedpr_l_crypto_zkp_utils = "1.2.0" wedpr_l_macros = "1.0.0" -wedpr_l_protos = { path = "../../../protos"} +wedpr_l_protos = "1.2.0" wedpr_l_utils = "1.1.0" rand = "0.6" diff --git a/crypto/zkp/range_proof/Cargo.toml b/crypto/zkp/range_proof/Cargo.toml index 03de656..0c9dd40 100644 --- a/crypto/zkp/range_proof/Cargo.toml +++ b/crypto/zkp/range_proof/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wedpr_l_crypto_zkp_range_proof" -version = "1.1.0" +version = "1.2.0" authors = [ "WeDPR " ] edition = "2018" license = "Apache-2.0" @@ -12,6 +12,6 @@ description = "Library of WeDPR shared zkp Function implement range proof." bulletproofs = { package = "wedpr_l_bulletproofs", version = "1.0.5" } curve25519-dalek = { version = "1", features = [ "serde" ] } merlin = "1" -wedpr_l_crypto_zkp_utils = { path = "../utils"} +wedpr_l_crypto_zkp_utils = "1.2.0" wedpr_l_macros = "1.0.0" wedpr_l_utils = "1.1.0" diff --git a/crypto/zkp/utils/Cargo.toml b/crypto/zkp/utils/Cargo.toml index eb6c6d5..b28fb99 100644 --- a/crypto/zkp/utils/Cargo.toml +++ b/crypto/zkp/utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wedpr_l_crypto_zkp_utils" -version = "1.1.0" +version = "1.2.0" authors = [ "WeDPR " ] edition = "2018" license = "Apache-2.0" diff --git a/protos/Cargo.toml b/protos/Cargo.toml index 8fb426e..397c4b9 100644 --- a/protos/Cargo.toml +++ b/protos/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wedpr_l_protos" -version = "1.1.1" +version = "1.2.0" authors = [ "WeDPR " ] edition = "2018" license = "Apache-2.0" diff --git a/third_party/fisco_bcos_java_sdk/Cargo.toml b/third_party/fisco_bcos_java_sdk/Cargo.toml index 701c765..5479b0a 100644 --- a/third_party/fisco_bcos_java_sdk/Cargo.toml +++ b/third_party/fisco_bcos_java_sdk/Cargo.toml @@ -11,7 +11,7 @@ description = "Library of WeDPR specific utilities for FISCO BCOS Java Sdk." [dependencies] curve25519-dalek = { version = "1", features = ["serde"] } wedpr_l_utils = "1.1.0" -wedpr_l_crypto_zkp_utils = "1.1.0" +wedpr_l_crypto_zkp_utils = { path = "../../crypto/zkp/utils"} rand = "0.6" wedpr_l_common_coder_hex = { path = "../../common/coder/hex"} sha3 = "0.8.2"