zkSNARK Utilities¶
Functions for ezkl circuit setup, witness generation, proof generation, and
I/O serialization. Source: src/zkSNARK.py.
Setup¶
setup_prediction_zksnark ¶
setup_prediction_zksnark(h, P, dy, CPCT_cov, y, jac_fh, jac_Ch, Kt, KCR_status, nonce_h, nonce_P, nonce_dy, nonce_CPCT_cov, nonce_KCR_status, nonce_kalman_diff, nonce_kalman_threshold, prediction_model, model_path, settings_path, compiled_model_path, vk_path, pk_path, py_run_args, data_path)
Exports the prediction model to ONNX and runs the full ezkl setup pipeline to produce a verification key (VK) and proving key (PK) for the prediction circuit.
Steps performed: ONNX export → ezkl.gen_settings() → ezkl.compile_circuit() → ezkl.setup(). Asserts that all output artefacts exist before returning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h, P, dy, CPCT_cov, y, jac_fh, jac_Ch, Kt, KCR_status
|
Tensor
|
Representative input tensors used to trace the ONNX graph. |
required |
nonce_kalman_diff, nonce_kalman_threshold
|
Tensor
|
Nonce tensors appended to each circuit input for ezkl Poseidon hashing. |
required |
prediction_model
|
Module
|
The PredictionWrapper forward method to export. |
required |
model_path
|
str
|
Output path for the ONNX model file. |
required |
settings_path
|
str
|
Output path for the ezkl settings JSON. |
required |
compiled_model_path
|
str
|
Output path for the compiled circuit. |
required |
vk_path
|
str
|
Output path for the verification key. |
required |
pk_path
|
str
|
Output path for the proving key. |
required |
py_run_args
|
ezkl PyRunArgs object specifying circuit configuration (scale, etc.). |
required | |
data_path
|
str
|
Path to the input data JSON (used only for reference). |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple[float, float, float, float]: (model_export_time, settings_generation_time, compile_circuit_time, circuit_setup_time) in seconds. |
Source code in src/zkSNARK.py
setup_hypothesis_zksnark ¶
setup_hypothesis_zksnark(dy, CPCT_cov, U, S, KCR_status, hypothesis_model, model_path, settings_path, compiled_model_path, vk_path, pk_path, py_run_args)
Exports the hypothesis test model to ONNX and runs the full ezkl setup pipeline to produce a VK and PK for the hypothesis circuit.
Steps performed: ONNX export → ezkl.gen_settings() → ezkl.compile_circuit() → ezkl.setup(). The UCL is baked into the HypothesisTest model as a constant, so no CDF lookup table inputs are required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dy
|
list[float]
|
DVAL-summed innovation vector (flat). |
required |
CPCT_cov
|
list[float]
|
DVAL-summed innovation covariance (flat). |
required |
U
|
Tensor
|
Left singular vectors of CPCT_cov. |
required |
S
|
Tensor
|
Diagonal matrix of inverse square-root singular values. |
required |
KCR_status
|
list[float]
|
Kalman check status carry-in (flat). |
required |
hypothesis_model
|
Module
|
The HypothesisTest module to export. |
required |
model_path
|
str
|
Output path for the ONNX model file. |
required |
settings_path
|
str
|
Output path for the ezkl settings JSON. |
required |
compiled_model_path
|
str
|
Output path for the compiled circuit. |
required |
vk_path
|
str
|
Output path for the verification key. |
required |
pk_path
|
str
|
Output path for the proving key. |
required |
py_run_args
|
ezkl PyRunArgs object specifying circuit configuration. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple[float, float, float, float]: (model_export_time, settings_generation_time, compile_circuit_time, circuit_setup_time) in seconds. |
Source code in src/zkSNARK.py
Prove & Witness¶
generate_prediction_zksnark ¶
generate_prediction_zksnark(data_path, settings_path, compiled_model_path, witness_path, proof_path, pk_path, gen_proof=True)
Generates the witness and optionally the zkSNARK proof for the prediction circuit.
Downloads the SRS if needed, runs ezkl.gen_witness() to produce the witness file, then optionally runs ezkl.mock() (for sanity checking) and ezkl.prove() to produce a Plonk proof. Prints Poseidon hashes of all circuit inputs and outputs regardless of whether a proof is generated, and parses the witness to return the DVAL-summed residuals and covariance for passing into the hypothesis circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str
|
Path to the JSON file containing serialized circuit inputs. |
required |
settings_path
|
str
|
Path to the ezkl settings JSON (used for SRS download). |
required |
compiled_model_path
|
str
|
Path to the compiled prediction circuit. |
required |
witness_path
|
str
|
Output path for the generated witness JSON. |
required |
proof_path
|
str
|
Output path for the generated proof file. |
required |
pk_path
|
str
|
Path to the proving key. |
required |
gen_proof
|
bool
|
If True, generates a proof; if False, only generates the witness. |
True
|
Returns:
| Type | Description |
|---|---|
|
Tuple[list, list, list, float, float]: - dy: DVAL-summed innovation vector as a flat float list. - CPCT_cov: DVAL-summed innovation covariance as a flat float list. - KCR_status: Kalman check status as a flat float list. - witness_generation_time: Seconds taken for witness generation. - prove_time: Seconds taken for proof generation (0 if gen_proof=False). |
Source code in src/zkSNARK.py
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 | |
generate_hypothesis_zksnark ¶
generate_hypothesis_zksnark(data_path, settings_path, compiled_model_path, witness_path, proof_path, pk_path, gen_proof=True)
Generates the witness and optionally the zkSNARK proof for the hypothesis circuit.
Downloads the SRS if needed, runs ezkl.gen_witness() to produce the witness file, then optionally runs ezkl.prove() to generate a Plonk proof. Prints Poseidon hashes of all circuit inputs (residual, covariance, U, S, KCR status) in both modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str
|
Path to the JSON file containing serialized circuit inputs. |
required |
settings_path
|
str
|
Path to the ezkl settings JSON (used for SRS download). |
required |
compiled_model_path
|
str
|
Path to the compiled hypothesis circuit. |
required |
witness_path
|
str
|
Output path for the generated witness JSON. |
required |
proof_path
|
str
|
Output path for the generated proof file. |
required |
pk_path
|
str
|
Path to the proving key. |
required |
gen_proof
|
bool
|
If True, generates a proof; if False, only generates the witness. |
True
|
Returns:
| Type | Description |
|---|---|
|
Tuple[float, float]: - witness_generation_time: Seconds taken for witness generation. - prove_time: Seconds taken for proof generation (0 if gen_proof=False). |
Source code in src/zkSNARK.py
I/O Serialization¶
dump_state_estimate ¶
dump_state_estimate(h_se, P_se, dy_se, CPCT_cov_se, y_se, jac_fh_se, jac_Ch_se, Kt_se, KCR_se, nonce_h_se, nonce_P_se, nonce_dy_se, nonce_CPCT_cov_se, nonce_KCR_status_se, nonce_kalman_diff_se, nonce_kalman_threshold_se, prediction_data_path, reset_residual)
Serializes prediction circuit inputs to a JSON file in the format expected by ezkl.
Converts all input tensors (state, covariance, residuals, Jacobians, Kalman gain, nonce columns) to flat float lists via convert_tensor_to_float_array, then writes them as the input_data array to prediction_data_path. If reset_residual=True, zeros out dy, CPCT_cov, and KCR_status before writing (used at the start of each new DVAL interval).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h_se, P_se, dy_se, CPCT_cov_se, y_se, jac_fh_se, jac_Ch_se, Kt_se, KCR_se
|
Sequences of tensors for each circuit input field. |
required | |
nonce_kalman_diff_se, nonce_kalman_threshold_se
|
Sequences of nonce tensors for each circuit input field. |
required | |
prediction_data_path
|
str
|
Output path for the JSON input data file. |
required |
reset_residual
|
bool
|
If True, zero out dy, CPCT_cov, and KCR_status arrays before writing (marks the start of a fresh DVAL accumulation interval). |
required |
Source code in src/zkSNARK.py
extract_state_estimate ¶
Parses prediction circuit outputs from a witness JSON and returns them as tensors.
Reads the rescaled outputs from 'pretty_elements', converts them to floats, and reshapes each into the padded tensor format used as inputs for the next prediction circuit call (i.e., with one trailing nonce column/row).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
SD
|
int
|
State space dimension. |
required |
MD
|
int
|
Measurement space dimension. |
required |
prediction_witness_path
|
str
|
Path to the prediction witness JSON file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
|
Source code in src/zkSNARK.py
dump_hypothesis_test_inputs ¶
Serializes hypothesis circuit inputs to a JSON file in the format expected by ezkl.
Flattens U and S and writes the input_data dict (containing dy, CPCT_cov, U, S, KCR_status) to data_path. The UCL is baked into the compiled circuit, so no CDF lookup table inputs are included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dy
|
list[float]
|
DVAL-summed innovation vector (flat). |
required |
CPCT_cov
|
list[float]
|
DVAL-summed innovation covariance (flat). |
required |
U
|
Tensor
|
Left singular vectors of CPCT_cov. |
required |
S
|
Tensor
|
Diagonal inverse square-root singular value matrix. |
required |
KCR_status
|
list[float]
|
Kalman check status as a flat float list. |
required |
data_path
|
str
|
Output path for the JSON input data file. |
required |
Source code in src/zkSNARK.py
extract_hypothesis_test_outputs ¶
Parses hypothesis circuit outputs from either a proof file or a witness file.
When gen_proof=True, reads public outputs from 'pretty_public_inputs' (proof format). When gen_proof=False, reads from 'pretty_elements' (witness format). Extracts the six circuit outputs in order: alarm status, SVD check, KCR check, chi-squared statistic, SVD diff, and SVD threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hypothesis_proof_path
|
str
|
Path to the proof JSON (gen_proof=True) or witness JSON (gen_proof=False). |
required |
gen_proof
|
bool
|
Determines which JSON key to use for output extraction. |
True
|
Returns:
| Type | Description |
|---|---|
|
Tuple[list, list, list, list, list, list]: (alarm_status_output, SVD_check_output, KCR_check_output, chi_square_test_statistic_output, svd_diff_output, svd_threshold_output) Each is a list of rescaled string values from ezkl's pretty output. |
Source code in src/zkSNARK.py
dump_check_results ¶
Writes SVD and Kalman check results to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output path for the JSON file. |
required |
svd_results
|
dict
|
Dictionary of check result data to serialize. |
required |
Source code in src/zkSNARK.py
Utilities¶
convert_tensor_to_float_array ¶
Flattens a sequence of tensors into a single Python list of floats.
Detaches each tensor from the computation graph, moves to CPU, flattens, concatenates, and converts to a list. Used to serialize circuit inputs and outputs into the JSON format expected by ezkl.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h_tensor
|
Sequence[Tensor]
|
A list or tuple of tensors to flatten. |
required |
Returns:
| Type | Description |
|---|---|
|
list[float]: Concatenated flat list of all tensor values. |
Source code in src/zkSNARK.py
dump_setup_timing_csv ¶
dump_setup_timing_csv(filename, model_export_time, settings_generation_time, compile_circuit_time, circuit_setup_time)
Appends a single-row timing record for the zkSNARK setup phase to a CSV file.
Creates the file with a header row on first call; subsequent calls append data only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the output CSV file. |
required |
model_export_time
|
float
|
Seconds taken to export the model to ONNX. |
required |
settings_generation_time
|
float
|
Seconds taken by ezkl.gen_settings(). |
required |
compile_circuit_time
|
float
|
Seconds taken by ezkl.compile_circuit(). |
required |
circuit_setup_time
|
float
|
Seconds taken by ezkl.setup() (VK/PK generation). |
required |
Source code in src/zkSNARK.py
dump_proving_times ¶
Appends per-period proving time metrics from a nested dict to a CSV file.
The dict maps period identifiers to dicts of metric name → value. Creates the header row on first call; subsequent calls append data rows only. Returns immediately if nested_dict is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the output CSV file. |
required |
nested_dict
|
dict
|
Mapping of {period: {metric_name: value, ...}}. |
required |
Source code in src/zkSNARK.py
print_section_header ¶
Prints a centered section header banner to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The section label to center in the banner. |
required |
width
|
int
|
Total banner width in characters. Default 80. |
80
|
char
|
str
|
Character used to draw the top and bottom border. Default '='. |
'='
|
Source code in src/zkSNARK.py
async_compile
async
¶
Async wrapper around ezkl.gen_witness() for use in an asyncio event loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str
|
Path to the JSON file containing circuit input data. |
required |
compiled_model_path
|
str
|
Path to the compiled ezkl circuit (.compiled). |
required |
witness_path
|
str
|
Output path for the generated witness JSON file. |
required |
Returns:
| Type | Description |
|---|---|
|
The result returned by ezkl.gen_witness(). |
Source code in src/zkSNARK.py
async_srs
async
¶
Async wrapper around ezkl.get_srs() for downloading the structured reference string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings_path
|
str
|
Path to the ezkl settings JSON file that specifies the required SRS size. |
required |
Returns:
| Type | Description |
|---|---|
|
The result returned by ezkl.get_srs(). |