This simplifies the MerkleTree (and container) API. Defer the max depth check when assigning the witness (merkle proof siblings) to the merkle tree circuit. In this implementation the native Merkle Tree branches grow as much as they needed. There are no checks of max depth in the merkle tree. All keys are 256 bits (I added a debug_assert for this); so in the worst case a path will have depth 256. It can't have a longer depth because the `insert` method calls `prove_nonexistence` which errors if the key already exists; another one may exist which must be different and thus require a path <= 256 depth. Resolve #436
42 lines
1.3 KiB
Rust
42 lines
1.3 KiB
Rust
#![allow(clippy::uninlined_format_args)] // TODO: Remove this in another PR
|
|
//! Simple example of building a signed dict and verifying it
|
|
//!
|
|
//! Run: `cargo run --release --example signed_dict`
|
|
use std::collections::HashSet;
|
|
|
|
use pod2::{
|
|
backends::plonky2::{primitives::ec::schnorr::SecretKey, signer::Signer},
|
|
frontend::SignedDictBuilder,
|
|
middleware::{containers::Set, Params, Value},
|
|
};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let params = Params::default();
|
|
|
|
// Create a schnorr key pair to sign the dict
|
|
let sk = SecretKey::new_rand();
|
|
let pk = sk.public_key();
|
|
println!("Public key: {}\n", pk);
|
|
|
|
let signer = Signer(sk);
|
|
|
|
// Build the signed dict
|
|
let mut builder = SignedDictBuilder::new(¶ms);
|
|
// The values can be String, i64, bool, Array, Set, Dictionary, ...
|
|
builder.insert("name", "Alice");
|
|
builder.insert("lucky_number", 42);
|
|
builder.insert("human", true);
|
|
let friends_set: HashSet<Value> = ["Bob", "Charlie", "Dave"]
|
|
.into_iter()
|
|
.map(Value::from)
|
|
.collect();
|
|
builder.insert("friends", Set::new(friends_set));
|
|
|
|
// Sign the dict and verify it
|
|
let signed_dict = builder.sign(&signer)?;
|
|
signed_dict.verify()?;
|
|
|
|
println!("{}", signed_dict);
|
|
|
|
Ok(())
|
|
}
|