Re-implement serialization (#201)

* Serialization tests now pass again

* Tidy up and test more edge-cases

* Use attributes rather than custom serializer for arrays

* Add JSON Schema support

* Tests for JSON Schema generation and validation

* Add comments

* Support custom predicates

* Clippy fixes

* Make deserialization/constructor functions pub(crate)
This commit is contained in:
Rob Knight 2025-04-22 04:19:20 -07:00 committed by GitHub
parent 26a6b2d143
commit bf6d8aee8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 554 additions and 255 deletions

View file

@ -5,15 +5,15 @@
use std::{any::Any, fmt};
use anyhow::{anyhow, Result};
use base64::{prelude::BASE64_STANDARD, Engine};
use serde::{Deserialize, Serialize};
// use base64::prelude::*;
// use serde::{Deserialize, Serialize};
use crate::backends::plonky2::mainpod::process_private_statements_operations;
use crate::{
backends::plonky2::{
mainpod::{
extract_merkle_proofs, hash_statements, layout_statements, normalize_statement,
process_public_statements_operations, Operation, Statement,
process_private_statements_operations, process_public_statements_operations, Operation,
Statement,
},
primitives::merkletree::MerkleClaimAndProof,
},
@ -31,7 +31,7 @@ impl PodProver for MockProver {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MockMainPod {
params: Params,
id: PodId,
@ -166,14 +166,17 @@ impl MockMainPod {
})
}
// pub fn deserialize(serialized: String) -> Result<Self> {
// let proof = String::from_utf8(BASE64_STANDARD.decode(&serialized)?)
// .map_err(|e| anyhow::anyhow!("Invalid base64 encoding: {}", e))?;
// let pod: MockMainPod = serde_json::from_str(&proof)
// .map_err(|e| anyhow::anyhow!("Failed to parse proof: {}", e))?;
// MockMainPods include some internal private state which is necessary
// for verification. In non-mock Pods, this state will not be necessary,
// as the public statements can be verified using a ZK proof.
pub(crate) fn deserialize(serialized: String) -> Result<Self> {
let proof = String::from_utf8(BASE64_STANDARD.decode(&serialized)?)
.map_err(|e| anyhow::anyhow!("Invalid base64 encoding: {}", e))?;
let pod: MockMainPod = serde_json::from_str(&proof)
.map_err(|e| anyhow::anyhow!("Failed to parse proof: {}", e))?;
// Ok(pod)
// }
Ok(pod)
}
}
impl Pod for MockMainPod {
@ -282,8 +285,7 @@ impl Pod for MockMainPod {
}
fn serialized_proof(&self) -> String {
todo!()
// BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
}
}