mirror of
https://github.com/mii443/lamport_sigs.rs.git
synced 2025-08-22 15:05:49 +00:00
Add missing (Partial)Eq/Ord impls.
This commit is contained in:
62
src/lib.rs
62
src/lib.rs
@ -11,6 +11,7 @@
|
||||
extern crate ring;
|
||||
extern crate rand;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use rand::OsRng;
|
||||
use rand::Rng;
|
||||
@ -30,9 +31,29 @@ pub struct PublicKey {
|
||||
impl PartialEq for PublicKey {
|
||||
#[allow(trivial_casts)]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.zero_values == other.zero_values &&
|
||||
self.one_values == other.one_values &&
|
||||
self.algorithm as *const Algorithm == other.algorithm as *const Algorithm
|
||||
self.algorithm as *const Algorithm == other.algorithm as *const Algorithm &&
|
||||
self.zero_values == other.zero_values && self.one_values == other.one_values
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for PublicKey {}
|
||||
|
||||
impl PartialOrd for PublicKey {
|
||||
fn partial_cmp(&self, other: &PublicKey) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for PublicKey {
|
||||
#[allow(trivial_casts)]
|
||||
fn cmp(&self, other: &PublicKey) -> Ordering {
|
||||
self.zero_values
|
||||
.cmp(&other.zero_values)
|
||||
.then(self.one_values.cmp(&other.one_values))
|
||||
.then((self.algorithm as *const Algorithm).cmp(
|
||||
&(other.algorithm as
|
||||
*const Algorithm),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,8 +66,6 @@ impl Hash for PublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for PublicKey {}
|
||||
|
||||
/// A one-time signing private key
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PrivateKey {
|
||||
@ -104,10 +123,13 @@ impl PublicKey {
|
||||
|
||||
/// Serializes a public key into a byte vector
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
self.zero_values.iter().chain(self.one_values.iter()).fold(Vec::new(), |mut acc, i| {
|
||||
self.zero_values.iter().chain(self.one_values.iter()).fold(
|
||||
Vec::new(),
|
||||
|mut acc, i| {
|
||||
acc.append(&mut i.clone());
|
||||
acc
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Verifies that the signature of the data is correctly signed with the given key
|
||||
@ -232,12 +254,10 @@ impl PrivateKey {
|
||||
|
||||
impl Drop for PrivateKey {
|
||||
fn drop(&mut self) {
|
||||
let zeroize_vector = |vector: &mut Vec<Vec<u8>>| {
|
||||
for v2 in vector.iter_mut() {
|
||||
let zeroize_vector = |vector: &mut Vec<Vec<u8>>| for v2 in vector.iter_mut() {
|
||||
for byte in v2.iter_mut() {
|
||||
*byte = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
zeroize_vector(&mut self.zero_values);
|
||||
@ -257,7 +277,8 @@ impl PartialEq for PrivateKey {
|
||||
|
||||
for i in 0..self.zero_values.len() {
|
||||
if self.zero_values[i] != other.zero_values[i] ||
|
||||
self.one_values[i] != other.one_values[i] {
|
||||
self.one_values[i] != other.one_values[i]
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -265,5 +286,24 @@ impl PartialEq for PrivateKey {
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for PrivateKey {}
|
||||
|
||||
impl PartialOrd for PrivateKey {
|
||||
fn partial_cmp(&self, other: &PrivateKey) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for PrivateKey {
|
||||
// ⚠️ This is not a constant-time implementation
|
||||
fn cmp(&self, other: &PrivateKey) -> Ordering {
|
||||
self.one_values.cmp(&other.one_values).then(
|
||||
self.zero_values.cmp(
|
||||
&other.zero_values,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests;
|
||||
|
Reference in New Issue
Block a user