-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproperty_tests.rs
More file actions
285 lines (263 loc) · 11 KB
/
Copy pathproperty_tests.rs
File metadata and controls
285 lines (263 loc) · 11 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
//! Property-based and statistical tests for Kontor PoR.
//!
//! Implements the FUZ-01..FUZ-05 coverage previously left as `#[ignore]`-d
//! stubs in `fuzzing_targets.rs`. Each test drives a large, fixed-seed random
//! sample (deterministic, non-flaky) over a primitive's public API and asserts
//! its invariant on every draw. The byte-oriented `cargo-fuzz` harnesses under
//! `fuzz/fuzz_targets/` remain the complementary deserialization-robustness
//! layer.
//!
//! Randomised loops (seeded `StdRng`) are used rather than `proptest` so the
//! suite has no extra dependency and builds offline; the trade-off is no
//! automatic shrinking. Each case prints its seed/index context on failure so
//! a counterexample is reproducible.
//!
//! Scope note: these exercise the stable core primitives (merkle, prepare,
//! poseidon domain tags, challenge-index derivation) plus a small bounded
//! prove/verify roundtrip — not circuit internals or proof wire-format, which
//! the dedicated circuit and e2e suites already cover.
use kontor_crypto::poseidon::{domain_tags, poseidon_hash_tagged};
use kontor_crypto::{
build_tree, derive_index_from_bits, field_from_uniform_bytes, get_padded_proof_for_leaf,
prepare_file, verify_merkle_proof_in_place, FieldElement,
};
use rand::{rngs::StdRng, Rng, RngCore, SeedableRng};
#[allow(dead_code)]
mod common;
use common::assertions::assert_prove_and_verify_succeeds;
use common::fixtures::{setup_test_scenario, TestConfig};
/// A uniformly-random field element drawn via the wide-reduction path used for
/// challenge seeds.
fn rand_field(rng: &mut StdRng) -> FieldElement {
let mut buf = [0u8; 64];
rng.fill_bytes(&mut buf);
field_from_uniform_bytes(&buf)
}
/// A random chunk layout: 1..=24 chunks of 1..=31 bytes each (each chunk fits a
/// single 31-byte field leaf), then zero-padded to a power-of-two leaf count.
///
/// `build_tree` itself does not pad the leaf layer — production always feeds it
/// a power-of-two symbol count (see `validate_and_encode`, which resizes to
/// `next_power_of_two` with 31-zero-byte chunks). We mirror that here so the
/// roundtrip exercises the real code path.
fn rand_padded_chunks(rng: &mut StdRng) -> Vec<Vec<u8>> {
let n = rng.gen_range(1..=24);
let mut chunks: Vec<Vec<u8>> = (0..n)
.map(|_| {
let len = rng.gen_range(1..=31);
let mut c = vec![0u8; len];
rng.fill_bytes(&mut c);
c
})
.collect();
let padded_len = chunks.len().next_power_of_two();
chunks.resize(padded_len, vec![0u8; 31]); // CHUNK_SIZE_BYTES
chunks
}
/// FUZ-01: `build_tree` → `get_padded_proof_for_leaf` → `verify_merkle_proof_in_place`.
///
/// - Never panics for any chunk layout.
/// - A padded proof for leaf `i` verifies against its own tree's root for every
/// padded leaf index, and has exactly `depth` siblings.
/// - Only-if direction: a proof does not verify against an unrelated root.
#[test]
fn fuz01_merkle_roundtrip() {
let mut rng = StdRng::seed_from_u64(0xF021_0001);
for case in 0..256 {
let chunks = rand_padded_chunks(&mut rng);
let (tree, root) = build_tree(&chunks).unwrap();
let depth = tree.layers.len().saturating_sub(1);
let num_leaves = tree.layers[0].len();
assert!(num_leaves.is_power_of_two(), "case {case}: leaves not 2^k");
for i in 0..num_leaves {
let proof = get_padded_proof_for_leaf(&tree, i, depth).unwrap();
assert_eq!(proof.siblings.len(), depth, "case {case}, leaf {i}");
assert!(
verify_merkle_proof_in_place(root, &proof),
"case {case}: valid proof for leaf {i} must verify"
);
}
// Only-if direction: a proof from this tree must not verify against an
// unrelated root. Skip the (astronomically rare) root collision.
let (_tree_b, root_b) = build_tree(&rand_padded_chunks(&mut rng)).unwrap();
if root_b != root {
let proof0 = get_padded_proof_for_leaf(&tree, 0, depth).unwrap();
assert!(
!verify_merkle_proof_in_place(root_b, &proof0),
"case {case}: proof must not verify against a foreign root"
);
}
// Tamper direction: corrupting a single sibling must break verification
// against the *correct* root — this catches a verifier that ignores the
// path or mishandles sibling order (the foreign-root case alone wouldn't).
if depth > 0 {
let mut tampered = get_padded_proof_for_leaf(&tree, 0, depth).unwrap();
let bogus = field_from_uniform_bytes(&[0xA5u8; 64]);
if tampered.siblings[0] != bogus {
tampered.siblings[0] = bogus;
assert!(
!verify_merkle_proof_in_place(root, &tampered),
"case {case}: a corrupted sibling must fail verification"
);
}
}
}
}
/// FUZ-02: `prepare_file` on arbitrary content/nonce.
///
/// - Never panics; returns coherent metadata (padded_len is a power of two,
/// `depth == log2(padded_len)`, `original_size == data.len()`, `validate()`
/// passes).
/// - `file_id` / `object_id` / `root` are deterministic for identical input.
/// - `object_id` depends only on content, not on the nonce.
#[test]
fn fuz02_prepare_file_coherent_and_deterministic() {
let mut rng = StdRng::seed_from_u64(0xF021_0002);
for case in 0..128 {
// Span several codewords (231×31 = 7161 data bytes each) so that
// padded_len and depth actually vary across cases (one codeword → depth
// 8, two→9, three→10) rather than pinning a single (depth, padded_len)
// point. Bounded at ~16 KB to keep erasure-coding cost reasonable.
let data_len = rng.gen_range(1..=16_000);
let mut data = vec![0u8; data_len];
rng.fill_bytes(&mut data);
let nonce_len = rng.gen_range(0..=32);
let mut nonce = vec![0u8; nonce_len];
rng.fill_bytes(&mut nonce);
let (_p1, m1) = prepare_file(&data, "f.dat", &nonce).unwrap();
let (_p2, m2) = prepare_file(&data, "f.dat", &nonce).unwrap();
// Determinism.
assert_eq!(
m1.file_id, m2.file_id,
"case {case}: file_id non-deterministic"
);
assert_eq!(
m1.object_id, m2.object_id,
"case {case}: object_id non-deterministic"
);
assert_eq!(m1.root, m2.root, "case {case}: root non-deterministic");
// Coherence.
assert!(
m1.padded_len.is_power_of_two(),
"case {case}: padded_len not 2^k"
);
assert_eq!(
1usize << m1.depth(),
m1.padded_len,
"case {case}: depth != log2(padded_len)"
);
assert_eq!(
m1.original_size,
data.len(),
"case {case}: original_size mismatch"
);
assert!(
m1.validate().is_ok(),
"case {case}: metadata failed validate()"
);
// object_id is content-only: changing the nonce leaves it unchanged.
let mut nonce2 = nonce.clone();
nonce2.push(rng.gen());
let (_p3, m3) = prepare_file(&data, "f.dat", &nonce2).unwrap();
assert_eq!(
m1.object_id, m3.object_id,
"case {case}: object_id depends on nonce"
);
}
}
/// FUZ-04: domain-tag separation.
///
/// For any `(x, y)`, the tagged Poseidon hash under each of the seven protocol
/// domain tags is pairwise distinct — the tag genuinely separates hashing
/// contexts and no two contexts collide.
#[test]
fn fuz04_domain_tag_separation() {
let mut rng = StdRng::seed_from_u64(0xF021_0004);
for case in 0..512 {
let x = rand_field(&mut rng);
let y = rand_field(&mut rng);
let tags = [
("leaf", domain_tags::leaf::<FieldElement>()),
("node", domain_tags::node::<FieldElement>()),
("challenge", domain_tags::challenge::<FieldElement>()),
("state_update", domain_tags::state_update::<FieldElement>()),
(
"root_commitment",
domain_tags::root_commitment::<FieldElement>(),
),
(
"challenge_per_file",
domain_tags::challenge_per_file::<FieldElement>(),
),
("challenge_id", domain_tags::challenge_id::<FieldElement>()),
];
let hashes: Vec<(&str, FieldElement)> = tags
.iter()
.map(|(name, tag)| (*name, poseidon_hash_tagged(*tag, x, y)))
.collect();
for (i, (name_i, hi)) in hashes.iter().enumerate() {
for (name_j, hj) in &hashes[i + 1..] {
assert_ne!(
hi, hj,
"case {case}: domain tags {name_i} and {name_j} collided"
);
}
}
}
}
/// FUZ-03: prove/verify roundtrip over small random shapes.
///
/// `verify(prove(x))` holds for valid `x` across tree depths 0..=3 and 1..=3
/// challenges per file, for arbitrary seeds. Nova proving is expensive, so the
/// case count is small; the e2e suite covers the fixed representative shapes
/// exhaustively.
#[test]
fn fuz03_prove_verify_roundtrip() {
let mut rng = StdRng::seed_from_u64(0xF021_0003);
for _ in 0..6 {
let depth = rng.gen_range(0..=3);
let challenges = rng.gen_range(1..=3);
let seed: u64 = rng.gen();
let mut config = TestConfig::for_depth(depth);
config.challenges_per_file = challenges;
config.seed = seed;
let setup = setup_test_scenario(&config).unwrap();
assert_prove_and_verify_succeeds(setup);
}
}
/// FUZ-05: challenge-index uniformity (seeded chi-squared).
///
/// `derive_index_from_bits` must spread challenge seeds uniformly across the
/// `2^depth` leaves so no leaf is over- or under-challenged. We draw a large
/// fixed-seed sample (deterministic, non-flaky) and require the chi-squared
/// statistic to stay below the p=0.999 critical value — failing only if the
/// distribution is *extremely* non-uniform.
#[test]
fn fuz05_challenge_index_uniformity() {
use statrs::distribution::{ChiSquared, ContinuousCDF};
const DEPTH: usize = 8;
const BUCKETS: usize = 1 << DEPTH; // 256 leaves
const SAMPLES: usize = BUCKETS * 600; // ~600 expected per bucket
let mut counts = vec![0u64; BUCKETS];
let mut rng = StdRng::seed_from_u64(0x00C0_FFEE);
for _ in 0..SAMPLES {
let f = rand_field(&mut rng);
let idx = derive_index_from_bits(f, DEPTH);
assert!(idx < BUCKETS, "index {idx} out of range");
counts[idx] += 1;
}
let expected = SAMPLES as f64 / BUCKETS as f64;
let chi2: f64 = counts
.iter()
.map(|&c| {
let d = c as f64 - expected;
d * d / expected
})
.sum();
let df = (BUCKETS - 1) as f64;
let critical = ChiSquared::new(df).unwrap().inverse_cdf(0.999);
assert!(
chi2 < critical,
"challenge indices not uniform: chi2={chi2:.1} >= critical(p=0.999, df={df})={critical:.1}"
);
}