implement the specified sparse merkletree (#82)
* wip * prototype custom predicates 1b * feat: implement custom pred recursion * files reorg, add github CI for rustfmt checks * start sparsemerkletree. impl add_leaf method, initial Leaf & Intermediate types with methods * mt: add hash computation of all the nodes in the tree, add method to print the tree to visualize it as a graphviz * mt: add (till the leaf) method which is used by get,contains,prove methods * mt: add verify (of inclusion) method * mt: update 'down' method to reuse siblings, update get,contains,prove methods (the three use 'down' under the hood) * Add nonexistence proofs and iterator * Add iterator test * migrate usage of old merkletree to the new merkletree impl in POD2 code --------- Co-authored-by: Eduard S. <eduardsanou@posteo.net> Co-authored-by: Ahmad <root@ahmadafuni.com>
This commit is contained in:
parent
2e9719a1ca
commit
c101d94530
9 changed files with 649 additions and 198 deletions
|
|
@ -2,6 +2,7 @@ use anyhow::Result;
|
|||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::constants::MAX_DEPTH;
|
||||
use crate::middleware::{
|
||||
containers::Dictionary, hash_str, AnchoredKey, Hash, Params, Pod, PodId, PodSigner, PodType,
|
||||
Statement, Value, KEY_SIGNER, KEY_TYPE,
|
||||
|
|
@ -19,7 +20,7 @@ impl PodSigner for MockSigner {
|
|||
kvs.insert(hash_str(&KEY_SIGNER), Value(pk_hash.0));
|
||||
kvs.insert(hash_str(&KEY_TYPE), Value::from(PodType::MockSigned));
|
||||
|
||||
let dict = Dictionary::new(&kvs);
|
||||
let dict = Dictionary::new(&kvs)?;
|
||||
let id = PodId(dict.commitment());
|
||||
let signature = format!("{}_signed_by_{}", id, pk_hash);
|
||||
Ok(Box::new(MockSignedPod {
|
||||
|
|
@ -49,13 +50,17 @@ impl Pod for MockSignedPod {
|
|||
}
|
||||
|
||||
// Verify id
|
||||
let mt = MerkleTree::new(
|
||||
let mt = match MerkleTree::new(
|
||||
MAX_DEPTH,
|
||||
&self
|
||||
.dict
|
||||
.iter()
|
||||
.map(|(&k, &v)| (k, v))
|
||||
.collect::<HashMap<Value, Value>>(),
|
||||
);
|
||||
) {
|
||||
Ok(mt) => mt,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let id = PodId(mt.root());
|
||||
if id != self.id {
|
||||
return false;
|
||||
|
|
@ -93,14 +98,16 @@ impl Pod for MockSignedPod {
|
|||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::frontend;
|
||||
use crate::middleware::{self, F, NULL};
|
||||
use plonky2::field::types::Field;
|
||||
use std::iter;
|
||||
|
||||
use super::*;
|
||||
use crate::constants::MAX_DEPTH;
|
||||
use crate::frontend;
|
||||
use crate::middleware::{self, F, NULL};
|
||||
|
||||
#[test]
|
||||
fn test_mock_signed_0() {
|
||||
fn test_mock_signed_0() -> Result<()> {
|
||||
let params = middleware::Params::default();
|
||||
let mut pod = frontend::SignedPodBuilder::new(¶ms);
|
||||
pod.insert("idNumber", "4242424242");
|
||||
|
|
@ -131,7 +138,7 @@ pub mod tests {
|
|||
.map(|(AnchoredKey(_, k), v)| (Value(k.0), v))
|
||||
.chain(iter::once(bad_kv))
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
let bad_mt = MerkleTree::new(&bad_kvs_mt);
|
||||
let bad_mt = MerkleTree::new(MAX_DEPTH, &bad_kvs_mt)?;
|
||||
bad_pod.dict.mt = bad_mt;
|
||||
assert_eq!(bad_pod.verify(), false);
|
||||
|
||||
|
|
@ -143,8 +150,10 @@ pub mod tests {
|
|||
.map(|(AnchoredKey(_, k), v)| (Value(k.0), v))
|
||||
.chain(iter::once(bad_kv))
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
let bad_mt = MerkleTree::new(&bad_kvs_mt);
|
||||
let bad_mt = MerkleTree::new(MAX_DEPTH, &bad_kvs_mt)?;
|
||||
bad_pod.dict.mt = bad_mt;
|
||||
assert_eq!(bad_pod.verify(), false);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue