add eq & partial_eq for RecursivePod, Pod traits and VDSet struct (for usage from introduction-pods) (#309)

* add eq & partial_eq for VDSet (for usage from introduction-pods)

* add eq & partial_eq impls for Pod & RecursivePod traits
This commit is contained in:
arnaucube 2025-06-24 17:41:46 +02:00 committed by GitHub
parent 256d76ae34
commit e1775d8578
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 121 additions and 9 deletions

View file

@ -77,6 +77,15 @@ pub struct VDSet {
vds_hashes: Vec<Hash>, vds_hashes: Vec<Hash>,
} }
impl PartialEq for VDSet {
fn eq(&self, other: &Self) -> bool {
self.root == other.root
&& self.tree_depth == other.tree_depth
&& self.vds_hashes == other.vds_hashes
}
}
impl Eq for VDSet {}
impl VDSet { impl VDSet {
fn new_from_vds_hashes(tree_depth: usize, mut vds_hashes: Vec<Hash>) -> Result<Self> { fn new_from_vds_hashes(tree_depth: usize, mut vds_hashes: Vec<Hash>) -> Result<Self> {
// before using the hash values, sort them, so that each set of // before using the hash values, sort them, so that each set of

View file

@ -1,4 +1,5 @@
use std::{ use std::{
any::Any,
collections::HashMap, collections::HashMap,
sync::{LazyLock, Mutex}, sync::{LazyLock, Mutex},
}; };
@ -73,7 +74,7 @@ impl EmptyPodVerifyTarget {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct EmptyPod { pub struct EmptyPod {
params: Params, params: Params,
id: PodId, id: PodId,
@ -192,6 +193,17 @@ impl Pod for EmptyPod {
}) })
.expect("serialization to json") .expect("serialization to json")
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<EmptyPod>() {
self == other
} else {
false
}
}
} }
impl RecursivePod for EmptyPod { impl RecursivePod for EmptyPod {

View file

@ -561,7 +561,7 @@ impl PodProver for Prover {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct MainPod { pub struct MainPod {
params: Params, params: Params,
id: PodId, id: PodId,
@ -676,6 +676,17 @@ impl Pod for MainPod {
}) })
.expect("serialization to json") .expect("serialization to json")
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<MainPod>() {
self == other
} else {
false
}
}
} }
impl RecursivePod for MainPod { impl RecursivePod for MainPod {

View file

@ -10,6 +10,8 @@ use crate::{
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Statement(pub Predicate, pub Vec<StatementArg>); pub struct Statement(pub Predicate, pub Vec<StatementArg>);
impl Eq for Statement {}
impl Statement { impl Statement {
pub fn is_none(&self) -> bool { pub fn is_none(&self) -> bool {
self.0 == Predicate::Native(NativePredicate::None) self.0 == Predicate::Native(NativePredicate::None)

View file

@ -1,3 +1,5 @@
use std::any::Any;
use itertools::Itertools; use itertools::Itertools;
use crate::{ use crate::{
@ -12,7 +14,7 @@ use crate::{
}, },
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct MockEmptyPod { pub struct MockEmptyPod {
params: Params, params: Params,
id: PodId, id: PodId,
@ -67,6 +69,17 @@ impl Pod for MockEmptyPod {
fn serialize_data(&self) -> serde_json::Value { fn serialize_data(&self) -> serde_json::Value {
serde_json::Value::Null serde_json::Value::Null
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<MockEmptyPod>() {
self == other
} else {
false
}
}
} }
impl RecursivePod for MockEmptyPod { impl RecursivePod for MockEmptyPod {

View file

@ -2,7 +2,7 @@
// MainPod // MainPod
// //
use std::{fmt, iter}; use std::{any::Any, fmt, iter};
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -56,6 +56,21 @@ pub struct MockMainPod {
merkle_proofs_containers: Vec<MerkleClaimAndProof>, merkle_proofs_containers: Vec<MerkleClaimAndProof>,
} }
impl PartialEq for MockMainPod {
fn eq(&self, other: &Self) -> bool {
self.params == other.params
&& self.id == other.id
&& self.vd_set == other.vd_set
&& self.input_signed_pods == other.input_signed_pods
&& self.input_recursive_pods == other.input_recursive_pods
&& self.statements == other.statements
&& self.operations == other.operations
&& self.public_statements == other.public_statements
&& self.merkle_proofs_containers == other.merkle_proofs_containers
}
}
impl Eq for MockMainPod {}
impl fmt::Display for MockMainPod { impl fmt::Display for MockMainPod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "MockMainPod ({}):", self.id)?; writeln!(f, "MockMainPod ({}):", self.id)?;
@ -365,6 +380,17 @@ impl Pod for MockMainPod {
}) })
.expect("serialization to json") .expect("serialization to json")
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<MockMainPod>() {
self == other
} else {
false
}
}
} }
impl RecursivePod for MockMainPod { impl RecursivePod for MockMainPod {

View file

@ -1,4 +1,4 @@
use std::collections::HashMap; use std::{any::Any, collections::HashMap};
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -45,7 +45,7 @@ impl PodSigner for MockSigner {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct MockSignedPod { pub struct MockSignedPod {
id: PodId, id: PodId,
signature: String, signature: String,
@ -158,6 +158,17 @@ impl Pod for MockSignedPod {
}) })
.expect("serialization to json") .expect("serialization to json")
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<MockSignedPod>() {
self == other
} else {
false
}
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -35,7 +35,7 @@ use crate::{
}; };
/// Schnorr signature over ecGFp5. /// Schnorr signature over ecGFp5.
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Signature { pub struct Signature {
pub s: BigUint, pub s: BigUint,
pub e: BigUint, pub e: BigUint,

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::LazyLock}; use std::{any::Any, collections::HashMap, sync::LazyLock};
use itertools::Itertools; use itertools::Itertools;
use num_bigint::{BigUint, RandBigInt}; use num_bigint::{BigUint, RandBigInt};
@ -67,7 +67,7 @@ impl PodSigner for Signer {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignedPod { pub struct SignedPod {
pub id: PodId, pub id: PodId,
pub signature: Signature, pub signature: Signature,
@ -204,6 +204,17 @@ impl Pod for SignedPod {
}) })
.expect("serialization to json") .expect("serialization to json")
} }
fn as_any(&self) -> &dyn Any {
self
}
fn equals(&self, other: &dyn Pod) -> bool {
if let Some(other) = other.as_any().downcast_ref::<SignedPod>() {
self == other
} else {
false
}
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -805,7 +805,17 @@ pub trait Pod: fmt::Debug + DynClone + Any {
}) })
.collect() .collect()
} }
fn as_any(&self) -> &dyn Any;
fn equals(&self, other: &dyn Pod) -> bool;
} }
impl PartialEq for Box<dyn Pod> {
fn eq(&self, other: &Self) -> bool {
self.equals(&**other)
}
}
impl Eq for Box<dyn Pod> {}
// impl Clone for Box<dyn Pod> // impl Clone for Box<dyn Pod>
dyn_clone::clone_trait_object!(Pod); dyn_clone::clone_trait_object!(Pod);
@ -828,6 +838,13 @@ pub trait RecursivePod: Pod {
where where
Self: Sized; Self: Sized;
} }
impl PartialEq for Box<dyn RecursivePod> {
fn eq(&self, other: &Self) -> bool {
self.equals(&**other)
}
}
impl Eq for Box<dyn RecursivePod> {}
// impl Clone for Box<dyn RecursivePod> // impl Clone for Box<dyn RecursivePod>
dyn_clone::clone_trait_object!(RecursivePod); dyn_clone::clone_trait_object!(RecursivePod);