diff --git a/src/hashes/ternary/kerl/bigint/i384/mod.rs b/src/hashes/ternary/kerl/bigint/i384/mod.rs index 779787dc..303678f0 100644 --- a/src/hashes/ternary/kerl/bigint/i384/mod.rs +++ b/src/hashes/ternary/kerl/bigint/i384/mod.rs @@ -168,6 +168,12 @@ impl PartialEq for I384 { impl PartialOrd for I384 { fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for I384 { + fn cmp(&self, other: &Self) -> Ordering { use Ordering::*; let mut zipped_iter = self.inner.iter().zip(other.inner.iter()); @@ -183,25 +189,25 @@ impl PartialOrd for I384 { const UMAX: u8 = core::u8::MAX; let numbers_negative = match zipped_iter.next() { // Case 1: both numbers are negative, s is less. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Some(Greater), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Greater, // Case 2: both numbers are negative, s is greater. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Some(Less), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Less, // Case 3: both numbers are negative, but equal. Some((NEGBIT..=UMAX, NEGBIT..=UMAX)) => true, // Case 4: only s is negative. - Some((NEGBIT..=UMAX, _)) => return Some(Less), + Some((NEGBIT..=UMAX, _)) => return Less, // Case 5: only o is negative. - Some((_, NEGBIT..=UMAX)) => return Some(Greater), + Some((_, NEGBIT..=UMAX)) => return Greater, // Case 6: both are positive, s is greater. - Some((s, o)) if s > o => return Some(Greater), + Some((s, o)) if s > o => return Greater, // Case 7: both are positive, s is less. - Some((s, o)) if s < o => return Some(Less), + Some((s, o)) if s < o => return Less, // Fallthrough case; only happens if s == o. Some(_) => false, @@ -212,24 +218,13 @@ impl PartialOrd for I384 { for (s, o) in zipped_iter { match s.cmp(o) { - Ordering::Greater => return if numbers_negative { Some(Less) } else { Some(Greater) }, - Ordering::Less => return if numbers_negative { Some(Greater) } else { Some(Less) }, + Ordering::Greater => return if numbers_negative { Less } else { Greater }, + Ordering::Less => return if numbers_negative { Greater } else { Less }, Ordering::Equal => continue, } } - Some(Equal) - } -} - -impl Ord for I384 { - fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - - // The ordering is total, hence `partial_cmp` will never return `None`. - None => unreachable!(), - } + Equal } } @@ -401,23 +396,6 @@ impl From> for I384 { impl Ord for I384 { fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - - // The ordering is total, hence `partial_cmp` will never return `None`. - None => unreachable!(), - } - } -} - -impl PartialEq for I384 { - fn eq(&self, other: &Self) -> bool { - self.inner == other.inner - } -} - -impl PartialOrd for I384 { - fn partial_cmp(&self, other: &Self) -> Option { use Ordering::*; let mut zipped_iter = self.inner.iter().zip(other.inner.iter()); @@ -433,25 +411,25 @@ impl PartialOrd for I384 { const UMAX: u32 = core::u32::MAX; let numbers_negative = match zipped_iter.next() { // Case 1: both numbers are negative, s is less. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Some(Greater), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Greater, // Case 2: both numbers are negative, s is greater. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Some(Less), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Less, // Case 3: both numbers are negative, but equal. Some((NEGBIT..=UMAX, NEGBIT..=UMAX)) => true, // Case 4: only s is negative. - Some((NEGBIT..=UMAX, _)) => return Some(Less), + Some((NEGBIT..=UMAX, _)) => return Less, // Case 5: only o is negative. - Some((_, NEGBIT..=UMAX)) => return Some(Greater), + Some((_, NEGBIT..=UMAX)) => return Greater, // Case 6: both are positive, s is greater. - Some((s, o)) if s > o => return Some(Greater), + Some((s, o)) if s > o => return Greater, // Case 7: both are positive, s is less. - Some((s, o)) if s < o => return Some(Less), + Some((s, o)) if s < o => return Less, // Fallthrough case; only happens if s == o. Some(_) => false, @@ -462,13 +440,25 @@ impl PartialOrd for I384 { for (s, o) in zipped_iter { match s.cmp(o) { - Ordering::Greater => return if numbers_negative { Some(Less) } else { Some(Greater) }, - Ordering::Less => return if numbers_negative { Some(Greater) } else { Some(Less) }, + Ordering::Greater => return if numbers_negative { Less } else { Greater }, + Ordering::Less => return if numbers_negative { Greater } else { Less }, Ordering::Equal => continue, } } - Some(Equal) + Equal + } +} + +impl PartialEq for I384 { + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner + } +} + +impl PartialOrd for I384 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } } @@ -502,27 +492,6 @@ impl From> for I384 { impl Ord for I384 { fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - - // The ordering is total, hence `partial_cmp` will never return `None`. - None => unreachable!(), - } - } -} - -impl PartialEq for I384 { - fn eq(&self, other: &Self) -> bool { - let mut are_equal = true; - for (a, b) in self.inner.iter().zip(other.inner.iter()) { - are_equal &= a == b - } - are_equal - } -} - -impl PartialOrd for I384 { - fn partial_cmp(&self, other: &Self) -> Option { use Ordering::*; let mut zipped_iter = self.inner.iter().rev().zip(other.inner.iter().rev()); @@ -538,25 +507,25 @@ impl PartialOrd for I384 { const UMAX: u8 = core::u8::MAX; let numbers_negative = match zipped_iter.next() { // Case 1: both numbers are negative, s is less. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Some(Greater), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Greater, // Case 2: both numbers are negative, s is greater. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Some(Less), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Less, // Case 3: both numbers are negative, but equal. Some((NEGBIT..=UMAX, NEGBIT..=UMAX)) => true, // Case 4: only s is negative. - Some((NEGBIT..=UMAX, _)) => return Some(Less), + Some((NEGBIT..=UMAX, _)) => return Less, // Case 5: only o is negative. - Some((_, NEGBIT..=UMAX)) => return Some(Greater), + Some((_, NEGBIT..=UMAX)) => return Greater, // Case 6: both are positive, s is greater. - Some((s, o)) if s > o => return Some(Greater), + Some((s, o)) if s > o => return Greater, // Case 7: both are positive, s is less. - Some((s, o)) if s < o => return Some(Less), + Some((s, o)) if s < o => return Less, // Fallthrough case; only happens if s == o. Some(_) => false, @@ -567,13 +536,29 @@ impl PartialOrd for I384 { for (s, o) in zipped_iter { match s.cmp(o) { - Ordering::Greater => return if numbers_negative { Some(Less) } else { Some(Greater) }, - Ordering::Less => return if numbers_negative { Some(Greater) } else { Some(Less) }, + Ordering::Greater => return if numbers_negative { Less } else { Greater }, + Ordering::Less => return if numbers_negative { Greater } else { Less }, Ordering::Equal => continue, } } - Some(Equal) + Equal + } +} + +impl PartialEq for I384 { + fn eq(&self, other: &Self) -> bool { + let mut are_equal = true; + for (a, b) in self.inner.iter().zip(other.inner.iter()) { + are_equal &= a == b + } + are_equal + } +} + +impl PartialOrd for I384 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } } @@ -737,23 +722,6 @@ impl From> for I384 { impl Ord for I384 { fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - - // The ordering is total, hence `partial_cmp` will never return `None`. - None => unreachable!(), - } - } -} - -impl PartialEq for I384 { - fn eq(&self, other: &Self) -> bool { - self.inner == other.inner - } -} - -impl PartialOrd for I384 { - fn partial_cmp(&self, other: &Self) -> Option { use Ordering::*; let mut zipped_iter = self.inner.iter().rev().zip(other.inner.iter().rev()); @@ -770,25 +738,25 @@ impl PartialOrd for I384 { let numbers_negative = match zipped_iter.next() { // Case 1: both numbers are negative, s is less. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Some(Greater), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s > o => return Greater, // Case 2: both numbers are negative, s is greater. - Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Some(Less), + Some((s @ NEGBIT..=UMAX, o @ NEGBIT..=UMAX)) if s < o => return Less, // Case 3: both numbers are negative, but equal. Some((NEGBIT..=UMAX, NEGBIT..=UMAX)) => true, // Case 4: only s is negative. - Some((NEGBIT..=UMAX, _)) => return Some(Less), + Some((NEGBIT..=UMAX, _)) => return Less, // Case 5: only o is negative. - Some((_, NEGBIT..=UMAX)) => return Some(Greater), + Some((_, NEGBIT..=UMAX)) => return Greater, // Case 6: both are positive, s is greater. - Some((s, o)) if s > o => return Some(Greater), + Some((s, o)) if s > o => return Greater, // Case 7: both are positive, s is less. - Some((s, o)) if s < o => return Some(Less), + Some((s, o)) if s < o => return Less, // Fallthrough case; only happens if s == o and positive. Some(_) => false, @@ -799,13 +767,25 @@ impl PartialOrd for I384 { for (s, o) in zipped_iter { match s.cmp(o) { - Ordering::Greater => return if numbers_negative { Some(Less) } else { Some(Greater) }, - Ordering::Less => return if numbers_negative { Some(Greater) } else { Some(Less) }, + Ordering::Greater => return if numbers_negative { Less } else { Greater }, + Ordering::Less => return if numbers_negative { Greater } else { Less }, Ordering::Equal => continue, } } - Some(Equal) + Equal + } +} + +impl PartialEq for I384 { + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner + } +} + +impl PartialOrd for I384 { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } } diff --git a/src/hashes/ternary/kerl/bigint/macros.rs b/src/hashes/ternary/kerl/bigint/macros.rs index 042ec737..644fdcf0 100644 --- a/src/hashes/ternary/kerl/bigint/macros.rs +++ b/src/hashes/ternary/kerl/bigint/macros.rs @@ -126,24 +126,20 @@ macro_rules! def_and_impl_ternary { impl Ord for $ident { fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - // Cannot be reached because the order is total. - None => unreachable!(), - } - } - } - - impl PartialOrd for $ident { - fn partial_cmp(&self, other: &Self) -> Option { use Ordering::Equal; for (a, b) in self.0.iter().zip(other.0.iter()).rev() { match a.cmp(&b) { Equal => continue, - other_ordering => return Some(other_ordering), + other_ordering => return other_ordering, } } - Some(Equal) + Equal + } + } + + impl PartialOrd for $ident { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } } }; diff --git a/src/hashes/ternary/kerl/bigint/u384/mod.rs b/src/hashes/ternary/kerl/bigint/u384/mod.rs index 9805eb72..bfb68f02 100644 --- a/src/hashes/ternary/kerl/bigint/u384/mod.rs +++ b/src/hashes/ternary/kerl/bigint/u384/mod.rs @@ -431,29 +431,25 @@ impl PartialEq for U384 { impl PartialOrd for U384 { fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for U384 { + fn cmp(&self, other: &Self) -> Ordering { use Ordering::*; let zipped_iter = self.inner.iter().rev().zip(other.inner.iter().rev()); for (s, o) in zipped_iter { match s.cmp(o) { - Ordering::Greater => return Some(Greater), - Ordering::Less => return Some(Less), + Ordering::Greater => return Greater, + Ordering::Less => return Less, Ordering::Equal => continue, } } - Some(Equal) - } -} - -impl Ord for U384 { - fn cmp(&self, other: &Self) -> Ordering { - match self.partial_cmp(other) { - Some(ordering) => ordering, - // The ordering is total, hence `partial_cmp` will never return `None`. - None => unreachable!(), - } + Equal } } diff --git a/src/keys/age.rs b/src/keys/age.rs index 270e9b75..ff296ac1 100644 --- a/src/keys/age.rs +++ b/src/keys/age.rs @@ -629,8 +629,7 @@ pub fn enc_vec( nonce: &[u8; 16], plaintext: &[u8], ) -> Vec { - let mut age = Vec::new(); - age.resize(enc_len(work_factor, plaintext.len()), 0_u8); + let mut age = vec![0u8; enc_len(work_factor, plaintext.len())]; let h = enc_header(password, salt, file_key, work_factor.0, &mut age[..]); enc_payload(file_key, nonce, plaintext, &mut age[h..]); age @@ -703,8 +702,7 @@ pub fn decrypt_vec(password: &[u8], max_work_factor: u8, age: &[u8]) -> Result &str { - // SAFETY: MnemonicRef is represented exactly as str due to repr(transparent) - unsafe { core::mem::transmute(self) } + &self.0 } } diff --git a/src/keys/slip10.rs b/src/keys/slip10.rs index 9299f684..feb6ab59 100644 --- a/src/keys/slip10.rs +++ b/src/keys/slip10.rs @@ -119,8 +119,8 @@ pub mod secp256k1 { let sk_bytes: &[u8; 32] = unsafe { &*(key_bytes[1..].as_ptr() as *const [u8; 32]) }; if let Ok(sk_delta) = k256::SecretKey::from_bytes(sk_bytes.into()) { - let sk = k256::SecretKey::from_bytes((&parent_key[1..]).try_into().unwrap()) - .expect("valid Secp256k1 parent secret key"); + let sk = + k256::SecretKey::from_bytes((&parent_key[1..]).into()).expect("valid Secp256k1 parent secret key"); let scalar_delta = sk_delta.to_nonzero_scalar(); let mut scalar = *sk.to_nonzero_scalar().as_ref(); @@ -129,7 +129,7 @@ pub mod secp256k1 { if scalar.is_zero().into() { false } else { - key_bytes[1..].copy_from_slice(&scalar.to_bytes()); + key_bytes[1..].copy_from_slice(&*scalar.to_bytes()); true } } else {