Skip to content

Commit 26321d5

Browse files
[storage/qmdb/current] Refactor current range proof planning (#3731)
1 parent f872040 commit 26321d5

12 files changed

Lines changed: 864 additions & 931 deletions

File tree

storage/fuzz/fuzz_targets/current_crash_recovery.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! checkpoint and verifies that `init()` succeeds and the DB is usable.
88
99
use arbitrary::{Arbitrary, Result, Unstructured};
10-
use commonware_cryptography::{Hasher as _, Sha256};
10+
use commonware_cryptography::Sha256;
1111
use commonware_parallel::Sequential;
1212
use commonware_runtime::{
1313
buffer::paged::CacheRef,
@@ -17,7 +17,10 @@ use commonware_runtime::{
1717
use commonware_storage::{
1818
journal::contiguous::variable::Config as VConfig,
1919
merkle::{full::Config as MerkleConfig, mmb, mmr, Graftable, Location},
20-
qmdb::current::{unordered::variable::Db as Current, VariableConfig},
20+
qmdb::{
21+
self,
22+
current::{unordered::variable::Db as Current, VariableConfig},
23+
},
2124
translator::TwoCap,
2225
};
2326
use commonware_utils::{sequence::FixedBytes, NZU64};
@@ -299,7 +302,7 @@ fn fuzz_family<F: Graftable>(input: &FuzzInput, suffix_base: &str) {
299302
.await
300303
.expect("recovery must succeed");
301304

302-
let mut hasher = Sha256::new();
305+
let hasher = qmdb::hasher::<Sha256>();
303306

304307
// Verify all committed KV pairs survived the crash and are provable.
305308
let root = db.root();
@@ -315,11 +318,11 @@ fn fuzz_family<F: Graftable>(input: &FuzzInput, suffix_base: &str) {
315318
);
316319

317320
let proof = db
318-
.key_value_proof(&mut hasher, k.clone())
321+
.key_value_proof(&hasher, k.clone())
319322
.await
320323
.expect("proof generation should not fail for committed key");
321324
assert!(
322-
Db::<F>::verify_key_value_proof(&mut hasher, k, v, &proof, &root),
325+
Db::<F>::verify_key_value_proof(&hasher, k, v, &proof, &root),
323326
"key value proof failed to verify after crash recovery"
324327
);
325328
}
@@ -330,11 +333,11 @@ fn fuzz_family<F: Graftable>(input: &FuzzInput, suffix_base: &str) {
330333
for i in floor..size {
331334
let loc = Location::<F>::new(i);
332335
let (proof, ops, chunks) = db
333-
.range_proof(&mut hasher, loc, NZU64!(4))
336+
.range_proof(&hasher, loc, NZU64!(4))
334337
.await
335338
.expect("range proof should not fail after recovery");
336339
assert!(
337-
Db::<F>::verify_range_proof(&mut hasher, &proof, loc, &ops, &chunks, &root),
340+
Db::<F>::verify_range_proof(&hasher, &proof, loc, &ops, &chunks, &root),
338341
"range proof failed to verify after crash recovery at loc {loc}"
339342
);
340343
}

storage/fuzz/fuzz_targets/current_ordered_operations.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#![no_main]
22

33
use arbitrary::Arbitrary;
4-
use commonware_cryptography::{sha256::Digest, Hasher, Sha256};
4+
use commonware_cryptography::{sha256::Digest, Sha256};
55
use commonware_parallel::Sequential;
66
use commonware_runtime::{buffer::paged::CacheRef, deterministic, Runner, Supervisor as _};
77
use commonware_storage::{
88
journal::contiguous::fixed::Config as FConfig,
99
merkle::{full::Config as MerkleConfig, mmb, mmr, Graftable, Location},
10-
qmdb::current::{ordered::fixed::Db as CurrentDb, FixedConfig as Config},
10+
qmdb::{
11+
self,
12+
current::{ordered::fixed::Db as CurrentDb, FixedConfig as Config},
13+
},
1114
translator::TwoCap,
1215
};
1316
use commonware_utils::{sequence::FixedBytes, NZUsize, NZU16, NZU64};
@@ -113,7 +116,7 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
113116
let suffix = suffix.to_string();
114117
let operations = data.operations.clone();
115118
runner.start(|context| async move {
116-
let mut hasher = Sha256::new();
119+
let hasher = qmdb::hasher::<Sha256>();
117120
let page_cache = CacheRef::from_pooler(
118121
&context,
119122
PAGE_SIZE,
@@ -251,13 +254,13 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
251254
let oldest_loc = db.sync_boundary();
252255
if start_loc >= oldest_loc {
253256
let (proof, ops, chunks) = db
254-
.range_proof(&mut hasher, start_loc, *max_ops)
257+
.range_proof(&hasher, start_loc, *max_ops)
255258
.await
256259
.expect("Range proof should not fail");
257260

258261
assert!(
259262
Db::<F>::verify_range_proof(
260-
&mut hasher,
263+
&hasher,
261264
&proof,
262265
start_loc,
263266
&ops,
@@ -292,15 +295,15 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
292295
let root = db.root();
293296

294297
if let Ok((range_proof, ops, chunks)) = db
295-
.range_proof(&mut hasher, start_loc, *max_ops)
298+
.range_proof(&hasher, start_loc, *max_ops)
296299
.await {
297300
// Try to verify the proof when providing bad proof digests.
298301
let bad_digests = bad_digests.iter().map(|d| Digest::from(*d)).collect();
299302
if range_proof.proof.digests != bad_digests {
300303
let mut bad_proof = range_proof.clone();
301304
bad_proof.proof.digests = bad_digests;
302305
assert!(!Db::<F>::verify_range_proof(
303-
&mut hasher,
306+
&hasher,
304307
&bad_proof,
305308
start_loc,
306309
&ops,
@@ -312,7 +315,7 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
312315
// Try to verify the proof when providing bad input chunks.
313316
if &chunks != bad_chunks {
314317
assert!(!Db::<F>::verify_range_proof(
315-
&mut hasher,
318+
&hasher,
316319
&range_proof,
317320
start_loc,
318321
&ops,
@@ -323,11 +326,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
323326

324327
let bad_prefix_peaks =
325328
bad_prefix_peaks.iter().map(|d| Digest::from(*d)).collect();
326-
if range_proof.unfolded_prefix_peaks != bad_prefix_peaks {
329+
if range_proof.prefix_witnesses != bad_prefix_peaks {
327330
let mut bad_proof = range_proof.clone();
328-
bad_proof.unfolded_prefix_peaks = bad_prefix_peaks;
331+
bad_proof.prefix_witnesses = bad_prefix_peaks;
329332
assert!(!Db::<F>::verify_range_proof(
330-
&mut hasher,
333+
&hasher,
331334
&bad_proof,
332335
start_loc,
333336
&ops,
@@ -338,11 +341,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
338341

339342
let bad_suffix_peaks =
340343
bad_suffix_peaks.iter().map(|d| Digest::from(*d)).collect();
341-
if range_proof.unfolded_suffix_peaks != bad_suffix_peaks {
344+
if range_proof.suffix_witnesses != bad_suffix_peaks {
342345
let mut bad_proof = range_proof.clone();
343-
bad_proof.unfolded_suffix_peaks = bad_suffix_peaks;
346+
bad_proof.suffix_witnesses = bad_suffix_peaks;
344347
assert!(!Db::<F>::verify_range_proof(
345-
&mut hasher,
348+
&hasher,
346349
&bad_proof,
347350
start_loc,
348351
&ops,
@@ -363,11 +366,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
363366
committed_op_count = db.bounds().await.end;
364367
let current_root = db.root();
365368

366-
match db.key_value_proof(&mut hasher, k.clone()).await {
369+
match db.key_value_proof(&hasher, k.clone()).await {
367370
Ok(proof) => {
368371
let value = db.get(&k).await.expect("get should not fail").expect("key should exist");
369372
let verification_result = Db::<F>::verify_key_value_proof(
370-
&mut hasher,
373+
&hasher,
371374
k,
372375
value,
373376
&proof,
@@ -394,10 +397,10 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
394397
committed_op_count = db.bounds().await.end;
395398
let current_root = db.root();
396399

397-
match db.exclusion_proof(&mut hasher, &k).await {
400+
match db.exclusion_proof(&hasher, &k).await {
398401
Ok(proof) => {
399402
let verification_result = Db::<F>::verify_exclusion_proof(
400-
&mut hasher,
403+
&hasher,
401404
&k,
402405
&proof,
403406
&current_root,

storage/fuzz/fuzz_targets/current_unordered_operations.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#![no_main]
22

33
use arbitrary::Arbitrary;
4-
use commonware_cryptography::{sha256::Digest, Hasher, Sha256};
4+
use commonware_cryptography::{sha256::Digest, Sha256};
55
use commonware_parallel::Sequential;
66
use commonware_runtime::{buffer::paged::CacheRef, deterministic, Runner, Supervisor as _};
77
use commonware_storage::{
88
journal::contiguous::fixed::Config as FConfig,
99
merkle::{full::Config as MerkleConfig, mmb, mmr, Graftable, Location},
10-
qmdb::current::{unordered::fixed::Db as CurrentDb, FixedConfig as Config},
10+
qmdb::{
11+
self,
12+
current::{unordered::fixed::Db as CurrentDb, FixedConfig as Config},
13+
},
1114
translator::TwoCap,
1215
};
1316
use commonware_utils::{sequence::FixedBytes, NZUsize, NZU16, NZU64};
@@ -103,7 +106,7 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
103106
let suffix = suffix.to_string();
104107
let operations = data.operations.clone();
105108
runner.start(|context| async move {
106-
let mut hasher = Sha256::new();
109+
let hasher = qmdb::hasher::<Sha256>();
107110
let page_cache = CacheRef::from_pooler(
108111
&context,
109112
PAGE_SIZE,
@@ -226,13 +229,13 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
226229
let oldest_loc = db.sync_boundary();
227230
if start_loc >= oldest_loc {
228231
let (proof, ops, chunks) = db
229-
.range_proof(&mut hasher, start_loc, *max_ops)
232+
.range_proof(&hasher, start_loc, *max_ops)
230233
.await
231234
.expect("Range proof should not fail");
232235

233236
assert!(
234237
Db::<F>::verify_range_proof(
235-
&mut hasher,
238+
&hasher,
236239
&proof,
237240
start_loc,
238241
&ops,
@@ -264,15 +267,15 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
264267
let root = db.root();
265268

266269
if let Ok((range_proof, ops, chunks)) = db
267-
.range_proof(&mut hasher, start_loc, *max_ops)
270+
.range_proof(&hasher, start_loc, *max_ops)
268271
.await {
269272
// Try to verify the proof when providing bad proof digests.
270273
let bad_digests = bad_digests.iter().map(|d| Digest::from(*d)).collect();
271274
if range_proof.proof.digests != bad_digests {
272275
let mut bad_proof = range_proof.clone();
273276
bad_proof.proof.digests = bad_digests;
274277
assert!(!Db::<F>::verify_range_proof(
275-
&mut hasher,
278+
&hasher,
276279
&bad_proof,
277280
start_loc,
278281
&ops,
@@ -284,7 +287,7 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
284287
// Try to verify the proof when providing bad input chunks.
285288
if &chunks != bad_chunks {
286289
assert!(!Db::<F>::verify_range_proof(
287-
&mut hasher,
290+
&hasher,
288291
&range_proof,
289292
start_loc,
290293
&ops,
@@ -295,11 +298,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
295298

296299
let bad_prefix_peaks =
297300
bad_prefix_peaks.iter().map(|d| Digest::from(*d)).collect();
298-
if range_proof.unfolded_prefix_peaks != bad_prefix_peaks {
301+
if range_proof.prefix_witnesses != bad_prefix_peaks {
299302
let mut bad_proof = range_proof.clone();
300-
bad_proof.unfolded_prefix_peaks = bad_prefix_peaks;
303+
bad_proof.prefix_witnesses = bad_prefix_peaks;
301304
assert!(!Db::<F>::verify_range_proof(
302-
&mut hasher,
305+
&hasher,
303306
&bad_proof,
304307
start_loc,
305308
&ops,
@@ -310,11 +313,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
310313

311314
let bad_suffix_peaks =
312315
bad_suffix_peaks.iter().map(|d| Digest::from(*d)).collect();
313-
if range_proof.unfolded_suffix_peaks != bad_suffix_peaks {
316+
if range_proof.suffix_witnesses != bad_suffix_peaks {
314317
let mut bad_proof = range_proof.clone();
315-
bad_proof.unfolded_suffix_peaks = bad_suffix_peaks;
318+
bad_proof.suffix_witnesses = bad_suffix_peaks;
316319
assert!(!Db::<F>::verify_range_proof(
317-
&mut hasher,
320+
&hasher,
318321
&bad_proof,
319322
start_loc,
320323
&ops,
@@ -332,11 +335,11 @@ fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
332335
committed_op_count = db.bounds().await.end;
333336
let current_root = db.root();
334337

335-
match db.key_value_proof(&mut hasher, k.clone()).await {
338+
match db.key_value_proof(&hasher, k.clone()).await {
336339
Ok(proof) => {
337340
let value = db.get(&k).await.expect("get should not fail").expect("key should exist");
338341
let verification_result = Db::<F>::verify_key_value_proof(
339-
&mut hasher,
342+
&hasher,
340343
k,
341344
value,
342345
&proof,

0 commit comments

Comments
 (0)