Refactor frontend/middleware types (#194)
* unify fe/be NativeOp and NativePred * remove Origin in favour of PodId * Combine string and hash in Key * use middleware::AnchoredKey in frontend * merge frontend/middleware types * refactor custom predicates * clean up a bit * fix middleware custom tests * clean up * clean up 2 * add acronyms in typos list
This commit is contained in:
parent
9e860ef262
commit
c232c8dae5
33 changed files with 1985 additions and 2800 deletions
|
|
@ -1,10 +1,10 @@
|
|||
use std::{any::Any, fmt};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use base64::prelude::*;
|
||||
// use base64::prelude::*;
|
||||
use plonky2::{hash::poseidon::PoseidonHash, plonk::config::Hasher};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// use serde::{Deserialize, Serialize};
|
||||
use crate::{
|
||||
backends::plonky2::primitives::merkletree,
|
||||
middleware::{
|
||||
|
|
@ -27,7 +27,7 @@ impl PodProver for MockProver {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MockMainPod {
|
||||
params: Params,
|
||||
id: PodId,
|
||||
|
|
@ -199,7 +199,7 @@ impl MockMainPod {
|
|||
// Public statements
|
||||
assert!(inputs.public_statements.len() < params.max_public_statements);
|
||||
let mut type_st = middleware::Statement::ValueOf(
|
||||
AnchoredKey(SELF, hash_str(KEY_TYPE)),
|
||||
AnchoredKey::from((SELF, KEY_TYPE)),
|
||||
middleware::Value::from(PodType::MockMain),
|
||||
)
|
||||
.into();
|
||||
|
|
@ -235,9 +235,9 @@ impl MockMainPod {
|
|||
pf,
|
||||
) => Some(MerkleClaimAndProof::try_from_middleware(
|
||||
params,
|
||||
root,
|
||||
key,
|
||||
Some(value),
|
||||
&root.raw(),
|
||||
&key.raw(),
|
||||
Some(&value.raw()),
|
||||
pf,
|
||||
)),
|
||||
middleware::Operation::NotContainsFromEntries(
|
||||
|
|
@ -245,7 +245,11 @@ impl MockMainPod {
|
|||
middleware::Statement::ValueOf(_, key),
|
||||
pf,
|
||||
) => Some(MerkleClaimAndProof::try_from_middleware(
|
||||
params, root, key, None, pf,
|
||||
params,
|
||||
&root.raw(),
|
||||
&key.raw(),
|
||||
None,
|
||||
pf,
|
||||
)),
|
||||
_ => None,
|
||||
})
|
||||
|
|
@ -417,14 +421,14 @@ impl MockMainPod {
|
|||
fill_pad(args, OperationArg::None, params.max_operation_args)
|
||||
}
|
||||
|
||||
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))?;
|
||||
// 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))?;
|
||||
|
||||
Ok(pod)
|
||||
}
|
||||
// Ok(pod)
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn hash_statements(statements: &[Statement], _params: &Params) -> middleware::Hash {
|
||||
|
|
@ -449,8 +453,8 @@ impl Pod for MockMainPod {
|
|||
let has_type_statement = self.public_statements.iter().any(|s| {
|
||||
s.0 == Predicate::Native(NativePredicate::ValueOf)
|
||||
&& !s.1.is_empty()
|
||||
&& if let StatementArg::Key(AnchoredKey(pod_id, key_hash)) = s.1[0] {
|
||||
pod_id == SELF && key_hash == hash_str(KEY_TYPE)
|
||||
&& if let StatementArg::Key(AnchoredKey { pod_id, ref key }) = s.1[0] {
|
||||
pod_id == SELF && key.hash() == hash_str(KEY_TYPE)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
|
@ -477,7 +481,7 @@ impl Pod for MockMainPod {
|
|||
.filter(|(_, s)| s.0 == Predicate::Native(NativePredicate::ValueOf))
|
||||
.flat_map(|(i, s)| {
|
||||
if let StatementArg::Key(ak) = &s.1[0] {
|
||||
vec![(i, ak.1, ak.0)]
|
||||
vec![(i, ak.pod_id, ak.key.hash())]
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
|
|
@ -536,10 +540,10 @@ impl Pod for MockMainPod {
|
|||
.1
|
||||
.iter()
|
||||
.map(|sa| match &sa {
|
||||
StatementArg::Key(AnchoredKey(pod_id, h)) if *pod_id == SELF => {
|
||||
StatementArg::Key(AnchoredKey(self.id(), *h))
|
||||
StatementArg::Key(AnchoredKey { pod_id, key }) if *pod_id == SELF => {
|
||||
StatementArg::Key(AnchoredKey::new(self.id(), key.clone()))
|
||||
}
|
||||
_ => *sa,
|
||||
_ => sa.clone(),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
|
|
@ -557,7 +561,8 @@ impl Pod for MockMainPod {
|
|||
}
|
||||
|
||||
fn serialized_proof(&self) -> String {
|
||||
BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
|
||||
todo!()
|
||||
// BASE64_STANDARD.encode(serde_json::to_string(self).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -570,19 +575,14 @@ pub mod tests {
|
|||
great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder,
|
||||
zu_kyc_sign_pod_builders,
|
||||
},
|
||||
middleware,
|
||||
middleware::{self},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_mock_main_zu_kyc() -> Result<()> {
|
||||
let params = middleware::Params::default();
|
||||
let sanctions_values = ["A343434340"].map(|s| crate::frontend::Value::from(s));
|
||||
let sanction_set = crate::frontend::Value::Set(crate::frontend::containers::Set::new(
|
||||
sanctions_values.to_vec(),
|
||||
)?);
|
||||
|
||||
let (gov_id_builder, pay_stub_builder, sanction_list_builder) =
|
||||
zu_kyc_sign_pod_builders(¶ms, &sanction_set);
|
||||
zu_kyc_sign_pod_builders(¶ms);
|
||||
let mut signer = MockSigner {
|
||||
pk: "ZooGov".into(),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,17 +2,19 @@ use std::{fmt, iter};
|
|||
|
||||
use anyhow::{anyhow, Result};
|
||||
use plonky2::field::types::Field;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// use serde::{Deserialize, Serialize};
|
||||
use crate::{
|
||||
backends::plonky2::{
|
||||
mock::mainpod::Statement,
|
||||
primitives::merkletree::{self},
|
||||
},
|
||||
middleware::{self, Hash, OperationType, Params, ToFields, Value, EMPTY_HASH, EMPTY_VALUE, F},
|
||||
middleware::{
|
||||
self, Hash, OperationType, Params, RawValue, ToFields, EMPTY_HASH, EMPTY_VALUE, F,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum OperationArg {
|
||||
None,
|
||||
Index(usize),
|
||||
|
|
@ -34,7 +36,7 @@ impl OperationArg {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum OperationAux {
|
||||
None,
|
||||
MerkleProofIndex(usize),
|
||||
|
|
@ -50,17 +52,17 @@ impl ToFields for OperationAux {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MerkleClaimAndProof {
|
||||
pub enabled: bool,
|
||||
pub root: Hash,
|
||||
pub key: Value,
|
||||
pub value: Value,
|
||||
pub key: RawValue,
|
||||
pub value: RawValue,
|
||||
pub existence: bool,
|
||||
pub siblings: Vec<Hash>,
|
||||
pub case_ii_selector: bool,
|
||||
pub other_key: Value,
|
||||
pub other_value: Value,
|
||||
pub other_key: RawValue,
|
||||
pub other_value: RawValue,
|
||||
}
|
||||
|
||||
impl MerkleClaimAndProof {
|
||||
|
|
@ -68,7 +70,7 @@ impl MerkleClaimAndProof {
|
|||
Self {
|
||||
enabled: false,
|
||||
root: EMPTY_HASH,
|
||||
key: Value::from(1),
|
||||
key: RawValue::from(1),
|
||||
value: EMPTY_VALUE,
|
||||
existence: false,
|
||||
siblings: iter::repeat(EMPTY_HASH).take(max_depth).collect(),
|
||||
|
|
@ -79,9 +81,9 @@ impl MerkleClaimAndProof {
|
|||
}
|
||||
pub fn try_from_middleware(
|
||||
params: &Params,
|
||||
root: &Value,
|
||||
key: &Value,
|
||||
value: Option<&Value>,
|
||||
root: &RawValue,
|
||||
key: &RawValue,
|
||||
value: Option<&RawValue>,
|
||||
mid_mp: &merkletree::MerkleProof,
|
||||
) -> Result<Self> {
|
||||
if mid_mp.siblings.len() > params.max_depth_mt_gadget {
|
||||
|
|
@ -152,7 +154,7 @@ impl fmt::Display for MerkleClaimAndProof {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Operation(pub OperationType, pub Vec<OperationArg>, pub OperationAux);
|
||||
|
||||
impl Operation {
|
||||
|
|
@ -209,7 +211,7 @@ impl fmt::Display for Operation {
|
|||
}
|
||||
match self.2 {
|
||||
OperationAux::None => (),
|
||||
OperationAux::MerkleProofIndex(i) => write!(f, "merkle_proof_{:02}", i)?,
|
||||
OperationAux::MerkleProofIndex(i) => write!(f, " merkle_proof_{:02}", i)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
use std::fmt;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// use serde::{Deserialize, Serialize};
|
||||
use crate::middleware::{
|
||||
self, AnchoredKey, NativePredicate, Params, Predicate, StatementArg, ToFields,
|
||||
self, NativePredicate, Params, Predicate, StatementArg, ToFields, WildcardValue,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Statement(pub Predicate, pub Vec<StatementArg>);
|
||||
|
||||
impl Statement {
|
||||
|
|
@ -81,15 +81,15 @@ impl TryFrom<Statement> for middleware::Statement {
|
|||
_ => Err(anyhow!("Ill-formed statement expression {:?}", s))?,
|
||||
},
|
||||
Predicate::Custom(cpr) => {
|
||||
let aks: Vec<AnchoredKey> = proper_args
|
||||
let vs: Vec<WildcardValue> = proper_args
|
||||
.into_iter()
|
||||
.filter_map(|arg| match arg {
|
||||
SA::None => None,
|
||||
SA::Key(ak) => Some(ak),
|
||||
SA::Literal(_) => unreachable!(),
|
||||
SA::WildcardLiteral(v) => Some(v),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
S::Custom(cpr, aks)
|
||||
S::Custom(cpr, vs)
|
||||
}
|
||||
Predicate::BatchSelf(_) => {
|
||||
unreachable!()
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ use crate::{
|
|||
backends::plonky2::primitives::merkletree::MerkleTree,
|
||||
constants::MAX_DEPTH,
|
||||
middleware::{
|
||||
containers::Dictionary, hash_str, AnchoredKey, Hash, Params, Pod, PodId, PodSigner,
|
||||
PodType, Statement, Value, KEY_SIGNER, KEY_TYPE,
|
||||
containers::Dictionary, hash_str, AnchoredKey, Hash, Key, Params, Pod, PodId, PodSigner,
|
||||
PodType, RawValue, Statement, Value, KEY_SIGNER, KEY_TYPE,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -17,26 +17,22 @@ pub struct MockSigner {
|
|||
}
|
||||
|
||||
impl MockSigner {
|
||||
pub fn pubkey(&self) -> Value {
|
||||
Value(hash_str(&self.pk).0)
|
||||
pub fn pubkey(&self) -> Hash {
|
||||
hash_str(&self.pk)
|
||||
}
|
||||
}
|
||||
|
||||
impl PodSigner for MockSigner {
|
||||
fn sign(&mut self, _params: &Params, kvs: &HashMap<Hash, Value>) -> Result<Box<dyn Pod>> {
|
||||
fn sign(&mut self, _params: &Params, kvs: &HashMap<Key, Value>) -> Result<Box<dyn Pod>> {
|
||||
let mut kvs = kvs.clone();
|
||||
let pubkey = self.pubkey();
|
||||
kvs.insert(hash_str(KEY_SIGNER), pubkey);
|
||||
kvs.insert(hash_str(KEY_TYPE), Value::from(PodType::MockSigned));
|
||||
kvs.insert(Key::from(KEY_SIGNER), Value::from(pubkey));
|
||||
kvs.insert(Key::from(KEY_TYPE), Value::from(PodType::MockSigned));
|
||||
|
||||
let dict = Dictionary::new(&kvs)?;
|
||||
let dict = Dictionary::new(kvs.clone())?;
|
||||
let id = PodId(dict.commitment());
|
||||
let signature = format!("{}_signed_by_{}", id, pubkey);
|
||||
Ok(Box::new(MockSignedPod {
|
||||
dict,
|
||||
id,
|
||||
signature,
|
||||
}))
|
||||
Ok(Box::new(MockSignedPod { id, signature, kvs }))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,18 +40,18 @@ impl PodSigner for MockSigner {
|
|||
pub struct MockSignedPod {
|
||||
id: PodId,
|
||||
signature: String,
|
||||
dict: Dictionary,
|
||||
kvs: HashMap<Key, Value>,
|
||||
}
|
||||
|
||||
impl MockSignedPod {
|
||||
pub fn deserialize(id: PodId, signature: String, dict: Dictionary) -> Self {
|
||||
Self {
|
||||
id,
|
||||
signature,
|
||||
dict,
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl MockSignedPod {
|
||||
// pub fn deserialize(id: PodId, signature: String, dict: Dictionary) -> Self {
|
||||
// Self {
|
||||
// id,
|
||||
// signature,
|
||||
// dict,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Pod for MockSignedPod {
|
||||
fn verify(&self) -> Result<()> {
|
||||
|
|
@ -63,10 +59,10 @@ impl Pod for MockSignedPod {
|
|||
let mt = MerkleTree::new(
|
||||
MAX_DEPTH,
|
||||
&self
|
||||
.dict
|
||||
.kvs
|
||||
.iter()
|
||||
.map(|(&k, &v)| (k, v))
|
||||
.collect::<HashMap<Value, Value>>(),
|
||||
.map(|(k, v)| (k.raw(), v.raw()))
|
||||
.collect::<HashMap<RawValue, RawValue>>(),
|
||||
)?;
|
||||
let id = PodId(mt.root());
|
||||
if id != self.id {
|
||||
|
|
@ -78,8 +74,11 @@ impl Pod for MockSignedPod {
|
|||
}
|
||||
|
||||
// 2. Verify type
|
||||
let value_at_type = self.dict.get(&hash_str(KEY_TYPE).into())?;
|
||||
if Value::from(PodType::MockSigned) != value_at_type {
|
||||
let value_at_type = self
|
||||
.kvs
|
||||
.get(&Key::from(KEY_TYPE))
|
||||
.ok_or(anyhow!("key not found"))?;
|
||||
if &Value::from(PodType::MockSigned) != value_at_type {
|
||||
return Err(anyhow!(
|
||||
"type does not match, expected MockSigned ({}), found {}",
|
||||
PodType::MockSigned,
|
||||
|
|
@ -88,7 +87,10 @@ impl Pod for MockSignedPod {
|
|||
}
|
||||
|
||||
// 3. Verify signature
|
||||
let pk_hash = self.dict.get(&hash_str(KEY_SIGNER).into())?;
|
||||
let pk_hash = self
|
||||
.kvs
|
||||
.get(&Key::from(KEY_SIGNER))
|
||||
.ok_or(anyhow!("key not found"))?;
|
||||
let signature = format!("{}_signed_by_{}", id, pk_hash);
|
||||
if signature != self.signature {
|
||||
return Err(anyhow!(
|
||||
|
|
@ -108,15 +110,15 @@ impl Pod for MockSignedPod {
|
|||
fn pub_statements(&self) -> Vec<Statement> {
|
||||
let id = self.id();
|
||||
// By convention we put the KEY_TYPE first and KEY_SIGNER second
|
||||
let mut kvs: HashMap<_, _> = self.dict.iter().collect();
|
||||
let key_type = Value::from(hash_str(KEY_TYPE));
|
||||
let mut kvs = self.kvs.clone();
|
||||
let key_type = Key::from(KEY_TYPE);
|
||||
let value_type = kvs.remove(&key_type).expect("KEY_TYPE");
|
||||
let key_signer = Value::from(hash_str(KEY_SIGNER));
|
||||
let key_signer = Key::from(KEY_SIGNER);
|
||||
let value_signer = kvs.remove(&key_signer).expect("KEY_SIGNER");
|
||||
[(&key_type, value_type), (&key_signer, value_signer)]
|
||||
[(key_type, value_type), (key_signer, value_signer)]
|
||||
.into_iter()
|
||||
.chain(kvs.into_iter().sorted_by_key(|kv| kv.0))
|
||||
.map(|(k, v)| Statement::ValueOf(AnchoredKey(id, Hash(k.0)), *v))
|
||||
.chain(kvs.into_iter().sorted_by_key(|kv| kv.0.hash()))
|
||||
.map(|(k, v)| Statement::ValueOf(AnchoredKey::from((id, k)), v))
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
|
@ -140,9 +142,8 @@ pub mod tests {
|
|||
|
||||
use super::*;
|
||||
use crate::{
|
||||
constants::MAX_DEPTH,
|
||||
frontend,
|
||||
middleware::{self, EMPTY_HASH, F},
|
||||
middleware::{self, EMPTY_VALUE, F},
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
|
@ -170,27 +171,25 @@ pub mod tests {
|
|||
assert!(bad_pod.verify().is_err());
|
||||
|
||||
let mut bad_pod = pod.clone();
|
||||
let bad_kv = (hash_str(KEY_SIGNER).into(), Value(PodId(EMPTY_HASH).0 .0));
|
||||
let bad_kvs_mt = &bad_pod
|
||||
.kvs()
|
||||
let bad_kv = (Key::from(KEY_SIGNER), Value::from(EMPTY_VALUE));
|
||||
let bad_kvs = bad_pod
|
||||
.kvs
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(AnchoredKey(_, k), v)| (Value(k.0), v))
|
||||
.chain(iter::once(bad_kv))
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
let bad_mt = MerkleTree::new(MAX_DEPTH, bad_kvs_mt)?;
|
||||
bad_pod.dict.mt = bad_mt;
|
||||
.collect::<HashMap<Key, Value>>();
|
||||
bad_pod.kvs = bad_kvs;
|
||||
assert!(bad_pod.verify().is_err());
|
||||
|
||||
let mut bad_pod = pod.clone();
|
||||
let bad_kv = (hash_str(KEY_TYPE).into(), Value::from(0));
|
||||
let bad_kvs_mt = &bad_pod
|
||||
.kvs()
|
||||
let bad_kv = (Key::from(KEY_TYPE), Value::from(0));
|
||||
let bad_kvs = bad_pod
|
||||
.kvs
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(AnchoredKey(_, k), v)| (Value(k.0), v))
|
||||
.chain(iter::once(bad_kv))
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
let bad_mt = MerkleTree::new(MAX_DEPTH, bad_kvs_mt)?;
|
||||
bad_pod.dict.mt = bad_mt;
|
||||
.collect::<HashMap<Key, Value>>();
|
||||
bad_pod.kvs = bad_kvs;
|
||||
assert!(bad_pod.verify().is_err());
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue