Skip to content

make Pod derive from Any #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/backends/plonky2/circuits/signedpod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ impl SignedPodVerifyTarget {

#[cfg(test)]
pub mod tests {
use std::any::Any;

use plonky2::plonk::{circuit_builder::CircuitBuilder, circuit_data::CircuitConfig};

use super::*;
Expand Down Expand Up @@ -218,7 +220,7 @@ pub mod tests {
let sk = SecretKey::new_rand();
let mut signer = Signer(sk);
let pod = pod.sign(&mut signer).unwrap();
let signed_pod = pod.pod.into_any().downcast::<SignedPod>().unwrap();
let signed_pod = (pod.pod as Box<dyn Any>).downcast::<SignedPod>().unwrap();

// use the pod in the circuit
let config = CircuitConfig::standard_recursion_config();
Expand Down
18 changes: 6 additions & 12 deletions src/backends/plonky2/mainpod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ impl PodProver for Prover {
.signed_pods
.iter()
.map(|p| {
let p = p
.as_any()
let p = (*p as &dyn Any)
.downcast_ref::<SignedPod>()
.expect("type SignedPod");
p.clone()
Expand Down Expand Up @@ -404,13 +403,6 @@ impl Pod for MainPod {
.collect()
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}

fn serialized_proof(&self) -> String {
todo!()
}
Expand Down Expand Up @@ -453,7 +445,7 @@ pub mod tests {

let mut prover = Prover {};
let kyc_pod = kyc_builder.prove(&mut prover, &params)?;
let pod = kyc_pod.pod.into_any().downcast::<MainPod>().unwrap();
let pod = (kyc_pod.pod as Box<dyn Any>).downcast::<MainPod>().unwrap();

pod.verify()
}
Expand Down Expand Up @@ -488,14 +480,16 @@ pub mod tests {
// Mock
let mut prover = MockProver {};
let kyc_pod = kyc_builder.prove(&mut prover, &params).unwrap();
let pod = kyc_pod.pod.into_any().downcast::<MockMainPod>().unwrap();
let pod = (kyc_pod.pod as Box<dyn Any>)
.downcast::<MockMainPod>()
.unwrap();
pod.verify().unwrap();
println!("{:#}", pod);

// Real
let mut prover = Prover {};
let kyc_pod = kyc_builder.prove(&mut prover, &params).unwrap();
let pod = kyc_pod.pod.into_any().downcast::<MainPod>().unwrap();
let pod = (kyc_pod.pod as Box<dyn Any>).downcast::<MainPod>().unwrap();
pod.verify().unwrap()
}
}
23 changes: 10 additions & 13 deletions src/backends/plonky2/mock/mainpod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MainPod
//

use std::{any::Any, fmt};
use std::fmt;

use anyhow::{anyhow, Result};

Expand Down Expand Up @@ -274,13 +274,6 @@ impl Pod for MockMainPod {
.collect()
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}

fn serialized_proof(&self) -> String {
todo!()
// BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
Expand All @@ -289,6 +282,8 @@ impl Pod for MockMainPod {

#[cfg(test)]
pub mod tests {
use std::any::Any;

use super::*;
use crate::{
backends::plonky2::mock::signedpod::MockSigner,
Expand Down Expand Up @@ -321,7 +316,9 @@ pub mod tests {

let mut prover = MockProver {};
let kyc_pod = kyc_builder.prove(&mut prover, &params)?;
let pod = kyc_pod.pod.into_any().downcast::<MockMainPod>().unwrap();
let pod = (kyc_pod.pod as Box<dyn Any>)
.downcast::<MockMainPod>()
.unwrap();

println!("{:#}", pod);

Expand All @@ -338,9 +335,7 @@ pub mod tests {

let mut prover = MockProver {};
let great_boy_pod = great_boy_builder.prove(&mut prover, &params)?;
let pod = great_boy_pod
.pod
.into_any()
let pod = (great_boy_pod.pod as Box<dyn Any>)
.downcast::<MockMainPod>()
.unwrap();

Expand All @@ -357,7 +352,9 @@ pub mod tests {
let tickets_builder = tickets_pod_full_flow()?;
let mut prover = MockProver {};
let proof_pod = tickets_builder.prove(&mut prover, &params)?;
let pod = proof_pod.pod.into_any().downcast::<MockMainPod>().unwrap();
let pod = (proof_pod.pod as Box<dyn Any>)
.downcast::<MockMainPod>()
.unwrap();

println!("{}", pod);
pod.verify()?;
Expand Down
15 changes: 5 additions & 10 deletions src/backends/plonky2/mock/signedpod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, collections::HashMap};
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use itertools::Itertools;
Expand Down Expand Up @@ -122,21 +122,14 @@ impl Pod for MockSignedPod {
.collect()
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}

fn serialized_proof(&self) -> String {
self.signature.to_string()
}
}

#[cfg(test)]
pub mod tests {
use std::iter;
use std::{any::Any, iter};

use plonky2::field::types::Field;

Expand All @@ -156,7 +149,9 @@ pub mod tests {

let mut signer = MockSigner { pk: "Molly".into() };
let pod = pod.sign(&mut signer).unwrap();
let pod = pod.pod.into_any().downcast::<MockSignedPod>().unwrap();
let pod = (pod.pod as Box<dyn Any>)
.downcast::<MockSignedPod>()
.unwrap();

pod.verify()?;
println!("id: {}", pod.id());
Expand Down
13 changes: 3 additions & 10 deletions src/backends/plonky2/signedpod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, collections::HashMap};
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use itertools::Itertools;
Expand Down Expand Up @@ -101,13 +101,6 @@ impl Pod for SignedPod {
.collect()
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}

fn serialized_proof(&self) -> String {
let mut buffer = Vec::new();
use plonky2::util::serialization::Write;
Expand All @@ -118,7 +111,7 @@ impl Pod for SignedPod {

#[cfg(test)]
pub mod tests {
use std::iter;
use std::{any::Any, iter};

use plonky2::field::types::Field;

Expand All @@ -140,7 +133,7 @@ pub mod tests {
let sk = SecretKey::new_rand();
let mut signer = Signer(sk);
let pod = pod.sign(&mut signer).unwrap();
let pod = pod.pod.into_any().downcast::<SignedPod>().unwrap();
let pod = (pod.pod as Box<dyn Any>).downcast::<SignedPod>().unwrap();

pod.verify()?;
println!("id: {}", pod.id());
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(clippy::get_first)]
#![feature(trait_upcasting)]

pub mod backends;
pub mod constants;
Expand Down
11 changes: 1 addition & 10 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl Params {
}
}

pub trait Pod: fmt::Debug + DynClone {
pub trait Pod: fmt::Debug + DynClone + Any {
fn verify(&self) -> Result<()>;
fn id(&self) -> PodId;
fn pub_statements(&self) -> Vec<Statement>;
Expand All @@ -501,9 +501,6 @@ pub trait Pod: fmt::Debug + DynClone {
})
.collect()
}
// Used for downcasting
fn into_any(self: Box<Self>) -> Box<dyn Any>;
fn as_any(&self) -> &dyn Any;
// Front-end Pods keep references to middleware Pods. Most of the
// middleware data can be derived directly from front-end data, but the
// "proof" data is only created at the point of proving/signing, and
Expand Down Expand Up @@ -537,12 +534,6 @@ impl Pod for NonePod {
fn pub_statements(&self) -> Vec<Statement> {
Vec::new()
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}
fn serialized_proof(&self) -> String {
"".to_string()
}
Expand Down
Loading