-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathmmr_bitmap.rs
More file actions
268 lines (242 loc) · 10.4 KB
/
mmr_bitmap.rs
File metadata and controls
268 lines (242 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#![no_main]
use arbitrary::Arbitrary;
use commonware_cryptography::{sha256, Digest, Sha256};
use commonware_parallel::Sequential;
use commonware_runtime::{deterministic, Clock, Metrics, Runner, Storage};
use commonware_storage::{merkle::Bagging::ForwardFold, MerkleizedBitMap, UnmerkleizedBitMap};
use commonware_utils::bitmap::BitMap;
use libfuzzer_sys::fuzz_target;
const MAX_OPERATIONS: usize = 100;
const CHUNK_SIZE: usize = 32;
enum Bitmap<E: Clock + Storage + Metrics, D: Digest, const N: usize> {
Merkleized(MerkleizedBitMap<E, D, N>),
Unmerkleized(UnmerkleizedBitMap<E, D, N>),
}
#[derive(Arbitrary, Debug, Clone)]
enum BitmapOperation {
Append { bit: bool },
GetBit { bit_offset: u64 },
SetBit { bit_offset: u64, bit: bool },
GetChunk { bit_offset: u64 },
LastChunk,
Len,
PrunedBits,
PruneToBit { bit_offset: u64 },
Merkleize,
GetNode { position: u64 },
Size,
Proof { bit_offset: u64 },
RestorePruned,
WritePruned,
}
#[derive(Debug)]
struct FuzzInput {
seed: u64,
operations: Vec<BitmapOperation>,
}
impl<'a> Arbitrary<'a> for FuzzInput {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let seed = u.arbitrary()?;
let num_ops = u.int_in_range(1..=MAX_OPERATIONS)?;
let mut operations = Vec::with_capacity(num_ops);
for _ in 0..num_ops {
operations.push(u.arbitrary()?);
}
Ok(FuzzInput { seed, operations })
}
}
fn fuzz(input: FuzzInput) {
let runner = deterministic::Runner::seeded(input.seed);
const PARTITION: &str = "fuzz-mmr-bitmap-test-partition";
runner.start(|context| async move {
let hasher = commonware_storage::mmr::StandardHasher::<Sha256>::new(ForwardFold);
let init_bitmap = MerkleizedBitMap::<_, _, CHUNK_SIZE>::init(
context.with_label("bitmap"),
PARTITION,
Sequential,
&hasher,
)
.await
.unwrap();
let mut bitmap = Bitmap::Merkleized(init_bitmap);
let mut bit_count = 0u64;
let mut pruned_bits = 0u64;
let mut restarts = 0usize;
for op in input.operations {
bitmap = match op {
BitmapOperation::Append { bit } => {
let mut bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap.into_dirty(),
Bitmap::Unmerkleized(bitmap) => bitmap,
};
bitmap.push(bit);
bit_count += 1;
Bitmap::Unmerkleized(bitmap)
}
BitmapOperation::GetBit { bit_offset } => {
if bit_count > 0 {
let live = bit_count.saturating_sub(pruned_bits);
if live > 0 {
let safe_offset = pruned_bits + (bit_offset % live);
let _ = match &bitmap {
Bitmap::Merkleized(bitmap) => bitmap.get_bit(safe_offset),
Bitmap::Unmerkleized(bitmap) => bitmap.get_bit(safe_offset),
};
}
}
bitmap
}
BitmapOperation::SetBit { bit_offset, bit } => {
let mut bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap.into_dirty(),
Bitmap::Unmerkleized(bitmap) => bitmap,
};
if bit_count > 0 {
let safe_offset = (bit_offset % bit_count).max(pruned_bits);
if safe_offset < bit_count {
bitmap.set_bit(safe_offset, bit);
}
}
Bitmap::Unmerkleized(bitmap)
}
BitmapOperation::GetChunk { bit_offset } => {
if bit_count > 0 {
let safe_offset = (bit_offset % bit_count).max(pruned_bits);
let chunk_aligned = (safe_offset / BitMap::<CHUNK_SIZE>::CHUNK_SIZE_BITS)
* BitMap::<CHUNK_SIZE>::CHUNK_SIZE_BITS;
if chunk_aligned >= pruned_bits && chunk_aligned < bit_count {
let _ = match &bitmap {
Bitmap::Merkleized(bitmap) => {
bitmap.get_chunk_containing(chunk_aligned)
}
Bitmap::Unmerkleized(bitmap) => {
bitmap.get_chunk_containing(chunk_aligned)
}
};
}
}
bitmap
}
BitmapOperation::LastChunk => {
if bit_count > pruned_bits {
let (chunk, bits) = match &bitmap {
Bitmap::Merkleized(bitmap) => bitmap.last_chunk(),
Bitmap::Unmerkleized(bitmap) => bitmap.last_chunk(),
};
assert!(bits <= BitMap::<CHUNK_SIZE>::CHUNK_SIZE_BITS);
assert!(chunk.len() == CHUNK_SIZE);
}
bitmap
}
BitmapOperation::Len => {
let count = match &bitmap {
Bitmap::Merkleized(bitmap) => bitmap.len(),
Bitmap::Unmerkleized(bitmap) => bitmap.len(),
};
assert_eq!(count, bit_count);
bitmap
}
BitmapOperation::PrunedBits => {
let pruned = match &bitmap {
Bitmap::Merkleized(bitmap) => bitmap.pruned_bits(),
Bitmap::Unmerkleized(bitmap) => bitmap.pruned_bits(),
};
assert_eq!(pruned, pruned_bits);
bitmap
}
BitmapOperation::PruneToBit { bit_offset } => {
let mut bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap,
Bitmap::Unmerkleized(bitmap) => bitmap.merkleize(&hasher).unwrap(),
};
if bit_count > 0 {
let safe_offset = (bit_offset % (bit_count + 1)).min(bit_count);
if safe_offset >= pruned_bits {
bitmap.prune_to_bit(safe_offset).unwrap();
// Update pruned_bits to match what was actually pruned
pruned_bits = bitmap.pruned_bits();
assert_eq!(bitmap.pruned_bits(), pruned_bits);
}
}
Bitmap::Merkleized(bitmap)
}
BitmapOperation::Merkleize => {
let bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap,
Bitmap::Unmerkleized(bitmap) => bitmap.merkleize(&hasher).unwrap(),
};
Bitmap::Merkleized(bitmap)
}
BitmapOperation::GetNode { position } => {
let bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap,
Bitmap::Unmerkleized(bitmap) => bitmap.merkleize(&hasher).unwrap(),
};
if bitmap.size() > 0 {
let safe_pos = position % bitmap.size().as_u64();
let _ = bitmap.get_node(safe_pos.into());
}
Bitmap::Merkleized(bitmap)
}
BitmapOperation::Size => {
match &bitmap {
Bitmap::Merkleized(bitmap) => bitmap.size(),
Bitmap::Unmerkleized(bitmap) => bitmap.size(),
};
bitmap
}
BitmapOperation::Proof { bit_offset } => {
let bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap,
Bitmap::Unmerkleized(bitmap) => bitmap.merkleize(&hasher).unwrap(),
};
if bit_count > pruned_bits {
let bit_offset = (bit_offset % (bit_count - pruned_bits)) + pruned_bits;
if let Ok((proof, chunk)) = bitmap.proof(&hasher, bit_offset).await {
let root = bitmap.root();
assert!(
MerkleizedBitMap::<
deterministic::Context,
sha256::Digest,
CHUNK_SIZE,
>::verify_bit_inclusion(
&hasher, &proof, &chunk, bit_offset, &root
),
"failed to verify bit {bit_offset}",
);
}
}
Bitmap::Merkleized(bitmap)
}
BitmapOperation::RestorePruned => {
let bitmap = MerkleizedBitMap::<_, _, CHUNK_SIZE>::init(
context
.with_label("bitmap")
.with_attribute("instance", restarts),
PARTITION,
Sequential,
&hasher,
)
.await
.unwrap();
restarts += 1;
// Update tracking variables to match restored state
bit_count = bitmap.len();
pruned_bits = bitmap.pruned_bits();
Bitmap::Merkleized(bitmap)
}
BitmapOperation::WritePruned => {
let mut bitmap = match bitmap {
Bitmap::Merkleized(bitmap) => bitmap,
Bitmap::Unmerkleized(bitmap) => bitmap.merkleize(&hasher).unwrap(),
};
let _ = bitmap.write_pruned().await;
Bitmap::Merkleized(bitmap)
}
}
}
});
}
fuzz_target!(|input: FuzzInput| {
fuzz(input);
});