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
//! # Valgrind/ctgrind Constant-Time Verification Harness
//!
//! This module provides the complete verification harness for mechanically proving
//! constant-time compliance of the Aethel-ID enclave rejection sampling implementation.
//!
//! ## Purpose
//!
//! Uses Valgrind Memcheck client requests to poison secret inputs (`s` and `tau`)
//! as "undefined memory" in Valgrind's shadow VM. Any secret-dependent conditional
//! branch or memory access index will trigger a Valgrind error:
//!
//! - `"Conditional jump or move depends on uninitialised value(s)"` → timing leak
//! - `"Use of uninitialised value of size 8 (Memory Address)"` → cache leak
//!
//! A clean run (`ERROR SUMMARY: 0 errors from 0 contexts`) proves constant-time
//! compliance.
//!
//! ## Expected Test Result Matrix
//!
//! | Code Construction | Valgrind Memcheck Behavior | CT Verification Result |
//! |---|---|---|
//! | Branch on Secret (`if secret_s[i] > 0`) | Conditional jump depends on uninitialised value | FAILED (Timing Leak) |
//! | Secret-Indexed Array (`table[secret_s[i]]`) | Use of uninitialised value of size 8 | FAILED (Cache Leak) |
//! | `enclave_plp_prove_fixed_time` | ERROR SUMMARY: 0 errors from 0 contexts | PASSED (Constant-Time) |
//!
//! ## CI Execution
//!
//! ```bash
//! # 1. Compile the Rust integration test binary
//! cargo test --test valgrind_ct_harness --no-run --release
//! # 2. Locate the compiled executable artifact
//! TEST_BIN=$(find target/release/deps -maxdepth 1 -type f -name "valgrind_ct_harness*")
//! # 3. Execute under Valgrind Memcheck with strict leak detection
//! valgrind \
//! --tool=memcheck \
//! --track-origins=yes \
//! --leak-check=full \
//! --error-exitcode=101 \
//! --verbose \
//! $TEST_BIN --nocapture
//! ```
//!
//! ## Rust Test Harness
//!
//! The Rust test harness (reproduced verbatim from the specification) is located
//! in `tests/valgrind_ct_harness.rs`. The C harness is in `tests/ct_harness.c`.
// ── Rust Test Harness (tests/valgrind_ct_harness.rs) ─────────────────────────
//
// Verbatim from specification:
//
// ```rust
// use aethel_enclave_plp::*;
// use valgrind_request::{make_mem_defined, make_mem_undefined};
//
// fn is_running_on_valgrind() -> bool {
// unsafe { valgrind_request::running_on_valgrind() > 0 }
// }
//
// #[test]
// fn ctgrind_verify_enclave_plp_constant_time() {
// if !is_running_on_valgrind() {
// eprintln!("\n[WARNING] Test is NOT running under Valgrind Memcheck!");
// }
// let mut secret_s = VectorK { vec: [Polynomial { coeffs: [0; RING_N] }; MODULE_K] };
// for k in 0..MODULE_K {
// for n in 0..RING_N {
// secret_s.vec[k].coeffs[n] = (n % 5) as i32 - 2;
// }
// }
// let mut tau = [0x5Au8; 32];
// let mut proof_out = PlpProof::zero();
// // POISON SECRETS: Mark secret memory as "undefined" in Valgrind's shadow VM
// unsafe {
// make_mem_undefined(
// &mut secret_s as *mut _ as *mut libc::c_void,
// core::mem::size_of::<VectorK>(),
// );
// make_mem_undefined(tau.as_mut_ptr() as *mut libc::c_void, tau.len());
// }
// let result = enclave_plp_prove_fixed_time(&mut proof_out, &secret_s, &tau);
// unsafe {
// make_mem_defined(&mut proof_out as *mut _ as *mut libc::c_void, core::mem::size_of::<PlpProof>());
// make_mem_defined(&mut secret_s as *mut _ as *mut libc::c_void, core::mem::size_of::<VectorK>());
// }
// assert!(result.is_ok(), "Proof generation failed during CT test");
// }
// ```
// ── C Verification Harness (tests/ct_harness.c) ───────────────────────────────
//
// Verbatim from specification:
//
// ```c
// #include <stdio.h>
// #include <stdint.h>
// #include <stdlib.h>
// #include <string.h>
// #include <valgrind/memcheck.h>
//
// #define RING_N 256
// #define MODULE_K 4
//
// typedef struct { int32_t coeffs[RING_N]; } Polynomial;
// typedef struct { Polynomial vec[MODULE_K]; } VectorK;
// typedef struct { VectorK z; uint32_t iteration_counter; } PlpProof;
//
// extern int32_t enclave_plp_prove_fixed_time(PlpProof* proof_out, const VectorK* s, const uint8_t tau[32]);
//
// int main(void) {
// VectorK secret_s;
// uint8_t tau[32];
// PlpProof proof_out;
// memset(&secret_s, 0, sizeof(VectorK));
// memset(tau, 0x77, 32);
// memset(&proof_out, 0, sizeof(PlpProof));
// for (int k = 0; k < MODULE_K; k++) {
// for (int n = 0; n < RING_N; n++) {
// secret_s.vec[k].coeffs[n] = (n % 3) - 1;
// }
// }
// VALGRIND_MAKE_MEM_UNDEFINED(&secret_s, sizeof(VectorK));
// VALGRIND_MAKE_MEM_UNDEFINED(tau, 32);
// int32_t status = enclave_plp_prove_fixed_time(&proof_out, &secret_s, tau);
// VALGRIND_MAKE_MEM_DEFINED(&proof_out, sizeof(PlpProof));
// VALGRIND_MAKE_MEM_DEFINED(&secret_s, sizeof(VectorK));
// if (status == 0) {
// printf("[SUCCESS] Constant-Time Proof Executed Cleanly.\n");
// return 0;
// } else {
// printf("[ERROR] Proof generation failed.\n");
// return 1;
// }
// }
// ```
// ── Rust module implementation ────────────────────────────────────────────────
use crate;
/// Result of a constant-time verification run.
/// Run the constant-time verification test without Valgrind poisoning.
///
/// This function exercises the same code path as the Valgrind harness but
/// without memory poisoning. It verifies that:
///
/// 1. The fixed-iteration loop completes without panicking.
/// 2. A valid proof is produced for the given inputs.
/// 3. The proof's iteration counter is within `[0, FIXED_ITERATION_CEILING)`.
///
/// For full constant-time verification, run the test binary under Valgrind
/// Memcheck as described in the module documentation.
///
/// # Example
///
/// ```rust,no_run
/// use aethel_core::ct_verify::run_ct_verification;
/// let result = run_ct_verification();
/// println!("CT verification result: {:?}", result);
/// ```
/// Run the C-harness equivalent verification test.
///
/// Mirrors the C `main()` function from `tests/ct_harness.c`, using the
/// same coefficient pattern `(n % 3) - 1` and tau pattern `0x77`.
///
/// Returns `true` if proof generation succeeded, `false` otherwise.