-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzzMerkleTree.php
More file actions
116 lines (100 loc) · 3.51 KB
/
Copy pathFuzzMerkleTree.php
File metadata and controls
116 lines (100 loc) · 3.51 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
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\Fuzzing;
use FediE2EE\PKD\Crypto\Merkle\{
InclusionProof,
IncrementalTree,
Tree
};
use ParagonIE\ConstantTime\Base64UrlSafe;
use PhpFuzzer\Config;
use RangeException;
use TypeError;
/** @var Config $config */
require_once dirname(__DIR__) . '/vendor/autoload.php';
$config->setTarget(function (string $input): void {
// Test InclusionProof construction
try {
$decoded = json_decode($input, true);
if (is_array($decoded)) {
$index = $decoded['index'] ?? 0;
$proof = $decoded['proof'] ?? [];
if (is_int($index) && is_array($proof)) {
$inclusionProof = new InclusionProof($index, $proof);
// Access properties
assert(property_exists($inclusionProof, 'index'));
assert(property_exists($inclusionProof, 'proof'));
assert(is_int($inclusionProof->index));
assert(is_string($inclusionProof->proof));
}
}
} catch (TypeError|\Exception) {
// Expected for malformed data
}
// Test IncrementalTree::fromJson
try {
$tree = IncrementalTree::fromJson($input);
assert(is_string($tree->toJson()));
assert(is_string($tree->getRoot()));
assert(is_string($tree->getEncodedRoot()));
} catch (TypeError|RangeException|\Exception) {
// Expected for malformed data
}
// Test IncrementalTree with Base64 encoded input (codebase pattern)
try {
$tree = IncrementalTree::fromJson(Base64UrlSafe::decodeNoPadding($input));
$encoded = Base64UrlSafe::encodeUnpadded($tree->toJson());
// Verify round-trip
$from = IncrementalTree::fromJson(Base64UrlSafe::decodeNoPadding($encoded));
assert($from instanceof IncrementalTree);
} catch (TypeError|RangeException|\Exception) {
// Expected for malformed data
}
// Test Tree construction with leaf data
try {
$leaves = [];
if (strlen($input) >= 32) {
// Split input into 32-byte chunks as leaf hashes
$chunks = str_split($input, 32);
foreach ($chunks as $chunk) {
if (strlen($chunk) === 32) {
$leaves[] = $chunk;
}
}
}
if (!empty($leaves)) {
$tree = new Tree($leaves, 'sha256');
$root = $tree->getRoot();
$tree->getEncodedRoot();
// Test inclusion proof generation and verification
if (count($leaves) > 0) {
$proof = $tree->getInclusionProof($leaves[0]);
$tree->verifyInclusionProof($root, $leaves[0], $proof);
}
}
} catch (TypeError|\Exception) {
// Expected for edge cases
}
// Test IncrementalTree operations
try {
$tree = new IncrementalTree([], 'sha256');
// Add leaves from fuzzed input
$chunks = str_split($input, 32);
foreach ($chunks as $chunk) {
if (strlen($chunk) > 0) {
$tree->addLeaf($chunk);
}
}
$tree->updateRoot();
$root = $tree->getRoot();
// Verify proofs for added leaves
foreach ($chunks as $chunk) {
if (strlen($chunk) > 0) {
$proof = $tree->getInclusionProof($chunk);
$tree->verifyInclusionProof($root, $chunk, $proof);
}
}
} catch (TypeError|\Exception) {
// Expected for edge cases
}
});