From 80d01b23c441b89dd58aae68b641868a33298486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 7 May 2021 17:01:53 +0200 Subject: [PATCH] sas: Return an array of seven emojis instead of a vector --- matrix_sdk/src/sas.rs | 2 +- .../src/verification/sas/helpers.rs | 28 ++++++++++++------- .../src/verification/sas/inner_sas.rs | 2 +- matrix_sdk_crypto/src/verification/sas/mod.rs | 6 ++-- .../src/verification/sas/sas_state.rs | 6 ++-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs index 5d8f1b81..17fa8eb7 100644 --- a/matrix_sdk/src/sas.rs +++ b/matrix_sdk/src/sas.rs @@ -112,7 +112,7 @@ impl Sas { } /// Get the emoji version of the short auth string. - pub fn emoji(&self) -> Option> { + pub fn emoji(&self) -> Option<[(&'static str, &'static str); 7]> { self.inner.emoji() } diff --git a/matrix_sdk_crypto/src/verification/sas/helpers.rs b/matrix_sdk_crypto/src/verification/sas/helpers.rs index 617a9827..b4db2572 100644 --- a/matrix_sdk_crypto/src/verification/sas/helpers.rs +++ b/matrix_sdk_crypto/src/verification/sas/helpers.rs @@ -402,7 +402,7 @@ fn extra_info_sas( /// Get the emoji version of the short authentication string. /// -/// Returns a vector of tuples where the first element is the emoji and the +/// Returns seven tuples where the first element is the emoji and the /// second element the English description of the emoji. /// /// # Arguments @@ -425,7 +425,7 @@ pub fn get_emoji( their_pubkey: &str, flow_id: &str, we_started: bool, -) -> Vec<(&'static str, &'static str)> { +) -> [(&'static str, &'static str); 7] { let bytes = sas .generate_bytes( &extra_info_sas(&ids, &sas.public_key(), their_pubkey, &flow_id, we_started), @@ -436,7 +436,7 @@ pub fn get_emoji( bytes_to_emoji(bytes) } -fn bytes_to_emoji_index(bytes: Vec) -> Vec { +fn bytes_to_emoji_index(bytes: Vec) -> [u8; 7] { let bytes: Vec = bytes.iter().map(|b| *b as u64).collect(); // Join the 6 bytes into one 64 bit unsigned int. This u64 will contain 48 // bits from our 6 bytes. @@ -449,7 +449,7 @@ fn bytes_to_emoji_index(bytes: Vec) -> Vec { // Take the top 42 bits of our 48 bits from the u64 and convert each 6 bits // into a 6 bit number. - vec![ + [ ((num >> 42) & 63) as u8, ((num >> 36) & 63) as u8, ((num >> 30) & 63) as u8, @@ -460,11 +460,19 @@ fn bytes_to_emoji_index(bytes: Vec) -> Vec { ] } -fn bytes_to_emoji(bytes: Vec) -> Vec<(&'static str, &'static str)> { +fn bytes_to_emoji(bytes: Vec) -> [(&'static str, &'static str); 7] { let numbers = bytes_to_emoji_index(bytes); // Convert the 6 bit number into a emoji/description tuple. - numbers.into_iter().map(emoji_from_index).collect() + [ + emoji_from_index(numbers[0]), + emoji_from_index(numbers[1]), + emoji_from_index(numbers[2]), + emoji_from_index(numbers[3]), + emoji_from_index(numbers[4]), + emoji_from_index(numbers[5]), + emoji_from_index(numbers[6]), + ] } /// Get the decimal version of the short authentication string. @@ -584,7 +592,7 @@ mod test { .into_iter() .map(emoji_from_index) .collect(); - assert_eq!(bytes_to_emoji(bytes), index); + assert_eq!(bytes_to_emoji(bytes), index.as_ref()); let bytes = vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; @@ -592,7 +600,7 @@ mod test { .into_iter() .map(emoji_from_index) .collect(); - assert_eq!(bytes_to_emoji(bytes), index); + assert_eq!(bytes_to_emoji(bytes), index.as_ref()); } #[test] @@ -612,8 +620,8 @@ mod test { fn proptest_emoji(bytes in prop::array::uniform6(0u8..)) { let numbers = bytes_to_emoji_index(bytes.to_vec()); - for number in numbers { - prop_assert!(number < 64); + for number in numbers.iter() { + prop_assert!(*number < 64); } } } diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index 49eeb5a5..03026e8f 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -343,7 +343,7 @@ impl InnerSas { } } - pub fn emoji(&self) -> Option> { + pub fn emoji(&self) -> Option<[(&'static str, &'static str); 7]> { match self { InnerSas::KeyRecieved(s) => Some(s.get_emoji()), InnerSas::MacReceived(s) => Some(s.get_emoji()), diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index e190b190..2fddb8f6 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -627,9 +627,9 @@ impl Sas { /// Get the emoji version of the short auth string. /// - /// Returns None if we can't yet present the short auth string, otherwise a - /// Vec of tuples with the emoji and description. - pub fn emoji(&self) -> Option> { + /// Returns None if we can't yet present the short auth string, otherwise + /// seven tuples containing the emoji and description. + pub fn emoji(&self) -> Option<[(&'static str, &'static str); 7]> { self.inner.lock().unwrap().emoji() } diff --git a/matrix_sdk_crypto/src/verification/sas/sas_state.rs b/matrix_sdk_crypto/src/verification/sas/sas_state.rs index 26801687..47cc9c3d 100644 --- a/matrix_sdk_crypto/src/verification/sas/sas_state.rs +++ b/matrix_sdk_crypto/src/verification/sas/sas_state.rs @@ -745,9 +745,9 @@ impl SasState { /// Get the emoji version of the short authentication string. /// - /// Returns a vector of tuples where the first element is the emoji and the + /// Returns a seven tuples where the first element is the emoji and the /// second element the English description of the emoji. - pub fn get_emoji(&self) -> Vec<(&'static str, &'static str)> { + pub fn get_emoji(&self) -> [(&'static str, &'static str); 7] { get_emoji( &self.inner.lock().unwrap(), &self.ids, @@ -968,7 +968,7 @@ impl SasState { /// /// Returns a vector of tuples where the first element is the emoji and the /// second element the English description of the emoji. - pub fn get_emoji(&self) -> Vec<(&'static str, &'static str)> { + pub fn get_emoji(&self) -> [(&'static str, &'static str); 7] { get_emoji( &self.inner.lock().unwrap(), &self.ids,