diff --git a/fastcrypto-tbls/src/ecies.rs b/fastcrypto-tbls/src/ecies.rs index 21ac1f995a..f93e36c19c 100644 --- a/fastcrypto-tbls/src/ecies.rs +++ b/fastcrypto-tbls/src/ecies.rs @@ -58,6 +58,10 @@ where Self(G::ScalarType::rand(rng)) } + pub fn from(sc: G::ScalarType) -> Self { + Self(sc) + } + pub fn decrypt(&self, enc: &Encryption) -> Vec { enc.decrypt(&self.0) } @@ -112,6 +116,12 @@ where } } +impl From for PublicKey { + fn from(p: G) -> Self { + Self(p) + } +} + impl Encryption { fn deterministic_encrypt(msg: &[u8], r_g: &G, r_x_g: &G) -> Self { let hkdf_result = Self::hkdf(r_x_g); diff --git a/fastcrypto-tbls/src/tests/ecies_tests.rs b/fastcrypto-tbls/src/tests/ecies_tests.rs index a1c79bf0e5..96861e26b9 100644 --- a/fastcrypto-tbls/src/tests/ecies_tests.rs +++ b/fastcrypto-tbls/src/tests/ecies_tests.rs @@ -3,7 +3,11 @@ use crate::ecies::*; use crate::random_oracle::RandomOracle; +use fastcrypto::bls12381::min_sig::BLS12381KeyPair; +use fastcrypto::groups::bls12381::{G2Element, Scalar}; use fastcrypto::groups::ristretto255::RistrettoPoint; +use fastcrypto::groups::GroupElement; +use fastcrypto::traits::KeyPair; use rand::thread_rng; const MSG: &[u8; 4] = b"test"; @@ -42,3 +46,18 @@ fn test_recovery_package() { .decrypt_with_recovery_package(&pkg, &ro, &encryption) .is_err()); } + +#[test] +fn test_blskeypair_to_group() { + let pair = BLS12381KeyPair::generate(&mut thread_rng()); + let (pk, sk) = (pair.public().clone(), pair.private()); + let pk: G2Element = bcs::from_bytes(pk.as_ref()).expect("should work"); + let ecies_pk = PublicKey::::from(pk); + let sk: Scalar = bcs::from_bytes(sk.as_ref()).expect("should work"); + let ecies_sk = PrivateKey::::from(sk); + assert_eq!( + ecies_pk, + PublicKey::::from_private_key(&ecies_sk) + ); + assert_eq!(*ecies_pk.as_element(), G2Element::generator() * sk); +} diff --git a/fastcrypto/src/groups/bls12381.rs b/fastcrypto/src/groups/bls12381.rs index 277be907a7..be82bd536b 100644 --- a/fastcrypto/src/groups/bls12381.rs +++ b/fastcrypto/src/groups/bls12381.rs @@ -652,7 +652,7 @@ impl ScalarType for Scalar { impl FiatShamirChallenge for Scalar { fn fiat_shamir_reduction_to_group_element(uniform_buffer: &[u8]) -> Self { const INPUT_LENGTH: usize = SCALAR_LENGTH - 10; // Safe for our prime field - assert!(INPUT_LENGTH >= uniform_buffer.len()); + assert!(INPUT_LENGTH <= uniform_buffer.len()); let mut bytes = [0u8; INPUT_LENGTH]; bytes.copy_from_slice(&uniform_buffer[..INPUT_LENGTH]); let mut ret = blst_fr::default();