Skip to content

Commit e1775d8

Browse files
authored
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
1 parent 256d76a commit e1775d8

File tree

10 files changed

+121
-9
lines changed

10 files changed

+121
-9
lines changed

src/backends/plonky2/basetypes.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ pub struct VDSet {
7777
vds_hashes: Vec<Hash>,
7878
}
7979

80+
impl PartialEq for VDSet {
81+
fn eq(&self, other: &Self) -> bool {
82+
self.root == other.root
83+
&& self.tree_depth == other.tree_depth
84+
&& self.vds_hashes == other.vds_hashes
85+
}
86+
}
87+
impl Eq for VDSet {}
88+
8089
impl VDSet {
8190
fn new_from_vds_hashes(tree_depth: usize, mut vds_hashes: Vec<Hash>) -> Result<Self> {
8291
// before using the hash values, sort them, so that each set of

src/backends/plonky2/emptypod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
any::Any,
23
collections::HashMap,
34
sync::{LazyLock, Mutex},
45
};
@@ -73,7 +74,7 @@ impl EmptyPodVerifyTarget {
7374
}
7475
}
7576

76-
#[derive(Clone, Debug)]
77+
#[derive(Clone, Debug, Eq, PartialEq)]
7778
pub struct EmptyPod {
7879
params: Params,
7980
id: PodId,
@@ -192,6 +193,17 @@ impl Pod for EmptyPod {
192193
})
193194
.expect("serialization to json")
194195
}
196+
197+
fn as_any(&self) -> &dyn Any {
198+
self
199+
}
200+
fn equals(&self, other: &dyn Pod) -> bool {
201+
if let Some(other) = other.as_any().downcast_ref::<EmptyPod>() {
202+
self == other
203+
} else {
204+
false
205+
}
206+
}
195207
}
196208

197209
impl RecursivePod for EmptyPod {

src/backends/plonky2/mainpod/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl PodProver for Prover {
561561
}
562562
}
563563

564-
#[derive(Clone, Debug)]
564+
#[derive(Clone, Debug, PartialEq, Eq)]
565565
pub struct MainPod {
566566
params: Params,
567567
id: PodId,
@@ -676,6 +676,17 @@ impl Pod for MainPod {
676676
})
677677
.expect("serialization to json")
678678
}
679+
680+
fn as_any(&self) -> &dyn Any {
681+
self
682+
}
683+
fn equals(&self, other: &dyn Pod) -> bool {
684+
if let Some(other) = other.as_any().downcast_ref::<MainPod>() {
685+
self == other
686+
} else {
687+
false
688+
}
689+
}
679690
}
680691

