Skip to content

Commit

Permalink
implement Clone, PartialEq, Eq for most types
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead authored and swcurran committed Feb 1, 2024
1 parent d81b4d0 commit faa2352
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 57 deletions.
10 changes: 9 additions & 1 deletion src/amcl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ impl Debug for PointG1 {
}
}

impl Eq for PointG1 {}

#[cfg(feature = "serde")]
impl SerializableCryptoPrimitive for PointG1 {
fn name() -> &'static str {
Expand Down Expand Up @@ -352,6 +354,8 @@ impl Debug for PointG2 {
}
}

impl Eq for PointG2 {}

#[cfg(feature = "serde")]
impl SerializableCryptoPrimitive for PointG2 {
fn name() -> &'static str {
Expand Down Expand Up @@ -414,6 +418,8 @@ impl PointG2Inf {
}
}

impl Eq for PointG2Inf {}

impl From<PointG2> for PointG2Inf {
fn from(value: PointG2) -> Self {
Self(value)
Expand Down Expand Up @@ -452,7 +458,7 @@ impl SerializableCryptoPrimitive for PointG2Inf {
#[cfg(feature = "serde")]
serializable_crypto_primitive!(PointG2Inf);

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct GroupOrderElement {
bn: BIG,
}
Expand Down Expand Up @@ -714,6 +720,8 @@ impl Debug for Pair {
}
}

impl Eq for Pair {}

#[cfg(feature = "serde")]
impl SerializableCryptoPrimitive for Pair {
fn name() -> &'static str {
Expand Down
15 changes: 10 additions & 5 deletions src/bn/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,17 @@ impl BigNumber {
Ok(bn)
}

// Question: Why does this need to be a Result? When is creating a BigNumber same as another
// BigNumber not possible given sufficient memory?
pub fn try_clone(&self) -> ClResult<BigNumber> {
let mut openssl_bn = BigNum::from_slice(&self.openssl_bn.to_vec()[..])?;
openssl_bn.set_negative(self.is_negative());
Ok(BigNumber { openssl_bn })
Ok(Self {
openssl_bn: self.openssl_bn.to_owned()?,
})
}
}

impl Clone for BigNumber {
fn clone(&self) -> Self {
self.try_clone()
.expect("Error allocating memory for BigNumber")
}
}

Expand Down
1 change: 1 addition & 0 deletions src/bn/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};

use crate::error::{Error as ClError, Result as ClResult};

#[derive(Clone)]
pub struct BigNumber {
bn: BigInt,
}
Expand Down
Loading

0 comments on commit faa2352

Please sign in to comment.