HypothesisTest & Checks¶
Chi-squared hypothesis test and integrity check modules used inside the
hypothesis zkSNARK circuit. Sources: src/HypothesisTest.py,
src/ComputeChecks.py.
Hypothesis Test¶
HypothesisTest ¶
Bases: Module
Chi-squared hypothesis test module for the zkSNARK compliance circuit.
Implements the statistical alarm decision over the DVAL-aggregated EKF residuals. Given the summed residual sum_dy and innovation covariance sum_CPCT_cov, computes the whitened chi-squared statistic and compares it against the upper control limit UCL = chi2.ppf(1 - alpha, df=MD). In ZKP mode, inputs carry one extra column of padding to maintain fixed ONNX tensor shapes across different DVAL configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D
|
int
|
DVAL interval length (number of EKF steps per proof interval). |
required |
SD
|
int
|
State space dimension. |
required |
MD
|
int
|
Measurement space dimension (degrees of freedom). |
required |
max_lut_val
|
float
|
Maximum value for the chi-squared CDF lookup table. |
required |
lu_num_points
|
int
|
Number of points in the CDF lookup table. |
required |
alpha
|
float
|
Significance level for the hypothesis test. Default 0.005. |
0.005
|
mode
|
str
|
'ZKP' for circuit-compatible mode, otherwise uses direct scipy CDF. |
'ZKP'
|
Source code in src/HypothesisTest.py
forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sum_dy
|
Tensor
|
DVAL-summed residual vector. Shape (MD+1, 1) in ZKP mode. |
required |
sum_CPCT_cov
|
Tensor
|
DVAL-summed innovation covariance. Shape (MD, MD+1) in ZKP mode. |
required |
U
|
Tensor
|
Left singular vectors of sum_CPCT_cov for whitening. Shape (MD, MD+1) in ZKP mode. |
required |
S
|
Tensor
|
Inverse square-root singular value matrix. Shape (MD, MD+1) in ZKP mode. |
required |
kcr_status_check
|
Tensor
|
Accumulated Kalman check status across the DVAL interval. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple of six tensors: - alarm_status: 1.0 if chi-squared statistic > UCL, else 0.0. - svd_check_status: 1.0 if SVD reconstruction check passes. - kcr_status: KCR check result (first element of kcr_status_check). - chi_squared_statistic: The computed test statistic (reported only; sensitive in production). - svd_diff: Element-wise SVD reconstruction error. - svd_threshold: Per-element tolerance used in the SVD check. |
Source code in src/HypothesisTest.py
CDF Lookup¶
Chi2CDFLookup ¶
Bases: Module
ONNX-compatible chi-squared CDF approximation via linear interpolation over a precomputed lookup table. Used inside the hypothesis test zkSNARK circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_points
|
int
|
Number of points in the lookup table. |
required |
MD
|
int
|
Degrees of freedom for the chi-squared distribution. |
required |
Source code in src/HypothesisTest.py
forward ¶
Approximates chi2.cdf(x, df=MD) by linearly interpolating in the lookup table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The test statistic value to look up (scalar). |
required |
xs
|
Tensor
|
Lookup table x-axis (num_points,). |
required |
cdf_vals
|
Tensor
|
Precomputed CDF values at xs (num_points,). |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Interpolated CDF value at x (scalar). |
Source code in src/HypothesisTest.py
Chi2CDFLookupNonZKP ¶
Bases: Module
Chi-squared CDF lookup using scipy directly. Not ONNX-compatible. Used only during non-ZKP testing and training to compute exact p-values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
MD
|
int
|
Degrees of freedom for the chi-squared distribution. |
required |
forward ¶
Integrity Checks¶
SVDReconstructionCheck ¶
Bases: Module
Verifies that an SVD decomposition of the residual covariance is valid.
Given U and S (where S holds the inverse square-root singular values), checks whether U @ S @ S @ U^T ≈ I. This confirms that the SVD factors passed into the hypothesis circuit correctly represent the inverse of sum_CPCT_cov, which is required for the chi-squared whitening step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
MD
|
int
|
Dimension of the measurement space (number of sensors). |
required |
rtol
|
float
|
Relative tolerance for the allclose check. Default 1e-12. |
1e-12
|
atol
|
float
|
Absolute tolerance for the allclose check. Default 0.1. |
0.1
|
Source code in src/ComputeChecks.py
forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sum_CPCT_cov
|
Tensor
|
DVAL-aggregated residual covariance (MD x MD). |
required |
U
|
Tensor
|
Left singular vectors from SVD of sum_CPCT_cov (MD x MD). |
required |
S
|
Tensor
|
Diagonal matrix of inverse square-root singular values (MD x MD). |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - is_allclose (torch.Tensor): Scalar in {0, 1}; 1 if reconstruction passes. - diff (torch.Tensor): Element-wise absolute difference |I - U S S U^T|. - threshold (torch.Tensor): Per-element tolerance used for the check. |
Source code in src/ComputeChecks.py
KalmanReconstructionCheck ¶
Bases: Module
Verifies the optimality of the Kalman gain supplied to the prediction circuit.
Checks whether CPCT_cov^T @ K^T ≈ C @ P^T, which is the condition implied by the optimal Kalman gain K = P C^T (C P C^T + R)^{-1}. This prevents a utility from supplying an incorrect gain that would distort the residual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rtol
|
float
|
Relative tolerance. Default 1e-12. |
1e-12
|
atol
|
float
|
Absolute tolerance. Default 0.1. |
0.1
|
Source code in src/ComputeChecks.py
forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
CPCT_cov
|
Tensor
|
Innovation covariance C P C^T + R (batch x MD x MD). |
required |
jac_Ch
|
Tensor
|
Jacobian of the observation function C w.r.t. h (batch x MD x SD). |
required |
P
|
Tensor
|
State error covariance (batch x SD x SD). |
required |
KT
|
Tensor
|
Transposed Kalman gain K^T (batch x MD x SD). |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - is_allclose (torch.Tensor): Scalar in {0, 1}; 1 if the gain check passes. - diff (torch.Tensor): Element-wise absolute difference |CPCT_cov^T K^T - C P^T|. - threshold (torch.Tensor): Per-element tolerance used for the check. |