Serialize and hash custom predicates (#90)
* Print pods from SignedPodBuilder * Add additional print to test printing SignedPodBuilder * Mock-prove and print MainPod * Implement ToFields for custom predicates and dependencies * Test: print serialization of a recursive batch * Rearrange serialization of CustomPredicate so args_len is always in the same position * Serialize predicates with first entry nonzero to avoid collision with padding * Off by one error in ethdos test BatchSelf(2) * cargo fmt * not a typo * Typos, trying again
This commit is contained in:
parent
05c21ebe6a
commit
a37b96ab4f
8 changed files with 262 additions and 39 deletions
|
|
@ -171,13 +171,16 @@ fn resolve_wildcard(args: &[&str], priv_args: &[&str], v: &HashOrWildcardStr) ->
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::middleware::{CustomPredicateRef, PodType};
|
||||
use crate::middleware::{CustomPredicateRef, Params, PodType};
|
||||
|
||||
#[test]
|
||||
fn test_custom_pred() {
|
||||
use NativePredicate as NP;
|
||||
use StatementTmplBuilder as STB;
|
||||
|
||||
let params = Params::default();
|
||||
params.print_serialized_sizes();
|
||||
|
||||
let mut builder = CustomPredicateBatchBuilder::new("eth_friend".into());
|
||||
let _eth_friend = builder.predicate_and(
|
||||
// arguments:
|
||||
|
|
@ -239,7 +242,7 @@ mod tests {
|
|||
builder.predicates.last().unwrap()
|
||||
);
|
||||
|
||||
let eth_dos_distance = Predicate::BatchSelf(3);
|
||||
let eth_dos_distance = Predicate::BatchSelf(2);
|
||||
|
||||
// next chunk builds:
|
||||
let eth_dos_distance_ind = builder.predicate_and(
|
||||
|
|
@ -311,5 +314,9 @@ mod tests {
|
|||
"b.2. eth_dos_distance = {}",
|
||||
builder.predicates.last().unwrap()
|
||||
);
|
||||
|
||||
let eth_dos_batch_b = builder.finish();
|
||||
let fields = eth_dos_batch_b.to_fields(params);
|
||||
println!("Batch b, serialized: {:?}", fields);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,6 +96,16 @@ pub struct SignedPodBuilder {
|
|||
pub kvs: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
impl fmt::Display for SignedPodBuilder {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
writeln!(f, "SignedPodBuilder:")?;
|
||||
for (k, v) in self.kvs.iter().sorted_by_key(|kv| kv.0) {
|
||||
writeln!(f, " - {}: {}", k, v)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl SignedPodBuilder {
|
||||
pub fn new(params: &Params) -> Self {
|
||||
Self {
|
||||
|
|
@ -347,6 +357,22 @@ pub struct MainPod {
|
|||
// TODO: metadata
|
||||
}
|
||||
|
||||
impl fmt::Display for MainPod {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
writeln!(f, "MainPod: {}", self.pod.id())?;
|
||||
writeln!(f, " valid? {}", self.pod.verify())?;
|
||||
writeln!(f, " statements:")?;
|
||||
for st in &self.pod.pub_statements() {
|
||||
writeln!(f, " - {}", st)?;
|
||||
}
|
||||
writeln!(f, " kvs:")?;
|
||||
for (k, v) in &self.pod.kvs() {
|
||||
writeln!(f, " - {}: {}", k, v)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl MainPod {
|
||||
pub fn id(&self) -> PodId {
|
||||
self.pod.id()
|
||||
|
|
@ -487,6 +513,7 @@ pub mod build_utils {
|
|||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::backends::mock_main::MockProver;
|
||||
use crate::backends::mock_signed::MockSigner;
|
||||
use crate::examples::{
|
||||
great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder,
|
||||
|
|
@ -498,7 +525,8 @@ pub mod tests {
|
|||
let params = Params::default();
|
||||
let (gov_id, pay_stub) = zu_kyc_sign_pod_builders(¶ms);
|
||||
|
||||
// TODO: print pods from the builder
|
||||
println!("{}", gov_id);
|
||||
println!("{}", pay_stub);
|
||||
|
||||
let mut signer = MockSigner {
|
||||
pk: "ZooGov".into(),
|
||||
|
|
@ -515,7 +543,11 @@ pub mod tests {
|
|||
let kyc = zu_kyc_pod_builder(¶ms, &gov_id, &pay_stub)?;
|
||||
println!("{}", kyc);
|
||||
|
||||
let mut prover = MockProver {};
|
||||
let kyc = kyc.prove(&mut prover)?;
|
||||
|
||||
// TODO: prove kyc with MockProver and print it
|
||||
println!("{}", kyc);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue