Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce module in trie-db for generating/verifying trie proofs. #45

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@

core/trie/.criterion/
**/.criterion/

**/fuzz/artifacts/
**/fuzz/corpus/
9 changes: 5 additions & 4 deletions test-support/reference-trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::marker::PhantomData;
use std::ops::Range;
use parity_scale_codec::{Decode, Input, Output, Encode, Compact, Error as CodecError};
use trie_root::Hasher;

use trie_db::{
node::{NibbleSlicePlan, NodePlan, NodeHandlePlan},
triedbmut::ChildReference,
Expand All @@ -33,11 +34,11 @@ use std::borrow::Borrow;
use keccak_hasher::KeccakHasher;

pub use trie_db::{
Trie, TrieMut, TrieDB, TrieDBMut, TrieError, TrieIterator, TrieDBNodeIterator,
NibbleSlice, NibbleVec, NodeCodec, Recorder,
encode_compact, decode_compact,
decode_compact, encode_compact,
nibble_ops, NibbleSlice, NibbleVec, NodeCodec, proof, Record, Recorder,
Trie, TrieConfiguration, TrieDB, TrieDBMut, TrieDBNodeIterator, TrieError, TrieIterator,
TrieLayout, TrieMut,
};
pub use trie_db::{Record, TrieLayout, TrieConfiguration, nibble_ops};
pub use trie_root::TrieStream;
pub mod node {
pub use trie_db::node::Node;
Expand Down
56 changes: 48 additions & 8 deletions trie-db/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@

#[macro_use]
extern crate criterion;
use criterion::{Criterion, black_box, Bencher};
extern crate memory_db;
extern crate rand;
extern crate trie_db;
extern crate trie_standardmap;

use criterion::{Bencher, black_box, Criterion};

use trie_db::{NibbleSlice, proof::{generate_proof, verify_proof}, Trie};
use trie_standardmap::{Alphabet, StandardMap, ValueMode};

criterion_group!(benches,
root_old,
root_new,
Expand All @@ -32,16 +41,10 @@ criterion_group!(benches,
trie_mut_build_b,
trie_iteration,
nibble_common_prefix,
trie_proof_verification,
);
criterion_main!(benches);

extern crate trie_standardmap;
extern crate trie_db;
extern crate memory_db;
extern crate rand;
use trie_standardmap::{Alphabet, StandardMap, ValueMode};
use trie_db::NibbleSlice;

fn nibble_common_prefix(b: &mut Criterion) {
let st = StandardMap {
alphabet: Alphabet::Custom(b"abcd".to_vec()),
Expand Down Expand Up @@ -463,3 +466,40 @@ fn trie_iteration(c: &mut Criterion) {
})
);
}

fn trie_proof_verification(c: &mut Criterion) {
use memory_db::HashKey;

let mut data = input_unsorted(29, 204800, 32);
let mut keys = data[(data.len() / 3)..]
.iter()
.map(|(key, _)| key.clone())
.collect::<Vec<_>>();
data.truncate(data.len() * 2 / 3);

let data = data_sorted_unique(data);
keys.sort();
keys.dedup();

let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default();
let root = reference_trie::calc_root_build(data, &mut mdb);

let trie = reference_trie::RefTrieDB::new(&mdb, &root).unwrap();
let proof = generate_proof(&trie, keys.iter()).unwrap();
let items = keys.into_iter()
.map(|key| {
let value = trie.get(&key).unwrap();
(key, value)
})
.collect::<Vec<_>>();

c.bench_function("trie_proof_verification", move |b: &mut Bencher|
b.iter(|| {
verify_proof::<reference_trie::ExtensionLayout, _, _, _>(
&root,
&proof,
items.iter()
).unwrap();
})
);
}
8 changes: 8 additions & 0 deletions trie-db/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ path = "fuzz_targets/no_ext_insert.rs"
[[bin]]
name = "no_ext_insert_rem"
path = "fuzz_targets/no_ext_insert_rem.rs"

[[bin]]
name = "trie_proof_valid"
path = "fuzz_targets/trie_proof_valid.rs"

[[bin]]
name = "trie_proof_invalid"
path = "fuzz_targets/trie_proof_invalid.rs"
9 changes: 9 additions & 0 deletions trie-db/fuzz/fuzz_targets/trie_proof_invalid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#![no_main]

use libfuzzer_sys::fuzz_target;
use trie_db_fuzz::fuzz_that_verify_rejects_invalid_proofs;

fuzz_target!(|data: &[u8]| {
fuzz_that_verify_rejects_invalid_proofs(data);
});
9 changes: 9 additions & 0 deletions trie-db/fuzz/fuzz_targets/trie_proof_valid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#![no_main]

use libfuzzer_sys::fuzz_target;
use trie_db_fuzz::fuzz_that_verify_accepts_valid_proofs;

fuzz_target!(|data: &[u8]| {
fuzz_that_verify_accepts_valid_proofs(data);
});
112 changes: 103 additions & 9 deletions trie-db/fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
// limitations under the License.



use memory_db::{MemoryDB, HashKey, PrefixedKey};
use hash_db::Hasher;
use keccak_hasher::KeccakHasher;
use memory_db::{HashKey, MemoryDB, PrefixedKey};
use reference_trie::{
RefTrieDBMutNoExt,
RefTrieDBMut,
reference_trie_root,
calc_root_no_extension,
compare_no_extension_insert_remove,
ExtensionLayout,
NoExtensionLayout,
proof::{generate_proof, verify_proof},
reference_trie_root,
RefTrieDBMut,
RefTrieDBMutNoExt,
};
use trie_db::{TrieMut, DBValue};
use keccak_hasher::KeccakHasher;


use std::convert::TryInto;
use trie_db::{DBValue, Trie, TrieDB, TrieDBMut, TrieLayout, TrieMut};

fn fuzz_to_data(input: &[u8]) -> Vec<(Vec<u8>,Vec<u8>)> {
let mut result = Vec::new();
Expand Down Expand Up @@ -173,3 +175,95 @@ pub fn fuzz_that_no_extension_insert_remove(input: &[u8]) {
let memdb = MemoryDB::<_, PrefixedKey<_>, _>::default();
compare_no_extension_insert_remove(data, memdb);
}

pub fn fuzz_that_verify_accepts_valid_proofs(input: &[u8]) {
let mut data = fuzz_to_data(input);
// Split data into 3 parts:
// - the first 1/3 is added to the trie and not included in the proof
// - the second 1/3 is added to the trie and included in the proof
// - the last 1/3 is not added to the trie and the proof proves non-inclusion of them
let mut keys = data[(data.len() / 3)..]
.iter()
.map(|(key, _)| key.clone())
.collect::<Vec<_>>();
data.truncate(data.len() * 2 / 3);

let data = data_sorted_unique(data);
keys.sort();
keys.dedup();

let (root, proof, items) = test_generate_proof::<ExtensionLayout>(data, keys);
assert!(verify_proof::<ExtensionLayout, _, _, _>(&root, &proof, items.iter()).is_ok());
}

pub fn fuzz_that_verify_rejects_invalid_proofs(input: &[u8]) {
if input.len() < 4 {
return;
}

let random_int = u32::from_le_bytes(
input[0..4].try_into().expect("slice is 4 bytes")
) as usize;

let mut data = fuzz_to_data(&input[4..]);
// Split data into 3 parts:
// - the first 1/3 is added to the trie and not included in the proof
// - the second 1/3 is added to the trie and included in the proof
// - the last 1/3 is not added to the trie and the proof proves non-inclusion of them
let mut keys = data[(data.len() / 3)..]
.iter()
.map(|(key, _)| key.clone())
.collect::<Vec<_>>();
data.truncate(data.len() * 2 / 3);

let data = data_sorted_unique(data);
keys.sort();
keys.dedup();

if keys.is_empty() {
return;
}

let (root, proof, mut items) = test_generate_proof::<ExtensionLayout>(data, keys);

// Make one item at random incorrect.
let items_idx = random_int % items.len();
match &mut items[items_idx] {
(_, Some(value)) if random_int % 2 == 0 => value.push(0),
(_, value) if value.is_some() => *value = None,
(_, value) => *value = Some(DBValue::new()),
}
assert!(verify_proof::<ExtensionLayout, _, _, _>(&root, &proof, items.iter()).is_err());
}

fn test_generate_proof<L: TrieLayout>(
entries: Vec<(Vec<u8>, Vec<u8>)>,
keys: Vec<Vec<u8>>,
) -> (<L::Hash as Hasher>::Out, Vec<Vec<u8>>, Vec<(Vec<u8>, Option<DBValue>)>)
{
// Populate DB with full trie from entries.
let (db, root) = {
let mut db = <MemoryDB<L::Hash, HashKey<_>, _>>::default();
let mut root = Default::default();
{
let mut trie = <TrieDBMut<L>>::new(&mut db, &mut root);
for (key, value) in entries {
trie.insert(&key, &value).unwrap();
}
}
(db, root)
};

// Generate proof for the given keys..
let trie = <TrieDB<L>>::new(&db, &root).unwrap();
let proof = generate_proof::<_, L, _, _>(&trie, keys.iter()).unwrap();
let items = keys.into_iter()
.map(|key| {
let value = trie.get(&key).unwrap();
(key,value)
})
.collect();

(root, proof, items)
}

1 change: 1 addition & 0 deletions trie-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl<T> MaybeDebug for T {}


pub mod node;
pub mod proof;
pub mod triedb;
pub mod triedbmut;
pub mod sectriedb;
Expand Down
Loading