681692
impl RecursivePod for MainPod {

src/backends/plonky2/mainpod/statement.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::{
1010
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
1111
pub struct Statement(pub Predicate, pub Vec<StatementArg>);
1212

13+
impl Eq for Statement {}
14+
1315
impl Statement {
1416
pub fn is_none(&self) -> bool {
1517
self.0 == Predicate::Native(NativePredicate::None)

src/backends/plonky2/mock/emptypod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::any::Any;
2+
13
use itertools::Itertools;
24

35
use crate::{
@@ -12,7 +14,7 @@ use crate::{
1214
},
1315
};
1416

15-
#[derive(Clone, Debug)]
17+
#[derive(Clone, Debug, PartialEq, Eq)]
1618
pub struct MockEmptyPod {
1719
params: Params,
1820
id: PodId,
@@ -67,6 +69,17 @@ impl Pod for MockEmptyPod {
6769
fn serialize_data(&self) -> serde_json::Value {
6870
serde_json::Value::Null
6971
}
72+
73+
fn as_any(&self) -> &dyn Any {
74+
self
75+
}
76+
fn equals(&self, other: &dyn Pod) -> bool {
77+
if let Some(other) = other.as_any().downcast_ref::<MockEmptyPod>() {
78+
self == other
79+
} else {
80+
false
81+
}
82+
}
7083
}
7184

7285
impl RecursivePod for MockEmptyPod {

src/backends/plonky2/mock/mainpod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// MainPod
33
//
44

5-
use std::{fmt, iter};
5+
use std::{any::Any, fmt, iter};
66

77
use itertools::Itertools;
88
use serde::{Deserialize, Serialize};
@@ -56,6 +56,21 @@ pub struct MockMainPod {
5656
merkle_proofs_containers: Vec<MerkleClaimAndProof>,
5757
}
5858

59+
impl PartialEq for MockMainPod {
60+
fn eq(&self, other: &Self) -> bool {
61+
self.params == other.params
62+
&& self.id == other.id
63+
&& self.vd_set == other.vd_set
64+
&& self.input_signed_pods == other.input_signed_pods
65+
&& self.input_recursive_pods == other.input_recursive_pods
66+
&& self.statements == other.statements
67+
&& self.operations == other.operations
68+
&& self.public_statements == other.public_statements
69+
&& self.merkle_proofs_containers == other.merkle_proofs_containers
70+
}
71+
}
72+
impl Eq for MockMainPod {}
73+
5974
impl fmt::Display for MockMainPod {
6075
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6176
writeln!(f, "MockMainPod ({}):", self.id)?;
@@ -365,6 +380,17 @@ impl Pod for MockMainPod {
365380
})
366381
.expect("serialization to json")
367382
}
383+
384+
fn as_any(&self) -> &dyn Any {
385+
self
386+
}
387+
fn equals(&self, other: &dyn Pod) -> bool {
388+
if let Some(other) = other.as_any().downcast_ref::<MockMainPod>() {
389+
self == other
390+
} else {
391+
false
392+
}
393+
}
368394
}
369395

370396
impl RecursivePod for MockMainPod {

src/backends/plonky2/mock/signedpod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{any::Any, collections::HashMap};
22

33
use itertools::Itertools;
44
use serde::{Deserialize, Serialize};
@@ -45,7 +45,7 @@ impl PodSigner for MockSigner {
4545
}
4646
}
4747

48-
#[derive(Clone, Debug)]
48+
#[derive(Clone, Debug, PartialEq, Eq)]
4949
pub struct MockSignedPod {
5050
id: PodId,
5151
signature: String,
@@ -158,6 +158,17 @@ impl Pod for MockSignedPod {
158158
})
159159
.expect("serialization to json")
160160
}
161+
162+
fn as_any(&self) -> &dyn Any {
163+
self
164+
}
165+
fn equals(&self, other: &dyn Pod) -> bool {
166+
if let Some(other) = other.as_any().downcast_ref::<MockSignedPod>() {
167+
self == other
168+
} else {
169+
false
170+
}
171+
}
161172
}
162173

163174
#[cfg(test)]

src/backends/plonky2/primitives/ec/schnorr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
};
3636

3737
/// Schnorr signature over ecGFp5.
38-
#[derive(Clone, Debug)]
38+
#[derive(Clone, Debug, PartialEq, Eq)]
3939
pub struct Signature {
4040
pub s: BigUint,
4141
pub e: BigUint,

src/backends/plonky2/signedpod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, sync::LazyLock};
1+
use std::{any::Any, collections::HashMap, sync::LazyLock};
22

33
use itertools::Itertools;
44
use num_bigint::{BigUint, RandBigInt};
@@ -67,7 +67,7 @@ impl PodSigner for Signer {
6767
}
6868
}
6969

70-
#[derive(Clone, Debug)]
70+
#[derive(Clone, Debug, PartialEq, Eq)]
7171
pub struct SignedPod {
7272
pub id: PodId,
7373
pub signature: Signature,
@@ -204,6 +204,17 @@ impl Pod for SignedPod {
204204
})
205205
.expect("serialization to json")
206206
}
207+
208+
fn as_any(&self) -> &dyn Any {
209+
self
210+
}
211+
fn equals(&self, other: &dyn Pod) -> bool {
212+
if let Some(other) = other.as_any().downcast_ref::<SignedPod>() {
213+
self == other
214+
} else {
215+
false
216+
}
217+
}
207218
}
208219

209220
#[cfg(test)]

src/middleware/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,17 @@ pub trait Pod: fmt::Debug + DynClone + Any {
805805
})
806806
.collect()
807807
}
808+
809+
fn as_any(&self) -> &dyn Any;
810+
fn equals(&self, other: &dyn Pod) -> bool;
808811
}
812+
impl PartialEq for Box<dyn Pod> {
813+
fn eq(&self, other: &Self) -> bool {
814+
self.equals(&**other)
815+
}
816+
}
817+
818+
impl Eq for Box<dyn Pod> {}
809819

810820
// impl Clone for Box<dyn Pod>
811821
dyn_clone::clone_trait_object!(Pod);
@@ -828,6 +838,13 @@ pub trait RecursivePod: Pod {
828838
where
829839
Self: Sized;
830840
}
841+
impl PartialEq for Box<dyn RecursivePod> {
842+
fn eq(&self, other: &Self) -> bool {
843+
self.equals(&**other)
844+
}
845+
}
846+
847+
impl Eq for Box<dyn RecursivePod> {}
831848

832849
// impl Clone for Box<dyn RecursivePod>
833850
dyn_clone::clone_trait_object!(RecursivePod);

0 commit comments

Comments
 (0)