-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathproofs_malleability.rs
More file actions
346 lines (318 loc) · 12.1 KB
/
proofs_malleability.rs
File metadata and controls
346 lines (318 loc) · 12.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#![no_main]
use arbitrary::Arbitrary;
use commonware_codec::{Decode, Encode};
use commonware_cryptography::{sha256::Digest, Hasher as _, Sha256};
use commonware_storage::{
bmt::Builder as BmtBuilder,
merkle::{
hasher::Standard, mem::Mem, mmb, mmr, verification, Bagging, Bagging::ForwardFold,
Family as MerkleFamily, Location,
},
};
use futures::executor::block_on;
use libfuzzer_sys::fuzz_target;
use std::{collections::HashSet, num::NonZeroUsize};
const MAX_MUTATIONS: usize = 50;
#[derive(Arbitrary, Debug, Clone)]
enum Mutation {
FlipBit { position: u16, bit_idx: u8 },
InsertByte { position: u16, value: u8 },
DeleteByte { position: u16 },
ReplaceByte { position: u16, value: u8 },
SwapBytes { pos1: u16, pos2: u16 },
}
#[derive(Arbitrary, Debug)]
enum ProofType {
Merkle,
MerkleMulti,
Bmt,
BmtMulti,
}
#[derive(Debug)]
struct FuzzInput {
proof: ProofType,
mutations: Vec<Mutation>,
positions: Vec<u8>,
elements: Vec<u8>,
/// Non-zero XOR mask applied to `inactive_peaks` to drive its mutation. The mask is non-zero
/// so the mutated value is guaranteed to differ from the original.
inactive_peaks_mask: NonZeroUsize,
}
impl<'a> Arbitrary<'a> for FuzzInput {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let num_elements: u8 = u.arbitrary()?;
let proof = u.arbitrary()?;
let num_mutations = u.int_in_range(1..=MAX_MUTATIONS)?;
let mutations = (0..num_mutations)
.map(|_| Mutation::arbitrary(u))
.collect::<Result<Vec<_>, _>>()?;
let num_positions = u.int_in_range(0..=num_elements)?;
let positions = (0..num_positions)
.map(|_| u.arbitrary::<u8>())
.collect::<Result<Vec<_>, _>>()?;
let elements = (0..num_elements)
.map(|_| u.arbitrary::<u8>())
.collect::<Result<Vec<_>, _>>()?;
let inactive_peaks_mask = NonZeroUsize::new(u.int_in_range(1..=usize::MAX)?).unwrap();
Ok(FuzzInput {
proof,
mutations,
positions,
elements,
inactive_peaks_mask,
})
}
}
fn mutate_proof_bytes<P, C>(proof: &mut P, mutation: &Mutation, cfg: &C)
where
P: Encode + Decode<Cfg = C>,
{
let serialized = proof.encode();
let mut bytes = serialized.to_vec();
let mutated = match mutation {
Mutation::FlipBit { position, bit_idx } => {
if bytes.is_empty() {
return;
}
let idx = (*position as usize) % bytes.len();
bytes[idx] ^= 1 << (bit_idx % 8);
true
}
Mutation::InsertByte { position, value } => {
let pos = (*position as usize) % (bytes.len() + 1);
bytes.insert(pos, *value);
true
}
Mutation::DeleteByte { position } => {
if bytes.is_empty() {
return;
}
let pos = (*position as usize) % bytes.len();
bytes.remove(pos);
true
}
Mutation::ReplaceByte { position, value } => {
if bytes.is_empty() {
return;
}
let pos = (*position as usize) % bytes.len();
bytes[pos] = *value;
true
}
Mutation::SwapBytes { pos1, pos2 } => {
if bytes.len() < 2 {
return;
}
let p1 = (*pos1 as usize) % bytes.len();
let p2 = (*pos2 as usize) % bytes.len();
if p1 != p2 {
bytes.swap(p1, p2);
true
} else {
false
}
}
};
if mutated {
if let Ok(m) = P::decode_cfg(bytes.as_slice(), cfg) {
*proof = m;
}
}
}
fn supported_root_specs<F: MerkleFamily>(merkle: &Mem<F, Digest>) -> Vec<(Bagging, usize)> {
let peak_count = F::peaks(merkle.size()).count();
let mut specs = Vec::with_capacity(2 * (peak_count + 1));
let mut push_unique = |spec| {
if !specs.contains(&spec) {
specs.push(spec);
}
};
for inactive_peaks in 0..=peak_count {
push_unique((Bagging::ForwardFold, inactive_peaks));
push_unique((Bagging::BackwardFold, inactive_peaks));
}
specs
}
fn fuzz_element_proof<F: MerkleFamily>(input: &FuzzInput, digests: &[Digest]) {
let build_hasher = Standard::<Sha256>::new(ForwardFold);
let mut merkle = Mem::<F, Digest>::new();
let batch = {
let mut batch = merkle.new_batch();
for digest in digests {
batch = batch.add(&build_hasher, digest);
}
batch.merkleize(&merkle, &build_hasher)
};
merkle.apply_batch(&batch).unwrap();
for (bagging, inactive_peaks) in supported_root_specs(&merkle) {
let hasher = Standard::<Sha256>::new(bagging);
let root = merkle.root(&hasher, inactive_peaks).unwrap();
for (leaf, element) in digests.iter().enumerate() {
let loc = Location::<F>::new(leaf as u64);
let original_proof = merkle.proof(&hasher, loc, inactive_peaks).unwrap();
assert!(original_proof.verify_element_inclusion(&hasher, element, loc, &root));
let mut mutated_proof = original_proof.clone();
mutated_proof.inactive_peaks ^= input.inactive_peaks_mask.get();
assert_ne!(mutated_proof, original_proof);
assert!(!mutated_proof.verify_element_inclusion(&hasher, element, loc, &root));
for mutation in &input.mutations {
let mut mutated_proof = original_proof.clone();
mutate_proof_bytes(&mut mutated_proof, mutation, &256);
if mutated_proof != original_proof {
assert!(!mutated_proof.verify_element_inclusion(&hasher, element, loc, &root));
}
}
}
}
}
fn fuzz_range_proof<F: MerkleFamily>(input: &FuzzInput, digests: &[Digest]) {
let hasher = Standard::<Sha256>::new(ForwardFold);
let mut merkle = Mem::<F, Digest>::new();
let batch = {
let mut batch = merkle.new_batch();
for digest in digests {
batch = batch.add(&hasher, digest);
}
batch.merkleize(&merkle, &hasher)
};
merkle.apply_batch(&batch).unwrap();
let root = merkle.root(&hasher, 0).unwrap();
let (start_idx, range_len) = if digests.is_empty() || input.positions.is_empty() {
(0, 0)
} else if input.positions.len() == 1 {
let i = (input.positions[0] as usize) % digests.len();
(i, 1)
} else {
let i1 = (input.positions[0] as usize) % digests.len();
let i2 = (input.positions[1] as usize) % digests.len();
(i1.min(i2), i1.abs_diff(i2) + 1)
};
let start_loc = Location::<F>::new(start_idx as u64);
let Ok(original_proof) =
merkle.range_proof(&hasher, start_loc..start_loc + range_len as u64, 0)
else {
return;
};
let range_elements: Vec<Digest> = digests[start_idx..start_idx + range_len].to_vec();
assert!(original_proof.verify_range_inclusion(&hasher, &range_elements, start_loc, &root));
let mut mutated_proof = original_proof.clone();
mutated_proof.inactive_peaks ^= input.inactive_peaks_mask.get();
assert_ne!(mutated_proof, original_proof);
assert!(!mutated_proof.verify_range_inclusion(&hasher, &range_elements, start_loc, &root));
for mutation in &input.mutations {
let mut mutated_proof = original_proof.clone();
mutate_proof_bytes(&mut mutated_proof, mutation, &256);
if mutated_proof != original_proof {
assert!(!mutated_proof.verify_range_inclusion(
&hasher,
&range_elements,
start_loc,
&root
));
}
}
for (bagging, inactive_peaks) in supported_root_specs(&merkle) {
let hasher = Standard::<Sha256>::new(bagging);
let root = merkle.root(&hasher, inactive_peaks).unwrap();
let Ok(original_proof) = block_on(verification::historical_range_proof(
&hasher,
&merkle,
merkle.leaves(),
start_loc..start_loc + range_len as u64,
inactive_peaks,
)) else {
continue;
};
assert!(original_proof.verify_range_inclusion(&hasher, &range_elements, start_loc, &root));
let mut mutated_proof = original_proof.clone();
mutated_proof.inactive_peaks ^= input.inactive_peaks_mask.get();
assert_ne!(mutated_proof, original_proof);
assert!(!mutated_proof.verify_range_inclusion(&hasher, &range_elements, start_loc, &root));
for mutation in &input.mutations {
let mut mutated_proof = original_proof.clone();
mutate_proof_bytes(&mut mutated_proof, mutation, &256);
if mutated_proof != original_proof {
assert!(!mutated_proof.verify_range_inclusion(
&hasher,
&range_elements,
start_loc,
&root
));
}
}
}
}
fn fuzz(input: FuzzInput) {
let digests: Vec<Digest> = input.elements.iter().map(|&v| Sha256::hash(&[v])).collect();
match input.proof {
ProofType::Merkle => {
fuzz_element_proof::<mmr::Family>(&input, &digests);
fuzz_element_proof::<mmb::Family>(&input, &digests);
}
ProofType::MerkleMulti => {
fuzz_range_proof::<mmr::Family>(&input, &digests);
fuzz_range_proof::<mmb::Family>(&input, &digests);
}
ProofType::Bmt => {
let mut builder = BmtBuilder::<Sha256>::new(digests.len());
for digest in &digests {
builder.add(digest);
}
let tree = builder.build();
let root = tree.root();
for (idx, digest) in digests.iter().enumerate() {
let original_proof = tree.proof(idx as u32).unwrap();
let mut hasher = Sha256::default();
assert!(original_proof
.verify_element_inclusion(&mut hasher, digest, idx as u32, &root)
.is_ok());
for mutation in &input.mutations {
let mut mutated_proof = original_proof.clone();
mutate_proof_bytes(&mut mutated_proof, mutation, &1);
if mutated_proof != original_proof {
assert!(mutated_proof
.verify_element_inclusion(&mut hasher, digest, idx as u32, &root)
.is_err());
}
}
}
}
ProofType::BmtMulti => {
let mut builder = BmtBuilder::<Sha256>::new(digests.len());
for digest in &digests {
builder.add(digest);
}
let tree = builder.build();
let root = tree.root();
let positions: Vec<u32> = input
.positions
.iter()
.filter(|_| !digests.is_empty())
.map(|&p| (p as u32) % (digests.len() as u32))
.collect::<HashSet<_>>()
.into_iter()
.collect();
let Ok(original_proof) = tree.multi_proof(&positions) else {
return;
};
let elements: Vec<(Digest, u32)> = positions
.iter()
.map(|&p| (digests[p as usize], p))
.collect();
let mut hasher = Sha256::default();
assert!(original_proof
.verify_multi_inclusion(&mut hasher, &elements, &root)
.is_ok());
for mutation in &input.mutations {
let mut mutated_proof = original_proof.clone();
mutate_proof_bytes(&mut mutated_proof, mutation, &positions.len());
if mutated_proof != original_proof {
assert!(mutated_proof
.verify_multi_inclusion(&mut hasher, &elements, &root)
.is_err());
}
}
}
}
}
fuzz_target!(|input: FuzzInput| fuzz(input));